|
| 1 | +using Autodesk.DesignScript.Interfaces; |
| 2 | +using Autodesk.DesignScript.Runtime; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System; |
| 5 | +using DynamoUnits; |
| 6 | +using Dynamo.Graph.Nodes.CustomNodes; |
| 7 | + |
| 8 | +namespace SampleZeroTouchUnits |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// The RectangleExample class demonstrates |
| 12 | + /// how to use the Dynamo Units API to convert between units. |
| 13 | + /// </summary> |
| 14 | + public class RectangleExample |
| 15 | + { |
| 16 | + const string meters = "autodesk.unit.unit:meters"; |
| 17 | + const string meters2 = "autodesk.unit.unit:squareMeters"; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// The Length value |
| 21 | + /// </summary> |
| 22 | + private readonly double Length; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// The Width value |
| 26 | + /// </summary> |
| 27 | + private readonly double Width; |
| 28 | + |
| 29 | + private Unit LengthUnit; |
| 30 | + private Unit WidthUnit; |
| 31 | + private Unit AreaUnit; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// |
| 35 | + /// </summary> |
| 36 | + /// <param name="width"></param> |
| 37 | + /// <param name="length"></param> |
| 38 | + public RectangleExample(double width, double length) |
| 39 | + { |
| 40 | + Length = length; |
| 41 | + Width = width; |
| 42 | + LengthUnit = Unit.ByTypeID($"{meters}-1.0.1"); |
| 43 | + WidthUnit = Unit.ByTypeID($"{meters}-1.0.1"); |
| 44 | + AreaUnit = Unit.ByTypeID($"{meters2}-1.0.1"); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// |
| 49 | + /// </summary> |
| 50 | + /// <param name="width"></param> |
| 51 | + /// <param name="length"></param> |
| 52 | + /// <param name="widthUnit"></param> |
| 53 | + /// <param name="lengthUnit"></param> |
| 54 | + public RectangleExample(double width, double length, Unit widthUnit, Unit lengthUnit) |
| 55 | + { |
| 56 | + Width = width; |
| 57 | + Length = length; |
| 58 | + |
| 59 | + LengthUnit = lengthUnit; |
| 60 | + WidthUnit = widthUnit; |
| 61 | + |
| 62 | + AreaUnit = Unit.ByTypeID($"{meters2}-1.0.1"); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// |
| 67 | + /// </summary> |
| 68 | + /// <param name="targetUnit"></param> |
| 69 | + /// <returns></returns> |
| 70 | + /// <exception cref="ArgumentException"></exception> |
| 71 | + public double GetLength(Unit targetUnit = null) |
| 72 | + { |
| 73 | + targetUnit ??= LengthUnit; |
| 74 | + ArgumentNullException.ThrowIfNull(targetUnit); |
| 75 | + |
| 76 | + if (!Unit.AreUnitsConvertible(LengthUnit, targetUnit)) |
| 77 | + { |
| 78 | + throw new ArgumentException($"{LengthUnit} is not convertible to {targetUnit}"); |
| 79 | + } |
| 80 | + |
| 81 | + var output = Utilities.ConvertByUnits(Length, LengthUnit, targetUnit); |
| 82 | + return output; |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// |
| 87 | + /// </summary> |
| 88 | + /// <param name="targetUnit"></param> |
| 89 | + /// <returns></returns> |
| 90 | + /// <exception cref="ArgumentException"></exception> |
| 91 | + public double GetWidth(Unit targetUnit) |
| 92 | + { |
| 93 | + targetUnit ??= WidthUnit; |
| 94 | + ArgumentNullException.ThrowIfNull(targetUnit); |
| 95 | + if (!Unit.AreUnitsConvertible(WidthUnit, targetUnit)) |
| 96 | + { |
| 97 | + throw new ArgumentException($"{LengthUnit} is not convertible to {targetUnit}"); |
| 98 | + } |
| 99 | + |
| 100 | + var output = Utilities.ConvertByUnits(Length, WidthUnit, targetUnit); |
| 101 | + return output; |
| 102 | + } |
| 103 | + |
| 104 | + string GetFirstSymbolText(Unit unit) |
| 105 | + { |
| 106 | + var symbols = DynamoUnits.Symbol.SymbolsByUnit(unit); |
| 107 | + foreach (var symbol in symbols) |
| 108 | + { |
| 109 | + return symbol.Text; |
| 110 | + } |
| 111 | + return string.Empty; |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// |
| 116 | + /// </summary> |
| 117 | + /// <param name="targetUnit"></param> |
| 118 | + /// <returns></returns> |
| 119 | + /// <exception cref="ArgumentException"></exception> |
| 120 | + public string GetArea(Unit targetUnit = null) |
| 121 | + { |
| 122 | + targetUnit ??= AreaUnit; |
| 123 | + if (!Unit.AreUnitsConvertible(AreaUnit, targetUnit)) |
| 124 | + { |
| 125 | + throw new ArgumentException($"{targetUnit.Name} is not a valid area unit"); |
| 126 | + } |
| 127 | + |
| 128 | + double area = Utilities.ParseExpressionByUnit(targetUnit, $"{Length}{GetFirstSymbolText(LengthUnit)} * {Width}{GetFirstSymbolText(WidthUnit)}"); |
| 129 | + return $"{area}{GetFirstSymbolText(targetUnit)}"; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments