Author Topic: Help deleting block from multiple drawings using Lee Mac's script writer  (Read 5236 times)

0 Members and 1 Guest are viewing this topic.

notredave

  • Newt
  • Posts: 140
All,

Good morning. I need help making this lisp routine work using Lee's script writer. It loads every drawing but does not delete the block named "squad". If someone can look at what I have and comment, I sure would appreciate it.

Thank you,
David

ronjonp

  • Needs a day job
  • Posts: 7526
Use something like this. The command erase will only erase items in the current space.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ b bn s)
  2.   (setq bn "squad")
  3.   (cond
  4.     ((setq s (ssget "_x" (list '(0 . "insert") (cons 2 bn))))
  5.      ;; Try to delete all blocks ( you may need to unlock layers )
  6.      (foreach x (mapcar 'cadr (ssnamex s))
  7.        (vl-catch-all-apply 'vla-delete (list (vlax-ename->vla-object x)))
  8.      )
  9.      ;; Purge block definition
  10.      (if (setq b (tblobjname "block" bn))
  11.        (vl-catch-all-apply 'vla-delete (list (vlax-ename->vla-object (cdr (assoc 330 (entget b))))))
  12.      )
  13.     )
  14.   )
  15.   (princ)
  16. )
« Last Edit: August 14, 2019, 01:13:41 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

notredave

  • Newt
  • Posts: 140
ronjonp,

You are the man! Thank you very much, it worked. I really appreciate all your help you have given me past and present. Take care!

ronjonp

  • Needs a day job
  • Posts: 7526
ronjonp,

You are the man! Thank you very much, it worked. I really appreciate all your help you have given me past and present. Take care!
Glad to help :) .. I updated the code above to purge the block def as well. Give it a go!

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7526
If you want something a bit more generic we could feed the function a block name like so:
Code - Auto/Visual Lisp: [Select]
  1. (defun myblockpurge (bn / b s)
  2.   (cond
  3.     ((setq s (ssget "_x" (list '(0 . "insert") (cons 2 bn))))
  4.      ;; Try to delete all blocks ( you may need to unlock layers )
  5.      (foreach x (mapcar 'cadr (ssnamex s))
  6.        (vl-catch-all-apply 'vla-delete (list (vlax-ename->vla-object x)))
  7.      )
  8.      ;; Purge block definition
  9.      (if (setq b (tblobjname "block" bn))
  10.        (vl-catch-all-apply 'vla-delete (list (vlax-ename->vla-object (cdr (assoc 330 (entget b))))))
  11.      )
  12.     )
  13.   )
  14.   (princ)
  15. )
  16. (myblockpurge "squad")
« Last Edit: August 14, 2019, 01:12:58 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

notredave

  • Newt
  • Posts: 140
ronjonp,

I tried your previous code post and it still deletes the block "squad" but when I open the drawing after, nothing has been purged. I'm sorry.

ronjonp

  • Needs a day job
  • Posts: 7526
ronjonp,

I tried your previous code post and it still deletes the block "squad" but when I open the drawing after, nothing has been purged. I'm sorry.
Try using the command purge and see if it works. If not then all blocks did not get deleted.

I just tested here and it works.

You can also add this to the code if you don't mind purging other items:
Code - Auto/Visual Lisp: [Select]
« Last Edit: August 14, 2019, 01:17:53 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
As an alternative, you could load my Delete Blocks program and call it in the following way from my Script Writer:
Code: [Select]
_.open *file* (load "DeleteBlocksV1-1.lsp" nil) (LM:deleteblocks:acdoc '("squad")) _.qsave _.close
The LM:deleteblocks function could alternatively be used in conjunction with my ObjectDBX Wrapper in the following way:
Code: [Select]
(defun c:delsquad ( )
    (LM:odbx '(lambda ( doc ) (LM:deleteblocks doc '("squad"))) nil t)
    (princ)
)

ronjonp

  • Needs a day job
  • Posts: 7526
Use Lee's suggestion then you'll know it's bomb proof.  :-) My code is just a quick one off...

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

notredave

  • Newt
  • Posts: 140
ronjonp,

I added your other line of code to have everything purged to no avail.

notredave

  • Newt
  • Posts: 140
Re: Help deleting block from multiple drawings using Lee Mac's script writer
« Reply #10 on: August 14, 2019, 01:34:07 PM »
LeeMac,

Thank you very much for chiming in but I'm not competent enough to implement your advice, lol. Thank you though for replying. I appreciate it.

David

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Help deleting block from multiple drawings using Lee Mac's script writer
« Reply #11 on: August 14, 2019, 01:47:09 PM »
Thank you very much for chiming in but I'm not competent enough to implement your advice, lol. Thank you though for replying. I appreciate it.

Hi David,

Which part in particular are you uncertain of? The first suggestion is following the same technique as you are currently using (loading & running an AutoLISP program from the Script), just using my Delete Blocks program rather than the code kindly posted by Ron.

Thanks,

Lee

notredave

  • Newt
  • Posts: 140
Re: Help deleting block from multiple drawings using Lee Mac's script writer
« Reply #12 on: August 14, 2019, 02:34:04 PM »
Lee,

Thank you very much for your suggestion but I got ron's code to work by adding purge command as shown in pic.