Flipping Joint

Flipping Joint

brad.bylls
Collaborator Collaborator
371 Views
2 Replies
Message 1 of 3

Flipping Joint

brad.bylls
Collaborator
Collaborator

# Copyright (c) 2023 Brad Bylls
# Used to create multiple simple rigid joints

import adsk.core
import adsk.fusion
import adsk.cam

# Import the entire apper package
from ..apper import apper

# Globals
_app = adsk.core.Application.get()
_ui = _app.userInterface
tools = adsk.core.ObjectCollection.create()
targets = adsk.core.ObjectCollection.create()

class QuickJoints(apper.Fusion360CommandBase):
    
    def validate_inputs(self, command, inputs, args, input_values) -> bool:

        global tools, targets

        if tools.count == targets.count:
            return True
        else:
            return False
        
    # =========================================================================== #
    
    def on_input_changed(self, command, inputs, changed_input, input_values):

        global tools, targets

        if changed_input.id == 'component':
            toolSelectionInput = inputs.itemById('component')
            tool = tools.add(toolSelectionInput)
            inputs.itemById('target').isEnabled = True
        elif changed_input.id == 'target':
            targetSelectionInput = inputs.itemById('target')
            target = targets.add(targetSelectionInput)
            inputs.itemById('component').isEnabled = True

    # =========================================================================== #

    def on_create(self, command, inputs):

        global root, comp

        design = _app.activeProduct
        root = design.rootComponent
        comp = design.activeComponent

        # Verify that a Fusion design is active.
        if not design:
            _ui.messageBox('A Fusion design must be active when invoking this command.')
            return()

        command.okButtonText = ("Create the Joints") # text in "OK" button
        command.isExecutedWhenPreEmpted = False

        # Create the command dialog
        _inputSelectTargets = inputs.addSelectionInput('component', 'Component Snap', 'Select the First Component Edge')
        _inputSelectTargets.addSelectionFilter(adsk.core.SelectionCommandInput.Edges)
        _inputSelectTargets.setSelectionLimits(0)

        _inputSelectTools = inputs.addSelectionInput('target', 'Target Snap', 'Select the Second Component Target')
        _inputSelectTools.addSelectionFilter(adsk.core.SelectionCommandInput.SketchPoints)
        _inputSelectTools.addSelectionFilter(adsk.core.SelectionCommandInput.CircularEdges)
        _inputSelectTools.setSelectionLimits(0)

        _inputErrMessage = inputs.addTextBoxCommandInput('errMessage', '', 'Component and Target\nMust be equal count.', 2, True)
        _inputErrMessage.isFullWidth = True

    # =========================================================================== #

    def on_execute(self, command, inputs, args, input_values):

        componentList = []
        component_input: adsk.core.SelectionCommandInput = inputs.itemById('component')

        for idx in range(0, component_input.selectionCount):
            componentList.append(component_input.selection(idx).entity)

        target_input: adsk.core.SelectionCommandInput = inputs.itemById('target')
        for idx in range(0, target_input.selectionCount):
            targets = target_input.selection(idx).entity
            components = componentList[idx]

            if targets.objectType == 'adsk::fusion::BRepEdge':
                component = adsk.fusion.JointGeometry.createByCurve(components, adsk.fusion.JointKeyPointTypes.CenterKeyPoint)
                target = adsk.fusion.JointGeometry.createByCurve(targets, adsk.fusion.JointKeyPointTypes.CenterKeyPoint)
                joints = comp.joints
                jointInput = joints.createInput(component, target)
                joint = joints.add(jointInput)
                
            else:
                component = adsk.fusion.JointGeometry.createByCurve(components, adsk.fusion.JointKeyPointTypes.CenterKeyPoint)
                target = adsk.fusion.JointGeometry.createByPoint(targets)
                joints = comp.joints
                jointInput = joints.createInput(component, target)
                joint = joints.add(jointInput)

        comp.isJointsFolderLightBulbOn = False   

    # =========================================================================== #

    # This function will be called when the user completes the command.

    def command_destroy(args: adsk.core.CommandEventArgs):
        global local_handlers
        local_handlers = []

    # =========================================================================== #
Brad Bylls
372 Views
2 Replies
Replies (2)
Message 2 of 3

mayank.malukani
Autodesk
Autodesk

Hi @brad.bylls , Can you please share your file or at least the part in which the 2 components are contained so that we can have a closer look?

0 Likes
Message 3 of 3

mayank.malukani
Autodesk
Autodesk

To attach your model :  Open it in Fusion 360, select the File menu, then Export and save to you hard drive.  Attach to a reply post using the Attachment section.

0 Likes