opBoolean.SUBTRACTION not working?

Federico_Casarini
Federico_Casarini Member Posts: 9 PRO
Hello! I am trying to create a quick feature script to engrave a text onto an existing object. This is the code I am using:

annotation { "Feature Type Name" : "Tag" }
export const Tag = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
    annotation { "Name" : "Location and Orientation", "Filter" : BodyType.MATE_CONNECTOR, "MaxNumberOfPicks" : 1 }  
    definition.direction is Query;

    annotation { "Name" : "Name", "Default" : "Name" }
    definition.myString is string;

    annotation { "Name" : "Size[mm]:" }
    isReal(definition.textsize, TAG_BOUNDS);
    }
    {
    var sketch_tag = newSketchOnPlane(context, id + "sketch11", {
            "sketchPlane" : plane(WORLD_COORD_SYSTEM)
    });
            
    skText(sketch_tag, "text1", {
            "text" : definition.myString,
            "construction": true,
            "fontName" : "OpenSans-Regular.ttf",
            "firstCorner" : vector(1,0.1) * millimeter ,
            "secondCorner" : vector(0.25,definition.textsize) * millimeter
    });
            
    skSolve(sketch_tag);
            extrude(context, id + "extrude21", {
            "entities" : qSketchRegion(id + "sketch11",true),
            "endBound" : BoundingType.BLIND,
            "depth" : -0.5 * millimeter 
    }); 
   //delete the sketch
   opDeleteBodies(context, id + "deleteBodies", { "entities" : qCreatedBy(id + "sketch11") });
    var dir = definition.direction;
    var direction = evMateConnector(context, { "mateConnector" : dir });  
    var start = direction.origin;  // xyz
    const moveFrom = coordSystem(vector(0, 0, 0) * millimeter, vector(1, 0, 0), vector(0, 0, 1)); 
    const moveTo = coordSystem( start, direction.xAxis, direction.zAxis );  // (origin * units, xAxis, zAxis)
    opTransform(context, id + "transform1", {
            "bodies" : qCreatedBy(id + "extrude21", EntityType.BODY),
            "transform" : toWorld(moveTo) * fromWorld(moveFrom)
    });

    var target = qSubtraction(qBodyType(qEverything(EntityType.BODY), BodyType.SOLID), qCreatedBy(id + "extrude21", EntityType.BODY));
        
    opBoolean(context, id + "boolean21", {
             "tools" : qBodyType(qCreatedBy(id + "extrude21", EntityType.BODY), BodyType.SOLID),
             "target" :target,
             "operationType" : BooleanOperationType.SUBTRACTION,
             "keepTools": false
    });

    debug(context, target);
    debug(context, qBodyType(qCreatedBy(id + "extrude21", EntityType.BODY), BodyType.SOLID));
    

});

The idea is to create a sketch on the top plane, extrude it, move it onto the selected plane through the mate connector and subtract the text from the original solid. Unfortunately, when I run the code, I get a "cannot resolve entities" error. The tools and target are intersecting each other. 
I also added a debug line to check if I was selecting the right solids and seems correct.



Can anyone help me with this? 

Thanks!

Fed 

Best Answers

Answers

  • Federico_Casarini
    Federico_Casarini Member Posts: 9 PRO
    Hi Jake, thanks for the help! Sorry it's the first time I post on the forum, I will create a code box next time.

    You are absolutely right, that worked! I guess that anything that is inside the "quotes" doesn't generate an error in the editor.

    Thanks a lot!

    Fed 
  • Federico_Casarini
    Federico_Casarini Member Posts: 9 PRO
    Yes! If the error at runtime could point at which line went wrong, it will certainly help the newbies like me! :) thanks again!
  • NeilCooke
    NeilCooke Moderator, Onshape Employees Posts: 6,126 image
    @Federico_Casarini - it does. If you open the FeatureScript Notices panel (the checkmark in curly braces at the top), it will give you a stack trace as to where the error lies.
    Senior Director, Technical Services, EMEA
  • Federico_Casarini
    Federico_Casarini Member Posts: 9 PRO
    Hi Neil, thanks for this! I was referring to the actual line that generates the error. Something like "targets is undefined" could be helpful. I appreciate it's the kind of error that you do only once, because now I know what to look for. But it might save some time to new starters.

    Thanks for all you help, super appreciated. You guys are doing a great job!

    Fed