Jump to content

count the number a given text letters e.g. 3No. H's


Recommended Posts

  • 7 months later...
  • Replies 33
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    15

  • stockers

    8

  • laijumalias

    4

  • ReMark

    3

Posted

tsel works very fine, thank you for your help

Posted
Try this:

 

(defun c:tsel (/ *error* unique BPT CATT CNT DOC ENT I ITM J
                    K LST OBJ OLST SPC SS TBLOBJ TSS UFLAG)

 ;; by Lee McDonnell (Lee Mac)

 (vl-load-com)

 (defun *error* (msg)
   (and uFlag (vla-EndUndoMark doc))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ))

 (defun unique (lst / result)
   (reverse
     (while (setq itm (car lst))
       (setq lst (vl-remove itm lst) result (cons itm result)))))

 (setq doc (vla-get-ActiveDocument (vlax-get-Acad-Object))

       spc (if (zerop (vla-get-activespace doc))
             (if (= (vla-get-mspace doc) :vlax-true)
               (vla-get-modelspace doc)
               (vla-get-paperspace doc))
             (vla-get-modelspace doc)) i 2)

 (cond (  (eq 4 (logand 4 (cdr (assoc 70 (tblsearch "LAYER" (getvar "CLAYER"))))))
          (princ "\n<< Current Layer Locked >>"))

       (t (if (and (setq k -1 ss  (ssget '((-4 . "<OR")
                                             (0 . "TEXT,MTEXT")
                                             (-4 . "<AND")
                                               (0 . "INSERT")
                                               (66 . 1)
                                             (-4 . "AND>")
                                           (-4 . "OR>"))))

                   (setq bPt (getpoint "\nSelect Point for Table: ")))
            (progn
              (setq uFlag (not (vla-StartUndoMark doc)))

              (while (setq ent (ssname ss (setq k (1+ k))))
                (setq obj (vlax-ename->vla-object ent))

                (setq lst

                  (append lst

                    (cond (  (eq "AcDbBlockReference" (vla-get-ObjectName Obj))

                             (mapcar (function vla-get-TextString)
                                     (append
                                       (vlax-safearray->list
                                         (vlax-variant-value
                                           (vla-getAttributes Obj)))
                                             (if
                                               (not
                                                 (vl-catch-all-error-p
                                                   (setq cAtt
                                                     (vl-catch-all-apply 'vlax-safearray->list
                                                       (list
                                                         (vlax-variant-value
                                                           (vla-getConstantAttributes Obj))))))) cAtt))))

                          (t (list (vla-get-TextString obj)))))))

              (foreach Str (unique lst)
                (setq cnt 0)

                (if (setq j -1 tss (ssget "_X" (list '(-4 . "<OR")
                                                       '(-4 . "<AND")
                                                         '(0 . "TEXT,MTEXT")
                                                          (cons 1 str)
                                                       '(-4 . "AND>")
                                                       '(-4 . "<AND")
                                                         '(0 . "INSERT")
                                                         '(66 . 1)
                                                       '(-4 . "AND>")
                                                     '(-4 . "OR>"))))

                  (while (setq ent (ssname tss (setq j (1+ j))))
                    (setq Obj (vlax-ename->vla-object ent))

                    (cond (  (eq "AcDbBlockReference" (vla-get-ObjectName Obj))

                             (foreach Att (append
                                            (vlax-safearray->list
                                              (vlax-variant-value
                                                (vla-getAttributes Obj)))
                                            (if
                                              (not
                                                (vl-catch-all-error-p
                                                  (setq cAtt
                                                    (vl-catch-all-apply 'vlax-safearray->list
                                                      (list
                                                        (vlax-variant-value
                                                          (vla-getConstantAttributes Obj))))))) cAtt))

                               (if (eq Str (vla-get-TextString Att)) (setq cnt (1+ cnt)))))

                          (t (setq cnt (1+ cnt))))))

               (setq oLst (cons (cons str cnt) oLst)))

             (setq tblObj
               (vla-addTable spc
                 (vlax-3D-point bPt) (+ 2 (length olst)) 2 (* 1.5 (getvar "DIMTXT"))
                   (* (apply 'max
                        (mapcar 'strlen
                          (append '("String")
                            (apply 'append
                              (mapcar (function (lambda (x) (list (car x) (itoa (cdr x))))) olst)))))

                      2.0 (getvar "DIMTXT"))))

             (vla-setText tblObj 0 0 "String Counter")
               (vla-setText tblObj 1 0 "String")              
                 (vla-setText tblObj 1 1 "Count")

             (foreach x (vl-sort olst (function (lambda (a b) (< (car a) (car b)))))
               (vla-setText tblObj i 0 (car x))
                 (vla-setText tblObj i 1 (itoa (cdr x)))
                   (setq i (1+ i)))

             (setq uflag (vla-EndUndoMark doc))))))

 (princ))

 

Is there a way to count the strings only in the selected objects in other words is it possible to give an option to count in the entire drawing or select the objects from where the count should perform.

Posted

Hi Lee, thanks for your response, Can you please provide the modified code.

Posted
Hi Lee, thanks for your response, Can you please provide the modified code.
He just lives to serve. :glare:
Posted

Hi Lee,

 

Awaiting your support on this code, thanks in advance.

Posted
Hi Lee,

 

Awaiting your support on this code, thanks in advance.

 

I don't have much time, of which, here it is voluntary.

  • 1 month later...
Posted
Is there a way to count the strings only in the selected objects in other words is it possible to give an option to count in the entire drawing or select the objects from where the count should perform.

 

Anybody with any help?

Posted

Lee, you are amazing.

Thanks for helping us always.

:thumbsup::thumbsup::thumbsup:

Posted
Anybody with any help?

 

Did my code work for you laijumalias? Or was I wasting my time?

Posted
Thought so.

 

You are too kind Lee, and the kind never get the credits they deserve. You are noted down as Awesomesauce in my book though :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...