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.

Options

Can someone help me out?

Dylan_StewartDylan_Stewart Member, Developers Posts: 107 PRO
Hello everyone,

I am trying to make a very simple hole feature that is driven off of FS sketches.
I am trying to eliminate all mouse clicks.

It seems very simple, I'm just missing something small. 

This is the intended outcome of what I want the code to do.


and here is the code that I have so far.
FeatureScript 392; import(path : "onshape/std/geometry.fs", version : "392.0"); annotation { "Feature Type Name" : "Hole Cut Out" } export const myFeature = defineFeature(function(context is Context, id is Id, definition is map) precondition { annotation { "Name" : "Width" } isLength(definition.width, LENGTH_BOUNDS); annotation { "Name" : "Height" } isLength(definition.height, LENGTH_BOUNDS); annotation { "Name" : "Material Thickness" } isLength(definition.thickness, LENGTH_BOUNDS); } { var width = definition.width; var height = definition.height; var holeX = width / 2; var holeY = height / 2; var squareVerticies = [ vector(0 * inch, 0 * inch), vector(0 * inch, height), vector(width, height), vector(width, 0 * inch)]; var holeVerticies = [ vector(holeX, holeY)]; var sketch1 = newSketch(context, id + "sketch1", { "sketchPlane" : qCreatedBy(makeId("Top"), EntityType.FACE) }); skLineSegment(sketch1, "line1", { "start" : squareVerticies[0], "end" : squareVerticies[1] }); skLineSegment(sketch1, "line2", { "start" : squareVerticies[1], "end" : squareVerticies[2] }); skLineSegment(sketch1, "line3", { "start" : squareVerticies[2], "end" : squareVerticies[3] }); skLineSegment(sketch1, "line4", { "start" : squareVerticies[3], "end" : squareVerticies[0] }); skSolve(sketch1); var holeSketch = newSketch(context, id + "Hole", { "sketchPlane" : qCreatedBy(makeId("Top"), EntityType.FACE) }); skCircle(holeSketch, "circle1", { "center" : holeVerticies[0], "radius" : 1 * inch }); skSolve(holeSketch); extrude(context, id + "extrude1", { "entities" : qSketchRegion(id + "sketch1"), "endBound" : BoundingType.SYMMETRIC, "depth" : definition.thickness }); extrude(context, id + "HoleExtrude", { "entities" : qSketchRegion(id + "holeSketch"), "endBound" : BoundingType.THROUGH_ALL, "bodyType" : ToolBodyType.SURFACE, "hasSecondDirection" : true, "secondDirectionBound" : SecondDirectionBoundingType.THROUGH_ALL }); opBoolean(context, id + "boolean1", { "tools" : qCreatedBy(id + "HoleExtrude", EntityType.FACE), "operationType" : BooleanOperationType.SUBTRACTION }); });
Thanks! 
Digital Engineering

Best Answer

  • Options
    mahirmahir Member, Developers Posts: 1,291 ✭✭✭✭✭
    Answer ✓
    Fixed that for ya. You can use a skRectangle. Also, the hole extrude was calling out the wrong sketch name. I added an opDeleteBodies to get rid of the sketches.

    </code></pre><pre class="CodeBlock"><code><pre class="CodeBlock"><code>FeatureScript 392;
    import(path : "onshape/std/geometry.fs", version : "392.0");
    
    annotation { "Feature Type Name" : "Hole Cut Out" }
    export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Width" }
            isLength(definition.width, LENGTH_BOUNDS);
    
            annotation { "Name" : "Height" }
            isLength(definition.height, LENGTH_BOUNDS);
    
            annotation { "Name" : "Material Thickness" }
            isLength(definition.thickness, LENGTH_BOUNDS);
    
    
        }
        {
            var width = definition.width;
            var height = definition.height;
            var holeX = width / 2;
            var holeY = height / 2;
            var squareVertices = [
                vector(0 * inch, 0 * inch),
                vector(width, height)];
            var holeVertices = [
                vector(holeX, holeY)];
    
            var sketch1 = newSketch(context, id + "sketch1", {
                    "sketchPlane" : qCreatedBy(makeId("Top"), EntityType.FACE)
                });
            
            skRectangle(sketch1, "rectangle1", {
            "firstCorner" : squareVertices[0],
            "secondCorner" : squareVertices[1]
            });
            
            skSolve(sketch1);
            
            var holeSketch = newSketch(context, id + "holeSketch", {
                    "sketchPlane" : qCreatedBy(makeId("Top"), EntityType.FACE)
                });
    
            skCircle(holeSketch, "circle1", {
                        "center" : holeVertices[0],
                        "radius" : 1 * inch
                    });
            skSolve(holeSketch);
    
            extrude(context, id + "extrude1", {
                        "entities" : qSketchRegion(id + "sketch1"),
                        "endBound" : BoundingType.SYMMETRIC,
                        "depth" : definition.thickness
                    });
    
            extrude(context, id + "HoleExtrude", {
                        "entities" : qSketchRegion(id + "holeSketch"),
                        "endBound" : BoundingType.THROUGH_ALL,
                        "operationType" : NewBodyOperationType.REMOVE,
                        "hasSecondDirection" : true,
                        "secondDirectionBound" : SecondDirectionBoundingType.THROUGH_ALL
                    });
                    
            opDeleteBodies(context, id + "deleteBodies1", {
                    "entities" : qUnion([qCreatedBy(id + "sketch1"), qCreatedBy(id + "holeSketch")])
            });
    
    
        });

