Author Topic: Detached Leader Question  (Read 4315 times)

0 Members and 1 Guest are viewing this topic.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Detached Leader Question
« on: July 02, 2004, 07:08:36 PM »
I have run across a LEADER with a 340 code with this entity name.
It appears to be a leader that has had its text deleted.
How do I test for this condition?

_1$
; error: Invalid Entity name <Entity name: 0>

I found this but looking for more.
Quote

An interesting bug, or gotcha, popped up in a routine dealing with
leaders. The routine is designed to change the dimscale, and associated
text of a leader, to the current dimscale.  To do this, I need, among
other steps, to get the associated text and scale it. However, the 340
group contains an invalid entity name (<Entity name: 0>) which when fed to
the scale command will crash autocad with a fatal error.

A workaround is to check to see if (entget ename) returns nil and if it
does, call (entdel ename) and check its return. If it returns nil, the
ename is invalid. The second test is neccessary because entget can return
nil if the entity is deleted.


((-1 . <Entity name: 2131560>)
 (0 . "LEADER") (5 . "3164")
 (100 . "AcDbEntity") (67 . 0) (8 . "0") (62 . 254)
 (100 . "AcDbLeader") (3 . "WESTEC") (71 . 1) (72 . 0) (73 . 0) (74 . 0)
 (75 . 0) (40 . 2.5)
                      (41 . 17.0536) (76 . 3) (10 1369.95 818.169 0.0)
                      (10 1350.36 811.77 0.0) (10 1347.28 811.77 0.0)
 (77 . 256)
                      (340 . <Entity name: 0>) (211 1.0 0.0 0.0)
 (210 0.0 0.0 1.0)
                      (212 0.0 0.0 0.0) (213 0.0 0.0 0.0)
)

I hope this information is of use to you.
Sincerely,
Michelle
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Detached Leader Question
« Reply #1 on: July 02, 2004, 08:07:24 PM »
Well, you could do a check of the group 340 data, for example you know that 14 characters of the value are as follows "<Entity name: " and you know that the last character is ">" What remains should be a hex number. So, using the HEX incrementing proggie posted elsewhere (I am too lazy to search for it at the moment) pass the remaining values to it, finally with a tiny bit of coding you SHOULD be able to return a real number and if that real number is equal to 0 or does not exist for whatever reason, you know the object is invalid.
Probably not what you were looking for but it SHOULD work without problem
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Detached Leader Question
« Reply #2 on: July 02, 2004, 10:11:36 PM »
<untested>

Perhaps ...

