Another Text Count Problem

Discussion in 'AutoCAD' started by sdpurkey, Feb 4, 2005.

  1. sdpurkey

    sdpurkey Guest

    I would like to enter a text string at the command prompt and have AutoCAD return how many times the string is found in the drawing. I see lots of tried to do something similar using the ssget command. For example I want to type "CFM" at the command prompt and have it return how many times *CFM* appears in the drawing. It would need to place wildcards on both sides of the text string. Is this possible? Please help. Thanks.
     
    sdpurkey, Feb 4, 2005
    #1
  2. sdpurkey

    Paul Turvill Guest

    This will do exactly what you describe:

    (defun C:COUNTEXT ( / txt1 txt2 n)
    (setq txt1 (getstring "\nText to Count: ")
    txt2 (strcat "*" txt1 "*")
    ss (ssget "X" (list '(0 . "TEXT")(cons 1 txt2)))
    n (if ss (sslength ss) 0)
    );; setq
    (alert (strcat txt1 " appears " (itoa n) " time(s) in this drawing."))
    (princ)
    );; defun

    Note that the foregoing is case sensitive, but that can be changed with the
    (strcase) function if necessary.
    ___
     
    Paul Turvill, Feb 4, 2005
    #2
  3. sdpurkey

    Paul Turvill Guest

    Oops ... forgot to localize ss:

    (defun C:COUNTEXT ( / txt1 txt2 ss n)
    ....
     
    Paul Turvill, Feb 4, 2005
    #3
  4. sdpurkey

    sdpurkey Guest

    Thanks Paul,

    That was exactly what I needed. This is a big help.

    Much Thanks,
    Shannon
     
    sdpurkey, Feb 4, 2005
    #4
  5. Dean,
    can you use FIND from within lisp
    without dialog box?
    Ursus Uziemblo
     
    Ursus Uziemblo, Feb 4, 2005
    #5
  6. It may be worth noting that the response to
    "Text to Count: " is a *wildcard*. :)

    If he's counting text that contains literal
    characters that have special meaning in
    wildcards (like * for example), and its not
    his intention for the string to be a wildcard,
    then he must escape the special characters
    (e.g. "*" must become "`*").
     
    Tony Tanzillo, Feb 4, 2005
    #6
  7. Don't forget that this only counts TEXT entities, not MText or Attributes

    You could include MText by doing this:

    (setq ss (ssget "X" (list '(0 . "TEXT,MTEXT")(cons 1 txt2))))

    Matt

     
    Matt Stachoni, Feb 4, 2005
    #7
  8. sdpurkey

    sdpurkey Guest

    Thanks, I may use both... One for finding just text entities and one for text and mtext. Most of the time I wouldn't want to include mtext in my count due to the way we work our drawings, however, there have been a few times when someone has used mtext when they should have just used text.

    I appreciate everyones help, this has really saved me a lot of time.
     
    sdpurkey, Feb 4, 2005
    #8
  9. sdpurkey

    Rudy Tovar Guest

    And yet another method...but just remember that the number times it occurs
    may differ if you have an mtext that contains that same word or phrase
    several times within the mtext...

    (defun c:TS (/ string ac ms ps string cn for-item name st)

    (setq string (getstring t "\nEnter Text: "))

    (if string
    (progn

    (setq AC (vla-get-ActiveDocument (vlax-get-Acad-Object))
    ms (vla-get-modelspace ac)
    ps (vla-get-paperspace ac)
    string (strcase string)
    cn 0
    )


    (vlax-for for-item ps
    (progn
    (setq name (vla-get-objectname for-item))

    (if (or
    (= name "AcDbText")
    (= name "AcDbMText")
    )
    (progn
    (setq st (vla-get-textstring for-item))
    (if
    (wcmatch st (strcat "*" string "*"))
    (setq cn (1+ cn))
    )
    )
    )
    )
    ) ;end of vla-for
    (vlax-for for-item ms
    (progn
    (setq name (vla-get-objectname for-item))

    (if (or
    (= name "AcDbText")
    (= name "AcDbMText")
    )
    (progn
    (setq st (vla-get-textstring for-item))
    (if
    (wcmatch st (strcat "*" string "*"))
    (setq cn (1+ cn))
    )
    )
    )
    )
    ) ;end of vla-for

    )
    ) ;end of string

    (rtos cn 2 0)
    )
     
    Rudy Tovar, Feb 4, 2005
    #9
  10. sdpurkey

    Paul Turvill Guest

    You can also use just

    (setq ss (ssget "X" (list (cons 1 txt2))))

    and it will find TEXT, MTEXT, RTEXT, and ATTRIBs ... in short, all objects
    that use Group Code 1 to contain string data.
    ____
     
    Paul Turvill, Feb 4, 2005
    #10
  11. sdpurkey

    Paul Turvill Guest

    Point taken. Thanks, Tony.
    ___
     
    Paul Turvill, Feb 4, 2005
    #11
  12. sdpurkey

    Jürg Menzi Guest

    Hi Paul
    You may get unexpected results with this filter:
    - Check group code 1 for 3DSolid, Body, Dimension, OLEFrame, Region and
    Tolerance
    - For Mtext and Table with >255 charcter you've also to check code 3...

    Cheers
     
    Jürg Menzi, Feb 5, 2005
    #12
  13. sdpurkey

    Jürg Menzi Guest

    Oops...
    Must be:
    - For Mtext and Table with >250 charcter you've also to check code 3...

    Sorry
     
    Jürg Menzi, Feb 5, 2005
    #13
  14. sdpurkey

    Paul Turvill Guest

    Those aren't likely to contain anything matching the user's input, are they?
    ___
     
    Paul Turvill, Feb 5, 2005
    #14
  15. It will select attribute definitions, but it won't select attributes because
    they are subobjects of blocks, so I'm not sure how useful it truly is.

    Matt

     
    Matt Stachoni, Feb 7, 2005
    #15
  16. and it will find TEXT, MTEXT, RTEXT, and ATTRIBs

    Should be:

    and it will find TEXT, MTEXT, RTEXT, and ATTDEFs

    --

    Marc'Antonio Alessi
    http://xoomer.virgilio.it/alessi
    (strcat "NOT a " (substr (ver) 8 4) " guru.")

    --
     
    Marc'Antonio Alessi, Feb 8, 2005
    #16
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.