Determine if document is a "Part" or a "Assembly

Determine if document is a "Part" or a "Assembly

isocam
Collaborator Collaborator
337 Views
2 Replies
Message 1 of 3

Determine if document is a "Part" or a "Assembly

isocam
Collaborator
Collaborator

Can anybody help?

 

Is it possible, using a Python script to determine if the document that is opened, is a "Part" or an "Assembly" and show this in a message box?

 

Eg

 

The document is a Part

 

The document is a Assembly

 

Many thanks in advance!

 

Darren

0 Likes
338 Views
2 Replies
Replies (2)
Message 2 of 3

kandennti
Mentor
Mentor

Hi @isocam .

 

I interpreted this as determining if Occurrence had a link.

 

You can use the isReferencedComponent property to determine this.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-6D3CD4ED-570D-433D-97AD-EB110DA94177 

 

# Fusion360API Python script
import adsk.core, adsk.fusion, traceback

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

        for occ in root.allOccurrences:
            print(f'{occ.name} : {occ.isReferencedComponent}')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

0 Likes
Message 3 of 3

BrianEkins
Mentor
Mentor

Fusion doesn't differentiate between a part and an assembly, so it's up to us to define what a part or assembly is.

 

The pure definition is a part is a component that only contains bodies and not any occurrences.  An assembly is a component that only contains occurrences and no bodies.  Anything else is some kind of a part and assembly hybrid.  Here's some code that determines this.

 

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

        if root.occurrences.count > 0 and root.bRepBodies.count > 0:
            ui.messageBox('Document is a hybrid of assembly and part.')
        elif root.occurrences.count > 0 and root.bRepBodies.count == 0:
            ui.messageBox('Document is an assembly.')
        elif root.occurrences.count == 0 and root.bRepBodies.count > 0:
            ui.messageBox('Document is a part.')
        elif root.occurrences.count == 0 and root.bRepBodies.count == 0:
            ui.messageBox('Document is an empty part.')
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 

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