object count per layer

object count per layer

Anonymous
Not applicable
3,191 Views
9 Replies
Message 1 of 10

object count per layer

Anonymous
Not applicable

thank you all. this lsp is count objects per layer all dwg.

I want to  add (ssget). specify a selection area. 

 

;; 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)

 

0 Likes
Accepted solutions (1)
3,192 Views
9 Replies
Replies (9)
Message 2 of 10

pbejse
Mentor
Mentor

@Anonymous wrote:

thank you all. this lsp is count objects per layer all dwg.

I want to  add (ssget). specify a selection area. 

 

 

... 


Try the modified attached lisp file [ layercount.lsp ]

 

snip:

 

....
(ssget)
	    (progn
		    (vlax-for obj (setq ss (vla-get-ActiveSelectionSet (vla-get-activedocument (vlax-get-acad-object))))
...

HTh

 

Message 3 of 10

Anonymous
Not applicable

thank you pbejse for your concern.

sorry but I really don't understood.

 

like this

(defun c:layercount ( / lst )   
(ssget)
 (progn
     (vlax-for obj (setq ss (vla-get-activedocument (vlax-get-acad-object))))

     

     (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))
                )
            )
        )
    )
 ) ; progn

 

 

error message : bad argument type: #<VLA-OBJECT IAcadDocument 000002330467aca8>

 

0 Likes
Message 4 of 10

cadheinz
Advocate
Advocate

Hallo

 

Have that synonymous times tested but get an error message

Habe das auch mal getestet bekomme aber eine Fehlermeldung

 

 

Befehl: LAYERCOUNT
Objekte wählen: Entgegengesetzte Ecke angeben: 4 gefunden
Objekte wählen:
; Fehler: Fehlerhafter Argumenttyp: VLA-Objektsammlung: #<VLA-OBJECT IAcadDocument 000002e3ac422658>
Befehl:

DH
0 Likes
Message 5 of 10

pbejse
Mentor
Mentor
Accepted solution

@cadheinz wrote:

Hallo

 

Have that synonymous times tested but get an error message

Habe das auch mal getestet bekomme aber eine Fehlermeldung  


Oops, thought i attached the file on my previous post. sorry about that.

 

 

0 Likes
Message 6 of 10

cadheinz
Advocate
Advocate

Thanks works perfectly

 

Danke funktioniert perfekt Smiley Happy

DH
0 Likes
Message 7 of 10

Anonymous
Not applicable

oh that is perfect.. thank you.

0 Likes
Message 8 of 10

pbejse
Mentor
Mentor

@Anonymous wrote:

oh that is perfect.. thank you.


Good for you, Glad i was able to help.

 

Lee Mac should take the credit for this,  kudos to LM Smiley Happy

 

 

0 Likes
Message 9 of 10

dusty_g
Enthusiast
Enthusiast

I've been using this program for a while and it has worked great.  Thanks again to Lee Mac.  I would like to adjust it but not familiar with the VLA commands, so I'm not sure how to do it.   What I would like is to have the counts per layer divided by 2 when it lists the object total.  So for example, there are 30 items on a layer, I would like it to divide by 2 and have it list 15 for that layer.  We need it to do this for some manufacturing process counts.

 

Any help/guidance would be appreciated, I'm sure it's pretty easy.  Let me know if you need further explanation.

 

 

0 Likes
Message 10 of 10

stev98312
Enthusiast
Enthusiast

Is there an easy way to alphabetize the layer list, rather than order by object count?

 

Steve

0 Likes