| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | __title__ = "FreeCAD OpenSCAD Workbench - replace object function" |
| | __author__ = "Sebastian Hoogen" |
| | __url__ = ["https://www.freecad.org"] |
| |
|
| | ''' |
| | This functions allows one to replace an object in the feature hierarchy |
| | ''' |
| |
|
| |
|
| | def replaceobj(parent, oldchild, newchild): |
| | for propname in parent.PropertiesList: |
| | propvalue = parent.getPropertyByName(propname) |
| | if type(propvalue) == list: |
| | bModified = False |
| | for dontcare in range(propvalue.count(oldchild)): |
| | propvalue[propvalue.index(oldchild)] = newchild |
| | bModified = True |
| | if bModified: |
| | if propname == "ExpressionEngine": |
| | |
| | FreeCAD.Console.PrintWarning("Expressions in "+parent.Name+" need to be modified, but they were not. Please do that manually.") |
| | continue |
| | setattr(parent,propname,propvalue) |
| | else: |
| | if propvalue == oldchild: |
| | setattr(parent,propname,newchild) |
| | print(propname, parent.getPropertyByName(propname)) |
| | |
| | parent.touch() |
| |
|
| | def replaceobjfromselection(objs): |
| | |
| | if len(objs) == 2: |
| | InListLength = tuple((len(obj.InList)) for obj in objs) |
| | if InListLength == (0,1): |
| | newchild,oldchild = objs |
| | parent = oldchild.InList[0] |
| | elif InListLength == (1,0): |
| | oldchild,newchild = objs |
| | parent = oldchild.InList[0] |
| | else: |
| | raise ValueError("Selection ambiguous. Select old child,\ |
| | new child, and parent") |
| | elif len(objs) == 3: |
| | if objs[2] in objs[0].InList: oldchild, newchild, parent = objs |
| | elif objs[0] in objs[1].InList: parent, oldchild, newchild = objs |
| | elif objs[0] in objs[2].InList: parent, newchild, oldchild = objs |
| | elif objs[1] in objs[0].InList: oldchild, parent, newchild = objs |
| | elif objs[1] in objs[2].InList: newchild, parent, oldchild = objs |
| | elif objs[2] in objs[1].InList: newchild, oldchild, parent = objs |
| | else: |
| | raise ValueError("Cannot determine current parent-child relationship") |
| | else: |
| | raise ValueError("Wrong number of selected objects") |
| | replaceobj(parent,oldchild,newchild) |
| | parent.Document.recompute() |
| |
|
| |
|
| | if __name__ == '__main__': |
| | import FreeCAD |
| | import FreeCADGui |
| | objs = [selobj.Object for selobj in FreeCADGui.Selection.getSelectionEx()] |
| | replaceobjfromselection(objs) |
| |
|