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.

Boolean add

guillaume_kellerguillaume_keller Member Posts: 5 EDU
Hello everyone,
I want to create a feature script to create simple joint automatically,

I want to "glue" a few parts together but I don't know how to identify the loop-created entities and how to use them in the op boolean
There is also something about the deleted sketch that I can't suppress
If someone has an answer I'll be thankfull, I'm new in the FeatureScript o:)

Here is the code :



FeatureScript 765;
import(path : "onshape/std/geometry.fs", version : "765.0");

annotation { "Feature Type Name" : "My Creneaux GOOD" }
export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {

        annotation { "Name" : "Face a couper", "Filter" : EntityType.FACE, "MaxNumberOfPicks" : 1 }
        definition.face is Query;

        annotation { "Name" : "Premier de la longueur ?" }
        definition.premLon is boolean;

        annotation { "Name" : "Epaisseur" }
        definition.ep is string;

        annotation { "Name" : "Longueur" }
        definition.long is string;

        annotation { "Name" : "Largeur" }
        definition.larg is string;

    }
    {

        var epaisseur = stringToNumber(definition.ep);
        var creneau = 3 * epaisseur;
        var longueur = stringToNumber(definition.long);
        var largeur = stringToNumber(definition.larg);

        //Premier support

        var sketch1 = newSketch(context, id + "sketch1", {
                "sketchPlane" : /*definition.face*/ qCreatedBy(makeId("Top"), EntityType.FACE)
            });
        skRectangle(sketch1, "rectangle1", {
                    "firstCorner" : vector(0, 0) * centimeter,
                    "secondCorner" : vector(longueur, largeur) * centimeter
                });
        skSolve(sketch1);

        //Deuxieme support

        var sketch2 = newSketch(context, id + "sketch2", {
                "sketchPlane" : /*definition.face*/ qCreatedBy(makeId("Top"), EntityType.FACE)
            });
        skRectangle(sketch2, "rectangle2", {
                    "firstCorner" : vector(0, 0) * centimeter,
                    "secondCorner" : vector(longueur, largeur) * centimeter
                });
        skSolve(sketch2);




        /*var sketch2 = newSketch(context, id + "sketch2", {
           "sketchPlane" : qCreatedBy(makeId("Top"), EntityType.FACE)
           });
           skRectangle(sketch2, "rectangle3", {
           "firstCorner" : vector(0, 0) * centimeter,
           "secondCorner" : vector(longueur,largeur) * centimeter
           });
           skRectangle(sketch2, "rectangle2", {
           "firstCorner" : vector(-creneau, -creneau) * centimeter,
           "secondCorner" : vector(longueur+creneau,largeur+creneau) * centimeter
           });
           skSolve(sketch2);*/


        //▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
        //Premiere partie
        //▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

        //Lignes sur X

        for (var i = 0; i < longueur / 2 / creneau; i += 1)
        {
            fCuboid(context, id + "cuboid1" + i, {
                        "corner1" : vector(i * 2 * creneau, 0, 0) * centimeter,
                        "corner2" : vector(i * 2 * creneau + creneau, epaisseur, epaisseur) * centimeter
                    });

        }

        for (var i = 0; i < longueur / 2 / creneau; i += 1)
        {
            fCuboid(context, id + "cuboid2" + i, {
                        "corner1" : vector(i * 2 * creneau, 0 + largeur - epaisseur, 0) * centimeter,
                        "corner2" : vector(i * 2 * creneau + creneau, epaisseur + largeur - epaisseur, epaisseur) * centimeter
                    });

        }


        //Lignes sur Y

        for (var i = 0; i < largeur / 2 / creneau; i += 1)
        {
            fCuboid(context, id + "cuboid3" + i, {
                        "corner1" : vector(0, i * 2 * creneau, 0) * centimeter,
                        "corner2" : vector(epaisseur, i * 2 * creneau + creneau, epaisseur) * centimeter
                    });

        }

        for (var i = 0; i < largeur / 2 / creneau; i += 1)
        {
            fCuboid(context, id + "cuboid4" + i, {
                        "corner1" : vector(0 + longueur - epaisseur, i * 2 * creneau, 0) * centimeter,
                        "corner2" : vector(epaisseur + longueur - epaisseur, i * 2 * creneau + creneau, epaisseur) * centimeter
                    });

        }

        extrude(context, id + "extrude1", {
                    "entities" : qSketchRegion(id + "sketch1"),
                    "endBound" : BoundingType.BLIND,
                    "depth" : -0.01 * centimeter,
                    "operationType" : NewBodyOperationType.ADD
                });


        extrude(context, id + "extrude2", {
                    "entities" : qSketchRegion(id + "sketch2"),
                    "endBound" : BoundingType.BLIND,
                    "depth" : (0.01 + epaisseur * 3) * centimeter,
                    "operationType" : NewBodyOperationType.NEW,
                });

        extrude(context, id + "extrude3", {
                    "entities" : qSketchRegion(id + "sketch2"),
                    "endBound" : BoundingType.BLIND,
                    "depth" : (epaisseur * 3) * centimeter,
                    "operationType" : NewBodyOperationType.NEW,
                });

        opBoolean(context, id + "boolean1", {
                    "tools" : qCreatedBy(id + "extrude3", EntityType.BODY),
                    "targets" : qCreatedBy(id + "extrude2", EntityType.BODY),
                    "operationType" : BooleanOperationType.SUBTRACTION
                });


        //▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
        //Deuxieme partie
        //▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

        for (var i = 0; i < longueur / 2 / creneau; i += 1)
        {
            fCuboid(context, id + "cuboid5" + i, {
                        "corner1" : vector(creneau + i * 2 * creneau, 0, 2 * epaisseur) * centimeter,
                        "corner2" : vector(creneau + i * 2 * creneau + creneau, epaisseur, 2 * epaisseur + epaisseur) * centimeter
                    });

            }

        /*for (var i = 0; i < longueur / 2 / creneau; i += 1)
           {
           fCuboid(context, id + "cuboid6" + i, {
           "corner1" : vector(creneau + i * 2 * creneau, 0 + largeur - epaisseur, 2 * epaisseur) * centimeter,
           "corner2" : vector(creneau + i * 2 * creneau + creneau, epaisseur + largeur - epaisseur, 2 * epaisseur + epaisseur) * centimeter
           });

           }*/


        //Lignes sur Y

        for (var i = 0; i < largeur / 2 / creneau; i += 1)
        {
            fCuboid(context, id + "cuboid7" + i, {
                        "corner1" : vector(0, creneau + i * 2 * creneau, 2 * epaisseur) * centimeter,
                        "corner2" : vector(epaisseur, creneau + i * 2 * creneau + creneau, 2 * epaisseur + epaisseur) * centimeter
                    });

        }


    });




