I'm doing a static non-linear study of an assembly.
I have checked the option to compute free-body forces in the options.
Once the study is computed, I am able to see the free-body forces and moments using the UI, like normal
But when coding a Macro, I'm not able to get these values:
Dim SwApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Function ErrorMsg(SwApp As SldWorks.SldWorks, Message As String)
SwApp.SendMsgToUser2 Message, 0, 0
SwApp.RecordLine "'*** WARNING - General"
SwApp.RecordLine "'*** " & Message
SwApp.RecordLine ""
End Function
Sub main()
Set SwApp = Application.SldWorks
Set Part = SwApp.ActiveDoc
Dim COSMOSWORKSObj As Object
Dim CWAddinCallBackObj As Object
Dim StudyManagerObj As Object
Dim ActiveDocObj As Object
Dim active_doc As Object
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
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 StudyOptions As Object, CWFeatObj As CosmosWorksLib.CWResults, ActDocExt As SldWorks.ModelDocExtension
Dim Assembly As AssemblyDoc, Component As Component2, FaceInPart As Entity, FaceInAssembly As Entity
Dim Point As RefPoint
Dim DispArray1 As Variant, DispArray2 As Variant, Force As Variant
Dim stepNumber As Long
Set Assembly = SwApp.ActiveDoc
Set Component = Assembly.GetComponentByName("centrifugal_hinge-1")
Set Part = Component.GetModelDoc2
Set FaceInPart = Part.GetEntityByName("tip", 2)
Set FaceInAssembly = Component.GetCorrespondingEntity(FaceInPart)
Set Point = Assembly.FeatureByName("Point1").GetSpecificFeature2()
Set CWAddinCallBackObj = SwApp.GetAddInObject("SldWorks.Simulation")
Set COSMOSWORKS = CWAddinCallBackObj.COSMOSWORKS
Set ActDoc = COSMOSWORKS.ActiveDoc
Set StudyMngr = ActDoc.StudyManager
bStudyFound = False
iNumberOfStudies = StudyMngr.StudyCount
For ix = 0 To iNumberOfStudies
StudyMngr.ActiveStudy = ix
Set Study = StudyMngr.GetStudy(ix)
If Study Is Nothing Then ErrorMsg SwApp, "Failed to get study number: " & ix: GoTo Lastline
If UCase(Study.Name) = UCase("x_rotation") Then
bStudyFound = True
Exit For
End If
Next
If bStudyFound = False Then
ErrorMsg SwApp, "Failed to find study named: " & sStudyName: GoTo Lastline
End If
Set StudyOptions = Study.NonLinearStudyOptions
Set CWFeatObj = Study.Results
DispArray1 = Array(Point)
DispArray2 = Array(FaceInAssembly)
Dim errCode As Long
Dim selectedFaces(0 To 0) As Object
Dim selectedAndModelReactionFM As Variant
Dim selectedOnlyReactionFM As Variant
Dim forces As Variant
Dim i As Long
stepNumber = CWFeatObj.GetMaximumAvailableSteps()
Force = CWFeatObj.GetFreeBodyForcesAndMomentsForAStep(Nothing, (DispArray1), (DispArray2), 0, stepNumber, errCode)
' Force = CWFeatObj.GetFreeBodyForcesAndMoments(Nothing, (DispArray1), (DispArray2), 0, errCode)
Debug.Print "Error code: "; errCode
Debug.Print "Free body forces and moments:"
Debug.Print " Free body force x-components of all selections: " & Force(0)
Debug.Print " Free body force y-components of all selections: " & Force(1)
Debug.Print " Free body force z-components of all selections: " & Force(2)
Debug.Print " Resultant free body force of all selections: " & Force(3)
Debug.Print " Free body moment x-components of all selections: " & Force(4)
Debug.Print " Free body moment y-components of all selections: " & Force(5)
Debug.Print " Free body moment z-components of all selections: " & Force(6)
Debug.Print " Resultant free body moment of all selections: " & Force(7)
Debug.Print " Free body force x-components of entire model: " & Force(8)
Debug.Print " Free body force y-components of entire model: " & Force(9)
Debug.Print " Free body force z-components of entire model: " & Force(10)
Debug.Print " Resultant free body force of entire model: " & Force(11)
Debug.Print " Free body moment x-components of entire model: " & Force(12)
Debug.Print " Free body moment y-components of entire model: " & Force(13)
Debug.Print " Free body moment z-components of entire model: " & Force(14)
Debug.Print " Resultant free body moment of entire model: " & Force(15)
Lastline:
End SubThe classic method GetFreeBodyForcesAndMoments doesn't work and returns the error code 4, saying this type of result is not part of the study.
The other method GetFreeBodyForcesAndMomentsForAStep makes SolidWorks crash. At first it displays an error window saying it is unable to read a write-only property. If I click Ok I can still use SolidWorks but the Macro is still running. Stopping the macro crashes SolidWorks.
If I replace the argument (DispArray1) by Null, the command makes SolidWorks crash directly, without the error message.
My version of SolidWorks is SolidWorks Design 2026 for Students.
