Insert Multiple DXF in one master Drawing

Insert Multiple DXF in one master Drawing

arshadmirza786
Collaborator Collaborator
411 Views
7 Replies
Message 1 of 8

Insert Multiple DXF in one master Drawing

arshadmirza786
Collaborator
Collaborator

Pls help to find a LISP to insert multiple DXF in one Master Dwg

 

Thanks

0 Likes
412 Views
7 Replies
Replies (7)
Message 2 of 8

roel.vander.werff
Explorer
Explorer

Hello arshadmirza786.

Here I have one.

It's works with both DWG and DXF and is menu based.

For DXF files you do have to make sure the UNITS are the same as the master drawing.

 

In the first menu just drop the directory with the files copied from your file-browser.

In the second menu you can choose the files you want to insert.

You can use the Ctrl en Shift keys to add multiple files at ones.

 

(defun c:insert-drawings ( / dir lst f count)
  (setq count 0)
  (setq dir (RvdW:Inputbox_1 "Insert directory"))
  (setq lst (LM:getfiles "Select Drawings" dir "dwg,dxf"))
  (RvdW:aup-start (length lst))
  (foreach f lst ; (setq f (nth 1 lst))
    (RvdW:insert-file (strcat (vl-filename-directory f) "\\") (strcat (vl-filename-base f) (vl-filename-extension f)))
    (RvdW:aup)
    (setq count (+ count 1))
    ) ; foreach
  (RvdW:aup-stop)
  (vl-cmdf "zoom" "E")
  (alert (strcat "We are done.\n" (rtos count) " drawings have been insert."))
  )

;___________________________________________________________________________________________________________