Answers

  • Options
    mahirmahir Member, Developers Posts: 1,291 ✭✭✭✭✭
    edited August 2016
    I haven't tried your code, but it looks good except for a couple small details. For the hole extrusion, the body type should probably be ToolBodyType.SOLID. Actually, you can leave that out as SOLID is the default. Also, an extrude command automatically performs a boolean operation. You just need to tell it what kind via the "operationType" property (NewBodyOperationType.NEW, ADD, REMOVE,INTERSECT). In your case you'd select REMOVE. So, no need for a separate opBoolean.

    You should check out the help file for the extrude command. It goes into more detail.
  • Options
    Dylan_StewartDylan_Stewart Member, Developers Posts: 107 PRO
    @mahir  Thank you for the help!! But unfortunately it's still is not working.

    I'll look at the help file and see what I can come up with....

    Seems like it's something really small and I'm just overlooking it. 
    Digital Engineering
  • Options
    mahirmahir Member, Developers Posts: 1,291 ✭✭✭✭✭
    Answer ✓
    Fixed that for ya. You can use a skRectangle. Also, the hole extrude was calling out the wrong sketch name. I added an opDeleteBodies to get rid of the sketches.

    </code></pre><pre class="CodeBlock"><code><pre class="CodeBlock"><code>FeatureScript 392;
    import(path : "onshape/std/geometry.fs", version : "392.0");
    
    annotation { "Feature Type Name" : "Hole Cut Out" }
    export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Width" }
            isLength(definition.width, LENGTH_BOUNDS);
    
            annotation { "Name" : "Height" }
            isLength(definition.height, LENGTH_BOUNDS);
    
            annotation { "Name" : "Material Thickness" }
            isLength(definition.thickness, LENGTH_BOUNDS);
    
    
        }
        {
            var width = definition.width;
            var height = definition.height;
            var holeX = width / 2;
            var holeY = height / 2;
            var squareVertices = [
                vector(0 * inch, 0 * inch),
                vector(width, height)];
            var holeVertices = [
                vector(holeX, holeY)];
    
            var sketch1 = newSketch(context, id + "sketch1", {
                    "sketchPlane" : qCreatedBy(makeId("Top"), EntityType.FACE)
                });
            
            skRectangle(sketch1, "rectangle1", {
            "firstCorner" : squareVertices[0],
            "secondCorner" : squareVertices[1]
            });
            
            skSolve(sketch1);
            
            var holeSketch = newSketch(context, id + "holeSketch", {
                    "sketchPlane" : qCreatedBy(makeId("Top"), EntityType.FACE)
                });
    
            skCircle(holeSketch, "circle1", {
                        "center" : holeVertices[0],
                        "radius" : 1 * inch
                    });
            skSolve(holeSketch);
    
            extrude(context, id + "extrude1", {
                        "entities" : qSketchRegion(id + "sketch1"),
                        "endBound" : BoundingType.SYMMETRIC,
                        "depth" : definition.thickness
                    });
    
            extrude(context, id + "HoleExtrude", {
                        "entities" : qSketchRegion(id + "holeSketch"),
                        "endBound" : BoundingType.THROUGH_ALL,
                        "operationType" : NewBodyOperationType.REMOVE,
                        "hasSecondDirection" : true,
                        "secondDirectionBound" : SecondDirectionBoundingType.THROUGH_ALL
                    });
                    
            opDeleteBodies(context, id + "deleteBodies1", {
                    "entities" : qUnion([qCreatedBy(id + "sketch1"), qCreatedBy(id + "holeSketch")])
            });
    
    
        });

  • Options
    Dylan_StewartDylan_Stewart Member, Developers Posts: 107 PRO
    Thank you @mahir this works great!!


    Digital Engineering
  • Options
    Jonathan_HutchinsonJonathan_Hutchinson Member Posts: 64 PRO
    Is BoundingType.SYMMETRIC supposed to no longer be available in the enum?
Sign In or Register to comment.