Skip to content

Commit

Permalink
assembly: fix adding constraint in sub-assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
realthunder committed Jun 22, 2018
1 parent 3427dc0 commit 735d6ac
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,9 @@ def getSelection(typeid=0,sels=None):
elementInfo = []
assembly = None
selSubname = None
infos = []
# first pass, collect hierarchy information, and find active assemble to
# use, i.e. which assembly to constraint
for sub in subs:
sobj = sel.Object.getSubObject(sub,1)
if not sobj:
Expand All @@ -1053,35 +1056,40 @@ def getSelection(typeid=0,sels=None):
raise RuntimeError('Selection {}.{} is not from an '
'assembly'.format(sel.Object.Name,sub))

if not assembly:
infos.append((sub,sobj,ret))

if isTypeOf(sobj,Assembly,True):
assembly = ret[-1].Assembly
if sub:
selSubname = sub
elif isTypeOf(sobj,(AsmConstraintGroup,AsmConstraint)):
assembly = ret[-1].Assembly
selSubname = sub[:-len(ret[-1].Subname)]
elif not assembly:
assembly = ret[0].Assembly
selSubname = sub[:-len(ret[0].Subname)]
found = ret[0]
else:
found = None
for r in ret:
if r.Assembly == assembly:
found = r
break
if not found:
raise RuntimeError('Selection {}.{} is not from the target '
'assembly {}'.format(
sel.Object.Name,sub,objName(assembly)))

if isTypeOf(sobj,Assembly,True):
continue
# second pass, collect element information
for sub,sobj,ret in infos:
found = None
for r in ret:
if r.Assembly == assembly:
found = r
break
if not found:
raise RuntimeError('Selection {}.{} is not from the target '
'assembly {}'.format(
sel.Object.Name,sub,objName(assembly)))

# check if the selection is a constraint group or a constraint
if isTypeOf(sobj,Assembly,True) or \
isTypeOf(sobj,(AsmConstraintGroup,AsmConstraint)):
if isTypeOf(sobj,AsmConstraint):
if cstr:
raise RuntimeError('more than one constraint selected')
assembly = ret[-1].Assembly
selSubname = sub[:-len(ret[-1].Subname)]
cstr = sobj
isTypeOf(sobj,AsmConstraintGroup):
continue

if isTypeOf(sobj,AsmConstraint):
if cstr:
raise RuntimeError('more than one constraint selected')
cstr = sobj
continue

# because we call Assembly.find() above with relativeToChild=False,
# we shall adjust the element subname by popping the first '.'
Expand Down Expand Up @@ -1148,9 +1156,10 @@ def make(typeid,sel=None,name='Constraint',undo=True):

if sel.SelObject:
FreeCADGui.Selection.clearSelection()
subname = sel.SelSubname
if subname:
subname += '.'
if sel.SelSubname:
subname = sel.SelSubname
else:
subname = ''
subname += sel.Assembly.Proxy.getConstraintGroup().Name + \
'.' + cstr.Name + '.'
FreeCADGui.Selection.addSelection(sel.SelObject,subname)
Expand Down

0 comments on commit 735d6ac

Please sign in to comment.