Пример #1
0
        /// <summary>
        /// Creates an unit system from the given prototype. This can be useful when
        /// e.g. extending one of the standard unit systems with custom units.
        /// If the prototype is an UnitSystem the created unit system will be an
        /// identical copy of the unit system. If not, only units that are convertible
        /// to the base unit will be added.
        /// </summary>
        /// <param name="prototype"></param>
        /// <returns></returns>
        public static UnitSystem CreateFrom(IUnitSystem prototype)
        {
            if (prototype is UnitSystem)
            {
                return((UnitSystem)((UnitSystem)prototype).Clone());
            }

            UnitSystem system = new UnitSystem();

            foreach (IDimension dimension in prototype.Dimensions)
            {
                system.AddDimension(dimension);

                // Register base unit
                IUnit baseUnit = prototype.GetBaseUnit(dimension);
                system.AddBaseUnit(baseUnit);

                // Register scaled units
                foreach (IUnit unit in prototype.GetSupportedUnits(dimension))
                {
                    if (!ReferenceEquals(unit, baseUnit) && prototype.CanConvert(baseUnit, unit))
                    {
                        system.AddScaledUnit(baseUnit, unit, prototype.CreateConverter(baseUnit, unit));
                    }
                }
            }
            return(system);
        }
Пример #2
0
        public void LengthTimesLength_Returns_AreaQuantity()
        {
            // Arrange
            SiUnitSystemFactory siFactory = new SiUnitSystemFactory();
            IUnitSystem         siSystem  = siFactory.Create();

            StandardQuantitiesCatalog.Setup();

            // Act
            Quantity <float> width    = new Quantity <float>(2.0f, StandardUnitsCatalog.Meter);
            Quantity <float> height   = new Quantity <float>(4.0f, StandardUnitsCatalog.Meter);
            Quantity <float> resultm2 = (Quantity <float>)(width * height);

            IValueConverter converter = siSystem.CreateConverter(resultm2.Unit, StandardUnitsCatalog.SquareCentimeter);

            Assert.NotNull(converter);

            Quantity <float> resultcm2 = new Quantity <float>(converter.Convert(resultm2.Value), converter.Target);

            // Assert
            Assert.AreEqual(8.0f, resultm2.Value);
            Assert.AreEqual(StandardUnitsCatalog.SquareMeter, resultm2.Unit);
            Assert.AreEqual(StandardDimensionsCatalog.Area, resultm2.Dimension);
            Assert.AreEqual(StandardDimensionsCatalog.Area, resultcm2.Dimension);
            Assert.AreEqual(resultm2.Value * 100.0f * 100.0f, resultcm2.Value);
        }
Пример #3
0
        private void ConvertTemperatures()
        {
            var             celcius              = new Quantity <double>(20, StandardUnitsCatalog.Celcius);
            IValueConverter toKelvin             = _siSystem.CreateConverter(celcius.Unit, StandardUnitsCatalog.Kelvin);
            IValueConverter toFahrenheit         = _siSystem.CreateConverter(celcius.Unit, StandardUnitsCatalog.Fahrenheit);
            var             kelvin               = new Quantity <double>(toKelvin.Convert(celcius.Value), toKelvin.Target);
            var             fahrenheit           = new Quantity <double>(toFahrenheit.Convert(celcius.Value), toFahrenheit.Target);
            IValueConverter fromFtoK             = _siSystem.CreateConverter(fahrenheit.Unit, StandardUnitsCatalog.Kelvin);
            var             kelvinFromFahrenheit = new Quantity <double>(fromFtoK.Convert(fahrenheit.Value), toKelvin.Target);

            Console.WriteLine("--- Temperature conversion ---");
            Console.WriteLine("{0} is equivalent to {1}", celcius, fahrenheit);
            Console.WriteLine("{0} is equivalent to {1}", celcius, kelvin);
            Console.WriteLine("{0} is equivalent to {1}", fahrenheit, kelvinFromFahrenheit);
            Console.WriteLine();
        }