Occurrence is visible strange behaviour

Occurrence is visible strange behaviour

rolandas_vegis
Advocate Advocate
1,005 Views
2 Replies
Message 1 of 3

Occurrence is visible strange behaviour

rolandas_vegis
Advocate
Advocate

Hello,

I have a model (see attachment). As you can see only the root component and the assembly components have visibility turned on. If I go from rootComponent->allOccurrences() I get the expected values from occurrence->isVisible(). However if I iterate through assembly level by level, i.e. rootComponent->occurrences(), and then through children of those I get that occurence->isVisible() always returns true.

 

Is there some logic behind it or is this a bug?

0 Likes
Accepted solutions (1)
1,006 Views
2 Replies
Replies (2)
Message 2 of 3

kandennti
Mentor
Mentor

Hi rolandas.vegis.

 

1.png

 

 

 

Although it may be hard to understand in Japanese,
I think that it is processing correctly.

 

 

 

 

 

 

import adsk.core, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        des = adsk.fusion.Design.cast(app.activeProduct)
        root = des.rootComponent
        
        occ_info = [get_occ_info(occ) for occ in root.allOccurrences]
        
        ui.messageBox('\n'.join(occ_info))
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def get_occ_info(occ):
    return 'name-{} , isLightBulbOn-{} , isVisible-{}'.format(
            occ.name, occ.isLightBulbOn, occ.isVisible)
0 Likes
Message 3 of 3

BrianEkins
Mentor
Mentor
Accepted solution

The behavior you're seeing is correct and there's a reason that your program isn't working and the one by @kandennti is.  The reason is "context".  Because you're getting the component and then getting the occurrence visibility you're getting the visibility in the context of the component.  In kandennti's program he's using the allOccurrences property to get all of the occurrences relative to the root component, which means they're all returned in context of the assembly.

 

Here's an example that hopefully helps to better illustrate this concept. Visibility of occurrences is managed at the top level of an assembly. You can see in the example below that it has to be this way for it work correctly. Notice that there are three instances of the Component5 subassembly.  I'm able to set the visibility of the occurrences in each one of those instances independently.  If we look at Component5 by itself, it contains Component6 and Component7. But in the context of the entire assembly there are three Component6 and three Component7 and each one of those can be controlled independently.  Even though there is actually only one real  Component5 component, there are three occurrences in the assembly that reference that component.  In the context of the top assembly these are three unique occurrences and can support some unique settings like visibility, color, and position.  To be able to reference something in the top assembly that exists somewhere else, you use a "proxy" object.  A proxy is an object that is representing an object in a different context. You can read more about this concept in the API help.

 

Visibility.png

 

Here's the code that generates the output above.  It's not using allOccurrences, but is traversing the assembly as you did but instead of going to the Component it's using childOccurences to get the occurrences from an occurrence. This returns proxies of the occurrences so they're in the context of the top-level assembly. It uses a recursive function to be able to iterate over an entire assembly regardless of the depth of the levels.

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

        getOccurrenceVisibility(root.occurrences, 0)        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def getOccurrenceVisibility(occs, level):
    spaces = ''.ljust(level*4)
    occ = adsk.fusion.Occurrence.cast(None)
    for occ in occs:
        print(spaces + occ.name + ':' + str(occ.isVisible))

        getOccurrenceVisibility(occ.childOccurrences, level+1)

 

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com