Quantity
An object representing a property which is measurable. A Quantity can be defined or derived from other quantities.
using Autodesk.ForgeUnits;
using System;
using System.Collections.Generic;
namespace DynamoUnits
{
public class Quantity
{
internal readonly Quantity forgeQuantity;
public string TypeId => forgeQuantity.getTypeId();
public string Name => forgeQuantity.getName();
public IEnumerable<Unit> Units {
get {
List<Unit> units = forgeQuantity.getUnits();
List<Unit> list = new List<Unit>();
foreach (Unit item in units) {
list.Add(new Unit(item));
}
return list;
}
}
internal Quantity(Quantity quantity)
{
if (quantity == null)
throw new ArgumentNullException();
forgeQuantity = quantity;
}
public static Quantity ByTypeID(string typeId)
{
try {
return new Quantity(Utilities.ForgeUnitsEngine.getQuantity(typeId));
} catch (Exception) {
if (Utilities.TryParseTypeId(typeId, out string typeName, out Version _)) {
Dictionary<string, Version> allRegisteredQuantityVersions = Utilities.GetAllRegisteredQuantityVersions();
if (allRegisteredQuantityVersions.TryGetValue(typeName, out Version value))
return new Quantity(Utilities.ForgeUnitsEngine.getQuantity(typeName + "-" + value.ToString()));
}
throw;
}
}
public override string ToString()
{
return "Quantity(Name = " + Name + ")";
}
public override int GetHashCode()
{
return (Name ?? string.Empty).GetHashCode() ^ (TypeId ?? string.Empty).GetHashCode();
}
public override bool Equals(object obj)
{
return EqualsImpl(obj as Quantity);
}
internal bool EqualsImpl(Quantity q)
{
if ((object)q == null)
return false;
if ((object)this == q)
return true;
if (GetType() != q.GetType())
return false;
if (Name == q.Name)
return TypeId == q.TypeId;
return false;
}
public static bool operator ==(Quantity lhs, Quantity rhs)
{
if ((object)lhs == null) {
if ((object)rhs == null)
return true;
return false;
}
return lhs.EqualsImpl(rhs);
}
public static bool operator !=(Quantity lhs, Quantity rhs)
{
return !(lhs == rhs);
}
}
}