End PDF insertion lisp after no more pages

End PDF insertion lisp after no more pages

Clay_Stringer
Enthusiast Enthusiast
1,797 Views
12 Replies
Message 1 of 13

End PDF insertion lisp after no more pages

Clay_Stringer
Enthusiast
Enthusiast

I've got a lisp that inserts PDFs into a specific spot.  How do I make this automatically end after no more pages are in the PDF file?  Max any file will have is 8 pages, but many will have less.  Each page has a specific spot it needs to go if it exists.  Right now this kinda works, just have to hit esc after it fails when it can't find a page number.  But I'd like something a little more elegant.    Any other suggestions on improvement also welcomed, I'm still learning.

 

(defun c:photosims (/ e1 e2 e3 e4 p1 p2 p3 p4 filepath pdfpath sims pagetotal pagenum)
  (setq e1 '(67.509680 211.320400))
  (setq p1 '(81.009680 211.320400))
  (setq e2 '(94.509680 227.320400))
  (setq p2 '(108.009680 227.320400))
  (setq e3 '(67.509680 227.320400))
  (setq p3 '(81.009680 227.320400))
  (setq e4 '(94.509680 211.320400))
  (setq p4 '(108.009680 211.320400))
  (setq filepath (getvar "dwgprefix"))
  (setq pdfpath (vl-string-subst "\\Photo Sims\\" "\\Drawings\\CDs\\" filepath))
  (setq sims (getfiled "Select Photo Sims" pdfpath "pdf" 8))
 ; (setq pagetotal (getreal "\nType Total Number of Pages: "))
 ; (setq pagenum 1)
  (command "clayer" "G-XREF PURPOSE1")
  (command "-pdfattach" sims 1 e1 1 0 )
  (command "-pdfattach" sims 2 p1 1 0 )
  (command "-pdfattach" sims 3 e2 1 0 )
  (command "-pdfattach" sims 4 p2 1 0 )
  (command "-pdfattach" sims 5 e3 1 0 )
  (command "-pdfattach" sims 6 p3 1 0 )
  (command "-pdfattach" sims 7 e4 1 0 )
  (command "-pdfattach" sims 8 p4 1 0 )
  (princ))

  

0 Likes
Accepted solutions (1)
1,798 Views
12 Replies
Replies (12)
Message 3 of 13

maratovich
Advisor
Advisor

Maybe this will help: AutoImportCAD 

---------------------------------------------------------------------
Software development
Automatic creation layouts and viewport. Batch printing drawings from model.
www.kdmsoft.net
0 Likes
Message 4 of 13

Clay_Stringer
Enthusiast
Enthusiast

I've seen this an a couple others.  As far as I can tell, as they iterate through the pages, the insertion point moves by adding to the x coordinate each step.  This doesn't work in my case, as each sheet has a specific insertion point.  A way to iterate through the page numbers but use a specific variable for each insertion point is what I need.  

0 Likes
Message 5 of 13

Clay_Stringer
Enthusiast
Enthusiast

Thanks, but from what little I can tell from the website, I would still need to enter the insertion point manually for each different sheet.  This also defeats the purpose of learning LISP.

0 Likes
Message 6 of 13

maratovich
Advisor
Advisor

@Clay_Stringer wrote:

I would still need to enter the insertion point manually for each different sheet.


No, this is not required. Everything is done automatically.

This is in case you cannot find a solution.

---------------------------------------------------------------------
Software development
Automatic creation layouts and viewport. Batch printing drawings from model.
www.kdmsoft.net
0 Likes
Message 7 of 13

CodeDing
Advisor
Advisor

@Clay_Stringer ,

 

You can use Lee's _PDFPageCount, found here:

https://www.theswamp.org/index.php?PHPSESSID=a86a2d2e880b6079f1d3aee842f41d9d&topic=39001.msg441632#...

 

As he mentions, it's relatively slow.. but it works and would allow you to loop your command only for the needed amount of pages. Here is his copied code:

(defun _PDFPageCount ( filename / fob fso mat reg res str )
 
  ;; Translation by Lee Mac of the VBScript code by Chanh Ong
  ;; found at http://docs.ongetc.com/?q=content/pdf-pages-counting-using-vb-script
  ;;
  ;; Call with fully qualified filename of PDF file:
  ;; (_PDFPageCount "C:\\Folder\\Filename.pdf")
  ;;
  ;; Returns integer describing number of pages in specified PDF file
 
  (if
    (and
      (setq filename (findfile filename))
      (eq ".PDF" (strcase (vl-filename-extension filename)))
    )
    (vl-catch-all-apply
      (function
        (lambda ( / _ReadAsTextFile _CountPage )
          (defun _ReadAsTextFile ( fso fn / fob str res )
            (setq fob (vlax-invoke fso 'getfile fn)
                  str (vlax-invoke fso 'opentextfile fn 1 0)
                  res (vlax-invoke str 'read (vlax-get fob 'size))
            )
            (vlax-invoke str 'close)
            (vlax-release-object str)
            (vlax-release-object fob)
            res
          )
          (defun _CountPage ( rgx str / mat pag )
            (vlax-put-property rgx 'pattern "/Type\\s*/Page[^s]")
            (vlax-put-property rgx 'ignorecase actrue)
            (vlax-put-property rgx 'global actrue)
            (setq mat (vlax-invoke rgx 'execute str)
                  pag (vlax-get mat 'count)
            )
            (vlax-release-object mat)
            (if (zerop pag) 1 pag)             
          )
          (setq fso (vlax-create-object "Scripting.FileSystemObject")
                reg (vlax-create-object "VBScript.RegExp")
                str (_ReadAsTextFile fso filename)
                res (_CountPage reg str)
          )
        )
      )
    )
  )
  (foreach obj (list str fob mat fso reg)
    (vl-catch-all-apply 'vlax-release-object (list obj))
  )
  res
)
(vl-load-com) (princ)

Best,

~DD

Message 8 of 13

Clay_Stringer
Enthusiast
Enthusiast

Its not necessarily the page count that is the issue.  How can I loop through the sheet numbers while using a specific variable for each specific sheets insertion point?  From all the other similar code I've seen, as it loops through the sheets the insertion point is defined by adding 8.5 to the x coord in each loop.  This doesn't work for me, as each sheet has a specific insertion point.  

Message 9 of 13

CodeDing
Advisor
Advisor
Accepted solution

@Clay_Stringer ,

 

I see you're a bit confused on how to implement Lee's code to your benefit.

Here's an example (untested, but should work) of how Lee's code would benefit your situation, regardless of the number of pages. And you get to use your preset points still.

 

You'll have to add lee's full function, I didn't want to paste it again, too lengthy.

 

Also, if you want users to type in pages manually, you can remove Lee's code (and the 1 line of code in your command) and just uncomment the line that is commented out for 'nPages'.

 

(defun _PDFPageCount ...
.....
);defun

(defun c:photosims (/ ptList filepath pdfpath sims nPages cnt)
  (vl-load-com)
  (setq ptList '(
    (67.509680 211.320400) (81.009680 211.320400)
    (94.509680 227.320400) (108.009680 227.320400)
    (67.509680 227.320400) (81.009680 227.320400)
    (94.509680 211.320400) (108.009680 211.320400)
  ));list/setq
  (setq filepath (getvar "dwgprefix"))
  (setq pdfpath (vl-string-subst "\\Photo Sims\\" "\\Drawings\\CDs\\" filepath))
  (setq sims (getfiled "Select Photo Sims" pdfpath "pdf" 8))
  (setq nPages (_PDFPageCount sims))
  ;;(initget 7) (setq nPages (getint "\nType Total Number of Pages: "))
  (setvar 'CLAYER "G-XREF PURPOSE1")
  (setq cnt 1)
  (repeat nPages
    (command "-PDFATTACH" sims cnt "_non" (nth (1- cnt) ptList) 1 0 )
    (setq cnt (1+ cnt))
  );repeat
  (prompt "\nComplete") (alert "Complete")
  (princ)
);defun

 

Hope that helps.

Best,

~DD

0 Likes
Message 10 of 13

pbejse
Mentor
Mentor

@Clay_Stringer wrote:

Its not necessarily the page count that is the issue.  How can I loop through the sheet numbers while using a specific variable for each specific sheets insertion point?  From all the other similar code I've seen, as it loops through the sheets the insertion point is defined by adding 8.5 to the x coord in each loop.  This doesn't work for me, as each sheet has a specific insertion point.  


 

I think it does help, with that function, the program can  specify how many time the command will run, it stops at the number of pages of the pdf.

 

 

(defun c:photosims (/ ipt_list filepath pdfpath sims pagetotal pagenum)    
(setq filepath (getvar "dwgprefix"))
(setq pdfpath (vl-string-subst "\\Photo Sims\\" "\\Drawings\\CDs\\" filepath))
(setq sims (getfiled "Select Photo Sims" pdfpath "pdf" 8))
(command "clayer" "G-XREF PURPOSE1")        
(setq ipt_list '((67.509680 211.320400) (81.009680 211.320400)
                 (94.509680 227.320400) (108.009680 227.320400)
                 (67.509680 227.320400) (81.009680 227.320400)
               	 (94.509680 211.320400) (108.009680 211.320400))
      )
(setq page 1
      numberofpages (_pdfpagecount sims) )

(while (And  (setq pt (car ipt_list))
             (> numberofpages 0))     
	        (command "-pdfattach" sims page "_non" pt 1 0)      
	        (setq ipt_list (Cdr ipt_list)
	              page     (1+ page)
                      numberofpages ( 1- numberofpages))
      		)
      )

 

 

 

Using while will ensure that the program will not go beyond the number of avaialble insertion point. In case the user selected a pdf with pages  more than your maximum number [ 8 ] of insertion point.

 

HTH

 

EDIT: too slow 🙂 , Same idea though.

 

Message 11 of 13

CodeDing
Advisor
Advisor

@pbejse ,

 

Your checks are more-correct. I was taking OPs word that we would never have more than 8 pages Lol.

I think if I were to cover ALL of my bases, it would ultimately look like so:

 

(defun _PDFPageCount ...
.....
);defun

(defun c:PHOTOSIMS ( / pdfPath cnt ptList ptLen nPages str)
  (vl-load-com)
  ;;get pdf
  (if (not (setq pdfPath
             (getfiled "Select Photo Sims"
               (vl-string-subst "\\Photo Sims\\" "\\Drawings\\CDs\\" (getvar 'DWGPREFIX))
               "pdf" 8)));getfiled/setq/not
    (exit)
  );if
  ;;preset variables
  (setq cnt 1
        ptList '((67.509680 211.320400) (81.009680 211.320400)
                 (94.509680 227.320400) (108.009680 227.320400)
                 (67.509680 227.320400) (81.009680 227.320400)
                 (94.509680 211.320400) (108.009680 211.320400))
        ptLen (length ptList)
        nPages (_PDFPageCount pdfPath)
        ;;nPages (progn (initget 7) (getint "\nType Total Number of Pages: "))
  );setq
  ;;main work
  (setvar 'CLAYER "G-XREF PURPOSE1")
  (setvar 'CMDECHO 0)
  (repeat (min nPages ptLen)
    (command "-PDFATTACH" pdfPath cnt "_non" (car ptList) 1 0)
    (setq cnt (1+ cnt) ptList (cdr ptList))
  );repeat
  (setvar 'CMDECHO 1)
  ;;check if anomaly
  (if (> nPages ptLen)
    (setq str (strcat "\nPhotosims Complete.. PDF longer than " (itoa ptLen) " pages, not all inserted."))
    (setq str "\nPhotosims Complete.")
  );if
  ;;finish up
  (prompt str) (alert str)
  (princ)
);defun

 

Best,

~DD

0 Likes
Message 12 of 13

Sea-Haven
Mentor
Mentor

Just my $0.05 add more insertion points you could have 20 the excellent code provided limits to the number of pages any way.

0 Likes
Message 13 of 13

Clay_Stringer
Enthusiast
Enthusiast

Thanks, this is what I was looking for, iterating though a point list rather than an individual variable for each sheet insert point.  This works good. Thanks

0 Likes