Hide Table of Contents

Get Motion Study Example (VBA)

This example shows how to get a Physical Simulation motion study and its properties.

'-----------------------------------------
' Preconditions:
' 1. Open a model that has a Physical Simulation motion study
'    named PhysSim.
' 2. In SOLIDWORKS, load the SOLIDWORKS Motion Study add-in 
'    (click Tools > Add-Ins > SOLIDWORKS Motion).
' 3. In the SOLIDWORKS VBA IDE, add a reference to the 
'    SolidWorks MotionStudy type library.
' 4. Open the Immediate window.
'
' Postconditions:
' 1. Gets the MotionManager.
' 2. Gets and activates the PhysSim motion study.
' 3. Gets some motion study properties and features.
' 4. Plays the motion study.
' 5. In SOLIDWORKS, click Stop in the MotionManager
'    to stop playing the motion study.
' 6. Gets the motion study results and whether the
'    results are out of date.
' 7. Examine the Immediate window.
'-----------------------------------------
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 swResults As SwMotionStudy.MotionStudyResults
Dim swMotionStudyProps As SwMotionStudy.MotionStudyProperties
Dim swPhysicalSimProps As SwMotionStudy.PhysicalSimulationMotionStudyProperties
Dim boolstatus As Boolean
Dim i As Long
Dim vMotionFeatures As Variant
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 motion study named PhysSim
    Set swMotionStudy1 = swMotionMgr.GetMotionStudy("PhysSim")
    If (swMotionStudy1 Is Nothing) Then
        MsgBox "PhysSim is not available."
        End
    End If
    swMotionStudy1.StudyType = swMotionStudyType_e.swMotionStudyTypePhysicalSimulation
    'Active the motion study
    If Not swMotionStudy1.IsActive Then swMotionStudy1.Activate
    Debug.Print "Duration of motion study: " & swMotionStudy1.GetDuration
    Debug.Print "Timebar position on timeline: " & swMotionStudy1.GetTime
    Debug.Print "Number of motion features: " & swMotionStudy1.GetMotionFeaturesCount
    Set swMotionStudyProps = swMotionStudy1.GetProperties(2)
    Set swPhysicalSimProps = swMotionStudyProps
    Debug.Print ""
    Debug.Print "Physical simulation properties: "
    Debug.Print "    Frame rate: " & swMotionStudyProps.GetFrameRate
    Debug.Print "    Geometric accuracy: " & swPhysicalSimProps.GeometricAccuracy
    Debug.Print "    Mate accuracy: " & swPhysicalSimProps.MateAccuracy
    swPhysicalSimProps.MateAccuracy = 1
    vMotionFeatures = swMotionStudy1.GetMotionFeatures
    For i = 0 To UBound(vMotionFeatures)
    Dim swFeat As Feature
        Set swFeat = vMotionFeatures(i)
        PhysicalSimFeatureTests swFeat, swModel
    Next i
    'Calculate
    swMotionStudy1.Calculate
    WaitForCaluculation swMotionStudy1
    Set swResults = swMotionStudy1.GetResults(2)
    Debug.Print ""
    Debug.Print "Are the motion study results out of date? " & swResults.IsOutOfDate
End Sub
Private Sub WaitForCaluculation(SwMotionStudy As MotionStudy)
    Do While SwMotionStudy.IsPlaying
        DoEvents
    Loop
End Sub
 
Private Sub PhysicalSimFeatureTests(swFeat As Feature, TopDoc As ModelDoc2)
    Dim SimTypeName As String
    Dim SimulationGravityFeatureData As SldWorks.SimulationGravityFeatureData
    SimTypeName = swFeat.GetTypeName
    Debug.Print ""
        ' Print the name of the type of simulation and its gravity strength
        Select Case SimTypeName
        Case "AEMGravity"
            Debug.Print "    Type of feature: AEMGravity"
        Case "AEMLinearMotor"
            Debug.Print "    Type of feature: AEMLinearMotor"
        Case "AEMRotationalMotor"
            Debug.Print "    Type of feature: AEMRotationalMotor"
        Case "AEMLinearSpring"
            Debug.Print "    Type of feature: AEMLinearSpring"
        Case Else
            Debug.Print "    Type of feature: " & SimTypeName
            MsgBox SimTypeName
    End Select
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:   Get 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) 2021 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.