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

Lisp routine to delete blocks

12 REPLIES 12
Reply
Message 1 of 13
Anonymous
6106 Views, 12 Replies

Lisp routine to delete blocks

I had posted a thread for this earlier too. I need a LISP routine that could delete specific blocks from drawings. I have over 100 drawings, each having 4 blocks that I need to get rid of. Let me know whats the best way of doing it. Thank you.
12 REPLIES 12
Message 2 of 13
Anonymous
in reply to: Anonymous

Use (delblock "Blockname1,Blockname2,etc.")
This will delete all references to the blocks listed in blockname and purge
them from the drawing

(defun delblock (blockname / ss)
(if (setq ss (ssget "x" (list (cons 0 "INSERT") (cons 2 blockname))))
(command "erase" ss "")
)
(command ".purge" "b" blockname "n")
)

wrote in message news:4938857@discussion.autodesk.com...
I had posted a thread for this earlier too. I need a LISP routine that
could delete specific blocks from drawings. I have over 100 drawings, each
having 4 blocks that I need to get rid of. Let me know whats the best way
of doing it. Thank you.
Message 3 of 13
Anonymous
in reply to: Anonymous

Thanks. Here is what I tried: My block name here is wwc1. If the others were wwc2, wwc3 and wwc4, how would I insert their names? Also, this gives an error saying "too few arguement".

(defun delblock (wwc1 / ss)
(if (setq ss (ssget "x" (list (cons 0 "INSERT") (cons 2 wwc1))))
(command "erase" ss "")
)
(command ".purge" "b" wwc1 "n")
)
Message 4 of 13
Anonymous
in reply to: Anonymous

Just like Jim said. Substitute your block names (separated by commas) for
the ones in his example. You won't get the "too few arguments" error if you
supply the blocknames following the call to (delblock ...) as in the
example. Read Jim's post again, carefully.
___

wrote in message news:4939155@discussion.autodesk.com...
Thanks. Here is what I tried: My block name here is wwc1. If the others
were wwc2, wwc3 and wwc4, how would I insert their names? Also, this gives
an error saying "too few arguement".

(defun delblock (wwc1 / ss)
(if (setq ss (ssget "x" (list (cons 0 "INSERT") (cons 2 wwc1))))
(command "erase" ss "")
)
(command ".purge" "b" wwc1 "n")
)
Message 5 of 13
Anonymous
in reply to: Anonymous

Thank you. But, I am still not getting the expected result. When I type in: (delblock), it says too few arguements)
When (delblock wwc1), it says 'BAD SSGET VALUE'.
When (delblock "wwc1"), it says 'no entries found with the name "wwc1"'.
Thanks again.
Message 6 of 13
Anonymous
in reply to: Anonymous

That message is coming from the purge command, indicating that the block
wwc1 was not in the drawing.
(delblock "wwc1") is the correct syntax.
If you have more than 1 then it is (delblock "wwc1,wwc2,wwc3,wwc4") or
whatever is required.

wrote in message news:4940174@discussion.autodesk.com...
Thank you. But, I am still not getting the expected result. When I type
in: (delblock), it says too few arguements)
When (delblock wwc1), it says 'BAD SSGET VALUE'.
When (delblock "wwc1"), it says 'no entries found with the name "wwc1"'.
Thanks again.
Message 7 of 13
Cad Monkey 19
in reply to: Anonymous

Here you go pal, give this one a try!

(DEFUN C:DELETEBLOCK (/)
(ALERT "WARNING! - THIS COMMAND DELETES ALL OCCURENCES OF BLOCK SELECTED")
(DEFUN *ERROR* (MSGE)
(PRINC "ERROR: ")
(SETQ MSGE (PRINC "\nFUNCTION CANCELLED"))
(TERPRI))
(SETQ ENTL (CDR (ASSOC 2 (ENTGET (CAR (ENTSEL "\nSELECT BLOCK TO DELETE: "))))))
(SETQ SETL (SSGET "X" (LIST (CONS 2 ENTL))))
(COMMAND "ERASE" SETL "")
(SETQ NAME (STRCAT "\nALL BLOCKS NAMED " ENTL " HAVE NOW BEEN DELETED"))
(PRINC NAME)
(PRINC)
)
Message 8 of 13
jjorovi
in reply to: Cad Monkey 19

