Hide Table of Contents

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 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 If


    ' Activate Motion Study 3




    ' 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."


        Debug.Print "Name of the feature added : " & swFeat.Name

    End If

End Sub

Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Create Linear Motor Feature Exampe VB
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2010 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.