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