예제 #1
0
    def on_input_changed(self, command: adsk.core.Command, inputs: adsk.core.CommandInputs, changed_input, input_values):
        ao = apper.AppObjects()
        app = adsk.core.Application.get()

        selectionInput = inputs.itemById('selection_input_id')
        if selectionInput.selectionCount > 0:
            selectedOcc = selectionInput.selection(0).entity
            # Prevent the root component from being selected
            if selectedOcc.objectType != 'adsk::fusion::Occurrence':
                selectionInput.clearSelection()
                return
            selectedComp = selectedOcc.component
            # Check if the part is parametric, if not check it's parent occurrence
            for i in range(2):
                if selectedComp.attributes.itemByName('vex_cad', 'part_data') and 'parameters' in vex_cad.getPartData(selectedComp):
                    if changed_input.id == 'selection_input_id':
                        # This is needed if the user selects a differant part without deselecting first
                        hideAllCommandInputs()
                        # Show the the controls for the parameters the part has
                        showSomeCommandInputs(selectedOcc)
                        if selectionInput.selectionCount == 0:
                            selectionInput.addSelection(selectedOcc)
                    else:
                        # ao.ui.messageBox('before updateInputs: ' + str(selectedOcc))
                        updateInputs(selectedOcc, changed_input)
                else:
                    selectionInput.clearSelection()
                    # If the selected part is an occurrence and has a parent occurrence
                    if '+' in selectedOcc.fullPathName:
                        # Get the sellected part's parent Occurrence
                        selectedOcc = selectedOcc.assemblyContext
                        selectedComp = selectedOcc.component
        else:
            # Hide the commands for the parameters from the last selected part
            hideAllCommandInputs()
예제 #2
0
 def show(self, occ):
     comp = occ.component
     ao = apper.AppObjects()
     parameter = vex_cad.getPartData(comp)['parameters'][self.parameterId]
     
     self.commandInput.expression = comp.modelParameters.item(parameter['index']).expression
     self.commandInput.minimumValue = parameter['min_value']
     self.commandInput.maximumValue = parameter['max_value']
     
     occMatrix3D = occ.transform
     if 'manipulator_point_offset' in parameter:
         pointOffset = parameter['manipulator_point_offset']
         newMatrix3D = adsk.core.Matrix3D.create()
         for i in range(3):
             newMatrix3D.setCell(i, 3, pointOffset[i])
         newMatrix3D.transformBy(occMatrix3D)
         occMatrix3DCoords = newMatrix3D.getAsCoordinateSystem()
     else:
         occMatrix3DCoords = occMatrix3D.getAsCoordinateSystem()
     
     occVector3D = occMatrix3DCoords[coordSys[parameter['manipulator_axis']]]
     if 'manipulator_axis_flipped' in parameter and parameter['manipulator_axis_flipped']:
         occVector3D.scaleBy(-1)
     occPoint3D = occMatrix3DCoords[coordSys['origin']]
     self.commandInput.setManipulator(occPoint3D, occVector3D)
     self.commandInput.isVisible = True
예제 #3
0
 def updatePart(self, occ):
     comp = occ.component
     parameter = vex_cad.getPartData(comp)['parameters'][self.parameterId]
     ao = apper.AppObjects()
     unitsMgr = ao.units_manager
     if unitsMgr.isValidExpression(self.commandInput.expression, ''):
         comp.modelParameters.item(parameter['index']).value = self.commandInput.value
예제 #4
0
 def onUpdate(self, occ, changedInput):
     comp = occ.component
     selectedName = self.dropDownInput.selectedItem.name
     if  changedInput == self.distanceValue.commandInput:
         ao = apper.AppObjects()
         unitsMgr = ao.units_manager
         self.dropDownInput.listItems.item(0).isSelected = True
     elif  selectedName != 'Custom':
         self.distanceValue.commandInput.expression = selectedName
예제 #5
0
    def on_html_event(self, html_args: adsk.core.HTMLEventArgs):

        # Parse incoming message and build message for Fusion message box
        data = json.loads(html_args.data)
        msg = "An event has been fired from the html to Fusion with the following data:\n"
        msg += '    Command: {}\n    arg1: {}\n    arg2: {}'.format(
            html_args.action, data['arg1'], data['arg2'])

        # Display Message
        ao = apper.AppObjects()
        ao.ui.messageBox(msg)
 def run_in_thread(self, thread, event_id, input_data=None):
     ao = apper.AppObjects()
     ao.ui.messageBox("test")
     # Every five seconds fire a custom event, passing a random number.
     for i in range(3):
         message = {
             "text": "Hello World!",
             "index": str(i),
             "event_id": event_id
         }
         time.sleep(3)
         ao.app.fireCustomEvent(event_id, json.dumps(message))
