From cbc19a51971b014a3bddaa318215015b8e1ac926 Mon Sep 17 00:00:00 2001 From: Sebastian Hoogen Date: Sun, 13 Apr 2014 12:44:58 +0200 Subject: [PATCH] Allow selection of two objects for OpenSCAD.replaceobject --- src/Mod/OpenSCAD/OpenSCADCommands.py | 11 +++++++--- src/Mod/OpenSCAD/replaceobj.py | 32 +++++++++++++++++++++------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/Mod/OpenSCAD/OpenSCADCommands.py b/src/Mod/OpenSCAD/OpenSCADCommands.py index 02d17be8f151..45b90ed7e38b 100644 --- a/src/Mod/OpenSCAD/OpenSCADCommands.py +++ b/src/Mod/OpenSCAD/OpenSCADCommands.py @@ -183,12 +183,17 @@ def GetResources(self): class ReplaceObject: def IsActive(self): - return FreeCADGui.Selection.countObjectsOfType('Part::Feature') == 3 + nobj = FreeCADGui.Selection.countObjectsOfType('Part::Feature') + if nobj == 3: return True + elif nobj == 2: return tuple((len(obj.InList)) for obj in \ + FreeCADGui.Selection.getSelection()) in ((0,1),(1,0)) + #else: return False + def Activated(self): import replaceobj - #objs=[selobj.Object for selobj in FreeCADGui.Selection.getSelectionEx()] objs=FreeCADGui.Selection.getSelection() - if len(objs)==3: + if len(objs)==3 or \ + tuple((len(obj.InList)) for obj in objs) in ((0,1),(1,0)): replaceobj.replaceobjfromselection(objs) else: FreeCAD.Console.PrintError(unicode(translate('OpenSCAD',\ diff --git a/src/Mod/OpenSCAD/replaceobj.py b/src/Mod/OpenSCAD/replaceobj.py index 384267b22183..290109303d4e 100644 --- a/src/Mod/OpenSCAD/replaceobj.py +++ b/src/Mod/OpenSCAD/replaceobj.py @@ -44,15 +44,31 @@ def replaceobj(parent,oldchild,newchild): parent.touch() def replaceobjfromselection(objs): - assert(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: assert(False) + # The Parent can be ommited as long as one object is orphaned + 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. Please select oldchild,\ + newchild 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 determin current parent-child relationship") + else: + raise ValueError("Wrong number of selected objects") replaceobj(parent,oldchild,newchild) + parent.Document.recompute() if __name__ == '__main__': import FreeCAD,FreeCADGui