SLW210 Posted Friday at 03:35 PM Posted Friday at 03:35 PM (edited) I don't believe I posted my Import multiple PDF pages as AutoCAD objects LISP. It does most of what I need, so I doubt if I'll spend any more time on it. ;;; Imports indicated page(s), converts to DWG entities, then arranges them spaced along +X. | ;;;-----------------------------------------------------------------------------------------------| ;;; ImPDF.lsp | ;;; | ;;; By SLW210 (a.k.a. Steve Wilson) | ;;; | ;;; Requires: AutoCAD 2017 + (for PDFIMPORT) | ;;;-----------------------------------------------------------------------------------------------| (defun c:ImPDF (/ pdfPath pgStart pgEnd pg insPt gap doc ms layerColor bgColor layerName blk ) (vl-load-com) ;; Detect background color (setq bgColor (getvar "BACKGROUNDCOLOR")) ; 0=black, 7=white (setq layerColor (if (= bgColor 0) 7 0 ) ) ; white on black, black on white ;; Select PDF file (setq pdfPath (getfiled "Select PDF file to import" "" "pdf" 8)) (if (not pdfPath) (exit) ) ;; Page range (setq pgStart (getint "\nStart page <1>: ")) (if (not pgStart) (setq pgStart 1) ) (setq pgEnd (getint "\nEnd page <same>: ")) (if (not pgEnd) (setq pgEnd pgStart) ) ;; Starting insertion point (setq insPt (getpoint "\nInsertion point: ")) (if (not insPt) (setq insPt '(0 0 0)) ) ;; Gap between pages (setq gap (getreal "\nGap between pages <5.0>: ")) (if (not gap) (setq gap 5.0) ) ;; Get AutoCAD document (setq doc (vla-get-ActiveDocument (vlax-get-acad-object))) ;; Set all entities inside block definition to layer + color (defun set-block-contents-color (blkRef layer color / blkDef) (if (and blkRef (vlax-method-applicable-p blkRef 'Name)) (progn (setq blkDef (vla-item (vla-get-Blocks doc) (vla-get-Name blkRef))) (vlax-for ent blkDef (vl-catch-all-apply 'vla-put-layer (list ent layer)) (vl-catch-all-apply 'vla-put-color (list ent color)) ) ) ) ) ;; Loop through pages (setq pg pgStart) (while (<= pg pgEnd) (princ (strcat "\nImporting page " (itoa pg) "...")) ;; Attach PDF underlay (command "_-PDFATTACH" pdfPath (itoa pg) insPt 1.0 0.0) (setq u (entlast)) ;; underlay reference ;; Import to geometry (All, then detach) (command "_PDFIMPORT" u "All" "D") ;; The imported block reference is the last entity (setq blk (entlast)) (if blk (progn ;; Create layer for this page (setq layerName (strcat "PDF_Page_" (itoa pg))) (if (not (tblsearch "layer" layerName)) (vl-catch-all-apply 'vla-Add (list (vla-get-Layers doc) layerName) ) ) ;; Move block reference to layer (vl-catch-all-apply 'vla-put-layer (list (vlax-ename->vla-object blk) layerName) ) ;; Set nested geometry inside block (set-block-contents-color (vlax-ename->vla-object blk) layerName layerColor ) ;; Compute block width for next insertion point (setq minX 1e20 maxX -1e20 ) (vl-catch-all-apply 'vla-getboundingbox (list (vlax-ename->vla-object blk) 'pmin 'pmax) ) (setq pmin (vlax-safearray->list pmin)) (setq pmax (vlax-safearray->list pmax)) (setq width (- (car pmax) (car pmin))) (if (<= width 0.0) (setq width 100.0) ) ;; Update insertion point for next page (setq insPt (list (+ (car insPt) width gap) (cadr insPt) 0)) ) ) ;; Next page (setq pg (1+ pg)) ) (command "_ZOOM" "_E") (princ "\nAll pages imported successfully." ) (princ) ) Edited 8 hours ago by SLW210 Update Code per RLX 2 1 Quote
rlx Posted Friday at 04:58 PM Posted Friday at 04:58 PM Great! Thanx! It did gave me an error after the first page so I changed one of your subs a little. added this line : (and blkRef (vlax-method-applicable-p blkRef 'Name)) (defun set-block-contents-color (blkRef layer color / blkDef) (if (and blkRef (vlax-method-applicable-p blkRef 'Name)) (progn (setq blkDef (vla-item (vla-get-Blocks doc) (vla-get-Name blkRef))) (vlax-for ent blkDef (vl-catch-all-apply 'vla-put-layer (list ent layer)) (vl-catch-all-apply 'vla-put-color (list ent color)) ) ) ) ) 1 1 Quote
BIGAL Posted yesterday at 12:01 AM Posted yesterday at 12:01 AM (edited) @SLW210 have a look at this by Lee-mac may be useful. I did have problems may be my Bricscad or pdf I am using. https://www.theswamp.org/index.php?PHPSESSID=a86a2d2e880b6079f1d3aee842f41d9d&topic=39001.msg441632#msg441632 This may also be useful once you know count. (if (not AH:getvalsm)(load "Multi Getvals.lsp")) (setq vals (AH:getvalsm (list "Enter Pdf range" "Enter start Pdf number" 6 4 "1" "Enter end Pdf number" 6 4 (RTOS COUNT 2 0)))) ("1" "22") Edited yesterday at 12:04 AM by BIGAL 1 Quote
SLW210 Posted yesterday at 11:35 AM Author Posted yesterday at 11:35 AM I only used it twice and it worked fine on those, latest one was 8 sheets, the first time was a while back not sure how many sheets. IIRC, it may only work on AutoCAD 2024 or newer in this version. Tested on AutoCAD 2026. I did this several months ago and just needed it again yesterday. So @rlx's addition may be needed for older. @BIGAL I thought about do a count, but I don't need it. I'll try to find time to research BricsCAD use. I was working on creating a Layout tab for each sheet, I might get back to it later, busy with paid work right now. Only other update I plan is maybe get more than one PDF, but that will much further down the line. Quote
rlx Posted yesterday at 12:26 PM Posted yesterday at 12:26 PM maybe its just a matter of name vs effective block name issue , haven't tested that. But I like the routine and maybe gonna extend it a little with above suggestions if I run into a project that could benefit from this. Did find a program , I think it's called able2extact or something , that also has OCR built in but it will set you back around two hundred dollars , lifte time subscription. 1 Quote
SLW210 Posted 8 hours ago Author Posted 8 hours ago I added the RLX change, I'll check it out when I get back to work. Quote
Recommended Posts
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.