Jump to content

Recommended Posts

Posted

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)
    (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)
)

 

 

  • Like 1
  • Thanks 1
Posted

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))
        )
      )
    )
  )

 

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...