Export to PDF Lisp Routine

Export to PDF Lisp Routine

Anonymous
Not applicable
8,877 Views
48 Replies
Message 1 of 49

Export to PDF Lisp Routine

Anonymous
Not applicable

I'm looking for a Lisp Routine that will allow me to export drawings to pdf's. Futhermore, the file name needs to be generated from the information in the title block attributes i.e. drawing number (1st 5 digits), sheet number (next 4 digits), revision number (next 2 digits), & sheet size (last character). For instance, 123-45678-0101-01-C. Also, I would like for it to be saved in a specific location & before it is saved, I would like to enter a folder name. Can anybody get me started. It seems simple but I'm not sure if I could start it from scratch. I would prefer, if someone already had a Lisp Routine that was similar to this so I could just edit it.

0 Likes
8,878 Views
48 Replies
Replies (48)
Message 21 of 49

hmsilva
Mentor
Mentor

@Anonymous wrote:

hey Henrique,

 

Thank you for your expertise,, You are AWESOME,

 

Just extra queries,

 

- how do I specify a default pdf output folder?

 

- and rather than seperating the attributes values in ' - ' (dash) in your pdf name, how can you separate the values in ' _ " (under score)  ?

 

Thanks again,

Koya


Hello Koya and welcome to the Autodesk Community!

- how do I specify a default pdf output folder?

change

(setq path (acet-ui-pickdir "Select directory" (getvar "dwgprefix")))

to

 

(setq path "c:\\The\\Path")

 

- and rather than seperating the attributes values in ' - ' (dash) in your pdf name, how can you separate the values in ' _ " (under score)  ?

chnage

(foreach n lst1
   (if (setq a (assoc n lst))
      (setq name (strcat name "-" (cdr a)))
   )
)
(setq filename (strcat path "\\" (vl-string-left-trim "-" name) ".pdf"))

to

 

(foreach n lst1
   (if (setq a (assoc n lst))
      (setq name (strcat name "_" (cdr a)))
   )
)
(setq filename (strcat path "\\" (vl-string-left-trim "_" name) ".pdf"))

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 22 of 49

Anonymous
Not applicable

 

THANK YOU VERY MUCH, YOU ARE A CHAMPION 🙂 🙂 🙂

 

Cheers,

Koya

0 Likes
Message 23 of 49

hmsilva
Mentor
Mentor

@Anonymous wrote:

 

THANK YOU VERY MUCH, YOU ARE A CHAMPION 🙂 🙂 🙂

 

Cheers,

Koya


You're welcome, Koya
Glad I could help!

Henrique

EESignature

0 Likes
Message 24 of 49

Anonymous
Not applicable
Hi Henrique,

Your lisp is working like a dream 🙂 ..
But I got 1 more query, how can I plot ONLY the current layout tab in a multi tab drawing?
I hope this query is not too much of a hassle.

much appreciated,
Koya
0 Likes
Message 25 of 49

hmsilva
Mentor
Mentor

@Anonymous wrote:
Hi Henrique,

Your lisp is working like a dream 🙂 ..
But I got 1 more query, how can I plot ONLY the current layout tab in a multi tab drawing?
I hope this query is not too much of a hassle.

much appreciated,
Koya

Hi Koya,

could you please post the working code?

 

Henrique

EESignature

0 Likes
Message 26 of 49

Anonymous
Not applicable
Hi Henrique,

Here is the lisp,

