I am trying to transform mass properties of a component from to it's own coordinate system into it's assembly coordinate system.
I can get the relative locations of the coordinate systems using the SW API example as show below. But this does not actually do the translation giving CG information relative to the next assembly coordinates.
Option Explicit
Sub OutputCompXform(swComp As SldWorks.Component2, nLevel As Long)
Dim vChild As Variant
Dim swChildComp As SldWorks.Component2
Dim sPadStr As String
Dim swCompXform As SldWorks.MathTransform
Dim vXform As Variant
Dim i As Long
For i = 0 To nLevel
sPadStr = sPadStr & " "
Next i
' Null for root component
Set swCompXform = swComp.Transform2
If Not swCompXform Is Nothing Then
vXform = swCompXform.ArrayData
' Root component has no name
Debug.Print sPadStr & "Component = " & swComp.Name2 & " (" & swComp.ReferencedConfiguration & ")"
Debug.Print sPadStr & " Suppr = " & swComp.IsSuppressed
Debug.Print sPadStr & " Hidden = " & swComp.IsHidden(False)
Debug.Print sPadStr & " Rot1 = (" + _
Str(vXform(0)) + ", " + _
Str(vXform(1)) + ", " + _
Str(vXform(2)) + ")"
Debug.Print sPadStr & " Rot2 = (" + _
Str(vXform(3)) + ", " + _
Str(vXform(4)) + ", " + _
Str(vXform(5)) + ")"
Debug.Print sPadStr & " Rot3 = (" + _
Str(vXform(6)) + ", " + _
Str(vXform(7)) + ", " + _
Str(vXform(8)) + ")"
Debug.Print sPadStr & " Trans = (" + _
Str(vXform(9)) + ", " + _
Str(vXform(10)) + ", " + _
Str(vXform(11)) + ")"
Debug.Print sPadStr & " Scale = " + Str(vXform(12))
Debug.Print ""
End If
' Recurse into subassembly
vChild = swComp.GetChildren
For i = 0 To UBound(vChild)
Set swChildComp = vChild(i)
Dim swCorrChildComp As SldWorks.Component2
Dim swMod As SldWorks.ModelDoc2
Set swMod = swComp.GetModelDoc2
Set swCorrChildComp = swMod.Extension.GetCorresponding(swChildComp)
OutputCompXform swCorrChildComp, nLevel + 1
Next i
End Sub
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swConf As SldWorks.Configuration
Dim swRootComp As SldWorks.Component2
Dim bRet As Boolean
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swConf = swModel.GetActiveConfiguration
Set swRootComp = swConf.GetRootComponent
Debug.Print "File = " & swModel.GetPathName
OutputCompXform swRootComp, 0
End Sub
'---------------------------------------
The resulting output is:
File = C:\\Documents and Settings\\jbass\\Desktop\\test.SLDASM
Component = Part4^test-1 (Default)
Suppr = False
Hidden = False
Rot1 = ( 1, 0, 0)
Rot2 = ( 0, .707106781186546, .707106781186549)
Rot3 = ( 0, -.707106781186549, .707106781186546)
Trans = ( 0, 0, 0)
Scale = 1
Component = Part2^test-1 (Default)
Suppr = False
Hidden = False
Rot1 = ( 1, 0, 0)
Rot2 = ( 0, 1, 0)
Rot3 = ( 0, 0, 1)
Trans = ( 0, 0, .0254)
Scale = 1
I am not sure where to go from here? My end result is that I need mass and CG of all components within an assembly relative to the global coordinate system.
SolidworksApi macros