Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Determine if open document is a Drawing

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
isocam
159 Views, 4 Replies

Determine if open document is a Drawing

Can anybody help?

 

Is it possible, using a Fusion 360 Python script to determine if an open document is a "Drawing"?

 

Many thanks!!!!

 

Darren

4 REPLIES 4
Message 2 of 5
kandennti
in reply to: isocam

Hi @isocam .

 

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core


def run(context):
    ui: adsk.core.UserInterface = None
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        doc: adsk.core.Document = app.activeDocument

        if doc.objectType == 'adsk::drawing::DrawingDocument':
            msg = '.f2d'
        elif doc.objectType == 'adsk::fusion::FusionDocument':
            msg = '.f3d or .f3z'
        else:
            msg = 'pcb'

        ui.messageBox(msg)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Message 3 of 5
zxynine
in reply to: kandennti

Its not always a good idea to use strings to represent class types. A more idealised version (IMO) would be:

 

# Fusion360API Python script

import traceback
import adsk.fusion, adsk.core, adsk.drawing

def run(context):
    ui: adsk.core.UserInterface = None
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        doc: adsk.core.Document = app.activeDocument

        if doc.objectType == adsk.drawing.DrawingDocument.objectType:
            msg = '.f2d'
        elif doc.objectType == adsk.fusion.FusionDocument.objectType:
            msg = '.f3d or .f3z'
        else:
            msg = 'pcb'

        ui.messageBox(msg)

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

 

Message 4 of 5
zxynine
in reply to: isocam

You can also just use type to compare so:

 

 

 

 

# Fusion360API Python script

import traceback
import adsk.fusion, adsk.core, adsk.drawing

def run(context):
    ui: adsk.core.UserInterface = None
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        docType: type = type(app.activeDocument)

        if docType is adsk.drawing.DrawingDocument:
            msg = '.f2d'
        elif docType is adsk.fusion.FusionDocument:
            msg = '.f3d or .f3z'
        else:
            msg = 'pcb'

        ui.messageBox(msg)

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

 

 

 

 

Or you can use the 'isinstance' func:

 

 

 

 

# Fusion360API Python script

import traceback
import adsk.fusion, adsk.core, adsk.drawing

def run(context):
    ui: adsk.core.UserInterface = None
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        doc: adsk.core.Document = app.activeDocument

        if isinstance(doc,adsk.drawing.DrawingDocument):
            msg = '.f2d'
        elif isinstance(doc,adsk.fusion.FusionDocument):
            msg = '.f3d or .f3z'
        else:
            msg = 'pcb'

        ui.messageBox(msg)

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

 

 

 

 

Im sure there's a slight difference in the performance of each, however for general purposes, they are all functionally equivalent. 

Message 5 of 5
kandennti
in reply to: zxynine

@zxynine .

 

Certainly, this is safer, since strings can be changed.
I learned a lot.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report