Automatically "Set" & "Read" Fusion 360 Attributes

Automatically "Set" & "Read" Fusion 360 Attributes

isocam
Collaborator Collaborator
575 Views
3 Replies
Message 1 of 4

Automatically "Set" & "Read" Fusion 360 Attributes

isocam
Collaborator
Collaborator

Can anybody help?

 

I have the following Python code to set and read a parts attribute.....

 

import adsk.core

 

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

ui = app.userInterface

selectionPrompt = 'Select a component.'

selectionFilter = 'Occurrences'

selectedOccurence = ui.selectEntity(selectionPrompt, selectionFilter)

selectedComponent = selectedOccurence.entity.component

compAttributes = selectedComponent.attributes

AttributeGroupName = 'DarrensAttributes'

AttributeName = "Part: Type"

AttributeValue = "FLAME CUT PROFILE"

newAttribute = compAttributes.add(AttributeGroupName, AttributeName, AttributeValue)


# Read Attribute

readAttribute = compAttributes.itemByName(AttributeGroupName, AttributeName)

attributeValue = readAttribute.value

textString = "Attribute value for " + AttributeName + " = \n" + attributeValue

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

 

Does anybody know how to modify the code so that I do not have to physically select the component first, I want it to automatically select the component for me.

 

Many thanks in advance!

 

Kind Regards

 

Darren

0 Likes
576 Views
3 Replies
Replies (3)
Message 2 of 4

Jorge_Jaramillo
Collaborator
Collaborator

@Jorge_Jaramillo - this post is being edited to remove PII.

 

Hi,
As you can have more than one component in the design, which one you want to be selected by default?
Or do you want the root component to be selected?

Regards,
Jorge Jaramillo

0 Likes
Message 3 of 4

isocam
Collaborator
Collaborator

Jorge,

 

It will always be the root component.

 

I will only run the AddIn when one component is active on screen.

 

Kind Regards

 

Darren

0 Likes
Message 4 of 4

Jorge_Jaramillo
Collaborator
Collaborator

@Jorge_Jaramillo - this post is being edited to remove PII.

 

Hi @isocam ,

 

You just need to make the following change in you code:

 

    # selectionPrompt = 'Select a component.'
    # selectionFilter = 'Occurrences'
    # selectedOccurence = ui.selectEntity(selectionPrompt, selectionFilter)
    # selectedComponent = selectedOccurence.entity.component
    selectedComponent = adsk.fusion.Design.cast(app.activeProduct).rootComponent

 

Comment out the marked lines and add the last one.

 

For the script to run correctly, Fusion 360 has to be in design mode.

 

Regards,

Jorge Jaramillo