(vl-load-com)
(defun c:SCI_Dwg2Pdf (/ hms:get_atts filename hnd lst lst1 name otab path
ss)
(defun hms:get_atts (enm / att obj)
(if (or (and (eq (type enm) 'ENAME)
(setq obj (vlax-ename->vla-object enm))
(eq (vla-get-hasattributes obj) :vlax-true)
)
(and (eq (type enm) 'VLA-OBJECT)
(setq obj enm)
(eq (vla-get-hasattributes obj) :vlax-true)
)
)
(mapcar
'(lambda (att)
(cons (vla-get-TagString att) (vla-get-TextString att))
)
(vlax-invoke obj "GetAttributes")
)
)
)
(cond ((setq path "C:\\_SCI_PDF_Output")
(setq otab (getvar 'CTAB))
(foreach layt (layoutlist)
(setvar 'CTAB layt)
(if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "a1h2016")
'(66 . 1) (cons 410 (getvar 'CTAB)))))
(progn
(setq hnd (ssname ss 0)
lst (hms:get_atts hnd)
lst1 '("PROJECT_NO" "ZONE" "DISC" "DOC" "SHT" "REV")
name ""
)
(foreach n lst1
(if (setq a (assoc n lst))
(setq name (strcat name "-" (cdr a)))
)
)
(setq filename (strcat path "\\" (vl-string-left-trim "-"
name) ".pdf"))
(command "_.-export" "_PDF" "_C" "_NO" filename)
)
)
)
(setvar 'CTAB otab)
)
)
(princ)
)

🙂 🙂 🙂
0 Likes
Message 27 of 49

Anonymous
Not applicable
Hi Henrique,

Here is the lisp,

(vl-load-com)
(defun c:SCI_Dwg2Pdf (/ hms:get_atts filename hnd lst lst1 name otab path
ss)
(defun hms:get_atts (enm / att obj)
(if (or (and (eq (type enm) 'ENAME)
(setq obj (vlax-ename->vla-object enm))
(eq (vla-get-hasattributes obj) :vlax-true)
)
(and (eq (type enm) 'VLA-OBJECT)
(setq obj enm)
(eq (vla-get-hasattributes obj) :vlax-true)
)
)
(mapcar
'(lambda (att)
(cons (vla-get-TagString att) (vla-get-TextString att))
)
(vlax-invoke obj "GetAttributes")
)
)
)
(cond ((setq path "C:\\_SCI_PDF_Output")
(setq otab (getvar 'CTAB))
(foreach layt (layoutlist)
(setvar 'CTAB layt)
(if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "a1h2016")
'(66 . 1) (cons 410 (getvar 'CTAB)))))
(progn
(setq hnd (ssname ss 0)
lst (hms:get_atts hnd)
lst1 '("PROJECT_NO" "ZONE" "DISC" "DOC" "SHT" "REV")
name ""
)
(foreach n lst1
(if (setq a (assoc n lst))
(setq name (strcat name "-" (cdr a)))
)
)
(setq filename (strcat path "\\" (vl-string-left-trim "-"
name) ".pdf"))
(command "_.-export" "_PDF" "_C" "_NO" filename)
)
)
)
(setvar 'CTAB otab)
)
)
(princ)
)

🙂 🙂 🙂
0 Likes
Message 28 of 49

hmsilva
Mentor
Mentor

Untested...

 

