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: 

Adding feature inputs 'extend' and 'thicken' clears commandInputs items

1 REPLY 1
SOLVED
Reply
Message 1 of 2
emanster.nz
134 Views, 1 Reply

Adding feature inputs 'extend' and 'thicken' clears commandInputs items

I've written an add-on that adds a button to the UI that when pressed prompts the user to select any number of surface bodies (ideally patches) and click okay to extend all the edges of the patches then thicken them. My issue is once the command is executed the function extendAndThickenPatch runs once and then the commandInputs list is cleared and I lose the ability to call on all other selections the user has made. This add-in works perfectly if the user selects only 1 patch but any more and it stops after a single round of the for loop in class ExtrudePatch, printing an out of range error. Attached is the plugin as well as the bracket part I've been using to test it. Is there a change I can make to iterate through all the selected items and make the changes to them via the multi selection without the inputs list clearing?

 

Code:

from typing import Union
import adsk.core, adsk.fusion, adsk.cam, traceback
from types import TracebackType

import subprocess
import os, os.path
import sys

handlers = []

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
         # Get the CommandDefinitions collection.
        cmdDefs = ui.commandDefinitions
        work_space = ui.workspaces.itemById('FusionSolidEnvironment')
        # Get toolbar
        tb_panels = work_space.toolbarPanels

        global TB_PANEL
        TB_PANEL = tb_panels.itemById('mm_Panel2')
        if TB_PANEL:
            TB_PANEL.deleteMe()
        # Create extra toolbar widget for Nester and Slicer
        TB_PANEL = tb_panels.add('mm_Panel2', 'Mold Maker', 'SelectPanel', False)
        
        addins_toolbar_panel = ui.allToolbarPanels.itemById('mm_Panel2')
        
        
        patchAndExtrudeButtonDef = cmdDefs.addButtonDefinition('Patch and Extrude Tool', 
                                                   'Foundrylab Patch and Extrude Tool', 
                                                   'Generates tube blockers for mold making',
                                                   'resources/step1')
        buttonpatchAndExtrudeCont = addins_toolbar_panel.controls.addCommand(patchAndExtrudeButtonDef, 'MoldMaking')
        
        # Connect to the command created event.
        buttonpatchAndExtrudeCont.isPromotedByDefault = True
        buttonpatchAndExtrudeCont.isPromoted = True

        #When pressed cast to task running class
        patchAndExtrudeCreated = patchAndExtrudeButtonPressedEventHandler()
        patchAndExtrudeButtonDef.commandCreated.add(patchAndExtrudeCreated)
        handlers.append(patchAndExtrudeCreated)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))



class patchAndExtrudeButtonPressedEventHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self,args):
        #ui = None
        
         
                
        cmd = adsk.core.Command.cast(args.command)
        cmd.setDialogMinimumSize(100,100)

        # Get the CommandInputs collection associated with the command.
        inputs = cmd.commandInputs

        # Create value input.
        
        selectInput = inputs.addSelectionInput('PatchTool','Select patches to extrude', 'Select a patch')
        selectInput.addSelectionFilter(adsk.core.SelectionCommandInput.SurfaceBodies)
        selectInput.setSelectionLimits(0)
        

        # Connect to the execute event.
        onExecute = ExtrudePatch()
        cmd.execute.add(onExecute)
        handlers.append(onExecute)

        
        

        """try:
            app = adsk.core.Application.get()
            ui = app.userInterface
            bodySel = ui.selectEntity('select surface entity', 'SurfaceBodies')
            if bodySel:
                extrudePatch(bodySel.entity)
            

        except:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))"""



class ExtrudePatch(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = app.activeProduct 
        eventArgs = adsk.core.CommandEventArgs.cast(args)

        # Grabs list of all commandInputs
        inputs = eventArgs.command.commandInputs

        # Selects the selectionInputs command input from commandInputs. commandInputs should be size 1
        selectionsList = inputs.item(0)

        # Number of patches selected by user
        count = selectionsList.selectionCount
        
        for num in range(count):
            try:
                patch = selectionsList.selection(num).entity
                extendAndThickenPatch(patch)
            except:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def extendAndThickenPatch(patch):
    app = adsk.core.Application.get()
    ui  = app.userInterface
    design = app.activeProduct

    inputEdges = adsk.core.ObjectCollection.create()

    # Get all the edges of this patch surface
    patchEdges = patch.edges

    # For every patch edge add it to inputEdges to extrude          
    for edge in range(patchEdges.count):
        inputEdges.add(patchEdges.item(edge))

    distance = adsk.core.ValueInput.createByReal(1.1)

    # Get the root component of the active design
    rootComp = design.rootComponent
    features = rootComp.features
    extendFeatures = features.extendFeatures
    extendFeatureInput = extendFeatures.createInput(inputEdges, distance, adsk.fusion.SurfaceExtendTypes.NaturalSurfaceExtendType)
    extendFeatureInput.extendAlignment = adsk.fusion.SurfaceExtendAlignment.FreeEdges
    extendFeatures.add(extendFeatureInput)

    thickenFeatures = features.thickenFeatures
    inputSurfaces = adsk.core.ObjectCollection.create()
           
    inputSurfaces.add(patch)
    thickness = adsk.core.ValueInput.createByReal(-0.002)
    thickenInput = thickenFeatures.createInput(inputSurfaces, thickness, False,  adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
    thickenFeatures.add(thickenInput)

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

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 

1 REPLY 1
Message 2 of 2
BrianEkins
in reply to: emanster.nz

Selections are somewhat fragile and Fusion will clear them if anything is changed in the file. That seems to be what's happening in your case. Doing the extend and thicken is modifying the file, which causes the current selections to be cleared.

 

What you can do is to first get all of the selections and save them in a list. Then you'll process the items in your list and it won't matter that the selection gets cleared.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com

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