Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- 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_Kempen
Member Posts: 248 EDU
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.
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.
To import these functions into your own code, simply paste in the following line at the top of your Feature Studio:
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:
Tagged:
2
Comments