-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnits.cs
126 lines (113 loc) · 4.43 KB
/
Units.cs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Collections.Generic;
using System.Windows.Data;
namespace PAENN2
{
public static class Units
{
public static string Length = "cm";
public static string Force = "kN";
public static string Moment = "kN.m";
public static string Load = "kN/m";
public static string Angle = "°";
public static string Spring = "kN/m";
public static string Displacement = "cm";
public static string Rotation = "rad";
public static Dictionary<string, string> Formats = new Dictionary<string, string> {
{ "Length", "F2" }, { "Force", "F2" }, { "Moment", "F2" },
{ "Load", "F2" }, { "Angle", "F1" }, { "Spring", "F1" },
{ "Displacement", "F3" }, { "Rotation", "F3" },
};
public static double ConvertLength(double value, string unit, bool ConvertFromDefault)
{
if (ConvertFromDefault)
{
switch (unit)
{
case "cm": return value;
case "m": return value / 100;
case "mm": return value * 10;
case "in": return value / 2.54;
case "ft": return value / 30.48;
default: throw new ArgumentException("Invalid length unit passed as argument.");
}
}
else
{
switch (unit)
{
case "cm": return value;
case "m": return value * 100;
case "mm": return value / 10;
case "in": return value * 2.54;
case "ft": return value * 30.48;
default: throw new ArgumentException("Invalid length unit passed as argument.");
}
}
}
public static double ConvertForce(double value, string unit, bool ConvertFromDefault)
{
if (ConvertFromDefault)
{
switch (unit)
{
case "kN": return value;
case "N": return value * 1000;
case "kgf": return value * 101.972;
case "tf": return value * 0.101972;
case "ft": return value * 224.809;
case "kip": return value * 0.224809;
default: throw new ArgumentException("Invalid force unit passed as argument.");
}
}
else
{
switch (unit)
{
case "kN": return value;
case "N": return value / 1000;
case "kgf": return value / 101.972;
case "tf": return value / 0.101972;
case "ft": return value / 224.809;
case "kip": return value / 0.224809;
default: throw new ArgumentException("Invalid force unit passed as argument.");
}
}
}
public static double ConvertAngle(double value, string unit, bool ConvertFromDefault)
{
if (ConvertFromDefault)
{
switch (unit)
{
case "°": return value;
case "rad": return value * Math.PI / 180;
default: throw new ArgumentException("Invalid angle unit passed as argument.");
}
}
else
{
switch (unit)
{
case "°": return value;
case "rad": return value * Math.PI / 180;
default: throw new ArgumentException("Invalid angle unit passed as argument.");
}
}
}
public static double ConvertMoment(double value, string unit, bool ConvertFromDefault)
{
string[] units = unit.Split('.');
double factor = ConvertForce(1, units[0], ConvertFromDefault);
factor *= ConvertLength(1, units[1], ConvertFromDefault);
return factor * value;
}
public static double ConvertLoad(double value, string unit, bool ConvertFromDefault)
{
string[] units = unit.Split('/');
double factor = ConvertForce(1, units[0], ConvertFromDefault);
factor /= ConvertLength(1, units[1], ConvertFromDefault);
return factor * value;
}
}
}