Hide Table of Contents

Create Motion Studies Example (VBA)

This example shows how to create, rename, and delete motion studies.

 

'--------------------------------------------------------

'

' Preconditions:

'                 (1) Part or assembly is open.

'                 (2) MotionManager tab is visible. If it is not visible,

'                     click View, MotionManager.

'                 (3) SOLIDWORKS nnnn MotionStudy Type Library, where

                      nnnn is the version number of the release, is

'                     selected on Tools, References.

'

' Postconditions: Two motion studies exist: Motion Study 1 and TestName.

'

'-------------------------------------------------------

Option Explicit

 

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 swMotionStudy2 As SwMotionStudy.MotionStudy

Dim swMotionStudy3 As SwMotionStudy.MotionStudy

Dim swSaveAVIData As SwMotionStudy.AVIParameter

Dim boolstatus As Boolean

 

Sub main()

 

    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

    

    ' Create new motion studies

    Set swMotionStudy2 = swMotionMgr.CreateMotionStudy()

    If (swMotionStudy2 Is Nothing) Then

        MsgBox "Motion Study 2 is not available."

        End

    End If

    

    Set swMotionStudy3 = swMotionMgr.CreateMotionStudy()

    If (swMotionStudy3 Is Nothing) Then

        MsgBox "Motion Study 3 is not available."

        End

    End If

    

    ' Get number of motion studies

    Debug.Print "Number of motion studies = " & swMotionMgr.GetMotionStudyCount

    

    ' Get motion study

    Set swMotionStudy1 = swMotionMgr.GetMotionStudy("Motion Study 1")

    If (swMotionStudy1 Is Nothing) Then

        MsgBox "Motion Study 1 is not available."

        End

    End If

        

   

    ' Get number and names of motion studies and whether they've been activated

    Dim vNames As Variant

    Dim i As Long

    vNames = swMotionMgr.GetMotionStudyNames()

    For i = 0 To UBound(vNames)

        Debug.Print vNames(i);

        boolstatus = swMotionMgr.ActivateMotionStudy(vNames(i))

        Debug.Print " activated = " & boolstatus

    Next i

    

   ' Attempt to activate a non-existing motion study

    boolstatus = swMotionMgr.ActivateMotionStudy("Motion Study 4") ' Should return False

    Debug.Print "Motion Study 4 activated = " & boolstatus

    

    ' Delete Motion Study2 and non-existing motion study

    boolstatus = swMotionMgr.DeleteMotionStudy("Motion Study 2")

        Debug.Print "Motion Study 2 deleted = " & boolstatus

    boolstatus = swMotionMgr.DeleteMotionStudy("Motion Study 4")

        Debug.Print "Motion Study 4 deleted = " & boolstatus ' Should return False

    

    ' Rename Motion Study 3 to TestName

    swMotionStudy3.Name = "TestName"

        

    ' Set and save AVI parameters

    Set swSaveAVIData = swMotionMgr.CreateAVIParameter()

    swSaveAVIData.FramePerSecond = 7.5

    swSaveAVIData.SaveEntireAnimation = True

    swSaveAVIData.OutputType = 1 ' Save as an .avi file

 

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

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Create Motion Studies Example (VBA)
*Comment:  
*   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:



x

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
x

Web Help Content Version: API Help (English only) 2018 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.