Arbitrary drop-down menu?

adamohern
adamohern Member, OS Professional Posts: 216 PRO
I want to include a list of standard sizes for a feature. How do I do that?

I see that the Extrude feature uses one for boundary type, but this appears to be a special case using globals. How do I assign arbitrary values to the dropdown menu items?
Tagged:

Comments

  • ilya_baran
    ilya_baran Onshape Employees, Developers, HDM Posts: 1,277 image
    You can declare your own enum like this:
    export enum Size
    {
        annotation { "Name" : "Small" }
        SMALL,
        annotation { "Name" : "Medium" }
        MEDIUM,
        annotation { "Name" : "Large" }
        LARGE
    }
    
    annotation { "Feature Type Name" : "My Feature" }
    export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Size" }
            definition.size is Size;
        }
        {
        }, { /* default parameters */ });
    


    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • adamohern
    adamohern Member, OS Professional Posts: 216 PRO
    Thank, Ilya. I guess what's confusing me is the use of the globals, in your case SMALL, MEDIUM, and LARGE. Where do those get defined, and kind of data should they be?
  • adamohern
    adamohern Member, OS Professional Posts: 216 PRO
    Sorry, reading up on enum objects here, and I think I understand. It's a really strange construct as I understand it. Apparently the all-caps assignment is interpreted as a string, but is also accessible as if it were a map key. It's like a map where the keys and values are identical. I wonder why it works this way.
  • ilya_baran
    ilya_baran Onshape Employees, Developers, HDM Posts: 1,277 image
    SMALL, MEDIUM, and LARGE are not globals -- they are enumeration values defined right inside the enum declaration (see https://en.wikipedia.org/wiki/Enumerated_type)
    Basically the declaration
    export enum Size
    {
        REALLY_SMALL,
        MEDIUM,
        TRULY_HUGE
    }
    defines a new enum type Size and says that a value of type Size may be one of three possible values: Size.REALLY_SMALL, Size.MEDIUM and Size.TRULY_HUGE.  See also https://cad.onshape.com/FsDoc/values.html#enumerations for specific info on how enums work "underneath" in FeatureScript .
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • adamohern
    adamohern Member, OS Professional Posts: 216 PRO
    Many thanks, Ilya.