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.
