<> <> <> <> DIRECTORY CD, CDApplications USING [BoundingRectO], CDBasics, CDCallSpecific, CDCells, CDDirectory, CDEnvironment, CDIO, CDOrient, CDProperties, CDRepetitions, CDInterestRects, TokenIO; CDRepetitionsImpl: CEDAR PROGRAM IMPORTS CD, CDRepetitions, CDApplications, CDBasics, CDCallSpecific, CDCells, CDDirectory, CDEnvironment, CDIO, CDOrient, CDProperties, TokenIO, CDInterestRects EXPORTS CDRepetitions SHARES CDDirectory = BEGIN OldSetInterest: PROC [ob: CD.ObPtr, r: CD.DesignRect] = BEGIN cptr: RepPtr = NARROW[ob.specificRef]; cptr.ir _ r; END; RepPtr: TYPE = CDRepetitions.RepPtr; RepRec: TYPE = CDRepetitions.RepRec; pForRepetitions: REF CD.ObjectProcs = CD.RegisterObjectType[$Repetitions]; ComputeBoundsR: PROC [ob: CD.ObPtr] RETURNS [CD.DesignRect] = BEGIN rp: RepPtr = NARROW[ob.specificRef]; sz: CD.DesignPosition = CDOrient.OrientedSize[rp.ob.size, rp.orientation]; toffset: CD.DesignPosition = [(rp.count-1)*rp.offset.x, (rp.count-1)*rp.offset.y]; r1: CD.DesignRect = CDBasics.RectAt[rp.offsetOfFirst, sz]; r2: CD.DesignRect = CDBasics.RectAt[CDBasics.AddPoints[rp.offsetOfFirst, toffset], sz]; RETURN CDBasics.Surround[r1, r2]; END; InternalCreateRepetition: PROC [ob: CD.ObPtr, count: NAT, offset: CD.DesignPosition, orientation: CD.Orientation] RETURNS [CD.ObPtr] = BEGIN repOb: CD.ObPtr = NEW[CD.ObjectDefinition]; rp: RepPtr = NEW[RepRec]; sz: CD.DesignPosition = CDOrient.OrientedSize[ob.size, orientation]; repOb.p _ pForRepetitions; count _ MAX[1, MIN[count, 512]]; repOb.size.x _ sz.x+(count-1)*ABS[offset.x]; repOb.size.y _ sz.y+(count-1)*ABS[offset.y]; repOb.layer _ ob.layer; rp.ob _ ob; rp.offsetOfFirst _ [0, 0]; IF offset.x<0 THEN rp.offsetOfFirst.x_ -(count-1)*offset.x; IF offset.y<0 THEN rp.offsetOfFirst.y_ -(count-1)*offset.y; rp.orientation _ orientation; rp.offset _ offset; rp.count _ count; repOb.specificRef _ rp; BEGIN iR: CD.DesignRect _ CDOrient.DeMapRect[ itemInWorld: iR, cellSize: ob.size, cellInstOrient: orientation ]; iR.x2 _ iR.x2+(count-1)*ABS[offset.x]; iR.y2 _ iR.y2+(count-1)*ABS[offset.y]; rp.ir _ iR END; RETURN [repOb]; END; CreateRepetition: PUBLIC PROC [design: CD.Design, ob: CD.ObPtr, count: NAT, offset: CD.DesignPosition, orientation: CD.Orientation_0] RETURNS [CD.ObPtr] = BEGIN repOb: CD.ObPtr = InternalCreateRepetition[ob, count, offset, orientation]; [] _ CDDirectory.Include[design, repOb]; RETURN [repOb]; END; DrawMeForRepetitions: PROC [aptr: CD.ApplicationPtr, pos: CD.DesignPosition, orient: CD.Orientation, pr: CD.DrawRef] = BEGIN ENABLE UNWIND => IF pr.nesting.table[pr.nestDepth_1]=aptr THEN pr.nestDepth _ pr.nestDepth-1; rptr: RepPtr _ NARROW[aptr.ob.specificRef]; rel, sz: CD.DesignPosition; o: CD.Orientation _ CDOrient.ComposeOrient[rptr.orientation, orient]; dummyApp: CD.ApplicationPtr _ NEW[CD.Application]; xi: REF INT; dummyApp.ob _ rptr.ob; rel _ rptr.offsetOfFirst; sz _ CDOrient.OrientedSize[rptr.ob.size, rptr.orientation]; pr.nesting.table[pr.nestDepth] _ aptr; pr.nestDepth _ pr.nestDepth+1; <<--hang two properties on the application; both pointing to the same int containing the >> <<--repetition number;>> <<--the first property has the name $CDxIndex>> <<--the second property name is the value of the property $CDxIndexProperty >> WITH CDProperties.GetPropFromApplication[aptr, $CDxIndex] SELECT FROM intP: REF INT => xi _ intP; ENDCASE => { xi _ NEW[INT]; CDProperties.PutPropOnApplication[aptr, $CDxIndex, xi]; WITH CDProperties.GetPropFromApplication[aptr, $CDxIndexProperty] SELECT FROM ixP: ATOM => { CDProperties.PutPropOnApplication[aptr, ixP, xi]; }; ENDCASE => NULL }; <<--do drawing>> FOR i: INT IN [0..rptr.count) DO r: CD.DesignRect _ CDOrient.MapRect[ itemInCell: CDBasics.RectAt[rel, sz], cellSize: aptr.ob.size, cellInstOrient: orient, cellInstPos: pos ]; IF pr.stopFlag^ THEN EXIT; xi^ _ i; pr.drawChild[dummyApp, CDBasics.BaseOfRect[r], o, pr]; rel _ CDBasics.AddPoints[rel, rptr.offset]; ENDLOOP; pr.nestDepth _ pr.nestDepth-1; END; QuickDrawMeForRepetitions: PROC [aptr: CD.ApplicationPtr, pos: CD.DesignPosition, orient: CD.Orientation, pr: CD.DrawRef] = BEGIN ENABLE UNWIND => IF pr.nesting.table[pr.nestDepth_1]=aptr THEN pr.nestDepth _ pr.nestDepth-1; rptr: RepPtr _ NARROW[aptr.ob.specificRef]; rel, sz: CD.DesignPosition; o: CD.Orientation _ CDOrient.ComposeOrient[rptr.orientation, orient]; dummyApp: CD.ApplicationPtr _ NEW[CD.Application]; xi: REF INT; dummyApp.ob _ rptr.ob; rel _ rptr.offsetOfFirst; sz _ CDOrient.OrientedSize[rptr.ob.size, rptr.orientation]; pr.nesting.table[pr.nestDepth] _ aptr; pr.nestDepth _ pr.nestDepth+1; <<--hang two properties on the application; both pointing to the same int containing the >> <<--repetition number;>> <<--the first property has the name $CDxIndex>> <<--the second property name is the value of the property $CDxIndexProperty >> WITH CDProperties.GetPropFromApplication[aptr, $CDxIndex] SELECT FROM intP: REF INT => xi _ intP; ENDCASE => { xi _ NEW[INT]; CDProperties.PutPropOnApplication[aptr, $CDxIndex, xi]; WITH CDProperties.GetPropFromApplication[aptr, $CDxIndexProperty] SELECT FROM ixP: ATOM => { CDProperties.PutPropOnApplication[aptr, ixP, xi]; }; ENDCASE => NULL }; <<--do drawing>> FOR i: INT IN [0..rptr.count) DO r: CD.DesignRect _ CDOrient.MapRect[ itemInCell: CDBasics.RectAt[rel, sz], cellSize: aptr.ob.size, cellInstOrient: orient, cellInstPos: pos ]; IF pr.stopFlag^ THEN EXIT; xi^ _ i; rptr.ob.p.quickDrawMe[dummyApp, CDBasics.BaseOfRect[r], o, pr]; rel _ CDBasics.AddPoints[rel, rptr.offset]; ENDLOOP; pr.nestDepth _ pr.nestDepth-1; END; Another: PROC [me: CD.ObPtr, from, to: CD.Design] RETURNS [CD.ObPtr] = BEGIN rp: RepPtr = NARROW[me.specificRef]; RETURN [CreateRepetition[to, rp.ob, rp.count, rp.offset, rp.orientation]] END; IncCount: CDCallSpecific.CallProc -- PROC [design: CD.Design, aptr: CD.ApplicationPtr, x: REF] RETURNS [done: BOOL_TRUE, removeMe: BOOL_FALSE, include: CD.ApplicationList_NIL, repaintMe: BOOL_FALSE, repaintInclude: BOOL_FALSE] -- = BEGIN sz: CD.DesignPosition _ aptr.ob.size; ap: CD.ApplicationPtr _ NEW[CD.Application_aptr^]; rptr: RepPtr _ NARROW[aptr.ob.specificRef]; newOb: CD.ObPtr = CreateRepetition[design, rptr.ob, rptr.count+1, rptr.offset, rptr.orientation]; IF ap.ob.size#newOb.size THEN { relativeNewBound: CD.DesignRect _ [x1: 0, y1: 0, x2: aptr.ob.size.x, y2: aptr.ob.size.y]; IF rptr.offset.x>=0 THEN relativeNewBound.x2 _ aptr.ob.size.x+rptr.offset.x ELSE relativeNewBound.x1 _ rptr.offset.x; IF rptr.offset.y>=0 THEN relativeNewBound.y2 _ aptr.ob.size.y+rptr.offset.y ELSE relativeNewBound.y1 _ rptr.offset.y; ap.location _ CDBasics.BaseOfRect[ CDOrient.MapRect[ itemInCell: relativeNewBound, cellSize: aptr.ob.size, cellInstOrient: aptr.orientation, cellInstPos: aptr.location]]; }; ap.ob _ newOb; include _ LIST[ap]; repaintMe _ TRUE; removeMe _ TRUE; repaintInclude _ TRUE; END; DecCount: CDCallSpecific.CallProc -- PROC [design: CD.Design, aptr: CD.ApplicationPtr, x: REF] RETURNS [done: BOOL_TRUE, removeMe: BOOL_FALSE, include: CD.ApplicationList_NIL, repaintMe: BOOL_FALSE, repaintInclude: BOOL_FALSE] -- = BEGIN sz: CD.DesignPosition _ aptr.ob.size; ap: CD.ApplicationPtr _ NEW[CD.Application_aptr^]; rptr: RepPtr _ NARROW[aptr.ob.specificRef]; newOb: CD.ObPtr = CreateRepetition[design, rptr.ob, rptr.count-1, rptr.offset, rptr.orientation]; IF ap.ob.size#newOb.size THEN { relativeNewBound: CD.DesignRect _ [x1: 0, y1: 0, x2: aptr.ob.size.x, y2: aptr.ob.size.y]; IF rptr.offset.x>=0 THEN relativeNewBound.x2 _ aptr.ob.size.x-rptr.offset.x ELSE relativeNewBound.x1 _ -rptr.offset.x; IF rptr.offset.y>=0 THEN relativeNewBound.y2 _ aptr.ob.size.y-rptr.offset.y ELSE relativeNewBound.y1 _ -rptr.offset.y; ap.location _ CDBasics.BaseOfRect[ CDOrient.MapRect[ itemInCell: relativeNewBound, cellSize: aptr.ob.size, cellInstOrient: aptr.orientation, cellInstPos: aptr.location]]; }; ap.ob _ newOb; include _ LIST[ap]; repaintMe _ TRUE; removeMe _ TRUE; repaintInclude _ TRUE; END; Expand: PROC [me: CD.ObPtr, from, to: CD.Design] RETURNS [CD.ObPtr] = BEGIN rptr: RepPtr = NARROW[me.specificRef]; cob: CD.ObPtr = CreateRepetitionCell[design: to, ob: rptr.ob, count: rptr.count, offset: rptr.offset, orientation: rptr.orientation ]; RETURN [cob] END; TransformToCell: CDCallSpecific.CallProc -- PROC [design: CD.Design, aptr: CD.ApplicationPtr, x: REF] RETURNS [done: BOOL_TRUE, removeMe: BOOL_FALSE, include: CD.ApplicationList_NIL, repaintMe: BOOL_FALSE, repaintInclude: BOOL_FALSE] -- = BEGIN cob: CD.ObPtr _ NIL; IF CDRepetitions.IsRepetition[aptr.ob] THEN cob _ Expand[aptr.ob, design, design]; done _ cob#NIL; IF done THEN { removeMe _ TRUE; repaintMe _ TRUE; include _ LIST[NEW[CD.Application _ [ ob: cob, location: aptr.location, orientation: aptr.orientation, selected: aptr.selected, properties: CDProperties.CopyProps[aptr.properties] ]]]; repaintInclude _ TRUE; } END; EnumerateRepeatedObjects: PROC [me: CD.ObPtr, p: CDDirectory.EnumerateObjectsProc, x: REF] = BEGIN rptr: RepPtr = NARROW[me.specificRef]; p[rptr.ob, x]; END; ReadRepetition: CD.InternalReadProc --PROC [] RETURNS [ObPtr]-- = BEGIN ob: CD.ObPtr = CDIO.ReadObject[]; count: NAT = TokenIO.ReadInt[]; offsetx: INT = TokenIO.ReadInt[]; offsety: INT = TokenIO.ReadInt[]; orientation: CD.Orientation = CDIO.ReadOrientation[]; RETURN [ InternalCreateRepetition[ ob: ob, count: count, offset: [offsetx, offsety], orientation: orientation ]]; END; WriteRepetition: CD.InternalWriteProc -- PROC [me: ObPtr] -- = BEGIN specific: RepPtr = NARROW[me.specificRef]; CDIO.WriteObject[specific.ob]; TokenIO.WriteInt[specific.count]; TokenIO.WriteInt[specific.offset.x]; TokenIO.WriteInt[specific.offset.y]; CDIO.WriteOrientation[specific.orientation]; END; CreateRepetitionCell: PUBLIC PROC [design: CD.Design, ob: CD.ObPtr, count: NAT, offset: CD.DesignPosition, orientation: CD.Orientation_0] RETURNS [CD.ObPtr] = BEGIN ap: CD.ApplicationPtr; pos: CD.DesignPosition _ [0, 0]; co: CD.ObPtr = CDCells.CreateEmptyCell[]; cp: CD.CellPtr = NARROW[co.specificRef]; count _ MAX[count, 1]; IF offset.x<0 THEN {pos.x _ -offset.x*(count-1)}; IF offset.y<0 THEN {pos.y _ -offset.y*(count-1)}; FOR i: NAT IN [1..count] DO ap _ NEW[CD.Application_[ob: ob, location: pos, orientation: orientation, selected: FALSE]]; pos _ CDBasics.AddPoints[pos, offset]; cp.contents _ CONS[ap, cp.contents] ENDLOOP; co.size _ CDBasics.SizeOfRect[CDApplications.BoundingRectO[cp.contents]]; BEGIN iR: CD.DesignRect _ CDOrient.MapRect[ itemInCell: iR, cellSize: ob.size, cellInstOrient: orientation ]; iR.x2 _ iR.x2+(count-1)*ABS[offset.x]; iR.y2 _ iR.y2+(count-1)*ABS[offset.y]; cp.ir _ cp.dIr _ iR; END; IF design#NIL THEN [] _ CDDirectory.Include[design, co, "-repetition"]; RETURN [co]; END; ReplaceDirectChildForReps: CDDirectory.ReplaceDChildsProc = BEGIN AdjustItself: PROC [me: CD.ObPtr] = BEGIN rp: RepPtr = NARROW[me.specificRef]; rp.offsetOfFirst _ [0, 0]; IF rp.offset.x<0 THEN rp.offsetOfFirst.x_ -(rp.count-1)*rp.offset.x; IF rp.offset.y<0 THEN rp.offsetOfFirst.y_ -(rp.count-1)*rp.offset.y; END; found: BOOL _ FALSE; rp: CDRepetitions.RepPtr = NARROW[me.specificRef]; FOR replaceList: CDDirectory.ReplaceList _ replace, replaceList.rest WHILE replaceList#NIL DO rep: REF CDDirectory.ReplaceRec = replaceList.first; IF rep.old=rp.ob THEN { IF rep.newSize#rep.oldSize OR rep.off#[0, 0] THEN { rp.offsetOfFirst _ CDBasics.BaseOfRect[ CDOrient.MapRect[ itemInCell: CDBasics.MoveRect[ [x1: 0, y1: 0, x2: rep.newSize.x, y2: rep.newSize.y], rep.off], cellSize: rep.oldSize, cellInstOrient: rp.orientation, cellInstPos: rp.offsetOfFirst]]; rp.ob _ rep.new; found _ TRUE; EXIT } }; ENDLOOP; IF found THEN { newR: CD.DesignRect _ ComputeBoundsR[me]; IF newR#CDBasics.RectAt[[0, 0], me.size] THEN { newSize: CD.DesignPosition _ CDBasics.SizeOfRect[newR]; oldSize: CD.DesignPosition _ me.size; newBase: CD.DesignPosition = CDBasics.BaseOfRect[newR]; AdjustItself[me]; me.size _ newSize; IF design#NIL THEN CDDirectory.RepositionObject[ design: design, ob: me, oldSize: oldSize, baseOff: newBase ] } } END; Index: PUBLIC PROC [environment: CD.DrawRef, skipInnerMost: NAT_0, ifNotFound: INT_0] RETURNS [INT] = BEGIN RETURN [CDEnvironment.GetIntProp[environment, $CDxIndex, skipInnerMost, ifNotFound]] END; InitRepetitions: PROC [] = BEGIN rp: REF CDDirectory.DirectoryProcs = CDDirectory.InstallDirectoryProcs[pForRepetitions]; rp.enumerateChildObjects _ EnumerateRepeatedObjects; rp.replaceDirectChilds _ ReplaceDirectChildForReps; rp.expand _ Expand; rp.another _ Another; pForRepetitions.drawMe _ DrawMeForRepetitions; pForRepetitions.quickDrawMe _ QuickDrawMeForRepetitions; pForRepetitions.internalRead _ ReadRepetition; pForRepetitions.internalWrite _ WriteRepetition; CDInterestRects.InstallOldSetInterest[pForRepetitions, OldSetInterest]; CDCallSpecific.Register[$TransformToCell, pForRepetitions, TransformToCell]; CDCallSpecific.Register[$IncCount, pForRepetitions, IncCount]; CDCallSpecific.Register[$DecCount, pForRepetitions, DecCount]; END; InitRepetitions[]; END.