Create PDF File from some Layouts only

Create PDF File from some Layouts only

Anonymous
Not applicable
1,292 Views
7 Replies
Message 1 of 8

Create PDF File from some Layouts only

Anonymous
Not applicable

Hello,

 

I have a developer background and a little knowledge of lisp, but I don't know anything of autolisp, I am trying to assisit a collegue.

We are trying to create an autolisp routine to create a PDF file for some layouts only, those we identify each one by its name.

I need help to achieve.

 

One of the reason to create a routine is because we will call it from other script that removes several layers and creates an etransmit file. We need to attach some of the layouts as PDF to the etransmit file.

 

Here is my current code:

 

(defun c:CREATE-PDF-LAYERS(/)
	(setvar "cmddia" 0)
	(setvar "filedia" 0)
;; Define target directory (setq dir_name "C:\\Users\\jvelasco\\Desktop\\CAD test\\PDFS\\")

;; Get the Layouts from the draw (setq doc (vla-get-activedocument (vlax-get-acad-object))) (vlax-for lay (vla-get-Layouts doc) (setq plotabs (cons (vla-get-name lay) plotabs)) )

;; Get the draw name without the extension (setq dwgname (GETVAR "dwgname")) (setq len (strlen dwgname)) (setq dwgname (substr dwgname 1 (- len 4)))

;; Create a list with all the layouts (setq plottablist (acad_strlsort plotabs))

;; Loops over layouts (setq len (length plottablist)) (setq x 0)
(repeat len (setq name (nth x plottablist)) ;; Filter layers to skip Model Layer and all named as STAMIC* (if (and (not (wcmatch name "Model")) (not (wcmatch name "STAMIC*")) )
;; Define the target PDF filename with path (setq pdffile (strcat dir_name dwgname "-" name ".pdf")) ;; (prompt (strcat "Generating PDF File for layout:" name "\n")) ;; (prompt (strcat pdffile "\n"))
;; Create PDF (command "-plot" "y" name "Dwg To PDF.pc3" "ISO full bleed A3 (297.00 x 420.00 MM)" "M" "L" "N" "E" "F" "C" "Y" "C:\\Users\\jvelasco\\Desktop\\CAD test\\\\PlotCfgs\\STBFile.stb" "Y" "Y" "Y" pdffile "Y" "Y") ) (setq x (+ x 1)) )
;; Restore configuration (setvar "cmddia" 1) (setvar "filedia" 1) (setq DWGNAME nil LEN nil NAME nil PLOTTABLIST nil pdffile nil) (princ) )

The commented lines

 

	      ;; (prompt (strcat "Generating PDF File for layout:" name "\n"))
	      ;; (prompt (strcat pdffile "\n"))

show only the layout name the PDF filename only for the desired layouts, but at the moment to create the PDF file with the -plot command I can't get the PDF files created. 

 

The command box shows the following output:

 

Command: CREATE-PDF-LAYERS
-plot 
Detailed plot configuration? [Yes/No] <No>: y
Enter a layout name or [?] <MP-PR>: 
Model Regenerating model - caching viewports.
Enter an output device name or [?] <Adobe PDF>: Dwg To PDF.pc3 
Enter paper size or [?] <ANSI A (11.00 x 8.50 Inches)>: ISO full bleed A3 (297.00 x 420.00 MM) 
Enter paper units [Inches/Millimeters] <Millimeters>: M 
Enter drawing orientation [Portrait/Landscape] <Landscape>: L 
Plot upside down? [Yes/No] <No>: N 
Enter plot area [Display/Extents/Limits/View/Window] <Window>: E 
Enter plot scale (Plotted Millimeters=Drawing Units) or [Fit] <Fit>: F 
Enter plot offset (x,y) or [Center] <Center>: C 
Plot with plot styles? [Yes/No] <Yes>: Y 
Enter plot style table name or [?] (enter . for none) <London.stb>: C:\Users\jvelasco\Desktop\CAD test\R38 DES MP CAMINT 01-13 - Standard\PlotCfgs\OBS_STB.stb 
Plot with lineweights? [Yes/No] <Yes>: Y 
Enter shade plot setting [As displayed/legacy Wireframe/legacy Hidden/Visual styles/Rendered] <As displayed>: Y
Enter file name <C:\Users\jvelasco\Desktop\CAD test\R38 DES MP CAMINT 01-13 - Standard\R38 DES MP CAMINT 01-13-Model.pdf>: Y 
Save changes to page setup [Yes/No]? <N> 
Restoring cached viewports - Regenerating layout.
Command: Y Unknown command "Y".  Press F1 for help.
Command: Y Unknown command "Y".  Press F1 for help.

