Esempio n. 1
0
		static LengthUnit()
		{
			_millimeter = new LengthUnit(((decimal)1) / 1000, "Millimeter", "mm");
			_centimeter = new LengthUnit(((decimal)1) / 100, "Centimeter", "cm");
			_mil = new LengthUnit(((decimal)254) / 10000000, "Mil", "mil");
			_point = new LengthUnit(((decimal)254) / 720000, "Point", "pt");
			_inch = new LengthUnit(((decimal)254) / 10000, "Inch", "\"");

			_shortcutToUnit = new SortedDictionary<string, LengthUnit>();
			_shortcutToUnit.Add(_millimeter.Shortcut, _millimeter);
			_shortcutToUnit.Add(_centimeter.Shortcut, _centimeter);
			_shortcutToUnit.Add(_mil.Shortcut, _mil);
			_shortcutToUnit.Add(_point.Shortcut, _point);
			_shortcutToUnit.Add(_inch.Shortcut, _inch);

			// Alternative shortcuts
			_shortcutToUnit.Add("Mil", _mil);
			_shortcutToUnit.Add("Inch", _inch);
			_shortcutToUnit.Add("inch", _inch);

			_shortcuts = new List<string>();
			foreach (string k in _shortcutToUnit.Keys)
				_shortcuts.Add(k);
			_shortcuts.Sort();
		}
Esempio n. 2
0
 public double ConvertFrom(double fromlength, LengthUnit fromunit)
 {
   return fromlength * (double)(fromunit.UnitInMeter / this.UnitInMeter);
 }
Esempio n. 3
0
        /// <summary>
        /// Converts a value (unit: points) in a given unit and returns it as text together with the unit.
        /// </summary>
        /// <param name="value">Value of length in points.</param>
        /// <param name="lastUnit">The unit to convert to.</param>
        /// <returns>A text string: the value together with the unit.</returns>
        public static string GetLengthMeasureText(double value, LengthUnit lastUnit)
        {
            double v = lastUnit.ConvertFrom(value, LengthUnit.Point);

            return(GUIConversion.ToString(v, "G5") + " " + lastUnit.Shortcut);
        }
Esempio n. 4
0
 public double ConvertFrom(double fromlength, LengthUnit fromunit)
 {
     return(fromlength * (double)(fromunit.UnitInMeter / this.UnitInMeter));
 }
Esempio n. 5
0
		/// <summary>
		/// Get a length value from a text string.
		/// </summary>
		/// <param name="txt">Text string. Consists of a number and optionally a unit.</param>
		/// <param name="unit">Gives the default unit to use if the text string don't contain a unit.
		/// On return, contains the unit actually used.</param>
		/// <param name="value">On return, gives the actual length (unit:points).</param>
		/// <returns>True if the conversion was successful, false otherwise.</returns>
		public static bool GetLengthMeasureValue(
			string txt,
			ref LengthUnit unit,
			ref double value)
		{
			txt = txt.Trim().ToLower();
			LengthUnit tempUnit = unit;
			foreach (string end in LengthUnit.Shortcuts)
			{
				if (txt.EndsWith(end))
				{
					tempUnit = LengthUnit.FromShortcut(end);
					txt = txt.Substring(0, txt.Length - end.Length).TrimEnd();
					break;
				}
			}

			double v;
			if (IsDouble(txt, out v))
			{
				value = LengthUnit.Point.ConvertFrom(v, tempUnit);
				unit = tempUnit;
				return true;
			}
			else
			{
				return false;
			}
		}
Esempio n. 6
0
		/// <summary>
		/// Converts a value (unit: points) in a given unit and returns it as text together with the unit.
		/// </summary>
		/// <param name="value">Value of length in points.</param>
		/// <param name="lastUnit">The unit to convert to.</param>
		/// <returns>A text string: the value together with the unit.</returns>
		public static string GetLengthMeasureText(double value, LengthUnit lastUnit)
		{
			double v = lastUnit.ConvertFrom(value, LengthUnit.Point);
			return GUIConversion.ToString(v, "G5") + " " + lastUnit.Shortcut;
		}
Esempio n. 7
0
		/// <summary>
		/// Parse a string that ends with a length unit to return the length unit. The string is trimmed at the end before use.
		/// </summary>
		/// <param name="s">String to parse.</param>
		/// <param name="lengthUnit">On success, returns the length unit parsed.</param>
		/// <param name="remainder">On success, returns the part of the input string, which belongs not to the length unit.</param>
		/// <returns>True if successfull, otherwise false.</returns>
		public static bool TryParse(string s, out LengthUnit lengthUnit, out string remainder)
		{
			s = s.TrimEnd();

			for (int i = _shortcuts.Count - 1; i >= 0; i--)
			{
				if (s.EndsWith(_shortcuts[i]))
				{
					lengthUnit = _shortcutToUnit[_shortcuts[i]];
					remainder = s.Substring(0, s.Length - _shortcuts[i].Length);
					return true;
				}
			}

			lengthUnit = null;
			remainder = null;
			return false;
		}