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:
Then it can be easily restructured and called as an intrinsic or built-in function as below:
It is possible to add a method to the Double data type or class such that it appears in the IntelliSense as shown below:
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:
2. Now in any other module of the project, for example, the main module, you may start using the extension methods as below:
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
2. Add a public static class:
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:
- The type of the first parameter is the data type that is being extended.
- Return type could be any.
- Need not be a returning function – a Sub/void is OK.
- <Extension()> attribute should be specified in VB.Net.
- The this keyword should be used in CSharp.
- static class and static methods in CSharp.
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.ActiveSheetFor 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.SheetsFor Each oSheet As SolidEdgeDraft.Sheet In oSheets
If bShowOrHide Then
oSheet.Layers.Item(sLayerName).ShowInContext(oSheet)
Else
oSheet.Layers.Item(sLayerName).HideInContext(oSheet)
End IfFor 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:
Observe that in the image above, extension methods have a downward pointing blue arrow in their icon.
After calling the ShowOrHideEverywhere method, the Layers panel in the Edgebar appears like this, where the layer ‘Dimensions’ is hidden in all drawing views:
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:
- Changes the syntax for calling a method.
- It makes a non-Class method look like a Class method.
- Clearer Syntax.
- Simpler programs that are easier to read.
Summary of Extension Methods in Solid Edge
- Extension methods, as the name suggests, are additional methods.
- Extension methods allow you to inject additional methods without modifying, deriving or recompiling the original class, struct or interface.
- 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
Comments