Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. 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.

How to use the API to execute a FeatureScript to create a custom feature?

he_qiu143he_qiu143 Member Posts: 6

I created a Part Studio and a Feature Studio in the same document.

Here is the content of the Feature Studio.

FeatureScript 2581;
import(path : "onshape/std/common.fs", version : "2581.0");

annotation { "Feature Type Name" : "Fillet Everything" }
export const filletEverything = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {

    }
    {
        opPlane(context, id + "plane1", {
                "plane" : plane(vector(0, 0, 6) * inch, vector(0, 0, 1))
        });
    });

As you can see, this script simply creates a plane. Now, I need to use the Onshape Python API to call this FeatureScript in the Part Studio to create a plane. My Python code is as follows:

        body = {
"btType": "BTFeatureDefinitionCall-1406",
"features": [
{
"featureType": "filletEverything",
"btType": "BTMFeature-134",
"name": "Fillet Everything 1",
"parameters": [],
"featureId": "FGMsohIYH03BN1f_0",
"subFeatures": [],
"namespace": "e03fd799aee4796bff7177d98::m4c232d7c5b8456deef0ccc33",
"nodeId": "QtTe+DBU77uVY1dM",
"returnAfterSubfeatures": False,
"suppressed": False
}
],
}

res = self._api.request('post', '/api/v10/partstudios/d/' + did + '/w/' + wid + '/e/' + eid + '/features',
body=body)

The content of the body is obtained by calling the getFeatures API, so the body content should be correct. However, whether I use the Python API or the API Explorer, I am unable to create the plane in the Part Studio. The error message returned is as follows(from python):

'{
"message" : "An internal error has occurred; support code 6a3cbb3b760a3d749babc43e",
"status" : 500,
"code" : 0,
"supportCode" : "6a3cbb3b760a3d749babc43e",
"moreInfoUrl" : ""
}'

Besides that, I have also tried using other versions of the API, such as v9, as well as the default API, but none of them were able to create the plane successfully. I believe the issue lies with the FeatureScript, because I can correctly create features like sketches and extrudes using a similar approach.

The reason for using the API to execute the FeatureScript is that, through FeatureScript, I can create a sketch plane by directly specifying the point coordinates and the normal (using the newSketchOnPlane function). Since my data only contains the Origin, normal, and x information, I have no choice but to use this approach to create the sketch plane

If anyone has experience with executing FeatureScript scripts in a Part Studio via the API, or if you have any insights into what might be causing this issue, I’d really appreciate your help!

Best Answer

  • EvanReeseEvanReese Member, Mentor Posts: 2,251 ✭✭✭✭✭
    edited February 4 Answer ✓

    It's not clear to me why you need the API for this. I don't think it's an issue with your FeatureScript code (though you may want to rename it from "filletEverything")

    I did my own test and got it working. Here's the FeatureScript and API body I used with glassworks.

    FeatureScript

    FeatureScript 2581;
    import(path : "onshape/std/common.fs", version : "2581.0");

    annotation { "Feature Type Name" : "Add a plane", "Feature Type Description" : "" }
    export const addPlane = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
    // Define the parameters of the feature type
    }
    {
    opPlane(context, id + "plane1", {
    "plane" : plane(vector(0, 0, 6) * inch, vector(0, 0, 1))
    });
    });

    API Request Body for POST/partstudios/d/{did}/{wvm}/{wvmid}/e/{eid}/features

    {
    "btType": "BTFeatureDefinitionCall-1406",
    "feature": {
    "btType": "BTMFeature-134",
    "namespace": "e548d56ece749371a5a6ad6ec::m3e236759fa16868e79d8699e",
    "name": "Add a plane 1",
    "suppressed": false,
    "parameters": [],
    "featureId": "FFdDMAZL9at4Q6m_0",
    "nodeId": "KG/OVwqfZXVDmvzg",
    "featureType": "addPlane",
    "returnAfterSubfeatures": false,
    "subFeatures": []
    }
    }

    Evan Reese

Answers

  • EvanReeseEvanReese Member, Mentor Posts: 2,251 ✭✭✭✭✭
    edited February 4 Answer ✓

    It's not clear to me why you need the API for this. I don't think it's an issue with your FeatureScript code (though you may want to rename it from "filletEverything")

    I did my own test and got it working. Here's the FeatureScript and API body I used with glassworks.

    FeatureScript

    FeatureScript 2581;
    import(path : "onshape/std/common.fs", version : "2581.0");

    annotation { "Feature Type Name" : "Add a plane", "Feature Type Description" : "" }
    export const addPlane = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
    // Define the parameters of the feature type
    }
    {
    opPlane(context, id + "plane1", {
    "plane" : plane(vector(0, 0, 6) * inch, vector(0, 0, 1))
    });
    });

    API Request Body for POST/partstudios/d/{did}/{wvm}/{wvmid}/e/{eid}/features

    {
    "btType": "BTFeatureDefinitionCall-1406",
    "feature": {
    "btType": "BTMFeature-134",
    "namespace": "e548d56ece749371a5a6ad6ec::m3e236759fa16868e79d8699e",
    "name": "Add a plane 1",
    "suppressed": false,
    "parameters": [],
    "featureId": "FFdDMAZL9at4Q6m_0",
    "nodeId": "KG/OVwqfZXVDmvzg",
    "featureType": "addPlane",
    "returnAfterSubfeatures": false,
    "subFeatures": []
    }
    }

    Evan Reese
  • he_qiu143he_qiu143 Member Posts: 6

    Thank you very much for your response! I need to transfer some models from CATIA to Onshape, where the planes are defined by 3D points and normals. However, I couldn’t find a way to create planes using 3D points in Onshape’s API; it can only be done through FeatureScript. That’s why I chose this approach. (The FeatureScript example provided is just a very simple sample used to explore how to call custom features.)

  • S1monS1mon Member Posts: 3,175 PRO

    And why wouldn't you just use STEP files? Are you trying to replicate parametric features?

  • Paul_J_PremakumarPaul_J_Premakumar Member, Onshape Employees Posts: 223
    edited February 4

    @he_qiu143 , You got the support code, because the body of the add Part studio feature endpoint expects feature:{} , while in the example you posted you has features:[] .

  • he_qiu143he_qiu143 Member Posts: 6


    Yes, Im trying to replicate parametric features

Sign In or Register to comment.