Getting Components and then getting mass properties info from those components sequentially

Can someone help me figure out why this macro won't compile? I am trying to get the mass properties of the components of an open
assembly relative to the origin of the over all assembly. I have an assembly open, then I am trying to loop through the components and get the mass properties information as well as the transform info and then transform the cg's and and translate them to the appropriate positions and then write the information to an excel spreadsheet.
Here's the Macro. If you can answer my question than you probably already know this, but just in case, in order to get the excel portion to work you have to be in the macro window and then check the box in tools -> references -> Micorsoft Excel 11.0 Object Library.
'----------------------------------------------------
' Preconditions:
' (1) Assembly is open.
' (2) Components are selected in feature manager.
' (3) go to tools->references-> and check "Microsoft Excel 10.0 Object Library" box
'
' Postconditions: None
'
' '
'----------------------------------------------------
Option Explicit
Public Enum swMassPropertiesStatus_e
swMassPropertiesStatus_OK = 0
swMassPropertiesStatus_UnknownError = 1
swMassPropertiesStatus_NoBody = 2
End Enum
Public Enum swUserPreferenceDoubleValue_e
swMaterialPropertyDensity = 7
End Enum Dim xlApp As Excel.Application Dim wb As Workbook, ws As Worksheet
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swAssy As SldWorks.ModelDoc2
Dim swGroup As Variant
Dim count As Integer
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim swCompModel As SldWorks.ModelDoc2
Dim swCompBody As SldWorks.Body2
Dim vMassProps As Variant
Dim swCompXform As SldWorks.MathTransform
Dim vXform As Variant
Dim nDensity As Double
Dim bRet As Boolean
Dim i As Integer
Dim j As Integer
Dim mcgx As Double
Dim mcgy As Double
Dim mcgz As Double
Dim m As Double
'Start Excel Set xlApp = New Excel.Application xlApp.WindowState = xlMaximized xlApp.Visible = True Set wb = xlApp.Workbooks.Add Set ws = wb.Sheets("Sheet1")
ws.Cells(1, 1) = "Part" ws.Cells(1, 2) = "Weight (lbs)" ws.Cells(1, 3) = "CGx (in)" ws.Cells(1, 4) = "CGy (in)" ws.Cells(1, 5) = "CGz (in)" ws.Cells(1, 6) = "Rot X1" ws.Cells(1, 7) = "Rot X2" ws.Cells(1, 8) = "Rot X3" ws.Cells(1, 9) = "Rot Y1" ws.Cells(1, 10) = "Rot Y2" ws.Cells(1, 11) = "Rot Y3" ws.Cells(1, 12) = "Rot Z1" ws.Cells(1, 13) = "Rot Z2" ws.Cells(1, 14) = "Rot Z3" ws.Cells(1, 15) = "Trans X" ws.Cells(1, 16) = "Trans Y" ws.Cells(1, 17) = "Trans Z" ws.Cells(1, 18) = "Scale" ws.Cells(1, 19) = "Corrected CGx" ws.Cells(1, 20) = "Corrected CGy" ws.Cells(1, 21) = "Corrected CGz"
Set swApp = Application.SldWorks Set swAssy = swApp.ActiveDoc swGroup = swAssy.GetComponents(0) count = swAssy.GetComponentCount(0)
mcgx = 0 mcgy = 0 mcgz = 0 m = 0
For i = 1 To count Set swComp = swGroup(i) Set swCompModel = swComp.GetModelDoc
Set swCompXform = swComp.Transform2
' Calculate component material density
'nDensity swCompModel.GetUserPreferenceDoubleValue(swMaterialPropertyDensity)
' Use this method to get component mass properties
vMassProps = swComp.GetMassProperties(2)
' Use this method to get component transforms
vXform = swCompXform.ArrayData
'Write data into excel sheet
j = i + 2 ws.Cells(j, 1) = swComp.Name2 ws.Cells(j, 2) = vMassProps(5) * 2.20462 ws.Cells(j, 3) = vMassProps(0) * 39.36996 ws.Cells(j, 4) = vMassProps(1) * 39.36996 ws.Cells(j, 5) = vMassProps(2) * 39.36996 'write the rotational matrix xforms ' ws.Cells(j, 6) = vXform(0) 'ws.Cells(j, 7) = vXform(3) 'ws.Cells(j, 8) = vXform(6) 'ws.Cells(j, 9) = vXform(1) 'ws.Cells(j, 10) = vXform(4) 'ws.Cells(j, 11) = vXform(7) 'ws.Cells(j, 12) = vXform(2) 'ws.Cells(j, 13) = vXform(5) 'ws.Cells(j, 14) = vXform(8) 'write the translation xform matrix 'ws.Cells(j, 15) = vXform(9) * 39.36996 'ws.Cells(j, 16) = vXform(10) * 39.36996 'ws.Cells(j, 17) = vXform(11) * 39.36996
'write the scalar ws.Cells(j, 18) = vXform(12)
'correct the cg and write scalar
ws.Cells(j, 19) = (vXform(9) + (vMassProps(0) * vXform(0) + vMassProps(1) * vXform(3) + vMassProps(2) * vXform(6))) * 39.36996 ws.Cells(j, 20) = (vXform(10) + (vMassProps(0) * vXform(1) + vMassProps(1) * vXform(4) + vMassProps(2) * vXform(7))) * 39.36996 ws.Cells(j, 21) = (vXform(11) + (vMassProps(0) * vXform(2) + vMassProps(1) * vXform(5) + vMassProps(2) * vXform(8))) * 39.36996
m = m + (vMassProps(5) * 2.20462) mcgx = mcgx + (vMassProps(5) * ws.Cells(j, 19)) * 2.20462 mcgy = mcgy + (vMassProps(5) * ws.Cells(j, 20)) * 2.20462 mcgz = mcgz + (vMassProps(5) * ws.Cells(j, 21)) * 2.20462
Next i
ws.Cells(2, 1) = "Top Level"
ws.Cells(2, 2) = m ws.Cells(2, 3) = (mcgx / m)
ws.Cells(2, 4) = (mcgy / m) ws.Cells(2, 5) = (mcgz / m)
End
End Sub
'---------------------------------------------------
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload
Change this :
Set swComp = swGroup(i - 1) ' items in arrays are indexed 0...n-1 ... nDensity swCompModel.GetUserPreferenceDoubleValue(swMaterialPropertyDensity)' old way. must be changed to take any material into account Dim body As SldWorks.Body2 ' this will work only for single body parts Set body = swComp.GetBody If body Is Nothing Then GoTo skip ' subassemblies are components without body vMassProps = body.GetMassProperties(nDensity) ' there is no Component:GetMassProperties method. ... skip: Next i
or even better, put an if then ... endif block instead of the goto ...skip:
Note that your assembly must be resolved. for lightweight components, swComp.GetModelDoc will return an empty ModelDoc.
Besides, for such applications it is often much easier to generate .csv (comma separated values) text files which can be read by Excel.
may I edit+post this code on http://swapi.wordpress.com/ ?
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.