Skip to content

Commit

Permalink
Allow selection of two objects for OpenSCAD.replaceobject
Browse files Browse the repository at this point in the history
  • Loading branch information
5263 authored and yorikvanhavre committed Apr 14, 2014
1 parent c860cca commit cbc19a5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
11 changes: 8 additions & 3 deletions src/Mod/OpenSCAD/OpenSCADCommands.py
Expand Up @@ -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',\
Expand Down
32 changes: 24 additions & 8 deletions src/Mod/OpenSCAD/replaceobj.py
Expand Up @@ -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
Expand Down

0 comments on commit cbc19a5

Please sign in to comment.