Unique component id

Unique component id

lichtzeichenanlage
Advisor Advisor
1,689 Views
6 Replies
Message 1 of 7

Unique component id

lichtzeichenanlage
Advisor
Advisor

Situation: 

 

I have a design with local and linked components:

 

1|-component.name="Fred:1"

2|-component.name="Fred:2" # instance of 1

3|-component.name="Hilde:1"

4|-component.name="Fred (1):1"

5|-component.name="Fred v1:1" # linked component named Fred

6|-component.name="Fred v1:1" # another linked component with the same name

7|-component.name="Fred v1:2" # instance of 6

8|-component.name="Klaus"

9|-component.name="Klaus" sub-component.name=Fred:1 

10|-component.name="Rudi"

11|-component.name="Rudi" sub-component.name=Fred:1 # instance of 1

 

I now want to get all component but only one component from each type and only if it does not have sub-components. So the result should be something like: 1, 3, 4,  5, 6, 9

 

Iterating over the list is not a problem. But is there a way to identify an object? Something like an unique id?

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

lichtzeichenanlage
Advisor
Advisor

Any hint? The information should be available within Fusion because that's what the Inspect -> Component Color Cycling Toggle is the kind of filter I'm looking for. 

0 Likes
Message 3 of 7

BrianEkins
Mentor
Mentor
Accepted solution

I'm not sure I follow exactly what you're trying to accomplish but if I am understanding correctly, it seems that your problem can be solved if you can compare one component with another to see if they're the same.  In the Fusion 360 API you can compare API objects directly to see if they represent the same entity in Fusion 360.

 

Here's a little script that demonstrates what I think you're trying to accomplish.

def compareComponents():
    ui = None
    
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        
        compList = []
        occList = []
        occ = adsk.fusion.Occurrence.cast(None)
        for occ in design.rootComponent.occurrences:
            foundComp = False
            comp = adsk.fusion.Component.cast(None)
            for comp in compList:
                if comp == occ.component:
                    foundComp = True
                    break
            
            if not foundComp:
                occList.append(occ)
                compList.append(occ.component)

        result = ''
        for occ in occList:
            result += occ.name + '\n'
            
        ui.messageBox(result)
    except:  
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))   

 

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

lichtzeichenanlage
Advisor
Advisor

Thanks for the answer. I'm new to python. I can read it, but writing...  My problem was, that I've compared comp with occ and my thought was, that the comparison was not implemented.  

 

if comp == occ.component:
0 Likes
Message 5 of 7

lichtzeichenanlage
Advisor
Advisor

One additional question: Why are you maintaining a components list and an occurrences list? The name is part from the component, too. Isn't it?

0 Likes
Message 6 of 7

lichtzeichenanlage
Advisor
Advisor

Based on your feedback I've create a little add-in that filters the STL export. If anybody is interested in the 0.1 result, please check this thread. Welcome are code reviews (I'm not a Python programmer, never used the Fusion API), feature suggestions, tests etc.

0 Likes
Message 7 of 7

BrianEkins
Mentor
Mentor
The reason for the two lists is that I need the list of components for the comparison and because in your original description of the problem you had the occurrence names, so the list of occurrences is just to display their name.
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes