FeatureScript 101

Pump Up the Presets

In this video we build on last week's hex nut code to add a drop-down list of standard metric nut sizes. We'll begin with a simple example using simple reusable code snippets, and then use that knowledge in our live code.

Enum.

First, we need a drop-down menu. An enum (i.e. "enumerated data type") in FeatureScript serves exactly this purpose: we define a bunch of values, and those values appear as options in the dropdown.

Note that there are two parts to each entry: the annotation containing the pretty name that will be displayed in the UI, and an all-caps internal name that will be used in our code. (The annotation is optional.)

Note: There is no shame in copy/paste coding, especially as a beginner. For what it's worth, I use snippets exactly like the ones below in my own work, and I recommend you do the same.

Hooray! We now have a drop-down menu. Play around with this a bit. Try adding new items to the menu, and modifying existing ones. See if you can break it.

Make it Function

Much like Adam himself, our drop-down menu is pretty, but useless. We need to equate a menu selection with an actual length value for modeling purposes.

To do that, we'll use a type of subroutine called a function. A function is just a piece of reusable logic that we can call whenever we need it. In this case, our function accepts a value of type MyEnum as input, and returns a value based on that.

This one, I admit, get's a bit programmy. Rather than delve into the intricacies of this function, for now we'll just copy/paste it into our code, just below the MyEnum declaration at the top.

Note that the keys in the return map correspond exactly with the names of values in MyEnum. This is crucial.

Next we need a function that relates those keys to values. This one is also fairly simple, but a bit more complex than the original example.

First, I'm declaring a constant equating 'mm' to 'millimeter', essentially creating a shorthand for myself. I would hate to have to type out "millimeter" four times on every line of this snippet!

Second, the values we're returning are actually maps (wrapped in curly braces) returning a bunch of key:value pairs. If we choose an M2 bolt, for example, we need not only the hole size, but also the pitch, outer diameter, and thickness. We can store all of those in a single map, return it, and access those parts later on as needed.

Conditional Parameters

And remember, we only need to show our explicit dimensions if the drop-down is on "Custom" mode. Otherwise we'll just use the values defined in our standard. There are two parts to this: one in the precondition, and one in the body of the feature.

First, let's make it so that our custom parameters only show up in the UI if we're in Custom mode:

Full Code

I highly recommend spending some time hacking at this code. Make some changes. Re-order things. Rename things. Remove things. Add things. Break it. Fix it. Get comfortable with how this code operates, because from this point forward I'm going to assume that you know how to do it.