AnsweredAssumed Answered

API to get free body forces from each selected face

Question asked by Aaron Gradeen on Oct 24, 2018
Latest reply on Oct 24, 2018 by Attilio Colangelo

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!