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.

Options

SetVeriable: how to pass value to the context with boxes or builtins?

max_borshmax_borsh Member, Developers Posts: 14
Hi Everybody.

I wanted to implement some logic for my custom feature. First time when I call this one, I have some points 
to identify edges. At this time I set my variable (array of edges) into context for subsequent operations.
So, I shall work with this array the second and subsequent times.

Supposing, I have this wrap of code:
//array for edges
var arrEdges = [];
//try to grap variable from context (if it's the first call of this function)
var defaultFilletEdges = getVariable(context, "filletEdges");
if(defaultFilletEdges is undefined) //default values for undefined state
{
    println("Variable was undefined");    
    defaultFilletEdges = qContainsPoint(getVariable(context,"Cut_Extrude1"), vector(9.0441480304E-16, -7.38510731150965, -5.88772) * inch);
    arrEdges = evaluateQuery(context, defaultFilletEdges);
    //set edges into out variable for them
    //after that we shall work with this edges instead of default points
    setVariable(context, "filletEdges", arrEdges);
}
else
{
    println("Variable found.");
    arrEdges = getVariable(context, "filletEdges");    
}<br>

But I have some error here:


Is there any way to set array of edges into context for reusability?

Comments

  • Options
    kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    An evaluated query gives an array of "transient queries" — So named because we only expect them to be valid references if used immediately. You could easily make a change to the context (via operations or intermediate features) such that your transient queries now point to the completely wrong thing, and they'll do so in a way that you can't really predict.

    Queries (the standard, non-evaluated kind) are the answer to this problem. They allow you to say which things you want regardless of the context. So we suggest doing e.g. setVariable(context, "filletEdges", defaultFilletEdges); instead. That particular query is based on a qContainsPoint, so there may still be a robustness problem even with non-transient query. If you find that won't return the right thing downstream, you may have to change the query to specify some more robust criteria (with more context we'd be happy to point you in the right direction).

    ...
    ....
    Sidebar: These rules were made by mere mortals like you. It is possible that, in some context, there's a legitimate reason to allow a transient Query stored in the context. If so, we're open to altering these rules to allow that legitimate need.

  • Options
    max_borshmax_borsh Member, Developers Posts: 14
    kevin_o_toole_1, thank you for each detailed response that you gave!

    I have already understood how to pass this thing into context.
    I went the same way you described. (setVariable(context, "filletEdges", defaultFilletEdges); instead).

    But now I stuck on another situation and have one more question to you..
    Supposing I found this edge that contains specific point and set it into the context.

    Why I did it? I just want to point specific edge of my Part1 and after changes
    some features 
    (in this case, way with qConteinsPoint is already wrong) to have the context with my array of edges. 

    If I had this array I would able to run the else block of operators and to use my edges instead of case of point definition etc.

    But there is a problem. If I make some changes in my features, build() function (of Part Studio) is recalled and created the new 
    Context. So I lose all data from context of previous session. Is it a right suggestion?

    I tried to keep this data in another context from another Part Studio (let it has PS2 name. why not?).

    Look at this piece of code:
    if (getVariable(PS2::build(), "smthData") is undefined)
    {
        println("Data undefined. Try to set it.");
                
        setVariable(PS2::build(), "smthData", 0);
    }
    else
    {
        print("Data was found! Its: ");
        println(getVariable(PS2::build(), "smthData"));
    }

    But I discovered I could get context from PS2 via(only) build()-function that create NEW context each time too.

    So is there any way to save some data between session of model's rendering? The way, where I have possibility to get edge via the point and use some kind of its reference? 
Sign In or Register to comment.