def IsActive(self): # we only insert variant links into assemblies and root parts if Asm4.getAssembly() or Asm4.getSelectedRootPart(): return True # if an existing variant link is selected, we duplicate it if Asm4.getSelectedVarLink(): return True return False
def IsActive(self): # if an App::Link is selected, even a broken one if Gui.Selection.getSelection() and Gui.Selection.getSelection()[0].isDerivedFrom('App::Link'): return True # there is an assembly or a root App::Part is selected elif Asm4.getAssembly() or Asm4.getSelectedRootPart(): return True return False
def Activated(self): # This function is executed when the command is activated self.UI = QtGui.QDialog() self.drawUI() # initialise stuff self.activeDoc = App.ActiveDocument self.rootAssembly = None self.origLink = None self.brokenLink = False self.allParts = [] self.partsDoc = [] self.filterPartList.clear() self.partList.clear() self.linkNameInput.clear() # if an Asm4 Assembly is present, that's where we put the variant link if Asm4.getAssembly(): self.rootAssembly = Asm4.getAssembly() # an App::Part at the root of the document is selected, we insert the link there elif Asm4.getSelectedRootPart(): self.rootAssembly = Asm4.getSelectedRootPart() # if a variant link is selected, we see if we can duplicate it if Asm4.getSelectedVarLink(): selObj = Asm4.getSelectedVarLink() parent = selObj.getParentGeoFeatureGroup() # if the selected link is in a root App::Part if parent.TypeId == 'App::Part' and parent.getParentGeoFeatureGroup() is None: self.rootAssembly = parent self.origLink = selObj self.origPart = selObj.SourceObject ''' # if a broken link is selected elif len(Gui.Selection.getSelection())==1 : selObj = Gui.Selection.getSelection()[0] if selObj.isDerivedFrom('App::Link') and selObj.LinkedObject is None: parent = selObj.getParentGeoFeatureGroup() # if the selected (broken) link is in a root App::Part if parent.TypeId == 'App::Part' and parent.getParentGeoFeatureGroup() is None: self.brokenLink = True self.rootAssembly = parent self.origLink = selObj self.UI.setWindowTitle('Re-link broken link') self.insertButton.setText('Replace') self.linkNameInput.setText(Asm4.labelName(selObj)) self.linkNameInput.setEnabled(False) ''' if self.rootAssembly is None: Asm4.warningBox( 'Please create an Assembly' ) return # Search for all App::Parts having a "Variables" property container in all open documents # Also store the document of the part for doc in App.listDocuments().values(): # don't consider temporary documents if not doc.Temporary: for obj in doc.findObjects("App::Part"): # we don't want to link to itself to the 'Model' object # other App::Part in the same document are OK # but only those at top level (not nested inside other containers) if obj != self.rootAssembly and obj.getParentGeoFeatureGroup() is None: # and only those that have a Variables property container variables = obj.getObject('Variables') if hasattr(variables,'Type') and variables.Type=='App::PropertyContainer': self.allParts.append( obj ) self.partsDoc.append( doc ) # build the list for part in self.allParts: newItem = QtGui.QListWidgetItem() newItem.setText( part.Document.Name +"#"+ Asm4.labelName(part) ) newItem.setIcon(part.ViewObject.Icon) self.partList.addItem(newItem) # if an existing valid App::Link was selected if self.origLink and not self.brokenLink: origPart = self.origLink.SourceObject # try to find the original part of the selected link origPartText = origPart.Document.Name +"#"+ Asm4.labelName(origPart) # MatchExactly, MatchContains, MatchEndsWith, MatchStartsWith ... partFound = self.partList.findItems( origPartText, QtCore.Qt.MatchExactly ) if partFound: self.partList.setCurrentItem(partFound[0]) # self.onItemClicked(partFound[0]) # if the last character is a number, we increment this number origName = self.origLink.Label lastChar = origName[-1] if lastChar.isnumeric(): (rootName,sep,num) = origName.rpartition('_') proposedLinkName = Asm4.nextInstance(rootName,startAtOne=True) # else we take the next instance else: proposedLinkName = Asm4.nextInstance(origName) # set the proposed name in the entry field if not self.brokenLink: self.linkNameInput.setText( proposedLinkName ) # show the UI self.UI.show()