Reference to a non shared member requires an object reference

Reference to a non shared member requires an object reference

MTheDesigner
Advocate Advocate
2,052 Views
6 Replies
Message 1 of 7

Reference to a non shared member requires an object reference

MTheDesigner
Advocate
Advocate

I have the following code that I copied directly out of the inventor API help . It doesn't work it seems that there is a problem with accessing ThisApplication. I don't know what is wrong and quite frankly, having half the code in the help documentation not work without edits is making it very difficult to learn how to code for this program. Does anyone know how I can correct this error?

Public Sub Main()
    ' Create a new clsSelect object.
    Dim oSelect As New clsSelect

    ' Call the pick method of the clsSelect object and set
    ' the filter to pick any face.
    Dim oFace As Face
    oFace = oSelect.Pick(kPartFaceFilter)

    ' Check to make sure an object was selected.
    If Not oFace Is Nothing Then
        ' Display the area of the selected face.
        MsgBox("Face area: " & oFace.Evaluator.Area & " cm^2")
    End If
End Sub
	
Public Class clsSelect
' Declare the event objects
Private WithEvents oInteractEvents As InteractionEvents
Private WithEvents oSelectEvents As SelectEvents

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

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

	    ' Create an InteractionEvents object.
	    oInteractEvents = ThisApplication.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(filter)

	    ' Start the InteractionEvents object.
	    oInteractEvents.Start

	    ' Loop until a selection is made.
	    Do While bStillSelecting
	        ThisApplication.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 ObjectsEnumerator
	    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

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

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

 

0 Likes
Accepted solutions (1)
2,053 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @MTheDesigner.  Can you provide a link to the sample you are talking about.  The sample I am thinking about (Link1) was for allowing you to window select stuff, and that was a VBA macro code, not iLogic or vb.net.  Are you using this code within an iLogic rule, or are you using it within Visual Studio or something like that?  If you are using it in an iLogic rule, then do you have the iLogic rule option 'Straight VB Code' turned on, or off?   An iLogic rule assumes several settings in the background that you don't normally see, just to make things easier for beginners new to coding in iLogic & vb.net.  It assumes a Class named "ThisRule" and assumes the "Sub Main" and "End Sub", if you don't include that in your simple codes.  There are also other similar settings.  Anyways, I believe the terms like "ThisApplication" and similar are defined within that "ThisRule" already for you, by the iLogic add-in, to make life easier.  That's likely why the other Class does not know what that term means.  You can always pass the Inventor.Application object into your method, then re-declare/define a variable called "ThisApplication" within that other Class/Method somewhere, then set its value from the input variable you supplied to it, then use that "ThisApplication" variable from that point on.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 7

MTheDesigner
Advocate
Advocate
Hi @WCrihfield, I am using the code provided in the code provided here: https://help.autodesk.com/view/INVNTOR/2019/ENU/?guid=GUID-0BD48573-7193-4285-87B7-6727555D053E
I don't know what the difference between macro code and rule code is. I have only been using iLogic rules up to this point. I had no idea about the assumed class of the rule. Does that mean that I need to write the class somewhere other than a rule? Also it looks like I have Straight VB code turned off, which I believe its default state.
0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor

No problem.  It's pretty common early on go get VBA stuff and iLogic stuff mixed up.  They are both pretty similar looking, and both stem from the same Visual Basic source coding language.  Autodesk still provides most 'samples' of code in VBA, even though they are essentially discontinuing having the VBA editor installed by default, due to security related issues.  The iLogic is an Inventor add-in and it uses VB.NET coding language as its basis.  VBA code can usually be converted to VB.NET (which can then be used in an iLogic rule) without too much trouble, but not always.  I don't recall seeing an iLogic rule version of that VBA macro sample before, but it does not look that challenging to create one.  I will see what I can do.  Someone else may beat me to it though, if they have already done this.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 7

WCrihfield
Mentor
Mentor
Accepted solution

Try this version out:

Class ThisRule
	Sub Main 'TestSelection()
	    ' Create a new clsSelect object.
	    Dim oSelect As New clsSelect
	
	    ' Call the pick method of the clsSelect object and set
	    ' the filter to pick any face.
	    Dim oFace As Face = oSelect.Pick(ThisApplication, kPartFaceFilter)
	
	    ' Check to make sure an object was selected.
	    If Not oFace Is Nothing Then
	        ' Display the area of the selected face.
	        MsgBox("Face area: " & oFace.Evaluator.Area & " cm^2")
	    End If
	End Sub
End Class

Class clsSelect
	' Declare the event objects
	Private WithEvents oInteractEvents As InteractionEvents
	Private WithEvents oSelectEvents As SelectEvents
	' Declare a flag that's used to determine when selection stops.
	Private bStillSelecting As Boolean

	Public Function Pick(oApp As Inventor.Application, filter As SelectionFilterEnum) As Object
		' Initialize flag.
		bStillSelecting = True
		' Create an InteractionEvents object.
		oInteractEvents = oApp.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(filter)
		' Start the InteractionEvents object.
		oInteractEvents.Start
		' Loop until a selection is made.
		Do While bStillSelecting
			oApp.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 ObjectsEnumerator = 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

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

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

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 :bulb: or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 7

MTheDesigner
Advocate
Advocate
The code still doesn't work(it doesn't let me select anything) but you did solve the error I was getting that prevented it from running so thanks for that.
0 Likes
Message 7 of 7

WCrihfield
Mentor
Mentor

I noticed that too.  I'm not sure what's up with that yet.  I'll have to do more tweaking/trial/error stuff on that tomorrow.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes