Adam shows you how to use FeatureScript for Onshape, and possibly also how to be Mr. Handy Pants around the house. But mostly FeatureScript.
Quick side note: SolidSmack has a lot of great sponsors, including several of the biggest CAD companies on earth. That’s awesome. One of those sponsors is Onshape.
This post, however, is not sponsored by anyone. This one’s all me.
As of a quiet rollout last night, FeatureScript is live.
FeatureScript is, in my opinion, the single most important feature of Onshape. It could very well be the killer app that brings Onshape into the mainstream. It’s a core piece of the Onshape architecture, and as of today it’s available to everyone.
(The follow-up tutorial mentioned in the video is available here.)
The features you write in FeatureScript appear in the Onshape UI just like any other feature, right alongside Extrude and Revolve and Sweep and Loft. The UI for your feature will look and work just like every other feature in Onshape. You can add your features to the toolbar and use them in all of your projects. You can even share your features with others so that they can
see how clever you are take advantage of them.
Things you can do with FeatureScript
The first thing most people will try to do in FeatureScript is essentially a macro-style list of existing Onshape features: run an extrude, then another extrude, then a fillet, then a boolean, then another boolean, etc.
For example, I like the SolidWorks “Cut With Surface” command, but Onshape doesn’t have it. I wrote my own in FeatureScript by implementing a split command, followed by a delete bodies command. Easy peasy.
Taking us one level deeper, every feature in Onshape was written using FeatureScript. That means that by definition, FeatureScript is capable of literally everything Onshape can do. If you’ve seen an Onshape feature do it, you can do it in FeatureScript.
So, for example, instead of using Onshape’s “extrude” command (the one you see in the Onshape UI), you can use the deeper opExtrude function, providing deeper, richer access to the internal workings of an extrude. Using this method I built an extrude command that can extrude multiple profiles at the same time, each normal to its own sketch plane (instead of all going the same direction, as in Onshape’s extrude). This is a trivial example, but there are many more.
Then you can do a lot of stuff that Onshape features can’t currently do, too. For example, there’s no 3D spline tool in Onshape, but FeatureScript gives us access to a spline that we can run through a selection of 3D points, essentially rolling our own 3D spline tool.
In experimenting with FeatureScript, I focused mainly on carpentry tools: lumber, plywood, dado cuts, mortise & tenon, tongue & groove, pegs, rabbets, and lap joints. I made custom features that can perform multiple joinery operations in a single feature, and one that can create an arbitrary number of standard boards around a perimeter.
The beauty of FeatureScript is that things don’t have to be linear: we can add conditional statements like “if distance x is further than 16 inches, add another joist”. You could dynamically generate staircases, railings, ribs, bosses, grilles, perf patterns, or even irregular structures like Voronoi patterns and generative architecture. The possibilities are really exciting.
Things you can’t do with FeatureScript
FeatureScript is fantastic, but there are a lot of things it will never be able to do.
First of all, despite having the word “Script” in the name, FeatureScript isn’t really a scripting system, at least not in the traditional desktop sense of the word. It is a script for the creation of features, which have a very limited and specific meaning in Onshape. Specifically, features exist within Part Studios, and help to define the geometry that is generated there.
For example, you cannot write a FeatureScript that creates a Part Studio or an Assembly. Features exist within Part Studios, so they certainly can’t create them. You can’t use FeatureScript to generate a drawing or a rendering. FeatureScript can’t email an administrator via email if Part 12345R6 changes, or run a cost analysis across an entire project. In short, FeatureScript is intended for creating geometric features within a Part Studio, and not much else.
There are lots of esoteric programmy things it can’t do, too. For one thing, it can’t create new geometry types. For example, you couldn’t use FeatureScript to create a subdivision surface modeling tool, or to manually generate degree-739 NURBS surfaces. You can’t build a voxel system or interact with the GL meshes rendered on screen.
As with any cloud system, security is extremely important within Onshape. As such, FeatureScript can’t make calls to external web addresses of any kind. So you can’t, for example, have a feature that spells out your most recent tweet in block letters.
Also, as you probably know, Onshape allows multiple users to work on the same document simultaneously, so it’s very important that all feature results be purely deterministic. In other words, a feature should always have the same result with each and every rebuild. This might seem obvious, but there are some important limitations that come up when you realize this.
For one thing, FeatureScript has no date or time functions. If it did, you could make features that rebuild differently based on the day of the year or time of day. This might seem like a humbug, but remember: you might not be the only one seeing it. If you’re looking at an Onshape document with a user in Japan, whose day or time should it use? That’s a superficial problem, but there are deeper ones. Suffice it to say that date functions in FeatureScript would be problematic, so there aren’t any.
Similarly, FeatureScript cannot generate truly random numbers. If it could, each rebuild could result in wildly different things. Your collaborator in Japan would be seeing something entirely different than what you’re seeing, and neither would be “true” to the file. Export would be entirely unpredictable. Instead you can use psuedo-random numbers generated using a seed value. These are random enough for most users, but without the problem of being non-deterministic.
Can other CAD tools do this?
Most CAD systems have some sort of macro system for stringing together sequences of tool operations. Most also have some sort of scripting system for automating tasks, typically using a non-proprietary language like Python or–in the case of SolidWorks–Visual Basic (Adam projectile vomits across the office). Some even have GUI interfaces for storing or recording macros, known variously as Macros, Library Features, User Defined Features, or Power Copy, depending on your tool. Grasshopper lets you create logic-driven geometry using a nodal system that’s essentially a visual programming language. All decent options.
Most CAD tools also have some sort of API for developing richer plugins, usually in c++, but you’re going to put on your Big Kid Pants for those, as they’re typically written and maintained for use by professional developers, not your average user.
All of these options are great, but FeatureScript is a bit different. As with any walled-garden, it comes with both advantages and disadvantages. On the upside, FeatureScript lets you write first-class features in Onshape that look and feel just like the features that ship with the product, and do it with surprisingly little code. Downsides include learning (another) proprietary language, no system-wide automation, and very limited access to the deep inner-workings of the system.
All in all, having written many plugins for other apps over the years, FeatureScript is really exciting. For me, the benefits vastly outweigh the limitations.
More to come
This is going to be fun. I’ll be publishing tutorials in the coming weeks (the first one is here). Stay tuned!