<> <> <> <> DIRECTORY CD, CDBasics, CDCells, CDDirectory, CDInstances, CDOps, CDProperties, CDRects, Rope, CDUtil; CDUtilImpl: CEDAR PROGRAM IMPORTS CD, CDBasics, CDCells, CDDirectory, CDInstances, CDOps, CDProperties, CDRects, Rope EXPORTS CDUtil = BEGIN InstRec: TYPE = CDUtil.InstRec; Direction: TYPE = CDUtil.Direction; CreateSimpleCell: PUBLIC PROC [design: CD.Design, contents: LIST OF InstRec, cellName: Rope.ROPE _ NIL, direction: Direction _ right, alignHigh: BOOL _ FALSE] RETURNS [CD.Object] = { r, ir: CD.Rect _ [0, 0, 0, 0]; c: CD.Object = CDCells.CreateEmptyCell[]; inst: CD.Instance; pos: CD.Position _ [0, 0]; IF Rope.IsEmpty[cellName] THEN cellName _ "-SimpleCell"; FOR list: LIST OF InstRec _ contents, list.rest WHILE list#NIL DO IF list.first.ob=NIL THEN LOOP; ir _ CDBasics.MapRect[list.first.ob.bbox, [[0,0], list.first.orient]]; SELECT direction FROM right => { pos.x _ r.x2-ir.x1; IF alignHigh THEN pos.y _ -ir.y2 ELSE pos.y _ -ir.y1 }; left => { pos.x _ r.x1-ir.x2; IF alignHigh THEN pos.y _ -ir.y2 ELSE pos.y _ -ir.y1 }; up => { pos.y _ r.y2-ir.y1; IF alignHigh THEN pos.x _ -ir.x2 ELSE pos.x _ -ir.x1 }; down => { pos.y _ r.y1-ir.y2; IF alignHigh THEN pos.x _ -ir.x2 ELSE pos.x _ -ir.x1 }; none => pos _ [0, 0]; ENDCASE => ERROR; inst _ CDCells.IncludeOb[cell: c, ob: list.first.ob, trans: [[pos.x+list.first.dX, pos.y+list.first.dY], list.first.orient], mode: dontResize ].newInst; r _ CDInstances.InstRectI[inst]; IF list.first.properties#NIL THEN CDProperties.AppendProps[inst.properties, list.first.properties, inst]; IF ~Rope.IsEmpty[list.first.name] THEN CDProperties.PutInstanceProp[inst, $InstanceName, list.first.name]; ENDLOOP; [] _ CDCells.ResizeCell[NIL, c]; IF design#NIL THEN [] _ CDDirectory.Include[design, c, cellName]; RETURN[c] }; PartialCopy: PUBLIC PROC [ob: CD.Object, design: CD.Design, instances: LIST OF Rope.ROPE_NIL, remove: BOOL_TRUE, prop: ATOM _ $InstanceName, name: Rope.ROPE_NIL] RETURNS [copy: CD.Object_NIL] = { <<--copy the object; creates a cell >> <<--remove TRUE: >> <<--all the instances with property prop are and a value in instances are removed >> <<--remove FALSE: >> <<--only the instances with property prop are and a value in instances are copied >> ExpandToCell: PROC [ob: CD.Object, from: CD.Design] RETURNS [cell: CD.Object] = { <<--returns cell; however we dont care if cell is in design or not...>> cell _ ob; IF ~CDCells.IsCell[cell] THEN { cell _ CDDirectory.ExpandRecursed[ob, from, from]; IF cell=NIL OR ~CDCells.IsCell[cell] THEN RETURN [NIL]; } }; ShouldCopy: PROC[i: CD.Instance] RETURNS [copy: BOOL_TRUE] = { <<--accesses parameters of CopyInst>> ref: REF = CDProperties.GetInstanceProp[i, prop]; IF ref#NIL THEN { val: Rope.ROPE = CDOps.ToRope[ref]; FOR rl: LIST OF Rope.ROPE _ instances, rl.rest WHILE rl#NIL DO IF Rope.Equal[val, rl.first] THEN RETURN [copy _ ~remove]; ENDLOOP }; copy _ remove }; cell: CD.Object _ ExpandToCell[ob, design]; <> IF cell#NIL THEN { newList: CD.InstanceList _ NIL; cp, newCp: CD.CellSpecific; EachInst: CDCells.InstEnumerator = { IF ShouldCopy[inst] THEN newList _ CONS[CDInstances.Copy[inst], newList]; }; copy _ CDCells.CreateEmptyCell[]; cp _ NARROW[cell.specific]; newCp _ NARROW[copy.specific]; copy.bbox _ cell.bbox; CDProperties.CopyProps[cell.properties, copy]; newCp^ _ cp^; newCp.sequence _ NIL; [] _ CDCells.EnumerateInstances[cell, EachInst]; newCp.contents _ newList; <<--interest rect: by luck, the right thing happens even without coding>> [] _ CDCells.ResizeCell[NIL, copy]; IF design#NIL THEN [] _ CDDirectory.Include[design, copy, name]; } ELSE ERROR; }; FlattenChild: CD.DrawProc = { fRef: REF FlattenRec = NARROW[pr.devicePrivate]; newInst: CD.Instance _ NEW[CD.InstanceRep_[ob: ob, trans: trans, properties: CDProperties.DCopyProps[readOnlyInstProps]]]; IF fRef.flattenInst[newInst] THEN ob.class.drawMe[pr, ob, trans, newInst.properties] ELSE fRef.cellPtr.contents _ CONS[newInst, fRef.cellPtr.contents]; }; FlattenBareRect: PROC [pr: CD.DrawRef, r: CD.Rect, l: CD.Layer] = { fRef: REF FlattenRec = NARROW[pr.devicePrivate]; inst: CD.Instance _ NEW[CD.InstanceRep_[ ob: CDRects.CreateBareRect[size: CDBasics.SizeOfRect[r], l: l], trans: [CDBasics.BaseOfRect[r], original] ]]; fRef.cellPtr.contents _ CONS[inst, fRef.cellPtr.contents]; }; FlattenRec: TYPE = RECORD [ cell: CD.Object, cellPtr: CD.CellSpecific, flattenInst: CDUtil.InstPredicate ]; DefaultFlattenInst: CDUtil.InstPredicate = { IF ~inst.ob.class.composed THEN RETURN [FALSE]; IF CDProperties.GetObjectProp[inst.ob, $DontFlatten]#NIL THEN RETURN [FALSE]; RETURN [TRUE] }; Flatten: PUBLIC PROC [ob: CD.Object, design: CD.Design, flattenInst: CDUtil.InstPredicate_NIL] RETURNS [new: CD.Object_NIL] = { fRef: REF FlattenRec = NEW[FlattenRec_[ cell: CDCells.CreateEmptyCell[], cellPtr: NIL, flattenInst: IF flattenInst#NIL THEN flattenInst ELSE DefaultFlattenInst ]]; fPr: CD.DrawRef = CD.CreateDrawRef[[ design: design, drawRect: FlattenBareRect, drawChild: FlattenChild, selections: FALSE, devicePrivate: fRef, contextFilter: NEW[CD.ContextFilter_ALL[TRUE]] ]]; fRef.cellPtr _ NARROW[fRef.cell.specific]; ob.class.drawMe[ob: ob, pr: fPr]; IF ~CDCells.IsEmpty[fRef.cell] THEN { new _ fRef.cell; CDCells.SetInterestRect[NIL, new, CD.InterestRect[ob], doit]; IF design#NIL THEN { [] _ CDDirectory.FixChildren[new, design]; <<--actually, fixing children would also work for NIL design>> <<--but PW sometimes use NIL design and we don't want to remove>> <<--all children in this case>> } }; }; END.