Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- 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.
convert units?
adamohern
Member, OS Professional Posts: 216 PRO
Silly question. I have a length parameter expressed internally as meters. I would like to know the same value in inches. How can I get the length map's value converted to inches?
0
Comments
That doesn't give me the proper value, nor would I expect it to.
* centimeter
, or gotten from evDistance, or calculated by dividing "just a volume" by "just an area". All these are just lengths. I can take one length, subtract another length from it, pass the result into a function that expects a length, all without caring about units.The flip side of this is: if you find yourself wanting to create a variable like "numberOfInches", it's worth thinking about why. That variable does have a bias about its own units, and once you create it, those units will need to be considered for the whole lifetime of that variable. numberOfInches can't be added to other FeatureScript lengths, nor can it be passed into functions expecting FeatureScript length. So, you're going to be paying the cost of maintaining such a variable every time it interacts with something else in FS.
You can instead write code which takes advantage of FS units in formulas, like:
The result, mercury_planet_mass, is "just a mass". By default, a ValueWithUnits will print in SI units (in this case, "3.3031834839597105e+23 kilogram"). You can see that value with different units by dividing out the units before printing, like
println("Mass: " ~ mercury_planet_mass / pound ~ " pounds");
Here is a snipet of my code:
This works but give me a some decimal at the end
//var inches = definition.myLength.value / inch;
but if i divide by inch
i get :
i did enter the lenght in INCH at the interface,
Any idea?
Thanks
Pat Farley
You were dividing a number by a "ValueWithUnits"
now i have inches but the other issues is , it is not rounded to the value i enter (32 inches)
i don't think that i could ask the builder to cut a 2x4 at 31.9999 inch ;-)
i will problably have to round it to the nearest 1/16.
i did not search for this function yet, is there anything that could do the job?
Thanks for your help guys
Pat Farley
i am trying this
i will let you know my find
Pat F
it now works with round(
Pat Farley
round(length, millimeter)
or without:round(number, 0.01)
, depending on what units the original value has.There's a few examples of printing values with units in a sane way at the top of the units module.
@kevin_o_toole_1
This may have already been answered and I missed it.
Is there a way to query the units that are on a number? Or perhaps a way to round with whatever units are already on the number?
Here is my current code, which only works if your studio is english.
Learn more about the Gospel of Christ ( Here )
CADSharp - We make custom features and integrated Onshape apps! Learn How to FeatureScript Here 🔴
ValuesWithUnits are agnostic to the unit type (they can more correctly be thought of as values with dimensions e.g. length, not units e.g. inches).
So no, there is no difference between setting a parameter value to "1 in" vs. setting it to "2.54 cm".
Sounds like you want code that does do different things for those two values, which smells a bit off. For instance, if you're trying to get two different units to compare equal, tolerantEquals() is definitely preferable to rounding both.
Granted, if you're interested in an end user output, correct units are definitely good. There's a couple ways to display values with units natively in Onshape:
- If you're trying to inform the user of an output value, you can use a READ_ONLY parameter to display it
- If you want many such outputs, a custom table can display those
And if you really do just need such a thing in the code, there is also a sneaky workaround where you can capture the default units of the workspace (at the time a feature was created) with an ALWAYS_HIDDEN length parameter with known defaults for each unit type.
Thank you for the explanation and examples. This is very helpful!
Ah, I like the work around.
Learn more about the Gospel of Christ ( Here )
CADSharp - We make custom features and integrated Onshape apps! Learn How to FeatureScript Here 🔴
Variable (Advanced)
Like Kevin said, this will capture the default units of the workspace at the time a feature was created.
precondition { annotation { "Name" : "Rounding / Precision", "Default" : RoundingPrecision.ONE_THOUSANDTH } definition.roundingPrecision is RoundingPrecision; //Get work space units work around. Reference kevin_o_toole_1 //https://forum.onshape.com/discussion/comment/43969#Comment_43969 annotation { "Name" : "Invisible length", "UIHint" : UIHint.ALWAYS_HIDDEN } isLength(definition.invisibleLength, { (inch) : [-10000, /* min (inclusive) */ 1.0, /* default */ 10000 /* max (inclusive) */], (meter) : 1, (centimeter) : 1, (millimeter) : 1, (foot) : 1, (yard) : 1 } as LengthBoundSpec); } { const units = definition.invisibleLength; var unitMultiplyer = switch (units) { (inch) : inch, (meter) : meter, (centimeter) : centimeter, (millimeter) : millimeter, (foot) : foot, (yard) : yard, }; var unitlessValue = definition.value / unitMultiplyer; var value = roundToPrecision(unitlessValue, 2); value = value * unitMultiplyer; setVariable(context, definition.name, value); setFeatureComputedParameter(context, id, { "name" : "d", "value" : value }); });
Learn more about the Gospel of Christ ( Here )
CADSharp - We make custom features and integrated Onshape apps! Learn How to FeatureScript Here 🔴
round(length, millimeter)
" syntax added to the documentation: https://cad.onshape.com/FsDoc/library.html#roundToPrecision-number-numberI also noticed that the min() and max() functions accept valueWithUnit types which isn't specifically mentioned. This is minor stuff but always better to be thorogh in documentation
Holy crap I'm glad I wrote that post over a year ago hahaha, I've completely forgotten I figured it out. Still have no idea how or why it works.
round funciton can be used to round value with units