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.

Custom FeatureScript API evaluate

daniel_zeng842daniel_zeng842 Member Posts: 4 EDU
Hi!

I was looking for a way to get information out of a FeatureScript, and I noticed there were several forum posts suggesting to use the API call "evaluate_featurescript":
https://forum.onshape.com/discussion/11342/evaluate-existing-featurescript-using-api

I'm wondering if someone can further elaborate on something similar to https://forum.onshape.com/discussion/5382/whats-a-complete-api-call-to-partstudios-featurescript-look-like (I believe now making a reference to custom feature code should be possible given that it was 5 years ago).

I currently have the following code, and I just want to see if I can get either the length or the "0" from the custom feature:

API call for evaluate_featurescript:
{
    "script" : "function (context is Context, queries is map) { return skribfunc(context, newId(), queries); }",

    "queries" : [ { "key" : "Length", "value" : [ "4 in" ] } ],

    "sourceMicroversion" : f["sourceMicroversion"]

}\
Where f is gotten from an API call to get the feature list.
(I have tried it with "script" : "function (context is Context, queries is map) { return skrib(context, newId(), queries); }", as well and both scripts resulted in "u'Function skribfunc with 3 argument(s) not found'")

Custom FeatureScript:
FeatureScript 1511;
import(path : "onshape/std/geometry.fs", version : "1511.0");

annotation { "Feature Type Name" : "skrib" }
export const skrib = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
        annotation { "Name" : "Length" }
        isLength(definition.Length, LENGTH_BOUNDS);
    }
    {
        println(definition.Length);
        return "0";
    });

function skribfunc(context is Context, id is Id, entities is Query)
{
    println(entities.Length);
    return "0";
}

Best Answer

  • caden_armstrong2caden_armstrong2 Member, User Group Leader Posts: 127 ✭✭✭
    Answer ✓
    My suggestion is to make another featurescript in another tab that has no access to your modified laser joint function.
    In there create a test function to see if you can get the result you want to eventually get through the API (in this case a "getVariable")
    Once you know your FS code works, then move it into the API call. Debugging FS code in an API application is much more difficult than working out the kinks inside of Onshape. In onshape you can do helpful things like addDebugEntities to check if you're doing it correctly.

Answers

  • caden_armstrong2caden_armstrong2 Member, User Group Leader Posts: 127 ✭✭✭
    The evaluate_featurescript API doesn't give you access to the featurescript code that is included in your custom feature. You can only call functions that are in the standard library. This is why it doesn't exist, the featurescript you evaluate exists in its own bubble. But there are some options to get data from a custom feature.

    With that being said, its also hard to suggest a fix for you because I don't understand what your goal is. The query that you've provided in your API call is also not valid. The skribfunc also isn't clear what its purpose is, as the Query datatype doesn't have a "Length" property. Could you expand on what you're trying to do?

  • daniel_zeng842daniel_zeng842 Member Posts: 4 EDU
    My end goal is to get information about which edges are paired with each other in the laser joint feature made by lemon1324. To do that, I was planning to use the evaluate_featurescript call to try to get the information from a custom feature that stores this information. However, I wanted to do this on a smaller scale first and see how I would get the information and the format it would be in. Thus I made the above FeatureScript.

    For skribfunc, I wasn't too sure if there was a difference between a "feature" and a "function" based on https://onshape-public.github.io/docs/featureaccess/#custom-features. I was just trying random things to try to get the function found error fixed.

    If you have any tips for how to approach this, it would be very helpful.
  • caden_armstrong2caden_armstrong2 Member, User Group Leader Posts: 127 ✭✭✭
    Getting information from a featurescript is a bit difficult if you can't edit code (unless its in the feature inputs)
    Most features don't actually store anything, they just modify the model. If you wrote the FS yourself, you could use "setAttribute" to store data, but the laser joint feature doesn't do that. (You could make your own version of it that does this, given that the code is public). You can then use the evaluate_featurescript (or just another featurescript) to get the attributes and thus the information you want.

    alternatively you could use "lastModifyingOperationId" on every individual edge in the model, and then find the ones that have an operation id that matches the laser joint (you'd have to set the rollback bar to just after the laser joint feature). But I'm not sure if you'd then be able to find the pairs, it would jsut tell you what has been modified.

  • daniel_zeng842daniel_zeng842 Member Posts: 4 EDU
    edited May 2021
    Yeah, I think my plan is to make a copy of the newest version of it and call evaluate_featurescript. For the API call for this, would the script be something like this:
    "script" : "function (context is Context, queries is map) { return getVariable(context, \"laser\"); }"
    if I have the following in the FeatureScript: setVariable(context, "laser", definition.length);

    I'm not too sure the second idea would work because the laser joint would modify 4 edges in total (given one tab and one base) and there would be no way to easily distinguish which ones would correspond to each other. Thank you for the idea though.

  • caden_armstrong2caden_armstrong2 Member, User Group Leader Posts: 127 ✭✭✭
    Answer ✓
    My suggestion is to make another featurescript in another tab that has no access to your modified laser joint function.
    In there create a test function to see if you can get the result you want to eventually get through the API (in this case a "getVariable")
    Once you know your FS code works, then move it into the API call. Debugging FS code in an API application is much more difficult than working out the kinks inside of Onshape. In onshape you can do helpful things like addDebugEntities to check if you're doing it correctly.
Sign In or Register to comment.