Hide Table of Contents

Run SOLIDWORKS Commands and Synthesize Mouse Events Example (VBA)

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

'-----------------------------------------------------------
' Preconditions:
' 1. Verify that the specified part document to open exists.
' 2. Right-click the project name in the Project Explorer and click
'    Insert > Class Module.
' 3. Copy Module to the main module.
' 4. Copy Class1 to the class module.
' 5. In the IDE, click Tools > References, click
'    SOLIDWORKS version Commands type library, and click OK.
' 6. 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. Click Reset in the IDE to stop executing the macro.
' 8. Examine the Immediate window.
'-----------------------------------------------------------
'Module
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swModelView As SldWorks.ModelView
Dim swMouse As SldWorks.mouse
Dim TheMouse As SldWorks.mouse
Dim obj As New Class1
Dim i As Long
Dim x As Double
Dim fileName As String
Dim errors As Long
Dim warnings As Long
Dim status As Boolean
Sub main()
    Set swApp = Application.SldWorks
    fileName = "C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2018\samples\tutorial\api\block20.sldprt"
    Set swModel = swApp.OpenDoc6(fileName, swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
    Set swModelDocExt = swModel.Extension
    Set swModelView = swModel.GetFirstModelView
    Set TheMouse = swModelView.GetMouse    
    obj.init TheMouse    
    x = 0    
    Debug.Print "Fit model to graphics area"
    swModelDocExt.RunCommand swCommands_ZoomToFit, ""
    
    'Move the mouse
    status = TheMouse.Move(50, 150, swMouse_Absolute + swMouse_MouseMove + swMouse_LeftDown)
    Debug.Print ("First call to Move: " & status)
       Debug.Print "Calls to Move within loop:"
       For i = 1 To 5
           DoEvents
           status = TheMouse.Move(5, 5, swMouse_MouseMove)
           Debug.Print ("  Call " & i & " to Move: " & status)
        Next
       status = TheMouse.Move(0, 0, swMouse_LeftUp)
       Debug.Print ("Last call to Move: " & status)      

           Debug.Print("Call to MoveXYZ: " & status)
           status = TheMouse.MoveXYZ(0.03720615681732, 0.0316583060694, 0.04991700841805, swMouse_LeftDown)
           Debug.Print ("Calls to Move within next loop: ")                
           For i = 1 To 5
               DoEvents
               status = TheMouse.Move(5, 5, swMouse_MouseMove)
               Debug.Print "    Call " & i + 1; " to Move: " & " & status
           Next

               status = TheMouse.Move(0, 0, swMouse_LeftUp)
               Debug.Print ("Call to Move after loop: " & status)            
       Debug.Print "Change view orientation to *Front"
       swModelDocExt.RunCommand swCommands_Front, ""   
 End Sub

'Class1

Dim WithEvents ms As SldWorks.mouse

Private Sub Class_Initialize()
End Sub
 

Public Sub init(mouse As Object)
    Set ms = mouse
End Sub



Private Function ms_MouseSelectNotify(ByVal Ix As Long, ByVal Iy As Long, ByVal x As Double, ByVal y As Double, ByVal z As Double) As Long
    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 Long, ByVal y As Long, ByVal WParam As Long) As Long
    Debug.Print "Left-mouse button pressed."
End Function
 

Back to top



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 (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) 2019 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.