DoEvents and VB.net (selection)

DoEvents and VB.net (selection)

Anonymous
Not applicable
3,549 Views
10 Replies
Message 1 of 11

DoEvents and VB.net (selection)

Anonymous
Not applicable
IV 2008, VS 2005

I am converting over my VBA to add-ins. Still quite new to VB.net and having problem with pausing for user selection. The following is based on an example from help. The DoEvents does not work like this in net and I can not seem to get anything else to work for the pause. Also seems that DoEvents is not suggested in net anyway.

So what is the proper way to pause for input? An example would be best... Thanks

Public Class TestPick
' Declare the event objects
Private WithEvents oInteraction As InteractionEvents
Private WithEvents oSelect As SelectEvents

' Declare a flag that's used to determine when selection stops.
Private bStillSelecting As Boolean

Public Function Pick(ByVal filter As SelectionFilterEnum) As Object
' Initialize flag.
bStillSelecting = True

' Create an InteractionEvents object.
oInteraction = Button.InventorApplication.CommandManager.CreateInteractionEvents

' Define that we want select events rather than mouse events.
oInteraction.SelectionActive = True

' Set a reference to the select events.
oSelect = oInteraction.SelectEvents

' Set the filter using the value passed in.
oSelect.AddSelectionFilter(filter)

' The InteractionEvents object.
oInteraction.Start()

' Loop until a selection is made.
Do While bStillSelecting
DoEvents() '---------------------------problem
Loop

' Get the selected item. If more than one thing was selected,
' just get the first item and ignore the rest.
Dim oSelectedEnts As ObjectsEnumerator
oSelectedEnts = oSelect.SelectedEntities
If oSelectedEnts.Count > 0 Then
Pick = oSelectedEnts.Item(1)
Else
Pick = Nothing
End If

' Stop the InteractionEvents object.
oInteraction.Stop()

' Clean up.
oSelect = Nothing
oInteraction = Nothing
End Function

End Class
0 Likes
Accepted solutions (1)
3,550 Views
10 Replies
Replies (10)
Message 2 of 11

Anonymous
Not applicable
Yes, I would also consider if you absolutely need to use DoEvents because in
VB.NET you can use "AddHandler" to connect an event sink method and
"RemoveHandler" to disconnect, it allows you to start/stop connecting to a
particular event method. So, there might not be a need to use DoEvents to
pump messages in this case.

But, if you need to use DoEvents, you could still use it:

' Loop until a selection is made.
Do While bStillSelecting
System.Windows.Forms.Application.DoEvents()
'---------------------------problem
Loop

You probably need to add a reference to "System.Windows.Forms"

-Venkatesh Thiyagarajan.

wrote in message news:5641501@discussion.autodesk.com...
IV 2008, VS 2005

I am converting over my VBA to add-ins. Still quite new to VB.net and having
problem with pausing for user selection. The following is based on an
example from help. The DoEvents does not work like this in net and I can not
seem to get anything else to work for the pause. Also seems that DoEvents is
not suggested in net anyway.

So what is the proper way to pause for input? An example would be best...
Thanks

Public Class TestPick
' Declare the event objects
Private WithEvents oInteraction As InteractionEvents
Private WithEvents oSelect As SelectEvents

' Declare a flag that's used to determine when selection stops.
Private bStillSelecting As Boolean

Public Function Pick(ByVal filter As SelectionFilterEnum) As Object
' Initialize flag.
bStillSelecting = True

' Create an InteractionEvents object.
oInteraction =
Button.InventorApplication.CommandManager.CreateInteractionEvents

' Define that we want select events rather than mouse events.
oInteraction.SelectionActive = True

' Set a reference to the select events.
oSelect = oInteraction.SelectEvents

' Set the filter using the value passed in.
oSelect.AddSelectionFilter(filter)

' The InteractionEvents object.
oInteraction.Start()

' Loop until a selection is made.
Do While bStillSelecting
DoEvents() '---------------------------problem
Loop

' Get the selected item. If more than one thing was selected,
' just get the first item and ignore the rest.
Dim oSelectedEnts As ObjectsEnumerator
oSelectedEnts = oSelect.SelectedEntities
If oSelectedEnts.Count > 0 Then
Pick = oSelectedEnts.Item(1)
Else
Pick = Nothing
End If

' Stop the InteractionEvents object.
oInteraction.Stop()

' Clean up.
oSelect = Nothing
oInteraction = Nothing
End Function

End Class
0 Likes
Message 3 of 11

Anonymous
Not applicable
That Worked.
Will look into AddHandler also...

Thanks!
0 Likes
Message 4 of 11

Anonymous
Not applicable
Hi, this method is not working in VB2005 with Inventor 2008 or 2009.

Its works greae in VBA for either version but not in vb2005 or 2008.

Here is the code. I have tried all version of the DoEvents. I have looked at the Event Handler method but thats long and involved. Any help would be great.

Imports Inventor
Imports System.Windows.Forms

Public Class clsSelect
Private Declare Function GetInputState Lib "user32" () As Long
Private WithEvents oInteractEvents As Inventor.InteractionEvents
Private WithEvents oSelectEvents As Inventor.SelectEvents
Public bStillSelecting As Boolean

Public Function Pick(ByVal filter As SelectionFilterEnum) As Object

