Area
using Autodesk.DesignScript.Runtime;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace DynamoUnits
{
[IsVisibleInDynamoLibrary(false)]
public class Area : SIUnit, IComparable, IEquatable<Area>
{
private const double SQUARE_METERS_TO_SQUARE_MILLIMETERS = 1000000;
private const double SQUARE_METERS_TO_SQUARE_CENTIMETERS = 10000;
private const double SQUARE_METERS_TO_SQUARE_INCH = 1550.0031;
private const double SQUARE_METERS_TO_SQUARE_FOOT = 10.763910417;
public const string SQUARE_METERS = "m²";
public const string SQUARE_MILLIMETERS = "mm²";
public const string SQUARE_CENTIMETERS = "cm²";
public const string SQUARE_INCHES = "in²";
public const string SQUARE_FEET = "ft²";
private AreaUnit _areaUnit = AreaUnit.SquareMeter;
[IsVisibleInDynamoLibrary(false)]
public AreaUnit AreaUnit {
get {
return _areaUnit;
}
set {
_areaUnit = value;
}
}
[IsVisibleInDynamoLibrary(false)]
public double UiAreaConversion {
get {
switch (_areaUnit) {
case AreaUnit.SquareMillimeter:
return ToSquareMillimeters;
case AreaUnit.SquareCentimeter:
return ToSquareCentimeters;
case AreaUnit.SquareMeter:
return 1;
case AreaUnit.SquareInch:
return ToSquareInch;
case AreaUnit.SquareFoot:
return ToSquareFoot;
default:
return 1;
}
}
}
[IsVisibleInDynamoLibrary(false)]
public override double UnitValue {
get {
return _value * UiAreaConversion;
}
}
[Obsolete("Area.ToSquareMillimeters is obsolete. Please use Convert Units.", false)]
public static double ToSquareMillimeters {
get {
return 1000000;
}
}
[Obsolete("Area.ToSquareFoot is obsolete. Please use Convert Units.", false)]
public static double ToSquareCentimeters {
get {
return 10000;
}
}
[Obsolete("Area.ToSquareInch is obsolete. Please use Convert Units.", false)]
public static double ToSquareInch {
get {
return 1550.0031;
}
}
[Obsolete("Area.ToSquareFoot is obsolete. Please use Convert Units.", false)]
public static double ToSquareFoot {
get {
return 10.763910417;
}
}
public new static Dictionary<string, double> Conversions => new Dictionary<string, double> {
{
"m²",
1
},
{
"mm²",
1000000
},
{
"cm²",
10000
},
{
"in²",
1550.0031
},
{
"ft²",
10.763910417
}
};
internal Area(double value)
: base(value)
{
}
internal Area(double value, AreaUnit unit)
: base(value)
{
AreaUnit = unit;
}
[Obsolete("Area.FromDouble is obsolete. Please use Number.", false)]
public static Area FromDouble(double value)
{
return new Area(value);
}
[IsVisibleInDynamoLibrary(false)]
public static Area FromDouble(double value, AreaUnit unit)
{
return new Area(value, unit);
}
[Obsolete("Area.FromSquareFeet is obsolete. Please use Number.", false)]
public static Area FromSquareFeet(double value)
{
return new Area(value / ToSquareFoot);
}
[Obsolete("Area.Add is obsolete. Please use + instead.", false)]
public override SIUnit Add(SIUnit x)
{
if (x is Area)
return new Area(_value + x.Value);
throw new UnitsException(GetType(), x.GetType());
}
[Obsolete("Area.Add is obsolete. Please use + instead.", false)]
public override SIUnit Add(double x)
{
return new Area(_value + x);
}
[Obsolete("Area.Subtract is obsolete. Please use + instead.", false)]
public override SIUnit Subtract(SIUnit x)
{
if (x is Area)
return new Area(_value - x.Value);
throw new UnitsException(GetType(), x.GetType());
}
[Obsolete("Area.Subtract is obsolete. Please use + instead.", false)]
public override SIUnit Subtract(double x)
{
return new Area(_value - x);
}
[Obsolete("Area.Multiply is obsolete. Please use * instead.", false)]
public override SIUnit Multiply(SIUnit x)
{
if (x is Length)
return new Volume(_value * x.Value);
throw new UnitsException(GetType(), x.GetType());
}
[Obsolete("Area.Multiply is obsolete. Please use * instead.", false)]
public override SIUnit Multiply(double x)
{
return new Area(_value * x);
}
[Obsolete("Area.Divide is obsolete. Please use / instead.", false)]
public override dynamic Divide(SIUnit x)
{
if (x is Area)
return _value / x.Value;
if (x is Length)
return new Length(_value / x.Value);
throw new UnitsException(GetType(), x.GetType());
}
[Obsolete("Area.Divide is obsolete. Please use / instead.", false)]
public override SIUnit Divide(double x)
{
return new Area(_value / x);
}
[Obsolete("Area.Modulo is obsolete. Please use % instead.", false)]
public override SIUnit Modulo(SIUnit x)
{
if (x is Area)
return new Area(_value % x.Value);
throw new UnitsException(GetType(), x.GetType());
}
[Obsolete("Area.Modulo is obsolete. Please use % instead.", false)]
public override SIUnit Modulo(double x)
{
return new Area(_value % x);
}
[Obsolete("Area.Round is obsolete. Please use Round instead.", false)]
public override SIUnit Round()
{
return new Area(Math.Round(_value * UiAreaConversion) / UiAreaConversion);
}
[Obsolete("Area.Ceiling is obsolete. Please use Ceiling instead.", false)]
public override SIUnit Ceiling()
{
return new Area(Math.Ceiling(_value * UiAreaConversion) / UiAreaConversion);
}
[Obsolete("Area.Floor is obsolete. Please use Floor instead.", false)]
public override SIUnit Floor()
{
return new Area(Math.Floor(_value * UiAreaConversion) / UiAreaConversion);
}
[Obsolete("Area.ConvertToHostUnits is obsolete. Please use Convert Between Units.", false)]
public override double ConvertToHostUnits()
{
AreaUnit hostApplicationInternalAreaUnit = BaseUnit.HostApplicationInternalAreaUnit;
if (hostApplicationInternalAreaUnit == AreaUnit.SquareFoot)
return _value / ToSquareFoot;
return _value;
}
[Obsolete("Area.SetValueFromString is obsolete.", false)]
public override void SetValueFromString(string value)
{
double result = 0;
if (double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out result)) {
double num = _value = result / UiAreaConversion;
} else {
Utils.ParseAreaFromString(value, out double square_inch, out double square_foot, out double square_millimeter, out double square_centimeter, out double square_meter);
result += square_millimeter / ToSquareMillimeters;
result += square_centimeter / ToSquareCentimeters;
result += square_meter;
result += square_inch / ToSquareInch;
result += square_foot / ToSquareFoot;
_value = ((result < 0) ? 0 : result);
}
}
[IsVisibleInDynamoLibrary(false)]
public bool Equals(Area 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(AreaUnit);
}
private string BuildString(AreaUnit unit)
{
double num;
switch (unit) {
case AreaUnit.SquareMillimeter:
num = _value * ToSquareMillimeters;
return num.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "mm²";
case AreaUnit.SquareCentimeter:
num = _value * ToSquareCentimeters;
return num.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "cm²";
case AreaUnit.SquareMeter:
return _value.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "m²";
case AreaUnit.SquareInch:
num = _value * ToSquareInch;
return num.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "in²";
case AreaUnit.SquareFoot:
num = _value * ToSquareFoot;
return num.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "ft²";
default:
return _value.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "m²";
}
}
[IsVisibleInDynamoLibrary(false)]
public int CompareTo(object obj)
{
if (obj == null)
return 1;
Area area = obj as Area;
if (area != null)
return _value.CompareTo(area.Value);
throw new ArgumentException("Object is not an Area");
}
[IsVisibleInDynamoLibrary(false)]
public override bool Equals(object obj)
{
if (obj == null)
return false;
Area area = obj as Area;
if (area == null)
return false;
return Math.Abs(area.Value - _value) < BaseUnit.Epsilon;
}
[IsVisibleInDynamoLibrary(false)]
public override int GetHashCode()
{
return Convert.ToInt32(_value);
}
}
}