Inventor.Selection Not Defined

Inventor.Selection Not Defined

amarinXG8V6
Advocate Advocate
481 Views
6 Replies
Message 1 of 7

Inventor.Selection Not Defined

amarinXG8V6
Advocate
Advocate

Hi everyone,

 

I'm trying to create some simple iLogic code using an object-oriented interface. Specifically, I'm trying to use the Inventor.Selection class in an Inventor parts (IPT) file. I'm trying to write a rule that when launched will ask the user to select a sketch, at which point the user would select a sketch. There are a few things that I need to do to the sketch including identifying each line as an individual vector and splitting it up into 24" segments with the remainder left at the end of each line. To first step is using the Inventor.Selection code and for some reason I keep getting the error listed below. I've tried putting "Imports Inventor" in the header of the iLogic rule but I still get the same error.

 

I noticed that if I uncheck the "Straight VB Code" option, I get this error.

 

amarinXG8V6_3-1675059407588.png

 

If I check the "Straight VB Code" then I get the following errors.

 

amarinXG8V6_5-1675059764252.png

 

 

Any help would be much appreciated. Sample code listed below:

Dim sel As Inventor.Selection sel = invApp.ActiveDocument.SelectSet sel.Select(Nothing, "Select a Sketch") If sel.Count > 0 Then Dim sketch As Inventor.Sketch = sel(1) MsgBox("You selected the sketch: " & sketch.Name) End If

amarinXG8V6_4-1675059475827.png

 

 

 

0 Likes
482 Views
6 Replies
Replies (6)
Message 2 of 7

A.Acheson
Mentor
Mentor

No need for any Imports and keep vb straight code off. Here is a vba sample to reference. 

 

'Set a reference to the select set.
Dim sel As SelectSet = ThisApplication.ActiveDocument.SelectSet 

If sel.Count > 0 Then 
  Dim sketch As PlanarSketch = sel.Select.Item(1) 
  MsgBox("You selected the sketch: " & sketch.Name) 
End If

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 7

WCrihfield
Mentor
Mentor

Hi @amarinXG8V6.  There is normally no such thing as an 'Inventor.Selection', but there is an Inventor.SelectSet, which is what you were trying to set as that variable's value, from the document.  That is often used to get objects that have been pre-selected (selected before the iLogic rule was started), but can also be used later to add things to the SelectSet for further processing by some 'commands' that you can execute on them.  If you want to be able to select an object while the rule is running, there is a built-in function called 'Pick', which was designed just for that purpose.  Below is an example of this in a similar situation.

Dim sel As Inventor.SelectSet = ThisDoc.Document.SelectSet
Dim oSketch As Sketch = Nothing
If sel.Count > 0 Then
	If TypeOf sel.Item(1) Is Sketch Then
		oSketch = sel.Item(1)
		MsgBox("You pre-selected a sketch named:  " & oSketch.Name, , "")
	End If
ElseIf sel.Count = 0 Then
	Dim oPicked = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchObjectFilter, "Select a Sketch.")
	If IsNothing(oPicked) OrElse TypeOf oPicked Is Sketch = False Then Exit Sub
	oSketch = oPicked
End If

That 'Straight VB Code' setting is only used in pretty advanced scenarios, where you might have fully developed a solution within Visual Studio, and want to transfer that solution's code down into an iLogic rule collection.  When that option is turned on, your code must fully comply will all standard vb.net coding rules, similarly to coding within Visual Studio.  With that option turned off, coding requirements are much more relaxed, and simpler.  The iLogic rule editor environment has already included several 'references' and 'Imports' for you in the background, as well as enclosing your code within a "Class' block, and a 'Sub' block for you in the background, so that you don't have to worry about that type of stuff for simple rules.  But we can also use our own Class, Sub, Function, Property, Enum type blocks of code within rules, if we choose to.  It is very flexible and forgiving, which can be pretty odd for someone with a history of using something like Visual Studio.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 7

amarinXG8V6
Advocate
Advocate

@A.Acheson & @WCrihfield ,

 

Thank you both for the quick reply and the sample code. I'm going to try it later today when I have a chance. I like the idea of using the "Pick" function since it allows it to be used after the rule is fired. That makes sense that the iLogic environment already has these "references" included in order to make it easier to code but I can also see how that can be confusing. I'll stay away from the "Straight VB" option for now.

 

Alex

0 Likes
Message 5 of 7

amarinXG8V6
Advocate
Advocate

@WCrihfield & @A.Acheson ,

 

The "Pick" sample code worked great. Now for the tough part (at least for me). How to I write code that iterates through each line segment in the selected sketch, splits each line into 4 ft increments (the increments could be considered a variable), and adds a work plane at each split? Here's an example in the screen shot below of what I would like for it to do. There will inevitable be a remainder at the end of each line segment and I would like to set a minimum for that remainder at 18 inches. As a note, there could be a remainder at the end of line segment. The example below is a simple sketch but it could be more complex. There could also be internal lines in the sketch (as in not just the perimeter). Is this something that can be done in iLogic?

I've tried to research this and I can't seem to find the right answer. I've even gone so far as to use ChatGBT but to be honest that's also not worked great. In my experience asking folks in the forums (such as you guys) has always been the most helpful. 

 

Thanks again for your help!

 

amarinXG8V6_0-1675228050881.png

amarinXG8V6_1-1675228258358.png

 

 

0 Likes
Message 6 of 7

Michael.Navara
Advisor
Advisor
Message 7 of 7

WCrihfield
Mentor
Mentor

Hi @Michael.Navara.  When I saw your post, it jolted my memory about a challenge I had worked on around that time, that sounded almost exactly the same, so I did a little digging to find it.  I came across two links, and I think they both related to the same user's profile.  That was certainly a memorable one, that's why I remembered it after all this time. 😉

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/ilogic-use-line-from-sketch-for-rect... 

 

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/ilogic-point-pattern/m-p/10489856 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes