Hide Table of Contents

Duplicate, Delete, and Create Motion Study Example (VBA)

This example shows how to create a duplicate motion study, delete an existing motion study, and create a new motion study.

 

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

'

' Preconditions: Model is open that has a

'                motion study named "Motion Study 1".

'                

' Postconditions: "Motion Study 1" is duplicated, then

'                deleted, and a new motion study for "Motion

'                Study 1" is created and saved as .avi file.

'

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

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

    ' Get the motion study named "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

    

    'Create a copy of the motion study

    swMotionStudy1.Duplicate

    

    ' Get the supported motion study types

    Dim MSTypes As Long

    boolstatus = swMotionStudy1.GetSupportedStudyTypes(MSTypes)

    Debug.Print ""

    Debug.Print "Supported study types: "

    Debug.Print "    Assembly: " & ((MSTypes And swMotionStudyType_e.swMotionStudyTypeAssembly) > 0)

    Debug.Print "    PhysicalSimulation: " & ((MSTypes And swMotionStudyType_e.swMotionStudyTypePhysicalSimulation) > 0)

    Debug.Print "    CosmosMotion: " & ((MSTypes And swMotionStudyType_e.swMotionStudyTypeCosmosMotion) > 0)

    Debug.Print "    LegacyCosmosMotion: " & ((MSTypes And swMotionStudyType_e.swMotionStudyTypeLegacyCosmosMotion) > 0)

    

    ' Get the current motion study type

    Dim CurStudyType As Long

    CurStudyType = swMotionStudy1.StudyType

    Debug.Print ""

    Debug.Print "Current study type: "

    Select Case CurStudyType

        Case swMotionStudyType_e.swMotionStudyTypeAssembly

            Debug.Print "    Assembly"

        Case swMotionStudyType_e.swMotionStudyTypePhysicalSimulation

            Debug.Print "    PhysicalSimulation"

        Case swMotionStudyType_e.swMotionStudyTypeCosmosMotion

            Debug.Print "    CosmosMotion"

        Case swMotionStudyType_e.swMotionStudyTypeLegacyCosmosMotion

            Debug.Print "    LegacyCosmosMotion"

    End Select

    

    'Is the motion study active?  If not, activate it

    If Not swMotionStudy1.IsActive Then swMotionStudy1.Activate

    

    'Create an animation of the rotating model

    'Delete any existing animation sequences,

    'set the animation duration to 10 seconds

    boolstatus = swMotionStudy1.CreateByRotateModel(True, swAnimatorAxisOfRotation_e.swRotationAboutYAxis, 1, swAnimatorDirectionOfRotation_e.swRotationClockwise, 10, 0)

 

    'Play the animation

    PlayAnimation swMotionStudy1

    

    'Stop playing the animation

    swMotionStudy1.Stop

    

    'Add an explode to the animation

    'Set the animation duration to 5 seconds

    boolstatus = swMotionStudy1.CreateByExplode(False, 5, 0)

    

    'Add to an collapse to the animation

    'Set the animation duration to 5 seconds

    boolstatus = swMotionStudy1.CreateByCollapse(False, 5, 5)

    

    'Set duration of animation to 15 seconds

    swMotionStudy1.SetDuration (15)

    

    'Calculate

    swMotionStudy1.Calculate

    

    Debug.Print ""

    Debug.Print "Study duration: " & swMotionStudy1.GetDuration

        

    'Play animation

    PlayAnimation swMotionStudy1

    

    'Set and save AVI parameters

    Set swSaveAVIData = swMotionMgr.CreateAVIParameter()

    swSaveAVIData.FramePerSecond = 7.5

    swSaveAVIData.SaveEntireAnimation = True

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

    swSaveAVIData.RendererType = swRendererType_e.swRendererType_Solidworks_Screen

    

    

    swMotionStudy1.Stop

    

    'Save animation as .avi file

    swMotionStudy1.SaveToAVI "C:\test\Anim1.avi", swSaveAVIData

    

End Sub

 

Private Sub PlayAnimation(swMotionStudy As swMotionStudy.MotionStudy)

    Debug.Print ""

    Dim cPlayMode As Long

    Debug.Print "Current play mode: "

    cPlayMode = swMotionStudy.PlayMode

    

    'Get current play mode

    Select Case cPlayMode

        Case 1

            Debug.Print "    Normal"

        Case 2

            Debug.Print "    Loop"

        Case 3

            Debug.Print "    Reciprocate"

    End Select

  

    'Set play mode to "Loop"

    Dim nPlayMode As Long

    Debug.Print "New play mode: "

    swMotionStudy.PlayMode = swAnimationPlayMode_e.swAnimationPlayModeLoop

    nPlayMode = swMotionStudy.PlayMode

    'Get new play mode

    Select Case nPlayMode

        Case 1

            Debug.Print "    Normal"

        Case 2

            Debug.Print "    Loop"

        Case 3

            Debug.Print "    Reciprocate"

    End Select

    

    'Set timebar to 0 on timeline

    swMotionStudy.SetTime 0

    'Play the animation

    swMotionStudy.Play

       

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:   Duplicate, Delete, and Create Motion Study 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) 2012 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.