Author Topic: Please help me fix the Lisp code that merges the drawings  (Read 484 times)

0 Members and 1 Guest are viewing this topic.

hcbn1997

  • Newt
  • Posts: 31
Please help me fix the Lisp code that merges the drawings
« on: April 05, 2024, 12:21:43 AM »
I often use lisp to merge imf drawings I found on here. But there is a problem: if you combine 100 drawings, they will all be arranged in a horizontal row, making tracking more difficult.
I hope you pros can help me fix it so that when I merge the drawings, there are 10 copies in 1 row.
For example, 100 drawings will have 10 vertical rows and 10 horizontal rows.
The other day I fixed the horizontal rows to be 1000 apart. Please, if you fix it, give me the distance between the vertical rows to be 5000 like in the photo.
Thanks everyone

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Please help me fix the Lisp code that merges the drawings
« Reply #1 on: April 05, 2024, 01:46:55 PM »
Here, try this mod., but I am unsure - it's untested, but should work...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:imf ( / pathname filelist p xht yht oldos k p1 p2 blname ptl ) ;;;Insert Multi Files by Nguyen Hoanh ( bo tat ca ban ve vao 1 file rieng, roi nhan lenh, chon ban ve dau tien )
  2.                    (getfiled "Hay chon file dwg bat ky thuoc thu muc" "" "dwg" 0)
  3.                    )
  4.         filelist (vl-sort (vl-directory-files pathname "*.dwg") '<)
  5.         p        (getpoint "\nDiem chen: ")
  6.         xht      (car p)
  7.         yht      (cadr p)
  8.         )
  9.   (setq oldos (getvar "osmode"))
  10.   (setvar "osmode" 0)
  11.   (setq k 0)
  12.   (foreach filename filelist
  13.     (setq k (1+ k))
  14.     (if (/= 0 (rem k 11))
  15.       (progn
  16.         (command "-insert"
  17.                  (strcat pathname "/" filename)
  18.                  (list xht yht)
  19.                  1.0
  20.                  1.0
  21.                  0.0
  22.                  )
  23.         (vla-getboundingbox
  24.           (vlax-ename->vla-object (entlast))
  25.           'p1
  26.           'p2
  27.           )
  28.         (setq p1     (vlax-safearray->list p1)
  29.               p2     (vlax-safearray->list p2)
  30.               xht    (+ xht (abs (car (mapcar '- p2 p1))) 1000)
  31.               blname (cdr (assoc 2 (entget (entlast))))
  32.               )
  33.         (setq ptl (cons (abs (cadr (mapcar '- p2 p1))) ptl))
  34.         (command ".explode" (entlast) "")
  35.         (command "-purge" "Block" blname "N")
  36.         )
  37.       (progn
  38.         (setq k (1+ k) xht (car p) yht (+ yht (car (vl-sort ptl '>)) 5000) ptl nil)
  39.         (command "-insert"
  40.                  (strcat pathname "/" filename)
  41.                  (list xht yht)
  42.                  1.0
  43.                  1.0
  44.                  0.0
  45.                  )
  46.         (vla-getboundingbox
  47.           (vlax-ename->vla-object (entlast))
  48.           'p1
  49.           'p2
  50.           )
  51.         (setq p1     (vlax-safearray->list p1)
  52.               p2     (vlax-safearray->list p2)
  53.               xht    (+ xht (abs (car (mapcar '- p2 p1))) 1000)
  54.               blname (cdr (assoc 2 (entget (entlast))))
  55.               )
  56.         (setq ptl (cons (abs (cadr (mapcar '- p2 p1))) ptl))
  57.         (command ".explode" (entlast) "")
  58.         (command "-purge" "Block" blname "N")
  59.         )
  60.       )
  61.     )
  62.   (setvar "osmode" oldos)
  63.   (princ)
  64.   )
  65.  
  66. (defun c:exf ( / filenamevalid getboundingbox ss2ent dxf gettag entbl blname taglst index tag ss lst e f fh ) ;;;EXtract Files by Nguyen Hoanh
  67.   (defun filenamevalid (str)
  68.     (vl-list->string
  69.       (vl-remove-if
  70.         '(lambda (x) (member x (vl-string->list "\\/:?>|")))
  71.         (vl-string->list str)
  72.         )
  73.       )
  74.     )
  75.   (defun getboundingbox ( ent / p1 p2 )
  76.     (vla-getboundingbox (vlax-ename->vla-object ent) 'p1 'p2)
  77.     (list (setq p1 (vlax-safearray->list p1))
  78.           (setq p2 (vlax-safearray->list p2))
  79.           )
  80.     )
  81.   (defun ss2ent ( ss / sodt index ent lstent )
  82.     (setq sodt  (if ss
  83.                   (sslength ss)
  84.                   0
  85.                   )
  86.           index 0
  87.           )
  88.     (repeat sodt
  89.       (setq ent    (ssname ss index)
  90.             index  (1+ index)
  91.             lstent (cons ent lstent)
  92.             )
  93.       )
  94.     (reverse lstent)
  95.     )
  96.   (defun dxf ( ent code ) (cdr (assoc code (entget ent))))
  97.   (defun gettag ( ent / entbl lst )
  98.     (setq entbl ent)
  99.     (while (and (setq entbl (entnext entbl))
  100.                 (= (dxf entbl 0) "ATTRIB")
  101.                 )
  102.       (setq lst (append lst (list (cons (dxf entbl 2) (dxf entbl 1)))))
  103.       )
  104.     lst
  105.     )
  106.   (setq entbl  (car (entsel "\nHay pick vao block khung ten"))
  107.         blname (dxf entbl 2)
  108.         taglst (gettag entbl)
  109.         index  0
  110.         )
  111.   (princ "\nCac tag trong block:")
  112.   (foreach pp taglst
  113.     (princ (strcat "\n" (itoa index) ": " (car pp)))
  114.     (setq index (1+ index))
  115.     )
  116.   (textscr)
  117.   (setq
  118.     tag (car
  119.           (nth (getint "\nHay nhan 0,1,2... de chon tag: ") taglst)
  120.           )
  121.     )
  122.   (command ".zoom" "e")
  123.   (setq oldos (getvar "osmode"))
  124.   (setvar "osmode" 0)
  125.   (setq ss  (ssget "x" (list (cons 0 "INSERT") (cons 2 blname)))
  126.         lst (ss2ent ss)
  127.         lst (mapcar '(lambda (e)
  128.                        (append (list e (cdr (assoc tag (gettag e))))
  129.                                (getboundingbox e)
  130.                                )
  131.                        )
  132.                     lst
  133.                     )
  134.         )
  135.   (foreach pp lst
  136.     (setq e  (nth 0 pp)
  137.           f  (strcat (getvar "dwgprefix")
  138.                      (filenamevalid (nth 1 pp))
  139.                      ".dwg"
  140.                      )
  141.           p1 (nth 2 pp)
  142.           p2 (nth 3 pp)
  143.           ss (ssget "_w" p1 p2)
  144.           ss (ssadd e ss)
  145.           )
  146.     (command ".wblock" f)
  147.     (if (setq fh (open f "r"))
  148.       (progn (close fh) (command "y"))
  149.       )
  150.     (command "" p1 ss "")
  151.     (command ".oops")
  152.     )
  153.   (setvar "osmode" oldos)
  154.   (command ".zoom" "p")
  155.   (princ)
  156.   )
  157.  

HTH.
Regards, M.R.
« Last Edit: April 05, 2024, 03:18:57 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

hcbn1997

  • Newt
  • Posts: 31
Re: Please help me fix the Lisp code that merges the drawings
« Reply #2 on: April 05, 2024, 09:17:03 PM »
Here, try this mod., but I am unsure - it's untested, but should work...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:imf ( / pathname filelist p xht yht oldos k p1 p2 blname ptl ) ;;;Insert Multi Files by Nguyen Hoanh ( bo tat ca ban ve vao 1 file rieng, roi nhan lenh, chon ban ve dau tien )
  2.                    (getfiled "Hay chon file dwg bat ky thuoc thu muc" "" "dwg" 0)
  3.                    )
  4.         filelist (vl-sort (vl-directory-files pathname "*.dwg") '<)
  5.         p        (getpoint "\nDiem chen: ")
  6.         xht      (car p)
  7.         yht      (cadr p)
  8.         )
  9.   (setq oldos (getvar "osmode"))
  10.   (setvar "osmode" 0)
  11.   (setq k 0)
  12.   (foreach filename filelist
  13.     (setq k (1+ k))
  14.     (if (/= 0 (rem k 11))
  15.       (progn
  16.         (command "-insert"
  17.                  (strcat pathname "/" filename)
  18.                  (list xht yht)
  19.                  1.0
  20.                  1.0
  21.                  0.0
  22.                  )
  23.         (vla-getboundingbox
  24.           (vlax-ename->vla-object (entlast))
  25.           'p1
  26.           'p2
  27.           )
  28.         (setq p1     (vlax-safearray->list p1)
  29.               p2     (vlax-safearray->list p2)
  30.               xht    (+ xht (abs (car (mapcar '- p2 p1))) 1000)
  31.               blname (cdr (assoc 2 (entget (entlast))))
  32.               )
  33.         (setq ptl (cons (abs (cadr (mapcar '- p2 p1))) ptl))
  34.         (command ".explode" (entlast) "")
  35.         (command "-purge" "Block" blname "N")
  36.         )
  37.       (progn
  38.         (setq k (1+ k) xht (car p) yht (+ yht (car (vl-sort ptl '>)) 5000) ptl nil)
  39.         (command "-insert"
  40.                  (strcat pathname "/" filename)
  41.                  (list xht yht)
  42.                  1.0
  43.                  1.0
  44.                  0.0
  45.                  )
  46.         (vla-getboundingbox
  47.           (vlax-ename->vla-object (entlast))
  48.           'p1
  49.           'p2
  50.           )
  51.         (setq p1     (vlax-safearray->list p1)
  52.               p2     (vlax-safearray->list p2)
  53.               xht    (+ xht (abs (car (mapcar '- p2 p1))) 1000)
  54.               blname (cdr (assoc 2 (entget (entlast))))
  55.               )
  56.         (setq ptl (cons (abs (cadr (mapcar '- p2 p1))) ptl))
  57.         (command ".explode" (entlast) "")
  58.         (command "-purge" "Block" blname "N")
  59.         )
  60.       )
  61.     )
  62.   (setvar "osmode" oldos)
  63.   (princ)
  64.   )
  65.  
  66. (defun c:exf ( / filenamevalid getboundingbox ss2ent dxf gettag entbl blname taglst index tag ss lst e f fh ) ;;;EXtract Files by Nguyen Hoanh
  67.   (defun filenamevalid (str)
  68.     (vl-list->string
  69.       (vl-remove-if
  70.         '(lambda (x) (member x (vl-string->list "\\/:?>|")))
  71.         (vl-string->list str)
  72.         )
  73.       )
  74.     )
  75.   (defun getboundingbox ( ent / p1 p2 )
  76.     (vla-getboundingbox (vlax-ename->vla-object ent) 'p1 'p2)
  77.     (list (setq p1 (vlax-safearray->list p1))
  78.           (setq p2 (vlax-safearray->list p2))
  79.           )
  80.     )
  81.   (defun ss2ent ( ss / sodt index ent lstent )
  82.     (setq sodt  (if ss
  83.                   (sslength ss)
  84.                   0
  85.                   )
  86.           index 0
  87.           )
  88.     (repeat sodt
  89.       (setq ent    (ssname ss index)
  90.             index  (1+ index)
  91.             lstent (cons ent lstent)
  92.             )
  93.       )
  94.     (reverse lstent)
  95.     )
  96.   (defun dxf ( ent code ) (cdr (assoc code (entget ent))))
  97.   (defun gettag ( ent / entbl lst )
  98.     (setq entbl ent)
  99.     (while (and (setq entbl (entnext entbl))
  100.                 (= (dxf entbl 0) "ATTRIB")
  101.                 )
  102.       (setq lst (append lst (list (cons (dxf entbl 2) (dxf entbl 1)))))
  103.       )
  104.     lst
  105.     )
  106.   (setq entbl  (car (entsel "\nHay pick vao block khung ten"))
  107.         blname (dxf entbl 2)
  108.         taglst (gettag entbl)
  109.         index  0
  110.         )
  111.   (princ "\nCac tag trong block:")
  112.   (foreach pp taglst
  113.     (princ (strcat "\n" (itoa index) ": " (car pp)))
  114.     (setq index (1+ index))
  115.     )
  116.   (textscr)
  117.   (setq
  118.     tag (car
  119.           (nth (getint "\nHay nhan 0,1,2... de chon tag: ") taglst)
  120.           )
  121.     )
  122.   (command ".zoom" "e")
  123.   (setq oldos (getvar "osmode"))
  124.   (setvar "osmode" 0)
  125.   (setq ss  (ssget "x" (list (cons 0 "INSERT") (cons 2 blname)))
  126.         lst (ss2ent ss)
  127.         lst (mapcar '(lambda (e)
  128.                        (append (list e (cdr (assoc tag (gettag e))))
  129.                                (getboundingbox e)
  130.                                )
  131.                        )
  132.                     lst
  133.                     )
  134.         )
  135.   (foreach pp lst
  136.     (setq e  (nth 0 pp)
  137.           f  (strcat (getvar "dwgprefix")
  138.                      (filenamevalid (nth 1 pp))
  139.                      ".dwg"
  140.                      )
  141.           p1 (nth 2 pp)
  142.           p2 (nth 3 pp)
  143.           ss (ssget "_w" p1 p2)
  144.           ss (ssadd e ss)
  145.           )
  146.     (command ".wblock" f)
  147.     (if (setq fh (open f "r"))
  148.       (progn (close fh) (command "y"))
  149.       )
  150.     (command "" p1 ss "")
  151.     (command ".oops")
  152.     )
  153.   (setvar "osmode" oldos)
  154.   (command ".zoom" "p")
  155.   (princ)
  156.   )
  157.  

HTH.
Regards, M.R.
Oh, it works exactly as I described. That's great, thank you so much for your help.
But one thing I hope the fix helps with is the merge sequence. Your code will merge from bottom to top but I would like it to merge from top to bottom.
For example, if there are 23 drawings, your current lisp's first row will have 3 drawings and the next 2 rows will have 10 drawings. What I want is that the first and second rows have 10 drawings and the third row has 3 drawings. The order from top to bottom I think will be easier to follow.
Looking forward to receiving your further help!

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Please help me fix the Lisp code that merges the drawings
« Reply #3 on: April 06, 2024, 02:17:27 AM »
OK, just change line 39 from this :
        (setq k (1+ k) xht (car p) yht (+ yht (car (vl-sort ptl '>)) 5000) ptl nil)
to this :
        (setq k (1+ k) xht (car p) yht (- yht (car (vl-sort ptl '>)) 5000) ptl nil)

Plus, you'll have to change sysvar "BASE" to be top-left point to all DWG files...
So maybe better is to leave like it was from bottom-left to top-right sorting of DWG files - and line 39 with that "+"
« Last Edit: April 06, 2024, 02:26:33 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Please help me fix the Lisp code that merges the drawings
« Reply #4 on: April 06, 2024, 08:37:55 AM »
Or even better... BASE should stay on 0,0,0 for every drawing, just make sure that extends lower left point of drawn title blocks is 0,0,0...
I've modified line 40 and added lines 35 and 59

Code - Auto/Visual Lisp: [Select]
  1. (defun c:imf ( / pathname filelist p xht yht oldos k p1 p2 blname ptl ) ;;;Insert Multi Files by Nguyen Hoanh ( bo tat ca ban ve vao 1 file rieng, roi nhan lenh, chon ban ve dau tien )
  2.                    (getfiled "Hay chon file dwg bat ky thuoc thu muc" "" "dwg" 0)
  3.                    )
  4.         filelist (vl-sort (vl-directory-files pathname "*.dwg") '<)
  5.         p        (getpoint "\nDiem chen: ")
  6.         xht      (car p)
  7.         yht      (cadr p)
  8.         )
  9.   (setq oldos (getvar "osmode"))
  10.   (setvar "osmode" 0)
  11.   (setq k 0)
  12.   (foreach filename filelist
  13.     (setq k (1+ k))
  14.     (if (/= 0 (rem k 11))
  15.       (progn
  16.         (command "-insert"
  17.                  (strcat pathname "/" filename)
  18.                  (list xht yht)
  19.                  1.0
  20.                  1.0
  21.                  0.0
  22.                  )
  23.         (vla-getboundingbox
  24.           (vlax-ename->vla-object (entlast))
  25.           'p1
  26.           'p2
  27.           )
  28.         (setq p1     (vlax-safearray->list p1)
  29.               p2     (vlax-safearray->list p2)
  30.               xht    (+ xht (abs (car (mapcar '- p2 p1))) 1000)
  31.               blname (cdr (assoc 2 (entget (entlast))))
  32.               )
  33.         (setq ptl (cons (abs (cadr (mapcar '- p2 p1))) ptl))
  34.         (command ".move" (entlast) "" (list 0.0 0.0 0.0) (list 0.0 (- (car ptl)) 0.0))
  35.         (command ".explode" (entlast) "")
  36.         (command "-purge" "Block" blname "N")
  37.         )
  38.       (progn
  39.         (setq k (1+ k) xht (car p) yht (- yht (car (vl-sort ptl '>)) 5000) ptl nil)
  40.         (command "-insert"
  41.                  (strcat pathname "/" filename)
  42.                  (list xht yht)
  43.                  1.0
  44.                  1.0
  45.                  0.0
  46.                  )
  47.         (vla-getboundingbox
  48.           (vlax-ename->vla-object (entlast))
  49.           'p1
  50.           'p2
  51.           )
  52.         (setq p1     (vlax-safearray->list p1)
  53.               p2     (vlax-safearray->list p2)
  54.               xht    (+ xht (abs (car (mapcar '- p2 p1))) 1000)
  55.               blname (cdr (assoc 2 (entget (entlast))))
  56.               )
  57.         (setq ptl (cons (abs (cadr (mapcar '- p2 p1))) ptl))
  58.         (command ".move" (entlast) "" (list 0.0 0.0 0.0) (list 0.0 (- (car ptl)) 0.0))
  59.         (command ".explode" (entlast) "")
  60.         (command "-purge" "Block" blname "N")
  61.         )
  62.       )
  63.     )
  64.   (setvar "osmode" oldos)
  65.   (princ)
  66.   )
  67.  
  68. (defun c:exf ( / filenamevalid getboundingbox ss2ent dxf gettag entbl blname taglst index tag ss lst e f fh ) ;;;EXtract Files by Nguyen Hoanh
  69.   (defun filenamevalid (str)
  70.     (vl-list->string
  71.       (vl-remove-if
  72.         '(lambda (x) (member x (vl-string->list "\\/:?>|")))
  73.         (vl-string->list str)
  74.         )
  75.       )
  76.     )
  77.   (defun getboundingbox ( ent / p1 p2 )
  78.     (vla-getboundingbox (vlax-ename->vla-object ent) 'p1 'p2)
  79.     (list (setq p1 (vlax-safearray->list p1))
  80.           (setq p2 (vlax-safearray->list p2))
  81.           )
  82.     )
  83.   (defun ss2ent ( ss / sodt index ent lstent )
  84.     (setq sodt  (if ss
  85.                   (sslength ss)
  86.                   0
  87.                   )
  88.           index 0
  89.           )
  90.     (repeat sodt
  91.       (setq ent    (ssname ss index)
  92.             index  (1+ index)
  93.             lstent (cons ent lstent)
  94.             )
  95.       )
  96.     (reverse lstent)
  97.     )
  98.   (defun dxf ( ent code ) (cdr (assoc code (entget ent))))
  99.   (defun gettag ( ent / entbl lst )
  100.     (setq entbl ent)
  101.     (while (and (setq entbl (entnext entbl))
  102.                 (= (dxf entbl 0) "ATTRIB")
  103.                 )
  104.       (setq lst (append lst (list (cons (dxf entbl 2) (dxf entbl 1)))))
  105.       )
  106.     lst
  107.     )
  108.   (setq entbl  (car (entsel "\nHay pick vao block khung ten"))
  109.         blname (dxf entbl 2)
  110.         taglst (gettag entbl)
  111.         index  0
  112.         )
  113.   (princ "\nCac tag trong block:")
  114.   (foreach pp taglst
  115.     (princ (strcat "\n" (itoa index) ": " (car pp)))
  116.     (setq index (1+ index))
  117.     )
  118.   (textscr)
  119.   (setq
  120.     tag (car
  121.           (nth (getint "\nHay nhan 0,1,2... de chon tag: ") taglst)
  122.           )
  123.     )
  124.   (command ".zoom" "e")
  125.   (setq oldos (getvar "osmode"))
  126.   (setvar "osmode" 0)
  127.   (setq ss  (ssget "x" (list (cons 0 "INSERT") (cons 2 blname)))
  128.         lst (ss2ent ss)
  129.         lst (mapcar '(lambda (e)
  130.                        (append (list e (cdr (assoc tag (gettag e))))
  131.                                (getboundingbox e)
  132.                                )
  133.                        )
  134.                     lst
  135.                     )
  136.         )
  137.   (foreach pp lst
  138.     (setq e  (nth 0 pp)
  139.           f  (strcat (getvar "dwgprefix")
  140.                      (filenamevalid (nth 1 pp))
  141.                      ".dwg"
  142.                      )
  143.           p1 (nth 2 pp)
  144.           p2 (nth 3 pp)
  145.           ss (ssget "_w" p1 p2)
  146.           ss (ssadd e ss)
  147.           )
  148.     (command ".wblock" f)
  149.     (if (setq fh (open f "r"))
  150.       (progn (close fh) (command "y"))
  151.       )
  152.     (command "" p1 ss "")
  153.     (command ".oops")
  154.     )
  155.   (setvar "osmode" oldos)
  156.   (command ".zoom" "p")
  157.   (princ)
  158.   )
  159.  
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

hcbn1997

  • Newt
  • Posts: 31
Re: Please help me fix the Lisp code that merges the drawings
« Reply #5 on: April 07, 2024, 07:49:00 PM »
It's great, exactly what I need.
 Thank you so much!!! :smitten: :smitten: :smitten: :smitten: :smitten: