Target multiple SOLIDWORKS versions using SwEx framework
When SwEx.Framework library is installed via nuget package, SOLIDWORKS interop libraries are also installed. Framework referencing latest interop libraries in its project, allowing the users to use the latest versions of API with the newer versions of the SOLIDWORKS.
Despite latest interop is referenced, Framework is compatible with older versions of SOLIDWORKS. Minimum supported version is SOLIDWORKS 2012. In order to enable forward compatibility, but at the same time take a benefit of newer APIs in the newer SOLIDWORKS versions, framework implements fallback mechanism for the APIs it is using internally. It means if the certain API used by framework is not available in the target version of SOLIDWORKS, older version of API is used.
It is recommended to use similar technique and implement fallback APIs if your add-in if it needs to target multiple versions of SOLIDWORKS.
Availability of certain method can be found by exploring the corresponding section in the SOLIDWORKS API help documentation (both web and local version)
Use the ISldWorks::IsVersionNewerOrEqual extension method provided by framework to decide which API to use. For example the ICustomPropertyManager::Get6 method is only available in SOLIDWORKS 2018 SP0, while ICustomPropertyManager::Get5 is available in SOLIDWORKS 2014 SP0, and older ICustomPropertyManager::Get4 method is available from SOLIDWORKS 2011 SP4.
That means if we want to extract custom properties in our add-in and target all SOLIDWORKS versions starting from the SOLIDWORKS 2012, we need to write the code below:
Public Sub ReadDescriptionProperty()
Dim prpMgr = App.IActiveDoc2.Extension.CustomPropertyManager("")
Dim prpName = "Description"
Dim val As String = ""
Dim resVal As String = ""
If App.IsVersionNewerOrEqual(SwVersion_e.Sw2018) Then
Dim wasRes As Boolean
Dim linkToPrp As Boolean
prpMgr.Get6(prpName, False, val, resVal, wasRes, linkToPrp)
ElseIf App.IsVersionNewerOrEqual(SwVersion_e.Sw2014) Then
Dim wasRes As Boolean
prpMgr.Get5(prpName, False, val, resVal, wasRes)
Else
prpMgr.Get4(prpName, False, val, resVal)
End If
Logger.Log($"{prpName} = {resVal} [{val}]")
End Sub
public void GetTolerance(IDimension dim)
{
var dimTol = dim.Tolerance;
double maxTol;
double minTol;
if (App.IsVersionNewerOrEqual(SwVersion_e.Sw2015, 3))
{
dimTol.GetMinValue2(out minTol);
dimTol.GetMaxValue2(out maxTol);
}
else
{
minTol = dimTol.GetMinValue();
maxTol = dimTol.GetMaxValue();
}
}
Note. Although it is possible to simply use the oldest version of the method which corresponds to the minimum required SOLIDWORKS version as SOLIDWORKS supports backward compatibility, it is not recommended practice as newer version of methods might include critical bug fixes.
ISldWorks::IsVersionNewerOrEqual method also allows to check the minor version (e.g. Service Pack).
For example IDimensionTolerance::GetMinValue2 and IDimensionTolerance::GetMaxValue2 methods were added in SOLIDWORKS 2015 SP3, while previous implementation of this method is available since SOLIDWORKS 2006.
Note, that we cannot simply check if current SOLIDWORKS version is 2015 as the method only works in SP3 and we need to explicitly specify the service pack
Public Sub GetTolerance(ByVal [dim] As IDimension)
Dim dimTol = [dim].Tolerance
Dim maxTol As Double
Dim minTol As Double
If App.IsVersionNewerOrEqual(SwVersion_e.Sw2015, 3) Then
dimTol.GetMinValue2(minTol)
dimTol.GetMaxValue2(maxTol)
Else
minTol = dimTol.GetMinValue
maxTol = dimTol.GetMaxValue
End If
End Sub
public void GetTolerance(IDimension dim)
{
var dimTol = dim.Tolerance;
double maxTol;
double minTol;
if (App.IsVersionNewerOrEqual(SwVersion_e.Sw2015, 3))
{
dimTol.GetMinValue2(out minTol);
dimTol.GetMaxValue2(out maxTol);
}
else
{
minTol = dimTol.GetMinValue();
maxTol = dimTol.GetMaxValue();
}
}