Delete Blocks from multiple drawings

Delete Blocks from multiple drawings

Anonymous
Not applicable
4,594 Views
14 Replies
Message 1 of 15

Delete Blocks from multiple drawings

Anonymous
Not applicable

Hi All,

I am looking for a LISP routine that will delete a block from multiple drawings.

I have read other posts about this and cant figure out how people used these LISP routines or scripts in a batch process.

I thought there was a LISP years ago that would do what I am looking for.

Anyone have any suggestions?

 

Thanks in advance

0 Likes
Accepted solutions (1)
4,595 Views
14 Replies
Replies (14)
Message 2 of 15

Moshe-A
Mentor
Mentor

@Anonymous  hi,

 

here is a command DELBLK to delete a block ("YourBlockName") from a dwg

it steps through all tabs (layouts) and delete only the 'free' blocks (e.g not nested)

 

to it run through all files use  >> script pro 2 << 

 

enjoy

Moshe

 

(defun c:delblk (/ ss)
 (foreach lay (layoutlist)
  (setvar "ctab" lay)

  (if (setq ss (ssget "_x" '((0 . "insert") (2 . "YourBlockName"))))
   (command "._erase" ss "")
  ); if
 ); foreach

 (princ) 
)

 

 

 

0 Likes
Message 3 of 15

Anonymous
Not applicable

Moshe-A, thanks for the quick reply. I should have been more specific, but I am looking for a LISP that removes a block from multiple drawings, NOT multiple layout tabs. Would this LISP work for multiple drawings if I used script Pro 2?

 

0 Likes
Message 4 of 15

Moshe-A
Mentor
Mentor

@Anonymous 

 


@Anonymous wrote:

Moshe-A, thanks for the quick reply. I should have been more specific, but I am looking for a LISP that removes a block from multiple drawings, NOT multiple layout tabs. Would this LISP work for multiple drawings if I used script Pro 2?

 


Yes

 

Replace "YourBlockName" with your actual block name.

 

0 Likes
Message 5 of 15

pbejse
Mentor
Mentor

@Anonymous wrote:

Hi All,

I am looking for a LISP routine that will delete a block from multiple drawings.

 


(Defun c:DeleteBlockName (/ f filesToProcess  cnt odbx)  
  (if
    (and
      (setq f (acet-ui-pickdir
		"Select folder to process"
		(getvar 'dwgprefix)
	      )
      )
      (setq filesToProcess (vl-directory-files f "*.dwg" 1))
      (setq blkname (vl-princ-to-string (lisped (if blkname blkname "Enter Block name"))))
      (not (member blkname '("0" "-1")))
      (snvalid blkname)
      )
     (progn
       (if (< (atoi (substr (getvar "ACADVER") 1 2)) 16)
	 (setq odbx (vlax-get-or-create-object "ObjectDBX.AxDbDocument"))
	 (setq odbx (vlax-get-or-create-object
		      (strcat "ObjectDBX.AxDbDocument."
			      (substr (getvar "ACADVER") 1 2)
		      )
		    )
	 )
       )

       (foreach	itm filesToProcess
	 (setq cnt 0)
	 (if
	   (not
	     (vl-catch-all-error-p
	       (vl-Catch-All-Apply
		 '(lambda ()
		    (vla-open odbx (setq fullname (strcat f "\\" itm)))
		  )
	       )
	     )
	   )
	    (progn
	      (vlax-for	layout (vla-get-layouts odbx)
		(vlax-for objBlock (vla-get-block layout)
		  (if
		    (and
		      (vlax-write-enabled-p objBlock)
		      (= (setq objtype (vla-get-objectname objBlock))
			 "AcDbBlockReference"
		      )
		      (eq (strcase
			    (Vlax-get objBlock 'effectivename)
			    )
			  blkname
		      )
		    )
		    (progn
		     (Vla-delete objBlock) (setq cnt (1+ cnt))
		     )
		  )
		)
	      )
	      (if (> cnt 0)
		(progn
	      		(vla-saveas odbx fullname)
		  	(princ (strcat "\n" (vl-filename-base fullname)
				       "\nDeleted " (itoa cnt) " block(s) : " blkname ))))
	    )
	 )
       )
       (vlax-release-object odbx)
     )
  )
  (princ)
)

HTh

0 Likes
Message 6 of 15

Anonymous
Not applicable

Pbejse, thank you for the LISP. I'm not sure how to get it to work though. When I load the app in AutoCAD and type the command prompt, it opens up a dialog box to select a folder. I am not sure what I am to do at this point. Is it looking for the folder where the block resides?

Thanks,

Nancy

0 Likes
Message 7 of 15

pbejse
Mentor
Mentor

@Anonymous wrote:

.. it opens up a dialog box to select a folder. I am not sure what I am to do at this point. Is it looking for the folder where the block resides?


The program prompts for the folder where the drawing files will be searched for the  block name to be deleted

If successful, you will see something like this on the command prompt

Sample File 1
Deleted 3 block(s) : TargetBlockName
Sample File 2
Deleted 1 block(s) : TargetBlockName

 

To be on the safe side, back up your files if you're not sure if this program is for you 

 

 

0 Likes
Message 8 of 15

Anonymous
Not applicable

Thank you pbejse!

0 Likes
Message 9 of 15

pbejse
Mentor
Mentor

@Anonymous wrote:

Thank you pbejse!


You are welcome @Anonymous Glad to help.

 

 

0 Likes
Message 10 of 15

Anonymous
Not applicable

pbejse, I thought it was working after you explained it, but I'm confused with something. When I load the LISP and I select the folder I want to run the LISP on, I am prompted with a window "Contents" of Edit Mtext window.

Can you tell me what I am to do with that?

0 Likes
Message 11 of 15

pbejse
Mentor
Mentor
Accepted solution

@Anonymous wrote:

pbejse, I thought it was working after you explained it, but I'm confused with something. When I load the LISP and I select the folder I want to run the LISP on, I am prompted with a window "Contents" of Edit Mtext window.

Can you tell me what I am to do with that?


That is where you type the name of the block to deleted inside the drawing within the selected folder

 

pbejse_0-1605718810090.png

 

Its an alternative for prompting the user to type in the command prompt. 

(setq blkname (getstring "\nEnter Block name: " T))
(snvalid blkname)

 

Command: Enter Block name: 

 

I personally dont like typed-in values.

Preferably the drawing where you invoke command will have the block that you want to delete, that way the program can prompt the user to select on a listbox instead of typing the name, which at times the user is not even sure of the of the spelling <--- really really

 

pbejse_1-1605719283052.png

 

Will that work for you?

 

 

0 Likes
Message 12 of 15

Anonymous
Not applicable

pbejse, sorry, I cannot get this to work.

This is what I am doing:

1. I have all the drawings in one folder, located on my desktop.

2. I open one drawing and "APPLOAD" the LISP.

3. I type the command prompt "deleteblockname"

4. I browse to the folder where my drawings reside (on desktop), then hit OK

5. I am prompted to enter text in the "Edit Mtext" window. I type the name of the block I want to delete.

In this case, the block name is "Seal Fant TX", then hit OK

 

Nothing happens at this point. The command window shows "Command" as if it were ready for another command.  I look at the drawings in the folder and the block is still there.

 

Do you know what this could possibly be?

 

 

 

0 Likes
Message 13 of 15

pbejse
Mentor
Mentor

@Anonymous wrote:

Do you know what this could possibly be?


[ update ]

This is whats missing

(snvalid (setq blkname (strcase blkname)))

Truly my bad 🙂

 

Glad it works for you now. 

Cheers

 

Message 14 of 15

adiez_ife
Explorer
Explorer

where will i insert this line?
(snvalid (setq blkname (strcase blkname)))

0 Likes
Message 15 of 15

Sea-Haven
Mentor
Mentor

Look at post 5.

0 Likes