Exemplo n.º 1
0
 public void TestLengthConvert(string test, double expected, string expectedUnits)
 {
     if (ValueWithUnits.TryParse(test, out ValueWithUnits valueWithUnits))
     {
         var converted = FilamentMath.ConvertLength(valueWithUnits.Value, FilamentMath.SupportedLengthAlias(valueWithUnits.Units), ConvertToLength.Millimeter);
         Assert.AreEqual(true, converted.WithinTolerance(expected, matchTolerance));
         Assert.AreEqual(expectedUnits, valueWithUnits.Units);
     }
     else
     {
         Assert.Fail();
     }
 }
Exemplo n.º 2
0
        public void TestConvertCompoundLength(string test, double expected, double tolerance, bool outcome)
        {
            if (CompoundFractionWithUnits.TryParse(test, out CompoundFractionWithUnits value))
            {
                //int roundDigits = 4;

                var converted = FilamentMath.ConvertLength(value.Value, FilamentMath.SupportedLengthAlias(value.Units), ConvertToLength.Millimeter);
                //var rounded = Math.Round(converted, roundDigits);
                //var meetsTolerance = Math.Abs(converted - expected) < tolerance;
                Assert.AreEqual(outcome, converted.WithinTolerance(expected, tolerance));
            }
            else
            {
                Assert.Fail();
            }
        }
Exemplo n.º 3
0
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            bool            unitsSet = false;
            ConvertToLength expectedUnits;

            //double conversionFactor = 1;
            if (parameter is string expected)
            {
                if (Enum.TryParse <ConvertToLength>(expected, out expectedUnits))
                {
                    unitsSet = true;
                }
            }
            else if (parameter is ConvertToLength actual)
            {
                expectedUnits = actual;
                unitsSet      = true;
            }
            else
            {
                expectedUnits = ConvertToLength.Millimeter;
                unitsSet      = true;
            }
            if (value is string str && unitsSet)
            {
                // TODO: implement a conversion factor for the units
                if (ValueWithUnits.TryParse(str, out ValueWithUnits valueWithUnits))
                {
                    return(FilamentMath.ConvertLength(valueWithUnits.Value, FilamentMath.SupportedLengthAlias(valueWithUnits.Units), expectedUnits));
                }
                else if (CompoundFractionWithUnits.TryParse(str, out CompoundFractionWithUnits compoundFractionWithUnits))
                {
                    return(FilamentMath.ConvertLength(compoundFractionWithUnits.Value, FilamentMath.SupportedLengthAlias(compoundFractionWithUnits.Units), expectedUnits));
                }
            }
            return(value);
            //throw new NotImplementedException();
        }
Exemplo n.º 4
0
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ConvertToLength convertTo = ConvertToLength.Millimeter;
            string          defaultUnits;

            if (parameter is string paraString)
            {
                if (Enum.TryParse(paraString, out ConvertToLength result))
                {
                    convertTo = result;
                }
                defaultUnits = shortHandnames[(int)convertTo];
            }
            else
            {
                defaultUnits = shortHandnames[(int)convertTo];
            }

            if (value is string str)
            {
                if (ValueWithUnits.TryParse(str, out ValueWithUnits valueWithUnits))
                {
                    //var match = Regex.Match(str, WeightWithUnitsConverter.regexFindNumberAndUnit);
                    SupportedLength supported = FilamentMath.SupportedLengthAlias(string.IsNullOrEmpty(valueWithUnits.Units) ? defaultUnits : valueWithUnits.Units);

                    return(FilamentMath.ConvertLength(valueWithUnits.Value, supported, convertTo));
                }
                else if (CompoundFractionWithUnits.TryParse(str, out CompoundFractionWithUnits compoundFractionWithUnits))
                {
                    //var fracMatch = Regex.Match(str, regexFindFraction);
                    SupportedLength supported = FilamentMath.SupportedLengthAlias(!string.IsNullOrEmpty(compoundFractionWithUnits.Units) ? compoundFractionWithUnits.Units : "in");
                    return(FilamentMath.ConvertLength(compoundFractionWithUnits.Value, supported, convertTo));
                    //double number;
                    //double numerator;
                    //double denominator;
                    //if (double.TryParse(fracMatch.Groups["numerator"].Value, out numerator) && double.TryParse(fracMatch.Groups["denominator"].Value, out denominator))
                    //{
                    //    if (!string.IsNullOrEmpty(fracMatch.Groups["whole"].Value))
                    //    {
                    //        if (double.TryParse(fracMatch.Groups["whole"].Value, out number))
                    //        {
                    //            return (number + (numerator / denominator)) * ConversionFactor(string.IsNullOrEmpty(fracMatch.Groups["units"].Value) ? "in" : fracMatch.Groups["units"].Value.ToLower(), convertTo);
                    //        }
                    //        else
                    //            return double.NaN;
                    //    }
                    //    else
                    //    {
                    //        return (numerator / denominator) * ConversionFactor(string.IsNullOrEmpty(fracMatch.Groups["units"].Value) ? "in" : fracMatch.Groups["units"].Value.ToLower(), convertTo);
                    //    }
                }
                else
                {
                    return(double.NaN);
                }
            }
            else
            {
                return(double.NaN);
            }
        }