-
Notifications
You must be signed in to change notification settings - Fork 26
/
Util.cs
322 lines (283 loc) · 10.5 KB
/
Util.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
namespace RevitExportGltf
{
static class Util
{
/// <summary>
/// c# 的扩展方法
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static string Point(this XYZ p)
{
return string.Format("{0}", "{1}", "{2}", Math.Round(p.X, 2), Math.Round(p.Y, 2), Math.Round(p.Z, 2));
}
public static int[] GetVec3MinMax(List<int> vec3)
{
int minVertexX = int.MaxValue;
int minVertexY = int.MaxValue;
int minVertexZ = int.MaxValue;
int maxVertexX = int.MinValue;
int maxVertexY = int.MinValue;
int maxVertexZ = int.MinValue;
for (int i = 0; i < vec3.Count; i += 3)
{
if (vec3[i] < minVertexX) minVertexX = vec3[i];
if (vec3[i] > maxVertexX) maxVertexX = vec3[i];
if (vec3[i + 1] < minVertexY) minVertexY = vec3[i + 1];
if (vec3[i + 1] > maxVertexY) maxVertexY = vec3[i + 1];
if (vec3[i + 2] < minVertexZ) minVertexZ = vec3[i + 2];
if (vec3[i + 2] > maxVertexZ) maxVertexZ = vec3[i + 2];
}
return new int[] { minVertexX, maxVertexX, minVertexY, maxVertexY, minVertexZ, maxVertexZ };
}
public static long[] GetVec3MinMax(List<long> vec3)
{
long minVertexX = long.MaxValue;
long minVertexY = long.MaxValue;
long minVertexZ = long.MaxValue;
long maxVertexX = long.MinValue;
long maxVertexY = long.MinValue;
long maxVertexZ = long.MinValue;
for (int i = 0; i < (vec3.Count / 3); i += 3)
{
if (vec3[i] < minVertexX) minVertexX = vec3[i];
if (vec3[i] > maxVertexX) maxVertexX = vec3[i];
if (vec3[i + 1] < minVertexY) minVertexY = vec3[i + 1];
if (vec3[i + 1] > maxVertexY) maxVertexY = vec3[i + 1];
if (vec3[i + 2] < minVertexZ) minVertexZ = vec3[i + 2];
if (vec3[i + 2] > maxVertexZ) maxVertexZ = vec3[i + 2];
}
return new long[] { minVertexX, maxVertexX, minVertexY, maxVertexY, minVertexZ, maxVertexZ };
}
public static float[] GetVec3MinMax(List<float> vec3)
{
List<float> xValues = new List<float>();
List<float> yValues = new List<float>();
List<float> zValues = new List<float>();
for (int i = 0; i < vec3.Count; i++)
{
if ((i % 3) == 0) xValues.Add(vec3[i]);
if ((i % 3) == 1) yValues.Add(vec3[i]);
if ((i % 3) == 2) zValues.Add(vec3[i]);
}
float maxX = xValues.Max();
float minX = xValues.Min();
float maxY = yValues.Max();
float minY = yValues.Min();
float maxZ = zValues.Max();
float minZ = zValues.Min();
return new float[] { minX, maxX, minY, maxY, minZ, maxZ };
}
public static int[] GetScalarMinMax(List<int> scalars)
{
int minFaceIndex = int.MaxValue;
int maxFaceIndex = int.MinValue;
for (int i = 0; i < scalars.Count; i++)
{
int currentMin = Math.Min(minFaceIndex, scalars[i]);
if (currentMin < minFaceIndex) minFaceIndex = currentMin;
int currentMax = Math.Max(maxFaceIndex, scalars[i]);
if (currentMax > maxFaceIndex) maxFaceIndex = currentMax;
}
return new int[] { minFaceIndex, maxFaceIndex };
}
public static float[] GetUVMinMax(List<float> vec3)
{
List<float> xValues = new List<float>();
List<float> yValues = new List<float>();
for (int i = 0; i < vec3.Count; i++)
{
if ((i % 2) == 0) xValues.Add(vec3[i]);
if ((i % 2) == 1) yValues.Add(vec3[i]);
}
float maxX = xValues.Max();
float minX = xValues.Min();
float maxY = yValues.Max();
float minY = yValues.Min();
return new float[] { minX, maxX, minY, maxY };
}
/// <summary>
/// From Jeremy Tammik's RvtVa3c exporter:
/// https://github.com/va3c/RvtVa3c
/// Return a string for a real number
/// formatted to two decimal places.
/// </summary>
public static string RealString(double a)
{
return a.ToString("0.##");
}
/// <summary>
/// From Jeremy Tammik's RvtVa3c exporter:
/// https://github.com/va3c/RvtVa3c
/// Return a string for an XYZ point
/// or vector with its coordinates
/// formatted to two decimal places.
/// </summary>
public static string PointString(XYZ p)
{
return string.Format("({0},{1},{2})",
RealString(p.X),
RealString(p.Y),
RealString(p.Z));
}
/// <summary>
/// From Jeremy Tammik's RvtVa3c exporter:
/// https://github.com/va3c/RvtVa3c
/// Return an integer value for a Revit Color.
/// </summary>
public static int ColorToInt(Color color)
{
return ((int)color.Red) << 16
| ((int)color.Green) << 8
| (int)color.Blue;
}
/// <summary>
/// From Jeremy Tammik's RvtVa3c exporter:
/// https://github.com/va3c/RvtVa3c
/// Extract a true or false value from the given
/// string, accepting yes/no, Y/N, true/false, T/F
/// and 1/0. We are extremely tolerant, i.e., any
/// value starting with one of the characters y, n,
/// t or f is also accepted. Return false if no
/// valid Boolean value can be extracted.
/// </summary>
public static bool GetTrueOrFalse(string s, out bool val)
{
val = false;
if (s.Equals(Boolean.TrueString,
StringComparison.OrdinalIgnoreCase))
{
val = true;
return true;
}
if (s.Equals(Boolean.FalseString,
StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (s.Equals("1"))
{
val = true;
return true;
}
if (s.Equals("0"))
{
return true;
}
s = s.ToLower();
if ('t' == s[0] || 'y' == s[0])
{
val = true;
return true;
}
if ('f' == s[0] || 'n' == s[0])
{
return true;
}
return false;
}
/// <summary>
/// From Jeremy Tammik's RvtVa3c exporter:
/// https://github.com/va3c/RvtVa3c
/// Return a string describing the given element:
/// .NET type name,
/// category name,
/// family and symbol name for a family instance,
/// element id and element name.
/// </summary>
public static string ElementDescription(Element e)
{
if (null == e)
{
return "<null>";
}
// For a wall, the element name equals the
// wall type name, which is equivalent to the
// family name ...
FamilyInstance fi = e as FamilyInstance;
string typeName = e.GetType().Name;
string categoryName = (null == e.Category)
? string.Empty
: e.Category.Name + " ";
string familyName = (null == fi)
? string.Empty
: fi.Symbol.Family.Name + " ";
string symbolName = (null == fi
|| e.Name.Equals(fi.Symbol.Name))
? string.Empty
: fi.Symbol.Name + " ";
return string.Format("{0} {1}{2}{3}<{4} {5}>",
typeName, categoryName, familyName,
symbolName, e.Id.IntegerValue, e.Name);
}
/// <summary>
/// From Jeremy Tammik's RvtVa3c exporter:
/// https://github.com/va3c/RvtVa3c
/// Return a dictionary of all the given
/// element parameter names and values.
/// </summary>
public static Dictionary<string, string> GetElementProperties(Element e, bool includeType)
{
IList<Parameter> parameters
= e.GetOrderedParameters();
Dictionary<string, string> a = new Dictionary<string, string>(parameters.Count);
// Add element category
a.Add("Element Category", e.Category.Name);
string key;
string val;
foreach (Parameter p in parameters)
{
key = p.Definition.Name;
if (!a.ContainsKey(key))
{
if (StorageType.String == p.StorageType)
{
val = p.AsString();
}
else
{
val = p.AsValueString();
}
if (!string.IsNullOrEmpty(val))
{
a.Add(key, val);
}
}
}
if (includeType)
{
ElementId idType = e.GetTypeId();
if (ElementId.InvalidElementId != idType)
{
Document doc = e.Document;
Element typ = doc.GetElement(idType);
parameters = typ.GetOrderedParameters();
foreach (Parameter p in parameters)
{
key = "Type " + p.Definition.Name;
if (!a.ContainsKey(key))
{
if (StorageType.String == p.StorageType)
{
val = p.AsString();
}
else
{
val = p.AsValueString();
}
if (!string.IsNullOrEmpty(val))
{
a.Add(key, val);
}
}
}
}
}
return a;
}
}
}