Recording mouse click coordinates

I'm trying to write a program in VBA that draws a few predetermined elements after the user clicks somewhere to determine the starting point. I've found a similar post on this forum from 2012 and someone recommended looking at IPrimitiveCommandEvents and ILocateCommandEvents in the VBA help document, but I can't find them. Someone else posted this (the original poster was happy with it):

 Dim inputQueue As CadInputQueue

dim point1 as point3d

 Set inputQueue = CadInputQueue
  Set inputMessage = inputQueue.GetInput(msdCadInputTypeDataPoint, msdCadInputTypeAny)
    Do
        Select Case inputMessage.InputType
            Case msdCadInputTypeDataPoint
                point1 = inputMessage.point
                Exit Do
            Case msdCadInputTypeReset
                Exit Sub
        End Select
   Loop
CadInputQueue.SendCommand " place point"
CadInputQueue.SendDataPoint point1, 1

but I can't make much sense of it because I'm a novice, and I'm not sure how to incorporate this into my own program.

Parents
  • Hi Sam,

    please respect the best practices and specify used product and its exact version. Although VBA is nearly the same in V8i and CE, it's different in details, so to know the version can be important.

    Also, please use Insert > Insert code tool every time you want to share a code, because it ensures the code is properly displayed.

    and someone recommended looking at IPrimitiveCommandEvents and ILocateCommandEvents

    It is not "recommended", it is mandatory. When you want to interact with a user, including mouse events, you must to implement one from the mentioned objects.

    in the VBA help document

    It is not VBA document, but MicroStation VBA help, which is different file. If I remember right, by default this help is in the same folder as the produt exe.

    Someone else posted this

    It's wrong. Despite of it works, nearly no time anything like this is recommended. Of course, exceptions exist, but they are quite specific situations.

    and I'm not sure how to incorporate this into my own program.

    What is your "own program"?

    because I'm a novice

    Are you novice in MicroStation VBA or in VBA as the language? Without knowing VBA and MicroStation, it's hard and confusing to try to write MicroStation VBA code (macros).

    With regards,

      Jan

  •  

    Sub Bmrthiswontwork()
        Dim startPoint As Point3d
        Dim point As Point3d, point2 As Point3d
        Dim lngTemp As Long
        Dim oMessage As CadInputMessage
    
        CadInputQueue.SendKeyin "DMSG ACTIVATETOOLBYPATH -"
        CadInputQueue.SendKeyin "MDL KEYIN LSTYLE LINESTYLE SETTINGS SYNCHRONIZE"
        
        startPoint.X = 0
        startPoint.Y = 0
        startPoint.Z = 0#
        point.X = startPoint.X
        point.Y = startPoint.Y
        point.Z = startPoint.Z
        
        CadInputQueue.SendDataPoint startPoint, 1
        CadInputQueue.SendReset
    
        CadInputQueue.SendKeyin "DMSG ACTIVATETOOLBYPATH -"
        CadInputQueue.SendKeyin "MDL KEYIN LSTYLE LINESTYLE SETTINGS SYNCHRONIZE"
        
        point.X = startPoint.X + 5
        point.Y = startPoint.Y + 3.25
        CadInputQueue.SendDataPoint point, 1
        CadInputQueue.SendReset
    
        CadInputQueue.SendKeyin "DMSG ACTIVATETOOLBYPATH -"
        CadInputQueue.SendKeyin "MDL KEYIN LSTYLE LINESTYLE SETTINGS SYNCHRONIZE"
        
        CadInputQueue.SendDataPoint point, 1
        CadInputQueue.SendDataPoint startPoint, 1
        CadInputQueue.SendReset
    
        CadInputQueue.SendKeyin "DMSG ACTIVATETOOLBYPATH -"
        CadInputQueue.SendKeyin "MDL KEYIN LSTYLE LINESTYLE SETTINGS SYNCHRONIZE"
        
        point.X = startPoint.X + 1.5
        point.Y = startPoint.Y + 3.5
        CadInputQueue.SendDataPoint point, 1
    
        point.X = startPoint.X + 5
        point.Y = startPoint.Y + 3.25
        CadInputQueue.SendDataPoint point, 1
        
        point.X = point.X + 3.5
        point.Y = point.Y + 0.25
        CadInputQueue.SendDataPoint point, 1
        CadInputQueue.SendReset
    
        CommandState.StartDefaultCommand
    End Sub

    I'm using CE 10.17.01.58. I generated this using the macro recorder function and cleaned it up a bit. It functions as intended but instead of beginning at the origin, I'd prefer to begin wherever the user clicks. I found the correct VBA help file and I'm assuming what I'm looking for is the datapoint method of the primitive command tools, however the "DataPoint" hyperlink seems to be broken. 

    I have some experience with autocad and coding, but I'm fairly new to both microstation and VBA. I went through a basic VBA tutorial but I haven't had much luck finding microstation VBA tutorials, so I've been attempting to learn via relevant forum posts.

  • Hi ,

    As mentioned, the MicroStation VBA/COM Object Model extends the Microsoft Office VBA API Coding and Editing Environments.

    So, learning and building upon Microsoft Office VBA and MicroStation Product (User) experience and resources are common and essential pre-requisites towards learning MicroStation VBA more productively.

    Consider taking a look at some of our available online resources provided here: MicroStation VBA Getting Started Resources.  Note both the Bentley Learn and Amazon stores listed sell a hands-on book that may offer some additional help.

    HTH,
    Bob  



  • Hi Sam,

    I generated this using the macro recorder function and cleaned it up a bit.

    The macro recorder is a good tool when it is not clear e.g. what key-in is called in certain situation, but not good tool how to start with MicroStation VBA. The problem is that macro produces nothing else than a script, repeating users' events. But VBA is "like object" environment, so the right code looks differently.

    It functions as intended but instead of beginning at the origin, I'd prefer to begin wherever the user clicks.

    It is what primitive and locate objects do: They allow to implement own tool, behaving similarly as standard MicroStation tool. I do not write "the same", because VBA is simple very limited environment, so plenty of limitations exist.

    There is even option to listen every mouse click (even when "some tool" is active), but it requires deep knowledge of MicroStation internals and using C++.

    I found the correct VBA help file and I'm assuming what I'm looking for is the datapoint method of the primitive command tools,

    Without sharing e.g. screen capture, it's hard to guess what exactly you mean and what can be wrong, but in my MicroStation VBA help file, all links in IPrimitiveCommandEvents Object page work fine.

    so I've been attempting to learn via relevant forum posts.

    It can be both useful and dangerous, because programming discussions often focus on solution of a specific issue, or at least to find some workaround, but rarely how to write right code.

    but I haven't had much luck finding microstation VBA tutorials

    MicroStation VBA help contains plenty of examples, usually accessible from class description page. Although it does not provide overall picture of VBA environment and how VBA code for MicroStation should be structured, they are great for understanding how API works.

    MicroStation CE installation also contains MicroStation VBA examples (by default, they are installed to C:\ProgramData\Bentley\MicroStation CONNECT Edition\Configuration\WorkSpaces\Example\WorkSets\MetroStation\Standards\Macros\). Some from them cover advanced complex topics (like native/VBA interaction), but in general, all of them can be used as training materials and to analyzed them and try what and how they do.

    With regards,

      Jan

