Use AutoLISP to assign all geometry (inside multiple blocks) on a layer to be "ByBlock"?

Use AutoLISP to assign all geometry (inside multiple blocks) on a layer to be "ByBlock"?

acadadmin3KNUF
Advocate Advocate
635 Views
6 Replies
Message 1 of 7

Use AutoLISP to assign all geometry (inside multiple blocks) on a layer to be "ByBlock"?

acadadmin3KNUF
Advocate
Advocate

Hello to all:

 

I am dealing with a very tedious task; I have around 800 or so AutoCAD blocks with two layers that need to have all the geometry on those layers set to "ByBlock".

 

Can someone write me AutoLISP code to accomplish this?  Layer names "Layer1" and "Layer2" are good to use in any code that gets posted here.  Thanks very much in advance! 😁

0 Likes
Accepted solutions (1)
636 Views
6 Replies
Replies (6)
Message 2 of 7

ronjonp
Mentor
Mentor

@acadadmin3KNUF 

Give this a whirl 🙂

 

(defun c:foo (/ a d)
  ;; RJP » 2024-08-03
  (vlax-for l (vla-get-layers (setq d (vla-get-activedocument (vlax-get-acad-object))))
    (cond ((= -1 (vlax-get l 'lock)) (vlax-put l 'lock 0) (setq a (cons l a))))
  )
  (vlax-for b (vla-get-blocks d)
    (if	(= 0 (vlax-get b 'isxref) (vlax-get b 'islayout))
      (vlax-for	o b
	(and (vlax-write-enabled-p o)
	     (wcmatch (strcase (vla-get-layer o)) "TREW-TB_ISOFRAMES,TREW-TB_SS_RAILS")
	     (vla-put-color o 0)
	)
      )
    )
  )
  ;; Relock layers
  (foreach l a (vlax-put l 'lock -1))
  (princ)
)

 

Message 3 of 7

acadadmin3KNUF
Advocate
Advocate

Hello...thanks very much for the reply!  Unfortunately, it looks like I should have been specific with the exact layer names right from the get-go...  I tried your code with one of my blocks, and it didn't work.  I thought I could swap in the actual layer names I need for the generic Layer 1 and 2, but it didn't work.  I've attached one of the blocks I need this to work with.  Can you rework your code so it applies to the layers named TREW-TB_ISOFRAMES and TREW-TB_SS_RAILS?  Thanks very much, we've almost got it!

0 Likes
Message 4 of 7

ronjonp
Mentor
Mentor
Accepted solution

Code updated above. It worked on your test drawing.

Message 5 of 7

ec-cad
Collaborator
Collaborator

You should probably add:

(command "_regen")

in there before end, so OP can see that it works.

 

ECCAD

0 Likes
Message 6 of 7

acadadmin3KNUF
Advocate
Advocate

Thanks very much!!! This will save me a LOT of grief/manual labor/testing! 😎

0 Likes
Message 7 of 7

ronjonp
Mentor
Mentor

@acadadmin3KNUF wrote:

Thanks very much!!! This will save me a LOT of grief/manual labor/testing! 😎


Glad to help 🙂

0 Likes