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: 

Can't create a pattern on path feature using the Fusion 360 API

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
bevis.hobbs21
241 Views, 2 Replies

Can't create a pattern on path feature using the Fusion 360 API

I am unable to create a pattern on a path feature using the Fusion 360 API.

I ask the user to select a component and multiple paths to pattern that component along. I then loop over those paths and create a pattern on path feature for each path, using the same component every time. But when I do that it comes up with an error.

This is my code:

import adsk.core, adsk.fusion, adsk.cam, traceback

handlers = []
app = adsk.core.Application.get()
if app:
    ui = app.userInterface

class PatternMultipPathsCommandExecuteHandler(adsk.core.CommandEventHandler):

    def __init__(self):
        super().__init__()

    def notify(self, args):
        try:
            command = args.firingEvent.sender
            component = command.commandInputs.itemById("component").selection(0).entity

            paths_selection = command.commandInputs.itemById("paths")
            paths = [paths_selection.selection(i).entity for i in range(paths_selection.selectionCount)]

            product = app.activeProduct
            design = adsk.fusion.Design.cast(product)
            rootComp = design.rootComponent
            features = rootComp.features

            inputEntites = adsk.core.ObjectCollection.create()
            inputEntites.add(component)

            for item in paths:
                path = features.createPath(item)
                quantity = adsk.core.ValueInput.createByString('3')
                distance = adsk.core.ValueInput.createByString('1 cm')

                pathPatterns = features.pathPatternFeatures
                pathPatternInput = pathPatterns.createInput(inputEntites, path, quantity, distance,
                                                            adsk.fusion.PatternDistanceType.SpacingPatternDistanceType)
                pathFeature = pathPatterns.add(pathPatternInput)
        except:
            if ui:
                ui.messageBox("Failed:\n{}".format(traceback.format_exc()))

class PatternMultipPathsCommandDestroyHandler(adsk.core.CommandEventHandler):

    def __init__(self):
        super().__init__()

    def notify(self, args):
        try: adsk.terminate()
        except:
            if ui:
                ui.messageBox("Failed:\n{}".format(traceback.format_exc()))

class PatternMultipPathsInputChangedHandler(adsk.core.InputChangedEventHandler):

    def __init__(self):
        super().__init__()

    def notify(self, args):
        try:
            eventArgs = adsk.core.InputChangedEventArgs.cast(args)
            cmdInput = eventArgs.input

            if cmdInput.id == "component":
                command = args.firingEvent.sender
                command.commandInputs.itemById("paths").hasFocus = True
        except:
            if ui:
                ui.messageBox("Failed:\n{}".format(traceback.format_exc()))

class PatternMultipPathsCommandCreated(adsk.core.CommandCreatedEventHandler):

    def __init__(self):
        super().__init__()

    def notify(self, args):
        try:
            cmd = args.command
            cmd.setDialogInitialSize(300, 150)

            onExecute = PatternMultipPathsCommandExecuteHandler()
            cmd.execute.add(onExecute); handlers.append(onExecute)

            onDestroy = PatternMultipPathsCommandDestroyHandler()
            cmd.destroy.add(onDestroy); handlers.append(onDestroy)

            onInputChanged = PatternMultipPathsInputChangedHandler()
            cmd.inputChanged.add(onInputChanged); handlers.append(onInputChanged)

            inputs = cmd.commandInputs

            i1 = inputs.addSelectionInput("component", "Component",
                                          (" " * 3)+"Select a component to be patterned.")
            i1.setSelectionLimits(1, 1); i1.addSelectionFilter("Bodies")

            i2 = inputs.addSelectionInput("paths", "Paths",
                                          (" " * 3)+"Select paths to pattern a component along.")
            i2.setSelectionLimits(1); i2.addSelectionFilter("Edges")
        except:
            if ui:
                ui.messageBox("Failed:\n{}".format(traceback.format_exc()))

def run(context):
    try:
        commandDefinitions = ui.commandDefinitions
        # check the command exists or not
        cmdDef = commandDefinitions.itemById("PatternMultipPaths")
        if not cmdDef:
            cmdDef = commandDefinitions.addButtonDefinition("PatternMultipPaths",
                                                            "Pattern Along Multiple Paths",
                                                            "Do a component pattern along multiple paths.")

        onCommandCreated = PatternMultipPathsCommandCreated()
        cmdDef.commandCreated.add(onCommandCreated)

        handlers.append(onCommandCreated)
        inputs = adsk.core.NamedValues.create()
        cmdDef.execute(inputs)

        adsk.autoTerminate(False)

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

And this is the error I get:

Traceback (most recent call last):
  File "C:/Users/Bevis Hobbs/AppData/Roaming/Autodesk/Autodesk Fusion 360/API/Scripts/Pattern along multiple paths/Pattern along multiple paths.py", line 37, in notify
    pathFeature = pathPatterns.add(pathPatternInput)
  File "C:/Users/Bevis Hobbs/AppData/Local/Autodesk/webdeploy/production/eba7fbfbb17ff9a9d7c25530f6f766b5ffb15678/Api/Python/packages\adsk\fusion.py", line 27228, in add
    return _fusion.PathPatternFeatures_add(self, input)
RuntimeError: 5 : Some input argument is invalid.

 

Labels (1)
2 REPLIES 2
Message 2 of 3
kandennti
in reply to: bevis.hobbs21

Hi @bevis.hobbs21 .

 

I tried it and did not get any errors.
What kind of data do I need to try to get an error?

Message 3 of 3
bevis.hobbs21
in reply to: kandennti

Not sure if it's a bug in Fusion 360 but the component I was selecting is a derived component and when I broke the link on the derived component the script worked perfectly.

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

Post to forums