Author Topic: Delete a layer within a block  (Read 4396 times)

0 Members and 1 Guest are viewing this topic.

GDF

  • Water Moccasin
  • Posts: 2081
Delete a layer within a block
« on: August 31, 2015, 08:21:03 AM »
Is it possible to delete a layer within a block without using the block editor?
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

ChrisCarlson

  • Guest
Re: Delete a layer within a block
« Reply #1 on: August 31, 2015, 08:22:59 AM »
As in delete a layer from the drawing or delete an object on that layer?

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Delete a layer within a block
« Reply #2 on: August 31, 2015, 08:25:36 AM »
Put simply:
Code - Auto/Visual Lisp: [Select]
  1.     (if (and (= "YourLayerHere" (vla-get-layer obj)) (vlax-write-enabled-p obj))
  2.         (vla-delete obj)
  3.     )
  4. )

GDF

  • Water Moccasin
  • Posts: 2081
Re: Delete a layer within a block
« Reply #3 on: August 31, 2015, 08:31:49 AM »
Thanks Lee.

Perfect!
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Delete a layer within a block
« Reply #4 on: August 31, 2015, 08:55:46 AM »
You're welcome!  :-)

GDF

  • Water Moccasin
  • Posts: 2081
Re: Delete a layer within a block
« Reply #5 on: August 31, 2015, 12:11:52 PM »
Example of how I'm using your routine:

(defun C:DLB (/ YourBlockHere YourLayerHere)
  (Setq YourBlockHere (cdr (assoc 2 (entget (car (entsel "Select Block: "))))))
  (vlax-for obj (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) YourBlockHere)
    (if (and (= "A-DIMS" (vla-get-layer obj)) (vlax-write-enabled-p obj))
        (vla-delete obj)
    )
  )
  (princ)
)
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Delete a layer within a block
« Reply #6 on: August 31, 2015, 12:21:40 PM »
Nice one Gary  :-)

Here are some tweaks to include a little more error trapping to account for null user input, dynamic blocks etc.:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:dlb ( / ent )
  2.     (while
  3.         (progn (setvar 'errno 0) (setq ent (car (entsel "\nSelect block: ")))
  4.             (cond
  5.                 (   (= 7 (getvar 'errno))
  6.                     (princ "\nMissed, try again.")
  7.                 )
  8.                 (   (null ent) nil)
  9.                 (   (/= "INSERT" (cdr (assoc 0 (entget ent))))
  10.                     (princ "\nSelected object is not a block.")
  11.                 )
  12.                 (   t
  13.                     (vlax-for obj
  14.                         (vla-item
  15.                             (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
  16.                             (vla-get-effectivename (vlax-ename->vla-object ent))
  17.                         )
  18.                         (if (and (= "A-DIMS" (vla-get-layer obj)) (vlax-write-enabled-p obj))
  19.                             (vla-delete obj)
  20.                         )
  21.                     )
  22.                     nil
  23.                 )
  24.             )
  25.         )
  26.     )
  27.     (princ)
  28. )

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Delete a layer within a block
« Reply #7 on: August 31, 2015, 12:42:55 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:dlb ( / ent )
  2.     (while
  3.         (progn (setvar 'errno 0) (setq ent (car (entsel "\nSelect block: ")))
  4.             (cond
  5.                 (   (= 7 (getvar 'errno))
  6.                     (princ "\nMISSED IT by that much Agent 99, try again.")
  7.                 )
  8.                 (   (null ent) nil)
  9.                 (   (/= "INSERT" (cdr (assoc 0 (entget ent))))
  10.                     (princ "\nSelected object is not a block.")
  11.                 )
  12.                 (   t
  13.                     (vlax-for obj
  14.                         (vla-item
  15.                             (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
  16.                             (vla-get-effectivename (vlax-ename->vla-object ent))
  17.                         )
  18.                         (if (and (= "A-DIMS" (vla-get-layer obj)) (vlax-write-enabled-p obj))
  19.                             (vla-delete obj)
  20.                         )
  21.                     )
  22.                     nil
  23.                 )
  24.             )
  25.         )
  26.     )
  27.     (princ)
  28. )

My tweak within Lee's Code.    :tongue2:
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Delete a layer within a block
« Reply #8 on: August 31, 2015, 01:06:15 PM »
My tweak within Lee's Code.    :tongue2:

 :lol:

ChrisCarlson

  • Guest
Re: Delete a layer within a block
« Reply #9 on: August 31, 2015, 01:16:23 PM »
Since we are offering tweaks, utilize Lee's selectif function and make a call to it. Very useful that way.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Delete a layer within a block
« Reply #10 on: August 31, 2015, 01:59:08 PM »
Since we are offering tweaks, utilize use Lee's selectif function and make a call to it. Very useful that way.
Here's my 'tweak'  :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

GDF

  • Water Moccasin
  • Posts: 2081
Re: Delete a layer within a block
« Reply #11 on: August 31, 2015, 03:15:55 PM »
Thanks 99
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

GDF

  • Water Moccasin
  • Posts: 2081
Re: Delete a layer within a block
« Reply #12 on: August 31, 2015, 04:58:15 PM »
another variation:

Code: [Select]
(defun c:DLBX ( / chgblk obj e i s YourLayerHere YourBlockHere)
  (setq YourLayerHere (strcase (getstring "\n* Enter Layer Name:")))
  (if (= YourLayerHere "")(setq YourLayerHere (strcase "A-DIMS")))
  (defun chgblk (YourBlockHere YourLayerHere)
    (vlax-for obj (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) YourBlockHere)
      (if (and (= YourLayerHere (vla-get-layer obj)) (vlax-write-enabled-p obj))
        (vla-delete obj)
      )
    )
  )
  (if (setq s (ssget "X" '((2 . "`unit*"))))
     (progn
        (setq i (1- (sslength s)))
          (while (<= 0 i)
                (setq e (ssname s i)
                      YourBlockHere (cdr (assoc 2 (entget e)))
                      i (1- i)
                )
                (chgblk (strcase YourBlockHere) YourLayerHere)
               
            )
        )
  )
  (princ)
)
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Delete a layer within a block
« Reply #13 on: August 31, 2015, 06:22:55 PM »
Careful Gary - consider that we are modifying the block definition for these blocks, and so this change need only be performed once for each block name.

If you are looking to modify all blocks with name starting "unit", it becomes far more efficient to iterate directly over the block collection, i.e.:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:dlbx nil
  2.         (if (wcmatch (strcase (vla-get-name blk) t) "unit*")
  3.             (vlax-for obj blk
  4.                 (if (and (= "A-DIMS" (strcase (vla-get-layer obj))) (vlax-write-enabled-p obj))
  5.                     (vla-delete obj)
  6.                 )
  7.             )
  8.         )
  9.     )
  10.     (princ)
  11. )

GDF

  • Water Moccasin
  • Posts: 2081
Re: Delete a layer within a block
« Reply #14 on: August 31, 2015, 07:06:16 PM »
Thanks Lee
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64