MassProperties X VBA X Configurations
MassProperties X VBA X Configurations
(OP)
I am writing a VBA program to read and write custom properties of parts. These parts can have configurations. I use EXCEL, list the custom properties in rows and the configurations (if they exist) of the part in columns. Everything works fine except for MassProperties (weight and volume). I can only retrieve the mass properties of the active configuration. I have made some tests, including try to change the active configuration with the program, but I think that it only works with assemblies. Can anyone give me a hint about how can I read, using VBA, the custom properties of several configurations of a part?
I am working with sw2001 SP11.
I am working with sw2001 SP11.






RE: MassProperties X VBA X Configurations
Option Explicit
Const KGtoLB = 2.204622
Dim swApp As Object
Dim Part As Object
Sub GetMassOfEachConfig()
Dim iConfigs As Long
Dim ConfigNames As Variant
Dim ConfigWgt() As Single
Dim v1 As Variant, lStatus As Long
Dim sTmp As String, i As Long
'Get SolidWorks
Set swApp = GetObject(, "SldWorks.Application")
Set Part = swApp.ActiveDoc
'Get a list of Configurations for active part
iConfigs = Part.GetConfigurationCount
ConfigNames = Part.GetConfigurationNames
'Traverse through the configurations
ReDim ConfigWgts(iConfigs)
For i = 0 To (iConfigs - 1)
Part.ShowConfiguration ConfigNames(i)
v1 = Part.GetMassProperties2(lStatus)
ConfigWgts(i) = v1(5) * KGtoLB
Next
'Report Results
For i = 0 To (iConfigs - 1)
sTmp = sTmp & ConfigNames(i) & ": " & ConfigWgts(i) & " lbs." & vbCrLf
Next
MsgBox sTmp
End Sub
DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
RE: MassProperties X VBA X Configurations
Thank you for your very useful answer. After reading it, I realised that I was not declaring and using the variable lStatus in the proper way. Every time that EXCEL reached my previous line MassProp = Part.GetMassProperties2(....) the result was:
- a VBA error message, or
- the values of the active configuration's mass properties were repeated for all configurations.
Now, after correcting the program based in your answer, everything is working fine!
Best Regards