Author Topic: Insert block on MLeader vertices  (Read 1049 times)

0 Members and 1 Guest are viewing this topic.

Ndruu

  • Mosquito
  • Posts: 5
Insert block on MLeader vertices
« on: September 23, 2022, 12:52:04 PM »
Hello!

I found Lee's amazing code here on the forums. http://www.theswamp.org/index.php?topic=55023.msg594013#msg594013
It inserts points on the vertices the selected MLeader.

It was not hard to modify the code to insert a block instead of points, but
I would like to select multiple MLeaders and iterate through all of them, inserting my block on all the vertices.
I am not as advanced in coding to write it myself so I am asking for your help.

This is how far I got with it:

Code: [Select]
(defun LM:mleadervertices ( ent )
    (mapcar '(lambda ( x ) (massoc 10 x))
        (massoc "LEADER_LINE{"
            (cdr
                (assoc "LEADER{"
                    (cdr
                        (assoc "CONTEXT_DATA{"
                            (parsedxfdata (entget ent))
                        )
                    )
                )
            )
        )
    )
)
(defun massoc ( k l )
    (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= k (car x))) l))
)
(defun parsedxfdata ( l / foo )
    (defun foo ( / x )
        (setq x (car l)
              l (cdr l)
        )
        (cond
            (   (or (null x) (= "}" (cdr x)))
                nil
            )
            (   (and (= 'str (type (cdr x))) (wcmatch (cdr x) "*{*"))
                (cons (cons (cdr x) (foo)) (foo))
            )
            (   (cons x (foo)))
        )
    )
    (foo)
)

(defun c:test ( / c e )
    (if
        (and
            (setq e (car (entsel "\nSelect mleader: ")))
            (= "MULTILEADER" (cdr (assoc 0 (entget e))))
            (setq c 1)
        )
        (foreach l (LM:mleadervertices e)
            (foreach v l
      (entmake (list '(0 . "INSERT") '(2 . "myblockname") (cons 10 v) (cons 62 c) '(41 . 1.0) '(42 . 1.0) '(43 . 1.0) '(50 . 0)))
            )
            (setq c (1+ (rem c 255)))
        )
    )
)


ronjonp

  • Needs a day job
  • Posts: 7528
Re: Insert block on MLeader vertices
« Reply #1 on: September 23, 2022, 01:52:58 PM »
Welcome to TheSwamp! Give this a go:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ c s)
  2.   (if (and ;; Use ssget filtering out mleaders
  3.            (setq s (ssget '((0 . "MULTILEADER"))))
  4.            (setq c 1)
  5.       )
  6.     ;; Then iterate the selections set
  7.     ;; Check out Lee's site for many ways to do this .. this is the 'slow' way :)
  8.     ;; http://www.lee-mac.com/selsetprocessing.html
  9.     (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
  10.       (foreach l (lm:mleadervertices e)
  11.         (foreach v l
  12.           (entmake (list '(0 . "INSERT")
  13.                          '(2 . "myblockname")
  14.                          (cons 10 v)
  15.                          (cons 62 c)
  16.                          '(41 . 1.0)
  17.                          '(42 . 1.0)
  18.                          '(43 . 1.0)
  19.                          '(50 . 0)
  20.                    )
  21.           )
  22.         )
  23.         (setq c (1+ (rem c 255)))
  24.       )
  25.     )
  26.   )
  27.   (princ)
  28. )
« Last Edit: September 23, 2022, 09:35:22 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Ndruu

  • Mosquito
  • Posts: 5
Re: Insert block on MLeader vertices
« Reply #2 on: September 23, 2022, 07:39:43 PM »
Thank you ronjonp!

I tried the code but it gives me Error: bad argument type: lentityp nil after I select the multileaders and the program halts, not inserting any blocks.
In VLIDE it stops on

(= "MULTILEADER" (cdr (assoc 0 (entget e))))

So I fiddled around the code and with the help of google the following works, but it is not 100%, because the lentityp nil error is still there.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ c s)
  2.   (defun *error* (msg)
  3.    (if  (not
  4.   (member msg '("Function cancelled" "quit / exit abort"))
  5. )
  6.      (princ (strcat "\nError: " msg))
  7.    )
  8.  )
  9.   ((= "MULTILEADER" (cdr (assoc 0 (entget e)))) ;
  10.   (if (and ;; Use ssget filtering out mleaders
  11.            (setq s (ssget '((0 . "MULTILEADER"))))
  12.            ;; this line moved before if (= "MULTILEADER" (cdr (assoc 0 (entget e))))
  13.            (setq c 1)
  14.       )
  15.     ;; Then iterate the selections set
  16.     ;; Check out Lee's site for many ways to do this .. this is the 'slow' way :)
  17.     ;; http://www.lee-mac.com/selsetprocessing.html
  18.     (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
  19.       (foreach l (lm:mleadervertices e)
  20.         (foreach v l
  21.           (entmake (list '(0 . "INSERT")
  22.                                '(2 . "myblockname")
  23.                                (cons 10 v)
  24.                                (cons 62 c)
  25.                                '(41 . 1.0)
  26.                                '(42 . 1.0)
  27.                                '(43 . 1.0)
  28.                                '(50 . 0)
  29.                    )
  30.           )
  31.         )
  32.         (setq c (1+ (rem c 255)))
  33.       )
  34.     )
  35.     (princ)
  36.   ) ;if
  37.   (princ)
  38.  ) ;compare
  39. )

ronjonp

  • Needs a day job
  • Posts: 7528
Re: Insert block on MLeader vertices
« Reply #3 on: September 23, 2022, 09:32:50 PM »
My bad ..copy paste then off to something else. Try the updated code above.  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Ndruu

  • Mosquito
  • Posts: 5
Re: Insert block on MLeader vertices
« Reply #4 on: September 26, 2022, 11:10:18 AM »
Works like a charm! Thank you very much ronjonp!

ronjonp

  • Needs a day job
  • Posts: 7528
Re: Insert block on MLeader vertices
« Reply #5 on: September 26, 2022, 02:41:09 PM »
Works like a charm! Thank you very much ronjonp!
Glad to help.  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC