Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Determine if open document is a Drawing

isocam
Collaborator

Determine if open document is a Drawing

isocam
Collaborator
Collaborator

Can anybody help?

 

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

 

Many thanks!!!!

 

Darren

0 Likes
Reply
Accepted solutions (1)
354 Views
4 Replies
Replies (4)

kandennti
Mentor
Mentor

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()))
0 Likes

zxynine
Enthusiast
Enthusiast

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()))

 

1 Like

zxynine
Enthusiast
Enthusiast

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. 

1 Like

kandennti
Mentor
Mentor
Accepted solution

@zxynine .

 

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

1 Like