Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. 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.

Options

Enumerations, annotations and map keys!

bartyborrisbartyborris 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>&nbsp;&nbsp;&nbsp; precondition<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; annotation { "Name" : "Tooth Type" }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; definition.ToothType is ToothType;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var choice = definition.ToothType;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; println("definition.ToothType is: "~ definition.ToothType);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; println("Is definition.ToothType a string? " ~ definition.ToothType is string );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; println('Is definition.ToothType == "T5"? ' ~ definition.ToothType == "T5");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; println('Is (definition.ToothType as string) equal to "T5": ' ~ (definition.ToothType as string) == "T5");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; choice = definition.ToothType as string;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var toothPitch = {"T5" : 5 * millimeter, "T10": 10 * millimeter};<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; println("The tooth pitch is: " ~ toothPitch[choice] / millimeter ~ "mm");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; });<br>&nbsp;&nbsp;&nbsp; <br>export enum ToothType<br>{<br>&nbsp;&nbsp;&nbsp; annotation {"Name" : "T5"}<br>&nbsp;&nbsp;&nbsp; T5,<br>&nbsp;&nbsp;&nbsp; annotation {"Name" : "T10"}<br>&nbsp;&nbsp;&nbsp; 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



Comments

  • Options
    kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    edited August 2016
    FeatureScript map keys don't have to be strings. For your case, rather than converting everything to normal strings, it's preferable to use enum values (ToothTypes) everywhere.

    To get that value without any conversions, you can do:
    ));</code>var toothPitch is ValueWithUnits = {
        ToothType.T5  : 5 * millimeter,
        ToothType.T10 : 10 * millimeter
    }[definition.ToothType];
    
    <pre><code>println("The tooth pitch is: " ~ toString(<span>toothPitch</span>

  • Options
    bartyborrisbartyborris OS Professional Posts: 11 PRO
    Much cleaner.  Thank you Kevin.
  • Options
    Alex_PittAlex_Pitt Member Posts: 59 PRO
    I'm new to maps. Can the keys in a map be edges? And can the corresponding value in the map be a Solid Body? I know that Arrays can contain Solid Bodies.  I want to create a record (whether it's a map or an array)  that links an edge to a body. So Edge1 belongs to SolidBody1, Edge2 belongs to SolidBody2, Edge3, belongs to SolidBody3 etc. 
  • Options
    Konst_ShKonst_Sh Member Posts: 42 PRO
    @Alex_Pitt There is a query constructor that creates the subset of entities owned by specified body query: https://cad.onshape.com/FsDoc/library.html#qOwnedByBody-Query-Query
    So edgesOwnedByBody1 = qOwnedByBody(allEdges, queryOfBody1);

Sign In or Register to comment.