Automatically create a PDF upon save of a Drawing?

Automatically create a PDF upon save of a Drawing?

gobluejd
Advocate Advocate
1,296 Views
10 Replies
Message 1 of 11

Automatically create a PDF upon save of a Drawing?

gobluejd
Advocate
Advocate

Is there anyway to automatically create a PDF of a drawing upon save?  Basically I want it to export as a PDF and possibly have the version # in the PDF file name?

 

Is that possible?  From what I can see it seems simple via an Add-in or using Python but I am no clue what to do.

0 Likes
Accepted solutions (2)
1,297 Views
10 Replies
Replies (10)
Message 2 of 11

kandennti
Mentor
Mentor
Accepted solution

Hi @gobluejd -San.

 

Interesting idea.

 

The UserInterface.commandTerminated event is fired when all commands are finished.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-E381414E-1CFF-4007-B1DF-C413D7E6841A 

 

This event should be used to export PDF files when the following conditions are met
・When a save command is executed.
・When the active document is a drawing.

 

We have created a simple add-in.
When you start the add-in execution, a dialog will appear to select a folder to export the PDF file.
After that, the PDF file will be exported every time the drawing is saved.
If you want to stop the function, please exit the add-in.

#FusionAPI_python addin

import traceback
import adsk.core as core
import adsk.drawing as drawing
import pathlib

_handlers = []
_exportDir = ""
_target_cmd_ids = [
    "PLM360SaveCommand",
]

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

        global _exportDir
        _exportDir = get_export_folder_path()
        if len(_exportDir) < 1: return

        global _handlers
        onCommandTerminated = MyCommandTerminatedHandler()
        ui.commandTerminated.add(onCommandTerminated)
        _handlers.append(onCommandTerminated)

        app.log(
            "Start addin(AutoPdfOnSave)"
        )

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


def stop(context):
    try:
        core.Application.get().log(
            "Stop addin(AutoPdfOnSave)"
        )
    except:
        print('Failed:\n{}'.format(traceback.format_exc()))


class MyCommandTerminatedHandler(core.ApplicationCommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args: core.ApplicationCommandEventArgs):

        global _target_cmd_ids
        if args.commandId not in _target_cmd_ids: return

        app: core.Application = core.Application.get()
        doc: drawing.DrawingDocument = drawing.DrawingDocument.cast(app.activeDocument)
        if not doc: return

        dataFile: core.DataFile = doc.dataFile
        filename = f"{dataFile.name} v{dataFile.versionNumber}.pdf"

        global _exportDir
        exportPath = str(
            pathlib.Path(_exportDir) / filename
        )

        drawExpMgr: drawing.DrawingExportManager = doc.drawing.exportManager
        expOpt: drawing.DrawingExportOptions = drawExpMgr.createPDFExportOptions(
            exportPath
        )
        drawExpMgr.execute(expOpt)

        app.log(
            f"Export PDF:{exportPath}"
        )


def get_export_folder_path(
) -> str:
    app: core.Application = core.Application.get()
    ui: core.UserInterface = app.userInterface

    dialog: core.FolderDialog = ui.createFolderDialog()
    dialog.title = "PDF Export Folder"
    res = dialog.showDialog()
    if res == core.DialogResults.DialogOK:
        return dialog.folder
    else:
        return ""

 

Each time you save, the exported log is written to a TextCommands window.

1.png

Message 3 of 11

gobluejd
Advocate
Advocate

Thank you! Going to look at now and try.

0 Likes
Message 4 of 11

gobluejd
Advocate
Advocate

I do not think it is working.  If it matters, I am on MAC.  Also the folder will be:

 

/Users/jeffreydickerson/Library/CloudStorage/Dropbox/Drawings

0 Likes
Message 5 of 11

kandennti
Mentor
Mentor

@gobluejd -San.

 

I don't use Dropbox, so I tried using OneDrive and was able to export PDFs without any problems.
I ran it on Win10, but since it doesn't do any OS-dependent processing, it should work on MAC as well, so there may be other problems.

 

Can I export to another local folder?
Also, PDF export is not supported by the personal license.

0 Likes
Message 6 of 11

gobluejd
Advocate
Advocate

I have never installed a script before. So I may have done wrong. I have a license and not the free one. 

maybe give brief description of how to install? All I did was unzip and put in Dropbox (maybe that’s it and has to be on local drive???)

 

I am at home now and will look at again tomorrow. 

thank you again!!!

0 Likes
Message 7 of 11

gobluejd
Advocate
Advocate

I also loaded it via the tools menu (I think) where you can upload scripts and point to directory. 

0 Likes
Message 9 of 11

gobluejd
Advocate
Advocate

I can not seem to get it to work.  I extract to my Documents on MAC.  I the open Fusion, go to Utilities, click add ins, then at top of menu select Add-Ins.  At the bottom, I select CREATE, made sure Add-In Radial button is selected.  Select Python, bane the add-in and select folder location on my MAC (Documents) (select the file folder (

Users/jeffreydickerson/Documents/AutoPdfOnSave) and then hit create.  I have tried opening an existing drawing, changing something minor and hit save and it doesn't do anything.  I have also tried creating a new part and drawing, I do not get any dialog on where to save.

 

I also have the text commands open and do not see anything happening.

 

 

 

0 Likes
Message 10 of 11

kandennti
Mentor
Mentor
Accepted solution

@gobluejd -San.

 

The add-in may not be registered properly.

Extract the zip file. After pressing the green +, specify the following folder

└ AutoPdfOnSave/
    └ AutoPdfOnSave/  <--here
        └ AutoPdfOnSave.manifest
        └ AutoPdfOnSave.py

The folders containing "xx.manifest" and "xx.py" must all have the same name, including the folder name.

 

If you see the following, registration is complete.

1.png

0 Likes
Message 11 of 11

gobluejd
Advocate
Advocate

Got it! Thank you so much.