Jump to content

Deleting Layers in LISP


Lee Mac

Recommended Posts

Posted

This was brought to my attention via another thread on the forum, and, after conducting a search of the forum I can only find VBA solutions.

 

My question is how would one delete a layer?

 

Seems a simple one, (and I've probably missed something and so probably is...), but I can't quite work it out.

 

I have tried this:

 

(entdel (tblobjname "LAYER" "Layerx"))

 

But I didn't think this would work, and as you've probably guessed - it didn't. o:)

 

Any help in this matter is appreciated as always, and I thank you for your time spent reading this post. :)

 

Cheers

 

Lee

Posted

You can already do this through Express Tools and the Layer Delete command. It will wipe out everything on the layer then delete the layer itself. Is there any reason to reinvent the wheel or am I missing something here?

Posted

Well, it would need to be used within another LISP function - and express tools cannot be accessed whilst within another LISP.

 

But, tbh, I didn't actually know that tool existed, so I shall look at the LISP behind it and learn from it.

 

Thanks for the heads up ReMark :thumbsup:

 

EDIT: Can't find the delete layer LISP in the Express Tools :(

Posted
(vl-catch-all-apply
   'vla-Delete
   (list (vlax-ename->vla-object (tblobjname "LAYER" "Layerx"))
   )
 )

Posted

With some error messages:

 

(defun DeleteLayer(Name / layCol dLay oVal)
 (vl-load-com)
 (if
   (and
     (/= Name "0")
     (/= (strcat Name)(getvar "CLAYER"))
     ); end or
   (progn
     (setq layCol(vla-get-Layers
	    (vla-get-ActiveDocument
	      (vlax-get-acad-object))))
      (if(vl-catch-all-error-p
          (setq dLay(vl-catch-all-apply 'vla-Item
     (list layCol(strcat Name)))))
        (princ "\nLayer does not exist! ")
          (if(vl-catch-all-error-p
      (vl-catch-all-apply 'vla-Delete
         (list dLay)))
     (princ "\nCan't delete layer in use! ")
   (setq oVal T)
 ); end if
       ); end if
     ); end progn
   (princ "\nCan't delete active layer or layer \"0\"! ")
  ); end if
 oVal
); end of DeleteLayer

Posted

Many thanks for your code ASMI - there are many error trappings in there to help. :)

 

I have tried to dissect your code to understand the VL behind it (as I am endeavouring to learn VL now)...

 

Would you be so kind as to tell me if my annotations are correct, or correct me if I am wrong...

 

(defun DeleteLayer(Name / layCol dLay oVal)
 (vl-load-com)
 (if
   (and
     (/= Name "0") ; [b][color=Red]Check its not Layer 0[/color][/b]
     (/= (strcat Name)(getvar "CLAYER")) ; [b][color=Red]Check its not Current Layer[/color][/b]
     ); end or
   (progn
     (setq layCol(vla-get-Layers
           (vla-get-ActiveDocument
             (vlax-get-acad-object)))) ; [b][color=Red]Retrieve Current Layer Collection[/color][/b]
      (if(vl-catch-all-error-p
          (setq dLay(vl-catch-all-apply 'vla-Item
        (list layCol(strcat Name))))) ; [b][color=Red]Retrieve Layer Object Name from Layer Collection?[/color][/b][color=Red][i] [b][color=SeaGreen](same as tblobjname?)[/color][/b][/i][/color]
        (princ "\nLayer does not exist! ") ; [b][color=Red]If [i][color=SeaGreen]vl-catch-all-error-p[/color][/i] returns [color=Blue]T[/color], then Layer Doesn't Exist[/color][/b]
          (if(vl-catch-all-error-p
         (vl-catch-all-apply 'vla-Delete
            (list dLay))) [b][color=Red]; If Possible, Delete the Layer[/color][/b]
        (princ "\nCan't delete layer in use! ") ; [b][color=Red]If[/color][/b] [b][i][color=SeaGreen]vl-catch-all-error-p[/color][/i][color=ReD] returns[/color] [color=Blue]T[/color][color=Red], then Layer Cannot be Deleted. [/color][/b]
      (setq oVal T)
    ); end if
       ); end if
     ); end progn
   (princ "\nCan't delete active layer or layer \"0\"! ")
  ); end if
 oVal
); end of DeleteLayer

Posted

The function vl-catch-all-apply can return or some success value or error object #. It can be catched by vl-catch-all-error-p function or decoded with vl-catch-all-error-message function. Try in command line:

 

Command: (setq Val(vl-catch-all-apply '/ (list 2 0)))
#<%catch-all-apply-error%>

Command: (vl-catch-all-error-p Val)
T

Command: (vl-catch-all-error-message Val)
"divide by zero"

 

This design is often used to prevent errors with ActiveX objects, as in the case of an error the program was terminated.

Posted

Thanks ASMI for the explanation - your time is much appreciated. :)

 

Cheers

 

Lee

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...