(if (eq 'ename (type (cdr (assoc 340 TheData))))
    ;; rock n roll
)

Either that or trap an entget call using the bogus ename with a vl-catch-all-apply construct.

DOH -- this is on the test server, but I'll post it anyway.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Detached Leader Question
« Reply #3 on: July 02, 2004, 10:20:04 PM »
Had a test of that and no go .....

CAB, you might want to grab this if'n you see it in time...

Code: [Select]

(entget (cdr (assoc 340(entget (car (entsel))))))


Returns nil if no attached text and the text list if attached
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Detached Leader Question
« Reply #4 on: July 02, 2004, 10:26:37 PM »
<untested>

Perhaps ...

Code: [Select]

(defun myEntGet ( ename / result )
    (if (eq 'ename (type ename))
        (vl-catch-all-apply
           '(lambda () (setq result (entget ename)))
        )
    )
    result
)


Test the result for null and branch accordingly.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Detached Leader Question
« Reply #5 on: July 03, 2004, 12:15:44 AM »
Thanks guys, both worked.
I think I had a car when I needed a cdr & it was driving me nuts.

Here is the snippet


Code: [Select]
       ((= (cdr (assoc 0 entbl)) "LEADER") ; found a leader
         (setvar 'clayer (cdr (assoc 8 entbl)))
         ;;  Set Dim Style Current
         (setq d:styold (getvar "dimstyle"))
         (command "-dimstyle" "restore" (cdr (assoc 3 entbl)))
         (setq d:stynew (getvar "dimstyle"))
         ;;  Look for text to set Text Style Current
         (foreach x entbl
           (if (= (car x) 340) ; 330 for text pointer to leader
             (if (and (setq e (entget (cdr x))) ; valid ent ??
                   (wcmatch (cdr (assoc 0 e)) "*TEXT*")
                 )
               (progn
                 (setq t:stynew (cdr (assoc 7 (entget (cdr x))))
                       t:styold (getvar "textstyle")
                 )
                 (setvar "TextSize" (cdr (assoc 40 (entget (cdr x)))))
                 (setvar "TextStyle" t:stynew)
               )
             )
           )
         )
        )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Detached Leader Question
« Reply #6 on: July 03, 2004, 12:41:36 AM »
Quote from: CAB
Thanks guys, both worked.
I think I had a car when I needed a cdr & it was driving me nuts.

Here is the snippet


Code: [Select]
       ((= (cdr (assoc 0 entbl)) "LEADER") ; found a leader
         (setvar 'clayer (cdr (assoc 8 entbl)))
         ;;  Set Dim Style Current
         (setq d:styold (getvar "dimstyle"))
         (command "-dimstyle" "restore" (cdr (assoc 3 entbl)))
         (setq d:stynew (getvar "dimstyle"))
         ;;  Look for text to set Text Style Current
         (foreach x entbl
           (if (= (car x) 340) ; 330 for text pointer to leader
             (if (and (setq e (entget (cdr x))) ; valid ent ??
                   (wcmatch (cdr (assoc 0 e)) "*TEXT*")
                 )
               (progn
                 (setq t:stynew (cdr (assoc 7 (entget (cdr x))))
                       t:styold (getvar "textstyle")
                 )
                 (setvar "TextSize" (cdr (assoc 40 (entget (cdr x)))))
                 (setvar "TextStyle" t:stynew)
               )
             )
           )
         )
        )

HI Charles. Question -- does the 340 group ever appear more than once? (I'm doubting so). If that's the case the foreach is over processing.

Consider this as a alternate to the foreach loop:

Code: [Select]

        (cond
            (   (and
                    (setq ename (cdr (assoc 340 entbl)))
                    (setq data (entget ename))
                    (wcmatch (cdr (assoc 0 data)) "MTEXT,TEXT")
                )
                (setq
                    t:stynew (cdr (assoc 7 data))
                    t:styold (getvar "textstyle")
                )
                (setvar "TextSize" (cdr (assoc 40 data)))
                (setvar "TextStyle" t:stynew)
            )
        )


I coded it blind but it should work.

<Sorry for buttin' in, can't help myself>

These messages will be lost when Mark switches ther server back over!
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Detached Leader Question
« Reply #7 on: July 03, 2004, 08:43:05 AM »
You would know better that me. I started form the other end so to speak.

The text/Mtext uses 330 to point to the leader & it can have more that one 330
code, right? Making sure here, you guy's have to keep me honest. After just over
a year with this acad lisp stuff there is a lot I don't know. So don't hesitate
to point me back to the path.

That said I have a similar code segment to iterate through the text/mtext to test
the 330 code for a link back to the LEADER. What is your favorite way to step through
an entity list? I tried to figure a way to use MEMBER so I could speed things up
although not really a speed issue but I try to keep that in mind when coding. But
MEMBER only works if you know the exact name of the item. So I fell back to FOREACH.
I guess WHILE could be made faster by leaving to loop when you get a hit.


 I will save the thread & restore to the main server if you like.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Detached Leader Question
« Reply #8 on: July 03, 2004, 12:58:14 PM »
Hi CAB -- It appears to me, based on nominal experimentation, that a LEADER has one hard pointer (340 group) to an associated TEXT entity and that said TEXT entity has a soft pointer (330 group) back to the LEADER. However, as you noted, there will be more than one 330 group present in the TEXT entity's data, as each entity has a soft pointer (330 group) to it's owner, e.g. MODELSPACE. The upshot being that using ASSOC on the TEXT entity's data will not work per the previous snip I posted for the LEADER's entity data. Fascinating Michael, truly rivoting.

Having said that, is there anything you need beyond this (assuming all you're doing is setting the current TEXTSTYLE and TEXTSIZE from a LEADER)?

Code: [Select]
;;  If the specified leader entity has associated text, save the existing
;;  textsize and textstyle, then set them according to the properties of
;;  the associated text, then return the original properties to the caller
;;  so that if need be, they can be restored later.

(defun SetTextStyleFromLeader ( LeaderEname / LeaderData TextEname TextData PreviousProperties )
    (if (eq "LEADER" (cdr (assoc 0 (setq LeaderData (entget LeaderEname)))))
        (if (setq TextEname (cdr (assoc 340 LeaderData)))
            (cond
                (   (setq TextData (entget TextEname))
                    (setq PreviousProperties
                        (mapcar
                           '(lambda (varname) (cons varname (getvar varname)))
                           '("TextStyle" "TextSize")
                        )
                    )
                    (foreach pair '(("TextStyle" . 7)("TextSize" . 40))
                        (setvar (car pair) (cdr (assoc (cdr pair) TextData)))
                    )
                    ;; return the previous properties to the caller
                    PreviousProperties
                )
            )    
        )
    )  
)


Example:
(SetTextStyleFromLeader ValidLeaderEname)  ==> (("TextSize" . 0.18) ("TextStyle" . "Standard"))

Without too much effort this function could be made generic so that if it is at all possible to set the TEXTSIZE and TEXTSTYLE from the ename passed to the function it would, e.g. the passed ename corresponds to a LEADER, A DIMENSION, TEXT, MTEXT ... I'll let you code that one. :)

Cheers,

MP
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Detached Leader Question
« Reply #9 on: July 03, 2004, 03:50:41 PM »
Thanks for the info & code, never boring to me. :)
Very interested in your code, It will have to wait till tomorrow.
Off to dinner.

Here is what I have so far:

Code: [Select]
;;;====================================================================;
;;;                     SetCurrent.lsp                                 ;
;;;                   Charles Alan Butler                              ;
;;;                 ab2draft@TampaBay.rr.com                           ;
;;;                     @ Copyright 2004                               ;
;;;                  Original routine 2003                             ;
;;;====================================================================;
;;  
;;  Revision 06/17/04
;;  Revision 06/28/04
;;    Added Leader detection & layer change for any object selected.
;;  Revision 07/03/04
;;    Added cross check for leader to text & text to leader
;;  
;;  Routine to set current Layer, Text Style and/or Dim Style by
;;  picking an existing object in the drawing  
;;  
;;  OBJECT SELECTED         Set Current
;;  TEXT, MTEXT or Rtext    Layer, Text Style
;;  Dimension               Layer, Dim Style
;;  Leader                  Layer, Dim Style
;;  Leader w/text           Layer, Dim & Text Style
;;  Text, mtext w/Leader    Layer, Dim & Text Style
;;  Any other object        Layer
;;  
;;  Enter tds from the command line to run
;;  or set up a menu button with ^C^Ctds
;;  
;;;====================================================================;
;;;  THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED      ;
;;;  WARRANTY.  ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR   ;
;;;  PURPOSE AND OF MERCHANTABILITY ARE HEREBY DISCLAIMED.             ;
;;;====================================================================;
;;;  Copyright 2004  by Charles Alan Butler. All Rights Reserved.      ;
;;;                                                                    ;
;;;  You are hereby granted permission to use, copy and modify this    ;
;;;  software without charge, provided you do so exclusively for       ;
;;;  your own use or for use by others in your organization in the     ;
;;;  performance of their normal duties, and provided further that     ;
;;;  the above copyright notice appears in all copies and both that    ;
;;;  copyright notice and the limited warranty and restricted rights   ;
;;;  notice below appear in all supporting documentation.              ;
;;;                                                                    ;
;;;  Incorporation of any part of this software into other software,   ;
;;;  except when such incorporation is exclusively for your own use    ;
;;;  or for use by others in your organization in the performance of   ;
;;;  their normal duties, is prohibited without the prior written      ;
;;;  consent of Charles Alan Butler, 1403 Duelda Drive,                ;
;;;  Brandon Florida, 33511                                            ;
;;;                                                                    ;
;;;  Copying, modification and distribution of this software or any    ;
;;;  part thereof in any form except as expressly provided herein is   ;
;;;  prohibited without the prior written consent of Charles Alan      ;
;;;  Butler, 1403 Duelda Drive, Brandon Florida, 33511                 ;
;;;                                                                    ;
;;;====================================================================;
(defun c:tds (/ ent entbl t:styold t:stynew d:styold d:stynew usercmd)
  (if (setq ent
             (car (entsel "\nSelect Dimension or Text to make current: "))
      )
    (progn
      (setq entbl (entget ent)) ; Get entity definition list
      (setq usercmd (getvar "CMDECHO"))
      (setvar "CMDECHO" 0)
      (command "undo" "begin")
      (cond
        ;; ============================================================
        ((= (cdr (assoc 0 entbl)) "LEADER") ; found a leader
         (setvar 'clayer (cdr (assoc 8 entbl)))
         ;;  Set Dim Style Current
         (setq d:styold (getvar "dimstyle"))
         (command "-dimstyle" "restore" (cdr (assoc 3 entbl)))
         (setq d:stynew (getvar "dimstyle"))
         (cond
           ((and ; chek to see if text is attched
              (setq elst (entget(cdr (assoc 340 entbl))))
              (wcmatch (cdr (assoc 0 elst)) "*TEXT*")
            )
              (setq  t:stynew (cdr (assoc 7 elst))
                     t:styold (getvar "textstyle")
              )
              (setvar "TextSize" (cdr (assoc 40 elst)))
              (setvar "TextStyle" t:stynew)
          )
        )
        ); end cond 1

        ;; ===========================================================
        ((wcmatch (cdr (assoc 0 entbl)) "*TEXT*") ; gets Rtext as well
         (setq t:stynew (cdr (assoc 7 entbl))
               t:styold (getvar "textstyle")
         )
         (setvar "TextSize" (cdr (assoc 40 entbl)))
         (setvar "TextStyle" t:stynew)
         (setvar 'clayer (cdr (assoc 8 entbl)))
         ;;  Look for leader to set dim Style Current
         (foreach x entbl
           (if (= (car x) 330) ; pointer to leader
             (if (and (setq e (entget (cdr x))) ; valid ent ??
                   (= (cdr (assoc 0 e)) "LEADER"))
               (progn
                 (setq d:styold (getvar "dimstyle"))
                 (command "-dimstyle" "restore" (cdr (assoc 3 entbl)))
                 (setq d:stynew (getvar "dimstyle"))
               )
             )
           )
         )
        ) ; end cond 2

        ;; =============================================================
        ((= (cdr (assoc 0 entbl)) "DIMENSION")
         (setq d:styold (getvar "dimstyle"))
         (command "-dimstyle" "restore" (cdr (assoc 3 entbl)))
         (setq d:stynew (getvar "dimstyle"))
         (command "'Layer" "M" (cdr (assoc 8 entbl)) "")
        ) ; end cond 3

        ;; =============================================================
        (t ; catch any other object
         (if (cdr (assoc 8 entbl))
           (setvar 'clayer (cdr (assoc 8 entbl)))
         )
         (prompt "\n*-* Object is not Text or Dimension.")
        ) ; end cond (T)

      ) ; end Cond stmt
      ;;  ***************  Display Changes Made  *******************
      (if (or d:styold t:styold)
        (prompt (strcat "\n*-* Object selected: " (cdr (assoc 0 entbl))))
      )
      (prompt (strcat "\n*-* Layer changed to: " (cdr (assoc 8 entbl))))
      (if d:styold
        (prompt
          (strcat "\n*-* Dimension style changed: "
                  d:styold                " to "
                  d:stynew                "."
                 )
        )
      )
      (if t:styold
        (prompt
          (strcat "\n*-* Text style changed: " t:styold " to " t:stynew ".")
        )
      )
      (command "undo" "end")
      (setvar "CMDECHO" usercmd)
    ) ; end progn
  ) ; endif
  (princ)
) ;End of Defun
(prompt "\nText / Dimension Style Changer Loaded, Type TDS to run")
(princ)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Detached Leader Question
« Reply #10 on: July 03, 2004, 11:55:01 PM »
Just a heads up.... There CAN be more than one leader attached to a single mtext object. BUT there can only be one mtext object per leader.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Detached Leader Question
« Reply #11 on: July 04, 2004, 01:07:51 AM »
Quote from: Keith
Just a heads up.... There CAN be more than one leader attached to a single mtext object. BUT there can only be one mtext object per leader.

That is a good heads up, I hadn't considred that, duh. Thanks Keith.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Detached Leader Question
« Reply #12 on: July 04, 2004, 01:10:30 AM »
No problem .. just thought I would give that tidbit of info just incase you (or CAB) didn't know .. in my office we frequently use multiple leaders attached to a single mtext item to identify multiple instances of an object that must be annotated.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Detached Leader Question
« Reply #13 on: July 04, 2004, 11:38:33 AM »
Thanks Keith, I did not know that.

In that case it would make more sense to use the WHILE when linking from LEADER
to TEXT & exit the loop at the first TEXT found.

Modifying code.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Detached Leader Question
« Reply #14 on: July 04, 2004, 01:21:24 PM »
Ok updated this section:
Code: [Select]
       ;; ===========================================================
        ((wcmatch (cdr (assoc 0 entbl)) "*TEXT*") ; gets Rtext as well
         (setq t:stynew (cdr (assoc 7 entbl))
               t:styold (getvar "textstyle")
         )
         (setvar "TextSize" (cdr (assoc 40 entbl)))
         (setvar "TextStyle" t:stynew)
         (setvar 'clayer (cdr (assoc 8 entbl)))
         ;;  Look for leader to set dim Style Current
         (setq idx (1+ (length entbl)))
         (while (> (setq idx (1- idx))-1)
           (setq x (nth idx entbl))
           (if (and (= (car x) 330) ; pointer to leader
                    (setq e (entget (cdr x))) ; valid ent ??
                    (= (cdr (assoc 0 e)) "LEADER"))
             (progn
               (setq d:styold (getvar "dimstyle"))
               (command "-dimstyle" "restore" (cdr (assoc 3 entbl)))
               (setq d:stynew (getvar "dimstyle")
                     idx 0)
             ); progn
           ); endif
         ); while
        ) ; end cond 2
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.