From 735d6ac260e7b2af9cb4a9cf7acc3263ff8305c5 Mon Sep 17 00:00:00 2001 From: "Zheng, Lei" Date: Sat, 23 Jun 2018 07:52:21 +0800 Subject: [PATCH] assembly: fix adding constraint in sub-assembly --- assembly.py | 61 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/assembly.py b/assembly.py index e5e3b3b..f8ea44c 100644 --- a/assembly.py +++ b/assembly.py @@ -1042,6 +1042,9 @@ class AsmConstraint(AsmGroup): 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: @@ -1053,35 +1056,40 @@ class AsmConstraint(AsmGroup): raise RuntimeError('Selection {}.{} is not from an ' 'assembly'.format(sel.Object.Name,sub)) - if 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))) + infos.append((sub,sobj,ret)) if isTypeOf(sobj,Assembly,True): - continue + 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)] + + # 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 '.' @@ -1148,9 +1156,10 @@ class AsmConstraint(AsmGroup): 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)