8 Replies Latest reply on Jul 2, 2013 8:29 PM by Artem Taturevych

    Macro for calculation of Moment of inertia

    Arun Gokul

      Can anybody help me for finding out the moment of inertia (at o/p co-ordiante system) with a macro.

       

      Artem has given me  the  code for that

       

       

      swMass.SetCoordinateSystem swModel.Extension.GetCoordinateSystemTransformByName ("YourCoordSystemName")

       

      But when i pasted that code in the macro, and running it's not working.

       

       

       

      can you help me out

        • Re: Macro for calculation of Moment of inertia
          Artem Taturevych

          Change the name of the coordinate system you are interesting in instead of YourCoordSystemName.

           

          The below code works for me.

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swPart As SldWorks.PartDoc

          Dim swMass As SldWorks.MassProperty

           

          Sub main()

           

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swPart = swModel

            

              Set swMass = swModel.Extension.CreateMassProperty

            

              swMass.AddBodies swPart.GetBodies2(swBodyType_e.swAllBodies, False)

           

              Dim val As String

              Dim params As Variant

           

              swMass.SetCoordinateSystem swModel.Extension.GetCoordinateSystemTransformByName("Coordinate System1")

           

              swMass.UseSystemUnits = False

              val = swMass.Mass

              MsgBox "Mass - " & val

              val = swMass.Volume

              MsgBox "Volume - " & val

              val = swMass.Density

              MsgBox "Density - " & val

              val = swMass.SurfaceArea

              MsgBox "Surface area - " & val

              params = swMass.CenterOfMass

              MsgBox "Center of mass - X: " & params(0) & " ,Y: " & params(1) & ", and Z: " & params(2)

              params = swMass.PrincipleMomentsOfInertia

              MsgBox "Principle Moments Of Inertia - X: " & params(0) & " ,Y: " & params(1) & ", and Z: " & params(2)

           

          End Sub

          __________________________

          Regards,

          Artem Taturevych

          Application Engineer at Intercad

          http://intercad.com.au/

          Tel: +61 2 9454 4444

            • Re: Macro for calculation of Moment of inertia
              Arun Gokul

              Hi Artem,

               

              I've modified the code according to my requirement.

               

              Can you please explain me the code in red and how to get Moment of Inerta at Output co-ordinate system

               

              Dim swApp As SldWorks.SldWorks

              Dim swModel As SldWorks.ModelDoc2

              Dim swPart As SldWorks.PartDoc

              Dim swMass As SldWorks.MassProperty

               

               

              Sub main()

               

               

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                  Set swPart = swModel

               

                  Set swMass = swModel.Extension.CreateMassProperty

               

                  swMass.AddBodies swPart.GetBodies2(swBodyType_e.swAllBodies, False)

               

               

                  Dim val As String

                  Dim var As String

                  Dim params As Variant

               

                  swMass.SetCoordinateSystem swModel.Extension.GetCoordinateSystemTransformByName("Coordinate System1")

               

                  swMass.UseSystemUnits = True

               

                  val = swMass.Mass

               

                  var = 2.20462 * val

               

                  MsgBox "Mass - " & Round(val, 3) & " kgs" & "    Mass - " & Round(var, 3) & " lbs"

               

               

                  params = swMass.PrincipleMomentsOfInertia

               

                  MsgBox "Moments Of Inertia - X: " & params(0) & " ,Y: " & params(1) & ", and Z: " & params(2)

               

               

              End Sub

               

               

              Can you explain what should in place of Coordinate System1 for output co-ordinate system??

               

              Is PrincipleMomentsOfInertia correct for finding out moment of inertia because the values at both locations differ

               

              Find the difference in the attached image.

               

              Thank You in Advance

               

              Message was edited by: Arun Gokul

                • Re: Macro for calculation of Moment of inertia
                  Artem Taturevych

                  Hi Arun,

                   

                  Excuse me, I think I now understand what you are looking for. Try this macro:

                   

                  Dim swApp As SldWorks.SldWorks

                  Dim swModel As SldWorks.ModelDoc2

                  Dim swPart As SldWorks.PartDoc

                  Dim swMass As SldWorks.MassProperty

                   

                  Sub main()

                   

                      Set swApp = Application.SldWorks

                      Set swModel = swApp.ActiveDoc

                      Set swPart = swModel

                    

                      Set swMass = swModel.Extension.CreateMassProperty

                    

                      swMass.AddBodies swPart.GetBodies2(swBodyType_e.swAllBodies, False)

                   

                      Dim val As String

                      Dim params As Variant

                     

                      swMass.SetCoordinateSystem swModel.Extension.GetCoordinateSystemTransformByName("Coordinate System3")

                   

                      params = swMass.GetMomentOfInertia(swMassPropertyMoment_e.swMassPropertyMomentAboutCoordSys)

                      MsgBox "Principle Moments Of Inertia: " & params(0) & " , " & params(1) & " , " & params(2) & " , " & params(3) & " , " & params(4) & " , " & params(5) & " , " & params(6) & " , " & params(7) & " , " & params(8)

                   

                  End Sub

                   

                  The Coordinate System is your output coordinate system:

                  CoordSys.png

                   

                  __________________________

                  Regards,

                  Artem Taturevych

                  Application Engineer at Intercad

                  http://intercad.com.au/

                  Tel: +61 2 9454 4444