Mass PDF Drawing Export

Mass PDF Drawing Export

bmxjeff
Collaborator Collaborator
2,153 Views
7 Replies
Message 1 of 8

Mass PDF Drawing Export

bmxjeff
Collaborator
Collaborator

I'm working on a large project. Like 200 Prints+ Does anyone know of a way to export The Drawings in PDF for a Project? 

 

I found one for STEP files. One for reporting the PDF's would save me .. like 2 days of opening files : ) 

 

Thanks!

Jeff Hooper (Owner)
Hooper Machine and Design
[X] AUTODESK AMATUR ORDINARY
0 Likes
2,154 Views
7 Replies
Replies (7)
Message 2 of 8

BrianEkins
Mentor
Mentor

There's almost not drawing functionality exposed through the API. The one thing that is there is the functionality to export PDF. Here's a small sample that illustrates it.

import adsk.core, adsk.drawing, traceback

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

        # Get the Drawing object.
        drawing = adsk.drawing.Drawing.cast(app.activeProduct)
        if drawing is None:
            ui.messageBox('A drawing must be active.')
            return

        expMgr: adsk.drawing.DrawingExportManager = drawing.exportManager
        pdfOptions = expMgr.createPDFExportOptions('C:\Temp\Sheet1.pdf')
        pdfOptions.sheetRange = "1"
        expMgr.execute(pdfOptions)

        pdfOptions = expMgr.createPDFExportOptions('C:\Temp\Sheet2.pdf')
        pdfOptions.sheetRange = "2"
        expMgr.execute(pdfOptions)

        pdfOptions = expMgr.createPDFExportOptions('C:\Temp\SheetAll.pdf')
        pdfOptions.sheetsToExport = adsk.drawing.PDFSheetsExport.AllPDFSheetsExport
        expMgr.execute(pdfOptions)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

You might still run into some issues depending on what you need to do. As I said, very little drawing functionality is exposed through the API. Something that is not there is accessing the sheets. If you need to open a drawing and export each sheet as PDF, that's not easy because you can't find out how many sheets there are. I guess you could write out sheet 1, and sheet 2, and keep going until it fails. Of course, if you can't access sheets, you can't get any information about the sheet, like it's name, if you wanted to use that as part of the filename. 

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

gvisca44
Advocate
Advocate

Try this: @bmxjeff 

 

When you run it - you are prompted to select all the drawings (multiselect).  Then prompted for an output file location.  Then set and wait ... but a hell of a lot faster that doing it manually.

 

No error checking / drawing validity checking ... just a very simple export of what you've created.

 

PDF Export options are in the exportDrawingsToPDF function.  Change them as you see fit.

 

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

app = None
ui = None
design:adsk.fusion.Design = None

def run(context):
    global app, ui, design
    ui = None

    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        drawingFiles:adsk.core.DataFiles = getDrawingFiles()
        adsk.doEvents()

        #outputFolder:adsk.core.DataFolder = getDrawingOutputFolder()
        outputDirectory = getDrawingOutputDirectory()

        for drawingFile in drawingFiles:
            #doc:adsk.core.Document = app.documents.open(drawingFile) 
            drawingDoc:adsk.drawing.DrawingDocument = app.documents.open(dataFile=drawingFile, visible=True)
            #drawingFile.
            exportDrawingToPdf(drawingDoc, outputDirectory)
            drawingDoc.close(saveChanges=False)



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

def getDrawingFiles() -> adsk.core.DataFiles:

    cloudDialog = ui.createCloudFileDialog()
    cloudDialog.title = 'Select drawings to export.'
    cloudDialog.isMultiSelectEnabled = True
    cloudDialog.filter = 'f2d'

    dialogResult = cloudDialog.showOpen()
    if dialogResult == adsk.core.DialogResults.DialogOK:
        #dataFolder:adsk.core.DataFolder = cloudDialog.dataFolder
        dataFiles:adsk.core.DataFiles = cloudDialog.dataFiles
        return dataFiles

def getDrawingOutputFolder() -> adsk.core.DataFolder:
    cloudDialog = ui.createCloudFileDialog()
    cloudDialog.title = 'Select output folder.'

    dialogResult = cloudDialog.showSave()
    if dialogResult == adsk.core.DialogResults.DialogOK:
        dataFolder:adsk.core.DataFolder = cloudDialog.dataFolder
        dataFiles:adsk.core.DataFiles
        return dataFolder
    
def getDrawingOutputDirectory():
    folderDialog = ui.createFolderDialog()
    folderDialog.title = 'Selection output folder.'
    dialogResult = folderDialog.showDialog()

    if dialogResult == adsk.core.DialogResults.DialogOK:
        foldername = folderDialog.folder
        return foldername
    else:
        return
    
def exportDrawingToPdf(
        drawingDoc:adsk.drawing.DrawingDocument, 
        #outputFolder:adsk.core.DataFolder
        outputDir:str
    ):

    msg = f'{drawingDoc.name}'
    app.log(msg)

    #msg = f'{outputFolder.name}'
    #app.log(msg)

    msg = f'{outputDir}'
    app.log(msg)

    outputFileName = drawingDoc.name

    filename = outputDir + '/' + outputFileName + '.pdf'

    exportManager:adsk.drawing.DrawingExportManager = drawingDoc.drawing.exportManager
    PDFOptions:adsk.drawing.PDFExportOptions = exportManager.createPDFExportOptions(filename)
    PDFOptions.filename = filename
    PDFOptions.sheetsToExport = adsk.drawing.PDFSheetsExport.AllPDFSheetsExport
    PDFOptions.openPDF = False
    PDFOptions.useLineWeights = False

    exportOK = exportManager.execute(PDFOptions)

    return exportOK

 

Message 4 of 8

markstorer
Explorer
Explorer

Many thanks for this - It was exactly what I was looking for !!!.   (During the process of creating/ installing your script, I even learnt Python )

0 Likes
Message 5 of 8

markstorer
Explorer
Explorer

Many thanks for this - It was exactly what I was looking for !!!.   

0 Likes
Message 6 of 8

bmxjeff
Collaborator
Collaborator

Thanks for your work on this Glenn! 

Jeff Hooper (Owner)
Hooper Machine and Design
[X] AUTODESK AMATUR ORDINARY
0 Likes
Message 7 of 8

zenmech-015
Explorer
Explorer

Thanks for the script, it works. By chance does anyone know of a script to auto update drawings to latest reference of 3d models instead of opening it.

0 Likes
Message 8 of 8

kandennti
Mentor
Mentor

Hi @zenmech-015 -san.

I don't think there's a way to update without opening the document.

If you open the document, the DrawingDocument.updateAllReferences method should be useful.
https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-27FD805F-2A69-462F-B255-213DA00BA85A 

0 Likes