fixes #0003137: Unit UserString wrong

This commit is contained in:
wmayer
2017-07-24 16:52:03 +02:00
parent 80ad5737f0
commit 7ab8a9996d
3 changed files with 25 additions and 3 deletions

View File

@@ -42,6 +42,14 @@ QString UnitsSchemaInternal::schemaTranslate(const Quantity &quant, double &fact
double UnitValue = std::abs(quant.getValue());
Unit unit = quant.getUnit();
// In order to get the right factor always express the target
// units as internal units where length is in mm and mass in kg
// Example:
// For W/mm/K we get the factor of 1000000.0 because
// W/mm/K = kg*m^2/s^3/mm/K
// = 10e6 * kg*mm^2/s^3/mm/K
// = 10e6 * kg*mm/s^3/K
// now do special treatment on all cases seems necessary:
if (unit == Unit::Length) { // Length handling ============================
if (UnitValue < 0.000000001) {// smaller then 0.001 nm -> scientific notation
@@ -108,7 +116,7 @@ QString UnitsSchemaInternal::schemaTranslate(const Quantity &quant, double &fact
else if (unit == Unit::ThermalConductivity) {
if (UnitValue < 1000) {
unitString = QString::fromLatin1("W/mm/K");
factor = 1.0;
factor = 1000000.0;
}
else {
unitString = QString::fromLatin1("W/m/K");

View File

@@ -131,7 +131,7 @@ QString UnitsSchemaMKS::schemaTranslate(const Quantity &quant, double &factor, Q
else if (unit == Unit::ThermalConductivity) {
if (UnitValue < 1000) {
unitString = QString::fromLatin1("W/mm/K");
factor = 1.0;
factor = 1000000.0;
}
else {
unitString = QString::fromLatin1("W/m/K");

View File

@@ -4,10 +4,16 @@ import FreeCAD
import unittest
import math
def tu(str):
return FreeCAD.Units.Quantity(str).Value
def ts(q):
return q.UserString
def ts2(q):
return FreeCAD.Units.Quantity(q.UserString).UserString
#---------------------------------------------------------------------------
# define the functions to test the FreeCAD UnitApi code
#---------------------------------------------------------------------------
@@ -36,6 +42,14 @@ class UnitBasicCases(unittest.TestCase):
#self.failUnless(compare(tu('1fo(3+7/16)in'),392.112500))thisgivesaparsersyntaxerror!!!
self.failUnless(compare(tu('1\'(3+7/16)"'), 392.112500))
def testSelfConsistency(self):
qu = FreeCAD.Units.Quantity("0.23 W/m/K")
self.assertTrue(ts(qu), ts2(qu))
qu = FreeCAD.Units.Quantity("237 mm*kg/(s^3*K)")
self.assertTrue(ts(qu), ts2(qu))
qu = FreeCAD.Units.Quantity("237.000 W/mm/K")
self.assertTrue(ts(qu), ts2(qu))
def testTrigonometric(self):
#tu=FreeCAD.Units.translateUnit
self.failUnless(compare(tu('sin(pi)'), math.sin(math.pi)))