Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

MassProperties X VBA X Configurations

Status
Not open for further replies.

Guest
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.
 
Replies continue below

Recommended for you

You may have to loop through the configurations, activating each and reading the mass properties. Then populate your excel file with the value rather than maintaining the link. Here is some code to get you through the configurations and get the mass properties:
Code:
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.
 
dsi

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor