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

0 Members and 1 Guest are viewing this topic.

MSTG007

  • Gator
  • Posts: 2507
  • I can't remeber what I already asked! I need help!
Xlist Block Name to MLeader Text
« on: March 25, 2021, 01:12:00 PM »
I am sure this has been asked here before. I am not having any luck finding it.

Is there a way I can take the xlist Block Name (only) into a MLeader?

I have seen similar routines which take the block name from the current dwg into MLeader, but not from within a xref.


The other question i would have is if, a block is buried 4 levels down. (1.Xref, 2.Block, 3.Inside Block2. 4.Inside Block3).

Could it still get the block name?

Thanks for any help on this!

Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7308
Re: Xlist Block Name to MLeader Text
« Reply #1 on: March 25, 2021, 03:25:13 PM »
Use NENTSEL like so:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ a e)
  2.   (if (setq e (nentsel "\nPick a block: "))
  3.     (foreach b (append (list (car e)) (last e))
  4.       (if (setq a (cdr (assoc 2 (entget b))))
  5.         (print a)
  6.       )
  7.     )
  8.   )
  9.   (princ)
  10. )

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

MSTG007

  • Gator
  • Posts: 2507
  • I can't remeber what I already asked! I need help!
Re: Xlist Block Name to MLeader Text
« Reply #2 on: March 25, 2021, 04:35:44 PM »
Thanks for the hint down the road.

I was able to get the mleader in it. It now shows with the following:
MLeader Text Shows (xUtility|site - transformer_1) <--- Anyway to remove the "|" and the prior xUtility?)

Code: [Select]
(defun c:foo1 (/ a e)
  (if (setq e (nentsel "\nPick a block: "))
    (foreach b (append (list (car e)) (last e))
      (if (setq a (cdr (assoc 2 (entget b))))
       (vl-cmdf "_.Mleader" "\\" "\\" a) ;;Added MLeader
;;MLeader Text Shows (xUtility|site - transformer_1)<--- Anyway to remove the "|" and the prior xUtility?)
      )
    )
  )
  (princ)
)

thank you again.
Civil3D 2020

Tharwat

  • Swamp Rat
  • Posts: 613
  • Hypersensitive
Re: Xlist Block Name to MLeader Text
« Reply #3 on: March 25, 2021, 06:17:26 PM »
You can explore your second thread in CADTutor for more info about parsing a string in Mleader object.  :grinwink:

https://www.cadtutor.net/forum/topic/72594-removing-text-in-front-of-a-dash-mtext/?tab=comments#comment-578797

ronjonp

  • Needs a day job
  • Posts: 7308
Re: Xlist Block Name to MLeader Text
« Reply #4 on: March 25, 2021, 06:26:05 PM »
MP to the rescue again ( he helped me with the same problem 15 years ago ) ...  use stripxrefprefix from here: http://www.theswamp.org/index.php?topic=9293.msg119755#msg119755

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

MSTG007

  • Gator
  • Posts: 2507
  • I can't remeber what I already asked! I need help!
Re: Xlist Block Name to MLeader Text
« Reply #5 on: March 26, 2021, 08:53:51 AM »
Thank you Ron for saving the day. Apparently i am 15 years to late. Anyways, the function makes sense. I am trying to figure out how to add the "Select Previous" command within the function of the routine. I am hoping that once the Mleader is created with the block values in it, i could activate the function to remove the prefix stuff inside it.

Code: [Select]
(defun StripXrefPrefix ( string / position )
    (if (setq position (vl-string-position 124 string 0 t))
        (substr string (+ 2 position))
        string
    )
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun c:foo2 (/ a e)
  (if (setq e (nentsel "\nPick a block: "))
    (foreach b (append (list (car e)) (last e))
      (if (setq a (cdr (assoc 2 (entget b))))
       (vl-cmdf "_.Mleader" "\\" "\\" a) ;;Added MLeader
 ;;      (StripString "123|456|789" "|");; Example
       (StripString "previous" "|");;MLeader Text Shows (xUtility|site - transformer_1)<--- Anyway to remove the "|" and the prior xUtility?)
      )
    )
  )
  (princ)
)
Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7308
Re: Xlist Block Name to MLeader Text
« Reply #6 on: March 26, 2021, 04:05:30 PM »
The code I posted retrieves all the nested blocks. Do you want all this info in the mleader?

Try this .. lists blocks from the top down:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo2 (/ 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)) (last e))
  11.              (and (setq a (cdr (assoc 2 (entget b)))) (setq r (strcat (stripxrefprefix a) "\\\P" r)))
  12.            )
  13.            (vl-cmdf "_.Mleader" (cadr e) "\\" r)
  14.     )
  15.   )
  16.   (princ)
  17. )
