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: 

How to reference a feature by its name?

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
luca.giorcelli
534 Views, 7 Replies

How to reference a feature by its name?

I'm sure it's incredibly simple, but I've been searching for hours and haven't figured it out yet. 

 

Screenshot 2020-05-17 at 13.52.30.png

I just want to suppress a feature named "myFeature" in timeline using the Feature.isSuppressed Property

 

In API reference I find the following

 

 

feature_var.isSuppressed = True

 

 

Where "feature_var" is a variable referencing a Feature object.

QUESTION
What the right way to reference a Feature by its name? I suppose something like this...

getFeatureByName("myFeature").isSuppressed = True

Please can you tell me the right syntax?

 

7 REPLIES 7
Message 2 of 8

Hello,

 

Try something like this :

 

features= rootComp.features

myFeature = features.itemByName('myFeature')

myFeature.deleteMe()
Message 3 of 8

Screenshot 2020-05-17 at 20.03.52.png

 

@JeromeBriot thanks, it works!

Message 4 of 8

Sorry, I should have posted a complete code:

import adsk.core, adsk.fusion, traceback

def run():
        
    app = adsk.core.Application.get()
    ui = app.userInterface

    try:

        design = app.activeProduct

        rootComponent = design.rootComponent

        features= rootComponent.features

        myFeature = features.itemByName('myFeature')

        if myFeature:
            myFeature.deleteMe()

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

itemByName seems to works only if the feature belongs to root component. But I'm not able to reference a feature inside a child component.

In the example below I unsuccessfully try to suppress featureB inside childComponent:1

Screenshot 2020-05-19 at 14.07.43.png

 

The code used is the following

 

 

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app :adsk.core.Application = adsk.core.Application.get()
        ui  :adsk.core.UserInterface = app.userInterface
        design :adsk.fusion.Design = app.activeProduct
        rootComp = design.rootComponent
        features = rootComp.features

        features.itemByName("featureB").isSuppressed = True


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

 

 

 

The error I get is the following one

Screenshot 2020-05-19 at 14.10.07.png

 

Instead, everything works well if I try tu suppress featureA (belonging to root component) using the same code above.

Screenshot 2020-05-19 at 14.12.18.png

 

Is there some syntax to reference features inside subcomponents? E.g. something like "childComponent1:/featureB".

Message 6 of 8

Hello,

 

You can iterate through all the components in the current design:

 

import adsk.core, adsk.fusion, traceback

def run(context):
        
    app = adsk.core.Application.get()
    ui = app.userInterface

    try:

        design = adsk.fusion.Design.cast(app.activeProduct)

        allComponents = design.allComponents

        for component in allComponents:

            features = component.features

            feature = features.itemByName('featureA')

            if feature:
                ui.messageBox('{} - {}'.format(component.name, feature.name))
                feature.deleteMe()

            feature = features.itemByName('featureB')

            if feature:
                ui.messageBox('{} - {}'.format(component.name, feature.name))
                feature.deleteMe()

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

 

Message 7 of 8

Thanks! It works

Message 8 of 8

I've run the script with a more complex design. The differences are as follows:

  • nested components (on multiple levels)
  • shared designs
  • sheet metal bended components.

While iterating trough components, I get this error:

 

Screenshot 2020-05-28 at 13.22.01.png

 

I cannot understand the meaning. Some advice? Thanks.

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

Post to forums  

Technology Administrators