Hide Table of Contents

Create Plots and Get Values (VB.NET)

This example shows how to get the translational displacement and velocity values for a selected face and how to plot these values.


' Preconditions:
' 1. SOLIDWORKS Premium, which includes SOLIDWORKS Motion,
'    is installed.

'    a. SOLIDWORKS Motion Study add-in is loaded (in
'       SOLIDWORKS, click Tools > Add-Ins > SOLIDWORKS Motion.
'    b. Open:
'       <SOLIDWORKS_install_dir>\samples\tutorial\motionstudies\valve_cam.sldasm.
'    c. Click the 1200 Motion Study tab in the lower-left corner of
'       the MotionManager.
'    d. Select Motion Analysis in the Type of Study list at the
'       upper-left corner of the MotionManager.
' 3. In the IDE:
'    a. SOLIDWORKS Motion Study primary interop assembly is referenced
'       (right-click the name of the project in the Project Explorer,
'       select Add Reference, browse to <SOLIDWORKS_install_dir>\api\redist\CLR2,
'       and select SolidWorks.Interop.swmotionstudy.dll)
'    b. Open the Immediate window.
'    c. Run the macro.
' Postconditions:
' 1. Motion analysis is calculated for the
'    two selected faces.
' 2. Translational displacement and translational velocity
'    values for the face first selected are printed
'    to the Immediate window.
' 3. Translational displacement and translational velocity
'    plots are inserted and displayed in SOLIDWORKS.
' 4. Names of the plot features are printed to the Immediate window.
' NOTE: Because the assembly is used in a SOLIDWORKS tutorial,
' do not save any changes when closing the model.


Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports SolidWorks.Interop.swmotionstudy
Imports System
Imports System.Diagnostics

Partial Class SolidWorksMacro

Public Sub Main()

Dim swModel As ModelDoc2
Dim swSelMgr As SelectionMgr
Dim swModelDocExt As ModelDocExtension
Dim MotionMgr As MotionStudyManager
Dim MotionStudy As MotionStudy
Dim MotionStudyResults As CosmosMotionStudyResults
Dim status As Boolean
        Dim swSimPlotFeatureData As MotionPlotFeatureData
Dim swSimPlotXAxisFeatureData As MotionPlotAxisFeatureData
Dim swSimPlotYAxisFeatureData(0 To 1) As MotionPlotAxisFeatureData
Dim swFace(0 To 0) As Face2
Dim swFaceArray() As Object
        Dim swYAxisArray() As Object
        Dim PlotOutput As MotionPlotFeatureOutput
Dim swPlotFeature1 As Feature
Dim swPlotFeature2 As Feature
Dim swXData As Object
        Dim swYData As Object
        Dim nameYAxis(0 To 1) As String
        Dim i As Integer
        Dim j As Integer

        swModel = swApp.ActiveDoc
        swSelMgr = swModel.SelectionManager
        swModelDocExt = swModel.Extension

' Select the faces for which to calculate
        ' motion analysis, then calculate it and
        ' get the results
        swModel.ShowNamedView2("*Right", 4)
        status = swModelDocExt.SelectByID2(
"", "FACE", 0.03426699306681, 0.03342024416822, 0.02599934303839, True, 0, Nothing, 0)
        status = swModelDocExt.SelectByID2(
"", "FACE", 0.03047373686337, 0.006937653650944, 0.02566622869226, True, 0, Nothing, 0)
        MotionMgr = swModelDocExt.GetMotionStudyManager()
        MotionStudy = MotionMgr.GetMotionStudy(
        status = MotionStudy.Calculate
        MotionStudyResults = MotionStudy.GetResults(4)

' Create a plot feature data and create the  x and y axes feature data
        swSimPlotFeatureData = MotionStudyResults.CreatePlotFeatureData()
        swSimPlotXAxisFeatureData = MotionStudyResults.CreatePlotXAxisFeatureData()
        swSimPlotYAxisFeatureData(0) = MotionStudyResults.CreatePlotYAxisFeatureData()
        swSimPlotYAxisFeatureData(1) = MotionStudyResults.CreatePlotYAxisFeatureData()

' Set the type of plots
        nameYAxis(0) = "swMotionPlotAxisType_TRANS_DISP"
        swSimPlotYAxisFeatureData(0).Type = swMotionPlotAxisType_e.swMotionPlotAxisType_TRANS_DISP
        swSimPlotYAxisFeatureData(0).Component = 1
        nameYAxis(1) =
        swSimPlotYAxisFeatureData(1).Type = swMotionPlotAxisType_e.swMotionPlotAxisType_TRANS_VELOCITY
        swSimPlotYAxisFeatureData(1).Component = 1

' Get the entity whose motion you want analyzed
        swFace(0) = swSelMgr.GetSelectedObject6(1, -1)
        swFaceArray = swFace
        swSimPlotYAxisFeatureData(0).Entities = swFaceArray
        swSimPlotYAxisFeatureData(1).Entities = swFaceArray

' Get the plot's x-axis and y-axes values
        swYAxisArray = swSimPlotYAxisFeatureData
        PlotOutput = MotionStudyResults.GetValues(swSimPlotFeatureData, swSimPlotXAxisFeatureData, (swYAxisArray))
        swXData = PlotOutput.GetXAxis()

' Print the x-axis values and the y-axis translational
        ' displacement values and the y-axes translational velocity
        ' values to the Immediate window
For i = 0 To UBound(swYAxisArray)
"------ YAxis Type : " & nameYAxis(i))
            swYData = PlotOutput.GetYAxis(swSimPlotYAxisFeatureData(i))
For j = 0 To UBound(swXData)
                 Debug.Print(" (x, y) : (" & Strings.Format(swXData(j) & ", " & swYData(j) & ")"))
Next j
Next i

' Insert and display the translational displacement plot
        swPlotFeature1 = MotionStudyResults.InsertPlotFeature(swSimPlotFeatureData, swSimPlotXAxisFeatureData, swSimPlotYAxisFeatureData(0))
         Debug.Print("Name of plot feature: " & swPlotFeature1.Name)

' Insert and display the translational velocity plot
        swPlotFeature2 = MotionStudyResults.InsertPlotFeature(swSimPlotFeatureData, swSimPlotXAxisFeatureData, swSimPlotYAxisFeatureData(1))
         Debug.Print("Name of plot feature: " & swPlotFeature2.Name)

End Sub

    ''' <summary>
    ''' The SldWorks swApp variable is pre-assigned for you.
    ''' </summary>
    Public swApp As SldWorks




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:   Creates Plots and Get Values (VB.NET)
*   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) 2015 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.