diff --git a/InitGui.py b/InitGui.py index e352cbf..1494b58 100644 --- a/InitGui.py +++ b/InitGui.py @@ -70,7 +70,7 @@ class Lattice2Workbench (Workbench): import lattice2PopulateCopies as mod cmdsArrayTools = cmdsArrayTools + mod.exportedCommands - import lattice2Compose as mod + import lattice2PopulateChildren as mod cmdsArrayTools = cmdsArrayTools + mod.exportedCommands import lattice2Downgrade as mod diff --git a/lattice2Compose.py b/lattice2Compose.py deleted file mode 100644 index e770f9c..0000000 --- a/lattice2Compose.py +++ /dev/null @@ -1,224 +0,0 @@ -#*************************************************************************** -#* * -#* Copyright (c) 2015 - Victor Titov (DeepSOIC) * -#* * -#* * -#* This program is free software; you can redistribute it and/or modify * -#* it under the terms of the GNU Lesser General Public License (LGPL) * -#* as published by the Free Software Foundation; either version 2 of * -#* the License, or (at your option) any later version. * -#* for detail see the LICENCE text file. * -#* * -#* This program is distributed in the hope that it will be useful, * -#* but WITHOUT ANY WARRANTY; without even the implied warranty of * -#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * -#* GNU Library General Public License for more details. * -#* * -#* You should have received a copy of the GNU Library General Public * -#* License along with this program; if not, write to the Free Software * -#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * -#* USA * -#* * -#*************************************************************************** - -__title__="Lattice Compose object: combine elements of two lattices" -__author__ = "DeepSOIC" -__url__ = "" - -import math - -import FreeCAD as App -import Part - -from lattice2Common import * -import lattice2BaseFeature -import lattice2CompoundExplorer as LCE -import lattice2Executer - -# -------------------------- document object -------------------------------------------------- - -def makeCompose(name): - '''makeCompose(name): makes a Compose object.''' - return lattice2BaseFeature.makeLatticeFeature(name, Compose, ViewProviderCompose) - -class Compose(lattice2BaseFeature.LatticeFeature): - "The Lattice Compose object" - - operList = ['MultiplyPlacements','AveragePlacements', 'IgnoreBasePlacements','OverrideBasePlacements'] - - def derivedInit(self,obj): - self.Type = "LatticeCompose" - - obj.addProperty("App::PropertyEnumeration","Operation","Lattice Compose","Operation to perform between pairs of shapes") - - obj.Operation = Compose.operList - - - obj.addProperty("App::PropertyLink","Base","LatticeCompose Base","Base object. Usually a compound of generic shapes, but can be a lattice too.") - - obj.addProperty("App::PropertyBool","LoopSequence","LatticeCompose Base","If index goes out of range, apply modulo math.") - - obj.addProperty("App::PropertyBool","FlattenBaseHierarchy","LatticeCompose Base","Unpack subcompounds, to use all shapes, not just direct children.") - - obj.addProperty("App::PropertyBool","KeepBaseFirstItemPos","LatticeCompose Base","Apply extra transform, so that first item doesn't move.") - obj.KeepBaseFirstItemPos = False - - obj.addProperty("App::PropertyLink","Tool","LatticeCompose Tool","Tool object. Must be a lattice object. Contains placements to be applied.") - - obj.addProperty("App::PropertyBool","FlattenToolHierarchy","LatticeCompose Tool","Unpack subcompounds, to use all shapes, not just direct children.") - obj.FlattenToolHierarchy = True - - - def derivedExecute(self,obj): - # cache stuff - base = obj.Base.Shape - if base.ShapeType != 'Compound': - base = Part.makeCompound([base]) - if obj.FlattenBaseHierarchy: - baseChildren = LCE.AllLeaves(base) - else: - baseChildren = base.childShapes() - - tool = obj.Tool.Shape - if tool.ShapeType != 'Compound': - tool = Part.makeCompound([tool]) - if obj.FlattenToolHierarchy: - toolChildren = LCE.AllLeaves(tool) - else: - toolChildren = tool.childShapes() - - iBase = 0 - isMult = obj.Operation == 'MultiplyPlacements' # cache mode comparisons to speed them up - isAvg = obj.Operation == 'AveragePlacements' - isIgnore = obj.Operation == 'IgnoreBasePlacements' - isOverride = obj.Operation == 'OverrideBasePlacements' - - #mode validity logic - if not lattice2BaseFeature.isObjectLattice(obj.Tool): - lattice2Executer.warning(obj, 'Tool is not a lattice object. Results may be unexpected.\n') - outputIsLattice = lattice2BaseFeature.isObjectLattice(obj.Base) - if isOverride and outputIsLattice: - lattice2Executer.warning(obj, 'Base is a lattice object. OverrideBasePlacements operation requires a generic compound as Base. So, the lattice is being treated as a generic compound.\n') - outputIsLattice = False - - # initialize output containers and loop variables - outputShapes = [] #output list of shapes - outputPlms = [] #list of placements - bFirst = True - plmMatcher = App.Placement() #extra placement, that aligns first tool member and first base member - - - # the essence - for toolChild in toolChildren: - - # early test for termination - if iBase > len(baseChildren)-1: - if obj.LoopSequence: - iBase = 0 - else: - FreeCAD.Console.PrintWarning(obj.Name+': There are '+str(len(toolChildren)-len(baseChildren))+ - ' more placements in Tool than children in Base. Those placements'+ - ' were dropped.\n') - break - - #cache some stuff - basePlm = baseChildren[iBase].Placement - toolPlm = toolChild.Placement - - if not outputIsLattice: - outputShape = baseChildren[iBase].copy() - - #prepare alignment placement - if bFirst: - bFirst = False - if obj.KeepBaseFirstItemPos: - plmMatcher = toolPlm.inverse() - - #mode logic - if isMult: - outPlm = toolPlm.multiply(plmMatcher.multiply(basePlm)) - elif isAvg: - plm1 = toolPlm - plm2 = plmMatcher.multiply(basePlm) - transl = plm1.Base*0.5 + plm2.Base*0.5 - a1,b1,c1,d1 = plm1.Rotation.Q - a2,b2,c2,d2 = plm2.Rotation.Q - rot = App.Rotation(a1+a2,b1+b2,c1+c2,d1+d2) #no divide-by-two, because FreeCAD will normalize the quaternion automatically - outPlm = App.Placement(transl,rot) - elif isIgnore: - outPlm = toolPlm - elif isOverride: - assert(not outputIsLattice) - outputShape.transformShape(toolPlm.inverse.multiply(plmMatcher.multiply(basePlm))) - outPlm = toolPlm - - if outputIsLattice: - outputPlms.append(outPlm) - else: - outputShape.Placement = outPlm - outputShapes.append(outputShape) - - iBase += 1 - - if outputIsLattice: - return outputPlms - else: - obj.Shape = Part.makeCompound(outputShapes) - -class ViewProviderCompose(lattice2BaseFeature.ViewProviderLatticeFeature): - - def getIcon(self): - return getIconPath('Lattice2_Compose.svg') - - def claimChildren(self): - return [self.Object.Base, self.Object.Tool] - -# -------------------------- /document object -------------------------------------------------- - -# -------------------------- Gui command -------------------------------------------------- - -def CreateCompose(name): - sel = FreeCADGui.Selection.getSelectionEx() - FreeCAD.ActiveDocument.openTransaction("Create Compose") - FreeCADGui.addModule("lattice2Compose") - FreeCADGui.addModule("lattice2Executer") - FreeCADGui.doCommand("f = lattice2Compose.makeCompose(name='"+name+"')") - FreeCADGui.doCommand("f.Base = App.ActiveDocument."+sel[0].ObjectName) - FreeCADGui.doCommand("f.Tool = App.ActiveDocument."+sel[1].ObjectName) - FreeCADGui.doCommand("for child in f.ViewObject.Proxy.claimChildren():\n"+ - " child.ViewObject.hide()") - FreeCADGui.doCommand("lattice2Executer.executeFeature(f)") - FreeCADGui.doCommand("f = None") - FreeCAD.ActiveDocument.commitTransaction() - - -class _CommandCompose: - "Command to create Compose feature" - def GetResources(self): - return {'Pixmap' : getIconPath("Lattice2_Compose.svg"), - 'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_Compose","Compose arrays"), - 'Accel': "", - 'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_Compose","Lattice Compose: element-wise operations between compounds")} - - def Activated(self): - if len(FreeCADGui.Selection.getSelection()) == 2 : - CreateCompose(name = "Compose") - else: - mb = QtGui.QMessageBox() - mb.setIcon(mb.Icon.Warning) - mb.setText(translate("Lattice2_Compose", "Please select two objects, first. The fist object is Base, second is Tool. Base can contain real shapes, as well as be a lattice object. Tool is typically a lattice object.", None)) - mb.setWindowTitle(translate("Lattice2_Compose","Bad selection", None)) - mb.exec_() - - def IsActive(self): - if FreeCAD.ActiveDocument: - return True - else: - return False - -FreeCADGui.addCommand('Lattice2_Compose', _CommandCompose()) - -exportedCommands = ['Lattice2_Compose'] - -# -------------------------- /Gui command -------------------------------------------------- - diff --git a/lattice2PopulateChildren.py b/lattice2PopulateChildren.py new file mode 100644 index 0000000..5967c24 --- /dev/null +++ b/lattice2PopulateChildren.py @@ -0,0 +1,335 @@ +#*************************************************************************** +#* * +#* Copyright (c) 2015 - Victor Titov (DeepSOIC) * +#* * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU Lesser General Public License (LGPL) * +#* as published by the Free Software Foundation; either version 2 of * +#* the License, or (at your option) any later version. * +#* for detail see the LICENCE text file. * +#* * +#* This program is distributed in the hope that it will be useful, * +#* but WITHOUT ANY WARRANTY; without even the implied warranty of * +#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +#* GNU Library General Public License for more details. * +#* * +#* You should have received a copy of the GNU Library General Public * +#* License along with this program; if not, write to the Free Software * +#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +#* USA * +#* * +#*************************************************************************** + +__title__="Lattice PopulateWithChildren object: puts a copy of an object at every placement in a lattice object (makes the array real)." +__author__ = "DeepSOIC" +__url__ = "" + +import math + +import FreeCAD as App +import Part + +from lattice2Common import * +import lattice2BaseFeature +import lattice2CompoundExplorer as LCE +import lattice2Executer +from lattice2PopulateCopies import DereferenceArray + +# -------------------------- document object -------------------------------------------------- + +def makeLatticePopulateChildren(name): + '''makeLatticePopulateChildren(name): makes a LatticePopulateChildren object.''' + return lattice2BaseFeature.makeLatticeFeature(name, LatticePopulateChildren, ViewProviderLatticePopulateChildren) + +class LatticePopulateChildren(lattice2BaseFeature.LatticeFeature): + "The Lattice PopulateChildren object" + + def derivedInit(self,obj): + self.Type = "LatticePopulateChildren" + + obj.addProperty("App::PropertyLink","Object","Lattice PopulateChildren","Compound containing objects to be copied.") + + obj.addProperty("App::PropertyEnumeration","ObjectTraversal","Lattice PopulateChildren","Sets whether first-level compound is traversed, or the whole compounding tree.") + obj.ObjectTraversal = ["Direct children only","Recursive"] + obj.ObjectTraversal = "Direct children only" + + obj.addProperty("App::PropertyBool","LoopObjectSequence","Lattice PopulateChildren","If true, children of Object will be traversed in a loop if there are more placements than children. Otherwise, extra placements will be dropped.") + + obj.addProperty("App::PropertyEnumeration","Referencing","Lattice PopulateChildren","Reference for array of placements.") + obj.Referencing = ["Origin","First item", "Last item", "Use PlacementsFrom"] + + + obj.addProperty("App::PropertyLink","PlacementsTo","Lattice PopulateChildren", "Placement or array of placements, containing target locations.") + obj.addProperty("App::PropertyLink","PlacementsFrom", "Lattice PopulateChildren","Placement or array of placements to be treated as origins for PlacementsTo.") + + + def derivedExecute(self,obj): + + outputIsLattice = lattice2BaseFeature.isObjectLattice(obj.Object) + + if not lattice2BaseFeature.isObjectLattice(obj.Object): + if obj.ObjectTraversal == "Direct children only": + objectShapes = obj.Object.Shape.childShapes() + if obj.Object.Shape.ShapeType != "Compound": + lattice2Executer.warning(obj,"shape supplied as object is not a compound. It is going to be downgraded one level down (e.g, if it is a wire, the edges are going to be enumerated as children).") + elif obj.ObjectTraversal == "Recursive": + objectShapes = LCE.AllLeaves(obj.Object.Shape) + else: + raise ValueError("Traversal mode not implemented: "+obj.ObjectTraversal) + else: + objectPlms = lattice2BaseFeature.getPlacementsList(obj.Object, obj) + placements = lattice2BaseFeature.getPlacementsList(obj.PlacementsTo, obj) + + + # Precompute referencing + placements = DereferenceArray(obj, placements, obj.PlacementsFrom, obj.Referencing) + + + # initialize output containers and loop variables + outputShapes = [] #output list of shapes + outputPlms = [] #list of placements + iChild = 0 + numChildren = len(objectPlms) if outputIsLattice else len(objectShapes) + + # the essence + for iPlm in range(len(placements)): + if iChild > numChildren: + if obj.LoopObjectSequence: + iChild = 0 + else: + break + + plm = placements[iPlm] + + if outputIsLattice: + objectPlm = objectPlms[iChild] + outputPlms.append(plm.multiply(objectPlm)) + else: + outputShape = objectShapes[iChild] + outputShape.Placement = plm.multiply(outputShape.Placement) + outputShapes.append(outputShape) + + iChild += 1 + + if len(placements) > numChildren and not obj.LoopObjectSequence: + lattice2Executer.warning(obj,"There are fewer children to populate, than placements to be populated (%1, %2). Extra placements will be dropped.".replace("%1", str(numChildren)).replace("%2",str(len(placements)))) + + if len(placements) < numChildren: + lattice2Executer.warning(obj,"There are more children to populate, than placements to be populated (%1, %2). Extra children will be dropped.".replace("%1", str(numChildren)).replace("%2",str(len(placements)))) + + if outputIsLattice: + return outputPlms + else: + obj.Shape = Part.makeCompound(outputShapes) + return None + +class ViewProviderLatticePopulateChildren(lattice2BaseFeature.ViewProviderLatticeFeature): + + def getIcon(self): + if lattice2BaseFeature.isObjectLattice(self.Object): + return getIconPath( + {"Origin":"Lattice2_PopulateChildren_Plms_Normal.svg", + "First item":"Lattice2_PopulateChildren_Plms_Array.svg", + "Last item":"Lattice2_PopulateChildren_Plms_Array.svg", + "Use PlacementsFrom":"Lattice2_PopulateChildren_Plms_Move.svg", + }[self.Object.Referencing] + ) + else: + return getIconPath( + {"Origin":"Lattice2_PopulateChildren_Normal.svg", + "First item":"Lattice2_PopulateChildren_Array.svg", + "Last item":"Lattice2_PopulateChildren_Array.svg", + "Use PlacementsFrom":"Lattice2_PopulateChildren_Move.svg", + }[self.Object.Referencing] + ) + + def claimChildren(self): + children = [self.Object.Object, self.Object.PlacementsTo] + if self.Object.Referencing == "Use PlacementsFrom": + children.append(self.Object.PlacementsFrom) + return children + +# -------------------------- /document object -------------------------------------------------- + +# -------------------------- Gui command -------------------------------------------------- + + + +def CreateLatticePopulateChildren(name, label, shapeObj, latticeObjFrom, latticeObjTo, refmode): + '''utility function; sharing common code for all populate-Children commands''' + FreeCADGui.addModule("lattice2PopulateChildren") + FreeCADGui.addModule("lattice2Executer") + + #fill in properties + FreeCADGui.doCommand("f = lattice2PopulateChildren.makeLatticePopulateChildren(name='"+name+"')") + FreeCADGui.doCommand("f.Object = App.ActiveDocument."+shapeObj.Name) + FreeCADGui.doCommand("f.PlacementsTo = App.ActiveDocument."+latticeObjTo.Name) + if latticeObjFrom is not None: + FreeCADGui.doCommand("f.PlacementsFrom = App.ActiveDocument."+latticeObjFrom.Name) + FreeCADGui.doCommand("f.Referencing = "+repr(refmode)) + FreeCADGui.doCommand("f.Label = " + repr(label)) + + #execute + FreeCADGui.doCommand("lattice2Executer.executeFeature(f)") + + #hide something + if (refmode != "Origin" and refmode != "Use PlacementsFrom") or lattice2BaseFeature.isObjectLattice(shapeObj): + FreeCADGui.doCommand("f.Object.ViewObject.hide()") + FreeCADGui.doCommand("f.PlacementsTo.ViewObject.hide()") + if latticeObjFrom is not None: + FreeCADGui.doCommand("f.PlacementsFrom.ViewObject.hide()") + + #finalize + FreeCADGui.doCommand("Gui.Selection.addSelection(f)") + FreeCADGui.doCommand("f = None") + +def cmdPopulate_shapes_nonFromTo(refmode): + sel = FreeCADGui.Selection.getSelectionEx() + (lattices, shapes) = lattice2BaseFeature.splitSelection(sel) + if len(shapes) > 0 and len(lattices) == 1: + FreeCAD.ActiveDocument.openTransaction("Populate with Children") + lattice = lattices[0] + for shape in shapes: + CreateLatticePopulateChildren("Populate",u"Populate "+lattice.Object.Label+u" with "+shape.Object.Label,shape.Object,None,lattice.Object,refmode) + deselect(sel) + FreeCAD.ActiveDocument.commitTransaction() + elif len(shapes) == 1 and len(lattices) > 1: + shape = shapes[0] + FreeCAD.ActiveDocument.openTransaction("Populate with Children") + for lattice in lattices: + CreateLatticePopulateChildren("Populate",u"Populate "+lattice.Object.Label+u" with "+shape.Object.Label,shape.Object,None,lattice.Object,refmode) + deselect(sel) + FreeCAD.ActiveDocument.commitTransaction() + elif len(shapes) == 0 and len(lattices) == 2: + shape = lattices[0] + lattice = lattices[1] + FreeCAD.ActiveDocument.openTransaction("Populate with Children") + CreateLatticePopulateChildren("Populate",u"Populate "+lattice.Object.Label+u" with "+shape.Object.Label,shape.Object,None,lattice.Object,refmode) + deselect(sel) + FreeCAD.ActiveDocument.commitTransaction() + else: + raise SelectionError("Bad selection","Please select some shapes and some arrays, first. You can select multiple shapes and one array, or multiple arrays and one shape.") + +def cmdPopulate_shapes_FromTo(): + sel = FreeCADGui.Selection.getSelectionEx() + (lattices, shapes) = lattice2BaseFeature.splitSelection(sel) + if len(shapes) == 0 and len(sel) >= 3: + shapes = sel[:-2] + lattices = sel[-2:] + if len(shapes) > 0 and len(lattices) == 2: + FreeCAD.ActiveDocument.openTransaction("Populate with Children") + latticeFrom = lattices[0] + latticeTo = lattices[1] + for shape in shapes: + CreateLatticePopulateChildren("Populate",u"Moved "+shape.Object.Label, shape.Object, latticeFrom.Object, latticeTo.Object,"Use PlacementsFrom") + deselect(sel) + FreeCAD.ActiveDocument.commitTransaction() + else: + raise SelectionError("Bad selection","Please select either:\n one or more shapes, and two placements/arrays \nor\nthree placements/arrays") + + +class _CommandLatticePopulateChildren_Normal: + "Command to create LatticePopulateChildren feature" + def GetResources(self): + return {'Pixmap' : getIconPath("Lattice2_PopulateChildren_Normal.svg"), + 'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_PopulateChildren","Populate with Children"), + 'Accel': "", + 'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_PopulateChildren","Populate with Children: distribute children of an object to placements in an array. Select a compound to take children from, and a placement/array.")} + + def Activated(self): + try: + if len(FreeCADGui.Selection.getSelection())==0: + infoMessage("Populate with Children", + "Populate with Children command. Places each child of a selected object at different placements of an array.\n\n"+ + "Please select an object that is a compound, and an array of placements. Then invoke the command. It is also allowed to use another array of placements instead of compound.\n\n"+ + "A new compound will be created, with child shapes placed by corresponding placements of the array.") + return + cmdPopulate_shapes_nonFromTo("Origin") + except Exception as err: + msgError(err) + + def IsActive(self): + if FreeCAD.ActiveDocument: + return True + else: + return False + +FreeCADGui.addCommand('Lattice2_PopulateChildren_Normal', _CommandLatticePopulateChildren_Normal()) + +class _CommandLatticePopulateChildren_Array: + "Command to create LatticePopulateChildren feature" + def GetResources(self): + return {'Pixmap' : getIconPath("Lattice2_PopulateChildren_Array.svg"), + 'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_PopulateChildren","Populate with Children: Build Array"), + 'Accel': "", + 'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_PopulateChildren","Populate with Children: Build Array: poplate placements with children so that the first child is not moved. Select a compound, and a placement/array.")} + + def Activated(self): + try: + if len(FreeCADGui.Selection.getSelection())==0: + infoMessage("Populate with Children: Build Array", + "Populate with Children: Build Array command. Creates an array from children packed into a compound.\n\n"+ + "Please select a compound, and an array of placements. Then invoke the command. It is also allowed to use another array of placements instead of compound.\n\n"+ + "Compared to plain 'Populate With Children' command, the placements are treated as being relative to the first placement in the array. As a result, the first child always remains wher it was.") + return + cmdPopulate_shapes_nonFromTo("First item") + except Exception as err: + msgError(err) + + def IsActive(self): + if FreeCAD.ActiveDocument: + return True + else: + return False + +FreeCADGui.addCommand('Lattice2_PopulateChildren_Array', _CommandLatticePopulateChildren_Array()) + +class _CommandLatticePopulateChildren_Move: + "Command to create LatticePopulateChildren feature" + def GetResources(self): + return {'Pixmap' : getIconPath("Lattice2_PopulateChildren_Move.svg"), + 'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_PopulateChildren","Move children"), + 'Accel': "", + 'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_PopulateChildren","Move children: move children of compound from one placement to another placement. Select a compound, a placement/array to move from, and an array to move to.")} + + def Activated(self): + try: + if len(FreeCADGui.Selection.getSelection())==0: + infoMessage("Move children", + "Moved Children command. Creates a compound from another compound, by moving its children.\n\n"+ + "Each child is moved from one placement to another placement. Please select a compound, then a placement/array to move from, and an array of placements to move to (order matters).\n"+ + "An array of placements can be used insead of a compound.") + return + cmdPopulate_shapes_FromTo() + except Exception as err: + msgError(err) + + def IsActive(self): + if FreeCAD.ActiveDocument: + return True + else: + return False + +FreeCADGui.addCommand('Lattice2_PopulateChildren_Move', _CommandLatticePopulateChildren_Move()) + +class _CommandLatticePopulateChildrenGroup: + def GetCommands(self): + return ("Lattice2_PopulateChildren_Normal","Lattice2_PopulateChildren_Array","Lattice2_PopulateChildren_Move") + + def GetDefaultCommand(self): # return the index of the tuple of the default command. + return 0 + + def GetResources(self): + return { 'MenuText': 'Populate with Children:', + 'ToolTip': 'Populate with Children: put children of compound at corresponding placements in an array of placements.'} + + def IsActive(self): # optional + return True + +FreeCADGui.addCommand('Lattice2_PopulateChildrenGroupCommand',_CommandLatticePopulateChildrenGroup()) + +exportedCommands = ['Lattice2_PopulateChildrenGroupCommand'] + +# -------------------------- /Gui command -------------------------------------------------- + diff --git a/lattice2PopulateCopies.py b/lattice2PopulateCopies.py index d80e9cb..49d6d5c 100644 --- a/lattice2PopulateCopies.py +++ b/lattice2PopulateCopies.py @@ -35,6 +35,40 @@ import lattice2BaseFeature import lattice2CompoundExplorer as LCE import lattice2Executer +# ---------------------------shared code-------------------------------------- +def DereferenceArray(obj,placements, lnkFrom, refmode): + '''common implementation of treatment Referencing property. Returns a list of placements to use directly. + obj - feature being executed (used for error reporting; can be None) + placements - the array, converted into a list of placements. + lnkFrom - object linked as a lattice of 'from' placements. Can be None, if mode is not 'Use PlacemenetsFrom' + refmode - a string - enum property item''' + + plmDeref = App.Placement() #inverse placement of reference (reference is a substitute of origin) + if lnkFrom is not None and refmode != "Use PlacementsFrom": + lattice2Executer.warning(obj,"Referencing mode is '"+refmode+"', doesn't need PlacementsFrom link to be set. The link is set, but it will be ignored.") + if refmode == "Origin": + return placements + elif refmode == "First item": + plmDeref = placements[0].inverse() + elif refmode == "Last item": + plmDeref = placements[0].inverse() + elif refmode == "Use PlacementsFrom": + if lnkFrom is None: + raise ValueError("Referencing mode is 'Move from to', but PlacementsFrom link is not set.") + placementsFrom = lattice2BaseFeature.getPlacementsList(lnkFrom, obj) + if len(placementsFrom) == 1: + plmDeref = placementsFrom[0].inverse() + elif len(placementsFrom) == len(placements): + return [lattice2BaseFeature.makeMoveFromTo(placementsFrom[i], placements[i]) for i in range(0, len(placements))] + else: + latticeExecuter.warning(obj,"Lengths of arrays linked as PlacementsTo and PlacementsFrom must equal, or PlacementsFrom can be one placement. Violation: lengths are "+str(len(placements))+ " and "+str(len(placementsFrom))) + else: + raise ValueError("Referencing mode not implemented: "+refmode) + + return [plm.multiply(plmDeref) for plm in placements] + + + # -------------------------- document object -------------------------------------------------- def makeLatticePopulateCopies(name): @@ -68,30 +102,7 @@ class LatticePopulateCopies(lattice2BaseFeature.LatticeFeature): if outputIsLattice: objectPlms = lattice2BaseFeature.getPlacementsList(obj.Object,obj) - # Precompute referencing - plmDeref = App.Placement() #inverse placement of reference (reference is a substitute of origin) - if obj.PlacementsFrom is not None and obj.Referencing != "Use PlacementsFrom": - lattice2Executer.warning(obj,"Referencing mode is '"+obj.Referencing+"', doesn't need PlacementsFrom link to be set. The link is set, but it will be ignored.") - if obj.Referencing == "Origin": - pass - elif obj.Referencing == "First item": - plmDeref = placements[0].inverse() - elif obj.Referencing == "Last item": - plmDeref = placements[0].inverse() - elif obj.Referencing == "Use PlacementsFrom": - if obj.PlacementsFrom is None: - raise ValueError("Referencing mode is 'Move from to', but PlacementsFrom link is not set.") - placementsFrom = lattice2BaseFeature.getPlacementsList(obj.PlacementsFrom, obj) - if len(placementsFrom) == 1: - plmDeref = placementsFrom[0].inverse() - elif len(placementsFrom) == len(placements): - for i in range(0, len(placements)): - placements[i] = lattice2BaseFeature.makeMoveFromTo(placementsFrom[i],placements[i]) - else: - latticeExecuter.warning(obj,"Lengths of arrays linked as PlacementsTo and PlacementsFrom must equal, or PlacementsFrom can be one placement. Violation: lengths are "+str(len(placements))+ " and "+str(len(placementsFrom))) - else: - raise ValueError("Referencing mode not implemented: "+obj.Referencing) - + placements = DereferenceArray(obj, placements, obj.PlacementsFrom, obj.Referencing) # initialize output containers and loop variables outputShapes = [] #output list of shapes @@ -99,14 +110,13 @@ class LatticePopulateCopies(lattice2BaseFeature.LatticeFeature): # the essence for plm in placements: - refdPlm = plm.multiply(plmDeref) if outputIsLattice: for objectPlm in objectPlms: - outputPlms.append(refdPlm.multiply(objectPlm)) + outputPlms.append(plm.multiply(objectPlm)) else: outputShape = objectShape.copy() - outputShape.Placement = refdPlm.multiply(outputShape.Placement) + outputShape.Placement = plm.multiply(outputShape.Placement) outputShapes.append(outputShape) if outputIsLattice: