Simple Question

billy2
billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,206 PRO
I figured geometry.fs would include a math library with sin() or cos(). Do I have to load an additional library?






FeatureScript 275;
import(path : "onshape/std/geometry.fs", version : "275.0");
//import(path : "onshape/std/units.fs", version : "275.0");

annotation { "Feature Type Name" : "Cylinders" }
export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
        // Define the parameters of the function definition
        annotation { "Name" : "Radius" }
        isLength(definition.radius, LENGTH_BOUNDS);

        annotation { "Name" : "Length" }
        isLength(definition.length, LENGTH_BOUNDS);
    }

    {
        // Define the function's action
        const DEBUG = false;
        
        if ( DEBUG ){      
            debug(context,"Test DEBUG");
        }     
        
        var i = 1;
        var j = 1;
        var n = 4;
        var pi = 4 * atan(1);
        var ang= 360 / n;

        //ang = 360 / 2 / pi / n;

        for (var cnt = 1; cnt <= n; cnt += 1)
        {
            i=sin(ang*cnt);
            j=cos(ang*cnt);

            fCylinder(context, id + "cylinder" + cnt, {
                        "bottomCenter" : vector(-1*i, -1*j, 0) * definition.length,
                        "topCenter" : vector(1*i, 1*j, 0) * definition.length,
                        "radius" : definition.radius
                    });
        }
       




    }, { /* default parameters */ });

Comments

  • kevin_o_toole_1
    kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565 image
    As specified in the units module, the sin function takes in a ValueWithUnits, not a plain number. It's defined this way so that we don't have degree/radian confusion.

    You can create a ValueWithUnits by multiplying by any unit. In your code you can either set ang to 360 * degree / n, or to 2 * PI * radian / n (where PI is a constant set in math.fs). These will both behave identically. 

    Importing the units module separately isn't necessary, since (essentially) all of the standard library is imported automatically when you import geometry.fs.

  • billy2
    billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,206 PRO
    thanks, that worked.