Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- 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.
Predicates within an array?

The predicate doesn't recognize the input map unless it is hard-coded with the same name "widget". Is there a trick to getting this to work? One that doesn't involve hard-coding the map name into the predicate?
FeatureScript 2752; import(path : "onshape/std/common.fs", version : "2752.0"); annotation { "Feature Type Name" : "test" } export const test = defineFeature(function(context is Context, id is Id, definition is map) precondition { annotation { "Name" : "Widgets", "Item name" : "Widget" } definition.myWidgets is array; for (var widget in definition.myWidgets) { testPredicate(widget); // this is the only declaration } } { // feature body }); predicate testPredicate(d is map) { annotation { "Name" : "My Length" } isLength(d.myLength, LENGTH_BOUNDS); }
Learn more about the Gospel of Christ ( Here )
CADSharp - We make custom features and integrated Onshape apps! Learn How to FeatureScript Here 🔴
Best Answer
-
tabetha_bulnes Member, Developers, HON-TS Posts: 31 PRO
interesting idea. I was able to get it to work but there are some quirks you have to work around.
The var name MUST MATCH the map name in the inner predicate. And then if you want to have multiple uses in a single FS then you need to add a string variable otherwise it flags the inner predicate as a duplicate.
annotation { "Feature Type Name" : "test" }
export const test = defineFeature(function(context is Context, id is Id, definition is map)
precondition
{
annotation { "Name" : "Widgets", "Item name" : "Widget" } definition.myWidgets is array; for (var widget in definition.myWidgets) { testPredicate(definition, widget, "0"); // this is the only declaration } annotation { "Name" : "Widgets", "Item name" : "Widget" } definition.myWidgets2 is array; for (var widget in definition.myWidgets2) { testPredicate(definition, widget, "1"); // this is the only declaration } export predicate testPredicate(definition is map, widget is map, i is string)
{
annotation { "Name" : "My Length" } isLength(widget["myLength" ~ i], LENGTH_BOUNDS);
}2
Answers
Predicates can take additional inputs. So you need to have both the definition as an input and the array item.
Ex.
predicate testPredicate(definition is map, widget is map)
{
isLength(widget.myLength)
}
and then just call it inside the array as you are expecting, but with both definition and widget
Side note, definition cant be passed in twice, so a predicate only work in either array or not in an array, but not both.
Custom FeatureScript and Onshape Integrated Applications
I don't even know what a predicate is or does.
@joshtargo Read the predicates section of https://cad.onshape.com/FsDoc/top-level.html
Its a special type of function typically used for type checking. They get used in the precondition of a feature as a way of making the feature definition/inputs templatable and repeatable. If you have common inputs across a library of features it lets them share that input code.
Custom FeatureScript and Onshape Integrated Applications
@Caden_Armstrong how is that technique different than what I was doing? It still requires the array name to be literal, hardcoded into the predicate, so there might as well not even be an input for the array map. It would mean that the predicate is useless unless all arrays are named the exact same which defeats the purpose of having a predicate like this.
Learn more about the Gospel of Christ ( Here )
CADSharp - We make custom features and integrated Onshape apps! Learn How to FeatureScript Here 🔴
interesting idea. I was able to get it to work but there are some quirks you have to work around.
The var name MUST MATCH the map name in the inner predicate. And then if you want to have multiple uses in a single FS then you need to add a string variable otherwise it flags the inner predicate as a duplicate.
O and I guess one more edit to the above would be that you don't need to reference the definition in the inner predicate nor the export the predicate.
Ty for the input @tabetha_bulnes & @Caden_Armstrong. This is further than I was able to get previously. Seems like a type check "bug" or lacking implied functionality.
Learn more about the Gospel of Christ ( Here )
CADSharp - We make custom features and integrated Onshape apps! Learn How to FeatureScript Here 🔴