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.

Struggling with feature script units...

dave_franchinodave_franchino Member Posts: 52 ✭✭
Hey kind folks,

I'm trying to explore feature script and am really struggling to get units correct. It's worth saying that I don't really know javascript so I'm probably way over my head. 

In the following feature script i'm trying to create a spline on a plane based on equations for x and y.  

I'm getting an error... 

Can not subtract number and ValueWithUnits (map)



https://cad.onshape.com/documents/58cd64645b11f7ce6f6724b0/w/f1e068070f1a512dbf46e2fb/e/efde6f957c03969e8312bbeb

Can anyone help me with how to define variables so that I can use the correctly?

Thanks!  

Comments

  • Caden_ArmstrongCaden_Armstrong Member Posts: 177 PRO
    It would help if your document was made public.

    Sounds like you have a variable somewhere that does not have a length assigned to it.
    An equation like:

    var b = 6*millimeter;
    var a = 5 + b;

    Doesn't work because b has a type of "value with units" where as "5" is just a number. In FeatureScript keeping track of what has units and what doesn't have units really matters. But the benefit is that FeatureScript does a lot of the heavy lifting when it comes to unit conversion.
    www.smartbenchsoftware.com --- fs.place --- Renaissance
    Custom FeatureScript and Onshape Integrated Applications
  • jelte_steur814jelte_steur814 Member Posts: 183 PRO
    To complement Caden's point:

    a = 5*inch + b; would work.

    in case you're not sure about b,

    println(b);


  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    edited June 20
    @dave_franchino this could be all wrong but....

    When writing FS I change the document units to meters. If you think in inches, then you'll have to worry about values with units when writing scripts. The base units for FS are meters, anything else requires values with units.

    If you printout what's returned, you'll see when things are value with unit's. To remove them just /meter then print the return again and they're gone. If you want to add them to something that doesn't have them, then *meter. Then print them again and you'll see they are back. I prefer to work without them, but I could be wrong.

    When I'm writing FS my units are meters. If you're converting m^3/h to gpm in your feature script, then you might want to stick with values with units. Honestly, I don't think m^3/h is a type in FS, there's probably a way to add it though.


  • dave_franchinodave_franchino Member Posts: 52 ✭✭
    @caden_armstrong2, @jelte_steur814, @billy2,

    Thanks for your input - sorry about the private document - I'm new to onshape and didn't realize I needed to make the document private. I have hopefully fixed it here:

    https://cad.onshape.com/documents/58cd64645b11f7ce6f6724b0/w/f1e068070f1a512dbf46e2fb/e/efde6f957c03969e8312bbeb

    I'm trying to write an equation and use that to drive a spline on a plane.  

    The relevant section of code that's failing is as follows:  

                var x is ValueWithUnits = (definition.numTeeth * definition.toothPitch) * cos(angle) + definition.eccentricty * cos((definition.numTeeth + 1) * angle) - definition.mainDiameter / 2 * cos(gamma + angle);


                var y is ValueWithUnits = ((definition.numTeeth * definition.toothPitch) * sin(angle) + definition.eccentricty * sin((definition.numTeeth + 1) * angle) - definition.mainDiameter / 2 * sin(gamma + angle));



                // Convert the point to sketch coordinates
                var point = vector(x, y,0);
                // debug(context, "got here");
                var planePoint = worldToPlane(sketchPlane, point);
                debug(context, "got here 2");
                points = append(points, planePoint);

    The line that is failing is             var planePoint = worldToPlane(sketchPlane, point);

    Can not subtract number and ValueWithUnits (map)

    Do you see what I'm doing wrong? 

    Sorry about the naive questions - I'm a rookie here. Be gentle! THanks for the help!! 

    Dave
  • jelte_steur814jelte_steur814 Member Posts: 183 PRO
    edited June 24
    @dave_franchino

    I think you're point is bugging. you're creating a vector with (x, y, 0).
    x and y are both ValueWithUnits, 0 is not. Try "0*meter".

    I tried it in a copy and I "got here 2" ;-)
    https://cad.onshape.com/documents/88de244120ace6beb90dbd86/w/97eedd8f6159633b6f313277/e/7df1a69d29d51d884d8f2c63?renderMode=0&uiState=6679241dd33e3b19e075ccae

    Tip: a println(point);  or debug(context, point); provides more valuable insight than debug(context, got here);
    try that and see if you can spot the difference between 0 and 0*meter there.

    kind regards

    Jelte
  • jelte_steur814jelte_steur814 Member Posts: 183 PRO
    BTW: when you import 'geometry.fs'  your'e basically importing all fs requires: it already includes 'common.fs' which includes 'units.fs' and 'mathUtils.fs' which in turn includes 'math.fs'. 

    you might get away with only 'common.fs' which will make it slightly faster. not sure though. if that starts bugging, try 'geometry.fs' instead (rather than both)

Sign In or Register to comment.