6 Replies Latest reply on Feb 16, 2015 11:45 AM by Attilio Colangelo

    VBA Macro to get and save Area MOI

    Dave Marquis

      Hi All

      I need help...I'm trying to write a VBA macro that takes a section of a part, gets the area moment of inertia (specifically, Ixx and Iyy) and saved it (appended) to a text file. I can get the first and last, but can't seem to find the function to get the MOI values.

       

      Thanks

        • Re: VBA Macro to get and save Area MOI
          Attilio Colangelo

          Try

           

          GetSectionProperties2 Method (IModelDocExtension)

            • Re: VBA Macro to get and save Area MOI
              Dave Marquis

              Thanks Attilio

               

              Great!

               

              But, I'll confess I'm new to VBA in Solidworks, but have a fair bit of experience with Excel. Sadly, nothing's working as I hoped or at all.

               

              Ultimately, I want to iterate along a direction of a part, taking several section cuts, getting the Ixx and Iyy at each cut, and saving them to a single text file for post-processing.

               

              The problem is that the part, a hollow tube, has cross-sectional area changes along the length of my part. So, as I progress up the tube, I need to get the current surface, take the "measurements" and then save them into a file.

               

              So for the code line:

              boolstatus = Part.Extension.SelectByID2("Unknown", "MANIPULATOR", -0.01254874606678, 0, -0.06175785285308, False, 0, Nothing, 0)

               

              the y-component (0 in this case) is easy; that's my section cut position. However, the x and z will not be fixed.

               

              Can you help me build this? Please.

                • Re: VBA Macro to get and save Area MOI
                  Keith Rice

                  Hi Dave,

                   

                  If you want help with macro you are currently writing, then please read this so you know what information you need to provide for us to help you: How to ask a good question in the API forum

                   

                  I would recommend breaking down your problem into smaller problems and posting about each one individually. People are usually more likely to help with clearly defined bite-sized problems rather than "How do I do X, Y, and Z?" (which is usually a serious programming effort)

                   

                  If you are trying to do it all on your own, you will be spinning your wheels until you learn what I call the three pillars of SolidWorks API programming:

                  1. Basic programming (usually with VBA)

                  2. Understanding the SolidWorks API Help

                  3. Understanding the SolidWorks API Object model

                   

                  I have an article that might help bring clarity to some of these topics here: 7 Mistakes New SolidWorks API Programmers Make

                   

                  Hope this helps you get started.

                   

                  Keith

                  SolidWorks API Tutorials

                  • Re: VBA Macro to get and save Area MOI
                    Attilio Colangelo

                    Hi Dave,

                     

                    Here is some sample code I tested on a tapered cylinder.  The most important method(s) to fully understand are those related to CreateSectionViewData and CreateSectionView.  This might get you started but as the others mentioned you will require a more in-depth knowledge of the API to turn this into a production tool.

                     

                    Dim swApp As SldWorks.SldWorks

                    Dim swModel As SldWorks.ModelDoc2

                    Dim swModelDocExt As SldWorks.ModelDocExtension

                    Dim swModelViewMgr As SldWorks.ModelViewManager

                    Dim swSelMgr As SldWorks.SelectionMgr

                    Dim svData As SldWorks.SectionViewData

                    Dim plane1 As SldWorks.Feature

                    Dim boolstatus As Boolean

                    Dim sectionProps As Variant

                    Dim face1(0 To 0) As SldWorks.entity

                    Dim i As Integer

                     

                    Sub main()

                        Set swApp = Application.SldWorks

                        Set swModel = swApp.ActiveDoc

                        Set swModelDocExt = swModel.Extension

                        Set swModelViewMgr = swModel.ModelViewManager

                        Set swSelMgr = swModel.SelectionManager

                    Open "E:\test.txt" For Output As #1

                     

                    For i = 1 To 5  '5 slices

                        boolstatus = swModelDocExt.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 1, Nothing, 0)

                        Set plane1 = swSelMgr.GetSelectedObject6(1, 0)

                        Set svData = swModelViewMgr.CreateSectionViewData()

                            svData.FirstPlane = plane1

                            svData.FirstOffset = i * 0.002 '2 mm slices

                            svData.Redraw = True

                        boolstatus = swModelViewMgr.CreateSectionView(svData)

                        Debug.Print " Section bodies are valid: " & boolstatus

                        boolstatus = swModelDocExt.SelectByID2("", "FACE", 0, 0, i * 0.002, False, 8, Nothing, swSelectOptionDefault)

                        Set face1(0) = swSelMgr.GetSelectedObject5(8)

                        sectionProps = swModelDocExt.GetSectionProperties2(face1(0))

                        Debug.Print sectionProps(13)

                     

                        Write #1, sectionProps(13) 'Write MOI to text file

                    Next i

                            Close #1

                    End Sub

                • Re: VBA Macro to get and save Area MOI
                  Jessica Smith

                  HI Dave! There may be an app here that could help you! www.wappsy.net it's the only place that has 100% SolidWorks apps free to download

                   

                  Hope you find one you like!

                   

                  Jessica

                  • Re: VBA Macro to get and save Area MOI
                    Clark Honzik

                    In order to understand how to use GetSectionProperties2 Method (IModelDocExtension) you first need to be able to understand the SW API object model or at least a basic understanding of the object model.  Without that you will probably be a little lost.  If you are really interested in learning how to program against the SW API I would suggest starting with the tutorials in SolidWorks.  You can take it further by taking training from your VAR as well.  You will want to have some basic programming skills to get the most out of it.  You can write your code in VBA but you also have the option of VSTA which allows you to write your code in .net giving you a lot more capability.  If you are feeling really ambitious you can even learn how to write your own SW add-ins unleashing the power of event based triggers.