예제 #7
0
 def command_event_received(self, event_args, command_id, command_definition):
     global importingPart
     if command_id == 'FusionImportCommand':
         importingPart = True
     if command_id == 'FusionMoveCommand':
         if importingPart:
             ao = apper.AppObjects()
             tempPart = ao.ui.activeSelections.item(0).entity
             if tempPart.component.attributes.itemByName('vex_cad', 'part_data') and 'parameters' in vex_cad.getPartData(tempPart.component):
                 global importedPart
                 importedPart = tempPart
             else:
                 importingPart = False
예제 #8
0
    def on_execute(self, command: adsk.core.Command,
                   command_inputs: adsk.core.CommandInputs, args,
                   input_values):

        # Get Reference to Palette
        ao = apper.AppObjects()
        palette = ao.ui.palettes.itemById(self.palette_id)

        # Get input value from string input
        message = input_values['palette_string']

        # Send message to the HTML Page
        if palette:
            palette.sendInfoToHTML('send', message)
예제 #9
0
    def __init__(self, name: str, options: dict):
        super().__init__(name, options)

        ao = apper.AppObjects()

        self.palette_id = options.get('palette_id', 'Default Command Name')
        self.palette_name = options.get('palette_name', 'Palette Name')

        debug_path = options.get('palette_html_file_url_debug')
        palette_is_local = options.get('palette_is_local', True)

        if palette_is_local:
            rel_path = options.get('palette_html_file_url')

            if self.fusion_app.debug and (debug_path is not None):
                self.palette_html_file_url = debug_path

            elif rel_path is not None:

                local_path_from_root = os.path.dirname(
                    os.path.relpath(
                        sys.modules[self.__class__.__module__].__file__,
                        self.fusion_app.root_path))

                self.palette_html_file_url = os.path.join(
                    './', local_path_from_root, rel_path)
            else:
                raise AttributeError(
                    "Resource Path not defined for Palette.  Set palette_html_file_url in command options"
                )
        else:
            # TODO add some url validation
            self.palette_html_file_url = options.get('palette_html_file_url')

        if self.fusion_app.debug:
            ao.ui.messageBox(self.palette_html_file_url)

        self.palette_is_visible = options.get('palette_is_visible', True)
        self.palette_show_close_button = options.get(
            'palette_show_close_button', True)
        self.palette_is_resizable = options.get('palette_is_resizable', True)
        self.palette_width = options.get('palette_width', 600)
        self.palette_height = options.get('palette_height', 600)
        self.palette_force_url_reload = options.get('palette_force_url_reload',
                                                    True)

        self.palette = None
        self.args = None
        self.handlers = []
        self.html_handlers = []
예제 #10
0
    def on_create(self, command: adsk.core.Command, inputs: adsk.core.CommandInputs):

        ao = apper.AppObjects()

        selectionInput = inputs.addSelectionInput('selection_input_id', 'Part', 'Component to select')
        selectionInput.setSelectionLimits(1, 1)
        selectionInput.addSelectionFilter('Occurrences')

        createAllCommandInputs(inputs)
        hideAllCommandInputs()

        global importingPart
        global importedPart
        if importingPart:
            # if importedPart.attributes.itemByName('vex_cad', 'part_data'):
            # importedCompAttributes = vex_cad.getPartData(importedPart)
            # if 'parameters' in importedCompAttributes:
            selectionInput.addSelection(importedPart)
            showSomeCommandInputs(importedPart)
            importingPart = False
예제 #11
0
 def command_event_received(self, event_args, command_id, command_definition):
     ao = apper.AppObjects()
     msg = "You just started the {} command with id: {} ".format(command_definition.name, command_id)
     ao.ui.messageBox(msg)
예제 #12
0
 def custom_event_received(self, event_dict):
     ao = apper.AppObjects()
     ao.ui.messageBox(str(event_dict))
예제 #13
0
 def command_event_received(self, event_args, command_id, command_definition):
     if command_id == 'FusionMoveCommand':
         ao = apper.AppObjects()
         if importingPart:
             modify_part = ao.ui.commandDefinitions.itemById('VEX CAD_VEX CAD Library_modify_part')
             modify_part.execute()
