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 Subtraction in a for loop

justin_lordjustin_lord Member Posts: 2
edited July 2021 in Community Support
I'm trying to recreate this monopole gear seen here
Three motions are invoked by the interaction between the MP-gear and   Download Scientific Diagram Anyone have any ideas for designing this monopole gear cad

I'm trying to do this by rotating the big spherical gear and the small monopole gear to mimic a gear mesh, then perform a boolean subtraction. Then repeating that process as many times as I need to get the resolution I want. 

Here is my code. I can get the parts to rotate the way I want them to in the loop, just not the boolean subtraction.

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

annotation { "Feature Type Name" : "Gear Hobbing" }
export const gearHob = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {

        annotation { "Name" : "Gear Cutter", "Filter" : EntityType.BODY && BodyType.SOLID, "MaxNumberOfPicks" : 1 }
        definition.cuttingTool is Query;

        annotation { "Name" : "Gear to Cut", "Filter" : EntityType.BODY && BodyType.SOLID, "MaxNumberOfPicks" : 1 }
        definition.gearBlank is Query;

        annotation { "Name" : "Cutting Axis", "Filter" : EntityType.EDGE, "MaxNumberOfPicks" : 1 }
        definition.cuttingAxis is Query;

        annotation { "Name" : "Secondary Axis", "Filter" : EntityType.EDGE, "MaxNumberOfPicks" : 1 }
        definition.gearAxis is Query;

    }
    {
        var count = 10;
        const gearCuttingAxis = line(vector(0, 0, 0) * inch, vector(0, 0, 1));
        const toCutAxis = line(vector(0, -2.25, 0) * inch, vector(0, 0, 1));

        //const i = 1;

        //Rotating the gear cutter
        //opTransform(context, id + ("transform" ~ i), {
        //            "bodies" : definition.cuttingTool,
        //            "transform" : rotationAround(gearCuttingAxis, 1 * degree)
        //        });

        //opTransform(context, id + ("transform" ~ (i + count + 2)), {
        //            "bodies" : definition.gearBlank,
        //            "transform" : rotationAround(toCutAxis, -0.5 * degree)
        //        });

        for (var i = 0; i < count; i += 1)
        {
            //Rotating the gear cutter
            opTransform(context, id + ("transform"~i), {
                    "bodies" : definition.cuttingTool,
                    "transform" : rotationAround(gearCuttingAxis, 1 * degree)
            });

            //Rotating the gear to be cut
            opTransform(context, id + ("transform"~(i+count+2)), {
                    "bodies" : definition.gearBlank,
                    "transform" : rotationAround(toCutAxis, -0.5*degree)
            });
        
        
            //Performing the hobbing operation
            opBoolean(context, id + ("boolean1"~i), {
                    "tools" : qCreatedBy(id + ("transform"~i), EntityType.BODY),
                    "targets" : qCreatedBy(id + ("transform"~(i+count+2)), EntityType.BODY),
                    "operationType" : BooleanOperationType.SUBTRACTION,
                    "keepTools" : true
           });
        }
    });

Answers

  • Alex_KempenAlex_Kempen Member Posts: 244 EDU
    It's hard to tell what exactly is going on since you've opted to paste your entire code (instead of, say, linking to your feature studio), but I think your issue might be that you're trying to use qCreatedBy() on a transform, when you can instead pass definition.gearBlank and definition.cuttingTool into opBoolean directly.

    If you think about it, a transform doesn't really create anything, just move them around, so those qCreatedBy doesn't return valid queries from transforms. You can see this by debugging your tools and targets queries before the opBoolean runs ( i.e. debug(context, definition.gearBlank); ); if the query resolves to nothing, then you've found your problem.

    P.S. If you make your transform id's unique (i.e. id + ("cutterTransform" ~ i) and id + ("gearBlankTransform" ~ i), you won't have to worry about their id's overlapping, and your code will be easier to understand. Also, you can maybe use opPattern instead of opTransform to quickly make some copies of your cutting tool, then subtract them from your gear blank:
    var myTransforms = [];
    var myIds = [];
    for (var i = 0; i < count; i += 1)
    {
        myTransforms = append(myTransforms, rotationAround(toCutAxis, 1 * degree));
        myIds = append(myIds, "cuttingTool" ~ i);
    }
    
    // create a bunch of tool copies
    opPattern(context, id + "cuttingToolPattern", {
        "entities" : definition.cuttingTool,
        "transforms" : myTransforms,
        "instanceNames" : myIds
    };
    
    opBoolean(context, id + "gearCut", {
        "tools" : qCreatedBy(id + "cuttingToolPattern", EntityType.BODY),
        "targets" : definition.gearBlank,
        "operationType" : booleanOperationType.SUBTRACTION
    }
    
    // delete tool copies
    opDeleteBodies(context, id + "deleteCuttingTool", { 
        "entities" : qUnion(qCreatedBy(id + "cuttingToolPattern", EntityType.BODY), definition.cuttingTool) 
    });
    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



Sign In or Register to comment.