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.

How to Reference Individual Sketch Entities

michael_diehlmichael_diehl Member Posts: 5

I’m currently exploring FeatureScript and working on a function that creates a set of rectangles within a sketch:

export function cskDraw(context is Context, sketch is Sketch, featurePrefix is string)
{
const offsets = [0, 50, 100];
for (var i = 0; i < 3; i += 1)
{
skRectangle(sketch, featurePrefix ~ ("rectangle" ~ (i + 1)), {
"firstCorner" : vector(xMin + offsets[i], yMin + offsets[i]) * millimeter,
"secondCorner" : vector(xMax + offsets[i], yMax + offsets[i]) * millimeter
});
}
}

I’m invoking it like so:

const id_fwFrontSketch = id + "fwSketchFront";
const fwFrontSketch = newSketchOnPlane(context, id_fwFrontSketch, {
"sketchPlane" : plane(vector(0, 0, 0), vector(0, -1, 0), vector(1, 0, 0))
});
cskDraw(context, fwFrontSketch, "recs");
skSolve(fwFrontSketch);

I know I can highlight the resulting sketch regions using something like:

debug(context, qSketchRegion(id_fwFrontSketch), DebugColor.RED);

However, this approach applies the same color to all rectangles at once. I’d like to highlight each rectangle individually with different colors. For instance, "rectangle1" in red and the others in blue.

My challenge is determining how to reference each rectangle individually. I notice that the query functions like qCreatedBy require an Id rather than a string. Is there a straightforward way to query each rectangle by its assigned name or another identifier?

Any guidance on selectively highlighting specific rectangles within the same sketch would be greatly appreciated.

Comments

  • Caden_ArmstrongCaden_Armstrong Member Posts: 195 PRO
    edited December 6

    sketchEntityQuery(id + "sketch1", EntityType.BODY, sketchEntityId)

    To the Onshape dev who made this one: why doesnt this one start with q? qSketchEntityQuery follows naming conventions…or better yet, qSketchEntity?

    www.smartbenchsoftware.com --- fs.place --- Renaissance
    Custom FeatureScript and Onshape Integrated Applications
  • michael_diehlmichael_diehl Member Posts: 5

    @Caden_Armstrong

    Thank you for response. I tried using sketchEntityQueryand the only output I get is "debug: Query resolves to nothing". I tried using this both before and after 'skSolve', in case that matters.

  • Caden_ArmstrongCaden_Armstrong Member Posts: 195 PRO

    Rectangles aren't a single body, they are actually just 4 line segments that are constrainted.
    So FeatureScript is creating 4 bodies with id "sketchsegmentid+left/right/top/bottom". So you need to reference each rectangle side individually.

    Here is an example.
    https://cad.onshape.com/documents/a1f40918d4d33ea9daed6320/w/9e622d0c612befc74430a282/e/6d1aae827bc128ec1296c8fe

    www.smartbenchsoftware.com --- fs.place --- Renaissance
    Custom FeatureScript and Onshape Integrated Applications
  • michael_diehlmichael_diehl Member Posts: 5

    @Caden_Armstrong Thank you for being so helpful! But, Im's still a little confused. I can't figure out how to use `totalRectangle`to extrude that rectangle into a cube.

  • Caden_ArmstrongCaden_Armstrong Member Posts: 195 PRO

    If you want to extrude the rectangles into cubes, you need the faces not the edges.
    You could just take one edge and do qAdjacent to find the face adjacent to the edge.

    Are you extruding them all the same height? Or different heights?

    You could also just iterate through the list of faces by doing an evaluateQuery on the original qSketchRegion

    www.smartbenchsoftware.com --- fs.place --- Renaissance
    Custom FeatureScript and Onshape Integrated Applications
Sign In or Register to comment.