3 Replies Latest reply on Apr 11, 2016 11:41 AM by Michael Spens

    macro export moment of inertia of assembly to excel

    Collin Koolen

      hi i'm new to this forum

      i'm not new to solid works

       

      my problem is this: i have an assembly and i want to export the 'Moments of inertia Taken at the output coordinate system' to excel

      i got so far that i can output the principal moments of inertia of the x axis at the center of mass of each part with a macro.

      but i want the moments of inertia taken at the output coordinat system of a whole assembly

       

      here is my code so far can anybody help me

      Dim swApp As SldWorks.SldWorks

      Dim SwModel As SldWorks.ModelDoc2

      Dim swModExt As SldWorks.ModelDocExtension

      Dim swAssembly As SldWorks.AssemblyDoc

      Dim SwComp As SldWorks.Component2

      Dim MassProp As SldWorks.MassProperty

      Dim Component As Variant

      Dim Components As Variant

      Dim Bodies As Variant

      Dim BodyInfo As Variant

      Dim CenOfM As Variant

      Dim RetBool As Boolean

      Dim RetVal As Long

      Dim xlApp As Excel.Application

      Dim xlWorkBooks As Excel.Workbooks

      Dim xlBook As Excel.Workbook

      Dim xlsheet As Excel.Worksheet

      Dim OutputPath As String

      Dim OutputFN As String

      Dim xlCurRow As Integer

       

      Sub main()

      Set swApp = Application.SldWorks

      Set SwModel = swApp.ActiveDoc

      If SwModel Is Nothing Then

              swApp.SendMsgToUser2 "An assembly must be an active document.", swMbWarning, swMbOk

              Exit Sub

            End If

           

              

      If SwModel.GetType <> swDocASSEMBLY Then

              swApp.SendMsgToUser2 "An assembly must be an active document.", swMbWarning, swMbOk

              Exit Sub

      Else

      Set swAssembly = SwModel

      End If

      Set swModExt = SwModel.Extension

      Set MassProp = swModExt.CreateMassProperty

       

      OutputPath = Environ("USERPROFILE") & "\Google Drive\"

      OutputFN = SwModel.GetTitle & ".xlsx"

      If Dir(OutputPath & OutputFN) <> "" Then

      Kill OutputPath & OutputFN

      End If

      Set xlApp = Excel.Application

      xlApp.Visible = True

      Set xlWorkBooks = Excel.Workbooks

      Set xlBook = xlWorkBooks.Add()

      Set xlsheet = xlBook.Worksheets("Blad1")

      xlsheet.Range("A1").Value = "Component"

      xlsheet.Range("B1").Value = "Type"

      xlsheet.Range("C1").Value = "Mass (kg)"

      xlsheet.Range("D1").Value = "Volume [m³]"

      xlBook.SaveAs OutputPath & OutputFN

      xlCurRow = 2

      RetVal = swAssembly.ResolveAllLightWeightComponents(False)

      Components = swAssembly.GetComponents(False)

      For Each Component In Components

          Set SwComp = Component

          If SwComp.GetSuppression <> 0 Then

          'If LCase(Right(SwComp.GetPathName, 3)) <> "asm" Then

              Bodies = SwComp.GetBodies2(0)

      'MsgBox SwComp.Name

              'If Bodies <> Empty Then

                  RetBool = MassProp.AddBodies(Bodies)

                  CenOfM = MassProp.CenterOfMass

                 

                  xlsheet.Range("A" & xlCurRow).Value = SwComp.Name

                  xlsheet.Range("C" & xlCurRow).Value = MassProp.Mass

                  xlsheet.Range("D" & xlCurRow).Value = MassProp.Volume

                 xlsheet.Range("E" & xlCurRow).Value = MassProp.PrincipleMomentsOfInertia

                 

                 

                  If LCase(Right(SwComp.GetPathName, 3)) = "asm" Then

                  xlsheet.Range("C" & xlCurRow).Value = 0

                  ElseIf LCase(Right(SwComp.GetPathName, 3)) = "prt" Then

                  xlsheet.Range("B" & xlCurRow).Value = "Part"

                  Else

                  xlsheet.Range("B" & xlCurRow).Value = "Undetermined"

                  End If 'Right 3 of file extension

                  xlCurRow = xlCurRow + 1

                 

                 

              'End If 'UBound(Bodies) <> -1

             

          'End If 'Not an Assembly

          End If 'swComp.GetSuppression <> 0

      Next Component

       

      xlsheet.UsedRange.EntireColumn.AutoFit

      xlBook.Save

      'xlWorkBooks.Close

      'xlApp.Quit

      End Sub

       

       

       

       

      i already took a look at these two thread's

      Macro for calculation of Moment of inertia

      Re: macro to export center of mass/gravity of parts from an assembly to excel

       

      can you help me

        • Re: macro export moment of inertia of assembly to excel
          Michael Spens

          Hi Collin,

          In your component loop, add the following to get the moments of inertia about the assembly coordinate system.  The key is that the MassProperty (MassProp) instance is taken from the Assembly's extension.  When you use GetMomentOfInertia and pass the option for swMassPropertyMomentAboutCoordSys, the component's moments are taken about the source of MassProp (the assembly). 

           

                      xlsheet.Range("A" & xlCurRow).Value = SwComp.Name

                      xlsheet.Range("C" & xlCurRow).Value = MassProp.Mass

                      xlsheet.Range("D" & xlCurRow).Value = MassProp.Volume

                      xlsheet.Range("E" & xlCurRow).Value = MassProp.PrincipleMomentsOfInertia

           

                      'New stuff added here...

                      Dim MOM As Variant

                      MOM = MassProp.GetMomentOfInertia(swMassPropertyMomentAboutCoordSys)

                      Dim m As Integer

                      For m = 0 To UBound(MOM)

                          xlsheet.Cells(xlCurRow, m + 6).Value = MOM(m)

                      Next m

           

          Mike

            • Re: macro export moment of inertia of assembly to excel
              Collin Koolen

              Hi Mike

               

              Thank you! it works realy well

              i even got it to work with my own coordinate system

              but i may have a other question, now i have al this information of every single component in a assembly.

              is there way to get the moment of inertia of a complete assembled  assembly?

              so without the component loop in my code

               

              Collin

                • Re: macro export moment of inertia of assembly to excel
                  Michael Spens

                  Glad it worked out for you Collin.  To get the assembly mass properties, call GetMomentOfInertia before adding bodies.  That gets the data for the assembly itself.  For example, add a call right after you've connected to the assembly extension like this...

                   

                  If SwModel.GetType <> swDocASSEMBLY Then

                          swApp.SendMsgToUser2 "An assembly must be an active document.", swMbWarning, swMbOk

                          Exit Sub

                  Else

                  Set swAssembly = SwModel

                  End If

                  Set swModExt = SwModel.Extension

                  Set MassProp = swModExt.CreateMassProperty

                  'added call here...

                  Dim assyMOI As Variant

                  assyMOI = MassProp.GetMomentOfInertia(swMassPropertyMomentAboutCoordSys)