Products

Extension Methods in the Solid Edge API

Prolog

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.


  


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:

 EB04.png

Then it can be easily restructured and called as an intrinsic or built-in function as below:

 EB05.png

It is possible to add a method to the Double data type or class such that it appears in the IntelliSense as shown below:

EB10.png

and call it directly on the variables without the need to pass arguments.

Definition

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.

To create such extension methods to both .Net and Solid Edge objects, follow these steps:

VB.Net

1. Add a module to your project and include the 2 statements as shown in the image below:


EB01.png

2. Now in any other module of the project, for example, the main module, you may start using the extension methods as below:


  


EB02.png

Note how the extension method can be called directly on the variable instead of passing the variable as an argument to the function.

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.

Let’s see how to create a library and also how to create extension methods using CSharp both in the next step.

CSharp


 


1. Create a class library in a CSharp

EB03.png

2. Add a public static class:

EB06.png

3. Note that the methods are also static.

4. Finally, note the keyword this in the function argument, which denotes the method is an extension method for the data type or class following it.

Compile the project and reference the library generated in either a CSharp or VB.Net project from the pulldown menu Project – Add Reference.

In the Reference Manager dialog, select ‘Browse Category’ in the left pane and then click the Browse… button at the bottom.

Extension Method Rules:

  1. The type of the first parameter is the data type that is being extended.
  2. Return type could be any.
  3. Need not be a returning function – a Sub/void is OK.
  4. <Extension()> attribute should be specified in VB.Net.
  5. The this keyword should be used in CSharp.
  6. static class and static methods in CSharp.

TipIcon.png One way to ‘extend’ a class is to create a new class that inherits the old one. This doesn’t work if the class is ‘sealed’; that means it can’t be inherited.

Extension Methods in Solid Edge

Extension methods can be added to Solid Edge objects too. For example, the Layer object.

Here are two functions ShowOrHideInViews and ShowOrHideEverywhere that extend the existing functions list for the Layer object and show up in the IntelliSense for a Layer object:

<Extension()>
Public Sub ShowOrHideInViews(ByVal oLayer As SolidEdgeFramework.Layer, ByVal bShowOrHide As Boolean)
Dim oDoc As DraftDocument = CType(oLayer.Parent, DraftDocument)
Dim sLayerName As String = oLayer.Name
Dim oSheet As Sheet = oDoc.ActiveSheet

For Each oView As DrawingView In oSheet.DrawingViews
If bShowOrHide = True Then
oView.ShowLayer(sLayerName)
ElseIf bShowOrHide = False Then
oView.HideLayer(sLayerName)
End If
Next
End Sub

<Extension()>
Public Sub ShowOrHideEverywhere(ByVal oLayer As SolidEdgeFramework.Layer, ByVal bShowOrHide As Boolean)
Dim oDoc As SolidEdgeDraft.DraftDocument = CType(oLayer.Parent, SolidEdgeDraft.DraftDocument)
Dim sLayerName As String = oLayer.Name
Dim oSections As SolidEdgeDraft.Sections = oDoc.Sections
Dim oSection As SolidEdgeDraft.Section = oSections.WorkingSection
Dim oSheets As SolidEdgeDraft.SectionSheets = oSection.Sheets

For Each oSheet As SolidEdgeDraft.Sheet In oSheets
If bShowOrHide Then
oSheet.Layers.Item(sLayerName).ShowInContext(oSheet)
Else
oSheet.Layers.Item(sLayerName).HideInContext(oSheet)
End If

For Each oView As DrawingView In oSheet.DrawingViews
If bShowOrHide = True Then
oView.ShowLayer(sLayerName)
ElseIf bShowOrHide = False Then
oView.HideLayer(sLayerName)
End If
Next
Next
End Sub

The methods follow all the rules mentioned above and add to the Layer object’s functions as below:

EB09.png

TipIcon.png Observe that in the image above, extension methods have a downward pointing blue arrow in their icon.

EB07.png

After calling the ShowOrHideEverywhere method, the Layers panel in the Edgebar appears like this, where the layer ‘Dimensions’ is hidden in all drawing views:

EB08.png

TipIcon.png Note: The Visual Studio 2015 project files are attached to this article. Note the three modules:


Myextensions.vb


Form1 [Design]


Form1 .vb

Advantages of Extension Methods:

  1. Changes the syntax for calling a method.
  2. It makes a non-Class method look like a Class method.
  3. Clearer Syntax.
  4. Simpler programs that are easier to read.

Summary of Extension Methods in Solid Edge

  1. Extension methods, as the name suggests, are additional methods.
  2. Extension methods allow you to inject additional methods without modifying, deriving or recompiling the original class, struct or interface.
  3. Extension methods can be added to your own custom class, .NET framework classes, third-party classes or interfaces and to Solid Edge objects.

Tushar Suradkar


www.CADVertex.com


www.SurfAndCode.IN

Tushar Suradkar

Solid Edge blogger, Programmer

More from this author

Comments

6 thoughts about “Extension Methods in the Solid Edge API

Leave a Reply

This article first appeared on the Siemens Digital Industries Software blog at https://blogs.sw.siemens.com/solidedge/extension-methods-in-the-solid-edge-api/