Lee Mac Posted January 19, 2009 Posted January 19, 2009 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. Any help in this matter is appreciated as always, and I thank you for your time spent reading this post. Cheers Lee Quote
ReMark Posted January 19, 2009 Posted January 19, 2009 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? Quote
Lee Mac Posted January 19, 2009 Author Posted January 19, 2009 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 EDIT: Can't find the delete layer LISP in the Express Tools Quote
VovKa Posted January 19, 2009 Posted January 19, 2009 (vl-catch-all-apply 'vla-Delete (list (vlax-ename->vla-object (tblobjname "LAYER" "Layerx")) ) ) Quote
Lee Mac Posted January 19, 2009 Author Posted January 19, 2009 Fantastic VovKa, Thank you for your time. Quote
ASMI Posted January 19, 2009 Posted January 19, 2009 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 Quote
Lee Mac Posted January 19, 2009 Author Posted January 19, 2009 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 Quote
ASMI Posted January 19, 2009 Posted January 19, 2009 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. Quote
Lee Mac Posted January 19, 2009 Author Posted January 19, 2009 Thanks ASMI for the explanation - your time is much appreciated. Cheers Lee Quote
Recommended Posts
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.