Author Topic: delete block in all layout.  (Read 6685 times)

0 Members and 1 Guest are viewing this topic.

Andrea

  • Water Moccasin
  • Posts: 2372
delete block in all layout.
« on: October 04, 2006, 11:47:53 PM »
Hi all...

I'm trying to find a routine who can delete a specific block name in ALL LAYOUT
without changing layout..

I know it's possible....but to much work here...
so if you can give me a hand...it will be appreciated..

thanks
Keep smile...

kpblc

  • Bull Frog
  • Posts: 396
Re: delete block in all layout.
« Reply #1 on: October 05, 2006, 12:35:40 AM »
Code: [Select]
(defun test (/ block_name)
  (if (and (setq block_name (getstring t "\nBlock Name : "))
   (tblobjname "block" block_name)
   ) ;_ end of and
    (foreach item
  (mapcar
    'vlax-ename->vla-object
    (vl-remove-if
      'listp
      (mapcar
'cadr
(ssnamex
  (ssget "_X"
(list '(0 . "INSERT")
       '(410 . "~model")
       (cons 2 block_name)
       ) ;_ end of list
) ;_ end of ssget
  ) ;_ end of ssnamex
) ;_ end of mapcar
      ) ;_ end of vl-remove-if
    ) ;_ end of mapcar
      (vla-erase item)
      ) ;_ end of foreach
    ) ;_ end of if
  ) ;_ end of defun
Sorry for my English.

CADmium

  • Newt
  • Posts: 33
Re: delete block in all layout.
« Reply #2 on: October 05, 2006, 02:15:54 AM »
a function in ActivX :
Code: [Select]
(defun blockrefdelete(Blockname / Block)
  (vlax-for layout(vla-get-layouts(vla-get-activedocument(vlax-get-acad-object)))
    (vlax-for ITEM (vla-get-block Layout)
      (if(and(member(strcase(vla-get-objectname  ITEM))
                   '("ACDBBLOCKREFERENCE" "ACDBMINSERTBLOCK")
             )
             (=(strcase(vla-get-name ITEM))(strcase BLOCKNAME))
         )
        (vla-delete ITEM)
      ) 
    )
  )
)

or try
Code: [Select]
(defun blockrefdelete(BLOCKNAME / LISTE)
  (if(and(setq BLOCK (tblobjname "block" BLOCKNAME))   
         (setq LISTE(entget(cdr (assoc 330 (entget BLOCK)))))
         (setq LISTE(cdr(reverse(cdr(member(assoc 102 LISTE)LISTE)))))
     )
    (mapcar '(lambda(X)
               (not(vl-catch-all-error-p
                     (vl-catch-all-apply
                       'vla-delete
                       (list X)
                     )
                   )
               )
             )
             (vl-remove-if-not '(lambda(Y) Y)       
                (mapcar '(lambda(X) (vlax-ename->vla-object  X))
                        (mapcar '(lambda(X) (cdr X)) LISTE)
                )
             )   
    )             
  )
)


and you can call both functions