(vl-load-com)
(defun c:SCI_Dwg2Pdf (/ hms:get_atts filename hnd lst lst1 name otab path ss)
    (defun hms:get_atts (enm / att obj)
        (if (or (and (eq (type enm) 'ENAME)
                     (setq obj (vlax-ename->vla-object enm))
                     (eq (vla-get-hasattributes obj) :vlax-true)
                )
                (and (eq (type enm) 'VLA-OBJECT)
                     (setq obj enm)
                     (eq (vla-get-hasattributes obj) :vlax-true)
                )
            )
            (mapcar
                '(lambda (att)
                     (cons (vla-get-TagString att) (vla-get-TextString att))
                 )
                (vlax-invoke obj "GetAttributes")
            )
        )
    )
    (cond ((setq path "C:\\_SCI_PDF_Output")
           ;(setq otab (getvar 'CTAB))
           ;(foreach layt (layoutlist)
               ;(setvar 'CTAB layt)
               (if (setq ss (ssget "_X"
                                   (list '(0 . "INSERT")
                                         '(2 . "a1h2016")
                                         '(66 . 1)
                                         (cons 410 (getvar 'CTAB))
                                   )
                            )
                   )
                   (progn
                       (setq hnd  (ssname ss 0)
                             lst  (hms:get_atts hnd)
                             lst1 '("PROJECT_NO" "ZONE" "DISC" "DOC" "SHT" "REV")
                             name ""
                       )
                       (foreach n lst1
                           (if (setq a (assoc n lst))
                               (setq name (strcat name "-" (cdr a)))
                           )
                       )
                       (setq filename (strcat path
                                              "\\"
                                              (vl-string-left-trim
                                                  "-"
                                                  name
                                              )
                                              ".pdf"
                                      )
                       )
                       (command "_.-export" "_PDF" "_C" "_NO" filename)
                   )
               )
           ;)
           ;(setvar 'CTAB otab)
          )
    )
    (princ)
)

 

Hope this helps,
Henrique

EESignature

Message 29 of 49

Anonymous
Not applicable
Oh wow !!!!!, that's excellent Henrique!

Very much appreciate your talent 🙂
0 Likes
Message 30 of 49

hmsilva
Mentor
Mentor

You're welcome, Koya
Glad I could help

Henrique

EESignature

0 Likes
Message 31 of 49

Anonymous
Not applicable

hi Henrique,

 

I hope everything is well.

 

Just to add another customisation on your lisp,

- how do I load and specify a preffered page setup before plotting ? ☺

- your lisp is shown below, , ,

 

(vl-load-com)
(defun c:SCI_Dwg2Pdf (/ hms:get_atts filename hnd lst lst1 name otab path
ss)
(defun hms:get_atts (enm / att obj)
(if (or (and (eq (type enm) 'ENAME)
(setq obj (vlax-ename->vla-object enm))
(eq (vla-get-hasattributes obj) :vlax-true)
)
(and (eq (type enm) 'VLA-OBJECT)
(setq obj enm)
(eq (vla-get-hasattributes obj) :vlax-true)
)
)
(mapcar
'(lambda (att)
(cons (vla-get-TagString att) (vla-get-TextString att))
)
(vlax-invoke obj "GetAttributes")
)
)
)
(cond ((setq path "C:\\_SCI_PDF_Output")
(setq otab (getvar 'CTAB))
(foreach layt (layoutlist)
(setvar 'CTAB layt)
(if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "a1h2016")
'(66 . 1) (cons 410 (getvar 'CTAB)))))
(progn
(setq hnd (ssname ss 0)
lst (hms:get_atts hnd)
lst1 '("PROJECT_NO" "ZONE" "DISC" "DOC" "SHT" "REV")
name ""
)
(foreach n lst1
(if (setq a (assoc n lst))
(setq name (strcat name "-" (cdr a)))
)
)
(setq filename (strcat path "\\" (vl-string-left-trim "-"
name) ".pdf"))
(command "_.-export" "_PDF" "_C" "_NO" filename)
)
)
)
(setvar 'CTAB otab)
)
)
(princ)
)

 

- Thank you in advance 😊

0 Likes
Message 32 of 49

hmsilva
Mentor
Mentor

Hi dizon.cad,
try to do a search in 'Search This Board' for 'page setup' and you'll find several approaches.
One of them is using the 'CopyFrom' method from a file with the preffered page setup...

 

I'm on vacation, and I don't have AutoCAD in this laptop, but when I return home, I'll see what I can do.

 

Henrique

EESignature

0 Likes
Message 33 of 49

Anonymous
Not applicable

hi Henrique,

 

I hope you had a good holiday.

I tried your suggestion but 'I had no luck'.

So I tried this other approach from your Lisp.

(as Underlline below)

 

(vl-load-com)

(defun c:Dwg2Pdf (/ hms:get_atts filename hnd lst lst1 name otab path ss)

 

  (defun hms:get_atts (enm / att obj)

    (if (or (and (eq (type enm) 'ENAME)

                 (setq obj (vlax-ename->vla-object enm))

                 (eq (vla-get-hasattributes obj) :vlax-true)

            )

            (and (eq (type enm) 'VLA-OBJECT)

                 (setq obj enm)

                 (eq (vla-get-hasattributes obj) :vlax-true)

            )

        )

      (mapcar

        '(lambda (att)

           (cons (vla-get-TagString att) (vla-get-TextString att))

         )

        (vlax-invoke obj "GetAttributes")

      )

    )

  )

 

  (cond ((setq path "C:\\PDF_Output")

         (setq otab (getvar 'CTAB))

         (foreach layt (layoutlist)

           (setvar 'CTAB layt)

           (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "a1h2014") '(66 . 1) (cons 410 (getvar 'CTAB)))))

             (progn

               (setq hnd  (ssname ss 0)

                     lst  (hms:get_atts hnd)

                     lst1 '("PROJECT_NO" "ZONE" "DISC" "DOC" "SHT" "REV")

                     name ""

               )

               (foreach n lst1

                 (if (setq a (assoc n lst))

                   (setq name (strcat name "-" (cdr a)))

                 )

               )

               (setq filename (strcat path "\\" (vl-string-left-trim "-" name) ".pdf"))

               (command "_.-export" "_PDF" "_C" "_NO" filename)

               (command "-PLOT" "NO" "" "A1-A3" "DWG To PDF.PC3" filename "no" "yes")

 

             )

           )

         )

         (setvar 'CTAB otab)

        )

  )

  (princ)

)

;------------------------------------------------------------------------------

 

I ran my edited version in 'AutoScript' and it did work for the first 5 drawings but then some how it had lost function and Autocad was requiring command line inputs.

Please, please let me know on what I missed. 😞

 

Much appreciated,

 

Koya

 

 

0 Likes
Message 34 of 49

hmsilva
Mentor
Mentor

Hi Koya,

probably the named page setup, is not present in the dwg...

try to load this program, and ensures the named page setup before using it in the plot command...

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 35 of 49

Anonymous
Not applicable
Hi Henrique,


Thanks for your advice, the Lisp you have suggested did its job and loaded
'My Page Setup' into the drawing.
But, it did not make my 'My Page Setup' to Current.

[image: Inline image 1]

So when I batch export to pdf (using your dwg2pdf lisp) the outcome is a
mis-aligned drawing.
Is there an extra coding to set 'My Page Setup' to Current and applies this
to all tabs?

Many thanks,

Koya
0 Likes
Message 36 of 49

hmsilva
Mentor
Mentor

Hi Koya,

load this function in your code, and call it just before the plot command ... (SetNamePageSetup "A1-A3")

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 37 of 49

Anonymous
Not applicable

Hi Henrique,

 

How are things?

Apologies for resurficing my old query, I think I was too enthus with your autolisp solution.

I am now wanting to utilize more of the Single Tab - Export to PDF lisp and realize that I did not make a proper test of the lisp and it does not work, as shown below.

Please, please I need you expertise,  🙂

 

Untested,

 

(vl-load-com)
(defun c:SCI_Dwg2Pdf (/ hms:get_atts filename hnd lst lst1 name otab path ss)
    (defun hms:get_atts (enm / att obj)
        (if (or (and (eq (type enm) 'ENAME)
                     (setq obj (vlax-ename->vla-object enm))
                     (eq (vla-get-hasattributes obj) :vlax-true)
                )
                (and (eq (type enm) 'VLA-OBJECT)
                     (setq obj enm)
                     (eq (vla-get-hasattributes obj) :vlax-true)
                )
            )
            (mapcar
                '(lambda (att)
                     (cons (vla-get-TagString att) (vla-get-TextString att))
                 )
                (vlax-invoke obj "GetAttributes")
            )
        )
    )
    (cond ((setq path "C:\\_SCI_PDF_Output")
           ;(setq otab (getvar 'CTAB))
           ;(foreach layt (layoutlist)
               ;(setvar 'CTAB layt)
               (if (setq ss (ssget "_X"
                                   (list '(0 . "INSERT")
                                         '(2 . "a1h2016")
                                         '(66 . 1)
                                         (cons 410 (getvar 'CTAB))
                                   )
                            )
                   )
                   (progn
                       (setq hnd  (ssname ss 0)
                             lst  (hms:get_atts hnd)
                             lst1 '("PROJECT_NO" "ZONE" "DISC" "DOC" "SHT" "REV")
                             name ""
                       )
                       (foreach n lst1
                           (if (setq a (assoc n lst))
                               (setq name (strcat name "-" (cdr a)))
                           )
                       )
                       (setq filename (strcat path
                                              "\\"
                                              (vl-string-left-trim
                                                  "-"
                                                  name
                                              )
                                              ".pdf"
                                      )
                       )
                       (command "_.-export" "_PDF" "_C" "_NO" filename)
                   )
               )
           ;)
           ;(setvar 'CTAB otab)
          )
    )
    (princ)
)

 

0 Likes
Message 38 of 49

hmsilva
Mentor
Mentor

Untested...

 

(vl-load-com)
(defun c:SCI_Dwg2Pdf (/ hms:get_atts filename hnd lst lst1 name otab path ss)
   (defun hms:get_atts (enm / att obj)
      (if (or (and (eq (type enm) 'ENAME)
                   (setq obj (vlax-ename->vla-object enm))
                   (eq (vla-get-hasattributes obj) :vlax-true)
              )
              (and (eq (type enm) 'VLA-OBJECT)
                   (setq obj enm)
                   (eq (vla-get-hasattributes obj) :vlax-true)
              )
          )
         (mapcar
            '(lambda (att)
                (cons (vla-get-TagString att) (vla-get-TextString att))
             )
            (vlax-invoke obj "GetAttributes")
         )
      )
   )
   (if (/= (getvar 'CTAB) "Model")
      (if (and (vl-file-directory-p "C:\\_SCI_PDF_Output")
               (setq path "C:\\_SCI_PDF_Output")
          )
         (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "a1h2016") '(66 . 1) (cons 410 (getvar 'CTAB)))))
            (progn
               (setq hnd  (ssname ss 0)
                     lst  (hms:get_atts hnd)
                     lst1 '("PROJECT_NO" "ZONE" "DISC" "DOC" "SHT" "REV")
                     name ""
               )
               (foreach n lst1
                  (if (setq a (assoc n lst))
                     (setq name (strcat name "-" (cdr a)))
                  )
               )
               (setq filename (strcat path "\\" (vl-string-left-trim "-" name) ".pdf"))
               (command "_.-export" "_PDF" "_C" "_NO" filename)
            )
            (prompt "\n \"a1h2016\" was not found in current tab... ")
         )
         (prompt "\n \"C:\\_SCI_PDF_Output\" directory was not found... ")
      )
      (prompt "\n Command not allowed in \"Model\" tab... ")
   )
   (princ)
)

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 39 of 49

Anonymous
Not applicable

Thanks again Henrique,, 🙂

Much appreciated 🙂

0 Likes
Message 40 of 49

Anonymous
Not applicable

Hi Henrique,

 

I hope you had good holidays.

I just want to put a couple more twist on your lisp.

 

Let's say, from the code below the pdf name you extracted is "TRN1003-1" (DWGNAME is TRN1003 and at Revision 1).

(setq hnd (ssname ss 0)
lst (hms:get_atts hnd)
lst1 '("DWGNAME" "REV")
name ""
)
(foreach n lst1
(if (setq a (assoc n lst))
(setq name (strcat name "-" (cdr a)))
)

_________________________________________________________________

 

What will be the correct line of lisp codes to extract pdf name in these formats? 

TRN1003-(1)

&

TRN1003-Rev1

 

I hope this isn't to much of a hassle at the start of the year.  🙂

 

Cheers,

Koya

 

0 Likes