Volume
using Autodesk.DesignScript.Runtime;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace DynamoUnits
{
[IsVisibleInDynamoLibrary(false)]
public class Volume : SIUnit, IComparable, IEquatable<Volume>
{
private const double CUBIC_METERS_TO_CUBIC_MILLIMETERS = 1000000000;
private const double CUBIC_METERS_TO_CUBIC_CENTIMETERS = 1000000;
private const double CUBIC_METERS_TO_CUBIC_INCHES = 61023.744095;
private const double CUBIC_METERS_TO_CUBIC_FEET = 35.3147;
public const string CUBIC_METERS = "m³";
public const string CUBIC_MILLIMETERS = "mm³";
public const string CUBIC_CENTIMETERS = "cm³";
public const string CUBIC_INCHES = "in³";
public const string CUBIC_FEET = "ft³";
private VolumeUnit _volumeUnit = VolumeUnit.CubicMeter;
[IsVisibleInDynamoLibrary(false)]
public VolumeUnit VolumeUnit {
get {
return _volumeUnit;
}
set {
_volumeUnit = value;
}
}
[IsVisibleInDynamoLibrary(false)]
public double UiVolumeConversion {
get {
switch (_volumeUnit) {
case VolumeUnit.CubicMillimeter:
return ToCubicMillimeter;
case VolumeUnit.CubicCentimeter:
return ToCubicCentimeter;
case VolumeUnit.CubicMeter:
return 1;
case VolumeUnit.CubicInch:
return ToCubicInch;
case VolumeUnit.CubicFoot:
return ToCubicFoot;
default:
return 1;
}
}
}
[IsVisibleInDynamoLibrary(false)]
public override double UnitValue {
get {
return _value * UiVolumeConversion;
}
}
[Obsolete("Volume.ToCubicMillimeter is obsolete. Please use Convert Units.", false)]
public static double ToCubicMillimeter {
get {
return 1000000000;
}
}
[Obsolete("Volume.ToCubicCentimeter is obsolete. Please use Convert Units.", false)]
public static double ToCubicCentimeter {
get {
return 1000000;
}
}
[Obsolete("Volume.ToCubicInch is obsolete. Please use Convert Units.", false)]
public static double ToCubicInch {
get {
return 61023.744095;
}
}
[Obsolete("Volume.ToCubicFoot is obsolete. Please use Convert Units.", false)]
public static double ToCubicFoot {
get {
return 35.3147;
}
}
public new static Dictionary<string, double> Conversions => new Dictionary<string, double> {
{
"m³",
1
},
{
"mm³",
1000000000
},
{
"cm³",
1000000
},
{
"in³",
61023.744095
},
{
"ft³",
35.3147
}
};
internal Volume(double value)
: base(value)
{
}
internal Volume(double value, VolumeUnit unit)
: base(value)
{
VolumeUnit = unit;
}
[Obsolete("Volume.FromDouble is obsolete. Please use Number.", false)]
public static Volume FromDouble(double value)
{
return new Volume(value);
}
[IsVisibleInDynamoLibrary(false)]
public static Volume FromDouble(double value, VolumeUnit unit)
{
return new Volume(value, unit);
}
[Obsolete("Volume.FromCubicFeet is obsolete. Please use Number.", false)]
public static Volume FromCubicFeet(double value)
{
return new Volume(value / ToCubicFoot);
}
[Obsolete("Volume.Add is obsolete. Please use + instead.", false)]
public override SIUnit Add(SIUnit x)
{
if (x is Volume)
return new Volume(_value + x.Value);
throw new UnitsException(GetType(), x.GetType());
}
[Obsolete("Volume.Add is obsolete. Please use + instead.", false)]
public override SIUnit Add(double x)
{
return new Volume(_value + x);
}
[Obsolete("Volume.Subtract is obsolete. Please use - instead.", false)]
public override SIUnit Subtract(SIUnit x)
{
if (x is Volume)
return new Volume(_value - x.Value);
throw new UnitsException(GetType(), x.GetType());
}
[Obsolete("Volume.Subtract is obsolete. Please use - instead.", false)]
public override SIUnit Subtract(double x)
{
return new Volume(_value - x);
}
[Obsolete("Volume.Multiply is obsolete. Please use * instead.", false)]
public override SIUnit Multiply(SIUnit x)
{
throw new UnitsException(GetType(), x.GetType());
}
[Obsolete("Volume.Multiply is obsolete. Please use * instead.", false)]
public override SIUnit Multiply(double x)
{
return new Volume(_value * x);
}
[Obsolete("Volume.Multiply is obsolete. Please use / instead.", false)]
public override dynamic Divide(SIUnit x)
{
if (x is Length)
return new Area(_value / x.Value);
if (x is Area)
return new Length(_value / x.Value);
throw new UnitsException(GetType(), x.GetType());
}
[Obsolete("Volume.Multiply is obsolete. Please use / instead.", false)]
public override SIUnit Divide(double x)
{
return new Volume(_value / x);
}
[Obsolete("Volume.Multiply is obsolete. Please use % instead.", false)]
public override SIUnit Modulo(SIUnit x)
{
if (x is Volume)
return new Volume(_value % x.Value);
throw new UnitsException(GetType(), x.GetType());
}
[Obsolete("Volume.Multiply is obsolete. Please use % instead.", false)]
public override SIUnit Modulo(double x)
{
return new Volume(_value % x);
}
[Obsolete("Volume.Multiply is obsolete. Please use Round instead.", false)]
public override SIUnit Round()
{
return new Volume(Math.Round(_value * UiVolumeConversion) / UiVolumeConversion);
}
[Obsolete("Volume.Multiply is obsolete. Please use Ceiling instead.", false)]
public override SIUnit Ceiling()
{
return new Volume(Math.Ceiling(_value * UiVolumeConversion) / UiVolumeConversion);
}
[Obsolete("Volume.Multiply is obsolete. Please use Floor instead.", false)]
public override SIUnit Floor()
{
return new Volume(Math.Floor(_value * UiVolumeConversion) / UiVolumeConversion);
}
[Obsolete("Volume.ConvertToHostUnits is obsolete. Please use Convert Between Units.", false)]
public override double ConvertToHostUnits()
{
VolumeUnit volumeUnit = VolumeUnit;
if (volumeUnit == VolumeUnit.CubicFoot)
return _value / ToCubicFoot;
return _value;
}
[Obsolete("Volume.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 / UiVolumeConversion;
} else {
Utils.ParseVolumeFromString(value, out double cubic_inch, out double cubic_foot, out double cubic_millimeter, out double cubic_centimeter, out double cubic_meter);
result += cubic_millimeter / ToCubicMillimeter;
result += cubic_centimeter / ToCubicCentimeter;
result += cubic_meter;
result += cubic_inch / ToCubicInch;
result += cubic_foot / ToCubicFoot;
_value = ((result < 0) ? 0 : result);
}
}
[IsVisibleInDynamoLibrary(false)]
public bool Equals(Volume 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(VolumeUnit);
}
private string BuildString(VolumeUnit unit)
{
double num;
switch (unit) {
case VolumeUnit.CubicMillimeter:
num = _value * ToCubicMillimeter;
return num.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "mm³";
case VolumeUnit.CubicCentimeter:
num = _value * ToCubicCentimeter;
return num.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "cm³";
case VolumeUnit.CubicMeter:
return _value.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "m³";
case VolumeUnit.CubicInch:
num = _value * ToCubicInch;
return num.ToString(BaseUnit.NumberFormat, CultureInfo.InvariantCulture) + "in³";
case VolumeUnit.CubicFoot:
num = _value * ToCubicFoot;
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;
Volume volume = obj as Volume;
if (volume != null)
return _value.CompareTo(volume.Value);
throw new ArgumentException("Object is not a Volume");
}
[IsVisibleInDynamoLibrary(false)]
public override bool Equals(object obj)
{
if (obj == null)
return false;
Volume volume = obj as Volume;
if (volume == null)
return false;
return Math.Abs(volume.Value - _value) < BaseUnit.Epsilon;
}
[IsVisibleInDynamoLibrary(false)]
public override int GetHashCode()
{
return Convert.ToInt32(_value);
}
}
}