(blockrefdelete (cdr(assoc 2(entget(ssname (ssget "_:S" '((0 . "INSERT")))0)))))
« Last Edit: October 05, 2006, 03:48:16 AM by CADmium »
"Bei 99% aller Probleme ist die umfassende Beschreibung des Problems bereits mehr als die Hälfte der Lösung desselben."

Patrick_35

  • Guest
Re: delete block in all layout.
« Reply #3 on: October 05, 2006, 09:30:40 AM »
Hi
An another

Code: [Select]
(defun c:delb(/ bloc bls bl ent)
  (if (setq bloc (getstring T "\nBlock name : "))
    (if (tblsearch "block" bloc)
      (progn
        (setq bls (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))))
        (vlax-for bl bls
          (vlax-for ent bl
            (if (eq (vla-get-objectname ent) "AcDbBlockReference")
              (if (eq (strcase (vla-get-name ent)) (strcase bloc))
                (vla-delete ent)
              )
            )
          )
        )
      )
      (princ (strcat "\nBlock " bloc " don't exist"))
    )
  )
  (princ)
)

@+

kpblc

  • Bull Frog
  • Posts: 396
Re: delete block in all layout.
« Reply #4 on: October 05, 2006, 09:43:46 AM »
Your code will erase block definition, not reference (i think). And anyway you're working with block collection (not insertions)
Sorry for my English.

Patrick_35

  • Guest
Re: delete block in all layout.
« Reply #5 on: October 05, 2006, 10:08:43 AM »
There is an trick. Test it

@+

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: delete block in all layout.
« Reply #6 on: October 05, 2006, 10:18:02 AM »
This is an oldie I created. May not be what you want.
Deletes a list of block names and purges them.

<edit: removed code as it does not work as planed>
« Last Edit: October 05, 2006, 10:49:28 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: delete block in all layout.
« Reply #7 on: October 05, 2006, 10:25:58 AM »
This is an oldie I created. May not be what you want.
Deletes a list of block names and purges them.

Today, my morning began from such program and coffee...
Your program deletes all blocks only in the last page

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: delete block in all layout.
« Reply #8 on: October 05, 2006, 10:37:23 AM »
Thanks Evgeniy

Checking the code, you are correct.
Back then I was using one layout drawings & switched to xrefs & multi tabs
and left that code behind. I'll put it in the trash bin and use yours. :)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: delete block in all layout.
« Reply #9 on: October 05, 2006, 10:40:56 AM »
I have deleted the last post...
Has rechecked it is it does not work: (

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: delete block in all layout.
« Reply #10 on: October 05, 2006, 11:20:05 AM »
kpblc & Evgeniy

If I may be so bold, I took your routines & modified for my subroutine like this.

Code: [Select]
(defun delblock_1 (bnames / ss)
  (if (setq ss
             (ssget
               "_X"
               (list
                 '(0 . "INSERT")
                 (cons 2
                       (substr
                         (apply
                           (function strcat)
                           (mapcar (function (lambda (x) (strcat "," x)))
                                   bnames
                           )
                         )
                         2
                       ) ;_  substr
                 )
               ) ;_ end of list
             ) ;_ end of ssget
      )
    (foreach item
                  (mapcar
                    'vlax-ename->vla-object
                    (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
                  ) ;_ end of mapcar
      (vla-erase item)
    ) ;_ end of foreach
  )
) ;_ end of defun

Code: [Select]
(delblock_1 '("Aproved Title Block D" "Aproved Title Block"))
« Last Edit: October 05, 2006, 11:29:11 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Andrea

  • Water Moccasin
  • Posts: 2372
Re: delete block in all layout.
« Reply #11 on: October 05, 2006, 05:23:27 PM »
Woo !   :roll:

thanks all for the quick response..
Keep smile...

whdjr

  • Guest
Re: delete block in all layout.
« Reply #12 on: October 10, 2006, 03:46:36 PM »
This is my favorite however I am partial.   :-)

Andrea

  • Water Moccasin
  • Posts: 2372
Re: delete block in all layout.
« Reply #13 on: October 11, 2006, 01:21:59 PM »
nice one wh... :wink:
Keep smile...

ELOQUINTET

  • Guest
Re: delete block in all layout.
« Reply #14 on: February 02, 2007, 04:23:35 PM »
I just happened to come across this post in a search and was wondering if anyone has a lisp that I could run in a batch script to get rid of a specified block from all layouts without picking it. It would also be splendid if it then purged the block out of the drawing. I know this is available just haven't had the time to do a full sweep of the forum today.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: delete block in all layout.
« Reply #15 on: February 02, 2007, 05:35:34 PM »
Dan,

You could use this:

Code: [Select]
(defun eblock (bname / ss)
  (if (setq ss (ssget "_X" (list (cons 2 bname))))
    (progn
      (mapcar 'entdel (mapcar 'cadr (ssnamex ss)))
      (vla-purgeall
(vla-get-activedocument (vlax-get-acad-object))
      )
    )
  )
)
(eblock)

To use in your script: (eblock "yourblockname")

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

T.Willey

  • Needs a day job
  • Posts: 5251
Re: delete block in all layout.
« Reply #16 on: February 02, 2007, 05:45:47 PM »
If you don't want to purge everything, you can just try and erase the blocks definition from the block collection.

Code: [Select]
(if
 (not
  (vl-catch-all-error-p
   (setq BlkObj
    (vl-catch-all-apply
     'vla-Item
     (list
      (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-Acad-Object)))
      bname
     )
    )
   )
  )
 )
 (vl-catch-all-error-p 'vla-Delete (list BlkObj))
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: delete block in all layout.
« Reply #17 on: February 02, 2007, 07:54:38 PM »
Code: [Select]
.
 .....
      (mapcar 'entdel (mapcar 'cadr (ssnamex ss)))
 ....
.
Ron
Ron,
I, too, thought this would work until I was recently advised that (entdel) only works for Modelspace and the most recently active Layout. Changing the code to use ActiveX's Delete method will solve this and work for all Layouts.
Code: [Select]
(mapcar '(lambda (x)
                (vla-delete
                  (vlax-ename->vla-object x)
                 )
                )
   (mapcar 'cadr (ssnamex ss)))

ronjonp

  • Needs a day job
  • Posts: 7529
Re: delete block in all layout.
« Reply #18 on: February 03, 2007, 10:12:25 AM »
Good to know Jeff. Thanks.

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: delete block in all layout.
« Reply #19 on: February 03, 2007, 07:59:56 PM »
ditto
Thanks Jeff
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Joe Burke

  • Guest
Re: delete block in all layout.
« Reply #20 on: February 04, 2007, 12:24:35 PM »
I wonder if anyone else thinks a program which tries to delete a block from a drawing should offer an option? Either expode all the references, including nested, and then delete the definition. Or simply delete the references and pugre the block definition.

I wrote something recently which deals with that question. I definitely felt a need to offer that option.

ELOQUINTET

  • Guest
Re: delete block in all layout.
« Reply #21 on: February 06, 2007, 09:59:29 AM »
basically i have updated our titleblock and need to go into our library and delete the old one and purge it out because when i insert the new one it is being overriden. ron i tried yours and it works great but i have a few more questions. how can i modify this so it looks for several blocks or should i repeat it in my script looking for a different eblock name? also another thing i didn't think about is that some but not all of our files are read only. i'm wondering how i can find which files are then make my changes then reset them to read only afterwards. anyone know how to do this via script?

ronjonp

  • Needs a day job
  • Posts: 7529
Re: delete block in all layout.
« Reply #22 on: February 06, 2007, 12:39:33 PM »
basically i have updated our titleblock and need to go into our library and delete the old one and purge it out because when i insert the new one it is being overriden. ron i tried yours and it works great but i have a few more questions. how can i modify this so it looks for several blocks or should i repeat it in my script looking for a different eblock name? also another thing i didn't think about is that some but not all of our files are read only. i'm wondering how i can find which files are then make my changes then reset them to read only afterwards. anyone know how to do this via script?

Per Jeff M...you should use this variant:

Code: [Select]
(defun eblock (bname / ss)
  (if (setq ss (ssget "_X" (list (cons 2 bname))))
    (progn
      (mapcar '(lambda (x)
(vla-delete
   (vlax-ename->vla-object x)
)
       )
      (mapcar 'cadr (ssnamex ss))
      )
      (vla-purgeall
(vla-get-activedocument (vlax-get-acad-object))
      )
    )
  )
)
(eblock)

As far as filtering for different block names:

(eblock "blocknm1,blocknm2")

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ELOQUINTET

  • Guest
Re: delete block in all layout.
« Reply #23 on: February 06, 2007, 03:10:04 PM »
thanks ron and jeff i will give this a try when i have a moment