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.

unexpected selection behavior when filleting the results of a self-repeating custom feature

kyle_altendorfkyle_altendorf Member Posts: 72
Peek 2025-08-28 13-04.gif

i'm not quite sure what to make of this. the lower-right hole is the one based on the first item i selected for my custom feature and that seems to be relevant here, and in other cases where i ran into this with the same feature.

clicking the edge of the first instance selects both instances. deselecting the first does not deselect the second. while both are selected from the single click, it shows a single entity selected in the list. etc… i assume i've written a bug in my script, but i wouldn't have expected to be able to do this and i'm not sure what i'm doing wrong with the ids.

thanks for any thoughts you have on what i've got wrong here. There was an error displaying this embed.

Comments

  • MichaelPascoeMichaelPascoe Member Posts: 2,587 PRO

    Please share a link to your custom feature so we can have a look at the code.


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
  • kyle_altendorfkyle_altendorf Member Posts: 72

    huh, looks like my link got turned into a yellow circle exclamation mark for some reason. let's try this again. sorry.

    There was an error displaying this embed.

  • kyle_altendorfkyle_altendorf Member Posts: 72

    https://cad.onshape.com/documents/cdf6376c4598c490bca1f966/v/4c18c76026d6a3c0462474ec/e/0191915c73a5234fa1a2ca1b?renderMode=0&uiState=68b1c91362d5f2280f7e4da4

  • kyle_altendorfkyle_altendorf Member Posts: 72

    hopefully a *bump* here isn't too rude given the technical issues with posting the link.

  • MichaelPascoeMichaelPascoe Member Posts: 2,587 PRO
    edited September 8

    The entity ID's are getting confused. Looks like your loop is a little strange. Try to use just a standard for-loop and do not use in-line functions for starting out in FeatureScript:

    Here is an example of a standard for-loop:

    const evLocations = evaluateQuery(context, definition.locations);
    const qty = size(evLocations);
    
    for (var i = 0; i < qty; i += 1)
    {
        const thisVertex = evLocations[i];
        const location = evVertexPoint(context, {
                    "vertex" : thisVertex
                });
    
        const start = evPlane(context, { "face" : definition.startPlane });
        const nut = evPlane(context, { "face" : definition.nutPlane });
        var directionEntity = definition.startPlane;
    
        const sketch1 = newSketchOnPlane(context, id + i + "sketch1", {
                    "sketchPlane" : start, });
    
        skCircle(sketch1, "circle1", {
                    "center" : vector(0, 0) * meter,
                    "radius" : definition.counterboreDiameter / 2,
                });
        skSolve(sketch1);
    }
    

    .

    If this is giving you trouble, I recommend following the FeatureScript video tutorials in order, this way you learn all the concepts properly:

    FeatureScript Fundamentals by Onshape

    FeatureScript Video Tutorials by CADSharp

    .


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
  • kyle_altendorfkyle_altendorf Member Posts: 72

    thanks for the suggestion. i know i have a couple other inline functions i should reconsider, but i had thought that forEachEntity() was the proper way to get the looping ids handled well relative to the original entities i was building from. to confirm, forEachEntity() should not be my default pattern for this? it seems like a simple loop with i as the id modifier would be somewhat non-unique in the sense that created features will have ids based on simply the selection order and not the original entity they are built from. this could result in downstream references pointing at the wrong entities if the selection passed to my feature is changed, when instead it might be better for the downstream references to break than to reference the wrong entity.

    thanks for the video links, i'll take a look.

  • kyle_altendorfkyle_altendorf Member Posts: 72
            for (var i = 0; i < evaluateQueryCount(context, definition.locations); i += 1)
            {
                const entity = qNthElement(definition.locations, i);
                const id = id + i;
    

    this was a slightly different route than your exact code, but the same idea and it does indeed avoid the issue. i guess i'll print out the ids in both cases and try to better understand how my use of forEachEntity() is wrong. then maybe figure out how that doesn't trigger the id reuse error either. thanks again.

  • kyle_altendorfkyle_altendorf Member Posts: 72

    forEachEntity() is ok if i disable my cleanup code at the end. i knew my cleanup was a hack.

  • MichaelPascoeMichaelPascoe Member Posts: 2,587 PRO

    There are a variety of neat ways to loop. For whatever reason, I have found the most success using the old school way.


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
  • kyle_altendorfkyle_altendorf Member Posts: 72

    i still haven't quite figured out the underlying issue, but moving my cleanup outside of the forEachEntity() call and using the code below seems to both have the effect i want of not showing the sketches as well as avoiding the shown selection trouble.

            const toCleanup = evaluateQuery(context, qCreatedBy(id, EntityType.BODY))
                ->filter(function(body) {
                    return evOwnerSketchPlane(context, {"entity": body}) != undefined;
                })
                ->qUnion();
            if (evaluateQueryCount(context, toCleanup) > 0) {
                opDeleteBodies(context, id + "cleanup", {
                    "entities" : toCleanup,
                });
            }
    

    i'll clean up my code and post a new working version.

  • kyle_altendorfkyle_altendorf Member Posts: 72

    https://cad.onshape.com/documents/cdf6376c4598c490bca1f966/v/f0e9920ccf50036003da5f7e/e/0191915c73a5234fa1a2ca1b?renderMode=0&uiState=68beffa2f38c870f00ec4c8d

    i simplified the cleanup down to a single call to this at the very end of the feature function and outside of the forEachEntity().

    opDeleteBodies(context, id + "cleanup", {
        "entities" : qSketchFilter(qCreatedBy(id), SketchObject.YES),
    });
    

    thanks for getting me started down a useful debugging path.

  • MichaelPascoeMichaelPascoe Member Posts: 2,587 PRO

    Sure thing! Welcome to FeatureScript 😎


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
Sign In or Register to comment.