Evaluating FeatureScript

This page describes some of the APIs Onshape provides for evaluating FeatureScript.

ℹ️ Onshape API Notes
  • All Onshape API calls must be properly authenticated.
  • For Enterprise accounts, replace cad in URLs with your company domain.
  • Onshape IDs are written as: {did}, {wvmid}, {eid}, {pid}, {otherId}. See Onshape API Intro for information on what these IDs mean and how to obtain them from your documents.
  • Variables for replacement are written as: {variable1} or <variableTwo>. Never include the braces or angle brackets themselves with these variables.
  • Visit the Learning Center's Intro to the Onshape API course for additional instruction and videos.

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": ""
      }'
ℹ️ NOTE

Only lambda expressions can be evaluated with this endpoint.

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.

  1. 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.

Additional Resources