Skip to content

Commit

Permalink
add units example (#52)
Browse files Browse the repository at this point in the history
* add units example

* Update build.yml
  • Loading branch information
pinzart90 authored Feb 7, 2024
1 parent 25791ca commit df9e1b0
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
16 changes: 11 additions & 5 deletions src/DynamoSamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
132 changes: 132 additions & 0 deletions src/SampleZeroTouchUnits/RectangleExample.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// The RectangleExample class demonstrates
/// how to use the Dynamo Units API to convert between units.
/// </summary>
public class RectangleExample
{
const string meters = "autodesk.unit.unit:meters";
const string meters2 = "autodesk.unit.unit:squareMeters";

/// <summary>
/// The Length value
/// </summary>
private readonly double Length;

/// <summary>
/// The Width value
/// </summary>
private readonly double Width;

private Unit LengthUnit;
private Unit WidthUnit;
private Unit AreaUnit;

/// <summary>
///
/// </summary>
/// <param name="width"></param>
/// <param name="length"></param>
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");
}

/// <summary>
///
/// </summary>
/// <param name="width"></param>
/// <param name="length"></param>
/// <param name="widthUnit"></param>
/// <param name="lengthUnit"></param>
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");
}

/// <summary>
///
/// </summary>
/// <param name="targetUnit"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
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;
}

/// <summary>
///
/// </summary>
/// <param name="targetUnit"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
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;
}

/// <summary>
///
/// </summary>
/// <param name="targetUnit"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
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)}";
}
}
}
26 changes: 26 additions & 0 deletions src/SampleZeroTouchUnits/SampleZeroTouchUnits.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<ImportGroup Label="PropertySheets">
<Import Project="$(SolutionDir)Config\CS.props" />
</ImportGroup>
<PropertyGroup>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SampleZeroTouchUnits</RootNamespace>
<AssemblyName>SampleZeroTouchUnits</AssemblyName>
<!--EnableDynamicLoading prepares the project so that it can be used as a plugin -->
<!--https://learn.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support#simple-plugin-with-no-dependencies-->
<EnableDynamicLoading>true</EnableDynamicLoading>
<DocumentationFile>bin\$(Configuration)\SampleZeroTouchUnits.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DynamoVisualProgramming.Core" Version="3.1.0-beta3755" ExcludeAssets="runtime" />
<PackageReference Include="DynamoVisualProgramming.DynamoServices" Version="3.1.0-beta3755" ExcludeAssets="runtime" />
<PackageReference Include="DynamoVisualProgramming.ZeroTouchLibrary" Version="3.1.0-beta3755" ExcludeAssets="runtime" />
</ItemGroup>
<Target Name="AfterBuildOps" AfterTargets="Build">
<Message Text="Should run after build" Importance="High" />
<ItemGroup>
<PackageFiles Include="$(OutDir)\SampleZeroTouchUnits.dll;$(OutDir)\SampleZeroTouchUnits.XML;" />
</ItemGroup>
<Copy SourceFiles="@(PackageFiles)" DestinationFolder="$(SolutionDir)..\dynamo_package\Dynamo Samples\bin\" />
</Target>
</Project>

0 comments on commit df9e1b0

Please sign in to comment.