Create Simulation Gravity Feature (VBA)
This example shows how to define a simulation gravity feature data object
and then create the feature using that data object.
'--------------------------------------------------------
'
' Preconditions: (1)
An assembly document is open in which
' "Motion
Study 1" exists.
' (2)
MotionManager tab is visible. If it is not visible,
' click
View, MotionManager.
' (3)
SolidWorks 2009 MotionStudy Type Library is
' selected
on Tools, References
in SolidWorks VBA.
'
'
' Postconditions: The Gravity feature is added to "Motion
Study 1".
'
'-------------------------------------------------------
Option Explicit
Sub main()
Dim
swApp As SldWorks.SldWorks
Dim
swModel As SldWorks.ModelDoc2
Dim
swModelDocExt As SldWorks.ModelDocExtension
Dim
swMotionMgr As SwMotionStudy.MotionStudyManager
Dim
swMotionStudy1 As SwMotionStudy.MotionStudy
Dim
swGravityFeat As SldWorks.SimulationGravityFeatureData
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 and activate "Motion Study 1"
Set
swMotionStudy1 = swMotionMgr.GetMotionStudy("Motion
Study 1")
If
(swMotionStudy1 Is Nothing) Then
MsgBox
"Motion Study 1 is
not available."
End
End
If
'
Activate "Motion Study 1"
swMotionStudy1.Activate
'
Define Gravity feature
Set
swGravityFeat = swMotionStudy1.CreateDefinition(swFmAEMGravity)
If
swGravityFeat Is Nothing Then
Debug.Print
"ERROR: Creation of Gravity feature data object failed."
Exit
Sub
End
If
swGravityFeat.ReverseDirection = False
swGravityFeat.Axis = 0
swGravityFeat.Strength = 12
'
Create Gravity feature
Set
swFeat = swMotionStudy1.CreateFeature(swGravityFeat)
If
swFeat Is Nothing Then
Debug.Print
" ERROR: Creation of the Gravity feature failed."
Else
Debug.Print
"Name of the feature added : " & swFeat.Name
End
If
End Sub