Lisp to count entities on layers

Hey!

I used this code in a drawing to count how many entities there per layer. it shows the result in a Bricscad log window. It worked great for the first time, but when i wanted to use it on other drawings it doesnt show the correct values anymore. just the complete drawing count is correct again.

[code];; Layer Count  -  Lee Mac
;; Prints a report of the number of objects on each layer in a drawing

(defun c:layercount ( / lst )   
    (vlax-for blk (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
        (vlax-for obj blk
            (setq lst (layercount:assoc++ (vla-get-layer obj) lst))
            (if
                (and
                    (= "AcDbBlockReference" (vla-get-objectname obj))
                    (= :vlax-true (vla-get-hasattributes obj))
                )
                (foreach att (vlax-invoke obj 'getattributes)
                    (setq lst (layercount:assoc++ (vla-get-layer att) lst))
                )
            )
        )
    )
    (princ (layercount:padbetween "\n\n" "" "-" 62))
    (princ (layercount:padbetween "\nLayer" "Objects" " " 61))
    (princ (layercount:padbetween "\n" "" "-" 61))
    (foreach itm (vl-sort lst '(lambda ( a b ) (> (cdr a) (cdr b))))
        (princ (layercount:padbetween (strcat "\n" (car itm)) (itoa (cdr itm)) "." 61))
    )
    (princ (layercount:padbetween "\n" "" "-" 61))
    (princ (layercount:padbetween "\nTotal" (itoa (apply '+ (mapcar 'cdr lst))) "." 61))
    (princ (layercount:padbetween "\n" "" "-" 61))
    (textpage)
    (princ)
)
(defun layercount:assoc++ ( key lst / itm )
    (if (setq itm (assoc key lst))
        (subst (cons key (1+ (cdr itm))) itm lst)
        (cons  (cons key 1) lst)
    )
)
(defun layercount:padbetween ( s1 s2 ch ln )
    (
        (lambda ( a b c )
            (repeat (- ln (length b) (length c)) (setq c (cons a c)))
            (vl-list->string (append b c))
        )
        (ascii ch)
        (vl-string->list s1)
        (vl-string->list s2)
    )
)       
(vl-load-com) (princ)[/code]

The output gives you sth like this:

[code]743_W_161207_Balkondetail_nicht_A1-A7|Hoehenangabe-PNum...13
743_W_161206_2_A1-A7_Balkondetail|ABN7.5$G5S..............13
Mauer.....................................................12
Neu.......................................................12
AKORREKTUR_ANMERK$G1S.....................................12
743_W_161207_Balkondetail_nicht_A1-A7|EG_Fassade..........12
743_W_161206_2_A1-A7_Balkondetail|AANS_Dachflächen$S1X....12
743_W_161207_Balkondetail_nicht_A1-A7|Hoehenangabe-Hoehe..12
743_W_161206_2_A1-A7_Balkondetail|Text_aus................12
743_W_161207_Balkondetail_nicht_A1-A7|AN_ANS$O2R..........12
3STUHLBEIN$G3S............................................12
3STUHLFL$G3S..............................................12
A09_Raumbezeichnungen$T2S.................................12
743_W_161206_2_A1-A7_Balkondetail|AWANDBest$SMS...........12
743_W_161206_2_A1-A7_Balkondetail|E41_FASSADE.............12
743_W_161207_Balkondetail_nicht_A1-A7|A09_Raumbezeichnungen$T2S12
743_W_161206_2_A1-A7_Balkondetail|AV_GebaeudeNummer.......11
743_W_161207_Balkondetail_nicht_A1-A7|Rek_GP-Hoehe........11
743_W_161207_Balkondetail_nicht_A1-A7|E01_DECKEN..........11
743_W_161207_Balkondetail_nicht_A1-A7|AV_FixpunktSymbol...11
AB_Bemerkungen_Notizen$T3S................................11
AV_Grenze.................................................11
743_W_161206_2_A1-A7_Balkondetail|AWANDTR$S3S.............11
743_W_161206_2_A1-A7_Balkondetail|E52_AUSSENTUEREN........11
AWANDBest$SMS.............................................11
743_W_161206_2_A1-A7_Balkondetail|XREF....................10
743_W_161207_Balkondetail_nicht_A1-A7|Kontrollschacht-PNum10
[/code]


Comments

  • I just found out that it works when you deactivate all xrefs in the drawing.
    But he still doesnt count any nested objects in blocks etc.

    Does somebody know a possibility to change the code to do so? I found sth on the website of lee mac but cant quite wrap my head around it.

    http://lee-mac.com/assocplusplus.html

    [code];; Nested Assoc++  -  Lee Mac
    ;; Increments the value of a key in an association list with possible nested structure,
    ;; or adds the set of keys to the list if not present.
    ;; key - [lst] List of keys & subkeys
    ;; lst - [lst] Association list (may be nil)

    (defun LM:nassoc++ ( key lst / itm )
        (if key
            (if (setq itm (assoc (car key) lst))
                (subst (cons (car key) (LM:nassoc++ (cdr key) (cdr itm))) itm lst)
                (cons  (cons (car key) (LM:nassoc++ (cdr key) nil)) lst)
            )
            (if lst (list (1+ (car lst))) '(1))
        )
    )[/code]

    Anybody can help maybe?

    Cheers
  • Benjamin, you are wrong. Lee's code does count objects nested in blocks. But it does not check if a block is inserted or not. Nor does it account for the number of inserts.
  • BTW: If you are interested in a program to count blocks:
    http://www.b-k-g.nl/bkg_countblocks.html
    This program does not have to limitations mentioned in my previous post.

  • ok thanks.
    Is there a possibility to extend it to do a count like "entitites in block"*number of references?

    Unfortunately only counting blocks doesnt really help me. i wanted to use the lisp to do extended audit of my files. like see on which layer there are much too much objects etc.
  • I just found out why it would not show me the full layer count. My History in the command line doesnt show me all the lines he is producing, only the last 100 or so. So I added a little code to print the result into the logfile and it works!

    I tried to change the number of lines of the command line history in the options from default 256 to 500 or more but it wouldnt change anything. Anybody knows why?

    [code]
    ;; Layer Count  -  Lee Mac
    ;; Prints a report of the number of objects on each layer in a drawing
    (vl-load-com)



    (defun c:layercount ( / lst )

    ;clears the log file
    (defun clrlogfile ( / f)
      (setq f (open (getvar "LogFileName") "w"))
      (write-line "" f)
      (close f)
      )

    (clrlogfile);clears the logfile

    (setvar "logfilemode" 1);begin logging

        (vlax-for blk (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
            (vlax-for obj blk
                (setq lst (layercount:assoc++ (vla-get-layer obj) lst))
                (if
                    (and
                        (= "AcDbBlockReference" (vla-get-objectname obj))
                        (= :vlax-true (vla-get-hasattributes obj))
                    )
                    (foreach att (vlax-invoke obj 'getattributes)
                        (setq lst (layercount:assoc++ (vla-get-layer att) lst))
                    )
                )
            )
        )
    (princ (layercount:padbetween "\n\n" "" "-" 62))  
        (princ (layercount:padbetween "\nLayer" "Objects" " " 61))
        (princ (layercount:padbetween "\n" "" "-" 61))
        (foreach itm (vl-sort lst '(lambda ( a b ) (> (cdr a) (cdr b))))
            (princ (layercount:padbetween (strcat "\n" (car itm)) (itoa (cdr itm)) "." 61))
        )
        (princ (layercount:padbetween "\n" "" "-" 61))
        (princ (layercount:padbetween "\nTotal" (itoa (apply '+ (mapcar 'cdr lst))) "." 61))
        (princ (layercount:padbetween "\n" "" "-" 61))
        (textpage)
        (princ)
    (setvar "logfilemode" 0);end logging

    (startapp "Notepad" (getvar "LogFileName"));open it up in notepad
    )

    (defun layercount:assoc++ ( key lst / itm )
        (if (setq itm (assoc key lst))
            (subst (cons key (1+ (cdr itm))) itm lst)
            (cons  (cons key 1) lst)
        )
    )
    (defun layercount:padbetween ( s1 s2 ch ln )
        (
            (lambda ( a b c )
                (repeat (- ln (length b) (length c)) (setq c (cons a c)))
                (vl-list->string (append b c))
            )
            (ascii ch)
            (vl-string->list s1)
            (vl-string->list s2)
        )
    )       

    (vl-load-com) (princ)

    [/code]
  • A new SCRLHIST value applies to *new* Command Bar history.
This discussion has been closed.