Activate component with code

Activate component with code

erik
Advocate Advocate
1,848 Views
6 Replies
Message 1 of 7

Activate component with code

erik
Advocate
Advocate

I might be missing something but I can't find the feature to activate a custom made component.

In the API I found the feature to activate the root component but thats it.

 

Am I looking the wrong places or has this feature never been implemented?

 

- Erik

0 Likes
Accepted solutions (1)
1,849 Views
6 Replies
Replies (6)
Message 2 of 7

ekinsb
Alumni
Alumni
Accepted solution

The terminology you see in the user-interface makes this a little confusing.  The items listed in the browser in the user-interface are named "Component" but they aren't actually a component but are instead an occurrence or instance of a component.  You can have many occurrences that reference the same component.  Because of this activating a component doesn't make sense because it can be ambiguous when there is more than one.  Instead what you're activating is an occurrence.  In the API, the Occurrence object supports the activate method to do this.  This topic in the help also further explains this.  http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-88A4DB43-CFDD-4CFF-B124-7EE67915A07A


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 7

erik
Advocate
Advocate

Thank you Brian!

That was a huge oversight on my part. I am still trying to wrap my head around the structure of Components in Fusion.

 

When I use code to change the activated component it does change because I can extrude profiles and they show up in the component activated by the code.

But when I look to the left to check which component is active, none of them are. And the color on the extruded profile is displayed as though the component wasn't activated.

 

Do you know why?

 

- Erik

0 Likes
Message 4 of 7

ekinsb
Alumni
Alumni

Sorry for the slow response but I've been busy with Autodesk University.

 

With the API, it's not necessary to activate a component to be able to create content within it.  That's what the UI uses as a way for the user to indicate which component entities should be created in but when using the API you implicitly specify which component by which component you accessed the collections that you're using to create new entities.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 5 of 7

erik
Advocate
Advocate

I am not entirely sure I understand the last part of your post.

The thing is, I dont understand, why the browser doens't show any active component (with the radiobutton) and the body is grayish like they would be if another component where active.

 

I have linked a screencast where I show a short video of the workflow.

 

 

 

- Erik

 

0 Likes
Message 6 of 7

ekinsb
Alumni
Alumni

It does look like there's something odd going on in your video but I don't know what your program is doing or what's happened up to that point.

 

As far as what I said regarding that you don't need to activate components when using the API, here's an example.  In the UI if you want to create two new components and then add a sketch to the first component you would run the "New Component" command twice from the context menu of the root component.  Because the "New Component" command automatically activates the new component you need to activate the first component before creating the sketch so the sketch will be created in that component.

 

When using the API you can use the Occurrences collection you get from the root component and call the addNewComponent method twice to create two new components.  The method returns the occurrences that were created and from those I can get the components.  To create the new sketch I use the Component object that was created from the first addNewComponent call and get the Sketches collection from it and add a new sketch.  Because I got the Sketches collection from a specific component, Fusion knows where to create the sketch and it's completely independent of what's active in the UI.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 7 of 7

erik
Advocate
Advocate

That would make sense, but my addNewComponent doesn't activate the component. I have attached a new screen cast where I have commented the line of code activating my component.

 

 

 

I don't know how to turn of the bar on the left side of the screen cast, but first the new sketch creates a sketch in the root component, at when activating one of my new components it creates the sketch in that component.

 

This is the setup code:

#   setup Component
            newAOcc = _destinationAComp.occurrences.addNewComponent(adsk.core.Matrix3D.create())
            newBOcc = _destinationBComp.occurrences.addNewComponent(adsk.core.Matrix3D.create())
            
            newAComp = newAOcc.component
            newBComp = newBOcc.component
            
            #   apply appearance and name
            newAOcc.appearance = _allAppearances.itemByName(colorString)
            newBOcc.appearance = _allAppearances.itemByName(colorString)
            
            newAComp.name = colorString + ' A'
            newBComp.name = colorString + ' B'
            
            #newAOcc.activate()

I find the destinationComps through the root.occurrences.itemByName and destinationOcc.component.

I am wondering if it has something to do with me not calling it on the root and instead calling it on components on the root.

 

- Erik

 

0 Likes