Command: -plot 
Detailed plot configuration? [Yes/No] <No>: y
Enter a layout name or [?] <MP-PR>: STAMIC MP-CM 
Regenerating layout.
Regenerating model - caching viewports.
Enter an output device name or [?] <DWG To PDF.pc3>: Dwg To PDF.pc3 
Enter paper size or [?] <ISO full bleed A3 (297.00 x 420.00 MM)>: ISO full bleed A3 (297.00 x 420.00 MM) 
Enter paper units [Inches/Millimeters] <Millimeters>: M 
Enter drawing orientation [Portrait/Landscape] <Landscape>: L 
Plot upside down? [Yes/No] <No>: N 
Enter plot area [Display/Extents/Layout/View/Window] <Extents>: E 
Enter plot scale (Plotted Millimeters=Drawing Units) or [Fit] <1=1>: F 
Enter plot offset (x,y) or [Center] <Center>: C 
Plot with plot styles? [Yes/No] <Yes>: Y 
Enter plot style table name or [?] (enter . for none) <OBS_STB.stb>: C:\Users\jvelasco\Desktop\CAD test\R38 DES MP CAMINT 01-13 - Standard\PlotCfgs\OBS_STB.stb Plot with lineweights? [Yes/No] <Yes>: Y 
Scale lineweights with plot scale? [Yes/No] <No>: Y 
Plot paper space first? [Yes/No] <No>: Y 
Hide paperspace objects? [Yes/No] <No>: C:\Users\jvelasco\Desktop\CAD test\PDFS\R38 DES MP CAMINT 01-13-MP-PR.pdf 
Restoring cached viewports - Regenerating layout.
Command: Y Unknown command "Y".  Press F1 for help.
Command: Y Unknown command "Y".  Press F1 for help.

Command: -plot Detailed plot configuration? [Yes/No] <No>: y
Enter a layout name or [?] <MP-PR>: STAMIC MP-JU 
Regenerating layout.
Regenerating model - caching viewports.
Enter an output device name or [?] <DWG To PDF.pc3>: Dwg To PDF.pc3 
Enter paper size or [?] <ISO full bleed A3 (297.00 x 420.00 MM)>: ISO full bleed A3 (297.00 x 420.00 MM) 
Enter paper units [Inches/Millimeters] <Millimeters>: M 
Enter drawing orientation [Portrait/Landscape] <Landscape>: L 
Plot upside down? [Yes/No] <No>: N 
Enter plot area [Display/Extents/Layout/View/Window] <Extents>: E 
Enter plot scale (Plotted Millimeters=Drawing Units) or [Fit] <1=1>: F 
Enter plot offset (x,y) or [Center] <Center>: C 
Plot with plot styles? [Yes/No] <Yes>: Y 
Enter plot style table name or [?] (enter . for none) <OBS_STB.stb>: C:\Users\jvelasco\Desktop\CAD test\R38 DES MP CAMINT 01-13 - Standard\PlotCfgs\OBS_STB.stb 
Plot with lineweights? [Yes/No] <Yes>: Y 
Scale lineweights with plot scale? [Yes/No] <No>: Y 
Plot paper space first? [Yes/No] <No>: Y 
Hide paperspace objects? [Yes/No] <No>: 
C:\Users\jvelasco\Desktop\CAD test\PDFS\R38 DES MP CAMINT 01-13-MP-PR.pdf Restoring cached viewports - Regenerating layout.
Command: Y Unknown command "Y".  Press F1 for help.
Command: Y Unknown command "Y".  Press F1 for help.
Command:
Command: 'VLIDE
Command:
Command:

 

