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.

retrieve setVariable

mko_scmko_sc Member Posts: 24 PRO
After creating variables with:
import(path : "onshape/std/geometry.fs", version : "1472.0");
const mm = millimeter;

annotation { "Feature Type Name" : "write" }
export const write_Var = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
 
    }
    {
        setVariable(context, "VarCode1"      , 1 * mm);
        setVariable(context, "VarCode2"      , 2 * mm);
        setVariable(context, "VarCode3"      , 3 * mm);
        setVariable(context, "VarCode4"      , "cip4");
        setVariable(context, "VarCode5"      , "ciop5");

    });
How can I "retrieve" them for another document so that they are usable with #VarCode1read ?

import(path : "onshape/std/geometry.fs", version : "1472.0");
import(path : "5799c11b612193edf562abdc", version : "cf616b0c1992daddb8a4e7a3");


annotation { "Feature Type Name" : "read" }
export const read_Var = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
    }
    {
       var VarCode1read = try (getVariable(context,"VarCode1")); 
    });
after geometry i imported "wire".



Best Answer

  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    edited March 2021 Answer ✓
    In this document you can find both approaches. In feature studio 1 the const is decleared, in feature studio 2 fs1 is imported and variables from fs1 are used in the feature body where they are set to context variables, this feature is called in part studio 1, so that variables are now in its context. In fs3 part studio 1 is imported and context variable is extracted. This feature prints this variable value in the console of part studio 2.

Answers

  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    If you importing feature studio then for being avaliable after import variables should be defined on top level, outside of feature body like this:
    (note that i packed them into map for better management)

    export const variables = {
    "varCode1":varValue1,
    "varCode2": varValue2,
    ...
    };
    after you imported features studio this constant variables should be avaliable, and you can either iterate in the loop of for-in map to extract key-value pairs, or work with it directly if you know the keys.

    If you importing part studio, where the feature assigning context variables was used, you need to import to namespace (for example i named it "variables") like that:
    variables::import(path : "5799c11b612193edf562abdc", version : "cf616b0c1992daddb8a4e7a3");
    then call a build function of that namespace, which return the context, and get variable from those context:
    var var1 = getVariable(variables::build(), "varCode1");
  • mko_scmko_sc Member Posts: 24 PRO
    Thank you so much konstantin,.

    for "write" everything is clear,
    I did:
    import(path : "onshape/std/geometry.fs", version : "1472.0");
    export const variables  = 
    {
    "varfromCode1": 1 * mm,
    "varfromCode2": 2 * mm,
    "varfromCode3": 3 * mm,
    "VarfromCode4" : "cip4",
    };
    
    const mm = millimeter;
    annotation { "Feature Type Name" : "write" }
    export const write_Var = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
        }
        {
           setVariable(context, "VarCode1" , (getVariable(context,"VarfromCode1")));
           setVariable(context, "VarCode2" , (getVariable(context,"VarfromCode2")));<br>...
    
    

    but for the "read" it doesn't work ("build with 0 argument")
    import(path : "onshape/std/geometry.fs", version : "1472.0");
    variables::import(path : "5799c11b612193edf562abdc", version : "7f49b3a4002432e48b191dfb");
    
    annotation { "Feature Type Name" : "read" }
    export const read_Var = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
        }
        {
           var var1 = getVariable(variables::build(), "VarCode1");//!!!!!!!!!!!!<br>
        });
    
    
    Thanks again
    ..
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    If you trying to share data between 2 feature studios, then you just import feature studio with variables constant definition and it instantly avaliable for usage in the code, no need to define context variables, just make sure you prepended top level definition with export.
    The only reason to set and get context variables is if you want to pass through part studio context, so the schema bill be like this:
    1. in feature studio 1 in the feature function body you set some context variables with setVariable(context, varName, varValue)
    2. You use feature from feature studio in part studio 1, now variables are added to its context.
    3. Import part studio 1 in feture studio 2 into the namespace, call build function of that namespace (without arguments in simpliest case) and it will return context of part studio 1, which contains variables decleared in feature studio 1. Calling getVariable(variables::build(), varName) is the end of this process. This is the hard way of getting variables, which I wouldn't reccomend, but pretty usefull if need a context of foreign part studio for some another cases like getting attributes.
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    edited March 2021 Answer ✓
    In this document you can find both approaches. In feature studio 1 the const is decleared, in feature studio 2 fs1 is imported and variables from fs1 are used in the feature body where they are set to context variables, this feature is called in part studio 1, so that variables are now in its context. In fs3 part studio 1 is imported and context variable is extracted. This feature prints this variable value in the console of part studio 2.
  • mko_scmko_sc Member Posts: 24 PRO
    edited March 2021
    I do not know how to thank you!
    It was just what I was looking for ... thanks again :)
  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    mko_sc said:
    I do not know how to thank you!
    It was just what I was looking for ... thanks again :)
    I'm glad you got this figured out. I think I hadn't understood your original question on the Youtube video.
    Evan Reese
  • mko_scmko_sc Member Posts: 24 PRO
    Hi Evan
    Yes I am. As I wrote to you on YuoTube, we beginners are hungry for simple and clear examples.
    Congratulations and thank you for what you do.
Sign In or Register to comment.