{"id":5081,"date":"2014-08-17T23:40:27","date_gmt":"2014-08-18T06:40:27","guid":{"rendered":"https:\/\/blogs.plm.automation.siemens.com\/t5\/Solid-Edge-Blog\/Automation-in-the-Draft-Environment-Layer-Handling\/ba-p\/266339"},"modified":"2026-03-26T07:30:17","modified_gmt":"2026-03-26T11:30:17","slug":"automation-in-the-draft-environment-layer-handling","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/solidedge\/automation-in-the-draft-environment-layer-handling\/","title":{"rendered":"Automation in the Draft Environment: Layer Handling"},"content":{"rendered":"<p><P>For those who have done some LISP programming for old generation CAD programs, coding was fun. LISP punched lots of power in few keywords or lines. Moreover the commands could be issued to the CAD program in the same way as done interactively.<\/P><\/p>\n<p><P>The downside was the cryptic syntax and un-intuitive bug fixing methods where the user relied more on his gut feeling to determine if a line of code would work. The humble Notepad was a programmer&#8217;s most trusted friend.<\/P><\/p>\n<p><P>The advent of a new wave of 3D CAD programs in late 90&#8217;s turned a new leaf as also new programming techniques evolved viz. .Net which came with development kits (SDK) equipped with color-coded syntax, modern debugging tools, etc. One such member of the .Net family is VB.Net<\/P><\/p>\n<p><P>The most appealing feature of VB.Net is the natural language syntax and this article shows how to build a layer purging utility using VB.Net in the Solid Edge Draft environment. The dictionary meaning of Purge is to get rid of unwanted stuff. Lets understand this in the context of layers.<\/P><\/p>\n<p><P>Create a Draft document with several layers <span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 29px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/01-9.png\" alt=\"01.png\" title=\"01.png\" \/><\/span> created using the New Layer button <span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 22px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/02-9.png\" alt=\"02.png\" title=\"02.png\" \/><\/span><\/P><\/p>\n<p><P><span class=\"lia-inline-image-display-wrapper lia-image-align-left\" style=\"width: 175px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/03-11.png\" alt=\"03.png\" title=\"03.png\" \/><\/span>Note that layer icons with a red mark <span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 18px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/04-7.png\" alt=\"04.png\" title=\"04.png\" \/><\/span> have objects drawn on them &#8211; hence cannot be deleted.<\/P><\/p>\n<p><P>Layer with a pencil mark&nbsp;<span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 18px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/05-5.png\" alt=\"05.png\" title=\"05.png\" \/><\/span> is the active layer and also cannot be deleted.<\/P><\/p>\n<p><P>The remaining layers which are empty are redundant and can be deleted. I have not been able to determine how much of an overhead these empty layers are on the file size, but getting rid of them has been a popular idea over years and in a built-in command in 2D-only CAD programs, most notably AutoCAD.<\/P><\/p>\n<p><P><span class=\"lia-inline-image-display-wrapper lia-image-align-right\" style=\"width: 205px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/06-5.png\" alt=\"06.png\" title=\"06.png\" \/><\/span>Switch to Visual Studio if it is already open and create a new Console Application since this program needs no user interaction.<\/P><\/p>\n<p><P>Since this macro would run in just the Draft environment, adding references to just the SolidEdgeFrameWork, SolidEdgeFrameWorkSupport, Draft and Constants libraries would suffice.<\/P><\/p>\n<p><P>Refer some&nbsp;<a href=\"https:\/\/blogs.plm.automation.siemens.com\/t5\/Solid-Edge-Blog\/Automation-Part-Configurator-3\/ba-p\/263249\" target=\"_blank\" rel=\"noopener noreferrer\">previous articles in the Automation series<\/A> to see how to add Solid Edge references to a project in Visual Studio.<\/P><\/p>\n<p><P>Your few initial lines of code would look like these:<\/P><\/p>\n<p><PRE>Imports System.Runtime.InteropServices<\/p>\n<p>Module Module1<\/p>\n<p>  Sub Main()<br \/>\n    Dim oApp As SolidEdgeFramework.Application = Marshal.GetActiveObject(&#8220;SolidEdge.Application&#8221;)<\/p>\n<p>    Dim oDoc As SolidEdgeDraft.DraftDocument = oApp.ActiveDocument<br \/>\n  End Sub<\/p>\n<p>End Module<\/PRE><\/p>\n<p><P>To these, add the Layers object as below:<\/P><\/p>\n<p><P><span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 478px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/07-5.png\" alt=\"07.png\" title=\"07.png\" \/><\/span><\/P><\/p>\n<p><P>Note that the layers being addressed are only the ones in the currently active sheet unlike in AutoCAD where all layers belong to the drawing document.<\/P><\/p>\n<p><P>This is followed by running a For Each loop which goes door-to-door each layer and checks if it is empty and simply deletes it. So simple and in a natural language syntax that needs no further explanation:<\/P><\/p>\n<p><PRE>Sub Main()<br \/>\n  Dim oApp As SolidEdgeFramework.Application = Marshal.GetActiveObject(&#8220;SolidEdge.Application&#8221;)<\/p>\n<p>  Dim oDoc As SolidEdgeDraft.DraftDocument = oApp.ActiveDocument<\/p>\n<p>  Dim oLayers As SolidEdgeFramework.Layers = oDoc.ActiveSheet.Layers<\/p>\n<p><STRONG>  For Each oLayer As SolidEdgeFramework.Layer In oLayers<br \/>\n    If oLayer.IsEmpty Then<br \/>\n      oLayer.Delete()<br \/>\n    End If<br \/>\n  Next<\/STRONG><br \/>\nEnd Sub<\/PRE><\/p>\n<p><P>The line for deleting the layer can as well be written:<\/P><br \/>\n<P><span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 351px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/08-3.png\" alt=\"08.png\" title=\"08.png\" \/><\/span><\/P><\/p>\n<p><P>Such natural language syntax has lured several generations of casual users of CAD and Office program equally into taking up VB programming.<\/P><\/p>\n<p><P>&#8216;Play&#8217; the program by pressing F5 or the Run (Start Debugging) <span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 28px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/09-3.png\" alt=\"09.png\" title=\"09.png\" \/><\/span> icon. Such features fade the distinction between a sophisticated programming tool and your music player. Programming has never been so easy and fun. Don&#8217;t you agree ?<\/P><\/p>\n<p><P>The program would run into an error for it just checks if the layer is empty and not if the layer is active. If you too have created the layers casually as shown in image earlier in the article, Layer5 is the active layer but is not empty.<\/P><\/p>\n<p><P>To rectify. either create a sketch on this layer or add the following line at the beginning of the code:<\/P><\/p>\n<p><P>&nbsp;<span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 476px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/10-12.png\" alt=\"10.png\" title=\"10.png\" \/><\/span><\/P><\/p>\n<p><P>This sure is a quick-and-dirty way of fixing the code but a useful feature of VB is you want to quickly get up-and-running with your little macro.<\/P><\/p>\n<p><P>This leaves plenty of room to explore more on the VB and the Solid Edge fronts and make the utility more robust and less error prone.<\/P><\/p>\n<p><P>For now, build the macro and the exe will be lying in the BinDebug folder where you saved the project. Share it with your colleagues and flaunt your programming skills for you just added a new command to Solid Edge !<\/P><\/p>\n<p><P>~ Tushar Suradkar<\/P><br \/>\n<P><a href=\"http:\/\/surfandcode.blogspot.in\/2014\/01\/index-of-all-tutorials-on-this-solid.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">http:\/\/www.surfandcode.in<\/A><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For those who have done some LISP programming for old generation CAD programs, coding was fun. LISP punched lots of power in few keywords or lines. Moreover the commands could be issued to the CAD pr&#8230;<\/p>\n","protected":false},"author":42979,"featured_media":5112,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1,96],"tags":[33],"industry":[],"product":[],"coauthors":[],"class_list":["post-5081","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","category-tips-tricks","tag-developer"],"featured_image_url":"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/10-12.png","_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/posts\/5081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/users\/42979"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/comments?post=5081"}],"version-history":[{"count":11,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/posts\/5081\/revisions"}],"predecessor-version":[{"id":5113,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/posts\/5081\/revisions\/5113"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/media\/5112"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/media?parent=5081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/categories?post=5081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/tags?post=5081"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/industry?post=5081"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/product?post=5081"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/coauthors?post=5081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}