Cannot select objects during oInteractEvents

Cannot select objects during oInteractEvents

meck
Collaborator Collaborator
436 Views
2 Replies
Message 1 of 3

Cannot select objects during oInteractEvents

meck
Collaborator
Collaborator

Hi All,

I know I am doing something simple wrong here, but after 2 days of messing with it I am throwing my hands up.

I am using VB.net 2017. I need to allow a user to select a part or a sub-assembly in an assembly. I want to return the occurrence object from the selection. 

When the code runs it never reaches the oInteractEvents_OnTerminate or oSelectEvents_OnSelect subs. It just remains in the Do While bStillSelecting loop.

Any help would be greatly appreciated!

Here is my entire class...

 

Imports System.Runtime.InteropServices
Imports System.Activator
Imports System.Type

Public Class clsSelect

' Declare the event objects
Private WithEvents oInteractEvents As Inventor.InteractionEvents
Private WithEvents oSelectEvents As Inventor.SelectEvents

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

Public Function Pick(filter As Inventor.SelectionFilterEnum) As Object

Call StartInventor()

' Initialize flag.
bStillSelecting = True

' Create an InteractionEvents object.
oInteractEvents = invApp.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(Inventor.SelectionFilterEnum.kAllEntitiesFilter)

' Start the InteractionEvents object.
oInteractEvents.Start()

' Loop until a selection is made.
Do While bStillSelecting
invApp.UserInterfaceManager.DoEvents()
Loop

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

' Stop the InteractionEvents object.
oInteractEvents.Stop()
invApp.CommandManager.StopActiveCommand()

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

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

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

Public Function StartInventor() As Boolean
'Everything in this sub is fluff stolen from
'elsewhere with variable names changed to suit my purposes
Try
invApp = Marshal.GetActiveObject("Inventor.Application")
Return True
Catch ex As Exception
Try
Dim invAppType As Type = GetTypeFromProgID("Inventor.Application")
invApp = CreateInstance(invAppType)
invApp.Visible = True
'Note: if you shut down the Inventor session that was started
'this(way) there Is still an Inventor.exe running. We will use
'this Boolean to test whether or not the Inventor App will
'need to be shut down.
StartInventor = True
Catch ex2 As Exception
MsgBox(ex2.ToString(), MsgBoxStyle.Critical, "Unable to get or start Inventor")
Return False
End Try
End Try
End Function
End Class

Mike Eck
Master Drafter/ CAD Programmer
Using Inventor 2018
0 Likes
Accepted solutions (1)
437 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

After just a quick glance at your code, I have a suggestion (educated guess) as to what the problem may be.  There are two ways to declare event handlers in vb.net.  (WithEvents & Handles or AddHandler & AddressOf)  In your case, you're using the WithEvents route, so I believe you just need to add the 'Handles Object.Event'  statement at the end of your 'handler sub' definition line.  Try that and see if that fixes the situation.

 

PS:  Here is a link to one of my contribution posts about creating event handlers, in case you're interested.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

meck
Collaborator
Collaborator

Awesome thanks!

Mike Eck
Master Drafter/ CAD Programmer
Using Inventor 2018
0 Likes