API: center of mass calculation of parts in an assembly

Hello,
I have an assembly with N parts, and am trying to output the center of mass of each part, with respect to the assembly coordinate system to a
file. I have found some examples online, and have begun to craft a VBA script, which is shown below.
Now, the code below outputs the center of mass of each part, with respect to that part's coordinate system (each part is a cube of dimension 4cmx4cmx4cm, so the center of mass is reported as (0.02,0.02,002)). Here is the output of the code:
%Begin Output block-4,0.02,0.02,0.02,0.000064 block-3,0.02,0.02,0.02,0.000064 block-2,0.02,0.02,0.02,0.000064 block-1,0.02,0.02,0.02,0.000064 block-5,0.02,0.02,0.02,0.000064 % End Output
Any ideas on how to change the below code so that the output for each part is the center of mass with respect to the assembly coordinate system?
Thanks in advance, Paul Rebillot
% Begin Code Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks Dim swAssy As SldWorks.ModelDoc2 Dim swGroup As Variant Dim count As Integer Dim swComp As SldWorks.Component2 Dim swCompModel As SldWorks.ModelDoc2 Dim swCompBody As SldWorks.Body2 Dim vMassProps As Variant Dim i As Integer Dim status As Long Dim tempOutput As String Dim partName As String
Set swApp = Application.SldWorks Set swAssy = swApp.ActiveDoc swGroup = swAssy.GetComponents(0) count = swAssy.GetComponentCount(0)
Open "C:\test.txt" For Output As #1
For i = 1 To count Set swComp = swGroup(i - 1) Set swCompModel = swComp.GetModelDoc
' Get name of component partName = swComp.Name ' Use this method to get component mass properties ' Currently, this outputs CoM wrt to individual component coordinate system. ' need to output wrt to assembly coordinate system, or transform to this system. vMassProps = swCompModel.Extension.GetMassProperties(2, status) ' Output to file tempOutput = partName & "," & vMassProps(0) & "," & vMassProps(1) & "," & _ vMassProps(2) & "," & vMassProps(3) Print #1, tempOutput Next i
Close #1 ' Close file.
End Sub % End Code
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

You have to take the transformation matrix from each component, and use matrix algebra to transform the coordinates to the assembly coordinate system. There is helper classes/methods in the SW api (MathPoint, MathVector, Matrix etc) to make things easier. If you have no idea what this means, you should try to learn some basic matrix/vector stuff first; there are lots of great tutorials in the net (use google and search for eg. 3d transforms).
Basically the transform matrix of a component contains information about the position and the orientation of a component, and you can use matrix algebra to transform coordinates beetween component/assembly levels. Note that the component may be located in a sub-assembly, so this has to be done step by step for each assembly level.
Hope this helps!
-h-
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload
On Aug 4, 3:11 pm, "Heikki Leivo"

Thanks for your help. I do know how to do this - just hoping that there was a way to bypass the coordinate transformations, and have SW output this for me directly.
Thanks again! Paul
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

Polytechforum.com is a website by engineers for engineers. It is not affiliated with any of manufacturers or vendors discussed here. All logos and trade names are the property of their respective owners.