-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathToUnit.cs
More file actions
88 lines (81 loc) · 5.21 KB
/
Copy pathToUnit.cs
File metadata and controls
88 lines (81 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2026, 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.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("Converts an SI value to the given unit, e.g. 0.012 m → 12 LengthUnit.Millimeter. " +
"The inverse of FromUnit.")]
[Input("siValue", "The value in SI units.")]
[Input("unit", "The unit to convert to, as a member of one of the BH.oM.Units unit enums, e.g. " +
"LengthUnit.Millimeter. A UnitsNet unit is read as well, but records a warning.")]
[Output("value", "The value in the given unit, or NaN when the unit has no conversion.")]
public static double ToUnit(this double siValue, Enum unit)
{
// No unit is how a dimensionless quantity arrives - a Ratio or a Strain. Nothing to convert,
// so the value passes straight through.
if (unit == null)
{
Compute.RecordError($"BH.Engine.Units has no conversion for a null unit.");
return siValue;
}
switch (unit.GetType().Name)
{
case nameof(LengthUnit): return siValue.ToLength(unit);
case nameof(AreaUnit): return siValue.ToArea(unit);
case nameof(VolumeUnit): return siValue.ToVolume(unit);
case nameof(AreaMomentOfInertiaUnit): return siValue.ToAreaMomentOfInertia(unit);
case nameof(PressureUnit): return siValue.ToPressure(unit);
case nameof(ForceUnit): return siValue.ToForce(unit);
case nameof(TorqueUnit): return siValue.ToTorque(unit);
case nameof(ForcePerLengthUnit): return siValue.ToForcePerLength(unit);
case nameof(TorquePerLengthUnit): return siValue.ToMomentPerLength(unit);
case nameof(MassUnit): return siValue.ToMass(unit);
case nameof(AngleUnit): return siValue.ToAngle(unit);
case nameof(AccelerationUnit): return siValue.ToAcceleration(unit);
case nameof(DensityUnit): return siValue.ToDensity(unit);
case nameof(EnergyUnit): return siValue.ToEnergy(unit);
case nameof(SpeedUnit): return siValue.ToSpeed(unit);
case nameof(DurationUnit): return siValue.ToDuration(unit);
case nameof(TemperatureUnit): return siValue.ToTemperature(unit);
case nameof(TemperatureDeltaUnit): return siValue.ToTemperatureDelta(unit);
case nameof(CoefficientOfThermalExpansionUnit): return siValue.ToCoefficientOfThermalExpansion(unit);
case nameof(ElectricConductivityUnit): return siValue.ToElectricConductivity(unit);
case nameof(MassFractionUnit): return siValue.ToMassFraction(unit);
case nameof(MolalityUnit): return siValue.ToMolality(unit);
case nameof(WarpingMomentOfInertiaUnit): return siValue.ToWarpingMomentOfInertia(unit);
default:
Compute.RecordError($"BH.Engine.Units has no conversion for a {unit.GetType().Name}.");
return double.NaN;
}
}
/***************************************************/
}
}