Evaluating FeatureScript
This page describes some of the APIs Onshape provides for evaluating FeatureScript.
Prerequisites
|
---|
FeatureScript vs REST API
When working with complex geometry, you might find working directly in FeatureScript easier than working with the Onshape REST API. Click here to see the FeatureScript documentation.
Endpoints
- evalFeatureScript
curl -X 'POST' \ 'https://cad.onshape.com/api/v6/partstudios/d/{did}/w/{wid}/e/{eid}/featurescript?rollbackBarIndex=-1' \ -H 'accept: application/json;charset=UTF-8; qs=0.09' \ -H 'Authorization: Basic CREDENTIALS'\ -d '{ "libraryVersion": 2144, "script": "" }
Sketch plane IDs
The following string can be sent along with the featurescript API to get the plane ID. You can then use that ID to specify the plane on which to create a sketch.
function(context is Context, queries) {
return transientQueriesToStrings(evaluateQuery(context, qCreatedBy(makeId(\"{planeName}\"), EntityType.FACE)));
}
For example, to get the ID of the Top plane, you would make the following call:
curl -X 'POST' \
'https://cad.onshape.com/api/v8/partstudios/d/{did}/w/{wid}/e/{eid}/featurescript?rollbackBarIndex=-1' \
-H 'Accept: application/json;charset=UTF-8; qs=0.09' \
-H 'Authorization: Basic CREDENTIALS' \
-H 'Content-Type: application/json;charset=UTF-8; qs=0.09' \
-d '{
"script": "function(context is Context, queries) { return transientQueriesToStrings(evaluateQuery(context, qCreatedBy(makeId(\"Top\"), EntityType.FACE))); }"
}'
Sample Workflows
Calculate a tight bounding box
Though the Onshape API offers a getPartStudioBoundingBoxes endpoint, it does not result in a tight bounding box. The values returned are meant for graphics and visualization, and are approximate. To calculate a more accurate bounding box, we’ll use the evalFeatureScript endpoint.
- Call the evalFeatureScript. Replace the URL parameters with the IDs from the Part Studio you’re working with, and replace
CREDENTIALS
with your authorization credentials. The endpoint will return a tight bounding box in the response.curl -X 'POST' \ 'https://cad.onshape.com/api/v6/partstudios/d/{did}/w/{wid}/e/{eid}/featurescript?rollbackBarIndex=-1' \ -H 'accept: application/json;charset=UTF-8; qs=0.09' \ -H 'Authorization: Basic CREDENTIALS'\ -d '{ "libraryVersion": 2144, "script": "function(context is Context, definition is map) {const boundingBoxTight = evBox3d(context, { \n \"topology\" : qConstructionFilter(qEverything(), ConstructionObject.NO),\n \"tight\" : true\n}); \n return (boundingBoxTight);}" }'
Create a sketch
See API Guide: Features for the Create a sketch
tutorial.