Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ToUnit methods #76

Merged
merged 17 commits into from
Jul 26, 2022
9 changes: 9 additions & 0 deletions Localisation_Toolkit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
VisualStudioVersion = 16.0.29728.190
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Units_Engine", "Units_Engine\Units_Engine.csproj", "{45191654-A1A1-4F08-9F69-0C976EB6AEDA}"
ProjectSection(ProjectDependencies) = postProject
{3370E6DE-2ADD-46DF-9012-E4703970467C} = {3370E6DE-2ADD-46DF-9012-E4703970467C}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Units_oM", "Units_oM\Units_oM.csproj", "{3370E6DE-2ADD-46DF-9012-E4703970467C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +20,10 @@ Global
{45191654-A1A1-4F08-9F69-0C976EB6AEDA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{45191654-A1A1-4F08-9F69-0C976EB6AEDA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{45191654-A1A1-4F08-9F69-0C976EB6AEDA}.Release|Any CPU.Build.0 = Release|Any CPU
{3370E6DE-2ADD-46DF-9012-E4703970467C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3370E6DE-2ADD-46DF-9012-E4703970467C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3370E6DE-2ADD-46DF-9012-E4703970467C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3370E6DE-2ADD-46DF-9012-E4703970467C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
147 changes: 147 additions & 0 deletions Units_Engine/Convert/Acceleration/Acceleration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2022, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using UN = UnitsNet; //This is to avoid clashes between UnitsNet quantity attributes and BHoM quantity attributes
using UNU = UnitsNet.Units;

using System.ComponentModel;
using BH.oM.Base.Attributes;
using BH.oM.Units;
using BH.Engine.Base;

namespace BH.Engine.Units
{
public static partial class Convert
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/

[Description("Convert an acceleration into SI units (metresPerSecondSquared).")]
[Input("acceleration", "The quantity to convert.")]
[Input("unit", "The unit in which the quantity is defined. This can be a string, or you can use the BHoM Enum AccelerationUnit.")]
[Output("metresPerSecondSquared", "The equivalent number of metresPerSecondSquared.")]
public static double FromAcceleration(this double acceleration, object unit)
{
if (Double.IsNaN(acceleration) || Double.IsInfinity(acceleration))
{
Compute.RecordError("Quantity is not a real number.");
return double.NaN;
}

UN.QuantityValue qv = acceleration;
UNU.AccelerationUnit unitSI = UNU.AccelerationUnit.MeterPerSecondSquared;
UNU.AccelerationUnit unUnit = ToAccelerationUnit(unit);

if (unUnit != UNU.AccelerationUnit.Undefined)
return UN.UnitConverter.Convert(qv, unUnit, unitSI);

Compute.RecordError("Unit was undefined. Please use the appropriate BHoM Units Enum.");
return double.NaN;
}

/***************************************************/

[Description("Convert SI units (metresPerSecondSquared) into another acceleration unit.")]
[Input("metresPerSecondSquared", "The number of metresPerSecondSquared to convert.")]
[Input("unit", "The unit to convert to. This can be a string, or you can use the BHoM Enum AccelerationUnit.")]
[Output("acceleration", "The equivalent quantity defined in the specified unit.")]
public static double ToAcceleration(this double metresPerSecondSquared, object unit)
{
if (Double.IsNaN(metresPerSecondSquared) || Double.IsInfinity(metresPerSecondSquared))
{
Compute.RecordError("Quantity is not a real number.");
return double.NaN;
}

UN.QuantityValue qv = metresPerSecondSquared;
UNU.AccelerationUnit unitSI = UNU.AccelerationUnit.MeterPerSecondSquared;
UNU.AccelerationUnit unUnit = ToAccelerationUnit(unit);

if (unUnit != UNU.AccelerationUnit.Undefined)
return UN.UnitConverter.Convert(qv, unitSI, unUnit);

Compute.RecordError("Unit was undefined. Please use the appropriate BHoM Units Enum.");
return double.NaN;
}

/***************************************************/
/**** Private Methods ****/
/***************************************************/

private static UNU.AccelerationUnit ToAccelerationUnit(object unit)
{
if (unit == null || unit.ToString() == null)
return UNU.AccelerationUnit.Undefined;

if (unit.GetType() == typeof(string))
{
AccelerationUnit unitEnum;
if (Enum.TryParse<AccelerationUnit>(unit.ToString(), out unitEnum))
unit = unitEnum;
else
unit = unit.ToString().ToLower();
}

switch (unit)
{
case AccelerationUnit.CentimeterPerSecondSquared:
return UNU.AccelerationUnit.CentimeterPerSecondSquared;
case AccelerationUnit.DecimeterPerSecondSquared:
return UNU.AccelerationUnit.DecimeterPerSecondSquared;
case AccelerationUnit.FootPerSecondSquared:
return UNU.AccelerationUnit.FootPerSecondSquared;
case AccelerationUnit.InchPerSecondSquared:
return UNU.AccelerationUnit.InchPerSecondSquared;
case AccelerationUnit.KilometerPerSecondSquared:
return UNU.AccelerationUnit.KilometerPerSecondSquared;
case AccelerationUnit.KnotPerHour:
return UNU.AccelerationUnit.KnotPerHour;
case AccelerationUnit.KnotPerMinute:
return UNU.AccelerationUnit.KnotPerMinute;
case AccelerationUnit.KnotPerSecond:
return UNU.AccelerationUnit.KnotPerSecond;
case AccelerationUnit.MeterPerSecondSquared:
return UNU.AccelerationUnit.MeterPerSecondSquared;
case AccelerationUnit.MicrometerPerSecondSquared:
return UNU.AccelerationUnit.MicrometerPerSecondSquared;
case AccelerationUnit.MillimeterPerSecondSquared:
return UNU.AccelerationUnit.MillimeterPerSecondSquared;
case AccelerationUnit.NanometerPerSecondSquared:
return UNU.AccelerationUnit.NanometerPerSecondSquared;
case AccelerationUnit.StandardGravity:
return UNU.AccelerationUnit.StandardGravity;
case AccelerationUnit.Undefined:
default:
return UNU.AccelerationUnit.Undefined;
}
}
}
}


158 changes: 158 additions & 0 deletions Units_Engine/Convert/Angle/Angle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2022, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using UN = UnitsNet; //This is to avoid clashes between UnitsNet quantity attributes and BHoM quantity attributes
using UNU = UnitsNet.Units;

using System.ComponentModel;
using BH.oM.Base.Attributes;
using BH.oM.Units;
using BH.Engine.Base;

namespace BH.Engine.Units
{
public static partial class Convert
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/

[Description("Convert a angle into SI units (radian).")]
[Input("angle", "The quantity to convert.")]
[Input("unit", "The unit in which the quantity is defined. This can be a string, or you can use the BHoM Enum AngleUnit.")]
[Output("radian", "The equivalent number of radian.")]
public static double FromAngle(this double angle, object unit)
{
if (Double.IsNaN(angle) || Double.IsInfinity(angle))
{
Compute.RecordError("Quantity is not a real number.");
return double.NaN;
}

UN.QuantityValue qv = angle;
UNU.AngleUnit unitSI = UNU.AngleUnit.Radian;
UNU.AngleUnit unUnit = ToAngleUnit(unit);

if (unUnit != UNU.AngleUnit.Undefined)
return UN.UnitConverter.Convert(qv, unUnit, unitSI);

Compute.RecordError("Unit was undefined. Please use the appropriate BHoM Units Enum.");
return double.NaN;
}

/***************************************************/

[Description("Convert SI units (radian) into another angle unit.")]
[Input("radian", "The number of radian to convert.")]
[Input("unit", "The unit to convert to. This can be a string, or you can use the BHoM Enum AngleUnit.")]
[Output("angle", "The equivalent quantity defined in the specified unit.")]
public static double ToAngle(this double radian, object unit)
{
if (Double.IsNaN(radian) || Double.IsInfinity(radian))
{
Compute.RecordError("Quantity is not a real number.");
return double.NaN;
}

UN.QuantityValue qv = radian;
UNU.AngleUnit unitSI = UNU.AngleUnit.Radian;
UNU.AngleUnit unUnit = ToAngleUnit(unit);

if (unUnit != UNU.AngleUnit.Undefined)
return UN.UnitConverter.Convert(qv, unitSI, unUnit);

Compute.RecordError("Unit was undefined. Please use the appropriate BHoM Units Enum.");
return double.NaN;
}

/***************************************************/
/**** Private Methods ****/
/***************************************************/

private static UNU.AngleUnit ToAngleUnit(object unit)
{
if (unit == null || unit.ToString() == null)
return UNU.AngleUnit.Undefined;

if (unit.GetType() == typeof(string))
{
AngleUnit unitEnum;
if (Enum.TryParse<AngleUnit>(unit.ToString(), out unitEnum))
unit = unitEnum;
else
unit = unit.ToString().ToLower();
}

switch (unit)
{
case AngleUnit.Arcminute:
return UNU.AngleUnit.Arcminute;
case AngleUnit.Arcsecond:
return UNU.AngleUnit.Arcsecond;
case AngleUnit.Centiradian:
return UNU.AngleUnit.Centiradian;
case AngleUnit.Deciradian:
return UNU.AngleUnit.Deciradian;
case "degree":
case "degrees":
case "deg":
case AngleUnit.Degree:
return UNU.AngleUnit.Degree;
case AngleUnit.Gradian:
return UNU.AngleUnit.Gradian;
case AngleUnit.Microdegree:
return UNU.AngleUnit.Microdegree;
case AngleUnit.Microradian:
return UNU.AngleUnit.Microradian;
case AngleUnit.Millidegree:
return UNU.AngleUnit.Millidegree;
case AngleUnit.Milliradian:
return UNU.AngleUnit.Milliradian;
case AngleUnit.Nanodegree:
return UNU.AngleUnit.Nanodegree;
case AngleUnit.Nanoradian:
return UNU.AngleUnit.Nanoradian;
case "radian":
case "radians":
case "rad":
case AngleUnit.Radian:
return UNU.AngleUnit.Radian;
case "revolutions":
case "revs":
case "rev":
case AngleUnit.Revolution:
return UNU.AngleUnit.Revolution;
case AngleUnit.Undefined:
default:
return UNU.AngleUnit.Undefined;
}
}
}
}


Loading