{"id":1889,"date":"2019-04-21T10:26:26","date_gmt":"2019-04-21T17:26:26","guid":{"rendered":"https:\/\/blogs.plm.automation.siemens.com\/t5\/Solid-Edge-Blog\/Extension-Methods-in-the-Solid-Edge-API\/ba-p\/587333"},"modified":"2026-03-26T07:29:03","modified_gmt":"2026-03-26T11:29:03","slug":"extension-methods-in-the-solid-edge-api","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/solidedge\/extension-methods-in-the-solid-edge-api\/","title":{"rendered":"Extension Methods in the Solid Edge API"},"content":{"rendered":"<p><P><STRONG><SPAN>Prolog<\/SPAN><\/STRONG><\/P><\/p>\n<p><P><SPAN>Maintaining a library of commonly used functions and re-using them across projects is one of the best practices of programming especially with Solid Edge.<\/SPAN><\/P><br \/>\n<P><SPAN>&nbsp;&nbsp;<\/SPAN><\/P><br \/>\n<P><SPAN>Did you know it is possible to add methods to both intrinsic and Solid Edge objects and call them the same way as the built-in methods? For example, if you frequently use a function to convert an angle in degrees to radians by calling a function similar to the one below:<\/SPAN><\/P><\/p>\n<p><P><SPAN>&nbsp;<span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 532px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/EB04-1.png\" alt=\"EB04.png\" title=\"EB04.png\" \/><\/span><\/SPAN><\/P><\/p>\n<p><P><SPAN>Then it can be easily restructured and called as an intrinsic or built-in function as below:<\/SPAN><\/P><\/p>\n<p><P><SPAN>&nbsp;<span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 454px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/EB05-1.png\" alt=\"EB05.png\" title=\"EB05.png\" \/><\/span><\/SPAN><\/P><\/p>\n<p><P><SPAN>It is possible to add a method to the Double data type or class such that it appears in the IntelliSense as shown below:<\/SPAN><\/P><\/p>\n<p><P><SPAN> <span class=\"lia-inline-image-display-wrapper lia-image-align-center\" style=\"width: 392px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/EB10-1.png\" alt=\"EB10.png\" title=\"EB10.png\" \/><\/span><\/SPAN><\/P><\/p>\n<p><P><SPAN>and call it directly on the variables without the need to pass arguments.<\/SPAN><\/P><\/p>\n<p><P><STRONG><SPAN>Definition<\/SPAN><\/STRONG><\/P><\/p>\n<p><P><SPAN>Extension methods enable you to create a method in a module decorated with an attribute and have that method appear as if it was an instance method defined on another type. <\/SPAN><\/P><\/p>\n<p><P><SPAN>To create such extension methods to both .Net and Solid Edge objects, follow these steps:<\/SPAN><\/P><\/p>\n<p><P><STRONG><SPAN>VB.Net<\/SPAN><\/STRONG><\/P><\/p>\n<p><P>1. Add a module to your project and include the 2 statements as shown in the image below:<\/P><br \/>\n<P><SPAN><SPAN><span class=\"lia-inline-image-display-wrapper lia-image-align-center\" style=\"width: 595px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/EB01-1.png\" alt=\"EB01.png\" title=\"EB01.png\" \/><\/span><\/SPAN><\/SPAN><\/P><\/p>\n<p><P><SPAN>2. Now in any other module of the project, for example, the main module, you may start using the extension methods as below:<\/SPAN><\/P><br \/>\n<P><SPAN>&nbsp;<\/SPAN><SPAN>&nbsp;<\/SPAN><\/P><br \/>\n<P><SPAN><span class=\"lia-inline-image-display-wrapper lia-image-align-left\" style=\"width: 514px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/EB02-1.png\" alt=\"EB02.png\" title=\"EB02.png\" \/><\/span><\/SPAN><\/P><\/p>\n<p><P><SPAN>Note how the extension method can be called directly on the variable instead of passing the variable as an argument to the function.<\/SPAN><\/P><\/p>\n<p><P><SPAN>This, however, does not mean you must write all of the extension methods in every project. Create a class library and compile it as a DLL and simply referring to the library in the project will do the job.<\/SPAN><\/P><\/p>\n<p><P><SPAN>Let&#8217;s see how to create a library and also how to create extension methods using CSharp both in the next step.<\/SPAN><\/P><\/p>\n<p><P><STRONG><SPAN>CSharp<\/SPAN><\/STRONG><\/P><br \/>\n<P><STRONG><SPAN>&nbsp;<\/SPAN><\/STRONG><\/P><br \/>\n<P>1. Create a class library in a CSharp<\/P><\/p>\n<p><P><SPAN><span class=\"lia-inline-image-display-wrapper lia-image-align-center\" style=\"width: 537px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/EB03-1.png\" alt=\"EB03.png\" title=\"EB03.png\" \/><\/span><\/SPAN><\/P><\/p>\n<p><P><SPAN>2.&nbsp;<\/SPAN>Add a public static class:<\/P><\/p>\n<p><P><SPAN> <span class=\"lia-inline-image-display-wrapper lia-image-align-center\" style=\"width: 600px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/EB06-1.png\" alt=\"EB06.png\" title=\"EB06.png\" \/><\/span><\/SPAN><\/P><\/p>\n<p><P><SPAN>3.&nbsp;<\/SPAN>Note that the methods are also static.<\/P><\/p>\n<p><P>4. Finally, note the keyword <STRONG>this<\/STRONG> in the function argument, which denotes the method is an extension method for the data type or class following it.<\/P><\/p>\n<p><P><SPAN>Compile the project and reference the library generated in either a CSharp or VB.Net project from the pulldown menu Project &#8211; Add Reference.<\/SPAN><\/P><\/p>\n<p><P><SPAN>In the Reference Manager dialog, select &#8216;Browse Category&#8217; in the left pane and then click the Browse&#8230; button at the bottom.<\/SPAN><\/P><\/p>\n<p><P><STRONG><SPAN>Extension Method Rules:<\/SPAN><\/STRONG><\/P><\/p>\n<p><OL><\/p>\n<li>The type of the first parameter is the data type that is being extended.<\/li>\n<li>Return type could be any. <\/li>\n<li>Need not be a returning function &#8211; a Sub\/void is OK.<\/li>\n<li>&lt;Extension()&gt; attribute should be specified in VB.Net.<\/li>\n<li>The <STRONG>this<\/STRONG> keyword should be used in CSharp.<\/li>\n<li>static class and static methods in CSharp.<\/li>\n<p><\/OL><br \/>\n<P><SPAN><span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 30px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/TipIcon-43.png\" alt=\"TipIcon.png\" title=\"TipIcon.png\" \/><\/span>&nbsp;One way to \u2018extend\u2019 a class is to create a new class that inherits the old one. This doesn&#8217;t work if the class is &#8216;sealed&#8217;; that means it can&#8217;t be inherited.<\/SPAN><\/P><\/p>\n<p><P><STRONG><SPAN>Extension Methods in Solid Edge <\/SPAN><\/STRONG><\/P><\/p>\n<p><P><SPAN>Extension methods can be added to Solid Edge objects too. For example, the Layer object.<\/SPAN><\/P><\/p>\n<p><P><SPAN>Here are two functions <\/SPAN><EM>ShowOrHideInViews<\/EM> and <EM>ShowOrHideEverywhere<\/EM> <SPAN>that extend the existing functions list for the Layer object and show up in the IntelliSense for a Layer object:<\/SPAN><\/P><\/p>\n<p><PRE><STRONG>&lt;Extension()&gt;<\/STRONG><br \/>\nPublic Sub ShowOrHideInViews(ByVal oLayer As SolidEdgeFramework.Layer, ByVal bShowOrHide As Boolean)<br \/>\n  Dim oDoc As DraftDocument = CType(oLayer.Parent, DraftDocument)<br \/>\n  Dim sLayerName As String = oLayer.Name<br \/>\n  Dim oSheet As Sheet = oDoc.ActiveSheet<\/p>\n<p>  For Each oView As DrawingView In oSheet.DrawingViews<br \/>\n    If bShowOrHide = True Then<br \/>\n\toView.ShowLayer(sLayerName)<br \/>\n\tElseIf bShowOrHide = False Then<br \/>\n\t  oView.HideLayer(sLayerName)<br \/>\n\tEnd If<br \/>\n  Next<br \/>\nEnd Sub<\/p>\n<p><STRONG>&lt;Extension()&gt;<\/STRONG><br \/>\nPublic Sub ShowOrHideEverywhere(ByVal oLayer As SolidEdgeFramework.Layer, ByVal bShowOrHide As Boolean)<br \/>\n  Dim oDoc As SolidEdgeDraft.DraftDocument = CType(oLayer.Parent, SolidEdgeDraft.DraftDocument)<br \/>\n  Dim sLayerName As String = oLayer.Name<br \/>\n  Dim oSections As SolidEdgeDraft.Sections = oDoc.Sections<br \/>\n  Dim oSection As SolidEdgeDraft.Section = oSections.WorkingSection<br \/>\n  Dim oSheets As SolidEdgeDraft.SectionSheets = oSection.Sheets<\/p>\n<p>  For Each oSheet As SolidEdgeDraft.Sheet In oSheets<br \/>\n    If bShowOrHide Then<br \/>\n\toSheet.Layers.Item(sLayerName).ShowInContext(oSheet)<br \/>\n    Else<br \/>\n\toSheet.Layers.Item(sLayerName).HideInContext(oSheet)<br \/>\n    End If<\/p>\n<p>    For Each oView As DrawingView In oSheet.DrawingViews<br \/>\n\tIf bShowOrHide = True Then<br \/>\n\t  oView.ShowLayer(sLayerName)<br \/>\n\tElseIf bShowOrHide = False Then<br \/>\n\t  oView.HideLayer(sLayerName)<br \/>\n\tEnd If<br \/>\n    Next<br \/>\n  Next<br \/>\nEnd Sub<\/PRE><\/p>\n<p><P>The methods follow all the rules mentioned above and add to the Layer object&#8217;s functions as below:<\/P><\/p>\n<p><P><span class=\"lia-inline-image-display-wrapper lia-image-align-center\" style=\"width: 369px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/EB09-1.png\" alt=\"EB09.png\" title=\"EB09.png\" \/><\/span><\/P><\/p>\n<p><P><span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 30px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/TipIcon-43.png\" alt=\"TipIcon.png\" title=\"TipIcon.png\" \/><\/span>&nbsp;Observe that in the image above, extension methods have a downward pointing blue arrow in their icon.<\/P><\/p>\n<p><P><span class=\"lia-inline-image-display-wrapper lia-image-align-center\" style=\"width: 255px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/EB07-1.png\" alt=\"EB07.png\" title=\"EB07.png\" \/><\/span><\/P><\/p>\n<p><P>After calling the ShowOrHideEverywhere method, the Layers panel in the Edgebar appears like this, where the layer &#8216;Dimensions&#8217; is hidden in all drawing views:<\/P><\/p>\n<p><P><STRONG><span class=\"lia-inline-image-display-wrapper lia-image-align-center\" style=\"width: 223px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/EB08-1.png\" alt=\"EB08.png\" title=\"EB08.png\" \/><\/span><\/STRONG><\/P><\/p>\n<p><P><span class=\"lia-inline-image-display-wrapper lia-image-align-inline\" style=\"width: 30px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/TipIcon-43.png\" alt=\"TipIcon.png\" title=\"TipIcon.png\" \/><\/span>&nbsp;<STRONG>Note:<\/STRONG> The Visual Studio 2015 project files are attached to this article. Note the three modules:<\/P><br \/>\n<P>Myextensions.vb<\/P><br \/>\n<P>Form1 [Design]<\/P><br \/>\n<P>Form1 .vb<\/P><\/p>\n<p><P><STRONG><SPAN>Advantages of Extension Methods:<\/SPAN><\/STRONG><\/P><\/p>\n<p><OL><\/p>\n<li>Changes the syntax for calling a method.<\/li>\n<li>It makes a non-Class method look like a Class method. <\/li>\n<li>Clearer Syntax.<\/li>\n<li>Simpler programs that are easier to read.<\/li>\n<p><\/OL><\/p>\n<p><P><STRONG><SPAN>Summary of Extension Methods in Solid Edge<\/SPAN><\/STRONG><\/P><\/p>\n<p><OL><\/p>\n<li>Extension methods, as the name suggests, are additional methods.<\/li>\n<li>Extension methods allow you to inject additional methods without modifying, deriving or recompiling the original class, struct or interface.<\/li>\n<li>Extension methods can be added to your own custom class, .NET framework classes, third-party classes or interfaces and to Solid Edge objects.<\/li>\n<p><\/OL><\/p>\n<p><P><SPAN>Tushar Suradkar<\/SPAN><\/P><br \/>\n<P><SPAN><a href=\"http:\/\/www.cadvertex.com\/\" target=\"_blank\" rel=\"noopener nofollow noreferrer\">www.CADVertex.com<\/A><\/SPAN><\/P><br \/>\n<P><a href=\"http:\/\/www.surfandcode.in\/2014\/01\/index-of-all-tutorials-on-this-solid.html\" target=\"_blank\" rel=\"noopener nofollow noreferrer\"><SPAN>www.SurfAndCode.IN<\/SPAN><\/A><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;<br \/>\n  Prolog  <\/p>\n<p>  Maintaining a library of commonly used functions and re-using them across projects is one of the best practices of programming especially with Solid Edge.<br \/>\n  &#8230;<\/p>\n","protected":false},"author":42979,"featured_media":1925,"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":[],"industry":[],"product":[],"coauthors":[],"class_list":["post-1889","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","category-tips-tricks"],"featured_image_url":"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/TipIcon-45.png","_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/posts\/1889","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=1889"}],"version-history":[{"count":12,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/posts\/1889\/revisions"}],"predecessor-version":[{"id":1926,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/posts\/1889\/revisions\/1926"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/media\/1925"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/media?parent=1889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/categories?post=1889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/tags?post=1889"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/industry?post=1889"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/product?post=1889"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/coauthors?post=1889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}