Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Create Component From MeshBody?

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
Joshua.mursic
286 Views, 6 Replies

Create Component From MeshBody?

Hello, is it possible to create a component from a mesh body after the mesh has been inserted into the project? I know that you can use,  something like this:

newComponent = root.occurrences.addNewComponent(adsk.core.Matrix3D.create())
newComponent.component.meshBodies.add()

 

it insert a mesh from a file. But is there a way to target a mesh in the design environment and create a component from it? Like with a BRepBody?

adsk.fusion.BRepBody.createComponent()
6 REPLIES 6
Message 2 of 7
BrianEkins
in reply to: Joshua.mursic

The API doesn't directly support this, but a simple workaround exists. Because the "Create Components from Bodies" doesn't have a dialog, you can use the command. Here's an example that creates a new component using the first mesh body in the root component.

meshBody = des.rootComponent.meshBodies[0]
ui.activeSelections.add(meshBody)
cmd = ui.commandDefinitions.itemById('FusionCreateNewCompCmdFromBody') 
cmd.execute()
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 7
Joshua.mursic
in reply to: BrianEkins

Amazing, thank you very much

Message 4 of 7
Joshua.mursic
in reply to: BrianEkins

Hey @BrianEkins, I've run into an issue where when I call this function and then try to target the resulting component, I am unable to, because it dose not exist yet. 

 

meshBody = design.rootComponent.meshBodies[0]
ui.activeSelections.add(meshBody)
cmd = ui.commandDefinitions.itemById(u'FusionCreateNewCompCmdFromBody')

executed = cmd.execute()

while executed is False:
    pass

newComp = root.occurrences.item(root.occurrences.count-1)

 

I've tried making a while loop until the command returns that it was successful but that isn't working.

Message 5 of 7
Joshua.mursic
in reply to: BrianEkins

I tried creating a handler to let me know when the command terminated but my handler is never firing. Which suggests that the command is never being fired? I'm really at a loss for how to proceed.

import adsk.core, adsk.fusion, adsk.cam
app = adsk.core.Application.get()
ui = app.userInterface

local_handlers = []

def run():
    product = app.activeProduct
    design = adsk.fusion.Design.cast(product)
    root = design.rootComponent

    # Add handler
    onCommandTerminated = MyCommandTerminatedHandler()
    ui.commandTerminated.add(onCommandTerminated)
    local_handlers.append(onCommandTerminated)
    
    # Run the command
    meshBody = root.meshBodies.item(0)
    ui.activeSelections.add(meshBody)
    cmd = ui.commandDefinitions.itemById('FusionCreateNewCompCmdFromBody')
    cmd.execute()

# Event handler for the commandTerminated event.
class MyCommandTerminatedHandler(adsk.core.ApplicationCommandEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args):
        event_args = adsk.core.ApplicationCommandEventArgs.cast(args)

        if event_args.commandId == "FusionCreateNewCompCmdFromBody":
            ui.messageBox("Component Created")

 

Message 6 of 7
kandennti
in reply to: Joshua.mursic

Hi @Joshua.mursic .

 

The CommandDefinition.execute method is processed together after the script is finished, so in this case it is executed after the local_handlers are released.

At this time, we have not found a way to invoke other commands during processing other than using text commands.

・・・
        # Run the command
        meshBody: adsk.fusion.MeshBody = root.meshBodies.item(0)
        ui.activeSelections.add(meshBody)
        # cmd = ui.commandDefinitions.itemById('FusionCreateNewCompCmdFromBody')
        # cmd.execute()
        app.executeTextCommand(u'Commands.Start FusionCreateNewCompCmdFromBody')
・・・
Message 7 of 7
Joshua.mursic
in reply to: kandennti

Thank you this is exactly the issue. Much appreciated @kandennti 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report