5 Replies Latest reply on Mar 13, 2014 9:36 AM by Joshua Temple

    GetMassProperties/CreateMassProperties Hybrid?

    Joshua Temple

      Me again.


      No one on here or at work has been able to resolve this issue yet. I'm trying to remain optimistic that it is possible.  I just don't see how DS wouldn't have put in the capability for this.


      Once again: I'm trying to create a macro that will read both configuration specific information (CreateMassProperties) AND assigned mass (GetMassProperty).



      The issue is that CreateMassProperties overwrites any assigned mass and GetMassProperty only gets the mass of the last saved configuration of whichever part it's on. So if you're needing mass properties for -003 and the part was last saved in -001, it will give you the mass properties of -001.



      I need the macro to do both. I have tried and tried. I've looked at every bit of code I could find that bore some relevance to mine. They all have the same issues I'm having.



       For i = 0 To UBound(Bodies)
                      RetBool = SwComp.AddBodies(Bodies)
                      Set swBody = Bodies(i)
                      Mass = swBody.GetMassProperties2
                      xlsheet.Range("I" & xlCurRow).Value = Mass(5)
                      'swMass = .GetMassProperty 'ReferencedConfiguration & Chr(13)
                      xlsheet.Range("D" & xlCurRow).Value = swMass(5) 'vMassProp         'troubleshooting
                      nDensity = tmpModel.GetUserPreferenceDoubleValue(swMaterialPropertyDensity)  'DO NOT USE tmpModExt
                      vMassProp = swBody.GetMassProperties(nDensity)
                      xlsheet.Range("E" & xlCurRow).Value = vMassProp(5)  'troubleshooting
                      '******************  Example of Issue ****************
                      Assigned = tmpModel.GetMassProperties
                      xlsheet.Range("F" & xlCurRow).Value = Assigned(5)               'troubleshooting
                      xlsheet.Range("G" & xlCurRow).Value = MassProp.Mass         'from CreateMassProperties     'troubleshooting




      At the bottom you'll see the code that outputs the issues I mention above. I believe I have tried everything, but nothing has worked and I don't have any more ideas. I'm to the point where I've begun doing things I've already done months ago. The result is not any better.


      Can anyone take one last look at this?

        • Re: GetMassProperties/CreateMassProperties Hybrid?
          Artem Taturevych



          Sorry, I’m not sure that I completely understand your issue. I understand that you want to see the real mass for configurations. Do you need this for BOM purposes? Do you have an assembly and you want to output the mass to corresponding properties of each component (taking the referenced configuration into a consideration)? Do you need this to be a live auto updatable link or it is OK to rerun a macro every time when you changed the geometry to recalculate the mass?



          Regards, Artem Taturevych | Snr. Developer | IC3D ANZ


          IC3DSteel – Novice Steel Solution for SolidWorks

          translationXpert – SolidWorks files language translator

          LinkedIn - SolidWorks API Group

            • Re: GetMassProperties/CreateMassProperties Hybrid?
              Joshua Temple



              Thank you for your reply.


              It will be used on assemblies as they are released so it is find that it has to be run manually each time. We are taking the mass properties of each part in an assembly with reference to a global origin. Not for a BOM, just for tracking the weights of our assemblies.


              We need the macro to look at what specific configuration it is primarily. We have achieved that, but when the configuration has an assigned mass it ignores it and outputs what the "solved" mass is.

                • Re: GetMassProperties/CreateMassProperties Hybrid?
                  Artem Taturevych

                  Hi Joshua,


                  Please take a look at this macro. It traverses all part components in the assembly and outputs its mass to an immediate window. If the mass is assigned it is noted in the output.


                  Things to note:


                  1) You cannot add the bodies from the assembly (AddBodies returns true but it still returns the mass for whole assembly)

                  2) You cannot add components instead of bodies

                  3) You need to get the mass property of the component model

                  4) You need to activate the configuration if you want to get configuration specific mass properties

                  5) If you want to get the data (COG) relatieve to main assembly you need to get this from part firstly then use math transforms utility to convert the data.


                  Dim swApp As SldWorks.SldWorks

                  Dim swAssy As SldWorks.AssemblyDoc

                  Dim swMassPrp As SldWorks.MassProperty


                  Sub main()


                      Set swApp = Application.SldWorks


                      Set swAssy = swApp.ActiveDoc


                      Dim vComps As Variant

                      vComps = swAssy.GetComponents(False)


                      Dim i As Integer


                      For i = 0 To UBound(vComps)


                          Dim swComp As SldWorks.Component2

                          Set swComp = vComps(i)


                          Dim swRefModel As SldWorks.ModelDoc2

                          Set swRefModel = swComp.GetModelDoc2


                          If Not swRefModel Is Nothing Then


                              Set swMassPrp = swRefModel.Extension.CreateMassProperty()


                              If swRefModel.GetType() = swDocumentTypes_e.swDocPART Then

                                  'activate configuration to get the mass properties

                                  swRefModel.ShowConfiguration2 swComp.ReferencedConfiguration

                                  Debug.Print swComp.Name2 & ": " & swMassPrp.Mass & IIf(swMassPrp.UserAssigned, " (user assigned)", "")

                              End If


                          End If




                  End Sub



                  Regards, Artem Taturevych | Snr. Developer | IC3D ANZ


                  IC3DSteel – New Steel Solution for SolidWorks

                  translationXpert – SolidWorks files language translator

                  LinkedIn - SolidWorks API Group