11 Replies Latest reply on Dec 22, 2015 9:24 AM by Michael Nebel

    Simulation data probe macro

    George K.

      EDIT: I have moved this from Simulation to API and have added some detail to the post so it is clearer what I'm trying to do.

       

      I am trying to make a macro that will probe a face (defined by a split line, so as to make it a distinct controlled area that I can probe all of the element) for the first, second and third principal stresses as well as the strain energy density. I attempted to simply record the macro but it didn't pick up on what I was doing. All I was attempting to do was once I had run the simulation record macro that would select a result plot, second button click on the plot, go through probe (or "list selected", as that is what I was effectively selecting in the probe option anyway) and save a csv for the face I am interested in, then move onto the next result plot and repeat until I had exported the data for the plots I have mentioned. When I attempted to record this it gave me a macro with only a few lines of code which I think only recognised me clicking on the face, not the probing of the faces and the exporting of csv files.

       

      Sub main()

       

       

      Set swApp = _

      Application.SldWorks

       

       

      Set Part = swApp.ActiveDoc

      boolstatus = Part.Extension.SelectByID2("", "FACE", 2.36469774600323E-03, 8.58908409014703E-03, 1.00000999998429E-02, True, 0, Nothing, 0)

      Part.ClearSelection2 True

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("", "FACE", 2.73839242295537E-03, 8.61402378137655E-03, 0.010000099999786, True, 0, Nothing, 0)

      Part.ClearSelection2 True

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("", "FACE", 2.4164657299508E-03, 8.52676618163173E-03, 1.00000999999565E-02, True, 0, Nothing, 0)

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("", "FACE", 0.002399477940358, 8.61948923898126E-03, 1.00000999998429E-02, True, 0, Nothing, 0)

      Part.ClearSelection2 True

      End Sub

       

       

      Is it possible to record a macro to do this task, or do I need more advanced programming skills to do this?

       

      Thanks.

        • Re: Simulation data probe macro
          Jared Conway

          why not use the probe tool that is already built in?

           

          probably best to put this in the API forum as well

          • Re: Simulation data probe macro
            George K.

            Currently I am simply going through the probe function to get the first, second and third principal stresses as well as the strain energy density data from a specific area of the simulation and export as csv files. It is quite time consuming to do it manually for different geometry iterations, hence why I wondered if it is possible to make a macro.

             

            As for the API forum, should I repost or are there moderators who can move this? Some places get a bit funny about reproducing threads.

            • Re: Simulation data probe macro
              George K.

              I didn't realise I could move it myself. Now it is moved.

              • Re: Simulation data probe macro
                Attilio Colangelo

                Hi George,

                 

                I just completed a macro to do something similar.   It is easiest if you name the face in the model first (right click --> Face Properties-->"CustomFaceName" in the code below). My original code was just for the split line and I made some mods to get to the face. 

                 

                Set swApp = Application.SldWorks

                Set CWObject = swApp.GetAddInObject("SldWorks.Simulation.7") 'This works for SW Sim 2014

                Set COSMOSWORKS = CWObject.COSMOSWORKS

                Set ActDoc = COSMOSWORKS.ActiveDoc()

                Set StudyMngr = ActDoc.StudyManager()

                Set Study = StudyMngr.GetStudy(6) 'This a zero-index array of the for the number of the study tab in the model

                Set swEntityFace = swPart.GetEntityByName("CustomFaceName", 2)

                 

                swEntityArray = Array(swEntityFace ) 'This is a 1 item array but must be in this form for the GetStressForEntities3 call

                ErrorCodeEnum = Study.RunAnalysis 'Study must be meshed and ready to run at this point (outside of this macro)

                Set cwResults = Study.Results

                'cwResullts now gives you access to all the results you can get interactively

                'Component 9 is vonMises, face position 0 is Total top (bending + membrane), 1 is bottom

                vonMises = cwResults.GetStressForEntities3(True, 9, 1, Nothing, swEntityArray, 1, 0, 0, False, ErrorCodeEnum)

                'Components 6 ( 7, and 8) are the principal stresses

                principalStress1= cwResults.GetStressForEntities3(True, 6, 1, Nothing, swEntityArray, 1, 0, 0, False, ErrorCodeEnum)

                 

                I don't see an API call for Save as CSV (but there is one for Motion Studies?).  In the past I have processed the raw result arrays (e.g. vonMises) by writing to Excel programmatically.

                 

                I am somewhat new to this forum so excuse the code (non)formatting, etc.  I can fill in more details if you need.

                 

                Attilio

                  • Re: Simulation data probe macro
                    Aaron Gradeen

                    Hi Attilio,

                      Thanks for the post. I'm trying to run your code with similar objectives to George. I'm using 2015 SP 1.1. Is there any additional information which must be defined above the Sub main? (I don't declare option explicit). I get an error when the program gets to the line

                    "Set swEntityFace = swPart.GetEntityByName("CustomFaceName", 2)".

                    It says "Object required"

                    If I debug, I can tell that CWObject is never successfully set to anything  ie. when I hover over the variable VBA indicates CWObject=nothing. Any idea if the swApp.GetAddInObject("SldWorks.Simulation.7") is still adequate for 2015? I even tried changing it to swApp.GetAddInObject("SldWorks.Simulation") which is what they use in the API examples and it's still not working.

                    Can anyone help?

                      • Re: Simulation data probe macro
                        Attilio Colangelo

                        That was one of my first posts and now that I see it again it is lacking.  A couple things:

                         

                        1. swApp.GetAddInObject("SldWorks.Simulation")

                        will get the CWObject in 2015.  Also, make sure your project references in VBA are pointing to the Solidworks 2015 libraries. I struggled with this in SW 2014.

                         

                        2. Add the following:

                        Dim swPart  As SldWorks.ModelDoc2

                        Set swPart = swApp.ActiveDoc 'Assumes the active document is a part file

                        I did not have that in my original post and it is the source of the "Object required" error because "swPart" was never created.

                         

                        Hope that helps.

                          • Re: Simulation data probe macro
                            Aaron Gradeen

                            Thanks Attilio! That was certainly the issue. I'm finding that the "If object is nothing then" line of code is giving me some good error checking. I am now able to set swEntityFace to the correct face on the part. At this point my intentions stray from George's. I'm using the function GetFreeBodyForcesandMoment to ultimately obtain the normal and shear forces on this face (not so concerned about the moments as the faces will be quite small and therefore provide good resolution). I'm having trouble getting solidworks to select a reference point (tools -> reference geometry -> point) I have located in the centre of the part face. I think I need this point as one of the arguments in the function before it will spit out the forces. I have even tried just inserting a point with a sketch on the face. Still no luck yet. Here's the syntax

                            Set theSketch = swmodel.GetEntityByName("SketchName", 9)

                            I have tried using all different types of numbers where the argument 9 is shown. I'll keep trying and post the final code if I have any luck with it. Is anyone familiar with this getfreebodyforcesandmoments functionality?

                            Force = cwResults.GetFreeBodyForcesAndMoments(Nothing, swEntityPoint, swEntityArray, 1, errCode)

                            Thanks!

                            • Re: Simulation data probe macro
                              Andrew Paino

                              Attilio, or anyone else that may be able to help.

                               

                              I am attempting to do nearly the same thing as George in his original post, and am having a bit of trouble getting this macro to run. I've used your original code and the updates you suggested, and am getting a mismatch error in the last line. Any thoughts? The code is below.

                               

                              PS: I've changed the number of the study to match what my document contains and that appears to be working correctly. I've also confirmed there is a named entity within my document called "NewFaceName."

                               

                              Dim swApp As Object

                              Sub main()

                               

                              Set swApp = Application.SldWorks

                              Dim swPart  As SldWorks.ModelDoc2

                              Set swPart = swApp.ActiveDoc 'Assumes the active document is a part file

                              Set CWObject = swApp.GetAddInObject("SldWorks.Simulation")

                              Set COSMOSWORKS = CWObject.COSMOSWORKS

                              Set ActDoc = COSMOSWORKS.ActiveDoc()

                              Set StudyMngr = ActDoc.StudyManager()

                              Set Study = StudyMngr.GetStudy(3) 'This a zero-index array of the for the number of the study tab in the model

                              Set swEntityFace = swPart.GetEntityByName("NewFaceName", 2)

                               

                              swEntityArray = Array(swEntityFace)  'This is a 1 item array but must be in this form for the GetStressForEntities3 call

                              ErrorCodeEnum = Study.RunAnalysis 'Study must be meshed and ready to run at this point (outside of this macro)

                              Set cwResults = Study.Results

                              'cwResullts now gives you access to all the results you can get interactively

                              'Component 9 is vonMises, face position 0 is Total top (bending + membrane), 1 is bottom

                              vonMises = cwResults.GetStressForEntities3(True, 9, 1, Nothing, swEntityArray, 1, 0, 0, False, ErrorCodeEnum)

                               

                              End Sub

                                • Re: Simulation data probe macro
                                  Attilio Colangelo

                                  The short answer:

                                  Dim ErrorCodeEnum As Long

                                   

                                  ErrorCodeEnum needs to be declared because it will be returning a value (an error code)

                                   

                                  The long answer:

                                   

                                  It's probably a good idea to declare all the variables and their types.  Helps with debugging and stability.  Something like:

                                   

                                  Dim swEntityFace As SldWorks.Face2

                                  Dim swEntityArray As Variant

                                  Dim ErrorCodeEnum As Long

                                  Dim cwResults As CosmosWorksLib.cwResults

                                  Dim Study As CosmosWorksLib.CWStudy

                                  Dim swPart  As SldWorks.ModelDoc2

                                  .

                                  .

                                  .

                                   

                                  If that is my original code I probably came to this conclusion after a lot of trial and error.

                                    • Re: Simulation data probe macro
                                      Andrew Paino

                                      Attilio,

                                       

                                      Thank you for responding, and sorry about my delay in getting back to you. That was indeed a modified version of your original code.

                                       

                                      The root of all of my problems was that I had not added Solidworks Simulation 2015 type library as a reference within the IDE.

                                       

                                      For anyone else this may help, below is the final code that runs smoothly:

                                       

                                       

                                      Option Explicit

                                       

                                      Dim swApp           As SldWorks.SldWorks

                                      Dim swPart          As SldWorks.ModelDoc2

                                       

                                      Sub main()

                                       

                                      Dim COSMOSWORKS     As CosmosWorksLib.COSMOSWORKS

                                      Dim cwResults       As CosmosWorksLib.cwResults

                                      Dim Study           As CosmosWorksLib.CWStudy

                                      Dim swEntityFace    As SldWorks.Face2

                                      Dim swEntityArray   As Variant

                                      Dim CWObject        As Object

                                      Dim ErrorCodeEnum   As Long

                                      Dim ActDoc          As Object

                                      Dim StudyMngr       As Object

                                      Dim vonMises()      As Variant

                                       

                                      Set swApp = Application.SldWorks

                                      Set swPart = swApp.ActiveDoc

                                      Set CWObject = swApp.GetAddInObject("SldWorks.Simulation")

                                      Set COSMOSWORKS = CWObject.COSMOSWORKS

                                      Set ActDoc = COSMOSWORKS.ActiveDoc()

                                      Set StudyMngr = ActDoc.StudyManager()

                                       

                                      'This a zero-index array for the number of the study tab in the model, update this to reference a particular study

                                      Set Study = StudyMngr.GetStudy(3)

                                       

                                      'Be sure you have a face named "NewFaceName"

                                      Set swEntityFace = swPart.GetEntityByName("NewFaceName", 2)

                                       

                                      'This is a 1 item array but must be in this form for the GetStressForEntities3 call

                                      swEntityArray = Array(swEntityFace)

                                       

                                      'Study must be meshed and ready to run at this point (outside of this macro)

                                      ErrorCodeEnum = Study.RunAnalysis

                                       

                                      'cwResullts now gives you access to all the results you can get interactively

                                      Set cwResults = Study.Results

                                       

                                      'Component 9 is vonMises, face position 0 is Total top (bending + membrane), 1 is bottom

                                      vonMises = cwResults.GetStressForEntities3(True, 9, 1, Nothing, swEntityArray, 1, 0, 0, False, ErrorCodeEnum)

                                       

                                      End Sub