Learn how to streamline SOLIDWORKS add-in development using C# and VB.NET via free and open-source SwEx.AddIn Framework

# Usage of assembly component transformation in SOLIDWORKS API

SOLIDWORKS components are instances of models (parts or assemblies) in the another parent assembly. Component’s position in its space is driven by its transformation (regardless if the component is constrained by mates or moved in the space by free drag-n-drop operation). Transformation consists of 3 components: translation, rotation and scale.

To get the transformation of the component use the IComponent2::Transform2 SOLIDWORKS API property. The transform in this case represents the relation of the component origin coordinate systems to the root assembly origin coordinate system. It is not required to multiple the transform of sub-assemblies for its children components to get the total transformation of these components relative to root assembly.

In the example below the component is moved in space Along X, Y and Z coordinates. The following example will calculate the new positions of the component’s origin:

Dim swApp As SldWorks.SldWorks Dim swMathUtils As SldWorks.MathUtility Dim swModel As SldWorks.ModelDoc2 Dim swSelMgr As SldWorks.SelectionMgr Dim swComp As SldWorks.Component2 Sub main() Set swApp = Application.SldWorks Set swMathUtils = swApp.GetMathUtility Set swModel = swApp.ActiveDoc Set swSelMgr = swModel.SelectionManager Set swComp = swSelMgr.GetSelectedObject6(1, -1) Dim swTransform As SldWorks.MathTransform Set swTransform = swComp.Transform2 Dim dOrigPt(2) As Double dOrigPt(0) = 0: dOrigPt(1) = 0: dOrigPt(2) = 0 Dim swMathPt As SldWorks.MathPoint Set swMathPt = swMathUtils.CreatePoint(dOrigPt) Set swMathPt = swMathPt.MultiplyTransform(swTransform) Dim vCompOriginPt As Variant vCompOriginPt = swMathPt.ArrayData Debug.Print "Along X: " & vCompOriginPt(0) * 1000 & "mm; " & "Along Y: " & vCompOriginPt(1) * 1000 & "mm; " & "Along Z: " & vCompOriginPt(2) * 1000 & "mm" End Sub

The following line will be output to the Watch window as the result of running the macro on this sample model:

Along X: 75mm; Along Y: -50mm; Along Z: -100mm

Now let’s rotate the component and try to find the rotation angles. This component is rotated in all directions. **Red line** below - is the X axis of the assembly, **Green line** - Y axis, **Blue line** - Z axis. New X, New Y and New Z - are orientation of the corresponding axes in the component and dimensions indicate the angles between those axes.

Const PI As Double = 3.14159265359 Dim swApp As SldWorks.SldWorks Dim swMathUtils As SldWorks.MathUtility Dim swModel As SldWorks.ModelDoc2 Dim swSelMgr As SldWorks.SelectionMgr Dim swComp As SldWorks.Component2 Sub main() Set swApp = Application.SldWorks Set swMathUtils = swApp.GetMathUtility Set swModel = swApp.ActiveDoc Set swSelMgr = swModel.SelectionManager Set swComp = swSelMgr.GetSelectedObject6(1, -1) Dim swTransform As SldWorks.MathTransform Set swTransform = swComp.Transform2 Debug.Print "Angle between X axes: " & Round(GetAngle(1, 0, 0, swTransform) * 180 / PI, 2) & " deg" Debug.Print "Angle between Y axes: " & Round(GetAngle(0, 1, 0, swTransform) * 180 / PI, 2) & " deg" Debug.Print "Angle between Z axes: " & Round(GetAngle(0, 0, 1, swTransform) * 180 / PI, 2) & " deg" End Sub Function GetAngle(x As Double, y As Double, z As Double, transform As SldWorks.MathTransform) As Variant Dim dVect(2) As Double dVect(0) = x: dVect(1) = y: dVect(2) = z Dim swMathVecOrig As SldWorks.MathVector Dim swMathVecTrans As SldWorks.MathVector Set swMathVecOrig = swMathUtils.CreateVector(dVect) Set swMathVecTrans = swMathVecOrig.MultiplyTransform(transform) 'cos a= a*b/(|a|*|b|) GetAngle = ACos(swMathVecOrig.Dot(swMathVecTrans) / (swMathVecOrig.GetLength() * swMathVecTrans.GetLength())) End Function Function ACos(val As Double) As Double If val = 1 Then ACos = 0 ElseIf val = -1 Then ACos = 4 * Atn(1) Else ACos = Atn(-val / Sqr(-val * val + 1)) + 2 * Atn(1) End If End Function

Running the code above will output the following results for this sample model:

Angle between X axes: 110 deg

Angle between Y axes: 66.74 deg

Angle between Z axes: 75 deg