Esempio n. 1
0
 def onItemClicked( self, item ):
     for selected in self.partList.selectedIndexes():
         # get the selected part
         part = self.allParts[ selected.row() ]
         doc  = self.partsDoc[ selected.row() ]
         # by default, the link shall have the same name as the original part
         proposedLinkName = part.Label+'_var'
         # if it's a sub-assembly
         if part.Name == 'Model' or part.Name == 'Assembly':
             # if it has been renamed, we take the name given by the user
             if part.Name == part.Label:
                 proposedLinkName = part.Document.Name+'_var'
             # if not, we take the document
             else:
                 proposedLinkName = part.Label+'_var'
         # set the proposed name into the text field, unless it's a broken link
         if not self.brokenLink:
             # if the last character is a number, we increment this number
             lastChar = proposedLinkName[-1]
             if lastChar.isnumeric():
                 (rootName,sep,num) = proposedLinkName.rpartition('_')
                 proposedLinkName = Asm4.nextInstance(rootName)
             # if that name is already taken
             if self.activeDoc.getObject(proposedLinkName):
                 proposedLinkName = Asm4.nextInstance(proposedLinkName)
             self.linkNameInput.setText( proposedLinkName )
Esempio n. 2
0
 def Activated(self):
     instanceName = Asm4.nextInstance(self.partName)
     text, ok = QtGui.QInputDialog.getText(None,
                                           self.tooltip,
                                           'Enter new ' + self.partName +
                                           ' name :' + ' ' * 30,
                                           text=instanceName)
     if ok and text:
         # create Part
         newPart = App.ActiveDocument.addObject(self.partType, text)
         # add stuff if appropriate (not for groups)
         if self.partType in Asm4.containerTypes:
             # add an LCS at the root of the Part, and attach it to the 'Origin'
             lcs0 = newPart.newObject('PartDesign::CoordinateSystem',
                                      'LCS_0')
             lcs0.Support = [(newPart.Origin.OriginFeatures[0], '')]
             lcs0.MapMode = 'ObjectXY'
             lcs0.MapReversed = False
             # add AttachmentEngine
             # oooops, no, creates problems because it creates an AttachmentOffset property that collides with Asm4
             # newPart.addExtension("Part::AttachExtensionPython")
         # If an App::Part container is selected, move the created part/body/group there
         container = None
         if len(Gui.Selection.getSelection()) == 1:
             obj = Gui.Selection.getSelection()[0]
             if obj.TypeId == 'App::Part':
                 container = obj
         if container:
             if newPart.Name != 'Assembly':
                 container.addObject(newPart)
         # If the 'Part' group exists, move it there:
         else:
             partsGroup = App.ActiveDocument.getObject('Parts')
             if partsGroup and partsGroup.TypeId == 'App::DocumentObjectGroup' and newPart.TypeId in Asm4.containerTypes:
                 if newPart.Name != 'Assembly':
                     partsGroup.addObject(newPart)
         # recompute
         newPart.recompute()
         App.ActiveDocument.recompute()
Esempio n. 3
0
    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()
Esempio n. 4
0
    def Activated(self):
        # check that we have somewhere to put our stuff
        selectedObj = self.checkSelection()
        # default name increments the datum type's end numeral
        proposedName = Asm4.nextInstance(self.datumName, startAtOne=True)

        # check whether we have selected a container
        if selectedObj.TypeId in self.containers:
            parentContainer = selectedObj
        # if a datum object is selected
        elif selectedObj.TypeId in Asm4.datumTypes or selectedObj.TypeId == 'Sketcher::SketchObject':
            # see whether it's in a container
            parent = selectedObj.getParentGeoFeatureGroup()
            if parent.TypeId in self.containers:
                parentContainer = parent
        # if there is an assembly
        elif Asm4.getAssembly():
            parentContainer = Asm4.getAssembly()
        # something went wrong
        else:
            Asm4.warningBox("I can't create a " + self.datumType +
                            " with the current selections")

        # check whether there is already a similar datum, and increment the instance number
        # instanceNum = 1
        #while App.ActiveDocument.getObject( self.datumName+'_'+str(instanceNum) ):
        #    instanceNum += 1
        #datumName = self.datumName+'_'+str(instanceNum)
        if parentContainer:
            # input dialog to ask the user the name of the Sketch:
            #proposedName = Asm4.nextInstance( self.datumName + '_' + selectedObj.Label, startAtOne=True )
            text, ok = QtGui.QInputDialog.getText(
                None,
                'Create new ' + self.datumName,
                'Enter ' + self.datumName + ' name :' + ' ' * 40,
                text=proposedName)
            if ok and text:
                # App.activeDocument().getObject('Model').newObject( 'Sketcher::SketchObject', text )
                createdDatum = App.ActiveDocument.addObject(
                    self.datumType, text)
                parentContainer.addObject(createdDatum)
                createdDatum.Label = text
                # automatic resizing of datum Plane sucks, so we set it to manual
                if self.datumType == 'PartDesign::Plane':
                    createdDatum.ResizeMode = 'Manual'
                    createdDatum.Length = 100
                    createdDatum.Width = 100
                elif self.datumType == 'PartDesign::Line':
                    createdDatum.ResizeMode = 'Manual'
                    createdDatum.Length = 200
                # if color or transparency is specified for this datum type
                if self.datumColor:
                    Gui.ActiveDocument.getObject(
                        createdDatum.Name).ShapeColor = self.datumColor
                if self.datumAlpha:
                    Gui.ActiveDocument.getObject(
                        createdDatum.Name).Transparency = self.datumAlpha
                # highlight the created datum object
                Gui.Selection.clearSelection()
                Gui.Selection.addSelection(App.ActiveDocument.Name,
                                           parentContainer.Name,
                                           createdDatum.Name + '.')
                Gui.runCommand('Part_EditAttachment')