I don't understand why is trying to generate those "STAMIC ..." layout as PDF since the condition might skip to create the pdf in this case, and indeed it seems the desired layouts are not taken since I can't see at the execution result the name after "Enter a layout name:" line. But they are when they might not be (STAMIC ...). 

This issue is strungling my brain. Anybody could see the reason?

 

I will continue debuging the -plot command options, but I really appreciate if some advanced person could point me to the right way.

 

Thank you so much to take your time of reading this question, I'll appreciate any kind of help.

 

Regards.

0 Likes
Accepted solutions (1)
1,293 Views
7 Replies
Replies (7)
Message 2 of 8

hmsilva
Mentor
Mentor

Hi JMVelascoMartinez,

 

for the layout list, try the function

(layoutlist)

it will return all layouts, but not model space.

 

Your test will error, because you need to use a progn to perform more than one action in a 'if' test.

(repeat len
   (setq name (nth x plottablist))
   ;; Filter layers to skip Model Layer and all named as STAMIC*
   (if (and
          (not (wcmatch name "Model"))
          (not (wcmatch name "STAMIC*"))
          )
      (progn
         ;; Define the target PDF filename with path
         (setq pdffile (strcat dir_name dwgname "-" name ".pdf"))
         ;; (prompt (strcat "Generating PDF File for layout:" name "\n"))
         ;; (prompt (strcat pdffile "\n"))
         ;; Create PDF
         (command "-plot" "y" name "Dwg To PDF.pc3" "ISO full bleed A3 (297.00 x 420.00 MM)" "M" "L" "N" "E" "F" "C" "Y" "C:\\Users\\jvelasco\\Desktop\\CAD test\\PlotCfgs\\STBFile.stb" "Y" "Y" "Y" pdffile "Y" "Y")
         );progn
      );if
   (setq x (+ x 1))
   )

I did remove two '\\' from the .stb path...

 

Untested...

 

