Hide Table of Contents

Run SOLIDWORKS Commands and Synthesize Mouse Events Example (VB.NET)

This example shows how to run SOLIDWORKS commands and synthesize mouse events.

'-----------------------------------------------------------
' Preconditions:
' 1. Verify that the specified part document to open exists.
' 2. Click Tools > Options and click to clear the Stop VSTA debugger 
'    on macro exit check box, if it is selected.
' 3. In the IDE, right-click the project name in the Project Explorer,
'    click Add Reference, browse install_dir\api\redist\CLR2, click 
'    SolidWorks.Interop.swcommands.dll, and click OK.
' 4. Open the Immediate window.
'
' Postconditions:
' 1. Opens the specified part document.
' 2. Fits the model to the graphics area.
' 3. Moves the mouse.
' 4. Changes the view orientation to *Front.
' 5. Select an edge on the model.
' 6. Left-click anywhere outside the model in the graphics area.
' 7. Examine the Immediate window.
' 8. Click Stop Debugging in the IDE.
' 9. Click Tools > Options and click to select the Stop VSTA debugger 
'    on macro exit check box, if it is clear.
'-----------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports SolidWorks.Interop.swcommands
Imports System.Runtime.InteropServices
Imports System
Imports System.Diagnostics
 
Partial Class SolidWorksMacro
 
    Public WithEvents msMouse As Mouse
 
    Public Sub main()
 
        Dim swModel As ModelDoc2
        Dim swModelDocExt As ModelDocExtension
        Dim swModelView As ModelView
        Dim TheMouse As Mouse
        Dim i As Integer
        Dim x As Double
        Dim fileName As String
        Dim errors As Integer
        Dim warnings As Integer
        Dim status As Boolean 
 
        fileName = "C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\api\block20.sldprt"
        swModel = swApp.OpenDoc6(fileName, swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
        swModelDocExt = swModel.Extension
        swModelView = swModel.GetFirstModelView
        TheMouse = swModelView.GetMouse
 
        'Set up events
        msMouse = TheMouse
        AttachEventHandlers()
 
        x = 0
 
        Debug.Print("Fit model to graphics area")
        swModelDocExt.RunCommand(swCommands_e.swCommands_ZoomToFit, "")
 
        'Move the mouse
        status = TheMouse.Move(50, 150, swMouse_e.swMouse_Absolute + swMouse_e.swMouse_MouseMove + swMouse_e.swMouse_LeftDown)
        Debug.Print("First call to Move: " & status)
        Debug.Print("Calls to Move within loop:")
        For i = 1 To 5
            status = TheMouse.Move(5, 5, swMouse_e.swMouse_MouseMove)
            Debug.Print("  Call " & i & " to Move: " & status)
        Next
        status = TheMouse.Move(0, 0, swMouse_e.swMouse_LeftUp)
        Debug.Print("Last call to Move: " & status)
 
        status = TheMouse.MoveXYZ(0.03720615681732, 0.0316583060694, 0.04991700841805, swMouse_e.swMouse_LeftDown)
        Debug.Print("Call to MoveXYZ: " & status)
        Debug.Print("Calls to Move within next loop:")
        For i = 1 To 5
            status = TheMouse.Move(5, 5, swMouse_e.swMouse_MouseMove)
            Debug.Print("  Call " & (i + 1).ToString & " to Move: " & status)
        Next
 
        status = TheMouse.Move(10, 10, swMouse_e.swMouse_LeftUp)
        Debug.Print("Last call to Move: " & status)
 
        Debug.Print("Change view to *Front")
        swModelDocExt.RunCommand(swCommands_e.swCommands_Front, "")
 
    End Sub
 
    Sub AttachEventHandlers()
        AttachSWEvents()
    End Sub
 
    Sub AttachSWEvents()
 
        AddHandler msMouse.MouseSelectNotify, AddressOf Me.ms_MouseSelectNotify
        AddHandler msMouse.MouseLBtnDownNotify, AddressOf Me.ms_MouseLBtnDownNotify
 
    End Sub
 
    Private Function ms_MouseSelectNotify(ByVal Ix As IntegerByVal Iy As IntegerByVal x As DoubleByVal y As DoubleByVal z As DoubleAs Integer
        Debug.Print("Selection made:")
        Debug.Print(" Mouse location:")
        Debug.Print("   Window space coordinates:")
        Debug.Print("     " & Ix)
        Debug.Print("     " & Iy)
        Debug.Print("   World space coordinates:")
        Debug.Print("     " & x)
        Debug.Print("     " & y)
        Debug.Print("     " & z)
    End Function
 
    Private Function ms_MouseLBtnDownNotify(ByVal x As IntegerByVal y As IntegerByVal WParam As IntegerAs Integer
        Debug.Print("Left-mouse button pressed.")
    End Function
 
 
    ''' <summary>
    ''' The SldWorks swApp variable is pre-assigned for you.
    ''' </summary>
    Public swApp As SldWorks
 
 
End Class


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:   Run SOLIDWORKS Commands and Synthesize Mouse Events Example (VB.NET)
*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) 2016 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.