The code is very useful, but gives errors with some dynamic blocks. How can you improve?
Message 9 of 13
pbejse
in reply to: jjorovi


@jjorovi wrote:
The code is very useful, but gives errors with some dynamic blocks. How can you improve?

What code  are your refering to jjorovi?

 

Message 10 of 13
jjorovi
in reply to: pbejse

(DEFUN C:DELETEBLOCK (/)
 (ALERT "WARNING! - THIS COMMAND DELETES ALL OCCURENCES OF BLOCK SELECTED")
 (DEFUN *ERROR* (MSGE)
 (PRINC "ERROR: ")
 (SETQ MSGE (PRINC "\nFUNCTION CANCELLED"))
 (TERPRI))
 (SETQ ENTL (CDR (ASSOC 2 (ENTGET (CAR (ENTSEL "\nSELECT BLOCK TO DELETE: "))))))
 (SETQ SETL (SSGET "X" (LIST (CONS 2 ENTL))))
 (COMMAND "ERASE" SETL "")
 (SETQ NAME (STRCAT "\nALL BLOCKS NAMED " ENTL " HAVE NOW BEEN DELETED"))
 (PRINC NAME)
 (PRINC)
 ) 

 

Message 11 of 13
pbejse
in reply to: jjorovi

a quick mod

 

(defun c:deleteblock  (/ *error* msge effnme ent entl setl e)
      (vl-load-com)
      (alert
            "warning! - this command deletes all occurences of block selected")
      (defun *error*  (msge)
            (princ "error: ")
            (setq msge (princ "\nfunction cancelled"))
            (terpri))
      (setq effnme (lambda (x)
                         (vla-get-effectivename
                               (if (eq (type x) 'ENAME)
                                     (vlax-ename->vla-object x)
                                     x))))
      (princ "\nselect block to delete: ")
      (if (setq ent (ssget "_:S" '((0 . "INSERT"))))
            (progn
                  (setq entl (effnme (ssname ent 0)))
                  (ssget "x" (list (cons 2 (strcat entl ",`*U*"))))
                  (vlax-for
                         itm  (vla-get-ActiveSelectionSet
                                    (vla-get-ActiveDocument
                                          (vlax-get-acad-object)))
                        (if (eq (effnme itm) entl)
                              (vl-catch-all-apply
                                    'vla-delete
                                    (list itm))))
                  (princ (strcat "\nall blocks named "
                                 entl
                                 " have now been deleted")))
            (princ "\nNo Block Selected:"))

      (princ)
      )

 

HTH

Message 12 of 13
neil.merris
in reply to: pbejse

Not sure if 6 years is too long between comments, but I modified the previously mentioned code.  It's about as simple as they come, but hey IT WORKS.  For my example, I was removing a block called "ISSUESTAMP".

 

(defun C:IFBLOCKEXISTSDELETE (/)
(if (setq ss (ssget "x" (list (cons 0 "INSERT") (cons 2 "ISSUESTAMP"))))
(command "erase" ss "")
)
(command ".purge" "b" "ISSUESTAMP" "n")
)

 

Message 13 of 13
Kent1Cooper
in reply to: neil.merris


@neil.merris wrote:

....  It's about as simple as they come, but hey IT WORKS.  ....


 

... as long as the Block named is not a dynamic one.  And you could make it even simpler:

(defun C:IFBLOCKEXISTSDELETE (/ ss)
  (if (setq ss (ssget "x" '((2 . "ISSUESTAMP"))))
    (command
"erase" ss "" ".purge" "b" "ISSUESTAMP" "n"
) )
)

Presumably there won't be any things other than  Insertions with a 2-code entry that could possibly be of the same name, which is why you can omit the "INSERT" entity-type entry.  And since there's no evaluation of anything required inside the filter list, it can be a "quoted" list instead of using the (list) function outright.  And more than one command can be included in a (command) function.  But now I'm realizing that if there are none Inserted but the Block definition exists in the drawing, presumably you'd still want to Purge it, so maybe that ought to be pulled out into its own operation, not part of the 'then' expression in that (if) function.

 

 

[I did add localizing of the one variable.  And personally I would shorten the command name, too -- that's a mouthful!]

Kent Cooper, AIA

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

Post to forums  

Autodesk Design & Make Report

”Boost