Jump to content

Recommended Posts

Posted

Yes, I knew there are a lot of pdf scripts out there.
But I want to learn lisp a little bit.

 

I tried to make my own PDF lisp script.
But I dont know why it not work.

 

What it should do:

1. Delete PDF folder

2. Create PDF folder in same directory of dwg

3. Create new PDF for each layout in the created folder (2.) with dwg name and layout name

 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;                                                               ;
;                          Layouts To PDF                      ;
;                                                               ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun C:PDF_A2 ( / cmd )

(PROMPT ".....Drucke Layouts in A2....")
(setvar "cmddia" 0)
(setvar "filedia" 0)
  
  

  (setq sub "PDF") ;; Unterordner definieren
  
  ;Ordner löschen inklusive Inhalt
  (setq FSO (vlax-create-object "Scripting.FileSystemObject"))
  (vlax-invoke FSO "DeleteFolder" (strcat (getvar 'dwgprefix) sub) :vlax-true)
  (vlax-release-object FSO)
  

  'Ordner neu anlegen
  (cond
       (   (zerop (getvar 'dwgtitled))
           (princ "\nCurrent drawing is unsaved.")
       )
       (   (not (or (findfile (setq sub (strcat (getvar 'dwgprefix) sub))) (vl-mkdir sub)))
           (princ (strcat "\nUnable to create directory " sub))
       )
       (
  

(setq dwgpre (strcat (getvar "dwgprefix") "\\" sub))
(setq dwgname (strcat (getvar "dwgname")))  
  
(foreach Lay (layoutlist)
  
 
  (if (/= Lay "Modell")
  

  'Für jedes Layout PDF erzeugen  
  ;(PROMPT ".....Dies ist ein Layout....")
   (progn
      (setvar "ctab" Lay)
       (setq pdfname (strcat dwgpre "\\" dwgname "-" Lay ".pdf" ))  
     

   
				(COMMAND "-PLOT"
					"J"						;Erweiterter Plott?
					""						;Layoutname
					"DWG To PDF.pc3"				;Drucker
					"Iso full bleed A2 (420.00 x 594.00 MM)"	;Blatttyp
					"m"						;Papiereinheit
					"Q"					;Format Hochformat/Querformat
					"N"						;Auf dem Kopf?
					"Layout"						;Plottbereich (L = Layout)
					"1=1"						;Maßstab
					"0,0"						;Versatz
					"J"						;Mit Plotstilen plotten?
					"monochrome.ctb"				;Plotstil
					"J"						;Mit Linienstärke plotten?
					"N"						;Linienstärke mit Plotmaßstab skalieren?
					"N"						;Papierbereich zuerst Plotten?
					"N"						;Papierbereichobjekte ausblenden?
					pdfName						;PDF Name
				)
     
   )
    
  )

    
  
  
)

       )
  )
        
(setvar "cmddia" 1)
(setvar "filedia" 1)
(setq DWGNAME nil
     LEN nil
     NAME nil) 
(princ)
); defun ende

 

Posted

I've not got my CAD running at the moment, but where does your code stop working?

For example, does it delete and create the folders OK?

Posted

It stops at the PDF creation. 

The first PDF is saved. Second not. 

And it would be nice, if the PDF don't be opened after it's created. 

Posted (edited)

Rather than debug yours this does what you want just compare, the only difference is it makes a PDF directory rather than delete files. As you can see used since 2014 This version uses Ghostscript and joins all the pdf's into 1 pdf as well as individuals.

 

Rather than delete I would delete the pdf files if exist can use (command "shell" "Del dwgpre\*.pdf")

 

 mergepdfs.lsp

;Plots layouts by range
; By Alan H Feb 2014

(defun AH:pltlays ( / val1 val2 plotnames dwgname lendwg pdfname lay numlay numend dwgpre)
(SETVAR "PDMODE" 0)
(setvar "plottransparencyoverride" 2)
(setvar "fillmode" 1)
(setvar "textfill" 1)
(setq plotnames '())

; check that pdf directory exists
(setq dwgpre (strcat (getvar "dwgprefix") "\pdf"))
(if (= (vl-file-directory-p dwgpre) nil)
(vl-mkdir dwgpre)
)
(SETQ LAYOUTS (vla-get-Layouts (vla-get-activedocument (vlax-get-acad-object))))
(SETQ COUNT (- (VLA-GET-COUNT LAYOUTS) 1))

(if (not AH:getvalsm)(load "Multi Getvals.lsp"))
(setq vals (AH:getvalsm  (list "Enter plot range" "Enter start tab number" 6 4 "1" "Enter end tab number" 6 4 (RTOS COUNT 2 0))))

(setq numlay (atoi (nth 0 vals)))
(setq numend (atoi (nth 1 vals)))

(setq len (+ (- numend numlay) 1))

(setq dwgname (GETVAR "dwgname"))
(setq lendwg (strlen dwgname))
(setq dwgname (substr dwgname 1 (- lendwg 4)))

(repeat len
(vlax-for lay LAYOUTS
(if (= numlay (vla-get-taborder lay))
  (setvar "ctab" (vla-get-name lay))
) ; if

(setq pdfname (strcat dwgpre "\\" dwgname "-" (getvar "ctab") ".pdf" ))
) ; for

(setvar "textfill" 1)
(setvar "fillmode" 1)
(setvar "PLOTTRANSPARENCYOVERRIDE" 2)
    (COMMAND "-PLOT"  "Y"  "" "dwg to Pdf"
	       "Iso full bleed A3 (420.00 x 297.00 MM)" "m" "LANDSCAPE"  "N"   "W"  "-6,-6" "807,560" "1=2"  "C"
	       "y" "Acad.ctb" "Y"	"n" "n" "n" pdfName "N" "y"
    )
    
(setq numlay (+ numlay 1))
(setq plotnames (cons pdfname plotnames))
) ; end repeat
(setq trgfile (strcat (getvar "dwgprefix") "pdf\\" dwgname "-D" (nth 0 vals) "-D" (nth 1 vals) ".pdf"))

(setq plotnames (reverse plotnames))

(IF (= (length plotnames) 1)
(princ)
(progn
(if (not combinepdf)(load "mergepdfs"))
(combinepdf gsExe plotnames trgFile )
)
)

) ; defun

(AH:pltlays)


(princ)

 

Multi GETVALS.lsp

 

Edited by BIGAL
  • Like 2
Posted
12 hours ago, V4Mp said:

And it would be nice, if the PDF don't be opened after it's created. 

 

If you go to page setup, select "PDF to DWG.pc3", there should then be a button "PDF Options" where you'll find 'show results in viewer' checked, uncheck it.

 

I set up s new plotter "DWG to PDF no preview" to give me the option to have it open or not.

  • Like 1
Posted

Thanks for the code. But as I said, I want to learn LISP. And if I just copy the new script, edit it, I would not get what I have done wrong in my already created script.

Posted

@V4Mp Please note is that the two system variables that I disabled and I believe that you don't need to change them unless you have them changed in any other routines so you can enable them back if you want to by removing the semicolons at the start of the line.

 

Also note that I added two lines of codes at the end of the PLOT command since there are obligatory but I don't know if they are not required with your AutoCAD version, so you can remove them if they are not necessarily.

 

(defun C:PDF_A2 (/ sub fso dwgname pdfname)
  (PROMPT ".....Drucke Layouts in A2....")
;;;  (setvar "cmddia" 0)
;;;  (setvar "filedia" 0)
  (if (findfile (setq sub (strcat (getvar 'dwgprefix) "PDF")))
      ;; Unterordner definieren
      ;; Ordner löschen inklusive Inhalt
      (and (setq FSO (vlax-create-object "Scripting.FileSystemObject"))
           (vlax-invoke FSO "DeleteFolder" sub :vlax-true)
           (vlax-release-object FSO)
           )
    )
  (vl-mkdir sub)
  ;; 'Ordner neu anlegen
  (cond
    ((zerop (getvar 'dwgtitled))
     (princ "\nCurrent drawing is unsaved.")
    )
    ((not sub)
     (princ (strcat "\nUnable to create directory " sub))
    )
    ((setq dwgname (strcat (getvar "dwgname")))
     (foreach Lay (layoutlist)
       (if (/= Lay "Model")
         ;;'Für jedes Layout PDF erzeugen
         ;;(PROMPT ".....Dies ist ein Layout....")
         (progn
           (setvar "ctab" Lay)
           (setq pdfname (strcat sub "\\" dwgname "-" Lay ".pdf"))
           (COMMAND "-PLOT"          "J" ;Erweiterter Plott?
                    ""                  ;Layoutname
                    "DWG To PDF.pc3"    ;Drucker
                    "Iso full bleed A2 (420.00 x 594.00 MM)" ;Blatttyp
                    "m"                 ;Papiereinheit
                    "Q"                 ;Format Hochformat/Querformat
                    "N"                 ;Auf dem Kopf?
                    "Layout"            ;Plottbereich (L = Layout)
                    "1=1"               ;Maßstab
                    "0,0"               ;Versatz
                    "J"                 ;Mit Plotstilen plotten?
                    "monochrome.ctb"    ;Plotstil
                    "J"                 ;Mit Linienstärke plotten?
                    "N"                 ;Linienstärke mit Plotmaßstab skalieren?
                    "N"                 ;Papierbereich zuerst Plotten?
                    "N"                 ;Papierbereichobjekte ausblenden?
                    pdfName             ;PDF Name
                    "N" "J"
                   )
         )
       )
     )
    )
  )
;;  (setvar "cmddia" 1)
;;  (setvar "filedia" 1)
  (princ)
); defun ende

  

Posted

Might be worth recommending something like

(setq old_cmddia (getvar "cmddia"))

(setvar "cmddia" 0)

...

,,,

,,,

(setvar "cmddia" old_cmddia) 

(same with filedia) just in case their system is set to something else?

Posted

@Steven P

It's not required to set any system variable with this routine and I already commented them out if you did not notice that, although it is strongly recommended to have error handle to reset the system variable(s) back once any error take a place.

  • Like 2
Posted

Some additional info...

 

The default directory for plot to file operations can be found in options > plot and publish tab.

 

As to the file opening after printed, the PC3 file for DWG to PDF (as well as the Autocad PDF pc3 files) have a checkbox in custom properties called "show results in viewer".

This can be unchecked.

 

This would allow the LISP to run without opening the PDF every time you print something.

Then click OK to save your pc3 changes.

 

If you are using adobe printer, there is a similar built-in option in the printer settings that can be unchecked.

 

ChriS

Posted
On 2/12/2021 at 12:06 AM, BIGAL said:

Rather than debug yours this does what you want just compare, the only difference is it makes a PDF directory rather than delete files. As you can see used since 2014 This version uses Ghostscript and joins all the pdf's into 1 pdf as well as individuals.

 

Rather than delete I would delete the pdf files if exist can use (command "shell" "Del dwgpre\*.pdf")

 

  mergepdfs.lsp 1.05 kB · 17 downloads


;Plots layouts by range
; By Alan H Feb 2014

(defun AH:pltlays ( / val1 val2 plotnames dwgname lendwg pdfname lay numlay numend dwgpre)
(SETVAR "PDMODE" 0)
(setvar "plottransparencyoverride" 2)
(setvar "fillmode" 1)
(setvar "textfill" 1)
(setq plotnames '())

; check that pdf directory exists
(setq dwgpre (strcat (getvar "dwgprefix") "\pdf"))
(if (= (vl-file-directory-p dwgpre) nil)
(vl-mkdir dwgpre)
)
(SETQ LAYOUTS (vla-get-Layouts (vla-get-activedocument (vlax-get-acad-object))))
(SETQ COUNT (- (VLA-GET-COUNT LAYOUTS) 1))

(if (not AH:getvalsm)(load "Multi Getvals.lsp"))
(setq vals (AH:getvalsm  (list "Enter plot range" "Enter start tab number" 6 4 "1" "Enter end tab number" 6 4 (RTOS COUNT 2 0))))

(setq numlay (atoi (nth 0 vals)))
(setq numend (atoi (nth 1 vals)))

(setq len (+ (- numend numlay) 1))

(setq dwgname (GETVAR "dwgname"))
(setq lendwg (strlen dwgname))
(setq dwgname (substr dwgname 1 (- lendwg 4)))

(repeat len
(vlax-for lay LAYOUTS
(if (= numlay (vla-get-taborder lay))
  (setvar "ctab" (vla-get-name lay))
) ; if

(setq pdfname (strcat dwgpre "\\" dwgname "-" (getvar "ctab") ".pdf" ))
) ; for

(setvar "textfill" 1)
(setvar "fillmode" 1)
(setvar "PLOTTRANSPARENCYOVERRIDE" 2)
    (COMMAND "-PLOT"  "Y"  "" "dwg to Pdf"
	       "Iso full bleed A3 (420.00 x 297.00 MM)" "m" "LANDSCAPE"  "N"   "W"  "-6,-6" "807,560" "1=2"  "C"
	       "y" "Acad.ctb" "Y"	"n" "n" "n" pdfName "N" "y"
    )
    
(setq numlay (+ numlay 1))
(setq plotnames (cons pdfname plotnames))
) ; end repeat
(setq trgfile (strcat (getvar "dwgprefix") "pdf\\" dwgname "-D" (nth 0 vals) "-D" (nth 1 vals) ".pdf"))

(setq plotnames (reverse plotnames))

(IF (= (length plotnames) 1)
(princ)
(progn
(if (not combinepdf)(load "mergepdfs"))
(combinepdf gsExe plotnames trgFile )
)
)

) ; defun

(AH:pltlays)


(princ)

 

Multi GETVALS.lsp 2.07 kB · 92 downloads

 

I have set up my layouts and then i draged the lisp and dropped it in the DWG folder but i didn't got a reponce. Is there a key to write to activate the LISP? 

I don't know what to do to use this lisp.

Thanks for you efforts BIGAL.

 

Posted
11 hours ago, Tharwat said:

@Steven P

It's not required to set any system variable with this routine and I already commented them out if you did not notice that, although it is strongly recommended to have error handle to reset the system variable(s) back once any error take a place.

 

Thanks, I saw that though my comment was more as a note for the OP rather than any criticism.  

Posted

@fadysamy077@gmail.com I see that you are using AutoCAD LT. Unfortunately, the LT version does not support AutoLISP. If you have access to full AutoCAD, you can use this information to load the script:

 

  • Like 1
Posted

Hi,
that works very well.
But I have different layouts in A3 and A4 format, landscape and portrait.
How could that be taken into account?
lg. Martin

Posted
2 hours ago, martinle said:

Hi,
that works very well.
But I have different layouts in A3 and A4 format, landscape and portrait.
How could that be taken into account?
lg. Martin

 

 

In BigAls example, there is this line

 

    (COMMAND "-PLOT"  "Y"  "" "dwg to Pdf"
	       "Iso full bleed A3 (420.00 x 297.00 MM)" "m" "LANDSCAPE"  "N"   "W"  "-6,-6" "807,560" "1=2"  "C"
	       "y" "Acad.ctb" "Y"	"n" "n" "n" pdfName "N" "y"
    )

Change the "ISO full bleed A4 (420.00 x 297.00 MM)" to whatever paper size you want (copy it in here exactly as you see it in the page setup)

Change the "LANDSCAPE" to "Portrait" as required

 

All of the variables in the (Command "-Plot" ... line can be strings as shown or variables defined somewhere else in the LISP.  So you could have 4 routines A4LandscapePDF, A4PortraitPDF, A3LandsapePDF and A3PortraitPDF, each with these changes that will do it for you, or you could have a dialogue box or something for the user to select which to use

Posted

For fadsamy077, like Cadtutor you will need to save it 1st then you can drag & drop from explorer or use appload or a menu to run.

 

Re different sizes a couple of solutions, like Steven P we had multi options in a menu each was individual. If you have  a mixture of title block sizes and rotation L or P, then if you use a block can check size, and uses a cond to set some of the values in the code. Like the window corners, I use window not extents or layout found some times just not quite right. Had a seperate check title block is at 0,0 so always worked. Use oversize for window points and centre option, if use exact can some times drop a border line.

 

Maratovich has a very extensive plot routine worth looking at as well. 

 

image.png.387724fb368f1ff433340746287e5e8a.png

 

A last comment had a version for the multi floors we occupied it checked user name, so would send to correct printer on their floor, so 1 version for 3 floors.

 

 

Posted

Thank you all, but in my drawing there are different layouts A4 horizontal, A4 vertical, A3 horizontal and A3 vertical.
I can't plot in PDF that way.
Maybe someone has an idea?

Thanks, Martin

Posted
4 hours ago, martinle said:

Thank you all, but in my drawing there are different layouts A4 horizontal, A4 vertical, A3 horizontal and A3 vertical.
I can't plot in PDF that way.
Maybe someone has an idea?

Thanks, Martin

 

I use this to give me the paper dimensions, and then a look-up to match them to the standard papersizes that I use.  I saw a  better way to get this once but this was working for me. Might then be possible to use paper size and orientation (you can work that out, width or height being the longer gives that) and put that into the plot command line

 

 

  (setq acadObj (vlax-get-acad-object))
  (setq doc (vla-get-ActiveDocument acadObj))
  (vla-GetPaperSize (vla-get-ActiveLayout doc) 'PaperWidth 'PaperHeight)


(princ PaperWidth)
(princ PaperHeight)

 

 

Posted

Hello Steven, Thanks for your help, unfortunately I am only a beginner in Lisp and can therefore not implement it.
martin

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...