;-------------------------------------------------------------------------------------------------------;
;  Plot PDF	- PDF Plot Utility 	© tarhe-no.ir hamed hamzeh					;
;-------------------------------------------------------------------------------------------------------;
; This routine lets the user window select 'views' of page layouts in Model Space and then plots each	;
; view to a PDF	file											;
;-------------------------------------------------------------------------------------------------------;
;	- Limitations		: Currently works for ModelSpace only					;
;	- Printer driver used	: DWG to PDF.pc3							;
;	- Paper Size		: ISO expand A3 (420.00 x 297.00 MM)					;
;	- Plot Orientation	: Landscape								;
;	- Plot Output Location	: /ThisDrawingFileDirPath/ThisDrawingFileName-Page-#.pdf		;
;-------------------------------------------------------------------------------------------------------;
; Note-													;
; It is advisable to modify the custom properties of "DWG to PDF.pc3" plot driver to the following:	;
;	- Uncheck "Open in PDF viewer when done" (does not open each plotted file in PDF viewer)	;
;	- Uncheck "Include Layer Information"    (depends on user requirement)				;
;-------------------------------------------------------------------------------------------------------;

(defun c:pdf (/) (c:plot_pdf))
(defun c:plot_pdf (/ usercmd userosm space dir_path dwg_name pages pdf_name cntr pg# plot *error* )

(defun *error* (msg)
    (if (not
          (member msg '("console break" "Function cancelled" "quit / exit abort" "" nil))
        )
      (princ (strcat "\nError: " msg))
    )     
    (and usercmd (setvar "cmdecho" usercmd)
	 userosm (setvar "osmode" userosm)
        )
    (princ)
  ) ; end error function 
; routine starts here
  (setq usercmd	(getvar "cmdecho")
	userosm (getvar  "osmode")
	)
  (setq space (getvar "tilemode"))
  (if (= space 0)
      (prompt 	"\nCurrently this utility does not support plots from PaperSpace!
		\nYou may use the AutoCAD Publish command instead "
	)
    ); end if modelspace = false
  
  (if (= space 1)
     (progn
	  (setvar "cmdecho"  0)
	  (setvar "osmode"   1)
	  (setq dir_path (getvar "dwgprefix")
		dwg_name (getvar "dwgname"))
	  (princ "\nThis routine will plot each page layout to PDF file(s)
		  \nThe pdf file(s) will be saved to: /ThisDrawingFileDirPath/ThisDrawingFileName-Page-#.pdf 
		  \nAny file with the same name will be overwritten!"
		)
	  (initget 7) 
	  
	    (setq pages "Yes")  
	  (setq cntr 1)
	    (while (= pages "Yes")
	      (setq pg# (strcat "Page-"(itoa cntr)""))
		(prompt 
		   (strcat "\nSelect each page to set plot view (Page-"(itoa cntr)") : ")
		)
		(command "_-view" "_window" pg# pause pause)
	      (initget "Yes No")
	      (setq pages (getkword "Another? [Yes/No] <Yes>: "))
	      (or pages (setq pages "Yes"))
	      (if (= pages "Yes")
	    (progn
	      (setq cntr (1+ cntr))
	      ))
	  ) ;end view creation loop
	  (setq pages cntr)  
	  (initget "Yes No")
	    (setq plot (getkword 
	      (strcat "\n"(itoa (1- cntr))" page views have been created...proceed with plot? [Yes/No] <Yes>: ")))
	      (or plot (setq plot "Yes"))
	  
	  (if (= plot "Yes")
	    (progn
	      (setq cntr 1)
		(while (<= cntr pages)
		  (setq pg# 	 (strcat "Page-"(itoa cntr)"")
			pdf_name (strcat dir_path (substr dwg_name 1 (- (strlen dwg_name) 4)) " - " pg# ".pdf")
			)
		    (command 	"._-plot" 
				"_yes" 
				"" 
				"DWG to PDF.pc3" 
				"ISO expand A3 (420.00 x 297.00 MM)" 
				"_millimeters" 
				"_landscape" 
				"_no" 
				"_view" 
				pg# 
				"_fit" 
				"_center" 
				"_yes" 
				"monochrome.ctb" 
				"_yes" 
				"as" 
				pdf_name
				"_No" 
				"_Yes" 			
			)
		  (setq cntr (1+ cntr))
		) ;end plot loop
	     (prompt 
	       (strcat "\n"(itoa (1- cntr))" Pages successfully plotted! "))
	     (prompt "\nCreated by CJC aka 'RHINO'")
	    )
	  )
	)
     ); end if modelspace = true
  (*error* "")
  (princ)
)	
  (prompt "\n plot_pdf is now available - type <pdf> to run! ")