Evaluating FeatureScript

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

📘 Notes

  • This page provides sample code as curls. See the curl documentation for more information.
  • All Onshape API calls must be properly authenticated by replacing the CREDENTIALS variable in the curls below. See the API Keys page for instructions and the Quick Start for an example. All applications submitted to the Onshape App Store must authenticate with OAuth2.
  • This documentation refers to Onshape IDs in the following format: {did}, {wid}, {eid}, {pid}, {otherId}. These represent document, workspace, element, part, and other IDs (respectively) that are needed make the API calls. We sometimes abbreviate these variables as DWVEM Please see API Guide: API Intro for information on what these IDs mean and how to obtain them from your documents. Never include the curly braces ({}) in your API calls.
  • For Enterprise accounts, replace cad in all Onshape URLs with your company domain. https://cad.onshape.com > https://companyName.onshape.com

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.

  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