Ejemplo n.º 1
0
        /// <summary>
        /// Returns a unit of measure string presentation.
        /// </summary>
        /// <param name="unitOfMeasure">A unit of measure.</param>
        /// <returns>Unit of measure string presentation</returns>
        private string GetUnitOfMeasureString(UnitOfMeasure unitOfMeasure)
        {
            switch (unitOfMeasure)
            {
            case UnitOfMeasure.Millimeters:
                return("mm");

            case UnitOfMeasure.Centimeters:
                return("cm");

            case UnitOfMeasure.Inches:
                return("inch");

            case UnitOfMeasure.DeviceIndependentPixels:
                return("dip");

            case UnitOfMeasure.Pixels:
                return("px");

            case UnitOfMeasure.Points:
                return("point");

            case UnitOfMeasure.Twips:
                return("twip");

            case UnitOfMeasure.Emu:
                return("emu");
            }

            return(unitOfMeasure.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Converts the number of source units to target units.
        /// </summary>
        /// <param name="aUnits">A number of units.</param>
        /// <param name="aFromUnitOfMeasure">A from unit of measure.</param>
        /// <param name="aToUnitOfMeasure">A to unit of measure.</param>
        /// <returns></returns>
        public static double Convert(double aUnits, UnitOfMeasure aFromUnitOfMeasure, UnitOfMeasure aToUnitOfMeasure)
        {
            var vEnumType = typeof(UnitOfMeasure);
            //
            var vFromEnumInfo       = vEnumType.GetMember(aFromUnitOfMeasure.ToString());
            var vFromEnumAttributes = vFromEnumInfo[0].GetCustomAttributes(typeof(UnitOfMeasureAttribute), false);
            var vFromBaseUnit       = ((UnitOfMeasureAttribute)vFromEnumAttributes[0]).Base;
            var vFromEquals         = ((UnitOfMeasureAttribute)vFromEnumAttributes[0]).Equals;
            //
            var vToEnumInfo       = vEnumType.GetMember(aToUnitOfMeasure.ToString());
            var vToEnumAttributes = vToEnumInfo[0].GetCustomAttributes(typeof(UnitOfMeasureAttribute), false);
            var vToBaseUnit       = ((UnitOfMeasureAttribute)vToEnumAttributes[0]).Base;
            var vToEquals         = ((UnitOfMeasureAttribute)vToEnumAttributes[0]).Equals;

            //
            if (vToBaseUnit != vFromBaseUnit)
            {
                throw new ArgumentException(String.Format("Cannot convert incompatible UnitsOfMeasure {0} and {1}", aFromUnitOfMeasure, aToUnitOfMeasure));
            }
            //
            return((aUnits / vFromEquals) * vToEquals);
        }
Ejemplo n.º 3
0
 public string ToString(string format, IFormatProvider formatProvider)
 {
     if (format == null)
     {
     }
     else if (format.Contains("NU"))
     {
         return(UnitOfMeasure.ToString(mVolume, format, formatProvider));
     }
     else if (format.Contains("SU"))
     {
         var length1DUom = UnitOfMeasure as Length1DUnitOfMeasure;
         if (length1DUom != null)
         {
             return(UnitOfMeasure.ToString(mVolume, format.Replace("SU", "NU"), formatProvider) + @" " + length1DUom.VolumeUnitSymbol);
         }
         else
         {
             return(UnitOfMeasure.ToString(mVolume, format, formatProvider));
         }
     }
     return(mVolume.ToString((format ?? "").Replace("LU", ""), formatProvider) + @" Cubic " + UnitOfMeasure.ToString(format, formatProvider));
 }
Ejemplo n.º 4
0
        public void TestUnifiedSystem()
        {
            Assert.IsFalse(sys.Equals(null));

            Dictionary <String, UnitOfMeasure> unitMap = new Dictionary <String, UnitOfMeasure>();

            // check the SI units
            foreach (Unit unit in Enum.GetValues(typeof(Unit)))
            {
                UnitOfMeasure uom = sys.GetUOM(unit);

                Assert.IsNotNull(uom);
                Assert.IsNotNull(uom.Name);
                Assert.IsNotNull(uom.Symbol);
                Assert.IsNotNull(uom.Description);
                Assert.IsNotNull(uom.ToString());
                Assert.IsNotNull(uom.GetBaseSymbol());
                Assert.IsNotNull(uom.AbscissaUnit);
                Assert.IsNotNull(uom.ScalingFactor);
                Assert.IsNotNull(uom.Offset);

                // symbol uniqueness
                Assert.IsFalse(unitMap.ContainsKey(uom.Symbol));
                unitMap[uom.Symbol] = uom;
            }

            List <Unit> allUnits = new List <Unit>();

            foreach (Unit u in Enum.GetValues(typeof(Unit)))
            {
                allUnits.Add(u);
            }

            foreach (UnitOfMeasure uom in sys.GetRegisteredUnits())
            {
                if (uom.Enumeration != null)
                {
                    Assert.IsTrue(allUnits.Contains(uom.Enumeration.Value));
                }
            }

            foreach (UnitType unitType in Enum.GetValues(typeof(UnitType)))
            {
                UnitType found;
                bool     hasType = false;
                foreach (UnitOfMeasure u in sys.GetRegisteredUnits())
                {
                    if (u.UOMType.Equals(unitType))
                    {
                        found   = u.UOMType;
                        hasType = true;
                        break;
                    }
                }

                if (!hasType && !unitType.Equals(UnitType.UNCLASSIFIED))
                {
                    Assert.Fail("No unit found for type " + unitType);
                }
            }

            // constants
            foreach (Constant c in Enum.GetValues(typeof(Constant)))
            {
                Assert.IsTrue(sys.GetQuantity(c) != null);
            }
        }
Ejemplo n.º 5
0
 public string ToString(string format, IFormatProvider formatProvider)
 {
     return(UnitOfMeasure.ToString(mDuration, format, formatProvider));
 }
Ejemplo n.º 6
0
 public override string ToString()
 {
     return(UnitOfMeasure.ToString(mDuration, null, null));
 }
Ejemplo n.º 7
0
 public string ToString(string format, IFormatProvider formatProvider)
 {
     return(UnitOfMeasure.ToString(mLength, format, formatProvider));
 }
Ejemplo n.º 8
0
 public override string ToString()
 {
     return(UnitOfMeasure.ToString(mLength, null, null));
 }
Ejemplo n.º 9
0
        private void dgvUnitOfMeasure_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
        {
            if (e.Value == null || string.IsNullOrEmpty(e.Value.ToString()) || e.RowIndex > _allUoms.Count)
            {
                return;
            }

            if (e.RowIndex == _allUoms.Count)
            {
                var newUom = new UnitOfMeasure();
                _allUoms.Add(newUom);
                _baseUoms.Add(newUom);

                AryaTools.Instance.InstanceData.Dc.UnitOfMeasures.InsertOnSubmit(newUom);
            }
            var uom        = _allUoms[e.RowIndex];
            var headerText = dgvUnitOfMeasure.Columns[e.ColumnIndex].HeaderText.Replace(" ", string.Empty);
            var pi         = uom.GetType().GetProperty(headerText);

            if (pi.PropertyType == typeof(Int32?))  //Precision is integer
            {
                int val;
                if (int.TryParse(e.Value.ToString(), out val))
                {
                    pi.SetValue(uom, val);
                }
            }
            else if (headerText == "BaseUom")
            {
                if (e.Value.ToString() == _emptyUom.ToString())
                {
                    //set as base uom
                    pi.SetValue(uom, null);
                    //add to base uom list if not present in there
                    if (!_baseUoms.Contains(uom))
                    {
                        _baseUoms.Add(uom);
                    }
                }
                else
                {
                    var currentBaseUom = _baseUoms.First(bu => bu.ToString() == e.Value.ToString());
                    pi.SetValue(uom, currentBaseUom);
                    if (_baseUoms.Contains(uom))
                    {
                        _baseUoms.Remove(uom);
                    }
                }

                dgvUnitOfMeasure.InvalidateRow(e.RowIndex);
            }
            else if (headerText == "IsBaseUom" && Convert.ToBoolean(e.Value))
            {
                pi = uom.GetType().GetProperty("BaseUom");
                pi.SetValue(uom, null);
                //dgvUnitOfMeasure.Rows[e.RowIndex].Cells[3].ReadOnly = true;
                dgvUnitOfMeasure.InvalidateRow(e.RowIndex);
            }
            else if (headerText == "IsBaseUom" && !Convert.ToBoolean(e.Value))
            {
                pi = uom.GetType().GetProperty("BaseUom");
                pi.SetValue(uom, _baseUoms.ElementAt(1));
                //dgvUnitOfMeasure.Rows[e.RowIndex].Cells[3].ReadOnly = false;
                dgvUnitOfMeasure.InvalidateRow(e.RowIndex);
            }
            else
            {
                pi.SetValue(uom, e.Value);
            }

            AryaTools.Instance.SaveChangesIfNecessary(false, false);
            dgvUnitOfMeasure.RowCount = _allUoms.Count + 1;
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Converts the number of source units to target units.
 /// </summary>
 /// <param name="aUnits">A number of units.</param>
 /// <param name="aFromUnitOfMeasure">A from unit of measure.</param>
 /// <param name="aToUnitOfMeasure">A to unit of measure.</param>
 /// <returns></returns>
 public static double Convert(double aUnits, UnitOfMeasure aFromUnitOfMeasure, UnitOfMeasure aToUnitOfMeasure)
 {
     var vEnumType = typeof(UnitOfMeasure);
     //
     var vFromEnumInfo = vEnumType.GetMember(aFromUnitOfMeasure.ToString());
     var vFromEnumAttributes = vFromEnumInfo[0].GetCustomAttributes(typeof(UnitOfMeasureAttribute), false);
     var vFromBaseUnit = ((UnitOfMeasureAttribute)vFromEnumAttributes[0]).Base;
     var vFromEquals = ((UnitOfMeasureAttribute)vFromEnumAttributes[0]).IsEqualTo;
     //
     var vToEnumInfo = vEnumType.GetMember(aToUnitOfMeasure.ToString());
     var vToEnumAttributes = vToEnumInfo[0].GetCustomAttributes(typeof(UnitOfMeasureAttribute), false);
     var vToBaseUnit = ((UnitOfMeasureAttribute)vToEnumAttributes[0]).Base;
     var vToEquals = ((UnitOfMeasureAttribute)vToEnumAttributes[0]).IsEqualTo;
     //
     if (vToBaseUnit != vFromBaseUnit)
     {
         throw new ArgumentException(String.Format("Cannot convert incompatible UnitsOfMeasure {0} and {1}", aFromUnitOfMeasure, aToUnitOfMeasure));
     }
     //
     return (aUnits / vFromEquals) * vToEquals;
 }