Cannot select object in component using activeSelections

Cannot select object in component using activeSelections

thomasa88
Advocate Advocate
1,628 Views
9 Replies
Message 1 of 10

Cannot select object in component using activeSelections

thomasa88
Advocate
Advocate

I'm trying to select a sketch that resides within a component. However, I get a runtime error.

 

The below snippet shows trying to select a sketch in a component and then a sketch that resides in the rootComponent.

ui.activeSelections.add(app.activeProduct.rootComponent.occurrences.item(0).component.sketches[0])
RuntimeError: 3 : invalid argument entity
ui.activeSelections.add(app.activeProduct.rootComponent.sketches[0])
True

 If I manually select the sketch in the timeline and grab the selection from activeSelections, I can add it back to activeSelections. So it seems the object I select manually is wrong in some way?

1,629 Views
9 Replies
Replies (9)
Message 2 of 10

kandennti
Mentor
Mentor

Hi @thomasa88 .

 

I also tried.
BRepBody etc. of Component other than RootComponent cannot be added too.

This feels like a bug...

Message 3 of 10

thomasa88
Advocate
Advocate

I just found that the component itself cannot be selected, when accessed as parentComponent. It can be selected when being accessed on its own.

The rootComponent can be selected when accessed as parentComponent. So it seems something is wrong with the child entity.

Sample document: https://a360.co/39AIqw9

Item 0 is a sketch within the rootComponent. Item 2 is a sketch within a component (item 1)

 

t=design.timeline
ui.activeSelections.clear()
True
e0=t[0].entity

e2=t[2].entity

ui.activeSelections.add(e0.parentComponent)
True
ui.activeSelections.add(e2.parentComponent)
RuntimeError: 3 : invalid argument entity


e1=t[1].entity

e1 == e2.parentComponent
False                      # Expected this to be true

app.activeProduct.rootComponent == e0.parentComponent
True

 

 

Edit.

I was wrong about the component being selectable on its own. Its Occurence is selectable.

ui.activeSelections.add(root.allOccurrences[0].component.sketches[0])
RuntimeError: 3 : invalid argument entity
ui.activeSelections.add(root.allOccurrences[0].component)
RuntimeError: 3 : invalid argument entity
ui.activeSelections.add(root.allOccurrences[0])
True
0 Likes
Message 4 of 10

thomasa88
Advocate
Advocate

Bump.

 

Is there any way to know if this bug has been picked up by Autodesk?

 

I understand that it might not be solved in foreseeable time, but it would be good to know that it's known. (Also, I guess this is just a generation error)

 

It's really blocking my vertical timeline add-in.

 

screenshot.png

 

 

Message 5 of 10

BrianEkins
Mentor
Mentor

This isn't a bug but is working as expected.  To start with it's a good idea to read this article to get an understanding of what a proxy is.

 

http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-88A4DB43-CFDD-4CFF-B124-7EE67915A07A

 

Below is a sample script that demonstrates getting and selecting a sketch in a sub occurrence.  I've also attached the assembly I used to test it.  I created a new component (which internally created a new component) and then added a sketch to that component.  I then copied that occurrence and re-positioned the new one so I see two sketches.  The second occurrence is referencing the same component as the first so you're looking at two instances of the same sketch.  If you make a change to one you'll see the other one change.  That's because there is really only one. 

 

That's why trying to add that sketch to the active selection doesn't work because the sketch belongs to another component and just providing the sketch isn't enough for Fusion to know how to use it in the context of the root component.  In this case there are two instances of the sketch so Fusion doesn't know which one should be selected.  Creating a proxy specifies which occurrence the sketch is relative to.  You can see I create two proxies, one for each occurrence and by changing which one is passed to the active selection it will select one or the other.

 

def run(context):
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        des = adsk.fusion.Design.cast(app.activeProduct)
        root = des.rootComponent

        occ1 = root.occurrences.item(0)
        occ2 = root.occurrences.item(1)

        sk = occ1.component.sketches.item(0)
        skProxy1 = sk.createForAssemblyContext(occ1)
        skProxy2 = sk.createForAssemblyContext(occ2)

        ui.activeSelections.add(skProxy1)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) 
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 6 of 10

thomasa88
Advocate
Advocate

I see. So I need to find all occurencies and make proxies through them.

 

I tried going from from the Timeline and check the entries.

 

For a sketch, there is the createForAssembly() function you mention. However, I need to find the occurences to create the proxies. I tried going via the parentComponent, but I could not find any occurrences through allOccurrencesor occurrences.

Then I realized that this works:

 

> e=t[2].entity
> e.classType()
'adsk::fusion::Sketch'
> app.activeProduct.rootComponent.occurrencesByComponent(e.parentComponent).count
2

 

 

 

However for a BaseFeature feature, say BoxFeature, there is no createForAssemblyContext. Any idea how to solve that?

 

 

0 Likes
Message 7 of 10

thomasa88
Advocate
Advocate

Okay, I got it:

 

t=thomasa88lib.timeline.get_timeline()[1]
fe=t[1].entity
body=fe.bodies[0]
p=body.createForAssemblyContext(app.activeProduct.rootComponent.occurrencesByComponent(fe.parentComponent)[0])
ui.activeSelections.add(p)

 

Do you know if there would be cases where fe.parentComponent and body.parentComponent would return different values? I'm planning to loop the bodies, to get the same behavior as select in the normal timeline.

0 Likes
Message 8 of 10

thomasa88
Advocate
Advocate

Hm, there seems to be no way to edit a post that is too old, so some more spamming instead.

 

The solution I wrote above, selecting the bodies for e.g. BoxFeature does not select the actual feature (which has implications when trying to run the edit command on the selection). Is there a correct way to select a Feature?

 

.assemblyContext is None and there is no createForAssemblyContext. Selecting a BoxFeature in the GUI, I can see that ui.activeSelections has a proxy tothat feature.

 

Also, I found that I have to use allOccurrencesByComponent to find nested occurrences.

0 Likes
Message 9 of 10

BrianEkins
Mentor
Mentor

I would like to understand what you're trying to accomplish.  It's not a typical workflow to have a reference to an entity and then try to find an occurrence path to that entity.  One big problem with this approach is there can be more than one path.  That was demonstrated in my earlier response where there are two occurrences of the part that contains the sketch.  In that case there would be two valid paths to the sketch.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 10 of 10

thomasa88
Advocate
Advocate

Yes, in this case, I'm making a vertical timeline (so names can easily be seen and edited) and I want the "Select item in timeline" functionality. I also want the user to be able to double click a feature and open the edit dialog for that feature. For sketch and extrude this works fine now, but, for a Box, the edit dialog comes up with "empty" values, as I have not managed to select the box, but just the bodies of the box.

 

https://github.com/thomasa88/VerticalTimeline

(I'm planning to rework the user interaction, but for now, click and double-click on the lines, outside of

the name, to see the behavior.)

 

 

In another case, I just want to highlight what entity the user is naming: https://github.com/thomasa88/DirectName (For people finding this link, I hope to soon have the add-in in the app store)

My current detection uses inputChanged, so the correct entity is not highlighted until after the user has modified a name.

0 Likes