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.

Array Editing Logic Utility Functions

Alex_KempenAlex_Kempen Member Posts: 248 EDU
edited March 2021 in FeatureScript
Hello,
One thing I've found tricky in the past is handling array parameters inside editing logic. In particular, it's easy to make mistakes and cause your editing logic to error out when trying to figure out what parts of an array parameter have changed, since evaluating a non-existent query or taking the size of something which doesn't exist both crash your editing logic. Accordingly, I decided to spend today putting together two useful array editing logic functions, both of which can be found here

The first function is arrayParamterChanges. It can be used to detect when an internal parameter inside an array parameter has changed.
if (arrayParameterChanges(oldDefinition.myWidgets, definition.myWidgets))
{
     for (var i = 0; i < size(definition.widget); i += 1)
     {
         if (oldDefinition.widget[i].myParameter != definition.widget[i].myParameter)
         {
             definition.widget[i].myParameter = computedValue;
         }
         // Parameters associated with definition and oldDefinition can be safely evaluated
     }
}

The second function is arrayItemChanges. It can be used to detect when the number of items in an array parameter has changed, as defined by the enum ItemChangeType.
if (arrayItemChanges(oldDefinition.myWidgets, definition.myWidgets, ItemChangeType.ADDED))
{
    definition.myWidgets[size(definition.myWidgets) - 1].myParameter = computedValue;
    // Parameters associated with definition can be safely evaluated
}
Notably, I also took the time to trick out both functions with overloads, autocomplete values, and documentation, so you can look them up in your part studio, see an example use, and read what they're for whenever they autocomplete in your feature studio.

To import these functions into your own code, simply paste in the following line at the top of your Feature Studio:
import(path : "4c21d0c3c89c0a81aadfdac6/841685d36a7d6822945de892/3732a1478a38cc5723a9801f", version : "3418c85aac72cb011beda92d");
I hope you find these functions useful!
CS Student at UT Dallas
Alex.Kempen@utdallas.edu
Check out my FeatureScripts here:



Comments

  • Alex_KempenAlex_Kempen Member Posts: 248 EDU
    I've added some additional language to the documentation to make some of the behavior clearer, and I've added some internal function prototypes to prevent issues with the functions failing when a feature is first created (and oldDefinition is a map with no keys or values). I've also updated the import snippet above to point to the latest version.
    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



Sign In or Register to comment.