Get mouse position using iLogic

Get mouse position using iLogic

meck
Collaborator Collaborator
4,859 Views
27 Replies
Message 1 of 28

Get mouse position using iLogic

meck
Collaborator
Collaborator

I need to pick a point either in space or on the boarder of a drawing form, and get the position of the mouse where I clicked. I realize there are no WithEvents available in iLogic.

 

I have a routine using VBA that will do what I want when ran by itself. But when I call the routing from iLogic using InventorVb.RunMacro("DocumentProject", "modGetPoint", "TestGetDrawingPoint") it won't accept the mouse click it just stays in the Do Loop.

 

The VBA code is the code written by Brian Ekins in this post...

https://forums.autodesk.com/t5/inventor-customization/selecting-a-point2d-with-your-mouse-on-a-drawi...

 

All I did was try to call the routing from iLogic.

If someone has an answer, or if there is any other possible work around I'd really appreciate it. 

 

Thanks,

Mike

 

 

 

 

 

Mike Eck
Master Drafter/ CAD Programmer
Using Inventor 2018
0 Likes
Accepted solutions (1)
4,860 Views
27 Replies
Replies (27)
Message 2 of 28

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@meck,

 

Try the following iLogic code to get mouse position.

 

Sub Main()
 Dim oInteraction  As InteractionEvents 
 oInteraction = ThisApplication.CommandManager.CreateInteractionEvents

 Dim oMouse As MouseEvents
 oMouse = oInteraction.MouseEvents
  AddHandler oMouse.OnMouseClick  ,AddressOf oMouse_OnMouseDown 

  oInteraction.Start

 
End Sub

Sub oMouse_OnMouseDown(Button As MouseButtonEnum, ShiftKeys As ShiftStateEnum, ModelPosition As Point, ViewPosition As Point2d, View As Inventor.View)
  
  MessageBox.Show(ModelPosition.X & " in cm", "X")
  MessageBox.Show(ModelPosition.Y & " in cm", "Y")
  
End Sub

Please feel free to contact if there is any queries.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 3 of 28

meck
Collaborator
Collaborator

Hi All,

I'm revisiting this post, because I have been experiencing a weird quark with my code.

So in trying to debug this I finally just dumped Chandra Shekar's code into a ilogic rule all by itself. I noticed that the code does not pause to let me do a mouse click.

I tried adding a do loop, but it just ended up in an endless loop and never excepted the mouse click.

Is there some code I need to include to force it to wait until a mouse click has been preformed?

Thanks in advance!

Mike Eck
Master Drafter/ CAD Programmer
Using Inventor 2018
0 Likes
Message 4 of 28

chandra.shekar.g
Autodesk Support
Autodesk Support

@meck,

 

Can you please share non confidential iLogic code to test the behavior?

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 5 of 28

meck
Collaborator
Collaborator

Thanks for the response Chandra!

As I said I just pasted your code into a rule and hit Save & Run and nothing happened. 

 

I believe the drawing form is not getting focus, because if I click the form first, and then right click the rule and select Run Rule the code works.

 

I tried activating the form by placing ActiveSheet = ThisDrawing.Sheet("Sheet1:1") directly under the Sub Main, and it works intermittingly. I really need it to be rock solid.

 

Any help would be greatly appreciated!

Thanks,

Mike

 

Mike Eck
Master Drafter/ CAD Programmer
Using Inventor 2018
0 Likes
Message 6 of 28

chandra.shekar.g
Autodesk Support
Autodesk Support

@meck,

 

If you can provide non confidential drawing with form, I can test the feasibility.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 7 of 28

meck
Collaborator
Collaborator

Thanks for the response Chandra!

Attached is a drawing form with the code in a rule called Mouse Click.

 

I hope this helps.

Mike

Mike Eck
Master Drafter/ CAD Programmer
Using Inventor 2018
0 Likes
Message 8 of 28

MjDeck
Autodesk
Autodesk

Hi Mike,

 I'm wondering why it's important to have this work when you hit Save & Run. To work around this limitation, you can hit Save and then run the rule manually.

 Is there a workflow that doesn't involve Save & Run where this rule only works intermittently? If so, can you provide more details?


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 9 of 28

meck
Collaborator
Collaborator

My apologies for the confusion.

I am the only one here at my company that knows anything about programming. So to make it easy on the other designers I created an iLogic form with a checkbox on it. The checkbox acts as the trigger to fire the rule. I would dump the rule directly onto the form as a button, but using a checkbox ensures that the rule is not ran if other parameters in the rule have changed inadvertently. I set the checkbox back to false in the rule so that the rule can be ran again if necessary.

