From df9e1b0354092e39e80fef7e53d195417a57b2b0 Mon Sep 17 00:00:00 2001
From: pinzart90 <46732933+pinzart90@users.noreply.github.com>
Date: Wed, 7 Feb 2024 09:29:07 -0500
Subject: [PATCH] add units example (#52)
* add units example
* Update build.yml
---
.github/workflows/build.yml | 1 +
src/DynamoSamples.sln | 16 ++-
src/SampleZeroTouchUnits/RectangleExample.cs | 132 ++++++++++++++++++
.../SampleZeroTouchUnits.csproj | 26 ++++
4 files changed, 170 insertions(+), 5 deletions(-)
create mode 100644 src/SampleZeroTouchUnits/RectangleExample.cs
create mode 100644 src/SampleZeroTouchUnits/SampleZeroTouchUnits.csproj
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 6244a8b..3748c0a 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -49,6 +49,7 @@ jobs:
"${{ github.workspace }}\DynamoSamples\dynamo_linter\Sample Linter\bin\SampleLinter.dll",
"${{ github.workspace }}\DynamoSamples\dynamo_package\Dynamo Samples\bin\SampleLibraryUI.dll",
"${{ github.workspace }}\DynamoSamples\dynamo_package\Dynamo Samples\bin\SampleLibraryZeroTouch.dll",
+ "${{ github.workspace }}\DynamoSamples\dynamo_package\Dynamo Samples\bin\SampleZeroTouchUnits.dll",
"${{ github.workspace }}\DynamoSamples\dynamo_viewExtension\Sample View Extension\bin\SampleViewExtension.dll"
)
diff --git a/src/DynamoSamples.sln b/src/DynamoSamples.sln
index a9a1d9a..cdd718b 100644
--- a/src/DynamoSamples.sln
+++ b/src/DynamoSamples.sln
@@ -3,18 +3,20 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33815.320
MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleLibraryTests", "SampleLibraryTests\SampleLibraryTests.csproj", "{933B8108-4E74-470A-86C7-4B7F633115B9}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleLibraryTests", "SampleLibraryTests\SampleLibraryTests.csproj", "{933B8108-4E74-470A-86C7-4B7F633115B9}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleLibraryUI", "SampleLibraryUI\SampleLibraryUI.csproj", "{0A4B4EEA-8FAB-4AC8-90D4-27DBC5B0CF2A}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleLibraryUI", "SampleLibraryUI\SampleLibraryUI.csproj", "{0A4B4EEA-8FAB-4AC8-90D4-27DBC5B0CF2A}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleLibraryZeroTouch", "SampleLibraryZeroTouch\SampleLibraryZeroTouch.csproj", "{BD13C4DC-9045-4E49-B637-B6182B0E3A7F}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleLibraryZeroTouch", "SampleLibraryZeroTouch\SampleLibraryZeroTouch.csproj", "{BD13C4DC-9045-4E49-B637-B6182B0E3A7F}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleViewExtension", "SampleViewExtension\SampleViewExtension.csproj", "{146EBF48-E7A0-4ABE-809D-D7F3059E4EE1}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleViewExtension", "SampleViewExtension\SampleViewExtension.csproj", "{146EBF48-E7A0-4ABE-809D-D7F3059E4EE1}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleExtension", "SampleExtension\SampleExtension.csproj", "{8B27B070-8434-49C8-8D43-41A4AE53BC36}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleExtension", "SampleExtension\SampleExtension.csproj", "{8B27B070-8434-49C8-8D43-41A4AE53BC36}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleLinter", "SampleLinter\SampleLinter.csproj", "{5F559FDB-99B9-4F4A-9A91-C9EC94C771D8}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleZeroTouchUnits", "SampleZeroTouchUnits\SampleZeroTouchUnits.csproj", "{4F9ECB35-D321-482A-8ED4-CC8E9342AACE}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +47,10 @@ Global
{5F559FDB-99B9-4F4A-9A91-C9EC94C771D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5F559FDB-99B9-4F4A-9A91-C9EC94C771D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5F559FDB-99B9-4F4A-9A91-C9EC94C771D8}.Release|Any CPU.Build.0 = Release|Any CPU
+ {4F9ECB35-D321-482A-8ED4-CC8E9342AACE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {4F9ECB35-D321-482A-8ED4-CC8E9342AACE}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {4F9ECB35-D321-482A-8ED4-CC8E9342AACE}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {4F9ECB35-D321-482A-8ED4-CC8E9342AACE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/src/SampleZeroTouchUnits/RectangleExample.cs b/src/SampleZeroTouchUnits/RectangleExample.cs
new file mode 100644
index 0000000..a7be5fb
--- /dev/null
+++ b/src/SampleZeroTouchUnits/RectangleExample.cs
@@ -0,0 +1,132 @@
+using Autodesk.DesignScript.Interfaces;
+using Autodesk.DesignScript.Runtime;
+using System.Collections.Generic;
+using System;
+using DynamoUnits;
+using Dynamo.Graph.Nodes.CustomNodes;
+
+namespace SampleZeroTouchUnits
+{
+ ///
+ /// The RectangleExample class demonstrates
+ /// how to use the Dynamo Units API to convert between units.
+ ///
+ public class RectangleExample
+ {
+ const string meters = "autodesk.unit.unit:meters";
+ const string meters2 = "autodesk.unit.unit:squareMeters";
+
+ ///
+ /// The Length value
+ ///
+ private readonly double Length;
+
+ ///
+ /// The Width value
+ ///
+ private readonly double Width;
+
+ private Unit LengthUnit;
+ private Unit WidthUnit;
+ private Unit AreaUnit;
+
+ ///
+ ///
+ ///
+ ///
+ ///
+ public RectangleExample(double width, double length)
+ {
+ Length = length;
+ Width = width;
+ LengthUnit = Unit.ByTypeID($"{meters}-1.0.1");
+ WidthUnit = Unit.ByTypeID($"{meters}-1.0.1");
+ AreaUnit = Unit.ByTypeID($"{meters2}-1.0.1");
+ }
+
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public RectangleExample(double width, double length, Unit widthUnit, Unit lengthUnit)
+ {
+ Width = width;
+ Length = length;
+
+ LengthUnit = lengthUnit;
+ WidthUnit = widthUnit;
+
+ AreaUnit = Unit.ByTypeID($"{meters2}-1.0.1");
+ }
+
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public double GetLength(Unit targetUnit = null)
+ {
+ targetUnit ??= LengthUnit;
+ ArgumentNullException.ThrowIfNull(targetUnit);
+
+ if (!Unit.AreUnitsConvertible(LengthUnit, targetUnit))
+ {
+ throw new ArgumentException($"{LengthUnit} is not convertible to {targetUnit}");
+ }
+
+ var output = Utilities.ConvertByUnits(Length, LengthUnit, targetUnit);
+ return output;
+ }
+
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public double GetWidth(Unit targetUnit)
+ {
+ targetUnit ??= WidthUnit;
+ ArgumentNullException.ThrowIfNull(targetUnit);
+ if (!Unit.AreUnitsConvertible(WidthUnit, targetUnit))
+ {
+ throw new ArgumentException($"{LengthUnit} is not convertible to {targetUnit}");
+ }
+
+ var output = Utilities.ConvertByUnits(Length, WidthUnit, targetUnit);
+ return output;
+ }
+
+ string GetFirstSymbolText(Unit unit)
+ {
+ var symbols = DynamoUnits.Symbol.SymbolsByUnit(unit);
+ foreach (var symbol in symbols)
+ {
+ return symbol.Text;
+ }
+ return string.Empty;
+ }
+
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public string GetArea(Unit targetUnit = null)
+ {
+ targetUnit ??= AreaUnit;
+ if (!Unit.AreUnitsConvertible(AreaUnit, targetUnit))
+ {
+ throw new ArgumentException($"{targetUnit.Name} is not a valid area unit");
+ }
+
+ double area = Utilities.ParseExpressionByUnit(targetUnit, $"{Length}{GetFirstSymbolText(LengthUnit)} * {Width}{GetFirstSymbolText(WidthUnit)}");
+ return $"{area}{GetFirstSymbolText(targetUnit)}";
+ }
+ }
+}
diff --git a/src/SampleZeroTouchUnits/SampleZeroTouchUnits.csproj b/src/SampleZeroTouchUnits/SampleZeroTouchUnits.csproj
new file mode 100644
index 0000000..b739b86
--- /dev/null
+++ b/src/SampleZeroTouchUnits/SampleZeroTouchUnits.csproj
@@ -0,0 +1,26 @@
+
+
+
+
+
+ Properties
+ SampleZeroTouchUnits
+ SampleZeroTouchUnits
+
+
+ true
+ bin\$(Configuration)\SampleZeroTouchUnits.XML
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file