6 Replies Latest reply on Mar 10, 2014 8:01 AM by Joshua Temple

    Macro to see Config Specific Mass AND Assigned Mass

    Joshua Temple

      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
                      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.

        • Re: Macro to see Config Specific Mass AND Assigned Mass
          Matt Martens

          FYI, your assembly zip file didn't contain any part files. So, no one will be able to test it on your files.


          I did a little research into reading the mass of a file. If you use IModelDocExtension::GetMassProperties, the mass, whether it is actual or overridden, is the sixth item of the returned array. However, it is in kg's (or as the API calls it, system units) so you will need to multiply it by 2.20462 to get the mass in lbs.

            • Re: Macro to see Config Specific Mass AND Assigned Mass
              Joshua Temple

              Thank you for bringing that to my attention. I've attached the parts as well now. 


              Kg is fine as that we are all metric over here.


              Sixth in the array should be (5), correct?  I've been using that, but only getting SW solved masses.

                • Re: Macro to see Config Specific Mass AND Assigned Mass
                  Matt Martens

                  You are using IBody2::GetMassProperties to find your mass. In the API documentation it shows the mass as the (volume*density). This idicates to me that it doesn't read what is in the override. In the IModelDocExtension::GetMassProperties it just indicates the mass with no indication of how it is calculated. Perhaps I am reading into it too much or maybe this would mean that with the IBody2 object it calculates the mass while with the IModelDocExtension object it is reading whatever value is used for downline calculations.


                  Try using IModelDocExtension object to get your mass.


                  After experimenting with the mass property tool by itself without the API, I noticed that when you bring up the mass properties of just a body you cannot override anything like you can for the overall part or assembly. Perhaps this is why you always get the SW solved mass when you use the IBody2 object to retrieve the mass properties.

                    • Re: Macro to see Config Specific Mass AND Assigned Mass
                      Joshua Temple

                      Forgive me, do you mean:


                      swMass = tmpModel.GetMassProperties

                      xlsheet.Range("I" & xlCurRow).Value = swMass(5)


                      That gives me the mass of the last saved configuration for each part. So whichever state it was in when it was last saved is the mass it shows. If I open the part, change the config, and save it, the macro will the output that configuration's weight for anytime that part is shown. 




                      Seems I can get it one way or the other, but I can't seem to find a way to get both.

                        • Re: Macro to see Config Specific Mass AND Assigned Mass
                          Matt Martens

                          I experimented with trying to get the mass without having to open each part but I believe this is the method you will need to use. Open each document, activate the appropriate configuration, get the mass using the ModelDocExtension object I described and then close. You don't necessarily have to save each document. Just opening to the right configuration and closing them should be enough.


                          I just don't see any other way right now.

                            • Re: Macro to see Config Specific Mass AND Assigned Mass
                              Joshua Temple



                              Sorry for the late response. I was out of the office all last week after Monday.


                              Unfortunately, doing as you suggest simply will not work. Two reasons:


                              1)  If I open a part in a certain configuration, it will use that configuration for each part with the same name.

                              2)  Though the assembly I posted here is small, it is only for testing. The assemblies I'm needing this macro for have hundreds and hundreds of parts. Opening each one would simply take too long and we still have the first issue I mentioned.


                              I appreciate your help and your time. I'll just keep plugging away.