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

Return definition.groups[g].lengthInputMethod ?

MichaelPascoeMichaelPascoe Member Posts: 1,713 PRO
edited July 2021 in FeatureScript
I'm having trouble returning a boolean definition. I can't get definition.groups[g].lengthInputMethod to return with true when the opposite direction button is pressed.

My goal is to have an opposite direction boolean switch the user interface between "group.lengthEntities" and "group.dx". The opposite direction code has to be below "group.lengthEntities" and "group.dx" code in order for the opposite direction gui to appear beside the input box.

When the opposite direction is pressed, I would like a hidden boolean "group.lengthInputReturned" to change from false to true. 

Here is the code i have so far :
https://cad.onshape.com/documents/395304920b30c3b382fd312d/w/86481637ad1e9da01b0e5d60/e/296c17e767add081ff718e6a
annotation { "Feature Type Name" : "Cut List", "UIHint" : "NO_PREVIEW_PROVIDED", "Icon" : icon::BLOB_DATA, "Editing Logic Function" : "measure" }
export const cutList = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {

annotation { "Name" : "Cut List", "Item name" : "Item", "Item label template" : "#name" } //, "UIHint" : UIHint.COLLAPSE_ARRAY_ITEMS }
        definition.groups is array;
        for (var group in definition.groups)
        {

annotation { "Name" : "lengthInputReturned", "UIHint" : UIHint.ALWAYS_HIDDEN }
                group.lengthInputReturned is boolean;

                if (group.lengthInputReturned == false) //group.lengthInputMethod == Method2.MEASURE)
                {
                    annotation { "Name" : "Length", "Filter" : EntityType.FACE || EntityType.EDGE || EntityType.VERTEX, "MaxNumberOfPicks" : 2 }
                    group.lengthEntities is Query;
                }

                if (group.lengthInputReturned == true) //group.lengthInputMethod == Method2.MANUAL)
                {
                    annotation { "Name" : "Length" }
                    isLength(group.dx, { (inch) : [-1e5, 0, 1e5] } as LengthBoundSpec);
                }

                annotation { "Name" : "Measure", "UIHint" : UIHint.OPPOSITE_DIRECTION }
                group.lengthInputMethod is boolean;
        }
    }

export function measure(context is Context, id is Id, oldDefinition is map, definition is map, isCreating is boolean) returns map
{
    for (var g = 0; g < size(definition.groups); g += 1)
    {
        //Reference functions. Thanks Alex! 
        //https://cad.onshape.com/documents/eebb66e5a6139b9b86708957/w/093d288d7231f660efc44c25/e/8466437b7dc1144ccb506b50
        // if partQuery changes, and partQuery is a solid body which does not evaluate to nothing (i.e. a solid part is selected):
        // this condition is used because we don't need to update the material unless the user changes which parts they've selected

        if (oldDefinition.groups[g].lengthInputMethod != definition.groups[g].lengthInputMethod)
        {
            if (definition.groups[g].lengthInputMethod == true)
            {
                definition.groups[g].lengthInputReturned == true;
            }

            if (definition.groups[g].lengthInputMethod == false)
            {
                definition.groups[g].lengthInputReturned == false;
            }
        }
    }

    return definition;
}




Learn more about the Gospel of Christ  ( Here )

CADSharp  -  We make custom features and integrated Onshape apps!   cadsharp.com/featurescripts 💎

Best Answers

  • Options
    kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    edited July 2021 Answer ✓
    Simple fix I think. You have
     definition.groups[g].lengthInputReturned == true;

    but you want
     definition.groups[g].lengthInputReturned = true;
  • Options
    Alex_KempenAlex_Kempen Member Posts: 244 EDU
    Answer ✓
    The usage of library functions is actually pretty straightforward. Paste the following import at the top of your feature studio:
    import(path : "4c21d0c3c89c0a81aadfdac6/636ff98c3710f0d81fcecbf8/3732a1478a38cc5723a9801f", version : "17c016200f841487aca7f892");
    You'll then be able to call arrayParamterChanges (and arrayItemChanges) from my library directly:
    if (arrayParameterChanges(oldDefinition.group, definition.group))
    {
         for (var i = 0; i < size(definition.group); i += 1)
         {
             if (oldDefinition.group[i].myParameter != definition.group[i].myParameter)<br>         {
                 definition.group[i].myParameter = computedValue;<br>         }
             ... // Perform additional operations
         }
     }
    They'll also autocomplete if you start typing them, and since they're fully documented, you'll be able to see usage explanations and examples directly inside the autocomplete menu:

    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



  • Options
    Alex_KempenAlex_Kempen Member Posts: 244 EDU
    Answer ✓
    1. arrayParameterChanges is designed to run only when something changes inside an array parameter. If you want to update all the values of your array parameter when something not in an array parameter updates, I would condition the outside parameter, then run through the array as needed:
    if (oldDefinition.myParameter != definition.myParameter)
    {
       for (var i = 0; i < size(definition.groups); i += 1)
       {
           definition.groups[g].myParameter = myValue;
       }
    }

    2. Specified parameters is an optional argument which can be added to editing logic functions:
    export function measureEditLogic(context is Context, id is Id, oldDefinition is map, definition is map, isCreating is boolean, specifiedParameters is map) returns map
    The documentation states that "specified parameters map to true", which is a complicated way of saying that specifiedParameters is like your definition, except the values of parameters are all booleans corresponding to whether a user input something for that parameter. Thus:
    // A commonly used condition for updating a merge scope, to prevent overwriting a user input value
    if (!specifiedParameter.mergeScope && oldDefinition.myQuery != definition.myQuery)
    Note, however, that this does not work for anything array parameter related.

    3. And lastly, the code:
    if (definition.groups[g].widthInputMethod == true)
    {
        definition.groups[g].widthInputReturned = true;
    }
    
    if (definition.groups[g].widthInputMethod == false)
    {
        definition.groups[g].widthInputReturned = false;
    }
    Behaves identically to the code:
    definition.groups[g].widthInputReturned = definition.groups[g].widthInputMethod;
    

    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



