Evaluating FeatureScript

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

Prerequisites
  • All Onshape API calls must be properly authenticated.
    • 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.
  • For Standard accounts, replace {baseUrl} with cad in all endpoints. For Enterprise accounts, replace it with your company domain. Add /api and the version number, and then provide the endpoint.
    • https://{baseUrl}.onshape.com/api/v10/documents
    • https://cad.onshape.com/api/v10/documents
    • https://companySubDomain.onshape.com/api/v10/documents
  • Onshape IDs are written as: {did}, {wvmid}, {eid}, {pid}, {otherId}.
    • These represent document, workspace (or version or microversion), element, part, and other IDs, respectively.
    • Never include the braces {} with your IDs.
    • See API Guide: API Intro for information on what these IDs mean and how to obtain them from your documents.
  • This page provides sample code in cURL and python.
  • For additional instruction and video content, visit the Learning Center’s Intro to the Onshape API course.

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