ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
JTJoshua Temple03/03/2014

Good afternoon, Gentlemen:

Thanks to the wealth of knowledge available here on the forums and on the API help guide, I've been able to generate a macro that will export the mass properties for an assembly from SW to excel. This greatly improves our weight tracking ability.

However, I cannot seem to get the macro to look at both the configuration-specific mass AND the assigned Mass. Assigned mass should, of couse, supercede the configuration-specific mass, but the macro is only outputting the "solved" mass.

I've created a simple assembly to test the functionality of the macro. In it I have the following:

(1) Cylinder with no other configurations (only assigned mass)

(2) Bolts with differing configurations

(3) Thin Blocks. All three are different configurations and two of them have assigned mass.

My attempt here was to capture any possibility the macro might encounter as it pertains to configuration/assigned mass.

The macro does a good of job seeing the different configurations, but if the configuration (or part) has an assigned mass, it ignores it. So for any of the assigned masses, it just shows what SW solves for the mass. 

There is something inherently wrong with home I'm doing this as if I put in "(some cell in excel) = MassProp.userDefined" it returns as FALSE for all of them, no matter if it has an assigned mass or not. If I use swMass.UserDefined, it is TRUE for all parts.

I've tried to condense this down as much as possible. The whole code and test assembly is attached.

Components = swAssembly.GetComponents(False)           'False for not just top level components only

Set swModExt = SwModel.Extension

Set MassProp = swModExt.CreateMassProperty

On Error Resume Next

        For Each Component In Components

        Set SwComp = Component

        Set SwCompModel = SwComp.GetModelDoc2 'there is also a SwComp.GetModelDoc2... try if GetModelDoc doesn't work correctly

        Set SwCompBody = SwComp.GetBody

        Set tmpDocExt = SwCompBody.Extension

        Set swMass = SwCompModel.Extension.CreateMassProperty(2, 0) ' 0 = default, 1 = as is, 2 = maximum and second parameter is type of body: 0 = user body, 1 = normal body (2,0)

        'Set vMassProp = tmpDocExt.GetMassProperties(2, 0)

            If SwComp.GetSuppression <> 0 Then

                      

            Bodies = SwComp.GetBodies2(0)  'array for number of bodies

            RetBool = MassProp.AddBodies(Bodies)         'vMassProp.AddBodies(Bodies)

            RetBool2 = vMassProp.AddBodies(Bodies)      'troubleshooting

            RetBool3 = swMass.AddBodies(Bodies)          'troubleshooting

            CenOfM = MassProp.CenterOfMass

           If IsEmpty(Bodies) = True Then

           GoTo LNNext

           Else                          'If IsEmpty(Bodies) = False Then

                                           'If Bodies > 1 Then GoTo Multi

                                           'Else: GoTo SingleBody

           GoTo Multi

           End If

Multi:                                       'If UBound(Bodies) > 1 And LCase(Right(SwComp.GetPathName, 3)) = "asm" Then   (TAKEN OUT TO SIMPLIFY CODE DURING TROUBLESHOOTING)

            For i = 0 To UBound(Bodies)

                If swMass.UserAssigned = True Then       'COMING OUT TRUE FOR ALL PARTS (MassProp.UserAssigned comes out false for all)

               nDensity = SwCompModel.Extension.GetUserPreferenceDouble(swUserPreferenceDoubleValue_e.swMaterialPropertyDensity, swUserPreferenceOption_e.swDetailingNoOptionSpecified)

                vMassProp = SwCompBody.GetMassProperties(nDensity)

                xlsheet.Range("I" & xlCurRow).Value = vMassProp(5)                           'MassProp.Mass 'MassProp.Mass 'MassProp.Mass

                Else

                xlsheet.Range("I" & xlCurRow).Value = "NOPE"  'Just to show if the assigned mass was seen

                End If

                xlsheet.Range("D" & xlCurRow).Value = vMassProp                             'troubleshooting

                xlsheet.Range("E" & xlCurRow).Value = vMassProp(5)                         'troubleshooting

                xlsheet.Range("F" & xlCurRow).Value = vMassProp.Mass                    'troubleshooting

                xlsheet.Range("G" & xlCurRow).Value = swMass.Mass                        'troubleshooting

LNNext: Next Component

.....

I would really appreciate any help you can offer. I've been working on this one issue in this code for a couple weeks and have exhausted my individual abilities. Thank you, everyone.