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: 

Fusion command will not be executed at its place within a custom command. It apears when the custom command ended.

1 REPLY 1
Reply
Message 1 of 2
karl.ri
137 Views, 1 Reply

Fusion command will not be executed at its place within a custom command. It apears when the custom command ended.

My Add-IN generates a command button to do:

1. Generate sketches on activated componen(s).

2. The FusionMoveCommand is called to freely move the component(s one after another).

3. The generated sketches will be deleted.

Problem is, that the move does not start and execute in place as expected. It apears on screen after the end of the custom command where the sketches have been deleted already.

Any help would be great.
My code:

import os
from ...lib import fusion360utils as futil
from ... import config
# import traceback
import adsk
import adsk.core as core
import adsk.fusion as fusion

app = core.Application.get()
ui = app.userInterface
design: fusion.Design = app.activeProduct
root: fusion.Component = design.rootComponent
occs = root.allOccurrences

offset = 0.25  # cm, FräserDM + Sicherheit
deletesketches = True


CMD_ID = f'{config.COMPANY_NAME}_{config.ADDIN_NAME}_cmdDialog'
CMD_NAME = 'sketch_offset_move'
CMD_Description = 'Generate a sketch,Project and Offset body, start Move command'

IS_PROMOTED = True

WORKSPACE_ID = 'FusionSolidEnvironment'
PANEL_ID = 'SolidModifyPanel'
COMMAND_BESIDE_ID = 'FusionMoveCommand'

# Resource location for command icons, here we assume a sub folder in this directory named "resources".
ICON_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'resources', '')

# Local list of event handlers used to maintain a reference so
# they are not released and garbage collected.
local_handlers = []


# Executed when add-in is run.
def start():
    # Create a command Definition.
    cmd_def = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER)

    # Define an event handler for the command created event. It will be called when the button is clicked.
    futil.add_handler(cmd_def.commandCreated, command_created)

    # ******** Add a button into the UI so the user can run the command. ********
    # Get the target workspace the button will be created in.
    workspace = ui.workspaces.itemById(WORKSPACE_ID)

    # Get the panel the button will be created in.
    panel = workspace.toolbarPanels.itemById(PANEL_ID)

    # Create the button command control in the UI after the specified existing command.
    control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False)

    # Specify if the command is promoted to the main toolbar.
    control.isPromoted = IS_PROMOTED


# Executed when add-in is stopped.
def stop():
    # Get the various UI elements for this command
    workspace = ui.workspaces.itemById(WORKSPACE_ID)
    panel = workspace.toolbarPanels.itemById(PANEL_ID)
    command_control = panel.controls.itemById(CMD_ID)
    command_definition = ui.commandDefinitions.itemById(CMD_ID)

    # Delete the button command control
    if command_control:
        command_control.deleteMe()

    # Delete the command definition
    if command_definition:
        command_definition.deleteMe()


# Function that is called when a user clicks the corresponding button in the UI.
# This defines the contents of the command dialog and connects to the command related events.
def command_created(args: core.CommandCreatedEventArgs😞
    # General logging for debug.
    futil.log(f'{CMD_NAME} Command Created Event')


    # TODO Connect to the events that are needed by this command.
    futil.add_handler(args.command.execute, command_execute, local_handlers=local_handlers)
    # futil.add_handler(args.command.executePreview, command_preview, local_handlers=local_handlers)
    futil.add_handler(args.command.destroy, command_destroy, local_handlers=local_handlers)


# This event handler is called when the user clicks the OK button in the command dialog or
# is immediately called after the created event not command inputs were created for the dialog.
def command_execute(args: core.CommandEventArgs😞
    # General logging for debug.
    futil.log(f'{CMD_NAME} Command Execute Event')

    # TODO ******************************** Your code here ********************************

    actsels = ui.activeSelections.asArray()
    for actsel in actsels:
        selectedEnt = actsel.entity

        occ = occs.itemByName(selectedEnt.name)
        # Get the associated component.
        component = occ.component

        # target body
        body: fusion.BRepBody = component.bRepBodies[0]
        # create sketch
        skt: fusion.Sketch = component.sketches.add(component.xYConstructionPlane)
        skt.name = 'skofmo'

        # create project
        curves = skt.project(body)
        # skt.projectCutEdges(body)
       
        # Create the offset
        lines = skt.sketchCurves.sketchLines
        line = lines.item(0)
        curves = skt.findConnectedCurves(line)
        # curves = skt.sketchCurves

        dirPoint = core.Point3D.create(0, 0, 0)
        offsetcurv = skt.offset(curves, dirPoint, offset)

        # clean sketch
        skt.areConstraintsShown = False
        skt.arePointsShown = False
        skt.areProfilesShown = False
        skt.areDimensionsShown = False
        app.executeTextCommand(u"Commands.Start HideProjectedGeometries")
 
    # start the Move for the selected component
    # ui.activeSelections.clear()
    # ui.activeSelections.add(selectedEnt)
    ui.commandDefinitions.itemById('FusionMoveCommand').execute()

    # Delete the sketch
    if deletesketches:
        for actsel in actsels:
            selectedEnt = actsel.entity
            occ = occs.itemByName(selectedEnt.name)
            # Get the associated component.
            component = occ.component
            skt = component.sketches.itemByName('skofmo')
            skt.deleteMe()

       
# This event handler is called when the command terminates.
def command_destroy(args: core.CommandEventArgs😞


    # General logging for debug.
    futil.log(f'{CMD_NAME} Command Destroy Event')

    global local_handlers
    local_handlers = []

 

1 REPLY 1
Message 2 of 2
karl.ri
in reply to: karl.ri

I give up.

I found no way to keep the FusionMoveCommand under control within my custom command.

If the API would allow access to FusionNNCommand events, elegant solutions would be possible.

Charly

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

Post to forums  

Autodesk Design & Make Report