Esempio n. 1
0
 def IsActive(self):
     # We only insert a link into an Asm4  Model
     if App.ActiveDocument:
         (obj, tree) = Asm4.getSelectionTree()
         if tree and len(tree) >= 2:
             # the root container is the first element and must be an App::Part
             root = App.ActiveDocument.getObject(tree[0])
             if root and root.TypeId == 'App::Part':
                 # check that the object has a Placement property
                 if hasattr(obj, 'Placement') and obj.getTypeIdOfProperty(
                         'Placement') == 'App::PropertyPlacement':
                     return True
     return False
Esempio n. 2
0
    def Activated(self):
        (selDatum, selTree) = Asm4.getSelectionTree()
        # the selected datum is not deep enough
        if not selTree or len(selTree) < 3:
            message = selDatum.Name + ' is already at the top-level and cannot be imported'
            Asm4.warningBox(message)
            return

        # the root parent container is the first in the selection tree
        rootContainer = App.ActiveDocument.getObject(selTree[0])
        selection = self.getSelectedDatums()

        # special case where 2 objects are selected in order to update the placement of the second one
        if len(selection) == 2 and selection[1].getParentGeoFeatureGroup(
        ) == rootContainer:
            confirm = False
            targetDatum = selection[1]

            # target datum is free
            if targetDatum.MapMode == 'Deactivated':
                message = 'This will superimpose ' + Asm4.labelName(
                    targetDatum) + ' in ' + Asm4.labelName(
                        rootContainer) + ' on:\n\n'
                for objName in selTree[1:-1]:
                    message += '> ' + objName + '\n'
                message += '> ' + Asm4.labelName(selDatum)
                Asm4.warningBox(message)
                confirm = True
            # target datum is attached
            else:
                message = Asm4.labelName(targetDatum) + ' in ' + Asm4.labelName(
                    rootContainer) + ' is already attached to some geometry. '
                message += 'This will superimpose its Placement on:\n\n'
                for objName in selTree[1:-1]:
                    message += '> ' + objName + '\n'
                message += '> ' + Asm4.labelName(selDatum)
                confirm = Asm4.confirmBox(message)

            if confirm:
                self.setupTargetDatum(targetDatum,
                                      self.getDatumExpression(selTree))

                # hide initial datum
                selDatum.Visibility = False

                # select the newly created datum
                Gui.Selection.clearSelection()
                Gui.Selection.addSelection(App.ActiveDocument.Name,
                                           rootContainer.Name,
                                           targetDatum.Name + '.')

                # recompute assembly
                rootContainer.recompute(True)

            # Done with the special case, no need to continue with the normal process
            return

        # Single or Multiple selection(s) for regular case
        # Notify user that default names will be used and import all the objects
        proposedName = selTree[-2] + '_' + selDatum.Label

        if len(selection) == 1:
            message = 'Create a new ' + selDatum.TypeId + ' in ' + Asm4.labelName(
                rootContainer) + ' \nsuperimposed on:\n\n'
            for objName in selTree[1:]:
                message += '> ' + objName + '\n'
            message += '\nEnter name for this datum :' + ' ' * 40
            userSpecifiedName, confirm = QtGui.QInputDialog.getText(
                None, 'Import Datum', message, text=proposedName)
        else:
            message = str(
                len(selection)
            ) + " selected datum objects will be imported into the root assembly\n"
            message += "with their default names such as:\n"
            message += proposedName
            confirm = Asm4.confirmBox(message)

        if confirm:
            for index in range(len(selection)):
                (selDatum, selTree) = Asm4.getSelectionTree(index)
                # create a new datum object
                if len(selection) == 1:
                    proposedName = userSpecifiedName
                else:
                    proposedName = selTree[-2] + '_' + selDatum.Label

                targetDatum = rootContainer.newObject(selDatum.TypeId,
                                                      proposedName)
                targetDatum.Label = proposedName
                # apply existing view properties if applicable
                if hasattr(selDatum,
                           'ResizeMode') and selDatum.ResizeMode == 'Manual':
                    targetDatum.ResizeMode = 'Manual'
                    if hasattr(selDatum, 'Length'):
                        targetDatum.Length = selDatum.Length
                    if hasattr(selDatum, 'Width'):
                        targetDatum.Width = selDatum.Width
                targetDatum.ViewObject.ShapeColor = selDatum.ViewObject.ShapeColor
                targetDatum.ViewObject.Transparency = selDatum.ViewObject.Transparency

                self.setupTargetDatum(targetDatum,
                                      self.getDatumExpression(selTree))

                # hide initial datum
                selDatum.Visibility = False

            # select the last created datum
            Gui.Selection.clearSelection()
            Gui.Selection.addSelection(App.ActiveDocument.Name,
                                       rootContainer.Name,
                                       targetDatum.Name + '.')

        # recompute assembly
        rootContainer.recompute(True)