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.

Placing a skRectangle centered on a queried vertex

Dylan_StewartDylan_Stewart Member, Developers Posts: 107 PRO
Hello all! I am trying to write a feature that will place a sketched rectangle the is centered on a queried vertex. I am looking at @NeilCooke 's "Mounting Boss" feature for guidance and basing my code on what he has. I'm pretty close but I am not getting the desired results. 

FeatureScript 675;
import(path : "onshape/std/geometry.fs", version : "675.0");
export enum postSize
{
    annotation { "Name" : "Small" }
    SMALL,
    annotation { "Name" : "Large" }
    LARGE
}
export function postSizeID(value is postSize)
{
    return{
        "SMALL" : 4 * inch,
        "LARGE" : 6 * inch
    }[value as string];
    
}

annotation { "Feature Type Name" : "Adjustable Louvre" }
export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
        annotation { "Name" : "My Query", "Filter" : EntityType.VERTEX, "MaxNumberOfPicks" : 4 }
        definition.postLoc is Query;
        
        annotation { "Name" : "Face Plane", "Filter" : EntityType.FACE, "MaxNumberOfPicks" : 1 }
        definition.face is Query;
        
        annotation { "Name" : "Post Size" }
        definition.postSize is postSize;
        
        annotation { "Name" : "Louvre Height" }
        isLength(definition.louvreHeight, LENGTH_BOUNDS);
    }
    {
        var postSize = postSizeID(definition.postSize);
        var height = definition.louvreHeight;
        
        const postLocation = evaluateQuery(context, definition.postLoc);
        var sketchPlane is Plane = evOwnerSketchPlane(context, {
                "entity" : postLocation[0]
        });
        var topPlane = evPlane(context, { "face" : definition.face});
        definition.elemAdd = true;
        definition.elemId = 1;
        var nameId = 1;
        var startPoint = vector(postLocation-postSize/2);
        var endPoint = vector(postLocation+postSize/2);
        definition.sketch = newSketchOnPlane(context, id + "sketch1", {
                "sketchPlane" : topPlane
        });
        
        for (var location in postLocation)
        {
            var point is Vector = worldToPlane(topPlane, evVertexPoint(context, {
                    "vertex" : location
            }));
            
            // skRectangle(definition.sketch, "rectangle1" ~ nameId, {
            //         "center" : vector(point[0], point[1]),
            //         "firstCorner" : vector(point[0], point[1]),
            //         "secondCorner" : vector(postSize, postSize)
            // });
            skRectangle(definition.sketch, "rectangle2" ~ nameId, {
                    "center" : vector(point[0], point[1]),
                    "firstCorner" : startPoint,
                    "secondCorner" : endPoint
            });
        nameId += 1;
        }
        
        buildPost(context, id, definition);
        
    });
    function buildPost(context is Context, id is Id, definition is map)
        {
            skSolve(definition.sketch);
            
        }
It seems like I am just missing something small. 
Digital Engineering

Comments

  • Dylan_StewartDylan_Stewart Member, Developers Posts: 107 PRO
    Okay so after looking at the std sketch.fs I know the "center" does not work with skRectangle.... So that tells me there will have to be some math equations will have to be put in place. 
    Also, I realize now that my startPoint and endPoint variables are not separated by commas (fixed and still doesn't work).

    Digital Engineering
  • lanalana Onshape Employees Posts: 706
    edited September 2017
    I suspect things start falling apart at var startPoint = vector(postLocation-postSize/2);  where you are subtracting length from an array of queries.

    Instead move defining startPoint, endPoint into the body of the loop.
    const startPoint = vector(point[0] - 
    postHalfSize, point[1] - postHalfSize);
    with postHalfSize defined before of the loop.
  • Dylan_StewartDylan_Stewart Member, Developers Posts: 107 PRO
    Works perfectly!!!! Thank you @lana !
    Digital Engineering
Sign In or Register to comment.