AutoCAD quick publish
FileEditViewInsertFormatHelp

AutoCAD quick publish

Two lisp scripts for quickly creating PDFs

When working with drawings in AutoCAD every day, the plot dialogue quickly becomes tedious to navigate. You can save the sheet layout but not the save location. The save dialogue always defaults to the last folder you saved to. Most of the time, all you want is a PDF with the same name as the drawing, saved in the same folder. Here are two scripts to do that.

Requirements

For these scripts to work, they need the plot settings to be pre-configured. The printer must be set to "DWG TO PDF.pc3"; this makes sure the output is to a PDF rather than a physical printer. The rest of the plot settings just need to be set to whatever suits the sheet. If you don't want to have to set up the plot settings for every sheet,  make sure the template you use has it pre-configured.

How to use them

Create a plane text file with the extention ".lsp" and write one or both methods to it. In AutoCAD go to Manage -> Load Application -> Select the file you created -> click load. If you want the script to load every time AutoCAD opens then open the same Load Applications dialog -> in the corner is startup Suite -> add the file you created.
With the scripts loaded just type "QPLOT" or "QBPLOT" to run them.

Single sheet

This method takes the active sheet and saves it as a PDF in the same folder as the drawing, with the same name. If the PDF already exists, it will overwrite it.

Quick plot
(defun c:QPlot ( / acadApp doc layout plot configName filename filepath fullpdf )
  ;; Get AutoCAD application and document
  (setq acadApp (vlax-get-Acad-Object))
  (setq doc (vla-get-ActiveDocument acadApp))

  ;; Get the current layout
  (setq layout (vla-Item (vla-get-Layouts doc) (getvar 'ctab)))

  ;; Get the configured plot device
  (setq configName (vla-get-ConfigName layout))

  ;; Check for DWG To PDF.pc3
  (if (equal (strcase configName) "DWG TO PDF.PC3")
    (progn
    ;; Build full output path
      (setq filename (vl-filename-base (getvar 'DWGNAME)))
      (setq filepath (getvar 'DWGPREFIX))
      (setq fullpdf (strcat filepath filename ".pdf"))

      ;; Turn off background plotting to avoid issues
      (setvar 'BACKGROUNDPLOT 0)

      (setq currentPlot (vla-get-Plot doc))
      (vla-PlotToFile currentPlot fullpdf)
      (princ (strcat "\nPDF created at: " fullpdf))
    )
    (princ "\nCurrent layout is not set to DWG To PDF.pc3.")
  )
  (princ)
)

Batch plot

For drawings with multiple sheets, an alternative method is needed. This method takes all the sheets in the active document and outputs them into a single PDF, again in the same folder and with the same name as the drawing. It uses the same batch plot/publish method, but without the UI. To get it to work, the method has to create a DSD file which acts as a template telling AutoCAD what to publish. It then runs the publish method with this template and deletes the temporary template when complete.

Quick batch plot
(defun c:QBPlot ( / acadApp doc layouts lay layoutList filename filepath fullpdf dsdFile fp )
  ;; Active document
  (setq acadApp (vlax-get-acad-object))
  (setq doc (vla-get-ActiveDocument acadApp))

  ;; Output PDF name
  (setq filename (vl-filename-base (getvar "DWGNAME")))
  (setq filepath (getvar "DWGPREFIX"))
  (setq fullpdf (strcat filepath filename ".pdf"))

  ;; Temporary DSD file
  (setq dsdFile (strcat filepath filename "_temp.dsd"))

  ;; Collect layouts (excluding Model)
  (setq layoutList '())
  (vlax-for lay (vla-get-Layouts doc)
    (if (/= (strcase (vla-get-Name lay)) "MODEL")
      (setq layoutList
        (cons
          (cons (vla-get-TabOrder lay)
                (vla-get-Name lay))
          layoutList
        )
      )
    )
  )

  ;; Sort by tab order
  (setq layoutList
    (vl-sort layoutList
      '(lambda (a b)
        (< (car a) (car b))
      )
    )
  )

  ;; Write DSD file
  (setq fp (open dsdFile "w"))

  (write-line "[DWF6Version]" fp)
  (write-line "Ver=1" fp)

  ;; One sheet entry per layout
  (setq n 0)
  (foreach lay layoutList
    (write-line (strcat "[DWF6Sheet:" (itoa n) "]") fp)
    (write-line (strcat "DWG=" (getvar "DWGNAME")) fp)
    (write-line (strcat "Layout=" (cdr lay)) fp)
    (write-line "Setup=" fp)
    (write-line "OriginalSheetPath=" fp)
    (setq n (1+ n))
  )

  ;; Target PDF
  (write-line "[Target]" fp)
  (write-line "Type=6" fp)
  (write-line (strcat "DWF=" fullpdf) fp)

  (close fp)

  ;; Disable background plotting
  (setvar "BACKGROUNDPLOT" 0)

  ;; Publish the DSD
  (setvar "filedia" 0)
  (command "-publish" dsdFile)
  (setvar "filedia" 1)

  ;; Delete temporary DSD
  (vl-file-delete dsdFile)

  (princ (strcat "\nCreated: " fullpdf))
  (princ)
)
TG logoStart
Transient Geometry
Programs▶
Documents
Minesweeper
11:52 PM