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.

opBoolean.SUBTRACTION not working?

Federico_CasariniFederico_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_CasariniFederico_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_CasariniFederico_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!
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,671
    @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, EMEAI
  • Federico_CasariniFederico_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
Sign In or Register to comment.