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.

In Feature Script: Zero need ValueWithUnits when sketching somethings?

yvan_bourassayvan_bourassa Member Posts: 2 ✭✭
edited February 2022 in Community Support
Hello Everyone,

I try to sketch circle like this:

    precondition
    {
    annotation { "Name" : "Ring Diameter" }
    isLength(definition.myRingDiameter, { (millimeter) : [0, 120, 500] } as LengthBoundSpec);        
    }

var D = definition.myRingDiameter;
skCircle(sketchPINS, "circle1", {
                "center" : vector(0 , D/2),
                "radius" : definition.myPinDiameter/2
        });
 this code fail! Precondition of skCircle failed (value.center is undefined || is2dPoint(value.center))

var D = definition.myRingDiameter;
skCircle(sketchPINS, "circle1", {
                "center" : vector(0 * millimeter, D/2),
                "radius" : definition.myPinDiameter/2
        });
this code pass!

Someone have an explanation, why Zero need units?


Best Answers

  • mahirmahir Member, Developers Posts: 1,309 ✭✭✭✭✭
    Answer ✓
    It needs units because it's a coordinate. X, Y, and Z coordinates are generally nonzero, i.e. the nonzero numerical space is a lot larger than the zero space. So it's more straightforward to just expect them to have units. Otherwise you'd have to program an extra "If" into every function just to check if a value is zero and apply a default unit.

    Bottom line is featurescript won't let you be lazy. Variables that generally need units (distance, coordinates, angles, mass, etc) should be explicitly declared with units. It'll keep you out of trouble in the long run. It's no different than declaring a double or integer variable type. Zero is zero right? No, not really. An integer zero doesn't function like a double zero.
  • yvan_bourassayvan_bourassa Member Posts: 2 ✭✭
    Answer ✓
    Thanks, 

Answers

  • mahirmahir Member, Developers Posts: 1,309 ✭✭✭✭✭
    Answer ✓
    It needs units because it's a coordinate. X, Y, and Z coordinates are generally nonzero, i.e. the nonzero numerical space is a lot larger than the zero space. So it's more straightforward to just expect them to have units. Otherwise you'd have to program an extra "If" into every function just to check if a value is zero and apply a default unit.

    Bottom line is featurescript won't let you be lazy. Variables that generally need units (distance, coordinates, angles, mass, etc) should be explicitly declared with units. It'll keep you out of trouble in the long run. It's no different than declaring a double or integer variable type. Zero is zero right? No, not really. An integer zero doesn't function like a double zero.
  • _anton_anton Member, Onshape Employees Posts: 413
    edited February 2022
    If you look at the definition of is2dPoint in vector.fs, it checks that every term in the vector is a length - specifically, it's a ValueWithUnits and it has length units.

    This is essentially a data correctness check: even though zero of anything seems like it should be just zero, units are meant to signify *what physical quantity* your datum is. I'd expect that passing 0 degrees to something that expects length would fail, because that input can change. Being too smart about converting one type of zero to another might lead to your feature silently succeeding with a zero input of the wrong type but then mysteriously failing when that changes.

    Edit: Mahir beat me to it. :smile:
  • yvan_bourassayvan_bourassa Member Posts: 2 ✭✭
    Answer ✓
    Thanks, 

Sign In or Register to comment.