예제 #14
0
def defineParameterManagers():
    ao = apper.AppObjects()
    unitsMgr = ao.units_manager

    coordSys = {'origin': 0, 'xAxis': 1, 'yAxis': 2, 'zAxis': 3}

    def setInsertLightBulb(comp, insertType, isOn):
        for i in range(comp.occurrences.count):
            subComp = comp.occurrences.item(i).component
            if subComp.attributes.itemByName('vex_cad', 'part_data') and 'identifiers' in vex_cad.getPartData(subComp):
                identifiers = vex_cad.getPartData(subComp)['identifiers']
                if 'insert' in identifiers:
                    if insertType in identifiers['insert']:
                        subComp.isBodiesFolderLightBulbOn = isOn
    
    def iSInsertVisible(comp, insertType):
        for i in range(comp.occurrences.count):
            subComp = comp.occurrences.item(i).component
            if subComp.attributes.itemByName('vex_cad', 'part_data') and 'identifiers' in vex_cad.getPartData(subComp):
                identifiers = vex_cad.getPartData(subComp)['identifiers']
                if 'insert' in identifiers:
                    if insertType in identifiers['insert']:
                        return subComp.isBodiesFolderLightBulbOn


    class ParameterManager:
        def __init__(self, id, name):
            self.id = id
            self.name = name
            self.parameterId = id
        # Defaults
        def hide(self):
            self.commandInput.isVisible = False
        def onUpdate(self, occ):
            pass
        def previewUpdatePart(self, occ):
            comp = occ.component
            self.updatePart(occ)

        # Need to be redefined
        def create(self, commandInputs):
            pass
        def show(self, occ):
            pass
        def onUpdate(self, occ, changedInput):
            pass
    
    class ButtonRowInserts(ParameterManager):
        def create(self, commandInputs):
            self.commandInput = commandInputs.addButtonRowCommandInput(self.id, self.name, False)
        
        def show(self, occ):
            comp = occ.component
            self.commandInput.listItems.clear()
            isSquareSelected = iSInsertVisible(comp, 'square')
            isRoundSelected = iSInsertVisible(comp, 'round')
            self.commandInput.listItems.add('None', not isRoundSelected and not isSquareSelected, 'commands/resources/command_icons/insert_none')
            self.commandInput.listItems.add('Square', isSquareSelected, 'commands/resources/command_icons/insert_square')
            self.commandInput.listItems.add('Round', isRoundSelected, 'commands/resources/command_icons/insert_round')
            self.commandInput.isVisible = True

        def updatePart(self, occ):
            comp = occ.component
            itemName = self.commandInput.selectedItem.name
            if itemName == 'None':
                setInsertLightBulb(comp, 'square', False)
                setInsertLightBulb(comp, 'round', False)
            elif itemName == 'Square':
                setInsertLightBulb(comp, 'square', True)
                setInsertLightBulb(comp, 'round', False)
            elif itemName == 'Round':
                setInsertLightBulb(comp, 'square', False)
                setInsertLightBulb(comp, 'round', True)
    
    class DistanceModule(ParameterManager):
        def create(self, commandInputs):
            self.commandInput = commandInputs.addDistanceValueCommandInput(self.id, self.name, adsk.core.ValueInput.createByReal(0))
        
        def show(self, occ):
            comp = occ.component
            ao = apper.AppObjects()
            parameter = vex_cad.getPartData(comp)['parameters'][self.parameterId]
            
            self.commandInput.expression = comp.modelParameters.item(parameter['index']).expression
            self.commandInput.minimumValue = parameter['min_value']
            self.commandInput.maximumValue = parameter['max_value']
            
            occMatrix3D = occ.transform
            if 'manipulator_point_offset' in parameter:
                pointOffset = parameter['manipulator_point_offset']
                newMatrix3D = adsk.core.Matrix3D.create()
                for i in range(3):
                    newMatrix3D.setCell(i, 3, pointOffset[i])
                newMatrix3D.transformBy(occMatrix3D)
                occMatrix3DCoords = newMatrix3D.getAsCoordinateSystem()
            else:
                occMatrix3DCoords = occMatrix3D.getAsCoordinateSystem()
            
            occVector3D = occMatrix3DCoords[coordSys[parameter['manipulator_axis']]]
            if 'manipulator_axis_flipped' in parameter and parameter['manipulator_axis_flipped']:
                occVector3D.scaleBy(-1)
            occPoint3D = occMatrix3DCoords[coordSys['origin']]
            self.commandInput.setManipulator(occPoint3D, occVector3D)
            self.commandInput.isVisible = True

        def updatePart(self, occ):
            comp = occ.component
            parameter = vex_cad.getPartData(comp)['parameters'][self.parameterId]
            ao = apper.AppObjects()
            unitsMgr = ao.units_manager
            if unitsMgr.isValidExpression(self.commandInput.expression, ''):
                comp.modelParameters.item(parameter['index']).value = self.commandInput.value

    class DistanceGroup(ParameterManager):
        def create(self, commandInputs):
            self.group = commandInputs.addGroupCommandInput(self.id, self.name)
            self.distanceValue = DistanceModule(self.id + '_distance_value', 'Distance')
            self.distanceValue.create(self.group.children)
            self.distanceValue.parameterId = self.id

        def show(self, occ):
            self.distanceValue.show(occ)
            self.group.isVisible = True

        def hide(self):
            self.distanceValue.hide()
            self.group.isVisible = False

        def updatePart(self, occ):
            self.distanceValue.updatePart(occ)

    class DistanceSliderHoles(ParameterManager):
        def create(self, commandInputs):
            self.group = commandInputs.addGroupCommandInput(self.id, self.name)
            self.intSlider = self.group.children.addIntegerSliderCommandInput(self.id + '_int_slider', 'Holes', 0, 40)
            self.distanceValue = DistanceModule(self.id + '_distance_value', 'Distance')
            self.distanceValue.create(self.group.children)
            self.distanceValue.parameterId = self.id

        def show(self, occ):
            comp = occ.component
            parameter = vex_cad.getPartData(comp)['parameters'][self.parameterId]
            self.distanceValue.show(occ)
            self.intSlider.minimumValue = int(parameter['min_value'] / 1.27)
            self.intSlider.maximumValue = int(parameter['max_value'] / 1.27)
            if self.distanceValue.commandInput.isValidExpression:
                self.intSlider.valueOne = int(self.distanceValue.commandInput.value / 1.27 + 0.99)
            self.intSlider.isVisible = True
            self.group.isVisible = True

        def hide(self):
            self.distanceValue.hide()
            self.intSlider.isVisible = False
            self.group.isVisible = False

        def onUpdate(self, occ, changedInput):
            comp = occ.component
            if  changedInput == self.distanceValue.commandInput:
                if self.distanceValue.commandInput.isValidExpression:
                    self.intSlider.valueOne = int(self.distanceValue.commandInput.value / 1.27 + 0.99)
                
            else:
                self.distanceValue.commandInput.value = self.intSlider.valueOne * 1.27

        def updatePart(self, occ):
            self.distanceValue.updatePart(occ)

    class DistanceDropDown(ParameterManager):
        def create(self, commandInputs):
            self.group = commandInputs.addGroupCommandInput(self.id, self.name)
            self.dropDownInput = self.group.children.addDropDownCommandInput(self.id + '_options', 'Standard Sizes', 1)
            self.distanceValue = DistanceModule(self.id + '_distance_value', 'Distance')
            self.distanceValue.create(self.group.children)
            self.distanceValue.parameterId = self.id

        def show(self, occ):
            comp = occ.component
            parameter = vex_cad.getPartData(comp)['parameters'][self.parameterId]
            self.distanceValue.show(occ)
            self.dropDownInput.listItems.clear()
            self.dropDownInput.listItems.add('Custom', True)
            parameter = vex_cad.getPartData(comp)['parameters'][self.parameterId]
            for item in parameter["expressions"]:
                isSelected = comp.modelParameters.item(parameter['index']).value == unitsMgr.evaluateExpression(item, '')
                self.dropDownInput.listItems.add(item, isSelected)
            
            self.dropDownInput.isVisible = True
            self.distanceValue.isVisible = True
            self.group.isVisible = True

        def hide(self):
            self.distanceValue.hide()
            self.dropDownInput.isVisible = False
            self.group.isVisible = False

        def onUpdate(self, occ, changedInput):
            comp = occ.component
            selectedName = self.dropDownInput.selectedItem.name
            if  changedInput == self.distanceValue.commandInput:
                ao = apper.AppObjects()
                unitsMgr = ao.units_manager
                self.dropDownInput.listItems.item(0).isSelected = True
            elif  selectedName != 'Custom':
                self.distanceValue.commandInput.expression = selectedName

        def updatePart(self, occ):
            self.distanceValue.updatePart(occ)
    
    return [
        ButtonRowInserts('inserts_v1', 'Inserts'),
        DistanceGroup('distance_length_v1', 'Length'),
        DistanceDropDown('distance_list_size_v1', 'Size'),
        DistanceSliderHoles('distance_holes_length_v1', 'Length'),
        DistanceSliderHoles('distance_holes_width_v1', 'Width')
    ]