Author Topic: Delete a layer using objectdbx  (Read 6886 times)

0 Members and 1 Guest are viewing this topic.

GDF

  • Water Moccasin
  • Posts: 2081
Delete a layer using objectdbx
« on: January 23, 2012, 11:58:33 PM »
Using Lee's ObjectDBX Wrapper routine:
http://www.theswamp.org/index.php?topic=31827.msg373958#msg373958

I am trying to delete a layer. I keep running into problems...Key not found.
Also, I guess a tblsearch cannot be done in objectdbx.

Code: [Select]
(defun ARCH:DEL-LAY (layer_name / n ent acapp acsp adoc allrs layer_name TotalNumber CurrNumber)   
  (setq acapp (vlax-get-acad-object)
adoc  (vla-get-activedocument acapp)
acsp  (vla-get-block (vla-get-activelayout adoc))
allrs (vla-get-layers adoc)
  ) 
  (or (eq (vla-get-freeze (vla-item allrs layer_name)) :vlax-true)
    (vla-put-freeze (vla-item allrs layer_name) :vlax-false)
  )
  (or (eq (vla-get-lock (vla-item allrs layer_name)) :vlax-true)
    (vla-put-lock  (vla-item allrs layer_name) :vlax-false)
  )
  (or (eq (vla-get-layeron (vla-item allrs layer_name)) :vlax-false)
    (vla-put-layeron (vla-item allrs layer_name) :vlax-true)
  )
  (vlax-for lt (vla-get-layouts adoc)
    (vlax-for ob (vla-get-block lt)
      (if (eq (vla-get-layer ob) layer_name)
(progn
(vla-delete ob)
(vlax-release-object ob)
)
      )
    )
  ) 
  (princ)
)
;;;
(defun ARCH:DBX-LAY-DBX ( ) 
  (if (tblsearch "layer" "A-REVS-SYMB")(ARCH:DEL-LAY "A-REVS-SYMB"))   
)
;;;
(defun C:TEST ()
  (ARCH:LOAD (strcat ARCH#UTIF "ObjectDBXBase.lsp"))
  (LM:ODBX (ARCH:DBX-LAY-DBX) nil t)
)
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: 12905
  • London, England
Re: Delete a layer using objectdbx
« Reply #1 on: January 24, 2012, 08:05:48 AM »
Hi Gary,

When working with ObjectDBX you are working with the ObjectDBX Document Object, not the ActiveDocument, so the function supplied to my ObjectDBX Wrapper function will need to require a single argument - the ObjectDBX Document.

Try the following, (though, I haven't tested it):

Code - Auto/Visual Lisp: [Select]
  1. (defun ARCH:DEL-LAY ( doc name / layer )
  2.     (if
  3.         (null
  4.             (vl-catch-all-error-p
  5.                 (setq layer
  6.                     (vl-catch-all-apply 'vla-item (list (vla-get-layers doc) name))
  7.                 )
  8.             )
  9.         )
  10.         (progn
  11.             (vl-catch-all-apply 'vla-put-freeze (list layer :vlax-false))
  12.             (vla-put-lock layer :vlax-false)
  13.             (vla-put-layeron layer :vlax-true)
  14.  
  15.             (vlax-for block (vla-get-blocks doc)
  16.                 (vlax-for obj block
  17.                     (if (eq name (vla-get-layer obj)) (vla-delete obj))
  18.                 )
  19.             )
  20.             (vl-catch-all-apply 'vla-delete (list layer))
  21.             t ;; Success!
  22.         )
  23.     )
  24. )
  25.  
  26. (defun ARCH:DEL-LAY-DBX ( doc )
  27.     (ARCH:DEL-LAY doc "A-REVS-SYMB")
  28. )
  29.  
  30. (defun C:TEST ( )
  31.     (ARCH:LOAD (strcat ARCH#UTIF "ObjectDBXBase.lsp"))
  32.     (LM:ODBX 'ARCH:DEL-LAY-DBX nil t)
  33. )
  34.  

It will Thaw / Unlock / Turn On the layer (if it exists), then iterate over all blocks in the drawing (including Modelspace / Paperspace blocks), then, for each block, will iterate over all objects in the block. If the object resides on the layer in question, the object will be deleted.

Finally, the function attempts to delete the layer from the layer collection.

GDF

  • Water Moccasin
  • Posts: 2081
Re: Delete a layer using objectdbx
« Reply #2 on: January 24, 2012, 08:28:09 AM »
Thank you Lee.

PERFECT!!!

You just made my day and saved me a lot of time.
I didn't know enough to figure this one out.

Gary
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: 12905
  • London, England
Re: Delete a layer using objectdbx
« Reply #3 on: January 24, 2012, 08:34:46 AM »
Excellent Gary, I'm delighted to hear that it worked for you  :-)

Cheers,

Lee

GDF

  • Water Moccasin
  • Posts: 2081
Re: Delete a layer using objectdbx
« Reply #4 on: January 24, 2012, 03:33:51 PM »
Lee

Using the modified ARCH:DEL-LAY function...
Is it possible to delete more than one layer at a time, reading from a list?
Below example does not work:

(defun ARCH:DBX_DelLay (doc) (ARCH:DEL-LAY doc (list "A-REVS-CLOD" "A-REVS-SYMB")))

(defun C:REVS  ()
  (ARCH:LOAD (strcat ARCH#UTIF "ObjectDBXBase.lsp"))
  (LM:ODBX 'ARCH:DBX_DelLay nil t)
)
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: 12905
  • London, England
Re: Delete a layer using objectdbx
« Reply #5 on: January 24, 2012, 03:42:40 PM »
The easiest modification would be:

Code - Auto/Visual Lisp: [Select]
  1. (defun ARCH:DEL-LAY ( doc name / layer )
  2.     (if
  3.         (null
  4.             (vl-catch-all-error-p
  5.                 (setq layer
  6.                     (vl-catch-all-apply 'vla-item (list (vla-get-layers doc) name))
  7.                 )
  8.             )
  9.         )
  10.         (progn
  11.             (vl-catch-all-apply 'vla-put-freeze (list layer :vlax-false))
  12.             (vla-put-lock layer :vlax-false)
  13.             (vla-put-layeron layer :vlax-true)
  14.  
  15.             (vlax-for block (vla-get-blocks doc)
  16.                 (vlax-for obj block
  17.                     (if (eq name (vla-get-layer obj)) (vla-delete obj))
  18.                 )
  19.             )
  20.             (vl-catch-all-apply 'vla-delete (list layer))
  21.             t ;; Success!
  22.         )
  23.     )
  24. )
  25.  
  26. (defun ARCH:DEL-LAY-DBX ( doc )
  27.     (foreach layer '("A-REVS-CLOD" "A-REVS-SYMB")
  28.         (ARCH:DEL-LAY doc layer)
  29.     )
  30. )
  31.  
  32. (defun C:TEST ( )
  33.     (ARCH:LOAD (strcat ARCH#UTIF "ObjectDBXBase.lsp"))
  34.     (LM:ODBX 'ARCH:DEL-LAY-DBX nil t)
  35. )

But that is MASSIVELY inefficient, since every object in the drawing is being iterated for every layer.

Instead, consider this variation:

Code - Auto/Visual Lisp: [Select]
  1. (defun ARCH:DEL-LAYS ( doc layers / dbxlayers layer objlst )
  2.     (setq dbxlayers (vla-get-layers doc))
  3.     (foreach name layers
  4.         (if
  5.             (null
  6.                 (vl-catch-all-error-p
  7.                     (setq layer
  8.                         (vl-catch-all-apply 'vla-item (list dbxlayers name))
  9.                     )
  10.                 )
  11.             )
  12.             (progn
  13.                 (vl-catch-all-apply 'vla-put-freeze (list layer :vlax-false))
  14.                 (vla-put-lock layer :vlax-false)
  15.                 (vla-put-layeron layer :vlax-true)
  16.                 (setq objlst (cons layer objlst))
  17.             )
  18.         )
  19.     )
  20.     (if objlst ;; If at least one of the layers exists
  21.         (progn
  22.             (vlax-for block (vla-get-blocks doc)
  23.                 (vlax-for obj block
  24.                     (if (member (vla-get-layer obj) layers) (vla-delete obj))
  25.                 )
  26.             )
  27.             (foreach layer objlst
  28.                 (vl-catch-all-apply 'vla-delete (list layer))
  29.             )
  30.             t ;; Success for at least one of the layers
  31.         )
  32.     )
  33. )
  34.  
  35. (defun ARCH:DEL-LAYS-DBX ( doc )
  36.     (ARCH:DEL-LAYS doc '("A-REVS-CLOD" "A-REVS-SYMB"))
  37. )
  38.  
  39. (defun C:TEST ( )
  40.     (ARCH:LOAD (strcat ARCH#UTIF "ObjectDBXBase.lsp"))
  41.     (LM:ODBX 'ARCH:DEL-LAYS-DBX nil t)
  42. )

Again, both are untested.

GDF

  • Water Moccasin
  • Posts: 2081
Re: Delete a layer using objectdbx
« Reply #6 on: January 24, 2012, 04:02:38 PM »
Lee

Just tried out your modified "Instead, consider this variation" version.
It works beautifully. Thanks again.

I use theis routine to remove all revision clouds and delta symbols from a old job that is being reused and renamed.
A GREAT time saver and way much faster than using a script file.

Thanks again for posting your "ObjectDBX Wrapper" routine. I will be using it one more in the future, when I come up with other needs to clean up whole directories of drawings.

Gary






I can sit back a have a cold one now!
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: 12905
  • London, England
Re: Delete a layer using objectdbx
« Reply #7 on: January 24, 2012, 04:47:16 PM »
Just tried out your modified "Instead, consider this variation" version.
It works beautifully. Thanks again.

That's great Gary, I'm really pleased that the ObjectDBX Wrapper is of use to you  :-)

Enjoy your beverage  8-)

saycaphe

  • Guest
Re: Delete a layer using objectdbx
« Reply #8 on: June 26, 2012, 01:34:56 AM »
Hi guys.

I try to use the ObjectDBX Wrapper and code that Lee-Mac posted above to delete layers name "tekst" "markering" in DXF files, but it does not work.

Can you help me to make it work with there DXF file i've uploaded.

Thanks :)

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Delete a layer using objectdbx
« Reply #9 on: June 26, 2012, 06:45:42 AM »
I try to use the ObjectDBX Wrapper and code that Lee-Mac posted above to delete layers name "tekst" "markering" in DXF files, but it does not work.

AFAIK, you cannot use ObjectDBX to interface with DXF files, only with DWG / DWT / DWS.

BlackBox

  • King Gator
  • Posts: 3770
Re: Delete a layer using objectdbx
« Reply #10 on: June 26, 2012, 05:15:00 PM »
For those that may not already have it:

Autodesk // Labs_ | ScriptPro for AutoCAD
"How we think determines what we do, and what we do determines what we get."

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Delete a layer using objectdbx
« Reply #11 on: August 10, 2015, 04:58:59 AM »
434939575,
Please do not use the Report to Moderator system to ask questions ... just post your question in the thread.

Quote
The reporter has made the following comment:
Friend, can I DBX delete a layer? My English is not good!

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

azi

  • Guest
Re: Delete a layer using objectdbx
« Reply #12 on: November 14, 2017, 03:47:28 AM »

Hi Gary

Please explain how to use it


Many Thanks in advance