Answers

  • Options
    kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    edited July 2021 Answer ✓
    Simple fix I think. You have
     definition.groups[g].lengthInputReturned == true;

    but you want
     definition.groups[g].lengthInputReturned = true;
  • Options
    MichaelPascoeMichaelPascoe Member Posts: 1,713 PRO
    Well... that's embarrassing.
    Thanks Kevin!

    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   cadsharp.com/featurescripts 💎
  • Options
    Alex_KempenAlex_Kempen Member Posts: 244 EDU
    Also, you may want to consider using my custom library function arrayParamterChanges as an additional condition prior to the for loop in your editing logic order to prevent errors from things like taking the size of a non-existant array, especially since you're doing things with conditional visibility, which can be dangerous if it doesn't work properly. For example, I once accidentally trapped my friend in the wrong part of a UI by applying an update at an inopportune time. Oops!
    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



  • Options
    MichaelPascoeMichaelPascoe Member Posts: 1,713 PRO
    @Alex_Kempen
    I will try to keep this in mind. Looks a little advanced for my current knowledge.

    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   cadsharp.com/featurescripts 💎
  • Options
    Alex_KempenAlex_Kempen Member Posts: 244 EDU
    Answer ✓
    The usage of library functions is actually pretty straightforward. Paste the following import at the top of your feature studio:
    import(path : "4c21d0c3c89c0a81aadfdac6/636ff98c3710f0d81fcecbf8/3732a1478a38cc5723a9801f", version : "17c016200f841487aca7f892");
    You'll then be able to call arrayParamterChanges (and arrayItemChanges) from my library directly:
    if (arrayParameterChanges(oldDefinition.group, definition.group))
    {
         for (var i = 0; i < size(definition.group); i += 1)
         {
             if (oldDefinition.group[i].myParameter != definition.group[i].myParameter)<br>         {
                 definition.group[i].myParameter = computedValue;<br>         }
             ... // Perform additional operations
         }
     }
    They'll also autocomplete if you start typing them, and since they're fully documented, you'll be able to see usage explanations and examples directly inside the autocomplete menu:

    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



  • Options
    MichaelPascoeMichaelPascoe Member Posts: 1,713 PRO
    Legit!

    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   cadsharp.com/featurescripts 💎
  • Options
    MichaelPascoeMichaelPascoe Member Posts: 1,713 PRO
    edited July 2021
    The opposite direction booleans work great now.

    @Alex_Kempen
    I'm having some trouble getting another parameter to update. When I type a number into "Initial quantity" it does not update the array "quantity" until I change something in that group. The moment I check a box or update a number within that group, quantity updates and becomes 4.


    Also, is there a way to check if a user has not placed anything into an input? I would like the "initial" parameters to only be applied if the user has not entered any parameters. In some cases, "if (size(evaluateQuery(context, myQuery)) == 0) { }" would usually work, but not for this.


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   cadsharp.com/featurescripts 💎
  • Options
    Alex_KempenAlex_Kempen Member Posts: 244 EDU
    Answer ✓
    1. arrayParameterChanges is designed to run only when something changes inside an array parameter. If you want to update all the values of your array parameter when something not in an array parameter updates, I would condition the outside parameter, then run through the array as needed:
    if (oldDefinition.myParameter != definition.myParameter)
    {
       for (var i = 0; i < size(definition.groups); i += 1)
       {
           definition.groups[g].myParameter = myValue;
       }
    }

    2. Specified parameters is an optional argument which can be added to editing logic functions:
    export function measureEditLogic(context is Context, id is Id, oldDefinition is map, definition is map, isCreating is boolean, specifiedParameters is map) returns map
    The documentation states that "specified parameters map to true", which is a complicated way of saying that specifiedParameters is like your definition, except the values of parameters are all booleans corresponding to whether a user input something for that parameter. Thus:
    // A commonly used condition for updating a merge scope, to prevent overwriting a user input value
    if (!specifiedParameter.mergeScope && oldDefinition.myQuery != definition.myQuery)
    Note, however, that this does not work for anything array parameter related.

    3. And lastly, the code:
    if (definition.groups[g].widthInputMethod == true)
    {
        definition.groups[g].widthInputReturned = true;
    }
    
    if (definition.groups[g].widthInputMethod == false)
    {
        definition.groups[g].widthInputReturned = false;
    }
    Behaves identically to the code:
    definition.groups[g].widthInputReturned = definition.groups[g].widthInputMethod;
    

    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



  • Options
    MichaelPascoeMichaelPascoe Member Posts: 1,713 PRO
    So much knowledge! Thank you!

    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   cadsharp.com/featurescripts 💎
Sign In or Register to comment.