(vl-load-com)
(defun c:CREATE-PDF-LAYERS(/)
	(setvar "cmddia" 0)
	(setvar "filedia" 0)
        ;; Define target directory
  	(setq dir_name "C:\\Users\\jvelasco\\Desktop\\CAD test\\PDFS\\")

        ;|;; Get the Layouts from the draw
	(setq doc (vla-get-activedocument (vlax-get-acad-object)))
	(vlax-for lay (vla-get-Layouts doc)
	  (setq plotabs (cons (vla-get-name lay) plotabs))
	)|;

        ;; Get the draw name without the extension
	(setq dwgname (GETVAR "dwgname"))
	;(setq len (strlen dwgname))
	(setq dwgname (substr dwgname 1 (- len 4)))

        ;; Create a list with all the layouts
	(setq plottablist (acad_strlsort (layoutlist)))

        ;; Loops over layouts
	;|(setq len (length plottablist))
	(setq x 0)
	(repeat len
	  (setq name (nth x plottablist))|;
        (foreach name plottablist
	  ;; Filter layers to skip Model Layer and all named as STAMIC*
	  (if ;(and
		;(not (wcmatch name "Model"))
		(not (wcmatch name "STAMIC*"))
	      ;)
             (progn
              ;; Define the target PDF filename with path
	      (setq pdffile (strcat dir_name dwgname "-" name ".pdf"))
	      ;; (prompt (strcat "Generating PDF File for layout:" name "\n"))
	      ;; (prompt (strcat pdffile "\n"))

	      ;; Create PDF
	      (command "-plot" "y" name "Dwg To PDF.pc3" "ISO full bleed A3 (297.00 x 420.00 MM)" "M" "L" "N" "E" "F" "C" "Y" "C:\\Users\\jvelasco\\Desktop\\CAD test\\PlotCfgs\\STBFile.stb" "Y" "Y" "Y" pdffile "Y" "Y")
	     )
	  ;(setq x (+ x 1))
	  )
        )
        ;; Restore configuration
	(setvar "cmddia" 1)
	(setvar "filedia" 1)
	(setq DWGNAME nil
              LEN nil
              NAME nil
              PLOTTABLIST nil
	      pdffile nil) 
	(princ)
)

 

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 3 of 8

Anonymous
Not applicable

Thank you for your detailed explanations.

 

I have cleaned up my old routine with your suggestions

 

 

 

(vl-load-com)
(defun c:CREATE-PDF-LAYERS(/)
	(setvar "cmddia" 0)
	(setvar "filedia" 0)
    ;; Define target directory
  	(setq dir_name "C:\\Users\\jvelasco\\Desktop\\CAD test\\PDFS\\")


    ;; Get the draw name without the extension
	(setq dwgname (GETVAR "dwgname"))
	;(setq len (strlen dwgname))
	(setq dwgname (substr dwgname 1 (- len 4)))

        ;; Create a list with all the layouts
	(setq plottablist (acad_strlsort (layoutlist)))

        (foreach name plottablist
	  ;; Filter layers to skip Model Layer and all named as STAMIC*
	  (if (not (wcmatch name "STAMIC*"))
             (progn
                ;; Define the target PDF filename with path
	      	(setq pdffile (strcat dir_name dwgname "-" name ".pdf"))
	        ;; Create PDF
	        (command "-plot" "y" name "Dwg To PDF.pc3" "ISO full bleed A3 (297.00 x 420.00 MM)" "M" "L" "N" "E" "F" "C" "Y" "C:/Users/jvelasco/Desktop/CAD test/PlotCfgs/STBFile.stb" "Y" "Y" "Y" pdffile "Y" "Y")
	     )
	  )
        )
        ;; Restore configuration
	(setvar "cmddia" 1)
	(setvar "filedia" 1)
	(setq DWGNAME nil
              LEN nil
              NAME nil
              PLOTTABLIST nil
	      pdffile nil) 
	(princ)
)



but I can't check it since I am having an error:

 

Command: CREATE-PDF-LAYERS

; error: bad argument type: numberp: nil

 

 

 

that I can't understand since we are not using any parameter. I also tried to remove the / inside the function declaration but I still get the same error.

 

Could you help?

Thanks

 

 

0 Likes
Message 4 of 8

hmsilva
Mentor
Mentor
Accepted solution

Sorry, my bad....
I did commented

;(setq len (strlen dwgname))

and the code line

(setq dwgname (substr dwgname 1 (- len 4)))

gives the error

; error: bad argument type: numberp: nil

because len is nil

 

Try the following code (untested)

(vl-load-com)
(defun c:CREATE-PDF-LAYERS(/ dir_name dwgname name pdffile plottablist)
    (setvar "cmddia" 0)
    (setvar "filedia" 0)
    ;; Define target directory
    (setq dir_name "C:\\Users\\jvelasco\\Desktop\\CAD test\\PDFS\\")
    ;; Get the draw name without the extension
    (setq dwgname (vl-filename-base (GETVAR "dwgname")))
    ;; Create a list with all the layouts
    (setq plottablist (acad_strlsort (layoutlist)))
    (foreach name plottablist
        ;; Filter layouts to skip all named as STAMIC*
        (if (not (wcmatch name "STAMIC*"))
            (progn
                ;; Define the target PDF filename with path
                (setq pdffile (strcat dir_name dwgname "-" name ".pdf"))
                ;; Create PDF
                (command "-plot" "y" name "Dwg To PDF.pc3" "ISO full bleed A3 (297.00 x 420.00 MM)" "M" "L" "N" "E" "F" "C" "Y" "C:/Users/jvelasco/Desktop/CAD test/PlotCfgs/STBFile.stb" "Y" "Y" "Y" pdffile "Y" "Y")
                )
            )
        )
    ;; Restore configuration
    (setvar "cmddia" 1)
    (setvar "filedia" 1)
    (princ)
    )

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 5 of 8

Anonymous
Not applicable

Hi, no worries. I could have realized too. Thank you so much for your time.

Is there a way to know the line where the error is?

 

Anyways I still have problems trying to create the pdf from the -plot call, I continue looking for the right parameters.

Do you know any trick to get them?

 

I am running -plot command from the command bar and writting the responses, but I may be missing something.

 

Command: CREATE-PDF-LAYERS
-plot 
Detailed plot configuration? [Yes/No] <No>: y
Enter a layout name or [?] <MP-PR>: MP-CM 
Restoring cached viewports - Regenerating layout.
Enter an output device name or [?] <DWG To PDF.pc3>: Dwg To PDF.pc3 
Enter paper size or [?] <ISO full bleed A3 (297.00 x 420.00 MM)>: ISO full bleed A3 (297.00 x 420.00 MM) 
Enter paper units [Inches/Millimeters] <Millimeters>: M 
Enter drawing orientation [Portrait/Landscape] <Landscape>: L 
Plot upside down? [Yes/No] <No>: N 
Enter plot area [Display/Extents/Layout/View/Window] <Extents>: E 
Enter plot scale (Plotted Millimeters=Drawing Units) or [Fit] <1=1>: F 
Enter plot offset (x,y) or [Center] <Center>: C 
Plot with plot styles? [Yes/No] <Yes>: Y 
Enter plot style table name or [?] (enter . for none) <OBS_STB.stb>: C:/Users/jvelasco/Desktop/CAD test/PlotCfgs/STBFile.stb 
Restoring cached viewports - Regenerating layout.
Command: Y Unknown command "Y".  Press F1 for help.
Command: Y Unknown command "Y".  Press F1 for help.
Command: Y Unknown command "Y".  Press F1 for help.
Command: C:\Users\jvelasco\Desktop\CAD test\PDFS\R38 DES MP CAMINT 01-13-MP-CM.pdf Unknown command "PDF".  Press F1 for help.
Command: Y Unknown command "Y".  Press F1 for help.
Command: Y Unknown command "Y".  Press F1 for help.

... similar for the next layer ...

Regards.

0 Likes
Message 6 of 8

hmsilva
Mentor
Mentor

@Anonymous wrote:

Hi, no worries. I could have realized too. Thank you so much for your time.

Is there a way to know the line where the error is?

 


This Lee Mac's tutorial Debugging Code with the Visual LISP IDE may be useful

 

Enter the -plot command at commandline, and follow the prompt order in the code.

 

Hope this helps,
Henrique

 

EESignature

0 Likes
Message 7 of 8

Anonymous
Not applicable

I am doing on that way, I think it was stoped after the STB file due to a miss reference.

 

I still don't get the pdf but I guess I am close, the most important step was to loop over the layouts.

Thank you for your support.

 

Best regards.

0 Likes
Message 8 of 8

hmsilva
Mentor
Mentor

Try

(vl-load-com)
(defun c:CREATE-PDF-LAYERS (/ dir_name dwgname name pdffile plottablist stb)
    (if (setq stb (findfile "C:/Users/jvelasco/Desktop/CAD test/PlotCfgs/STBFile.stb "))
        (progn
            (setvar "cmddia" 0)
            (setvar "filedia" 0)
            ;; Define target directory
            (setq dir_name "C:\\Users\\jvelasco\\Desktop\\CAD test\\PDFS\\")
            ;; Get the draw name without the extension
            (setq dwgname (vl-filename-base (GETVAR "dwgname")))
            ;; Create a list with all the layouts
            (setq plottablist (acad_strlsort (layoutlist)))
            (foreach name plottablist
                ;; Filter layouts to skip all named as STAMIC*
                (if (not (wcmatch name "STAMIC*"))
                    (progn
                        ;; Define the target PDF filename with path
                        (setq pdffile (strcat dir_name dwgname "-" name ".pdf"))
                        ;; Create PDF
                        (command "-plot"                  "y"                      name                     "Dwg To PDF.pc3"
                                 "ISO full bleed A3 (297.00 x 420.00 MM)"          "M"                      "L"                      "N"
                                 "E"                      "F"                      "C"                      "Y"
                                 stb                  "Y"                      "Y"
                                 "Y"                      pdffile                  "Y"                      "Y"
                                )
                    )
                )
            )
            ;; Restore configuration
            (setvar "cmddia" 1)
            (setvar "filedia" 1)
        )
        (princ "\n>>>>> STB not found <<<<< ")
    )
    (princ)
)

 

Hope this helps,
Henrique

EESignature

0 Likes