Author Topic: Delete Block Table entity  (Read 6799 times)

0 Members and 1 Guest are viewing this topic.

cadman6735

  • Guest
Delete Block Table entity
« on: April 25, 2011, 02:32:01 PM »
Hi all

I am practicing with the following code

Code: [Select]
(defun c:test ( / )

  (setq sset (ssget "_X" '((0 . "INSERT"))))

  (setq cnt 0)

  (repeat (sslength sset)

    (setq
  ent (ssname sset cnt)
  entData (entget ent)
  entName (strcase (cdr (assoc 2 entData)))
)

    (if
      (wcmatch entName "*SEAL*,*SIG*")
      (entdel ent)
    )

    (setq cnt (1+ cnt))

  )

(princ)
)
I am trying to figure out how to delete a table entity in vanilla lisp without using purge command.

thanks
« Last Edit: April 25, 2011, 03:20:21 PM by cadman6735 »

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Delete Block Table entity
« Reply #1 on: April 25, 2011, 02:55:44 PM »
I guess it should be like this . :-)

Code: [Select]
(wcmatch (strcase entName) (strcase "*SEAL*,*SIG*"))

cadman6735

  • Guest
Re: Delete Block Table entity
« Reply #2 on: April 25, 2011, 03:00:51 PM »
Hi Tharwat

Your post confusses me...   I have already strcase my entName up in my setq.

I am having no trouble deleting the blocks from my drawing, I am looking to purge the block from the file without using the purge command in my lisp routine, only deleting the table enity using Vanilla lisp.

I can find plenty of examples with purge in the lisp function and plenty of examples deleteing the table entity via Visual Lisp, but having a problem finding an example using vanilla lisp.

Thanks

Edit

but I do like your technique better of where you place the strcase for entName.
Changed my repeat to match your suggestion...  Thanks

Code: [Select]
(repeat (sslength sset)

    (setq
  ent (ssname sset cnt)
  entData (entget ent)
  entName (cdr (assoc 2 entData))
)

    (if
      (wcmatch (strcase entName) "*SEAL*,*SIG*")
      (entdel ent)
    )

    (setq cnt (1+ cnt))

  )
« Last Edit: April 25, 2011, 03:24:12 PM by cadman6735 »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Delete Block Table entity
« Reply #3 on: April 25, 2011, 03:29:53 PM »
Hi,

I think it's not possible using 'Vanilla LISP': the entdel function do not work with BLOCK neither with BLOCK_RECORD.
What's the matter with Visual LISP ?
You can have a look yo this thread.
Speaking English as a French Frog

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Delete Block Table entity
« Reply #4 on: April 25, 2011, 03:32:44 PM »
What Gile said:

Code: [Select]
;;No workie
(and (setq blk (tblobjname "block" "test")) (entdel (cdr (assoc 330 (entget blk)))))
;;Workie
(and (setq blk (tblobjname "block" "test"))
     (vla-delete (vlax-ename->vla-object (cdr (assoc 330 (entget blk)))))
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

cadman6735

  • Guest
Re: Delete Block Table entity
« Reply #5 on: April 25, 2011, 03:43:17 PM »
Thanks guys

There is nothing wrong with using visual lisp or using the purge command

I am just practicing and wanted to know how to delete a table entity via vanilla lisp, but if it can't be done with vanilla lisp then I have learned what I desired to know...

This is probably why I could not find any examples of how to do it...   :wink:  

I will use ronjonp example

So is it save to assume that I can not delete any table entity via vanilla lisp?

Thanks again.

cadman6735

  • Guest
Re: Delete Block Table entity
« Reply #6 on: April 25, 2011, 03:57:03 PM »
gile

Just read your post to the link you provided...

Thanks this will help very much.

cadman6735

  • Guest
Re: Delete Block Table entity
« Reply #7 on: April 25, 2011, 05:01:30 PM »
Thanks Ron
Works Great

Code: [Select]
(defun c:test ( / )

  (setq sset (ssget "_X" '((0 . "INSERT"))))

  (setq cnt 0)

  (repeat (sslength sset)

     (setq
 ent (ssname sset cnt)
 entData (entget ent)
 entName (cdr (assoc 2 entData))
 entTblName (tblobjname "block" entName)
)

    (if
      (wcmatch (strcase entName) "*SEAL*,*SIG*")
      (progn
(entdel ent)
(vl-load-com)
(vla-delete (vlax-ename->vla-object (cdr (assoc 330 (entget entTblName)))))
      )
    )

    (setq cnt (1+ cnt))

  )

(princ)
)


ronjonp

  • Needs a day job
  • Posts: 7529
Re: Delete Block Table entity
« Reply #8 on: April 25, 2011, 05:34:54 PM »
Glad to help  :-)

Did not test but you may want to wrap the vla-delete like this:

Code: [Select]
(vl-catch-all-apply
  'vla-delete
  (list (vlax-ename->vla-object (cdr (assoc 330 (entget enttblname)))))
)

I'd imagine if the block was still referenced, it would fail.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

cadman6735

  • Guest
Re: Delete Block Table entity
« Reply #9 on: April 25, 2011, 05:53:46 PM »
Cool every hint helps, Thanks again Ron


cadman6735

  • Guest
Re: Delete Block Table entity
« Reply #10 on: April 26, 2011, 09:49:42 AM »
This is what I finished up with.

I am not sure if this is complete (void of errors) but it seems to work for me.  

My next step is to add a (getstring) function to get part of the block name via user input and add it to the (ssget) filter *SEAL* so I can choose different block other than blocks with Seal in the name.


Code: [Select]
(defun c:delBlock ( / sset cnt ent entData entName entTblName )
  
  (vl-load-com)

;--------------------------------------------------------------------------------------------------------------------------

  (vla-StartUndoMark acadActiveDocument)

;==========================================================================================================================
;  
;==========================================================================================================================

  (if
    (setq sset (ssget "_X" '((0 . "INSERT") (2 . "*SEAL*"))))
    (progn

      (setq cnt 0)

      (repeat (sslength sset)

;--------------------------------------------------------------------------------------------------------------------------

(setq
 ent (ssname sset cnt)
 entData (entget ent)
 entName (cdr (assoc 2 entData))
 entTblName (tblobjname "block" entName)
)

;--------------------------------------------------------------------------------------------------------------------------

(if
 (wcmatch (strcase entName) "*SEAL*")

 (progn
   (entdel ent)
   (vl-catch-all-apply 'vla-delete (list (vlax-ename->vla-object (cdr (assoc 330 (entget entTblName))))))
 )
)

;--------------------------------------------------------------------------------------------------------------------------

(setq cnt (1+ cnt))

;--------------------------------------------------------------------------------------------------------------------------

      )
    )

    (alert "No Block Definition in Drawing")
    
  )

;==========================================================================================================================
;  
;==========================================================================================================================

    (vla-EndUndoMark acadActiveDocument)

;--------------------------------------------------------------------------------------------------------------------------

    (princ)
)

cadman6735

  • Guest
Re: Delete Block Table entity
« Reply #11 on: April 26, 2011, 03:13:16 PM »
Here it is with the (getstring) for block name user input, I find it dangerouse because it is a total wildcard, it will filter by one letter and get all blocks that contain that one letter or you can spell out a portion of the desired block name to narrow down your selection.

WARNING:  This routine can delete blocks not intended to be deleted if too few letters are used for the selection set filter.
example:  if you enter "s" it will delete every block with an "s" in the name.
              if you enter "seal" it will delete all the blocks with "seal" in the name.
              if you enter "se" it will delete all the blocks with "se" in the name, including "seal" because "se" is part of "seal"

If this routine acts any different to what I explained above, please let me know, for I am a beginner and this makes me dangerouse.

Thanks

Code: [Select]
(defun c:TEST ( / blkName sset cnt ent entData entName entTblName )
 
  (vl-load-com)

;--------------------------------------------------------------------------------------------------------------------------

  (vla-StartUndoMark acadActiveDocument)

;==========================================================================================================================
;    
;==========================================================================================================================

  (setq blkName (getstring "\nEnter Block Name:  "))

  (if
    (setq sset (ssget "_X" (list '(0 . "INSERT") (cons 2 (strcat "*" blkName "*")))))
   
    (progn

      (setq cnt 0)

      (repeat (sslength sset)

(setq
  ent (ssname sset cnt)
  entData (entget ent)
  entName (cdr (assoc 2 entData))
  entTblName (tblobjname "block" entName)
)

(entdel ent)
(vl-catch-all-apply 'vla-delete (list (vlax-ename->vla-object (cdr (assoc 330 (entget entTblName))))))

(setq cnt (1+ cnt))

      )
     
    )

    (alert "No Block Definition in Drawing")
   
  )

;==========================================================================================================================
;    
;==========================================================================================================================

    (vla-EndUndoMark acadActiveDocument)

;--------------------------------------------------------------------------------------------------------------------------

    (princ)
)

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: Delete Block Table entity
« Reply #12 on: April 26, 2011, 03:29:54 PM »
Hi CADMan,

I would be inclined to approach it in the following way:

Code: [Select]
(defun c:test ( / name ss i b ) (vl-load-com)

  (setq name (getstring t "\nSpecify Block to Delete <All>: "))

  (if (setq ss (ssget "_X" (list (cons 0 "INSERT") (cons 2 (setq name (strcase (strcat "*" name "*")))))))
    (progn
      (repeat (setq i (sslength ss))
        (entdel (ssname ss (setq i (1- i))))
      )
      (while (setq b (tblnext "BLOCK" (null b)))
        (if (wcmatch (strcase (cdr (assoc 2 b))) name)
          (vl-catch-all-apply 'vla-delete
            (list
              (vlax-ename->vla-object
                (cdr
                  (assoc 330 (entget (tblobjname "BLOCK" (cdr (assoc 2 b)))))
                )
              )
            )
          )
        )
      )
    )
    (princ "\n--> No Blocks Found.")
  )
  (princ)
)

Since you needn't try to delete the block definition until all the INSERTs have been deleted.

Note the restrictions:

  • Doesn't account for anonymous Dynamic Block names
  • Doesn't account for Blocks on Locked Layers
  • Doesn't account for nested blocks

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Delete Block Table entity
« Reply #13 on: April 26, 2011, 03:46:15 PM »
  • Doesn't account for anonymous Dynamic Block names
  • Doesn't account for Blocks on Locked Layers
  • Doesn't account for nested blocks
It doesn't do much, does it??  It's kinda like the free-loading-college-buddy of LSP routines.  :wink:
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Delete Block Table entity
« Reply #14 on: April 26, 2011, 04:00:34 PM »
  • Doesn't account for anonymous Dynamic Block names
  • Doesn't account for Blocks on Locked Layers
  • Doesn't account for nested blocks
It doesn't do much, does it??  It's kinda like the free-loading-college-buddy of LSP routines.  :wink:
It just hangs around and smokes all your pot.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Delete Block Table entity
« Reply #15 on: April 26, 2011, 04:03:17 PM »
  • Doesn't account for anonymous Dynamic Block names
  • Doesn't account for Blocks on Locked Layers
  • Doesn't account for nested blocks
It doesn't do much, does it??  It's kinda like the free-loading-college-buddy of LSP routines.  :wink:
It just hangs around and smokes all your pot.
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

cadman6735

  • Guest
Re: Delete Block Table entity
« Reply #16 on: April 26, 2011, 05:13:19 PM »
Quote
It doesn't do much, does it??  It's kinda like the free-loading-college-buddy of LSP routines
Cold man, Cold...    I was pleased with my accomplishment, thanks for shattering my ego...  :-) just kidding

Very cool

Thanks for the feed back, this is what I need...

« Last Edit: April 26, 2011, 06:17:10 PM by cadman6735 »

cadman6735

  • Guest
Re: Delete Block Table entity
« Reply #17 on: April 27, 2011, 09:22:08 AM »
Hi Lee

Thanks for the code and advice.
Quote
Since you needn't try to delete the block definition until all the INSERTs have been deleted.

What's wrong with the way I am attmepting to do it?
Code: [Select]
(entdel ent)
(vl-catch-all-apply 'vla-delete (list (vlax-ename->vla-object (cdr (assoc 330 (entget entTblName))))))
I first delete the INSERT then (attempt) to delete the block definition one at a time vs deleting all INSERTS then deleting all definitions.  I am going a sequence (entdel) then (vla-delete).
Why is one way better than another?

(I say attempt because it only deletes a block defintion with the exact name "seal" not all the block definisions in the sset.)

It should delete anything caught by the sset, right?

Thanks for all your help.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Delete Block Table entity
« Reply #18 on: April 27, 2011, 09:25:39 AM »
If you have a thousand blocks the table entry delete will not work until all the blocks are removed. It would most likely be faster to delete all the blocks first then only call the table delete once rather than 1000 times.

Make sense?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

cadman6735

  • Guest
Re: Delete Block Table entity
« Reply #19 on: April 27, 2011, 09:51:37 AM »
Hi Ron

Yes, this makes sense.

So if I have 10 blocks of the same definition.  my code will delete one block at a time, so on the first pass I have only deleted one instance of the block leaving 9 behind so it is attempting to delete a definition that can't be deleted because there are still 9 left, right?  But on deleting the last INSERT it should delete the definition right? but this wastes time and probably has a chance to error too.  (I am only guessing about the error part)

Am I on the right track?
Thanks

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Delete Block Table entity
« Reply #20 on: April 27, 2011, 09:54:33 AM »
Exactamundo!  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC