<p>Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.</p></p><strong>First time visiting? Here are some places to start:</strong></p><ol style="list-style:inherit;margin-left:25px;"><li>Looking for a certain topic? Check out the <a href="https://forum.onshape.com/categories">categories filter</a> or use Search (upper right).</li><li>Need support? <a href="https://forum.onshape.com/post/question/community-support">Ask a question</a> to our <a href="https://forum.onshape.com/categories/community-support">Community Support</a> category.</li><li>Please submit support tickets for bugs but you can request improvements in the <a href="https://forum.onshape.com/categories/product-feedback">Product Feedback category</a>.</li><li>Be respectful, on topic and if you see a problem, Flag it.</li></ol><p>If you would like to contact our Community Manager personally, feel free to <a href="https://forum.onshape.com/profile/6/LouGallo">send a private message</a> or <a href="mailto:lgallo@onshape.com">an email</a>.
</p>

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

he_qiu143
he_qiu143 Member Posts: 10 ✭

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

  • EvanReese
    EvanReese Member, Mentor Posts: 3,082 PRO
    edited February 2025 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
    The Onsherpa | Reach peak Onshape productivity
    www.theonsherpa.com

Answers

  • EvanReese
    EvanReese Member, Mentor Posts: 3,082 PRO
    edited February 2025 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
    The Onsherpa | Reach peak Onshape productivity
    www.theonsherpa.com
  • he_qiu143
    he_qiu143 Member Posts: 10 ✭

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

  • S1mon
    S1mon Member Posts: 4,576 PRO

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

    Simon Gatrall | Product Development, Engineering, Design, Onshape | Ex- IDEO, PCH, Unagi, Carbon | LinkedIn

  • Paul_J_Premakumar
    Paul_J_Premakumar Member, Onshape Employees Posts: 238 image
    edited February 2025

    @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_qiu143
    he_qiu143 Member Posts: 10 ✭


    Yes, Im trying to replicate parametric features