-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add units example * Update build.yml
- Loading branch information
Showing
4 changed files
with
170 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |