• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    Autodesk Inventor Customization

    Reply
    Distinguished Contributor
    Posts: 248
    Registered: ‎12-15-2003

    Mouse click event Inventor 2012 VBA is erratic(code worked fine in 2010)

    601 Views, 5 Replies
    01-19-2012 12:26 PM

    I have a macro that I used to get XY points on a drawing sheet in order to place items. Now that I am on 64bit 2012, it still functions, but it is very erratic and I have to get it to work by clicking the mouse repeatedly and much harder than usual. Eventually it takes and my symbol is placed! The crosshair cursor also blinks in and out during this time also. I am not selecting anything in the drawing, I just want to click on a blank area and get the drawing coordinates like I used to in previous versions. Wondering if there is some new method to get the mouse click and underlying xy point in 2012? Here is the code I have been using:

     

    'class module clsGetPoint2D
    
    Option Explicit
    Public X As Double, Y As Double, Z As Double
    Public JustifyRight As Boolean
    
    ' Declare the event objects
    Private WithEvents oInteractEvents As InteractionEvents
    Private WithEvents oMouseEvents As Inventor.MouseEvents
    ' Declare a flag that's used to determine when selection stops.
    Private bStillSelecting As Boolean
    _______________________________________________________________
    Public Function GetPoint() As Point
    ' Initialize flag.
    bStillSelecting = True
    
    ' Create an InteractionEvents object.
    Set oInteractEvents = ThisApplication.CommandManager.CreateInteractionEvents
    
    ' Define that we want mouse events rather than select events.
    oInteractEvents.SelectionActive = False
    
    oInteractEvents.StatusBarText = "Click mouse for 2D location"
    oInteractEvents.SetCursor (kCursorBuiltInCrosshair)
    ' Set a reference to the mouse events.
    Set oMouseEvents = oInteractEvents.MouseEvents
    
    'Don't need move events
    oMouseEvents.MouseMoveEnabled = False
    
    ' Start the InteractionEvents object.
    oInteractEvents.Start
    
    ' Loop until a (2D) point in the IDW is selected.
    Do While bStillSelecting
        ThisApplication.UserInterfaceManager.DoEvents
    Loop
    
    ' Stop the InteractionEvents object.
    oInteractEvents.Stop
    
    ' Clean up.
    Set oMouseEvents = Nothing
    Set oInteractEvents = Nothing
    End Function
    _____________________________________________________
    Private Sub oMouseEvents_OnMouseClick(ByVal Button As MouseButtonEnum, ByVal ShiftKeys As ShiftStateEnum, ByVal ModelPosition As Point, ByVal ViewPosition As Point2d, ByVal View As View)
        X = ModelPosition.X
        Y = ModelPosition.Y
        Z = ModelPosition.Z
        bStillSelecting = False
    End Sub

     

     

    Please use plain text.
    Distinguished Contributor
    Posts: 248
    Registered: ‎12-15-2003

    Re: Mouse click event Inventor 2012 VBA is erratic(code worked fine in 2010)

    01-20-2012 12:27 PM in reply to: rschader

    I have noticed today that if I slowly pan the drawing with my spacepilot while this macro is running, then the mouse clicking and cursor are not erratic anymore and I can place my symbols and leaders from the macro much easier. Since I am using an outdated spacepilot driver which I cannot update unless I buy a new, supported device, for now I will just have to assume this is a bug caused by the spaceball drivers in Inventor? It would be nice if there was someway to programatically ignore the spaceball in VBA to avoid this issue perhaps? Still would be willing to look at new methods of getting the sheet position of a mouse click also.

    Please use plain text.
    Active Contributor
    ChrisVandeVoorde
    Posts: 45
    Registered: ‎01-05-2011

    Re: Mouse click event Inventor 2012 VBA is erratic(code worked fine in 2010)

    12-19-2012 04:14 AM in reply to: rschader

    Hello,

     

    I want to use this code to get the coördinates of a point2D in a drawing view. But I can't get it to work.

    I use Inventor 2013.

     

    Can you help me on how to implement this code?

     

    thank you!

    Please use plain text.
    Distinguished Contributor
    Posts: 248
    Registered: ‎12-15-2003

    Re: Mouse click event Inventor 2012 VBA is erratic(code worked fine in 2010)

    12-20-2012 08:28 AM in reply to: rschader

    The code I posted is simply for a class module. You need to have macro code elsewhere that calls this class module. Simplest case, something like:

     

    Dim oGetPoint2D As New clsGetPoint2D

    Call oGetPoint2D.GetPoint

    MsgBox "X = " & oGetPoint2D.X & vbNewLine & "Y = " & oGetPoint2D.Y

     

    I don't have 2013, but it should still work unless they made some drastic changes?

    Please use plain text.
    ADN Support Specialist
    Posts: 207
    Registered: ‎03-26-2007

    Re: Mouse click event Inventor 2012 VBA is erratic(code worked fine in 2010)

    01-11-2013 07:53 AM in reply to: rschader

    Happy New Year!

    One thing I would try is avoiding DoEvents and lets see if that helps:

    Class1:

    ' members
    Private WithEvents oInteractionEvents As InteractionEvents
    Private WithEvents oMouseEvents As MouseEvents
    
    Public Sub StartInteraction()
    
        Set oInteractionEvents = ThisApplication.CommandManager.CreateInteractionEvents()
    
        ' subscribe to receive mouse events
        Set oMouseEvents = oInteractionEvents.MouseEvents
        
        oInteractionEvents.SelectionActive = False
    
        ' start interaction
        Call oInteractionEvents.Start
        
    End Sub
    
    Private Sub oMouseEvents_OnMouseClick(ByVal Button As MouseButtonEnum, ByVal ShiftKeys As ShiftStateEnum, ByVal ModelPosition As Point, ByVal ViewPosition As Point2d, ByVal View As View)
        Set oMouseEvents = Nothing
        Set oInteractionEvents = Nothing
    End Sub
    
    Private Sub oInteractionEvents_OnTerminate()
        ' Nothing got selected - the user started another
        ' command or just cancelled this interaction
        Set oMouseEvents = Nothing
        Set oInteractionEvents = Nothing
    End Sub

    Module1:

    Dim cls1 As Class1
    
    Sub CheckInteractionEvents()
        Set cls1 = New Class1
        Call cls1.StartInteraction
    End Sub

    Cheers,



    Adam Nagy
    Developer Technical Services
    Autodesk Developer Network
    Please use plain text.
    Distinguished Contributor
    Posts: 248
    Registered: ‎12-15-2003

    Re: Mouse click event Inventor 2012 VBA is erratic(code worked fine in 2010)

    01-11-2013 08:55 AM in reply to: adam.nagy

    I will have to try that when I get a chance! I had wanted to switch to using CommandManager.Pick instead, but could find no way to use it to just capture a mouse click without actually picking anything.

    Please use plain text.