Create Force Feature (VBA)
This example shows how to create a force feature for use with Basic
Motion and Motion Analysis studies.
'------------------------------
' Preconditions: An assembly is open and Motion Study
3 exists.
' SolidWorks
MotionStudy type library is referenced.
'
' Postconditions: A Force feature is created.
'------------------------------
Option Explicit
Sub main()
Dim
swApp As SldWorks.SldWorks
Dim
swModel As SldWorks.ModelDoc2
Dim
swModelDocExt As SldWorks.ModelDocExtension
Dim
swSelMgr As SldWorks.SelectionMgr
Dim
swMotionMgr As SwMotionStudy.MotionStudyManager
Dim
swMotionStudy3 As SwMotionStudy.MotionStudy
Dim
swForceFeat As SldWorks.SimulationForceFeatureData
Dim
boolstatus As Boolean
Dim
swFeat As SldWorks.Feature
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swModelDocExt = swModel.Extension
'
Get the MotionManager
Set
swMotionMgr = swModelDocExt.GetMotionStudyManager()
If
(swMotionMgr Is Nothing) Then
End
End
If
'
Get Motion Study 3, which must must exist
Set
swMotionStudy3 = swMotionMgr.GetMotionStudy("Motion
Study 3")
If
(swMotionStudy3 Is Nothing) Then
MsgBox
"Motion Study 3 is not available."
End
End
If
'
ACtivate Motion Study 3
swMotionStudy3.Activate
'
Select a face
boolstatus
= swModelDocExt.SelectByID2("",
"FACE", -0.07792618280496, 0.06212618843159, 0.02214691016243,
False, 0, Nothing, 0)
'
Create the definition for the Force feature
Set
swForceFeat = swMotionStudy3.CreateDefinition(swFmAEMLinearForce)
If
swForceFeat Is Nothing Then
Debug.Print
"ERROR: Creation of force feature data object failed."
Exit
Sub
End
If
'
Define the Force feature
swForceFeat.ActionType = swSimulationForceAction_ActionOnly
Set
swSelMgr = swModel.SelectionManager
'
Get the selected face
swForceFeat.ActionLocation = swSelMgr.GetSelectedObject6(1,
-1)
Dim
RelObj As Component2
'
Get the component to which the just selected face belongs
Set
RelObj = swSelMgr.GetSelectedObjectsComponent3(1,
-1)
'
Select a face on a component and create the force feature relative to
this component
boolstatus
= swModelDocExt.SelectByID2("",
"FACE", -0.07924982844941, 0.06212618843165, 0.03225592518596,
False, 0, Nothing, 0)
swForceFeat.ReferenceComponent = RelObj
'
Create a force feature
Set
swFeat = swMotionStudy3.CreateFeature(swForceFeat)
If
swFeat Is Nothing Then
Debug.Print
" ERROR: Creation of the force feature failed."
Else
Debug.Print
"Name of the feature added : " & swFeat.Name
End
If
End Sub