I would not think triggering the rule in this manner would have any effect on weather the rule stops to accept the input from the user. But as I said earlier even when I use Save and Close to trigger the rule it still can at times "skip" the mouse click.

I really appreciate your help, but please do not burn your grey matter trying to figure this out. It's not a show stopper it's just an annoyance.

 

 

 

Mike Eck
Master Drafter/ CAD Programmer
Using Inventor 2018
0 Likes
Message 10 of 28

MjDeck
Autodesk
Autodesk

When it's running during a parameter change event, there are some limits on what a rule can do. For a rule that requires UI, it might be better not to run it during a parameter change.
Here's an alternative to using a True/False parameter to trigger the rule to run:
The attached drawing has a form with a rule button. The button will be disabled if you set the CaptureRuleEnabled parameter to False. That's an optional property (Enabling Parameter Name) that you can set on any rule button in a form.

Your current logic could drive that parameter. (Right now, it's just set to the value of another parameter by a simple rule.)

I'm wondering which version of Inventor you're using. Your tagline says 2013, but the drawing you posted is from 2018.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 11 of 28

JBerns
Advisor
Advisor

@chandra.shekar.g / @MjDeck,

 

Thanks for posting such helpful code.

 

I have a general note (text) in a drawing that needs to be replaced.

I was going to ask the user to select the existing note to be deleted and then pick a point for the new note.

Having to press ESC in the middle of a command to stop picking points is not intuitive for previous AutoCAD users.

 

I have been unsuccessful in modifying the code to get a single mouse click. Is it possible for this code to adapted to get a single point pick?

 

Thanks for your time. I look forward to your replies.

 

 

Regards,

Jerry

 

 

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 12 of 28

MjDeck
Autodesk
Autodesk

Hi Jerry,

Yes, I think it's possible. You have to either stop the interaction or disable the mouse click handler in the code that handles the mouse click.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 13 of 28

xenocatalyst
Advocate
Advocate
I have a quick question,

Why is this short piece of code divided into a Sub Main and a second Sub?

Can it be coded without being split into subs?
0 Likes
Message 14 of 28

MjDeck
Autodesk
Autodesk

I'm guessing that you're talking about the code in Message 2 above.

It cannot be coded without being split into subs. Sub Main hooks up the event handler sub oMouse_OnMouseDown to the mouse click event. The system will then call oMouse_OnMouseDown whenever you click one of the mouse buttons.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 15 of 28

xenocatalyst
Advocate
Advocate

Hi MjDeck

 

Yes the code in message 2.

 

I want to use the coords of the mouse click as the value of a createoint2d.

But as I'm reading, a sub doesn't pass data back to the sub main.

Apparently you need to use a function in place of a sub, But I cant get that working either.

And it needs to be done in Ilogic

0 Likes
Message 16 of 28

MjDeck
Autodesk
Autodesk

Set up things you need in Sub Main. Store those in data that lives outside the Subs, and is available to both of them.

Then in oMouse_OnMouseDown , you can use those things to create or modify something.

Here's an example that just stores the document.

Sub Main()
_doc = ThisDoc.Document

Dim oInteraction As InteractionEvents
oInteraction = ThisApplication.CommandManager.CreateInteractionEvents

Dim oMouse As MouseEvents
oMouse = oInteraction.MouseEvents
AddHandler oMouse.OnMouseClick, AddressOf oMouse_OnMouseDown

oInteraction.Start
End Sub

Private _doc As Document

Sub oMouse_OnMouseDown(Button As MouseButtonEnum, ShiftKeys As ShiftStateEnum, ModelPosition As Point, ViewPosition As Point2d, View As Inventor.View)

	Logger.Info("mouse model position in cm = {0}, {1}, {2}", ModelPosition.X, ModelPosition.Y, ModelPosition.Z)

	' use _doc ...
End Sub

 


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 17 of 28

xenocatalyst
Advocate
Advocate

Thanks, 

 

I have never used the logger before.

 

I think I have it working I have the sub write to a custom iproperty.

 

can you tell me what this line does?

Private _doc As Document

 

An one other thing, I need to try the code a few time before it produces a result. Any ideas why?

 

my code below:

Sub Main()
_doc = ThisDoc.Document

Dim oInteraction As InteractionEvents
oInteraction = ThisApplication.CommandManager.CreateInteractionEvents

Dim oMouse As MouseEvents
oMouse = oInteraction.MouseEvents
AddHandler oMouse.OnMouseClick, AddressOf oMouse_OnMouseDown
oInteraction.Start
End Sub

Private _doc As Document

Sub oMouse_OnMouseDown(Button As MouseButtonEnum, ShiftKeys As ShiftStateEnum, ModelPosition As Point, ViewPosition As Point2d, View As Inventor.View)

	Logger.Info("mouse model position in cm = {0}, {1}", ModelPosition.X, ModelPosition.Y)
	iProperties.Value("Custom", "TEMPCOORDS") = ModelPosition.X.tostring + ", " + ModelPosition.Y.ToString
	' use _doc ...
End Sub
0 Likes
Message 18 of 28

MjDeck
Autodesk
Autodesk

The line 

 

Private _doc As Document

 

declares a variable that is available to both Sub Main and the event handler. It's just an example. Maybe you don't need it. Maybe you can do everything you want with existing objects like iProperties.

There does seem to be a problem with this rule. I'll try to look into it in more detail. But note that it's better to do a UI interaction like this from an add-in, rather than an iLogic rule.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 19 of 28

MjDeck
Autodesk
Autodesk

Here's a more reliable version of the rule. It helps to declare the interaction objects outside of Sub Main. Sub Main runs once and then it's done. But the interaction objects have to stick around after Sub Main has finished.

Sub Main()
_doc = ThisDoc.Document

oInteraction = ThisApplication.CommandManager.CreateInteractionEvents
oMouse = oInteraction.MouseEvents
AddHandler oMouse.OnMouseClick, AddressOf oMouse_OnMouseDown

oInteraction.Start
End Sub

Private _doc As Document
Private oInteraction As InteractionEvents
Private oMouse As MouseEvents

Sub oMouse_OnMouseDown(Button As MouseButtonEnum, ShiftKeys As ShiftStateEnum, ModelPosition As Point, ViewPosition As Point2d, View As Inventor.View)

	Logger.Info("mouse model position in cm = {0}, {1}, {2}", ModelPosition.X, ModelPosition.Y, ModelPosition.Z)
	iProperties.Value("Custom", "TEMPCOORDS") = ModelPosition.X.ToString + ", " + ModelPosition.Y.ToString
End Sub

Mike Deck
Software Developer
Autodesk, Inc.

Message 20 of 28

xenocatalyst
Advocate
Advocate

I appreciate the help you are giving me, but i still cant wrap my head around how to make subs work.

The changes you made previously worked well and made that part of the rule very reliable.

 

My goal for my rule is to place a baseview of a pre determined part on a drawing sheet, at a location specified by the user.

Some of my code works and some doesn't.

 

It will get and report the location properly but will not place the baseview.

The sub also runs in an infinite loop until escape is pressed.

 

would you mind having a look at this code?

Sub Main()
	
	_doc = ThisDoc.Document
Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument 
Dim oSheet As Sheet = oDrawDoc.ActiveSheet
Dim DrawingViewScale As Double = 0.1
Dim myWorkSpace = ThisApplication.FileLocations.Workspace
Dim placepart As String = myWorkSpace +"\Test1-P001.ipt"
MessageBox.Show(placepart,"test1")'works

oInteraction = ThisApplication.CommandManager.CreateInteractionEvents
oMouse = oInteraction.MouseEvents
AddHandler oMouse.OnMouseClick, AddressOf oMouse_OnMouseDown
oInteraction.Start
End Sub 

Private _doc As DrawingDocument
Private oInteraction As InteractionEvents
Private oMouse As MouseEvents
Private oSheet As Sheet
Private placepart As String 'THIS DOES NOT WORK, I LACK THE UNDERSTANDING

Sub oMouse_OnMouseDown(Button As MouseButtonEnum, ShiftKeys As ShiftStateEnum, ModelPosition As Point, ViewPosition As Point2d, View As Inventor.View)
Dim DrawingViewScale As Double = 0.1
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oPoint1 As Point2d = oTG.CreatePoint2d(ModelPosition.X, ModelPosition.Y) 'works
MessageBox.Show("X coord: " +oPoint1.X.ToString +vbCrLf+"Y coord: " +oPoint1.Y.ToString , "Title") 'works
MessageBox.Show(placepart, "test2") 'THIS DOES NOT WORK

'Create the Base View 'THIS DOES NOT WORK
oBaseViw = oSheet.DrawingViews.AddBaseView(placepart, oPoint1, DrawingViewScale, ViewOrientationTypeEnum.kFrontViewOrientation, DrawingViewStyleEnum.kHiddenLineDrawingViewStyle)
End Sub
0 Likes