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.

Repeat the function's action for each selection of the UI

ben_partouchben_partouch Member, csevp Posts: 156 PRO
edited February 18 in FeatureScript

I have a simple function to split face using a text. I want to be able to select multiple faces and have the function executed for all of them.

How do I go about it?

Thank you

    // Define the function's action


    var center = evPlane(context, {
            "face" : definition.faceToSplit
        });


    debug(context, center, DebugColor.RED);



    var sketch2 = newSketchOnPlane(context, id + "sketch2", {
            "sketchPlane" : center
        });


    var text = skText(sketch2, "text1", {
            "text" : definition.text,
            "fontName" : "Poppins-Regular.ttf",
            "firstCorner" : vector(0, 0) * inch,
            "secondCorner" : vector((1 * inch), definition.fontSize)
        });

    skSolve(sketch2);

    var textEdges = qSketchRegion(id + "sketch2");
    var loop = qLoopEdges(textEdges);

    debug(context, loop, DebugColor.GREEN);



    opSplitFace(context, id + "splitFace1", {
                "faceTargets" : definition.faceToSplit,
                "edgeTools" : loop

            });



    opDeleteBodies(context, id + "deleteBodies2", {
                "entities" : qCreatedBy(id + "sketch2", EntityType.EDGE)
            });

Comments

  • Jed_YeiserJed_Yeiser Member Posts: 47 PRO
    edited February 18

    I'd recommend looking into using a for loop here - though you may run into challenges for any non-planar face as your code requires a planar face to create a plane which you then sketch on. I'd imagine that WHERE you want to split each face is a fairly specific location and that simply solving for 'a plane' on that face does not get you the resolution you're looking for. I'd recommend something similar to what follows:

    1. Create a Mate connector somewhere (the origin? why not. Doing so makes things easy).
    2. Figure out how many faces you have
    3. For each face:
      1. create a mate connector on the face at the location you'd like to move the sketch edges to
      2. (copy?) transform the of the reference sketch to the new location defined by the mate connector.
      3. split the face as you've done.

    Cheers,

    -j

  • jelte_steur814jelte_steur814 Member Posts: 403 PRO

    try putting the above in a 'forEachEntity'

    https://cad.onshape.com/documents/37fa98fdc1a0b9ce78ff38bb/w/257085d868fb32e6998b6a89/e/11361fdeab998371aea29978

  • ben_partouchben_partouch Member, csevp Posts: 156 PRO

    Thank you @jelte_steur814 , @Jed_Yeiser

    I tried this but it didn't work: (It worked for the first MC selected but not the next ones)

        forEachEntity(context, id + "operation", definition.locations, function(entity is Query, id is Id)
            {
                // perform operations with the entity
    
                var center = evPlane(context, {
                        "face" : definition.locations
                    });
    
    
                debug(context, center, DebugColor.RED);
    
    
                var sketch1 = newSketchOnPlane(context, id + "sketch1", {
                        "sketchPlane" : center
                    });
    
    
                var index = skCircle(sketch1, "circle1", {
                        "center" : vector(0, -26) * millimeter,
                        "radius" : 0.25 * inch
                    });
    
                skSolve(sketch1);
    
                var markerEdges = qSketchRegion(id + "sketch1");
                var loop = qLoopEdges(markerEdges);
    
                debug(context, loop, DebugColor.GREEN);
    
    
    
                opSplitFace(context, id + "splitFace1", {
                            "faceTargets" : definition.faceToSplit,
                            "edgeTools" : loop
                        });
    
                opDeleteBodies(context, id + "deleteBodies2", {
                            "entities" : qCreatedBy(id + "sketch1", EntityType.EDGE)
                        });
    
            });
    
  • jelte_steur814jelte_steur814 Member Posts: 403 PRO
    edited March 3

    forEachEntity applies the function to each instance of definition.locations by passing one of those instances into the 'entity' in the function.

    you could rename 'entity' to 'location' for clarity's sake and understand that your definition.locations in the var center = evPlane(…
    should be replaced by 'entity'/'location' so it will do one at a time.

    BTW. I've found that although AI doesn't understand Featurescript well, it does understand basic programming better than I do (because it's seen a lot more code) and can help out with questions like these that are not very onshape/featurescript specific, but more programming logic.

  • ben_partouchben_partouch Member, csevp Posts: 156 PRO

    Thanks a lot @jelte_steur814 .

    Used both your tips. ChatGPT got the first time.

Sign In or Register to comment.