Skip to content

Commit df9e1b0

Browse files
authored
add units example (#52)
* add units example * Update build.yml
1 parent 25791ca commit df9e1b0

File tree

4 files changed

+170
-5
lines changed

4 files changed

+170
-5
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ jobs:
4949
"${{ github.workspace }}\DynamoSamples\dynamo_linter\Sample Linter\bin\SampleLinter.dll",
5050
"${{ github.workspace }}\DynamoSamples\dynamo_package\Dynamo Samples\bin\SampleLibraryUI.dll",
5151
"${{ github.workspace }}\DynamoSamples\dynamo_package\Dynamo Samples\bin\SampleLibraryZeroTouch.dll",
52+
"${{ github.workspace }}\DynamoSamples\dynamo_package\Dynamo Samples\bin\SampleZeroTouchUnits.dll",
5253
"${{ github.workspace }}\DynamoSamples\dynamo_viewExtension\Sample View Extension\bin\SampleViewExtension.dll"
5354
)
5455

src/DynamoSamples.sln

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.6.33815.320
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleLibraryTests", "SampleLibraryTests\SampleLibraryTests.csproj", "{933B8108-4E74-470A-86C7-4B7F633115B9}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleLibraryTests", "SampleLibraryTests\SampleLibraryTests.csproj", "{933B8108-4E74-470A-86C7-4B7F633115B9}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleLibraryUI", "SampleLibraryUI\SampleLibraryUI.csproj", "{0A4B4EEA-8FAB-4AC8-90D4-27DBC5B0CF2A}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleLibraryUI", "SampleLibraryUI\SampleLibraryUI.csproj", "{0A4B4EEA-8FAB-4AC8-90D4-27DBC5B0CF2A}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleLibraryZeroTouch", "SampleLibraryZeroTouch\SampleLibraryZeroTouch.csproj", "{BD13C4DC-9045-4E49-B637-B6182B0E3A7F}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleLibraryZeroTouch", "SampleLibraryZeroTouch\SampleLibraryZeroTouch.csproj", "{BD13C4DC-9045-4E49-B637-B6182B0E3A7F}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleViewExtension", "SampleViewExtension\SampleViewExtension.csproj", "{146EBF48-E7A0-4ABE-809D-D7F3059E4EE1}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleViewExtension", "SampleViewExtension\SampleViewExtension.csproj", "{146EBF48-E7A0-4ABE-809D-D7F3059E4EE1}"
1313
EndProject
14-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleExtension", "SampleExtension\SampleExtension.csproj", "{8B27B070-8434-49C8-8D43-41A4AE53BC36}"
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleExtension", "SampleExtension\SampleExtension.csproj", "{8B27B070-8434-49C8-8D43-41A4AE53BC36}"
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleLinter", "SampleLinter\SampleLinter.csproj", "{5F559FDB-99B9-4F4A-9A91-C9EC94C771D8}"
1717
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleZeroTouchUnits", "SampleZeroTouchUnits\SampleZeroTouchUnits.csproj", "{4F9ECB35-D321-482A-8ED4-CC8E9342AACE}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +47,10 @@ Global
4547
{5F559FDB-99B9-4F4A-9A91-C9EC94C771D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
4648
{5F559FDB-99B9-4F4A-9A91-C9EC94C771D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
4749
{5F559FDB-99B9-4F4A-9A91-C9EC94C771D8}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{4F9ECB35-D321-482A-8ED4-CC8E9342AACE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{4F9ECB35-D321-482A-8ED4-CC8E9342AACE}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{4F9ECB35-D321-482A-8ED4-CC8E9342AACE}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{4F9ECB35-D321-482A-8ED4-CC8E9342AACE}.Release|Any CPU.Build.0 = Release|Any CPU
4854
EndGlobalSection
4955
GlobalSection(SolutionProperties) = preSolution
5056
HideSolutionNode = FALSE
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<ImportGroup Label="PropertySheets">
3+
<Import Project="$(SolutionDir)Config\CS.props" />
4+
</ImportGroup>
5+
<PropertyGroup>
6+
<AppDesignerFolder>Properties</AppDesignerFolder>
7+
<RootNamespace>SampleZeroTouchUnits</RootNamespace>
8+
<AssemblyName>SampleZeroTouchUnits</AssemblyName>
9+
<!--EnableDynamicLoading prepares the project so that it can be used as a plugin -->
10+
<!--https://learn.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support#simple-plugin-with-no-dependencies-->
11+
<EnableDynamicLoading>true</EnableDynamicLoading>
12+
<DocumentationFile>bin\$(Configuration)\SampleZeroTouchUnits.XML</DocumentationFile>
13+
</PropertyGroup>
14+
<ItemGroup>
15+
<PackageReference Include="DynamoVisualProgramming.Core" Version="3.1.0-beta3755" ExcludeAssets="runtime" />
16+
<PackageReference Include="DynamoVisualProgramming.DynamoServices" Version="3.1.0-beta3755" ExcludeAssets="runtime" />
17+
<PackageReference Include="DynamoVisualProgramming.ZeroTouchLibrary" Version="3.1.0-beta3755" ExcludeAssets="runtime" />
18+
</ItemGroup>
19+
<Target Name="AfterBuildOps" AfterTargets="Build">
20+
<Message Text="Should run after build" Importance="High" />
21+
<ItemGroup>
22+
<PackageFiles Include="$(OutDir)\SampleZeroTouchUnits.dll;$(OutDir)\SampleZeroTouchUnits.XML;" />
23+
</ItemGroup>
24+
<Copy SourceFiles="@(PackageFiles)" DestinationFolder="$(SolutionDir)..\dynamo_package\Dynamo Samples\bin\" />
25+
</Target>
26+
</Project>

0 commit comments

Comments
 (0)