Author Topic: Xlist Block Name to MLeader Text  (Read 4031 times)

0 Members and 1 Guest are viewing this topic.

BIGAL

  • Swamp Rat
  • Posts: 1398
  • 40 + years of using Autocad
Re: Xlist Block Name to MLeader Text
« Reply #15 on: March 30, 2021, 09:46:55 PM »
Nice coding Ronjonp sometimes with deep caddddd etc may be easier to understand using nth must remember though that nth starts at 0.

(vla-get-effectivename (vlax-ename->vla-object (car (nth 3 e))))
"90LU"

(vla-get-effectivename (vlax-ename->vla-object (nth 0 (nth 3 e))))
"90LU"
A man who never made a mistake never made anything

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Re: Xlist Block Name to MLeader Text
« Reply #16 on: March 31, 2021, 10:06:34 AM »
Interesting.... Thanks for sharing what it does. Always learning.... lol
Civil3D 2020

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Xlist Block Name to MLeader Text
« Reply #17 on: March 31, 2021, 10:25:33 AM »
An easy way to understand the compound list functions cadr/caddr/caddar/etc. is to recall that they are merely shorthand for a sequence of successive car/cdr functions, e.g.:

_$ (cadddr '(1 2 3 4 5 6 7 8 9))
4


Is equivalent to:

_$ (car (cdr (cdr (cdr '(1 2 3 4 5 6 7 8 9)))))
4


Likewise:

_$ (caddar '((1 2 3 4 5) 6 7 8 9))
3


Is equivalent to:

_$ (car (cdr (cdr (car '((1 2 3 4 5) 6 7 8 9)))))
3


With that knowledge, all you need to remember is that car accesses the first element of a list (car = content of the address register) and cdr removes the first element of a list returning the tail of the list (cdr = content of the decrement register):

_$ (car '(1 2 3 4 5 6 7 8 9))
1
_$ (cdr '(1 2 3 4 5 6 7 8 9))
(2 3 4 5 6 7 8 9)


And so when you see an expression such as:

(cddadr '(1 (2 3 4 5) 6 7 8 9))

You can extract the list item by reading the sequence of 'a's and 'd's from right-to-left.

In the above example, reading from right to left, we have the sequence:

dadd

Which means:
  • Remove the first element and return the remainder of the list
  • Extract the first element of the list
  • Remove the next two elements and return the remainder of the list
Evaluating this in sequence yields:

(cddadr '(1 (2 3 4 5) 6 7 8 9))
(cddar '((2 3 4 5) 6 7 8 9))
(cddr '(2 3 4 5))
(cdr '(3 4 5))
(4 5)


Which is indeed exactly what is returned when testing the expression at the console:

_$ (cddadr '(1 (2 3 4 5) 6 7 8 9))
(4 5)

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Xlist Block Name to MLeader Text
« Reply #18 on: March 31, 2021, 12:02:36 PM »
Nice explanation Lee!  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

tombu

  • Bull Frog
  • Posts: 288
  • ByLayer=>Not0
Re: Xlist Block Name to MLeader Text
« Reply #19 on: March 31, 2021, 03:30:34 PM »
Haven't used xrefs in years but if for the last "(assoc 3 something)" would (assoc 3 (reverse e)) work the same no matter how deep it was nested?
Tom Beauford P.S.M.
Leon County FL Public Works - Windows 7 64 bit AutoCAD Civil 3D

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Xlist Block Name to MLeader Text
« Reply #20 on: March 31, 2021, 03:56:58 PM »
Haven't used xrefs in years but if for the last "(assoc 3 something)" would (assoc 3 (reverse e)) work the same no matter how deep it was nested?

You have to check if there are 4 items in the list to make sure a nested object was picked. This is a little better than just using (last e):
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ stripxrefprefix a e r)
  2.   (defun stripxrefprefix (string / position)
  3.     (if (setq position (vl-string-position 124 string 0 t))
  4.       (substr string (+ 2 position))
  5.       string
  6.     )
  7.   )
  8.   (if (setq e (nentsel "\nPick a block: "))
  9.     (progn (setq r "")
  10.            (foreach b (append (list (car e))
  11.                               (if (cadddr e)
  12.                                 (last e)
  13.                               )
  14.                       )
  15.              (and (setq a (cdr (assoc 2 (entget b)))) (setq r (strcat (stripxrefprefix a) "\\\P" r)))
  16.            )
  17.            (or (= "" r) (vl-cmdf "_.Mleader" (cadr e) "\\" r))
  18.     )
  19.   )
  20.   (princ)
  21. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Xlist Block Name to MLeader Text
« Reply #21 on: March 31, 2021, 04:24:57 PM »
20210401 Edit: for ronionp
Haven't used xrefs in years but if for the last "(assoc 3 something)" would (assoc 3 (reverse e)) work the same no matter how deep it was nested?
You have to check if there are 4 items in the list to make sure a nested object was picked. This is a little better than just using (last e):
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ stripxrefprefix a e r)
  2.   (defun stripxrefprefix (string / position)
  3.     (if   (setq position (vl-string-position 124 string 0 t))
  4.       (substr string (+ 2 position))
  5.       string
  6.     )
  7.   )
  8.   (if (setq e (nentsel "\nPick a block: "))
  9.     (progn (setq r "")
  10.       (foreach b (append (list (car e))
  11.                (if (cadddr e)
  12.             (last e)
  13.                )
  14.             )
  15.         (and (setq a (cdr (assoc 2 (entget b)))) (setq r (strcat (stripxrefprefix a) "\\\P" r)))
  16.       )
  17.       (or (= "" r) (vl-cmdf "_.Mleader" (cadr e) "\\" r))
  18.     )
  19.   )
  20.   (princ)
  21. )

I don't know if my question is relevant: if you select a dimension inside the block or an attribute of the block?


Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Xlist Block Name to MLeader Text
« Reply #22 on: April 01, 2021, 07:47:34 AM »
Nice explanation Lee!  :-)

Thanks Ron!  :-)

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Re: Xlist Block Name to MLeader Text
« Reply #23 on: April 02, 2021, 09:32:20 AM »
Ron, Lee, thank you for sharing more to this! Great information!
Civil3D 2020