1 Reply Latest reply on Oct 24, 2018 8:09 PM by Attilio Colangelo

    API to get free body forces from each selected face

    Aaron Gradeen

      Hi All,

      I'm trying to get more familiar with SolidWorks API. I currently know very little. I would like to write an API in VBA that loops through all of the faces of an assembly which are selected and stores the free body forces from simulation results into an array which will later be written to a file. I'm attempting to accomplish this in small steps. I'm having issues using the GetFreeBodyForcesAndMoments function though. This is what I believe my code is doing.

      1) The selected face is identified using the .GetSelectedObject method.

      2) I use this .StudyManager function(?) to access the simulation results.

      3) I try to store the free body force into a variable called "Force"

      I think my arguments are not valid for step 3. Here's my code below. I'm not at all familiar with all of the CosmosWorksLib references.



      'Simulation add in has been enabled

      'study has been run

      'face of interest has been selected



      Sub main()

          Dim swApp As SldWorks.SldWorks

          Dim Part As SldWorks.PartDoc

          Dim Face As SldWorks.Face2

          Dim Model As SldWorks.ModelDoc2

          Dim ModelDocExt As SldWorks.ModelDocExtension

          Dim SelMgr As SldWorks.SelectionMgr

          Dim Entity As SldWorks.Entity

          Dim faceName As String

          Dim ret As Boolean

          Dim Force As Double

          Dim COSMOSWORKS As CosmosWorksLib.COSMOSWORKS, CWAddinCallBack As CosmosWorksLib.CWAddinCallBack, ActDoc As CosmosWorksLib.CWModelDoc

          Dim StudyMngr As CosmosWorksLib.CWStudyManager, Study As CosmosWorksLib.CWStudy, CWMesh As CosmosWorksLib.CWMesh

          Dim CWFeatObj As CosmosWorksLib.CWResults


          Set swApp = CreateObject("SldWorks.Application")

          ' Get active document

          Set Part = swApp.ActiveDoc

          Set Model = Part

          Set SelMgr = Model.SelectionManager

          Set Face = SelMgr.GetSelectedObject6(1, -1)

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


          Set ActDoc = COSMOSWORKS.ActiveDoc()

          Set StudyMngr = ActDoc.StudyManager()

          Set Study = StudyMngr.GetStudy(0)

          Set CWFeatObj = Study.Results

          'If CWFeatObj Is Nothing Then ErrorMsg swApp, "Failed to get result object": GoTo Lastline


          Force = CWFeatObj.GetFreeBodyForcesAndMoments(Nothing, 1, Face, 0, 0)



      End Sub


      Could the issue be with the last argument in the GetFreeBodyForcesAndMoments function? I'm thinking that if it's set to 0 it means there's no error code. Here's a blurb on the function from SW API help

      Function GetFreeBodyForcesAndMoments( _

         ByVal DispPlane As System.Object, _

         ByVal SelectedRefPoint As System.Object, _

         ByVal ArraySelectedEntities As System.Object, _

         ByVal NUnits As System.Integer, _

         ByRef ErrorCode As System.Integer _

      ) As System.Object


      Any help is greatly appreciated. Thanks!

        • Re: API to get free body forces from each selected face
          Attilio Colangelo

          A few comments:

          - The second argument ( SelectedRefPoint)can, and probably should be, NULL. Not sure what 1 represents.

          - The third argument (ArraySelectedEntities ) must be an array, even if a single item is selected

          - ErrorCode  gets returned to you after execution because it is a Reference type. Zero is successful execution but you declare this parameter as variable (how I wish we could just _make_ it zero but that's another story....)

          - The return must be dimensioned as an array (Variant)


          The following code snip was tested in SW2018 and Simulation 2018


          Set Face = SelMgr.GetSelectedObject6(1, -1)

          Dim  SelectedFaceArray(0) As Variant

          Dim arrayOfFreeBodyForcesAndMoments As Variant

          Dim errCode As swsResultsError_e 'Could just declare this as Long but get a verbose error message this way

          Set SelectedFaceArray(0) = Face 'Single element array

          arrayOfFreeBodyForcesAndMoments = cwResults.GetFreeBodyForcesAndMoments(Nothing, Null, SelectedFaceArray, swsForceUnit_e.swsForceUnitlbOrlbin, errCode)


          Hope that helps