Jump to content

Lisp to delete specific layers from file.


Catwoman

Recommended Posts

Hello,

 

I am looking for a LISP routine or instructions on how to create one that will delete (LAYDEL) specific layer names from the .dwg file.

Any samples or assistance would be greatly appreciated, as I am new to writing lisp routines.

Thank you!

 

Link to comment
Share on other sites

Do you want it to just delete the layer with out checking to see if anything is on it?

(defun C:DeleteLayer ()
  (vl-cmdf "_.Purge" "_LA" "layername" "_N")
  (princ)
)

 

Change "layername" to the layer you want to delete. purged if empty.

 

--EDIT---

 

Purging multiple layers by adding a , between layer names

(vl-cmdf "_.Purge" "_LA" "layer1,layer2,layer3,layer4,layer5" "_N")
or
(setq layer "layer1,layer2,layer3,layer4,layer5")
(vl-cmdf "_.Purge" "_LA" layer "_N")

 

Edited by mhupp
Link to comment
Share on other sites

@mhupp Thank you! 

The purge command is restricted to layers with no objects on them.  I need to use the command LAYDEL in order to remove layers including elements on those layers, but it does not appear to allow me to delete multiple layers at one time.

 

(defun C:delLayer ()
  (vl-cmdf "_.laydel" "_Name" "S-WALL,S-WALL-BRNG,S-WALL-MISC,S-WALL-NBRG,S-WALL-OPEN,S-WALL-SHEA,1st_Floor_Dimensions,1st_Floor_Annotations,2nd_Floor_Dimensions,2nd_Floor_Annotations" "OK" "_deletelayer")
  (princ)
)

 

Thank you for your assistance.

c

 

Link to comment
Share on other sites

Forgot about that. This will delete everything on said layers then purge them.

 

(defun C:DeleteLayer (/ layer)
  (vl-load-com)
  (setq layer "S-WALL,S-WALL-BRNG,S-WALL-MISC,S-WALL-NBRG,S-WALL-OPEN,S-WALL-SHEA,1st_Floor_Dimensions,1st_Floor_Annotations,2nd_Floor_Dimensions,2nd_Floor_Annotations")
  (vlax-for ent (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) ;deletes all objects on above layers
    (vlax-for obj ent                                                            ;regardless of where they are in the drawing.
      (if (wcmatch (vla-get-layer obj) layer)
        (vl-catch-all-apply (vla-delete obj))
      )
    )
  )
  ;(setvar 'clayer  "0") ;change to a layer not getting deleted.
  (vl-cmdf "_.Purge" "_LA" layer "_N") ;purges layers listed above
  (princ)
)

 

You could build a selection set and erase them, then purge. this would only work if everything is on the same tab like model space. if something was on another tab it wouldn't get deleted.

 

(setq layer "S-WALL,S-WALL-BRNG,S-WALL-MISC,S-WALL-NBRG,S-WALL-OPEN,S-WALL-SHEA,1st_Floor_Dimensions,1st_Floor_Annotations,2nd_Floor_Dimensions,2nd_Floor_Annotations")
(setq SS (ssget "_X" (list (cons 8 layer))))
(vl-cmdf "_.Erase" SS "")
(vl-cmdf "_.Purge" "_LA" layer "_N")

 

 

Edited by mhupp
Link to comment
Share on other sites

4 hours ago, mhupp said:

You could build a selection set and erase them, then purge. this would only work if everything is on the same tab like model space. if something was on another tab it wouldn't get deleted.

Using entdel would work though. Using SSGET X will get everything in the drawing. It's that when calling using commands that the selection set gets filtered to the current tab. Entdel or vla-delete will still take care of it.

 

I also had to deal with this problem in my workplace and I came up with some dirty way to go about it though (it wasn't a good practice, but at least good enough to get it working for me). I gave up being very precise, because there's one key factor that I could not yet still solve. One factor you'd still need to check is if the layer is locked or frozen before deleting anything. But the worst factor is checking if you have any blocks that has objects currently using that layer and then purging that block first, and it gets worse with nested blocks. With nested blocks, you need to purge the inner block first before being able to purge the outer one, so finding that sequence (and blocks) matters as well. Otherwise entdel or vla-delete will either yield an error, or won't delete the object, which then the layer won't be deleted.

 

Your DeleteLayer command gave me an idea though... I might try implementing some stuff.

Edited by Jonathan Handojo
  • Agree 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...