Lisp routine to delete 5 blocks (block1name, block2name, block3name, block4name, block5name) from 100 drawings

Lisp routine to delete 5 blocks (block1name, block2name, block3name, block4name, block5name) from 100 drawings

frank.cortesWRCXM
Participant Participant
1,027 Views
17 Replies
Message 1 of 18

Lisp routine to delete 5 blocks (block1name, block2name, block3name, block4name, block5name) from 100 drawings

frank.cortesWRCXM
Participant
Participant

Hi lisp experts, 

I am lisp beginner. and I am looking for a Lisp routine to delete 5 blocks (block1name, block2name, block3name, block4name, block5name) from 100 drawings. 

 

Can you give me some advice?

Thank you

0 Likes
1,028 Views
17 Replies
Replies (17)
Message 2 of 18

pendean
Community Legend
Community Legend
Message 3 of 18

paullimapa
Mentor
Mentor
0 Likes
Message 4 of 18

frank.cortesWRCXM
Participant
Participant

Thank for your response. I have tried those lisp already and they do not work if the blocks are in the layout.

I got this message

frankcortesWRCXM_0-1689185939592.png

If I run the lisp routine in paper space they remove the blocks , but I want to get a lisp to remove the list of blocks with no workspace restrictions (from model space or papers pace). any advice?

0 Likes
Message 5 of 18

paullimapa
Mentor
Mentor
(setq ss (ssget '((0 . "Insert") (2 . "Block1,block2,block3,block4,block5"))) i 0)


(repeat (sslength ss)
 (entdel (ssname ss i))
)

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 6 of 18

komondormrex
Mentor
Mentor

hi,

what do you mean exactly? delete block definitions of afore mentioned blocks and and theirs references or just references of those blocks?

0 Likes
Message 7 of 18

frank.cortesWRCXM
Participant
Participant

Hi

 I need to remove those blocks and their definitions.

 

Thanks 

0 Likes
Message 8 of 18

ronjonp
Mentor
Mentor

@frank.cortesWRCXM Give this a try:

(defun c:foo (/ a d)
  (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-for	o b
	(cond ((vlax-write-enabled-p o)
	       (cond ((and (wcmatch (vla-get-objectname o) "AcDbBlockReference")
			   (wcmatch (strcase (vla-get-name o)) "BLOCK1,BLOCK2,BLOCK3,BLOCK4,BLOCK5")
		      )
		      (vl-catch-all-apply 'vla-delete (list o))
		     )
	       )
	      )
	)
      )
    )
  )
  ;; Relock layers
  (foreach l a (vlax-put l 'lock -1))
  ;; Regen to see changes
  (vla-regen d acallviewports)
  ;; Purge
  (repeat 2 (vla-purgeall d))
  (princ)
)
0 Likes
Message 9 of 18

paullimapa
Mentor
Mentor

and to remove the definitions after the blocks have been deleted use this :

(command"_.Purge""_B""BlockName1,BlockName2,BlockName3,BlockName4,BlockName5""_N")

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 10 of 18

frank.cortesWRCXM
Participant
Participant

hi @ronjonp ,

Thank you so much for your help. The lisp works well (blocks located in Model or layout). Maybe because the blocks I want to remove are part of an Xref (my title block key map)? when I run the script, it does not do anything. can you help me with it?

 

Regards,

 

0 Likes
Message 11 of 18

ronjonp
Mentor
Mentor

@frank.cortesWRCXM 

If the blocks are nested within an xref they can not be directly erased. You'd need to open your titleblocks and remove the keymaps there.

0 Likes
Message 12 of 18

paullimapa
Mentor
Mentor

Maybe use Odbx method to open xref grab the blocks and vla-delete 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 13 of 18

frank.cortesWRCXM
Participant
Participant

Thank you all for your help. We do not want to open those files..You know that process involves a huge amount of time.

The Lee Mac delete blocks script works perfect as well and  remove them but the idea is not having to select the blocks.

 

 

0 Likes
Message 14 of 18

ronjonp
Mentor
Mentor

@frank.cortesWRCXM ODBX does not take a huge amount of time and with the code provided you don't have to manually select the blocks?

0 Likes
Message 15 of 18

marko_ribar
Advisor
Advisor

@ronjonp 

Something is wrong on my side while checking with ODBX... I used your function and feeded it to (LM:ODBX 'foo nil t) like you see in *.lsp I attached at the end... I created Block1,Block2,Block3,Block4,Block5,Block6,Block7,Block8,Block9,Block10 in each DWG - DWG copies... It doesn't matter I used BricsCAD for drawing, just ignore pop up when opening from ACAD...

Error is something like too few/ too many arguments during eval...

Can you see where is the problem - I saw that your function satisfies restrictions for ODBX...

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 16 of 18

ronjonp
Mentor
Mentor

@marko_ribar Untested but I think you need to change this:

 

(defun foo (/ a d)
  (vl-load-com)
  (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))))
  )

 

To this so its argument is the ODBX doc not active.

 

(defun foo (d / a)
  (vl-load-com)
  (vlax-for l (vla-get-layers d)
    (cond ((= -1 (vlax-get l 'lock)) (vlax-put l 'lock 0) (setq a (cons l a))))
  )

 

Lee has quite a few examples HERE.

Message 17 of 18

marko_ribar
Advisor
Advisor

Thanks Ron, it worked on my end...

Cheers...

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 18 of 18

ronjonp
Mentor
Mentor

@marko_ribar wrote:

Thanks Ron, it worked on my end...

Cheers...


Cheers!

0 Likes