(defun RvdW:Inputbox_1 ( txt / dcl_01 name )
  (setq dcl_01 (load_dialog (strcat (getvar 'TEMPPREFIX) "dcl-menu.dcl")))
  (new_dialog "Inputbox_1" dcl_01)
  (set_tile "txt1" txt)
  (mode_tile "name" 2)
  (action_tile "name" "(setq name $value)")
  (action_tile "accept" "(done_dialog)")
  (start_dialog)
  (unload_dialog dcl_01)
  name
  )

(defun PasswordDialogWrite (/ z)
 (setq z (open (strcat (getvar 'TEMPPREFIX) "dcl-menu.dcl") "w"))
 (write-line "Inputbox_1 :dialog{label=\"Input box 1\";:text{key=\"txt1\";fixed_width=true;width=50;}" z)
 (write-line ":edit_box{edit_limit=256;label=\"\";key=\"name\";}" z)
 (write-line ":button{key=\"accept\";label=\"OK\";is_default=true;fixed_width=true;alignment=centered;}" z)
 (write-line "}" z)
 (close z)
)

(defun RvdW:aup-start (c)
  (setq acet-ui-progress-counter nil)
  (acet-ui-progress "Working:" c)
  )

(defun RvdW:aup ()
  (if (= acet-ui-progress-counter nil)
    (acet-ui-progress (setq acet-ui-progress-counter 1))
    (acet-ui-progress (setq acet-ui-progress-counter (+ acet-ui-progress-counter 1)))
    )
  )

(defun RvdW:aup-stop ()
  (acet-ui-progress)
  (setq acet-ui-progress-counter nil)
  )

;;; controle of een dwg tekening niet open staat
;;; d = dir
;;; f = file
;;; if you don't have a sepatate dir and filename then use;
;;; d = nil
;;; f = value
(defun RvdW:insert-file (d f)
  (if (= d nil)
    (setq fl f)
    (setq fl (strcat d f))
    )
  (if (/= (findfile (strcat (substr fl 1 (- (strlen fl) 4)) ".dwl")) nil)
    (progn
      (if (=
	    (ACET-UI-MESSAGE (strcat "drawing " (vl-filename-base fl) " is open\blocked.\nDo you want to continue?") "YES/NO Dialog"
	      (+ Acet:YESNO) ; ja=6 nee=7
	      )
	    7)
	(quit)
	) ; if
      ) ; progn
    (RvdW:insert-drawings fl)
    ) ; if
  ) ; defun

(defun RvdW:insert-drawings ( fl / dbx acver doc l obj )
  (setq dbx
	 (vla-GetInterfaceObject (vlax-get-acad-object)
	   (if
	     (< (setq acver (atoi (getvar "ACADVER"))) 16)
	     "ObjectDBX.AxDbDocument"
	     (strcat "ObjectDBX.AxDbDocument." (itoa acver))
	     )
	   )
	)
  
  (cond ((= (vl-filename-extension fl) ".dxf")
	 (command "_.-insert" fl '(0. 0. 0.) """""")
	 (command "_.explode" (entlast))
	 )
	((= (vl-filename-extension fl) ".dwg")
	 (vla-open dbx fl :vlax-true)
	 (vlax-for obj (vla-get-modelspace (cond (doc) (dbx)))
	   (setq l (cons obj l))
	   )
	 (if l
	   (vlax-invoke (cond (doc) (dbx)) 'copyobjects l (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))
	   )
	 (vlax-release-object dbx)
	 )
	(T)
	)
  )

;;------------------------=={ Get Files Dialog }==----------------------;;
;;                                                                      ;;
;;  An analog of the 'getfiled' function for multiple file selection.   ;;
;;----------------------------------------------------------------------;;
;;  Author:  Lee Mac, Copyright © 2012  -  www.lee-mac.com              ;;
;;----------------------------------------------------------------------;;
;;  Arguments:                                                          ;;
;;  msg - [str/nil] Dialog box label; 'Select Files' if nil or "".      ;;
;;  def - [str/nil] Default directory; dwgprefix if nil or "".          ;;
;;  ext - [str/nil] File extension filter (e.g. "dwg;lsp"); "*" if nil  ;;
;;----------------------------------------------------------------------;;
;;  Returns:  List of selected files, else nil                          ;;
;;----------------------------------------------------------------------;;
;;  Version 1.6    -    2016-03-21                                      ;;
;;----------------------------------------------------------------------;;

(defun LM:getfiles ( msg def ext / *error* dch dcl des dir dirdata lst rtn )

  (defun *error* ( msg )
    (if (= 'file (type des))
      (close des)
      )
    (if (and (= 'int (type dch)) (< 0 dch))
      (unload_dialog dch)
      )
    (if (and (= 'str (type dcl)) (findfile dcl))
      (vl-file-delete dcl)
      )
    (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
      (princ (strcat "\nError: " msg))
      )
    (princ)
    )

  (if
    (and
      (setq dcl (vl-filename-mktemp nil nil ".dcl"))
      (setq des (open dcl "w"))
      (progn
	(foreach x
		 '(
		   "lst : list_box"
		   "{"
		   "    width = 40.0;"
		   "    height = 20.0;"
		   "    fixed_width = true;"
		   "    fixed_height = true;"
                        "    alignment = centered;"
		   "    multiple_select = true;"
		   "}"
		   "but : button"
		   "{"
		   "    width = 20.0;"
		   "    height = 1.8;"
		   "    fixed_width = true;"
		   "    fixed_height = true;"
		   "    alignment = centered;"
		   "}"
		   "getfiles : dialog"
		   "{"
		   "    key = \"title\"; spacer;"
		   "    : row"
		   "    {"
		   "        alignment = centered;"
		   "        : edit_box { key = \"dir\"; label = \"Folder:\"; }"
		   "        : button"
		   "        {"
		   "            key = \"brw\";"
		   "            label = \"Browse\";"
		   "            fixed_width = true;"
		   "        }"
		   "    }"
		   "    spacer;"
		   "    : row"
		   "    {"
		   "        : column"
		   "        {"
		   "            : lst { key = \"box1\"; }"
		   "            : but { key = \"add\" ; label = \"Add Files\"; }"
		   "        }"
		   "        : column {"
		   "            : lst { key = \"box2\"; }"
		   "            : but { key = \"del\" ; label = \"Remove Files\"; }"
		   "        }"
		   "    }"
		   "    spacer; ok_cancel;"
		   "}"
		   )
	  (write-line x des)
	  )
	(setq des (close des))
	(< 0 (setq dch (load_dialog dcl)))
	)
      (new_dialog "getfiles" dch)
      )
    (progn
      (setq ext (if (= 'str (type ext)) (LM:getfiles:str->lst (strcase ext) ";") '("*")))
      (set_tile "title" (if (member msg '(nil "")) "Select Files" msg))
      (set_tile "dir"
		(setq dir
		       (LM:getfiles:fixdir
			 (if (or (member def '(nil "")) (not (vl-file-directory-p (LM:getfiles:fixdir def))))
			   (getvar 'dwgprefix)
			   def
			   )
			 )
		      )
		)
      (setq lst (LM:getfiles:updatefilelist dir ext nil))
      (mode_tile "add" 1)
      (mode_tile "del" 1)

      (action_tile "brw"
	(vl-prin1-to-string
	  '(if (setq tmp (LM:getfiles:browseforfolder "" nil 512))
	    (setq lst (LM:getfiles:updatefilelist (set_tile "dir" (setq dir tmp)) ext rtn)
	     rtn (LM:getfiles:updateselected dir rtn)
	     )
	    )
	  )
	)

      (action_tile "dir"
	(vl-prin1-to-string
	  '(if (= 1 $reason)
	    (setq lst (LM:getfiles:updatefilelist (set_tile "dir" (setq dir (LM:getfiles:fixdir $value))) ext rtn)
	     rtn (LM:getfiles:updateselected dir rtn)
	     )
	    )
	  )
	)

      (action_tile "box1"
	(vl-prin1-to-string
	  '(
	    (lambda ( / itm tmp )
	     (if (setq itm (mapcar '(lambda ( n ) (nth n lst)) (read (strcat "(" $value ")"))))
	      (if (= 4 $reason)
	       (cond
		(   (equal '("..") itm)
		 (setq lst (LM:getfiles:updatefilelist (set_tile "dir" (setq dir (LM:getfiles:updir dir))) ext rtn)
		  rtn (LM:getfiles:updateselected dir rtn)
		  )
		 )
		(   (vl-file-directory-p (setq tmp (LM:getfiles:checkredirect (strcat dir "\\" (car itm)))))
		 (setq lst (LM:getfiles:updatefilelist (set_tile "dir" (setq dir tmp)) ext rtn)
		  rtn (LM:getfiles:updateselected dir rtn)
		  )
		 )
		(   (setq rtn (LM:getfiles:sort (append rtn (mapcar '(lambda ( x ) (strcat dir "\\" x)) itm)))
		     rtn (LM:getfiles:updateselected dir rtn)
		     lst (LM:getfiles:updatefilelist dir ext rtn)
		     )
		 )
		)
	       (if (vl-every '(lambda ( x ) (vl-file-directory-p (strcat dir "\\" x))) itm)
		(mode_tile "add" 1)
		(mode_tile "add" 0)
		)
	       )
	      )
	     )
	    )
	  )
	)

      (action_tile "box2"
	(vl-prin1-to-string
	  '(
	    (lambda ( / itm )
	     (if (setq itm (mapcar '(lambda ( n ) (nth n rtn)) (read (strcat "(" $value ")"))))
	      (if (= 4 $reason)
	       (setq rtn (LM:getfiles:updateselected dir (vl-remove (car itm) rtn))
		lst (LM:getfiles:updatefilelist dir ext rtn)
		)
	       (mode_tile "del" 0)
	       )
	      )
	     )
	    )
	  )
	)

      (action_tile "add"
	(vl-prin1-to-string
	  '(
	    (lambda ( / itm )
	     (if
	      (setq itm
	       (vl-remove-if 'vl-file-directory-p
		(mapcar '(lambda ( n ) (nth n lst)) (read (strcat "(" (get_tile "box1") ")")))
		)
	       )
	      (setq rtn (LM:getfiles:sort (append rtn (mapcar '(lambda ( x ) (strcat dir "\\" x)) itm)))
	       rtn (LM:getfiles:updateselected dir rtn)
	       lst (LM:getfiles:updatefilelist dir ext rtn)
	       )
	      )
	     (mode_tile "add" 1)
	     (mode_tile "del" 1)
	     )
	    )
	  )
	)

      (action_tile "del"
	(vl-prin1-to-string
	  '(
	    (lambda ( / itm )
	     (if (setq itm (read (strcat "(" (get_tile "box2") ")")))
	      (setq rtn (LM:getfiles:updateselected dir (LM:getfiles:removeitems itm rtn))
	       lst (LM:getfiles:updatefilelist dir ext rtn)
	       )
	      )
	     (mode_tile "add" 1)
	     (mode_tile "del" 1)
	     )
	    )
	  )
	)

      (if (zerop (start_dialog))
	(setq rtn nil)
	)
      )
    )
  (*error* nil)
  rtn
  )

(defun LM:getfiles:listbox ( key lst )
  (start_list key)
  (foreach x lst (add_list x))
  (end_list)
  lst
  )

(defun LM:getfiles:listfiles ( dir ext lst )
  (vl-remove-if '(lambda ( x ) (member (strcat dir "\\" x) lst))
    (cond
      (   (cdr (assoc dir dirdata)))
      (   (cdar
	    (setq dirdata
		   (cons
		     (cons dir
			   (append
			     (LM:getfiles:sortlist (vl-remove "." (vl-directory-files dir nil -1)))
			     (LM:getfiles:sort
			       (if (member ext '(("") ("*")))
				 (vl-directory-files dir nil 1)
				 (vl-remove-if-not
				   (function
				     (lambda ( x / e )
				       (and
					 (setq e (vl-filename-extension x))
					 (setq e (strcase (substr e 2)))
					 (vl-some '(lambda ( w ) (wcmatch e w)) ext)
					 )
				       )
				     )
				   (vl-directory-files dir nil 1)
				   )
				 )
			       )
			     )
			   )
		     dirdata
		     )
		  )
	    )
       )
      )
    )
  )

(defun LM:getfiles:checkredirect ( dir / itm pos )
  (cond
    (   (vl-directory-files dir) dir)
    (   (and
	  (=  (strcase (getenv "UserProfile"))
	      (strcase (substr dir 1 (setq pos (vl-string-position 92 dir nil t))))
	      )
	  (setq itm
		 (cdr
		   (assoc (substr (strcase dir t) (+ pos 2))
			  '(
			    ("my documents" . "Documents")
			    ("my pictures"  . "Pictures")
			    ("my videos"    . "Videos")
			    ("my music"     . "Music")
			    )
			  )
		   )
		)
	  (vl-file-directory-p (setq itm (strcat (substr dir 1 pos) "\\" itm)))
	  )
     itm
     )
    (   dir   )
    )
  )

(defun LM:getfiles:sort ( lst )
  (apply 'append
	 (mapcar 'LM:getfiles:sortlist
		 (vl-sort
		   (LM:getfiles:groupbyfunction lst
		     (lambda ( a b / x y )
		       (and
			 (setq x (vl-filename-extension a))
			 (setq y (vl-filename-extension b))
			 (= (strcase x) (strcase y))
			 )
		       )
		     )
		   (function
		     (lambda ( a b / x y )
		       (and
			 (setq x (vl-filename-extension (car a)))
			 (setq y (vl-filename-extension (car b)))
			 (< (strcase x) (strcase y))
			 )
		       )
		     )
		   )
		 )
	 )
  )

(defun LM:getfiles:sortlist ( lst )
  (mapcar (function (lambda ( n ) (nth n lst)))
	  (vl-sort-i (mapcar 'LM:getfiles:splitstring lst)
	    (function
	      (lambda ( a b / x y )
		(while
		  (and
		    (setq x (car a))
		    (setq y (car b))
		    (= x y)
		    )
		  (setq a (cdr a)
			b (cdr b)
			)
		  )
		(cond
		  (   (null x) b)
		  (   (null y) nil)
		  (   (and (numberp x) (numberp y)) (< x y))
		  (   (numberp x))
		  (   (numberp y) nil)
		  (   (< x y))
		  )
		)
	      )
	    )
	  )
  )

(defun LM:getfiles:groupbyfunction ( lst fun / tmp1 tmp2 x1 )
  (if (setq x1 (car lst))
    (progn
      (foreach x2 (cdr lst)
	(if (fun x1 x2)
	  (setq tmp1 (cons x2 tmp1))
	  (setq tmp2 (cons x2 tmp2))
	  )
	)
      (cons (cons x1 (reverse tmp1)) (LM:getfiles:groupbyfunction (reverse tmp2) fun))
      )
    )
  )

(defun LM:getfiles:splitstring ( str )
  (
   (lambda ( l )
     (read
       (strcat "("
	       (vl-list->string
		 (apply 'append
			(mapcar
			  (function
			    (lambda ( a b c )
			      (cond
				(   (member b '(45 46 92))
				 (list 32)
				 )
				(   (< 47 b 58)
				 (list b)
				 )
				(   (list 32 34 b 34 32))
				)
			      )
			    )
			  (cons nil l) l (append (cdr l) '(( )))
			  )
			)
		 )
	       ")"
	       )
       )
     )
    (vl-string->list (strcase str))
    )
  )

(defun LM:getfiles:browseforfolder ( msg dir flg / err fld pth shl slf )
  (setq err
	 (vl-catch-all-apply
	   (function
	     (lambda ( / app hwd )
	       (if (setq app (vlax-get-acad-object)
			 shl (vla-getinterfaceobject app "shell.application")
			 hwd (vl-catch-all-apply 'vla-get-hwnd (list app))
			 fld (vlax-invoke-method shl 'browseforfolder (if (vl-catch-all-error-p hwd) 0 hwd) msg flg dir)
			 )
		 (setq slf (vlax-get-property fld 'self)
		       pth (LM:getfiles:fixdir (vlax-get-property slf 'path))
		       )
		 )
	       )
	     )
	   )
	)
  (if slf (vlax-release-object slf))
  (if fld (vlax-release-object fld))
  (if shl (vlax-release-object shl))
  (if (vl-catch-all-error-p err)
    (prompt (vl-catch-all-error-message err))
    pth
    )
  )

(defun LM:getfiles:full->relative ( dir path / p q )
  (setq dir (vl-string-right-trim "\\" dir))
  (cond
    (   (and
	  (setq p (vl-string-position 58  dir))
	  (setq q (vl-string-position 58 path))
	  (/= (strcase (substr dir 1 p)) (strcase (substr path 1 q)))
	  )
     path
     )
    (   (and
	  (setq p (vl-string-position 92  dir))
	  (setq q (vl-string-position 92 path))
	  (= (strcase (substr dir 1 p)) (strcase (substr path 1 q)))
	  )
     (LM:getfiles:full->relative (substr dir (+ 2 p)) (substr path (+ 2 q)))
     )
    (   (and
	  (setq q (vl-string-position 92 path))
	  (= (strcase dir) (strcase (substr path 1 q)))
	  )
     (strcat ".\\" (substr path (+ 2 q)))
     )
    (   (= "" dir)
     path
     )
    (   (setq p (vl-string-position 92 dir))
     (LM:getfiles:full->relative (substr dir (+ 2 p)) (strcat "..\\" path))
     )
    (   (LM:getfiles:full->relative "" (strcat "..\\" path)))
    )
  )

(defun LM:getfiles:str->lst ( str del / pos )
  (if (setq pos (vl-string-search del str))
    (cons (substr str 1 pos) (LM:getfiles:str->lst (substr str (+ pos 1 (strlen del))) del))
    (list str)
    )
  )

(defun LM:getfiles:updatefilelist ( dir ext lst )
  (LM:getfiles:listbox "box1" (LM:getfiles:listfiles dir ext lst))
  )

(defun LM:getfiles:updateselected ( dir lst )
  (LM:getfiles:listbox "box2" (mapcar '(lambda ( x ) (LM:getfiles:full->relative dir x)) lst))
  lst
  )

(defun LM:getfiles:updir ( dir )
  (substr dir 1 (vl-string-position 92 dir nil t))
  )

(defun LM:getfiles:fixdir ( dir )
  (vl-string-right-trim "\\" (vl-string-translate "/" "\\" dir))
  )

(defun LM:getfiles:removeitems ( itm lst / idx )
  (setq idx -1)
  (vl-remove-if '(lambda ( x ) (member (setq idx (1+ idx)) itm)) lst)
  )

; ____________________________ end LM:getfiles ____________________________

 

0 Likes
Message 3 of 8

Sea-Haven
Mentor
Mentor

Please disregard this if dxf files are all over the place. Thats a lot of code above, if the dxf files are say in the one directory then about 4 lines of code will make a list of them. Then a foreach will dxfin them in. Limited testing as don't have many DXF's.

 

 

 

(defun c:wow ( / file-path dir-name files file)
(prompt "Select any dxf file for path ")
(setq file-path (getfiled "Select dxf file for directory location " "" "dxf" 16)) ; Prompt the user to select a file
(setq dir-name (vl-filename-directory file-path)) ; Extract the directory name
(setq files (vl-directory-files dir-name "*.dxf" 0))
(foreach file files
(command "dxfin" (strcat dir-name "\\" file))
)
(command "zoom" "E")
(princ)
)
(c:wow)

 

 

Be aware that dxfin uses 0,0 as base point so if all your dxf's are relevant to 0,0 they may go on top of each other, if thats the case maybe use -INSERT instead, as can work out side by side placement.

 

 

Message 4 of 8

baksconstructor
Advocate
Advocate

Use this - AutoImportCAD

0 Likes
Message 5 of 8

cadffm
Consultant
Consultant

BTW: "For DXF files you do have to make sure the UNITS are the same as the master drawing"

 

If you don't decide different units, yes.

but if not: Not "the same", but THE RIGHT units.

And if insunits 0 involved, make sure insunitsdefsource/insunitsdeftarget is set to your needs.

 

 

Sebastian

0 Likes
Message 6 of 8

roel.vander.werff
Explorer
Explorer

Hi cadffm,

 

The problem I have with choosing a differend Unit is that my Aligned Dimension's are not converted giving the wrong size to the text and symbols.

Besides that you indeed could choose a differend Unit.

Any idea why you can't use (vla-open dbx fl :vlax-true) for DXF files, it would make live so much easier.

0 Likes
Message 7 of 8

baksconstructor
Advocate
Advocate

The point is that when you combine files, you use blocks with the same names. Sizes are also blocks.
You need a special program to combine files.
You can't just paste them into each other - it will be a mess.

0 Likes
Message 8 of 8

roel.vander.werff
Explorer
Explorer

That's why I use VLA-OPEN for DWG files, I combine files by the masses this way.

As we have standardized our revision-drawings this works like a charm.

And the Aligned Dimension's get the correct measurements even when the Units of the drawings are different.

This does not work with Insert for DXF files, which is why I wonder why I can't use VLA-OPEN for DXF files and hope there my be an alternative VLA command that works with a DXF.

0 Likes