What invalidates a Product?

What invalidates a Product?

Anonymous
Not applicable
443 Views
1 Reply
Message 1 of 2

What invalidates a Product?

Anonymous
Not applicable

A piece of code I've been working on was flagged in code review, and we haven't been able to resolve this by reading the documentation.  Suppose we want to get the active product and do some work with it.  Previously, we had a line like

 

app = adsk.core.Application.get()
design = app.activeProduct

every time we wanted to do that, which seemed pretty wasteful, so we refactored it such that they were global variables.  Then we realized that the active product probably changes if they switch to a new design without closing the application, thus we used this getter function instead

 

design = None

def getDesign():
    global design
    if design is not None and design.isValid():    
        return design
    else:
        return app.activeProduct

however we aren't sure when exactly a Product object will be invalidated - the documentation just has this to say about the isValid property:  "Indicates if this object is still valid, i.e. hasn't been deleted or some other action done to invalidate the reference".

 

Does switching the design being worked on invalidate the reference, i.e. is `design` a reference to a specific design, or to `app.activeProduct` whatever that may be?  Or even more generally, what is the best way to get the active product while minimizing how often we need to get it from the app?

0 Likes
Accepted solutions (1)
444 Views
1 Reply
Reply (1)
Message 2 of 2

ekinsb
Alumni
Alumni
Accepted solution

All Fusion API objects support the isValid property.  It indicates if the reference you're holding to the Fusion object is still valid.  A reference can become invalid for many reasons.  For example, if you have a reference to a sketch line and the user deletes the line.  In the case of a Design, it can become invalid when the user closes that document that contains the design.

 

When you get a design it doesn't matter how you obtained it, the reference you have it to a specific design.  For example, if you use the Application.activeProduct property to get the active design, the design returned is for the design in the currently active document.  If the user activates another document so the activeProduct is not a different design, that doesn't change the reference you already have, which is to the document that was previosly active and t's still valid, as long as the user hasn't closed it.

 

If you always want to work on the design that the user is looking at (the active product) then you'll need to get it each time.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog