Author Topic: Count number of Texts in block  (Read 2699 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Count number of Texts in block
« on: May 07, 2012, 09:05:24 AM »
hello .

I am trying to get the number of texts in my block , but I am facing a problem with my code .
Can you please help me ? ( but not with VLA or VLAX functions please )
Code: [Select]
(defun c:count (/ n bl enty eget)
  (setq n 0)
  (if (setq bl (car (entsel "\n Select block please :")))
    (progn
      (setq enty (entnext bl))
      (while (/= (cdr (assoc 0 (setq eget (entget enty)))) "SEQEND" )
         (if (eq (cdr (assoc 0 eget)) "TEXT")
           (setq n (1+ n)))
         (setq enty (entnext enty)) )
      (print (itoa n))) )
  (princ))

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Count number of Texts in block
« Reply #1 on: May 07, 2012, 09:30:56 AM »
The entnext gives you the next entity following the specified ename. If you pick a block in a drawing, that ename refers to that instance (also known as INSERT or Reference) of the block (not the block itself - known as the Block DEFINITION). It's like a placeholder stating this block is here at this scale and rotated such - it does not contain anything about what's inside the block.

The SEQEND is only found after blocks containing AttDef entities (if we're only talking about blocks that is). Following an INSERT entity you might find ATTRIB entities (one for each attribute) and ended with a SEQEND. While these "are" text, they're a special case - modifyable per INSERT individually (no matter how many copies of that same block there is).

For TEXT inside a block, these are only stored in the block definition. And you can only get at the block definition through the BLOCK table.

So, after you've picked the INSERT entity. Entget it to see what its BlockName is (DXF code2). Then use tblsearch to extract the block definition with that name. This will then have a DXF code -2 which links to the first ename inside the block. From there you can step through all until entnext returns nil.

Hope that's little enough VLA/VLAX  ;)
« Last Edit: May 07, 2012, 09:36:34 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pBe

  • Bull Frog
  • Posts: 402
Re: Count number of Texts in block
« Reply #2 on: May 07, 2012, 09:47:22 AM »
Sample

Code - Auto/Visual Lisp: [Select]
  1. (Defun c:TExt? ( / cnt data)
  2.       (setq cnt 0 O (car (entsel)))
  3.       (setq data (tblobjname "BLOCK" (cdr (assoc 2 (entget o)))))
  4.                 (while (setq data (entnext data))
  5.                                 (if (eq (cdr (assoc 0 (entget data))) "TEXT")
  6.                                         (setq cnt (1+ cnt))))
  7. cnt)
« Last Edit: May 07, 2012, 11:13:26 PM by pBe »

kruuger

  • Swamp Rat
  • Posts: 637
Re: Count number of Texts in block
« Reply #3 on: May 07, 2012, 10:01:52 AM »
see also this one:
Code - Auto/Visual Lisp: [Select]
  1. ; =========================================================================================== ;
  2. ; Lista obiektow w definicji bloku / List of objects in block definition                      ;
  3. ;  Name   [STR] - nazwa bloku / block name                                                    ;
  4. ;  Entity [STR] - nazwa entycji / entity name                                                 ;
  5. ; ------------------------------------------------------------------------------------------- ;
  6. ; (cd:BLK_GetEntity "*Model_space" nil), (cd:BLK_GetEntity "NAZWA" "*LINE")                   ;
  7. ; =========================================================================================== ;
  8. (defun cd:BLK_GetEntity (Name Entity / en dt res)
  9.   (setq en (tblobjname "BLOCK" Name))
  10.   (while
  11.     (and
  12.       en
  13.       (setq en (entnext en))
  14.       (setq dt (entget en))
  15.       (/= "ENDBLK" (cdr (assoc 0 dt)))
  16.     )
  17.     (if
  18.       (if Entity
  19.         (wcmatch (cdr (assoc 0 dt)) (strcase Entity))
  20.         (cdr (assoc 0 dt))
  21.       )
  22.       (setq res
  23.         (cons
  24.           (cdr (assoc -1 dt))
  25.           res
  26.         )
  27.       )
  28.     )
  29.   )
  30.   (reverse res)
  31. )

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Count number of Texts in block
« Reply #4 on: May 07, 2012, 10:28:31 AM »
Maybe:

Code: [Select]
;;;ARG -> Block_Name Entity_Types
;;;RET -> 'INT - Number Of Entities Found
(defun blketqty (b e / l f)
  (setq f (cdr (assoc -2 (tblsearch "BLOCK" b))))
  (while f
    (if (wcmatch (cdr (assoc 0 (entget f))) (strcase e))
        (setq l (cons e l)))
    (setq f (entnext f)))
(length l))

Usage:

Code: [Select]
  (setq b "LEG6"
        e "ARC,CIRCLE")
  (prin1 (blketqty b e))

-David
« Last Edit: May 07, 2012, 10:32:01 AM by David Bethel »
R12 Dos - A2K

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Count number of Texts in block
« Reply #5 on: May 07, 2012, 10:58:25 AM »
Sample

Code - Auto/Visual Lisp: [Select]
  1. (Defun c:TExt? ( / cnt data)
  2.       (setq cnt 0 O (car (entsel)))
  3.       (setq data (tblobjname "BLOCK" (cdr (assoc 2 (entget o)))))
  4.       (while (setq data (entnext data))
  5.                             (if (eq (cdr (assoc 0 (entget data))) "TEXT")
  6.                                        (setq cnt (1+ cnt))))
  7. cnt)
Just a small typo: shouldn't there be a / before the localized variables?
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pBe

  • Bull Frog
  • Posts: 402
Re: Count number of Texts in block
« Reply #6 on: May 07, 2012, 11:06:07 AM »
Sample

Code - Auto/Visual Lisp: [Select]
  1. (Defun c:TExt? ( / cnt data)
  2.       (setq cnt 0 O (car (entsel)))
  3.       (setq data (tblobjname "BLOCK" (cdr (assoc 2 (entget o)))))
  4.       (while (setq data (entnext data))
  5.                             (if (eq (cdr (assoc 0 (entget data))) "TEXT")
  6.                                        (setq cnt (1+ cnt))))
  7. cnt)
Just a small typo: shouldn't there be a / before the localized variables?

Oops..  :laugh:
You're right... you're right...

Good catch irneb
Thank you for that.

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: Count number of Texts in block
« Reply #7 on: May 07, 2012, 11:16:28 AM »
Some obfuscated and pointless fun:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( )
  2.     (   (lambda ( a b c ) (if a (b c (tblobjname "BLOCK" (cdr (assoc 2 (entget (ssname a 0))))))))
  3.         (ssget "_+.:E:S" '((0 . "INSERT")))
  4.         (lambda ( a b ) (a b 0))
  5.         (lambda ( b c )
  6.             (if (setq b (entnext b))
  7.                 (if (eq "TEXT" (cdr (assoc 0 (entget b))))
  8.                     (a b (1+ c))
  9.                     (a b c)
  10.                 )
  11.                 c
  12.             )
  13.         )
  14.     )
  15. )

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Count number of Texts in block
« Reply #8 on: May 07, 2012, 11:25:06 AM »
Some obfuscated and pointless fun:
:lmao: Great code Lee! Even though it's a bit forced  ;) , it could be used as a very informative example of using lambda!
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: Count number of Texts in block
« Reply #9 on: May 07, 2012, 05:32:42 PM »
Some obfuscated and pointless fun:
:lmao: Great code Lee! Even though it's a bit forced  ;) , it could be used as a very informative example of using lambda!

Cheers Irné  :-)

Coder

  • Swamp Rat
  • Posts: 827
Re: Count number of Texts in block
« Reply #10 on: May 08, 2012, 12:23:08 AM »
Thank you all , that was very great work .  :-)

Thank you irneb for your nice explanations  , it is very helpful  :wink:

Many thanks

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Count number of Texts in block
« Reply #11 on: May 08, 2012, 03:10:12 AM »
Cheers Irné  :)
You're welcome! Nice to see someone's actually gotten my name spelled correctly  :pissed: :lmao:

Thank you irneb for your nice explanations  , it is very helpful  ;)
Glad you found it useful, you're more than welcome. Though I just gave a "pseudo code" in essay form  ;)
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.