API access to Align Components Function

API access to Align Components Function

Anonymous
Not applicable
877 Views
2 Replies
Message 1 of 3

API access to Align Components Function

Anonymous
Not applicable

There is an extremely handy feature in the UI called alignComponents  (see picture below). Is there some way to access this from the API? Otherwise, is there a script available that will do this? Given two components and planes or points or edges in each, is it possilbe to line those elements up?Capture.PNG

0 Likes
878 Views
2 Replies
Replies (2)
Message 2 of 3

ekinsb
Alumni
Alumni

I didn't even know about this command.  Thanks for pointing it out.  The API doesn't have a function that is the equivalent of this command.  However, there are a couple of workarounds.  The first does provide the exact same result as the command and is fairly easy to use.  It mimics what you're doing in the user interface.  You use the API to select the two things you want to align and then run the "Align Components" command.  Here's an example that demonstrates that.

 

def alignComponents():
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        # Get the entities to align.  This example has the user pick
        # two planar faces but it could be any geometry that's valid for the
        # Align Components command and could be found in any way.        
        face1 = ui.selectEntity('Select a planar face on the component to align to.', 'PlanarFaces').entity
        face2 = ui.selectEntity('Select a planar face on the component to align.', 'PlanarFaces').entity

        # Add these faces to the active selection.
        ui.activeSelections.clear
        ui.activeSelections.add(face1)
        ui.activeSelections.add(face2)

        # Get the Align Component command and execute it.
        alignCommand = ui.commandDefinitions.itemById('AlignComponentsCmd')
        alignCommand.execute()
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) 

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 3 of 3

ekinsb
Alumni
Alumni

The second approach is for you to do what the command is doing and given the two inputs perform the calculations to figure out how to transform one of the occurrences so that the geometry lines up correctly with the other occurrence.  It would be quite a bit of work.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes