Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Edit LISP to iterate over all blocks in drawing

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
patrick.legaultBTQSE
919 Views, 9 Replies

Edit LISP to iterate over all blocks in drawing

Hello,

 

I am trying to edit an older LISP created in the past where it asks to selct specific block in drawing in order to switch it to a correct layer/color/linetype.

 

My issue is trying to convert this to slect all block within the drawing. I have played around with ssget _X but it does not seem to yield the desired result.

9 REPLIES 9
Message 2 of 10

IMO this is much easier using VLA ... give this a try:

 

(defun c:foo (/ a d)
  ;; RJP » 2022-04-13
  ;; All blocks internals to layer 0, color byblock and linetype byblock
  (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
      (and (= 0 (vlax-get b 'isxref) (vlax-get b 'islayout)) (not (wcmatch (vla-get-name b) "*|*")))
       (vlax-for o b
	 (cond ((vlax-write-enabled-p o)
		(vl-catch-all-apply 'vla-put-layer (list o "0"))
		(vl-catch-all-apply 'vla-put-linetype (list o "ByBlock"))
		(vl-catch-all-apply 'vla-put-color (list o 0))
	       )
	 )
       )
    )
  )
  (foreach l a (vlax-put l 'lock -1))
  (vla-regen d acallviewports)
  (princ)
)

Update per sample drawing below.

(defun c:foo2 (/ a d f)
  ;; RJP » 2022-04-13
  (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)
    (setq f (and (= 0 (vlax-get b 'isxref) (vlax-get b 'islayout))))
    (vlax-for o	b
      (cond ((vlax-write-enabled-p o)
	     ;; Only internals of block definitions to layer 0
	     (and f (vl-catch-all-apply 'vla-put-layer (list o "0")))
	     ;; Everything else
	     (vl-catch-all-apply 'vla-put-linetype (list o "ByBlock"))
	     (vl-catch-all-apply 'vla-put-color (list o 0))
	    )
      )
    )
  )
  (foreach l a (vlax-put l 'lock -1))
  (vla-regen d acallviewports)
  (princ)
)

 

Message 3 of 10

Thank you for quick reply! Maybe its just my VLA knowledge, however this will not work for blocks inside of viewports no? It only sets my title block (from what I can gather).

 

Side note: I am putting "learning vla" on my list this is much cleaner!

Message 4 of 10

@patrick.legaultBTQSE 

The code above modifies all the block definitions within the drawing. Post your sample drawing where you say it does not work within viewports.

Message 5 of 10

I have edited the inital post to add a sample dwg with viewports

Message 6 of 10

@patrick.legaultBTQSE 

It seems to work here? Do you want items other than blocks to be on layer 0 and linetype and color to byblock?

Attached are all 437 block definitions within that file and a quick check, the code is doing it's job.

 

Also, your 'bubble' callouts are MTEXT within a square that is a block?

ronjonp_0-1649876429000.png

 

Message 7 of 10

@patrick.legaultBTQSE  See if the attached drawing is what you are after.

Message 8 of 10


@patrick.legaultBTQSE wrote:

Side note: I am putting "learning vla" on my list this is much cleaner!


Give THIS a look when you start learning 🙂

Message 9 of 10

Ah yes I see it now, must have bugged! Thank you very much ron! I will have a look at the basics documentation as well!

Message 10 of 10

@patrick.legaultBTQSE I posted another version of the code in my original post that does everything in the drawing. You'll need to grab that version (c:foo2 ).

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report