« Last Edit: March 26, 2021, 04:19:39 PM by ronjonp »

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

MSTG007

  • Gator
  • Posts: 2507
  • I can't remeber what I already asked! I need help!
Re: Xlist Block Name to MLeader Text
« Reply #7 on: March 29, 2021, 07:22:18 AM »
I see where your coming from with what you have posted. Within what you have shown, I am still trying to remove the "XREF|" portion of the information that list from the block down. The Block Name is what I am trying to show within the mleader text.

The last post i had, it was kind of a two step process that i tried to force but failed making it smooth lol.
Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7308
Re: Xlist Block Name to MLeader Text
« Reply #8 on: March 29, 2021, 11:24:04 AM »
I see where your coming from with what you have posted. Within what you have shown, I am still trying to remove the "XREF|" portion of the information that list from the block down. The Block Name is what I am trying to show within the mleader text.

The last post i had, it was kind of a two step process that i tried to force but failed making it smooth lol.
The example above will remove the 'XREF|' part of the names ... did you try it?

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

MSTG007

  • Gator
  • Posts: 2507
  • I can't remeber what I already asked! I need help!
Re: Xlist Block Name to MLeader Text
« Reply #9 on: March 29, 2021, 12:51:46 PM »
Sure did. foo2. It seems it removed the "|".
Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7308
Re: Xlist Block Name to MLeader Text
« Reply #10 on: March 29, 2021, 05:54:54 PM »
So are you good?

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

MSTG007

  • Gator
  • Posts: 2507
  • I can't remeber what I already asked! I need help!
Re: Xlist Block Name to MLeader Text
« Reply #11 on: March 30, 2021, 11:33:02 AM »
Within the code, could i get to strip out the xref name and just the block name?

Code: [Select]
(and (setq a (cdr (assoc 2 (entget b)))) (setq r (strcat (stripxrefprefix a) "\\\P" r)))
I thought initially it was the "|" character that was supposed to find and remove.

In the snapshot above, if it could remove the "xUtility" portion, i think it would be fine!

Just a thought, i have not tried this yet, but it should return block names for dynamic blocks too?

Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7308
Re: Xlist Block Name to MLeader Text
« Reply #12 on: March 30, 2021, 12:31:09 PM »
Within the code, could i get to strip out the xref name and just the block name?

Code: [Select]
(and (setq a (cdr (assoc 2 (entget b)))) (setq r (strcat (stripxrefprefix a) "\\\P" r)))
I thought initially it was the "|" character that was supposed to find and remove.

In the snapshot above, if it could remove the "xUtility" portion, i think it would be fine!

Just a thought, i have not tried this yet, but it should return block names for dynamic blocks too?
Maybe this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo2 (/ 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 (cadddr (setq e (nentsel "\nPick a block: ")))
  9.     (progn
  10.       (setq r (stripxrefprefix (vla-get-effectivename (vlax-ename->vla-object (car (cadddr e))))))
  11.       (vl-cmdf "_.Mleader" (cadr e) "\\" r)
  12.     )
  13.   )
  14.   (princ)
  15. )

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

MSTG007

  • Gator
  • Posts: 2507
  • I can't remeber what I already asked! I need help!
Re: Xlist Block Name to MLeader Text
« Reply #13 on: March 30, 2021, 01:28:29 PM »
Boom baby! you hit it....

Could I ask why was it changed to this?

Code: [Select]
(setq r (stripxrefprefix (vla-get-effectivename (vlax-ename->vla-object (car (cadddr e))))))
I dont remember seeing anything like this "cadddr e".
Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7308
Re: Xlist Block Name to MLeader Text
« Reply #14 on: March 30, 2021, 03:05:59 PM »
Boom baby! you hit it....

Could I ask why was it changed to this?

Code: [Select]
(setq r (stripxrefprefix (vla-get-effectivename (vlax-ename->vla-object (car (cadddr e))))))
I dont remember seeing anything like this "cadddr e".
CADDDR looks for the 4th item in a list. If a block is selected with nentsel, the container of the item is within that 4th entry. There could also be multiple blocks within which was why I had the foreach function before.

Use this to investigate: (mapcar 'print (nentsel "\nPick a block: "))

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

BIGAL

  • Swamp Rat
  • Posts: 780
  • 30 + 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: 2507
  • 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: 12503
  • 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: 7308
Re: Xlist Block Name to MLeader Text
« Reply #18 on: March 31, 2021, 12:02:36 PM »
Nice explanation Lee!  :-)

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

tombu

  • Bull Frog
  • Posts: 241
  • 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: 7308
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 10 x64 - AutoCAD /C3D 2020

Custom Build PC

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1199
  • 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: 12503
  • 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: 2507
  • 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