SOLIDWORKS C# API - Fix Unit Issue
Objective
I want to:
- Fix Unit issue for input values using Solidworks C# API.
For more Details on why Fixing Unit issue, please visit 🚀 Solidworks Macro - Fix Unit Issue article.
- We will continue from previous article 🚀 WPF Tutorials - Create Line UI - Part 2 article.
Demo Video
Below 🎬 video shows how to “Fix Unit Issue” in Solidworks C# API.
Please note that there are no explanation in the video.
Explanation of each step and why we write code this way is given in this post.
Add [UnitConversionHelper] class
- First we add
Helper
folder in our solution. - Then we add
UnitConversionHelper
class inside “Helper
” folder. - Please see below 👇🏻 image for reference.
Add [Public Properties]
- First we need to create a region.
- Region Name: Public Properties
- Inside this region, we we need 2 properties.
- Please see below 👇🏻 for code reference.
#region Public Properties
public double LengthConversionFactor { get; set; }
public double AngleConversionFactor { get; set; }
#endregion
- In above code, we create 2 properties.
- Name of 1st property:
LengthConversionFactor
- Name of 2nd property:
AngleConversionFactor
- Type of both properties:
double
- Name of 1st property:
- We expose them as public members, so that we can use them in other classes.
Add [Public Method]
-
Now we need to add a method to
UnitConversionHelper
class. - First we need to create a region.
- Region Name: Public Method
- Inside this Public Method region, we need to create a method.
- Method Name:
UnitConversion
- Method Return Type:
void
[means this method did not return anything] - Method parameters:
- Name of parameter:
swUnit
- Type of parameter:
swLengthUnit_e
[Parameter forSolidworks Length Unit
in current document.]
- Name of parameter:
-
Method Purpose: Depending upon the value of
swUnit
parameter, we will modify the values ofLengthConversionFactor
andAngleConversionFactor
properties. - Please see below 👇🏻 for code reference.
#region Public Method
public void UnitConversion(swLengthUnit_e swUnit)
{
}
#endregion
- Please see below 👇🏻 image for reference.
Update Properties Value
- Now we need to update values of
LengthConversionFactor
andAngleConversionFactor
properties. - For this, we will use
if-else if
conditional statement. - Please see below 👇🏻 for code reference.
#region Public Method
public void UnitConversion(swLengthUnit_e swUnit)
{
if (swUnit == swLengthUnit_e.swMETER)
{
LengthConversionFactor = AngleConversionFactor = 1;
}
else if (swUnit == swLengthUnit_e.swMM)
{
LengthConversionFactor = 1 / 1000;
AngleConversionFactor = 1 * 0.01745329;
}
else if (swUnit == swLengthUnit_e.swCM)
{
LengthConversionFactor = 1 / 100;
AngleConversionFactor = 1 * 0.01745329;
}
else if (swUnit == swLengthUnit_e.swINCHES)
{
LengthConversionFactor = 1 * 0.0254;
AngleConversionFactor = 1 * 0.01745329;
}
else if (swUnit == swLengthUnit_e.swFEET)
{
LengthConversionFactor = 1 * (0.0254 * 12);
AngleConversionFactor = 1 * 0.01745329;
}
else if (swUnit == swLengthUnit_e.swFEETINCHES)
{
LengthConversionFactor = 1 * 0.0254;
AngleConversionFactor = 1 * 0.01745329;
}
}
#endregion
- In above code, we are checking value of
swUnit
parameter. - Depending upon the value of
swUnit
parameter, we will update values ofLengthConversionFactor
andAngleConversionFactor
properties accordingly.
Register [UnitConversionHelper]
- Now we need to register
UnitConversionHelper
class into DI container. - After registering class, we can create instance of
UnitConversionHelper
class through DI container. - For this, go to “
App.xaml.cs
” class. - In “
App.xaml.cs
” class, insideRegisterTypes
method, we registerUnitConversionHelper
method into DI container. - Please see below 👇🏻 for code reference.
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.Register<PointViewModel>();
containerRegistry.RegisterSingleton<UnitConversionHelper>();
}
- Please see below 👇🏻 image for reference.
- In above code, we register
UnitConversionHelper
class asSingleton
. - By doing this, we only has single instance of
UnitConversionHelper
class in our application.
This is it !!!
I hope my efforts will helpful to someone!
If you found anything to add or update, please let me know on my e-mail.
Hope this post helps you to fix Unit issue in Solidworks application.
If you like the post then please share it with your friends also.
Do let me know by you like this post or not!
Till then, Happy learning!!!