Reply
  • Hi Sam,

    I generated this using the macro recorder function and cleaned it up a bit.

    The macro recorder is a good tool when it is not clear e.g. what key-in is called in certain situation, but not good tool how to start with MicroStation VBA. The problem is that macro produces nothing else than a script, repeating users' events. But VBA is "like object" environment, so the right code looks differently.

    It functions as intended but instead of beginning at the origin, I'd prefer to begin wherever the user clicks.

    It is what primitive and locate objects do: They allow to implement own tool, behaving similarly as standard MicroStation tool. I do not write "the same", because VBA is simple very limited environment, so plenty of limitations exist.

    There is even option to listen every mouse click (even when "some tool" is active), but it requires deep knowledge of MicroStation internals and using C++.

    I found the correct VBA help file and I'm assuming what I'm looking for is the datapoint method of the primitive command tools,

    Without sharing e.g. screen capture, it's hard to guess what exactly you mean and what can be wrong, but in my MicroStation VBA help file, all links in IPrimitiveCommandEvents Object page work fine.

    so I've been attempting to learn via relevant forum posts.

    It can be both useful and dangerous, because programming discussions often focus on solution of a specific issue, or at least to find some workaround, but rarely how to write right code.

    but I haven't had much luck finding microstation VBA tutorials

    MicroStation VBA help contains plenty of examples, usually accessible from class description page. Although it does not provide overall picture of VBA environment and how VBA code for MicroStation should be structured, they are great for understanding how API works.

    MicroStation CE installation also contains MicroStation VBA examples (by default, they are installed to C:\ProgramData\Bentley\MicroStation CONNECT Edition\Configuration\WorkSpaces\Example\WorkSets\MetroStation\Standards\Macros\). Some from them cover advanced complex topics (like native/VBA interaction), but in general, all of them can be used as training materials and to analyzed them and try what and how they do.

    With regards,

      Jan

Children
No Data