Placing a skRectangle centered on a queried vertex

Dylan_Stewart
Dylan_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_Stewart
    Dylan_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
  • lana
    lana Onshape Employees Posts: 782 image
    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_Stewart
    Dylan_Stewart Member, Developers Posts: 107 PRO
    Works perfectly!!!! Thank you @lana !
    Digital Engineering