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.

Feedback into UI panel

Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
Hello, is it possible to use a calculated value as a default in the UI panel and have that value displayed?
Thanks
Lee Hesketh
There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!

Best Answers

«1

Answers

  • Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
    So how would I implement it?
    There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,215
    What exactly are you trying to do?  What is the parameter and how would you want to calculate its default value?
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
    Well my value is the width of a part divided by three. I have a "width" parameter in the UI panel and would like to be able to have part_width/3 as the default value i.e. the value seen when script is first called
    There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,215
    Ok, an editing logic function can do that, assuming you can calculate part_width -- is that a variable or something computed from a preselection or just the overall size of everything in the part studio?
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
    It is a calculated value stored in a variable. It is just the width of one part which is already calculated
    There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,215
    Here's a slightly closer example of a feature that does this: https://cad.onshape.com/documents/8e50bb14e0b5602c307daafb/w/e49c36f7865e005268e46ec9/e/661a12669665a5ed18ddd2e1

    Notice that the length parameter is set to a string -- that sets the expression in the dialog.  If you want to set it to a value, you'd do something like
    getVariable(context, "part_width") / 3<br>
    instead of
    "#part_width / 3"
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
    Ah, sorry, I think I mislead you, I meant it is stored in a variable in the code i.e. var part_wide = //calculation;
    There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,215
    Ok, then you need to do that calculation also from within the editing logic function.  (You can factor that code out into its own function).
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
    I tried doing that in your example. I wrote var part_width = 2*inch; in the main body. But when I tried to do part_wide/3 in the setDefault function, it didn't recognise part_wide. 
    There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,714
    Hi @Lee_Hesketh - you need to pass your variable into your setDefault function. All variables have "scope" and there is no such thing as a "global" variable. You could also add your variable to the "definition" map which makes it easier to access in the editLogicFunction.

    e.g. definition.part_width = 2*inch;
    Senior Director, Technical Services, EMEAI
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,215
    Yeah, you can't just use a variable from the body in the editing logic function -- they're called at different times.  You have to do the same computation you do in the body in the editing logic function to get that variable -- and so you don't write the code twice, I suggested you write the computation of part_width as a function you call from both places.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
    @ilya_baran That would be ideal.
    I think it would be better if I just give you the file so I you can see how I have come to getting part_wide.
    The problem is that the other variables need to calculate part_width are also need in the rest of the script. Is there a way for the script have more arguments and each one can be extracted for use later on?
    https://cad.onshape.com/documents/580b256ade95e210af0261d8/w/1d22e17d9b07548059b7b311/e/971ebd1bc44955a2e5997b6d
    "Mortice and Tenon"
    There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!
  • Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
    Ok, that makes sense. One question though, how would I get specific data from the map? Would it just be something like initialComputations(result.intface1)
    There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!
  • Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
    *Say I wanted to use intFace1 again later in the script, how would I do that?
    There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,714
    Depends if the variable is still in scope. If not you could just call your function again, assuming the inputs are still the same.
    Senior Director, Technical Services, EMEAI
  • Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
    @NeilCooke
    So If I have all of the data in the initialComputations stored in result{}, going back to my OP, how would I display part_wide in the UI panel? Could I just use @ilya_baran's editingLogicExample code and add another parameter i.e.
    </code>export function initialComputations(context is Context, id is Id, oldDefinition is map, changedDefinition is map)
    <pre><code>{<br>&nbsp; &nbsp; var result = {};<br><br>&nbsp; &nbsp; ...<br>&nbsp; &nbsp; result.intFace1 = qNthElement(result.intFaces, 1);<br>&nbsp; &nbsp; ...<br>&nbsp; &nbsp; result.part_wide = evDistance(...);<br>&nbsp; &nbsp; ...<br>    if (oldDefinition.width== undefined) // first time
            changedDefinition.width= part_wide / 3;
        return changedDefinition;<br>&nbsp; &nbsp; return result;<br>}


    There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,714
    Without looking into it in too much depth, it looks like Ilya is suggesting that you create a function to calculate all your values, then call that function from your editLogicFunction (not do all the calcs in the editLogicFunction). Once you have the result from your called function you can assign it to definition.width inside the editLogicFunction (or whatever the definition map key is that you are using to display the width in the UI).
    Senior Director, Technical Services, EMEAI
  • Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
    I tried doing this in another document, just experimenting. Here is the code:
    FeatureScript 477;
    import(path : "onshape/std/geometry.fs", version : "477.0");
    
    annotation { "Feature Type Name" : "My Feature", "Editing Logic Function" : "setDefault" }
    export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "My Length" }
            isLength(definition.myLength, LENGTH_BOUNDS);
    
        }
        {
    
    
            var comps = initialComputations(context, id, definition);
            var intface1 = comps.intFace1;
            debug(context, intface1);
            //debug(context, intfaces);
        });
    
    function initialComputations(context, id, definition) returns map
    {
        var result = {};
        var bodies = qBodyType(qEverything(EntityType.BODY), BodyType.SOLID);
        result.intFaces = qSmallest(qOwnedByBody(bodies, EntityType.FACE));
    
        result.intFace1 = qNthElement(result.intFaces, 1);
        result.area = evArea(context, {
                        "entities" : result.intFace1
                    }) / meter;
        return result;
    }
    
    export function setDefault(context is Context, id is Id, oldDefinition is map, changedDefinition is map)
    {
        if (oldDefinition.myLength == undefined) // first time
            changedDefinition.myLength = initialComputations(context, id, definition).area;
        return changedDefinition;
    }
    But there is an error when I call initialComputations in the setDefault function. It says variable definition not found.
    What am I doing wrong?
    There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,714
    It should be oldDefinition.




    Senior Director, Technical Services, EMEAI
  • Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
    Thanks, it now shows no errors, but the correct value isn't being displayed. It is still the default 25mm
    There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!
  • Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
    Nevermind, I was being silly. I was just re-opening the feature instead of creating a new instance of it. It works! Time to implement it into my actual code.
    There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!
  • Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
    Now I have tried it in my actual code, the one linked further up, I now have an error 
    Attempt to dereference non-container undefined. Could this be a scope issue?
    There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!
  • Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
    @NeilCooke @ilya_baran
    I ran the whole code, and it sort of works. It doesn't show the calculated width, it's still the default 25mm. I tried running a new instance of it, still 25mm. I looked at the activity log and there were errors. The strange thing is that I debugged all the variables in InitialComputations in the main body and they are correct. When debugging part_wide/3, I get the correct value as shown. But the activity log shows errors when calling qEdgeAdjacent. What am I doing wrong?

    There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,714
    In qEdgeAdjacent, your query (that was a face or edge) is undefined.

    You may need to pass the query into your function and/or editLogicFunction - you could add it to the definition map and pass it that way.
    Senior Director, Technical Services, EMEAI
  • Lee_HeskethLee_Hesketh Member, Developers Posts: 148 ✭✭✭
    How would I do that?
    There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,714
    Hard to say without looking at the code. Where does the query come from? The user selection? In which case it would already be in the definition map.
    Senior Director, Technical Services, EMEAI
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,215
    In your editing logic function, you need to pass changedDefinition instead of oldDefinition to your initialComputations.  The first time (the time you care about) oldDefinition is empty -- that's why you're getting the error.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
Sign In or Register to comment.