LISP Get entities layer list within blocks and change layer of blocks

LISP Get entities layer list within blocks and change layer of blocks

darkfprh
Advocate Advocate
1,304 Views
5 Replies
Message 1 of 6

LISP Get entities layer list within blocks and change layer of blocks

darkfprh
Advocate
Advocate

Hi I need a lisp but I can't find and modify code.

I want to change block's layer to 0, but only blocks has 'layers' inside.

 

so I found this lisp but I cound't modify it.

It just get entities' name list.

but I need  entities' layer name and count it.

and as a result distinguish changing block's layer to 0.

 

-------------------------- Lisp Code --------------------------

 

(defun getblockitems (e / o n blks blk enames)
(setq
o (vlax-ename->vla-object e) ;insert object
n (vla-get-effectivename o) ;name
blks (vla-get-blocks (vla-get-document o)) ;block collection
blk (vla-item blks n) ;block of interest
)
;;build a list with the block definitions enames
(vlax-for n blk
(setq enames (cons (vlax-vla-object->ename o) enames))
)
(reverse enames) ;return list otherwise nil
)

0 Likes
1,305 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable

Would it be this ??

(vl-load-com)
(defun c:contarlayers (/ blockcomponents nme lay lstres fle )
  ;;without error handling.
  (defun :blockcomponents ( blk / ent lst )
    (if (setq ent (tblobjname "block" blk))
      (while (setq ent (entnext ent))
        (setq lst (cons ent lst))
      )
    )
    (reverse lst)
  )  
  (setq nme (getstring t "\nBlock name: "));;without verification
  (if (setq enm (:blockcomponents nme))
    (progn
      (foreach ent enm
        (setq lay (vla-get-layer (setq obj (vlax-ename->vla-object ent))))
        (setq lst (append lst (list lay))) 
      )
      (mapcar
        '(lambda (x)
           (if (not (assoc x res))
             (setq res (cons (cons x (- (length lst) (length (vl-remove x lst)))) res))
           )
         )
        lst
      )
      (setq res (reverse res));; result generates the list with layers and can be used for something else
      ;; creating the text file may be unnecessary
      (setq fle (open (setq loc (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) ".txt")) "W"))  
      (write-line "LAYER  AMOUNT" fle)
      (foreach x res (write-line (strcat (car x) " " (rtos (cdr x) 2 2)) fle));; will be a problem if the first item is a number !!
    )
    (prompt "\nOoops, No blocks selected!")
  )
  (startapp "notepad" loc)
  (princ)
)
;;(c:contarlayers)

 

0 Likes
Message 3 of 6

darkfprh
Advocate
Advocate

Thank you so much.

But I want to check all blocks and change layer to 0.

ssget X and repeat each blocks.

0 Likes
Message 4 of 6

Anonymous
Not applicable

@darkfprh  Do you want to change all entities in all your blocks to layer "0" ??

0 Likes
Message 5 of 6

Kent1Cooper
Consultant
Consultant

It's not clear to me, either, whether you want to change the Layer on which a Block is Inserted, or to change the Layer of the pieces in a Block's definition.  Either way, I think you would not want to use (ssget "X"), at least not at first, but would step through the Block table, and look at each Block's parts.  For the first purpose, if a Block definition is found to have whatever Layer(s) make you want to change Insertions of it to a particular Layer, then it could use (ssget "X") to find just Insertions of that Block, and change them.  For the second purpose, if the "right" [or "wrong"?] Layers are found, it could change things in the Block's definition, so that all Insertions of that Block would be changed, no matter how many Insertions there are of it, without your needing to find all of them.  Is one of those what you want to do?  Or am I misunderstanding?

Kent Cooper, AIA
0 Likes
Message 6 of 6

darkfprh
Advocate
Advocate

no I want to change only block.

0 Likes