Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
Enumerations, annotations and map keys!
bartyborris
OS Professional Posts: 11 PRO
Took me a bit to figure out how to link a user choice to a map key. Enumerations can be tricky at first (at least for me, even with the documentation). It's important to use "as string" to drop the prior type tag defined by the enumeration.
annotation { "Feature Type Name" : "My Feature" }<br>export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)<br> precondition<br> {<br> annotation { "Name" : "Tooth Type" }<br> definition.ToothType is ToothType;<br> }<br> {<br> var choice = definition.ToothType;<br> println("definition.ToothType is: "~ definition.ToothType);<br> println("Is definition.ToothType a string? " ~ definition.ToothType is string );<br> println('Is definition.ToothType == "T5"? ' ~ definition.ToothType == "T5");<br> println('Is (definition.ToothType as string) equal to "T5": ' ~ (definition.ToothType as string) == "T5");<br> <br> choice = definition.ToothType as string;<br> <br> var toothPitch = {"T5" : 5 * millimeter, "T10": 10 * millimeter};<br> <br> println("The tooth pitch is: " ~ toothPitch[choice] / millimeter ~ "mm");<br> <br> });<br> <br>export enum ToothType<br>{<br> annotation {"Name" : "T5"}<br> T5,<br> annotation {"Name" : "T10"}<br> T10<br>}Results:
definition.ToothType is: T5
Is definition.ToothType a string? true
Is definition.ToothType == "T5"? false
Is (definition.ToothType as string) equal to "T5": true
The tooth pitch is: 5mm
0
Comments
To get that value without any conversions, you can do: