You can be amazing at Onshape without knowing how to code your own custom features, but there is one use of Featurescript that I think every professional or serious user should know how to do. And that's to use Featurescript to create lists of variables to drive your designs.
I'm Evan, a partner at the product development firm Ovyl, let's get to it.
So this is a typical setup for something I would do on a project, I am controlling the design using a variable. So I've got this one set to length, and I can change the length of my box. Easy peasy. This will also let me configure it. So I can get multiple designs out of one document, which is pretty helpful. But there's a couple situations in which I wouldn't want to do it this way, you can see that I've got it packaged all nice here.
So reducing the length of the tree isn't really a big deal for me. But the main reason is, if I wanted to use these variables in a different part studio, I can't do that. So instead, I can create a custom feature that I insert into each part studio I want. And they all reference the same central database of variables, you can think of it sort of like global variables.
Another reason that I might prefer to do it all packaged up is a rebuild time. So if I'm working on a part studio that has a minute rebuild, and I want to edit a number of these variables, if I edit this one, and then width and then height, there's extra rebuild happening in the background. So even though my rebuild time may say that it only took 60 seconds, if I were to actually track that with a stopwatch it will have taken longer. So if I instead have the opportunity to edit them all at once in Featurescript, that's better, less rebuild time.
And then a final reason is the interoperability of your team, which is a core value we have here at Ovyl. And what I mean by that is it gives you the ability, even on very complex projects to explain what your variables do. So the bigger your project, the more variables you have, the more accidental cascading effects might happen if you change something and you don't know what you're doing. So you can only get so far with a descriptive name like length or nominal wall. That's probably enough for a project like this. But if it gets big, you'll want to be able to add entire paragraphs of text explaining what is connected to what. And you can do that with Featurescript. If I come down here to the plus icon and create a feature studio, you'll see that I have all of this space. And I can use a double slash and I can type whatever I want.
Woah, let's get rid of that.
Okay, so those are the three main reasons. And let's jump in and show you how I would set up these same variables using Featurescript instead of the variable feature.
With the cursor down here and below this text, I'm going to hit the new feature button. And that just creates this snippet of text. If you remember it, you could type it yourself, but I don't and you know why. So let me just give you a quick rundown of the anatomy of this snippet of text. This right here will be the name of your feature facing the user. So we'll type "Global Variables". This one is an internal name.
If you change it later, any places you've used, that feature will break. So we'll just call it "Global Variables" or whatever. If I'm making these for a specific project, or I have a project code or anything like that, I'll usually include that in the name here. Because you know, you might have multiples of these floating around in your toolbar, at least while you're working on that project.
All right, moving on down here. This is so explains itself, this blue highlighted text here is where you would define the feature UI. The UI is like the Extrude feature. So we don't need one, because we're just creating variables, we're just going to commit the feature and hit the check. That's it. So we need to go down here where we define the functions action. And there's really just two functions you need to know in Featurescript in order to set variables. One really, and then another one could be handy. So the function you need to know is called "Set variable", just typing "set V" and enter and will autocomplete. It's pretty straightforward.
So here where it says Name, that you'll need to input a string and that will be like, you know, you would type hashtag whatever it is for your future. In this case, let's do "height". Typing too fast. I'm hitting Tab, by the way to cycle through the options here. When you first insert a snippet, you'll see them all highlighted and you can tab and Shift Tab to jump around makes a little quicker.
So here I'm going to say that this is going to be 90 times millimeter. When you're using it Featurescript. You do have to type times millimeter, you can't just type 90 mm, can't explain why but it's how it works. That's it we're done. We have created a variable. So I'm going to go ahead and hit Commit right here. And you can think of this like save. In fact, the hotkey is Command S on a Mac. And yes, it just prevents your part studio.
And everywhere that this feature is used for rebuilding, every time you type in your character, it lets you get something that you're ready to push out and then push it. So let's go back to our part studio, I'm going to come up here where it says custom features in this workspace, click the drop down, insert my feature, boom, we got global variables, but it's at the bottom of the tree. And it's just like the variable feature, you need to have it above anything you want it to influence. So you'll see when we bring it up here, nothing is going to happen. And the reason for that is because variables really are variable, they can override one another. So I have this one here called "Height", and it's 25 millimeters. So it's overriding the one that we just created. If I suppress this, you'll see our box will become 90 millimeters tall, nice tall bucket of a box. Yes, so I will go back and set up all the rest of these variables in the same way. And we'll get back to you.
All right, that's it.
I'm going to hit commit, and hope that I spelled everything right. Back to our part studio. And if we suppress all of this, and I haven't messed anything up, we'll do a round of applause for me. Hey, it worked. All right, Oh, no. Something blew up. That's my insert, diameter, probably.
We're back. All right, you can retract your round of applause. But yes, this is now working. And if I change anything here, it's going to update in my design. So I could change my nominal to something more obvious, like one millimeter, not realistic, but will you'll be able to see the change.
Very thin, broke my lip features. But whatever. So I can now do three millimeters instead. And maybe you'll actually see a nice rebuild. Yes, so that works, this thing is a little chunkier now, this will be a nice, durable, maybe outdoor container. So yes, that's one way to set this up. But there's one thing that I would want to do, and I would normally do with these variables, if you see I go into my-- Let's see, I will unsuppressed my rib variable and jump in here, you can see I actually have it equal to the nominal wall times .6 so that the rib thickness is actually driven by the nominal wall. And it's always equal to 60% of the nominal wall. But the way that I have typed it here, it's just 1.5, which was 60% of 2.5. But it's not dynamically updating. So the way that we're going to do that is the other function that I mentioned, is get variable. This is all pretty straightforward stuff. All we need to input here, we leave the context alone on both of these, I'm going to use quotes, and type "nominalwall", boom! Oh sorry, we have now gotten the variable. And what this function does is it looks for a declared variable like this, and it pulls the value, which is here. So anywhere that I type, "GET variable", all of this highlighted text minus the semicolon, which is more like a punctuation mark in the code.
Anywhere I take that, it's the same as typing 2.5 millimeter. So I'm going to cut this code, get rid of the semicolon. And we'll come down here to where we actually want to use that value. Now all we got to do is times .6. So now, this bit of text here is the same as saying 2.5 millimeters, times .6. So our rib should now be 60% of our nominal wall at all times. Pretty good.
Now I do want to show you, I'm going to keep leveling this up and just show you some baby steps and work your way toward maybe even starting to write some actual features. I'm trying to trick you into writing some actual features here. But we'll do it in short, small steps. So one thing you might want to do is make the driving dimension something editable from the user interface. And that would also make them then configurable. So let's do that.
First, let me show you what we got, again, inside of the interface. So you can see that there's really not a whole lot going on. We have this slider and we have the final button. But neither of those are really of any value because there's nothing we can change to see the difference. So might as well not be there. In fact, there's a way to hide them, but I'm not going to get into it.
All right, so in order to make these things accessible from the user interface, I'm going to come up here. These are our parameters pretty much anything that you touch or type In the User Interface is a parameter. So I'm going to drop this down. And again, we got a bunch of snippets, so you don't have to remember them. I'm going to grab a length and type height, length is just anything times a linear dimension.
And we'll do the same for length. And we'll do the same, again for the width.
Okay, so a little bit of anatomy here, again, this is the user facing name. So this is what it will say in the user interface. And this right here, "definition.whatever" is the value. So anything the user types, if you push-- if you put this into your code, it's the same as putting whatever they type. So in this case, I want "definition.height" to be my height. "definition.length, and "definition.width."
Okay, so you'll see that we got a teeny tiny box. And the reason for that is in the parameter here, we're using the length bounds, which just will limit what numbers you're allowed to put into the box. And it also defines what the default number is. In this case, when you use length bounds, your default number is always going to be 25 millimeters if you're in millimeters. So in this case, we can hit the final button, it'll actually start being useful to us now. We'll do 20 on the height, 100 on the length and 50 on the width.
So aside from being just convenient, a convenient place to edit them all at once. The other benefit here, not that is that you can configure this. So I'm going to say small, big, whatever you want. And then I'm going to hit configure features. And you can configure this, just like a native feature, which I think is pretty freakin cool. So we've got all of these three parameters now configured, our small one can be that and our big one can be double everything.
And so now, if we grab big, we've got-- So that's handy. That's a handy way that you can extend just writing some variables. And it's also still declaring all the other ones that we needed inside of this one feature. So it's a pretty handy way to make, like a tool that you can use for a project.
But let me take it one more level up, I want to show something that I built for us to use internally at Ovyl, formerly Fractal. And that's a whole chart of variables related to a library of stock hardware that we always keep in stock, we have a small limited selection of various screw sizes and lengths with accompanying nuts, and threaded inserts and some springs and you know various things. And I've made configured on sheet models of all of these so that it's really easy for us to drop them in our designs.
And then I've also made a feature that calls variables relevant to any design considerations you might have, like the counterbore size, or the length and diameter of your inserts or anything like that. So it makes it really super easy to use them in the design. And then you know, we have them in the back, efficiency.
All right hardware library, I'm going to use the search tool. So here's the feature that I wrote for us to use internally here. And this one isn't project variables, but it is a huge list of essentially autocomplete values that could be valuable in a design. So I'm going to complete that. I got to move it to the top of my tree.
So now let's say I wanted to use some inserts that I know we have in stock, I can edit this sketch and set this. Let's see, type Insert. And now I have all of these insert depths and insert hole diameters. And I'm going to use whatever a 632, let's say, it's a little bit bigger. And the way that I'm driving the depth here is with this Mate connector offset.
All right, so I'm going to do insert. If I type 10, it will narrow it down. Oh, it's not 1032, 1024* Excuse me. Insert depth. Let's do that. Hit the check. So now I know that these are going to fit the hardware that is right behind me. That's a really handy way. Let's go look at what that feature looks like. Actually, before we do, I do want to show you a little bit of the UI. This is a really simple UI. This feature is really still only just declaring variables.
But I did add a couple of levels here. So you can include all the variables for all the hardware because things do autocomplete kind of can get a little cluttered, so I made it so you can choose which ones you want. So let's say we're just wanting inserts for this design. And I don't know why you would want one of these and not the other, it makes a little more sense when you get two screws, and may not need any counterbores for your design.
So all that I'm doing here is just either declaring or not declaring variables based on a couple of Boolean inputs, that that's the name for these check marks. Not- Nothing too crazy, that's going to fail, because I turned everything off. There we go. Right click, open link document, let's go check it out.
Okay, so the precondition here, you'll see this is our-- The start of our user interface. And there's a lot of text here, but nothing very complicated. I'm going to return to Main so that I can show you where some of these things are from.
Let's see, here is a declaration of a group parameter. That's one of these parameter group. So that auto completes, then you just drop your stuff in here. This is a Boolean, again, that's already right here for you. This creates one of those checkboxes. So there's nothing really wild happening here. This is an if statement. So it'll hide part of the UI if something is checked or unchecked. And then on down to the actual code, where I'm declaring these things. This is the same thing that we've already done, we're setting the variables.
In this case, there's a lot of repeating text. And this was before I learned a trick, but I'll show you in a few minutes or seconds. So I actually just set this up in a spreadsheet, because I'm not a code guy, I just wanted to get it done. And I just concatenated the parts that repeat and then had a table that I copy pasted from somewhere online, of all of the different dimensions that I needed, and then just copied and pasted from that into here. So saves me a whole ton of typing. And then I just did a little bit of formatting dropped these inside of some if statements to to either declare these variables or not, depending on the checkboxes. And that's it. So this looks like a lot of text. But there's really not a whole lot going on here. And this would be pretty easy for someone who's not really familiar with code to implement at their own company, with just a little bit of fiddling around.
So that's another sort of next level up of how you could treat variables. And you know, a value of creating variables with Featurescript that you would not practically do with the native variable feature.
I do want to show one more level up from there.
So I do a lot of injection molding. And it is really common for me to have, you know, a nominal and a rib and a draft and Fillets and a lot of the same variables I'm declaring over and over again, because I'm designing for the same processes over and over again. So to that end, I wrote the manufacturing variables custom feature, and all this thing does is declared the variables that I declared over and over again, benefits, of course, being that the default values are just a sensible thing. You know, I don't want it to be 25 millimeter nominal wall ever. And the rib thickness is already defined as 60% of the nominal. So that saves me some time.
The draft default values, again, are just pretty basic, I can add some clearances, and Fillets if I want, I can pick a different manufacturing process like laser cutting, and it has a whole different set of variables, but it will declare instead. So this is pretty handy. You can even prepend the name so that, you know, I could say I have a top enclosure and a bottom closure that have different nominals, I can still make that work. So really handy. And honestly, the idea here is a lot bigger than just this.
If you do anything that's industry specific for you, I'm thinking like speaker enclosures have a ton of math behind them. And you know, people have big old spreadsheets to try to figure out what numbers they need to put in their speaker design. You could do all of that with Featurescript and create an extremely powerful feature that doesn't actually produce any geometry.
So you could just have it setting up a lot of your variables and interactions and do some of the math. And then from there, it's pretty straightforward to create your model once you have all your numbers. I think this could apply to something like airfoil designs or the volume of you know fluids to wait or whatever anything that you're using a spreadsheet for right now. You could probably wrap all of that up into a custom feature that does that math for you on the fly.
All right, I do want to show one more thing. I know that you thought this video was going to be short.
The other thing I want to show is a slightly more concise way to write all of this. That takes a little bit more Featurescript know how so if you're already feeling intimidated- Maybe give this a try-- I mean, give exactly what we've already shown you a try. But if you want to step it up to the next level, this method is less typing. And it's easier to read and even easier for somebody who doesn't know anything about code to edit. So if you're setting it up for a team, it's actually probably a little bit easier for them to comprehend. And then this kind of can be intimidating to see. So I'm going to get rid of this and show you a different way that we can declare these.
Okay, so here is another slightly more concise way to declare these. One thing I like to do is I don't like typing millimeter all the time. So I can type a 'const' to declare a constant, mm could be anything, but of course, it's going to be mm, because I'm going to make it equal to millimeter and semicolon to end that. So anytime I type mm, now it's going to be as if I typed millimeter save me some typing time, which I am big on.
Next, here's where we're actually going to do the special part, I'm going to say var V equals open and close curly braces, what that represents is that we have now an empty map. And what a map is, is a key value pairing. And what that means is if you give it the key, it will get spit back the value. So an example of a non empty map might be something like this thing is 5 times mm which we can type now because of our constant, and then if I typed v dot, this thing that would equal 5 millimeters, it would be the same as if I typed 5 millimeters. But if I type v.newthing=7 millimeters, then it would actually add a new thing here, it would be the same as if this already had that in there.
All right, so backing up to where we were, we have an empty map, and we're going to add each of our new variables in the same way, so:
And so on, I'm going to go ahead and speed up the video here. Okay, we typed them all in here. And you can see how much more concise this is. And it's so much easier to read, you just ignore the v dot and then you know what your variable is called. And you know what the value is, it's just a little more straightforward. You'll also notice that we don't have to use Get variable anymore to reference another one. Here, you can just type-- You know, I've got my nominal wall declared here. That means anywhere I type v.nominalwall, it's the same as if I had typed, whatever value I have here. So I can just type v dot nominal wall times .6. So that's a lot more straightforward than having Get variable inside of your other function. It's just more readable. But if I commit this, it's going to fail, because we haven't actually declared the variable that other features can see. So in order to do that, we still need to use set variable. But we're going to use a loop to iterate through and set it for each of these things. But we only have to type it once. That's what's great about it. So we'll use a for in loop [phonetic 23:16]. And what this will do. Here where it says item, I like to say entry, it just makes more sense to my brain, and the container is V, that's our map, which now has all of these things inside of it. So the entry is each individual thing. And v is sort of like the bundle of all of the things and it will go through each thing and do whatever we ask it to do. So in this case, we're going to set a variable. And for the name, I'm going to do "entry.key." And the word key is reserved for maps. And it will actually output the string of whatever your key is.
Remember, it's a key value pairing. So here, we just need "entry.value". And that's it. We're done. So you can see it's a lot less typing, a lot less text, and it's a lot easier to read. So I'm going to come back here, nothing should fail. Yes, we're in business. So I think that's really it. I think we've we finally reached the end. Yes, that's beautiful.
All right. That's the end of the video. Hopefully, you learned something. If you didn't learn anything, either you weren't paying attention, or you should start making videos and I'll watch them. But if you had any questions, I will certainly unearth some questions. Let me know in the comments. And if you want to learn some more about Featurescript, and there's something specific you'd like to learn, no promises but also let me know in the comments, I'm wanting to make some more videos about Featurescript. Like the video and that will help me out and if you subscribe and hit the bell. You'll know when I launched more content like this. I try to pack it with info so yes, it's worth to subscribe. Thanks for watching!