Create Linear Motor Feature (VBA)
This example shows how to create a motor feature data object and a linear
motor feature.
'-----------------------------------
' Preconditions: Assembly document with at least two
' components
is open. SOLIDWORKS MotionStudy
' type
library is referenced.
'
' Postconditions: Linear motor 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
swMotorFeat As SldWorks.SimulationMotorFeatureData
Dim
swGravityFeat As Object
Dim
boolstatus As Boolean
Dim
swFeat As SldWorks.Feature
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swModelDocExt = swModel.Extension
Set
swSelMgr = swModel.SelectionManager
'
Get the MotionManager
Set
swMotionMgr = swModelDocExt.GetMotionStudyManager()
If
(swMotionMgr Is Nothing) Then
End
End
If
'
Get Motion Study 3
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 face on Part1
boolstatus
= swModelDocExt.SelectByID2("",
"FACE", -0.07792618280496, 0.06212618843159, 0.02214691016243,
False, 0, Nothing, 0)
'
Create linear motor feature data object definition
Set
swMotorFeat = swMotionStudy3.CreateDefinition(swFmAEMLinearMotor)
If
swMotorFeat Is Nothing Then
Debug.Print
"ERROR: Creation of motor feature data object failed."
Exit
Sub
End
If
'
Set some options
swMotorFeat.InterpolatedMotor swSimulationMotorDrive_Velocity,
1
swMotorFeat.DirectionReference = swSelMgr.GetSelectedObject6(1,
-1)
boolstatus
= swMotorFeat.LoadSplineData("Test_bouncingBall.csv")
'
Select Part1
Dim
RelObj As SldWorks.Component2
Set
RelObj = swSelMgr.GetSelectedObjectsComponent3(1,
-1)
'
Set more options
'
Select face on Part2
boolstatus
= swModelDocExt.SelectByID2("",
"FACE", -0.07924982844941, 0.06212618843165, 0.03225592518596,
False, 0, Nothing, 0)
swMotorFeat.RelativeComponent = RelObj
swMotorFeat.Location = swSelMgr.GetSelectedObject6(1,
-1)
'
Select same face on Part1 as previously selected
boolstatus
= swModelDocExt.SelectByID2("",
"FACE", -0.07792618280496, 0.06212618843159, 0.02214691016243,
False, 0, Nothing, 0)
Dim
ContactObj(0) As Object
Set
ContactObj(0) = swSelMgr.GetSelectedObject6(1,
-1)
'Cast
ContactObj to a Variant
Dim
vContact As Variant
vContact
= ContactObj
'
Set motor feature's load references, the load bearing faces
swMotorFeat.LoadReferances = vContact
'
Print the motor type
Debug.Print
swMotorFeat.MotorType
'
Create the linear motor feature
Set
swFeat = swMotionStudy3.CreateFeature(swMotorFeat)
If
swFeat Is Nothing Then
Debug.Print
" ERROR: Creation of the motor feature failed."
Else
Debug.Print
"Name of the feature added : " & swFeat.Name
End
If
End Sub