Is there a way to add each Method Name of an Enum to a List(Of String) or ArrayList or Collection?
Lets use the UnitsTypeEnum here.
It has a long list of 'Methods'. For example, "kInchLengthUnits" is the Name of one of its Methods.
I know there is a Help page for this list here, but I would like to add the name of each one to a list, so it could be used in an InputListBox, or UserForm ComboBox, or similar selection interaction.
Since it's not really a Collection, I can't just use a regular "For Each" or "For i = 1 To UnitsTypeEnum.Count" type of loop technique.
Is there a way to do this without having to manually type out each 'k' type individually, on separate lines to add them to the list?
Wesley Crihfield
(Not an Autodesk Employee)
Solved! Go to Solution.
Solved by yan.gauthier. Go to Solution.
If you are using .NET then simple use Enum.GetNames like this :
List<string> bomStructureDataSource = Enum.GetNames(typeof(BOMStructureEnum)).ToList();
string BomStructure = "kDefaultBOMStructure"
if (Enum.TryParse(BomStructure, true, out myBomStructure))
{
assyDoc.ComponentDefinition.BOMStructure = myBomStructure;
}
Within my iLogic Rule Editor dialog, I have all three options turned on for references (iLogic, Inventor, & Standard.Net), and most of that code is not recognized. Therefore, I assume it can only be used within Visual Studio? Or. perhaps I'm not understanding how to use it. Can you give a full working example, where I don't have to change things within it to make it work in my environment. I'm using Inventor Proffesional 2021 (and have the latest updated version of Visual Studio Code installed, but not very familiar with using it yet).
Wesley Crihfield
(Not an Autodesk Employee)
This is written in C#. you will need to change the syntax to VB.NET. It should look like something around that :
Dim bomStructureDataSource As String() = [Enum].GetNames(GetType(BOMStructureEnum))
Dim bomStructure As String = "kDefaultBOMStructure"
Dim myBomStructure As BOMStructureEnum
If [Enum].TryParse(bomStructure, myBomStructure) Then
ThisDoc.Document.ComponentDefinition.BomStructure = myBomStructure
End If
Thanks for your help so far. I really appreciate it.
I'm still not able to get that to work either. I've tried lot of modifications to this code, trying to get it to run the full code without errors, but it will only run just past the "If" line, but it throws an error at that point. Besides, I'm trying to get a list of names, not a comparison of one value within the list to another value.
I think the problem may be that BOMStructureEnum isn't a true System.Enum.
When I try this:
Dim bomStructureDataSource As String() = [Enum].GetNames(GetType(BOMStructureEnum))
MsgBox("Item 1 in the list = " & bomStructureDataSource(1))
I get an Error message that says "Type 'BOMStructureEnum' is not defined."
But when I do this:
Enum AnswersEnum
Maybe = 0
Yes = 1
No = 2
End Enum
Sub Main
Dim oAnswersList() As String = System.Enum.GetNames(GetType(AnswersEnum))
MsgBox("This should say 'Yes' >>> " & oAnswersList(1))
a = InputListBox("Prompt",oAnswersList)
End Sub
It works exactly as expected.
The message shows "This should say 'Yes' >>> Yes".
And the InputListBox shows all three options "Maybe",Yes", & "No".
Wesley Crihfield
(Not an Autodesk Employee)
Perhaps someone knows how Inventor's built-in Enums are specified, or where within the installation files they are specified, or something similar, that could shed more light on this situation? It seems like we're so close. Either Inventor's built-in enums arent recognized as System.Enum or it just can't figure out what their System.Type is. Do we need to (or can we) specify/assign what System.Type it is before the GetNames function? If the System.Type thing is going to be a deal breaker, is there another way, other than copying all names & values to an Excel spreadsheet and having to reference that?
I've looked at Microsofts documentation about these, but all their examples are in C#, which I am not familiar with, and don't know how to translate it to the VB.NET that is understood in either the iLogic or VBA Editor environments.
Wesley Crihfield
(Not an Autodesk Employee)
Have you tried using the same syntax. As I said, my example is from my own C# addin which use the same .NET framework as vb.NET. I can tell you BOMStructureEnum is an Enum.
Have you tried System.Enum.GetNames(GetType(Inventor.BOMStructureEnum))
As the syntax instead. I tried to translate my C#, but i am not 100% sure if i provided the right syntax
When I try to run the following from an external iLogic rule:
Dim oList() As String = System.Enum.GetNames(GetType(BOMStructureEnum))
MsgBox("BOMStructureEnum - 1st Name in its list is = " & oList(1))
I get the following message:
I get the same outcome with:
Dim oList() As String = [Enum].GetNames(GetType(BOMStructureEnum))
MsgBox("BOMStructureEnum - 1st Name in its list is = " & oList(1))
And with:
Dim oList As String() = [Enum].GetNames(GetType(BOMStructureEnum))
MsgBox("BOMStructureEnum - 1st Name in its list is = " & oList(1))
Same exact result when doing the GetType on a previous line, then puthing the resulting variable in its place like this:
Dim oType As System.Type = GetType(BOMStructureEnum)
Dim oList As String() = [Enum].GetNames(oType)
MsgBox("BOMStructureEnum - 1st Name in its list is = " & oList(1))
It just doesn't seem to recognize the System.Type of BOMStructureEnum.
I tried this in the VBA Editor too, with no luck. Maybe I don't have the right References turned on, but I don't know of a specific reference to turn on for it to recognize System.Enum.
Within the iLogic Rule Editor, it recognizes that BOMStructureEnum is an Enum.
It also regognizes System.Enum & System.Type.
GetType is red within the editor, so recognized as a 'reserved keyword', but I don't think it is seen as a Function.
Maybe I've got to include something in the header of the rule, like imports or references?
Wesley Crihfield
(Not an Autodesk Employee)
Alright, I made it work!
First, your Ilogic rule as to be in an assembly file. second, paste this :
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
Dim oList As String() = System.Enum.GetNames(GetType(Inventor.BOMStructureEnum))
MsgBox("BOMStructureEnum - 1st Name in its list is = " & oList(1))
Yep. That did it. Thanks again for your patience and work on my behalf.
I have now tried it that way on another different Enum, and it works too.
Strange how it seems that nothing in the List or MsgBox lines seems to have any connection to the ComponentDefinition, but simply specifying one above those lines in the code, somehow makes this work.
Yet another odd situation that isn't very well documented
Wesley Crihfield
(Not an Autodesk Employee)
No problem,
If you can make the jump to Addin, documentation is a little better and Intellisense is functional!
@WCrihfield wrote:Strange how it seems that nothing in the List or MsgBox lines seems to have any connection to the ComponentDefinition, but simply specifying one above those lines in the code, somehow makes this work.
Yet another odd situation that isn't very well documented
FYI, this is due to a quirk of iLogic, described elsewhere by @MjDeck as below:
"iLogic doesn't add a reference to the Inventor API unless it determines that you're using it in the rule. To let iLogic know that you want to use it, add a statement that includes ThisApplication (or ThisServer) or ThisDoc.Document."
(EDIT: Which means, you don't need to use the exact starter code as above. It doesn't need to access a ComponentDefinition or be run from an assembly file. It just needs to use "ThisDoc" or "ThisApplication" in order to tell iLogic to reference the Inventor API. So you could instead do something like this:
app = ThisApplication Dim oList As String() = System.Enum.GetNames(GetType(Inventor.BOMStructureEnum)) MsgBox("BOMStructureEnum - 1st Name in its list is = " & oList(1))
and it should run fine).
Indeed, this is not well documented (if at all). And in my opinion, it's kind of a silly thing anyway, because probably the vast majority of iLogic rules are intended to interact with the API in some way, so it seems it would be better to reference it by default than not, to avoid confusing situations like this one.
Mike, is this something that could be improved?
@DRoam , it's true that the vast majority of rules interact with the Inventor API in some way, but they don't necessarily do it directly. Instead, they might go through iLogic functions.
And if they do use the API directly, they almost always include some access to ThisApplication or ThisDoc.Document (or other iLogic object) to start from.
However, you're right. This could be improved. It's an optimization that probably isn't worthwhile, especially since it can cause problems. We could reference the Inventor API and import the Inventor types in all rules. We already do it for intellisense. I'll take a look at changing this.
Thanks for the additional explanation, Mike.
@MjDeck wrote:And if they do use the API directly, they almost always include some access to ThisApplication or ThisDoc.Document (or other iLogic object) to start from.
I think you're probably right. I think the optimization mainly becomes an issue when starting out on a rule, doing some "prep work", and then saving, before you've gotten to the point of using ThisApplication or ThisDoc. You get the "Type X Not Defined" error message and have no idea why. Or it can happen in situations like in this thread, where you're just interacting with infrastructure in the API without needing anything in the actual Inventor application or a document.
@MjDeck wrote:It's an optimization that probably isn't worthwhile, especially since it can cause problems.
This is exactly what I was thinking. Perhaps there could be an option to turn it off (the reference to the Inventor API), for straight-VB files that don't do anything Inventor-related? Either way, thanks for looking into it.
Thanks @DRoam & @MjDeck . That makes a lot more sence now. I knew iLogic understood VB.NET, and I knew that Enum was undstood by both, and I know I had seen the GetNames method before, but couldn't remember how to use it. I had revued Enum on the Micrisoft & Stackoverflow websites. (The Stackoverflow site is almost always more informational.) But I wasn't able to get it to work within the Inventor iLogic environment, unless I created the Enum within the same rule, as I did in the simple example above. It was very puzzling becavior. Knowing that I've got to be very close to the answer, with some variations working, and others do not, is the kind of stuff that drives you crazy, untill you get it figured out. 😁
Wesley Crihfield
(Not an Autodesk Employee)
Can't find what you're looking for? Ask the community or share your knowledge.