Length
using Autodesk.DesignScript.Runtime;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace DynamoUnits
{
[IsVisibleInDynamoLibrary(false)]
public class Length : SIUnit, IComparable, IEquatable<Length>
{
private const double METER_TO_MILLIMETER = 1000;
private const double METER_TO_CENTIMETER = 100;
private const double METER_TO_INCH = 39.37007874;
private const double METER_TO_FOOT = 3.280839895;
public const string METERS = "m";
public const string MILLIMETERS = "mm";
public const string CENTIMETERS = "cm";
public const string INCHES = "in";
public const string FEET = "ft";
private LengthUnit _lengthUnit = LengthUnit.Meter;
[IsVisibleInDynamoLibrary(false)]
public LengthUnit LengthUnit {
get {
return _lengthUnit;
}
set {
_lengthUnit = value;
}
}
[IsVisibleInDynamoLibrary(false)]
public double UiLengthConversion {
get {
switch (_lengthUnit) {
case LengthUnit.Millimeter:
return ToMillimeter;
case LengthUnit.Centimeter:
return ToCentimeter;
case LengthUnit.Meter:
return 1;
case LengthUnit.DecimalInch:
return ToInch;
case LengthUnit.FractionalInch:
return ToInch;
case LengthUnit.DecimalFoot:
return ToFoot;
case LengthUnit.FractionalFoot:
return ToFoot;
default:
return 1;
}
}
}
[IsVisibleInDynamoLibrary(false)]
public override double UnitValue {
get {
return _value * UiLengthConversion;
}
}
[Obsolete("Area.ToMillimeter is obsolete. Please use Convert Units.", false)]
public static double ToMillimeter {
get {
return 1000;
}
}
[Obsolete("Area.ToCentimeter is obsolete. Please use Convert Units.", false)]
public static double ToCentimeter {
get {
return 100;
}
}
[Obsolete("Area.ToMeter is obsolete. Please use Convert Units.", false)]
public static double ToMeter {
get {
return 1;
}
}
[Obsolete("Area.ToInch is obsolete. Please use Convert Units.", false)]
public static double ToInch {
get {
return 39.37007874;
}
}
[Obsolete("Area.ToFoot is obsolete. Please use Convert Units.", false)]
public static double ToFoot {
get {
return 3.280839895;
}
}
[IsVisibleInDynamoLibrary(false)]
public new static Dictionary<string, double> Conversions {
get {
return new Dictionary<string, double> {
{
"m",
1
},
{
"mm",
1000
},
{
"cm",
100
},
{
"in",
39.37007874
},
{
"ft",
3.280839895
}
};
}
}
internal Length(double value)
: base(value)
{
}
internal Length(double value, LengthUnit unit)
: base(value)
{
LengthUnit = unit;
}
[Obsolete("Length.FromDouble is obsolete. Please pass number values directly.", false)]
public static Length FromDouble(double value)
{
return new Length(value);
}
public static Length FromDouble(double value, LengthUnit unit)
{
return new Length(value, unit);
}
[Obsolete("Length.From feet is obsolete. Please pass number values directly.", false)]
public static Length FromFeet(double value)
{
return new Length(value / ToFoot);
}
public static Length FromFeet(double value, LengthUnit unit)
{
return new Length(value / ToFoot, unit);
}
[Obsolete("Length.Add is obsolete. Please use + instead.", false)]
public override SIUnit Add(SIUnit x)
{
if (x is Length)
return new Length(_value + x.Value);
throw new UnitsException(GetType(), x.GetType());
}
[Obsolete("Length.Add is obsolete. Please use + instead.", false)]
public override SIUnit Add(double x)
{
return new Length(_value + x);
}
[Obsolete("Length.Subtract is obsolete. Please use - instead.", false)]
public override SIUnit Subtract(SIUnit x)
{
if (x is Length)
return new Length(_value - x.Value);
throw new UnitsException(GetType(), x.GetType());
}
[Obsolete("Length.Subtract is obsolete. Please use - instead.", false)]
public override SIUnit Subtract(double x)
{
return new Length(_value - x);
}
[Obsolete("Length.Multiply is obsolete. Please use * instead.", false)]
public override SIUnit Multiply(SIUnit x)
{
if (x is Length)
return new Area(_value * x.Value);
if (x is Area)
return new Volume(_value * x.Value);
throw new UnitsException(GetType(), x.GetType());
}
[Obsolete("Length.Multiply is obsolete. Please use * instead.", false)]
public override SIUnit Multiply(double x)
{
return new Length(_value * x);
}
[Obsolete("Length.Divide is obsolete. Please use / instead.", false)]
public override dynamic Divide(SIUnit x)
{
if (x is Length)
return _value / x.Value;
throw new UnitsException(GetType(), x.GetType());
}
[Obsolete("Length.Divide is obsolete. Please use / instead.", false)]
public override SIUnit Divide(double x)
{
return new Length(_value / x);
}
[Obsolete("Length.Modulo is obsolete. Please use % instead.", false)]
public override SIUnit Modulo(SIUnit x)
{
if (x is Length)
return new Length(_value % x.Value);
throw new UnitsException(GetType(), x.GetType());
}
[Obsolete("Length.Modulo is obsolete. Please use % instead.", false)]
public override SIUnit Modulo(double x)
{
return new Length(_value % x);
}
[Obsolete("Length.Round is obsolete. Please use Round instead.", false)]
public override SIUnit Round()
{
return new Length(Math.Round(_value * UiLengthConversion) / UiLengthConversion) {
LengthUnit = LengthUnit
};
}
[Obsolete("Length.Ceiling is obsolete. Please use Ceiling instead.", false)]
public override SIUnit Ceiling()
{
return new Length(Math.Ceiling(_value * UiLengthConversion) / UiLengthConversion) {
LengthUnit = LengthUnit
};
}
[Obsolete("Length.Floor is obsolete. Please use Floor instead.", false)]
public override SIUnit Floor()
{
return new Length(Math.Floor(_value * UiLengthConversion) / UiLengthConversion) {
LengthUnit = LengthUnit
};
}
[Obsolete("Length.ConvertToHostUnits is obsolete. Please use Convert Between Units.", false)]
public override double ConvertToHostUnits()
{
LengthUnit hostApplicationInternalLengthUnit = BaseUnit.HostApplicationInternalLengthUnit;
if (hostApplicationInternalLengthUnit == LengthUnit.DecimalFoot)
return _value * ToFoot;
return _value;
}
[Obsolete("Length.SetValueFromString is obsolete.", false)]
public override void SetValueFromString(string value)
{
double num = 0;
if (Utils.ParseLengthInFeetFromString(value, out double feet, out double numerator, out double denominator)) {
num = ((feet != 0 || denominator == 0) ? feet : (numerator / denominator));
_value = num / UiLengthConversion;
} else {
double num2 = 0;
Utils.ParseLengthFromString(value, out feet, out double inch, out double m, out double cm, out double mm, out numerator, out denominator);
if (denominator != 0)
num2 = numerator / denominator;
num = ((!(feet < 0)) ? ((feet + inch / 12 + num2 / 12) / ToFoot) : ((feet - inch / 12 - num2 / 12) / ToFoot));
num += m;
num += cm / ToCentimeter;
num = (_value = num + mm / ToMillimeter);
}
}
[IsVisibleInDynamoLibrary(false)]
public bool Equals(Length other)
{
if (other == null)
return false;
if (Math.Abs(other.Value - _value) < BaseUnit.Epsilon)
return true;
return false;
}
[IsVisibleInDynamoLibrary(false)]
public override string ToString()
{
return BuildString(LengthUnit);
}
private string BuildString(LengthUnit unit)
{
double num;
switch (unit) {
case LengthUnit.Millimeter:
num = _value * ToMillimeter;
return num.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "mm";
case LengthUnit.Centimeter:
num = _value * ToCentimeter;
return num.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "cm";
case LengthUnit.Meter:
return _value.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "m";
case LengthUnit.DecimalInch:
num = _value * ToInch;
return num.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "in";
case LengthUnit.FractionalInch:
return Utils.ToFractionalInches(_value * ToInch);
case LengthUnit.DecimalFoot:
num = _value * ToFoot;
return num.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "ft";
case LengthUnit.FractionalFoot:
return Utils.ToFeetAndDecimalInches(_value * ToFoot);
default:
return _value.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "m";
}
}
[IsVisibleInDynamoLibrary(false)]
public int CompareTo(object obj)
{
if (obj == null)
return 1;
Length length = obj as Length;
if (length != null)
return _value.CompareTo(length.Value);
throw new ArgumentException("Object is not a Length");
}
[IsVisibleInDynamoLibrary(false)]
public override bool Equals(object obj)
{
if (obj == null)
return false;
Length length = obj as Length;
if (length == null)
return false;
return Math.Abs(length.Value - _value) < BaseUnit.Epsilon;
}
[IsVisibleInDynamoLibrary(false)]
public override int GetHashCode()
{
return Convert.ToInt32(_value);
}
}
}