Edit LISP to iterate over all blocks in drawing

Edit LISP to iterate over all blocks in drawing

patrick.legaultBTQSE
Explorer Explorer
1,686 Views
9 Replies
Message 1 of 10

Edit LISP to iterate over all blocks in drawing

patrick.legaultBTQSE
Explorer
Explorer

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.

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

ronjonp
Mentor
Mentor
Accepted solution

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

patrick.legaultBTQSE
Explorer
Explorer

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!

0 Likes
Message 4 of 10

ronjonp
Mentor
Mentor

@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.

0 Likes
Message 5 of 10

patrick.legaultBTQSE
Explorer
Explorer

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

0 Likes
Message 6 of 10

ronjonp
Mentor
Mentor

@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

 

0 Likes
Message 7 of 10

ronjonp
Mentor
Mentor

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

0 Likes
Message 8 of 10

ronjonp
Mentor
Mentor

@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

patrick.legaultBTQSE
Explorer
Explorer

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!

0 Likes
Message 10 of 10

ronjonp
Mentor
Mentor

@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 ).

0 Likes