Dim MyInventor As Inventor.Application
Dim oSelectedEnts As ObjectsEnumerator

MyInventor = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")

' Initialize flag.
bStillSelecting = True

' Create an InteractionEvents object.
oInteractEvents = MyInventor.CommandManager.CreateInteractionEvents

' Ensure interaction is enabled.
oInteractEvents.InteractionDisabled = False

' Set a reference to the select events.
oSelectEvents = oInteractEvents.SelectEvents

' Set the filter using the value passed in.
oSelectEvents.AddSelectionFilter(SelectionFilterEnum.kAssemblyOccurrenceFilter)

' Start the InteractionEvents object.
oInteractEvents.Start()

Do While bStillSelecting = True
System.Windows.Forms.Application.DoEvents()
Loop

MsgBox("Slection End")

' Get the selected item. If more than one thing was selected,
' just get the first item and ignore the rest.

oSelectedEnts = oSelectEvents.SelectedEntities
If oSelectedEnts.Count > 0 Then
Pick = oSelectedEnts.Item(1)
Else
Pick = Nothing
End If

' Stop the InteractionEvents object.
oInteractEvents.Stop()

' Clean up.
oSelectEvents = Nothing
oInteractEvents = Nothing
End Function

Sub oInteractEvents_OnTerminate()
' Set the flag to indicate we're done.
bStillSelecting = False
End Sub

Sub oSelectEvents_OnSelect(ByVal JustSelectedEntities As ObjectsEnumerator, ByVal SelectionDevice As SelectionDeviceEnum, ByVal ModelPosition As Point, ByVal ViewPosition As Point2d)
' Set the flag to indicate we're done.
bStillSelecting = False
End Sub



End Class
Message 5 of 11

ktelangCGFU6
Participant
Participant

Please can you provide a example of add handler and remove handler I am running into a same situation where

DoEvent is giving me an issue

Application.DoEvents is not working well for me.

I my case I am trying to get 2d points from user pick.

 

Public Function GetDrawingPoint(Prompt As String, button As MouseButtonEnum) As Point2d

m_position = Nothing
m_button = button

' Start selection
m_interaction = g_inventorApplication.CommandManager.CreateInteractionEvents
m_mouse = m_interaction.MouseEvents

m_interaction.StatusBarText = Prompt
m_interaction.Start()
m_continue = True

Do While m_continue
g_inventorApplication.DoEvents()
Loop


m_interaction.Stop()
GetDrawingPoint = m_position
Return GetDrawingPoint
'Return GetDrawingPoint
Debug.Print("GetDrawingPoint: " & GetDrawingPoint.X & "," & GetDrawingPoint.Y)


End Function


Private Sub m_mouse_OnMouseClick(button As MouseButtonEnum, ShiftKeys As ShiftStateEnum, ModelPosition As Point, ViewPosition As Point2d, View As View) Handles m_mouse.OnMouseClick
If button = m_button Then
m_position = g_inventorApplication.TransientGeometry.CreatePoint2d(ModelPosition.X, ModelPosition.Y)
Debug.Print(m_position.X & "," & m_position.Y)
End If

m_continue = False
' MsgBox("Mouse clicked: " & m_position.X & "," & m_position.Y)

End Sub

 

 

0 Likes
Message 6 of 11

JelteDeJong
Mentor
Mentor
Accepted solution

you might want to have a look at:

g_inventorApplication.UserInterfaceManager.DoEvents()

 

But if you are simply trying to let the user select  2 points you could do it with this 1 line of code:

ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchPointFilter, "Select a sketch point")

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 7 of 11

ktelangCGFU6
Participant
Participant

Thanks Application.UserInterfaceManager.DoEvents() worked.

 

0 Likes
Message 8 of 11

amarshallQMMYP
Contributor
Contributor

Hello, 

I've noticed that g_inventorApplication.UserInterfaceManager.DoEvents() results in the inventor ribbon being disabled. Would you know of any other alternatives? I'm trying to wait before checking for something while allowing inventor to function as normal as the program waits. 

0 Likes
Message 9 of 11

kuehnlein9FADZ
Participant
Participant

I am using

 

Dim oObject As Face
oObject = g_inventorApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "PlanFLÄCHE wählen")

 

to make the user pick a face with vb.net.

When I run the add-in in inventor the text "PlanFLÄCHE wählen" is prompted in the left lower corner of Inventor, but I cannot pick a face. Inventor is kind of frozen and does not react to any user action.

0 Likes
Message 10 of 11

WCrihfield
Mentor
Mentor

Hi @kuehnlein9FADZ.  So, not only would it not let you select anything, but it also would not let you tap the Escape key on the keyboard to get out of that command?  That is definitely odd behavior.  Did you use CommandManager.CreateInteractionEvents, InteractionEvents.SelectEvents, or InteractionEvents.Start, before you called that 'Pick' line in your code.  If so, that may be interfering with that function.  You do not need to use any of those to use the regular Pick function, because it works on its own.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 11 of 11

kuehnlein9FADZ
Participant
Participant

Yesterday I found out what the issue was:

I am using a form and on clicking OK the "real" code starts. I used form.ShowDialog to activate the form.

Now I changed the form activation to form.Show and it works.

@WCrihfield: Thanks for your thoughts. I was not using SelectEvents etc. just the Pick command. (which works fine after solving the form issue)