Comments

  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646
    To answer your second question first: Sketches are not deleted/consumed when you use them.  They'll stick around to be used as many times as they're needed.  If you'd like to get rid of them you can do:
    opDeleteBodies(context, id + "deleteSketches", { "entities" : qCreatedBy(id + "sketch1", EntityType.BODY) });


    To answer your first question, you need to use qUnion.  A query for all the loop created cuboids as you have the code now would look like:
    
    qUnion([qCreatedBy(id + "cuboid1", EntityType.BODY), qCreatedBy(id + "cuboid2", EntityType.BODY), qCreatedBy(id + "cuboid3", EntityType.BODY), ... repeat this all the way to 7]);

    The reason that you don't have to put all the individual "+ i" parts for the ids is because ids are hierarchical, so a parent id also represents the operations of its children.  Using this knowledge, you could simplify your code by having your code look like:
    //////// Do all the setup
    
    for (...)
    {
        fCuboid(context, id + "cuboidGroupA" + "1" + i, ...)
    }
    for (...)
    {
        fCuboid(context, id + "cuboidGroupA" + "2" + i, ...)
    }
    
    ...
    
    /////// Do some other stuff
    
    ...
    
    for (...)
    {
        fCuboid(context, id + "cuboidGroupB" + "1" + i, ...)
    }
    for (...)
    {
        fCuboid(context, id + "cuboidGroupB" + "2" + i, ...)
    }
    
    const allCuboids = qUnion([qCreatedBy(id + "cuboidGroupA", EntityType.BODY), qCreatedBy(id + "cuboidGroupB", EntityType.BODY)]);
    
    
    More reading on id hierarchy here:
    https://cad.onshape.com/FsDoc/library.html#Id

    Another simple way to collect these entities without changing how your code currently looks would be to do this before your first for loop:
    var cuboids = qNothing();
    And then after each for loop:
    cuboids = qUnion([cuboids, qCreatedBy(id + "cuboid<index>", EntityType.BODY)]);
    With <index> replaced with the corresponding number in that for loop.



    Hope this helps!
    Jake Rosenfeld - Modeling Team
  • guillaume_kellerguillaume_keller Member Posts: 5 EDU
    Thank you so much for the tips !
    My script is now running well !

    If I can ask another little question, I want to catch the length and the width of a rectangular face, is there a function to do this ? or do I need to catch them by hand ?

    Regards,
    Guillaume
  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646
    If the face is XYZ axis-aligned, you could use evBox3d, but it's probably more stable to find the edges you care about and use evLength.
    Jake Rosenfeld - Modeling Team
  • guillaume_kellerguillaume_keller Member Posts: 5 EDU
    Thank you for the answer, I found the function and it works well
    Another little question, if you want to get a point coordinates, what is the function related to ?
  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646
    The function you are looking for is `evVertexPoint`

    All of the geometry evaluation functions are found in `evaluate.fs` and are documented here:
    https://cad.onshape.com/FsDoc/library.html#module-evaluate.fs
    Jake Rosenfeld - Modeling Team
Sign In or Register to comment.