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.

Simple Question

billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,071 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_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    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.

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




Sign In or Register to comment.