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.

FeatureScript Editing Logic: Array Indices Not Updating Properly

mko_scmko_sc Member Posts: 27 PRO

Objectives:

I am working on a FeatureScript called "Base Array Numbering", which assigns an incremental index to each element of an array. The indices should always start from 1 and update automatically whenever the user adds or removes elements.

The feature uses:

  • Editing Logic Function (showLogic) to dynamically recalculate indices.
  • A for loop to iterate over the array and assign the arrayPointIndex value.

My goal is to ensure:

  1. At the first execution, indices are correctly set as 1, 2, 3, ....
  2. When the user adds new elements, the indices continue sequentially.
  3. If elements are removed, the remaining items are renumbered starting from 1.

The Problem:

Currently, the code works only on the first launch. When I reopen the feature or add new elements:

  1. Some items receive incorrect indices (often 2 is repeated).
  2. The numbering logic does not update correctly in the user interface.

Debug Output:
When printing values inside the showLogic function, I can see that indices are being recalculated. However, the interface does not reflect the correct values.

Question for the Community:

Why are the indices not updating properly in the user interface?
Is there something I am missing in my use of Editing Logic and for loops?
How can I ensure that the array indices are reassigned consistently when the feature is reopened or modified?

Any insights, corrections, or suggestions would be greatly appreciated. Thank you in advance for your help! 😊

https://cad.onshape.com/documents/ef57e2c5563e31fd6ab509b6/w/145b578f3abf4613a6c46697/e/0829cf8f973dd60a7caaa61e

Best Answers

  • Caden_ArmstrongCaden_Armstrong Member Posts: 195 PRO
    Answer ✓

    I suggest reading the documentation on editing logic. Some useful stuff in there.

    https://cad.onshape.com/FsDoc/uispec.html

    if the "isCreating" parameter is omitted, then the editing logic function only runs during creation.

    www.smartbenchsoftware.com --- fs.place --- Renaissance
    Custom FeatureScript and Onshape Integrated Applications
  • mko_scmko_sc Member Posts: 27 PRO
    Answer ✓

    Yes, Oliver, that’s exactly the goal!
    Let me clarify further:

    Expected Behavior:

    1. Each item in the array should have a sequential index starting from 1.
    2. When an item is removed, the remaining items should be renumbered, maintaining the sequence.
    3. When a new item is added, it should receive the next available index, keeping the sequence intact.

    For example:

    Item A: Index 1
    Item B: Index 2
    Item C: Index 3

    ______

    After removing Item B:

    Item A: Index 1
    Item C: Index 2

    ________

    After adding Item D:

    Item A: Index 1
    Item C: Index 2
    Item D: Index 3

    Current Issue:

    • The showLogic function recalculates indices correctly only on the first execution.
    • If I reopen the feature or modify the array, the indices don’t update correctly. Sometimes, the indices repeat or don’t align with the expected sequence.

    Do you have any suggestions on how I can reliably renumber the indices whenever the feature is reopened or modified?

    Thank you for taking the time to look into this! 😊

Answers

  • Oliver_CouchOliver_Couch Member Posts: 226 PRO

    To clarify, your goal is?

    • Item a: Index 1
    • Item B: Index 2 ← Delete
    • Item C: Index 3
    • Item D: Index 4

    Becomes

    • Item a: Index 1
    • Item C: Index 2
    • Item D: Index 3
    • Item E: Index 4 ← Add

    Correct?

  • Caden_ArmstrongCaden_Armstrong Member Posts: 195 PRO
    Answer ✓

    I suggest reading the documentation on editing logic. Some useful stuff in there.

    https://cad.onshape.com/FsDoc/uispec.html

    if the "isCreating" parameter is omitted, then the editing logic function only runs during creation.

    www.smartbenchsoftware.com --- fs.place --- Renaissance
    Custom FeatureScript and Onshape Integrated Applications
  • mko_scmko_sc Member Posts: 27 PRO
    Answer ✓

    Yes, Oliver, that’s exactly the goal!
    Let me clarify further:

    Expected Behavior:

    1. Each item in the array should have a sequential index starting from 1.
    2. When an item is removed, the remaining items should be renumbered, maintaining the sequence.
    3. When a new item is added, it should receive the next available index, keeping the sequence intact.

    For example:

    Item A: Index 1
    Item B: Index 2
    Item C: Index 3

    ______

    After removing Item B:

    Item A: Index 1
    Item C: Index 2

    ________

    After adding Item D:

    Item A: Index 1
    Item C: Index 2
    Item D: Index 3

    Current Issue:

    • The showLogic function recalculates indices correctly only on the first execution.
    • If I reopen the feature or modify the array, the indices don’t update correctly. Sometimes, the indices repeat or don’t align with the expected sequence.

    Do you have any suggestions on how I can reliably renumber the indices whenever the feature is reopened or modified?

    Thank you for taking the time to look into this! 😊

  • mko_scmko_sc Member Posts: 27 PRO
    edited December 18

    it's the parameter "isCreating is boolean"
    Thanks to allI 🙏
    leave code for newbies.

    "Basic structure for array numbering"

    import(path : "onshape/std/geometry.fs", version : "1511.0");
    
    annotation { "Feature Type Name" : "Base Array Numbering", "Editing Logic Function" : "showLogic" }
    export const baseArrayNumbering = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Select Items", "Item name" : "Item", "Item label template" : "Index: #arrayPointIndex" }
            definition.myVariables is array;
            for (var variable in definition.myVariables)
            {
                annotation { "Name" : "Array Point Index", "UIHint" : UIHint.ALWAYS_HIDDEN }
                isInteger(variable.arrayPointIndex, POSITIVE_COUNT_BOUNDS);
            }
        }
        {
        println("My element definition" ~ definition);
        });
    
    export function showLogic(context is Context, id is Id, oldDefinition is map, definition is map,isCreating is boolean) returns map
    {
        println("Number of items counted in showLogic: " ~ size(definition.myVariables));
        for (var i = 0; i < size(definition.myVariables); i += 1)
        {
            definition.myVariables[i].arrayPointIndex = i + 1;
            println("Inside function, currentIndex is: " ~ definition.myVariables[i].arrayPointIndex);
        }
        return definition;
    }
    

Sign In or Register to comment.