/
Utils.cs
332 lines (279 loc) · 12.7 KB
/
Utils.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
323
324
325
326
327
328
329
330
331
332
using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.Architecture;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB.Structure;
namespace BLOCKTools
{
class Utils
{
/// <summary>
/// Возвращает список всех чистых помещений
/// </summary>
public static List<Room> GetAllCleanRooms(Document doc)
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
RoomFilter roomFilter = new RoomFilter();
collector.WherePasses(roomFilter);
// Настройка фильтра для комнат заданным параметром "BL_Класс чистоты",
// равным "A", "B", "C" или "D"
Guid spGuid = new Guid("02f7e9d4-f055-479e-a36f-5b8e098d40f5");
Room fRoom = collector.FirstElement() as Room;
Parameter testSharedParam = fRoom.get_Parameter(spGuid);
ElementId sharedParamId = testSharedParam.Id;
ParameterValueProvider provider = new ParameterValueProvider(sharedParamId);
FilterStringRuleEvaluator fsreEqual = new FilterStringEquals();
string testStrCNC = "CNC";
string testVoidStrCNC = "";
FilterRule fCNCRule = new FilterStringRule(provider, fsreEqual, testStrCNC, false);
FilterRule fVoidStrRule = new FilterStringRule(provider, fsreEqual, testVoidStrCNC, false);
ElementParameterFilter paramNoCNCFilter = new ElementParameterFilter(fCNCRule, true);
ElementParameterFilter paramNoVoidFilter = new ElementParameterFilter(fVoidStrRule, true);
collector.WherePasses(paramNoVoidFilter).WherePasses(paramNoCNCFilter);
List<Room> cleanRooms = (from e in collector.ToElements()
where e is Room
select e as Room).ToList<Room>();
return cleanRooms;
}
/// <summary>
/// Возвращает список из Curve, составляющих границу помещения
/// </summary>
public static List<Line> GetRoomBoundary(Room room)
{
List<Line> roomBoundary = new List<Line>();
SpatialElementBoundaryOptions opt = new SpatialElementBoundaryOptions();
opt.SpatialElementBoundaryLocation = SpatialElementBoundaryLocation.Finish;
opt.StoreFreeBoundaryFaces = true;
IList<IList<BoundarySegment>> segments = room.GetBoundarySegments(opt);
if (null != segments)
{
foreach (IList<BoundarySegment> segmentList in segments)
{
foreach (BoundarySegment boundarySegment in segmentList)
{
roomBoundary.Add(boundarySegment.GetCurve() as Line);
}
}
}
return roomBoundary;
}
/// <summary>
/// Возвращает список нормализованных векторов на основе входного списка объектов типа Line, составляющих границы помещения
/// </summary>
public static List<XYZ> VectorDirection(IList<Line> boundary)
{
List<XYZ> direction = (from l in boundary
select l.Direction).ToList();
return direction;
}
/// <summary>
/// Возвращает список с метками ориетации углов(двух смежных линий в контуре комнаты) "ВНЕШ", "ВНУТ" или "ПАРР"
/// </summary>
public static List<string> GetRoomCornerOrientation(List<Line> roomBoundary)
{
string GetCornerOrientation(XYZ vc)
{
int vcZ = (int)vc.Z;
string sVecCr;
if (vcZ > 0)
{
sVecCr = "ВНУТ";
}
else if (vcZ < 0)
{
sVecCr = "ВНЕШ";
}
else
{
sVecCr = "ПАРР";
}
return sVecCr;
}
/*
string sbound = null;
for (int i = 0; i < roomBoundary.Count; i++)
{
sbound += roomBoundary[i].Direction.ToString() + "\n";
}
TaskDialog.Show("Направление линий границы", sbound);
*/
List<XYZ> normalizedVectors = Utils.VectorDirection(roomBoundary);
List<string> cornersOrientations = new List<string>();
string orientation;
XYZ v1 = normalizedVectors.Last();
XYZ v2 = normalizedVectors[0];
XYZ crossVector = v1.CrossProduct(v2);
orientation = GetCornerOrientation(crossVector);
cornersOrientations.Add(orientation);
for (int i = 0; i < normalizedVectors.Count - 1; i++)
{
v1 = normalizedVectors[i];
v2 = normalizedVectors[i + 1];
crossVector = v1.CrossProduct(v2);
orientation = GetCornerOrientation(crossVector);
cornersOrientations.Add(orientation);
//vcross += crossVector.Z + "\n";
//s += orientation + "\n";
}
//TaskDialog.Show("Ориентация вершин", s);
//TaskDialog.Show("Векторное произведение", vcross);
return cornersOrientations;
}
/// <summary>
/// Возвращает количество внутренних углов комнаты
/// </summary>
/// <param name="cornerList">список типов углов комнаты</param>
/// <returns></returns>
public static int GetInnerCornerCount(List<string> cornerList)
{
return (from c in cornerList where c.Equals("ВНУТ") select c).Count();
}
/// <summary>
/// Возвращает количество внешних углов
/// </summary>
/// <param name="cornerList">список типов углов комнаты</param>
/// <returns></returns>
public static int GetOutterCornerCount(List<string> cornerList)
{
return (from c in cornerList where c.Equals("ВНЕШ") select c).Count();
}
/// <summary>
/// Вовзращает угол между вектором v и базовым ветором [0, -1, 0]
/// </summary>
public static int VectorOrintationAngle(XYZ v)
{
int bx = 0;
int by = -1;
int bz = 0;
int vx = (int)v.X;
int vy = (int)v.Y;
int vz = (int)v.Z;
int angleInRad = (int)(Math.Acos((vx * bx + vy * by + vz * bz) /
(Math.Sqrt(Math.Pow(vx, 2) + Math.Pow(vy, 2) + Math.Pow(vz, 2)) *
Math.Sqrt(Math.Pow(bx, 2) + Math.Pow(by, 2) + Math.Pow(bz, 2)))) * 180 / Math.PI);
return angleInRad;
}
/// <summary>
/// Возвращает список углов поворота для позиционирования фабионов во внутренних углах помещения
/// </summary>
public static List<int> RoomAnglesOrientations(IList<string> cornerOrient, IList<Line> boundary)
{
List<XYZ> normalizedVectors = Utils.VectorDirection(boundary);
List<int> angles = (from v in normalizedVectors
let angle = Utils.VectorOrintationAngle(v)
select angle).ToList();
/*
string sAngles = null;
for (int i = 0; i < angles.Count; i++)
{
sAngles += angles[i] + "\n";
}
TaskDialog.Show("Углы", sAngles);
*/
int index = angles.IndexOf(0);
List<int> anglesPart1 = angles.GetRange(0, index);
List<int> anglesPart2 = angles.GetRange(index, angles.Count - index);
int rotateAngle = 0;
List<int> rotates = new List<int>();
for (int i = 0; i < angles.Count(); i++)
{
rotates.Add(0);
}
for (int i = 1; i < anglesPart2.Count(); i++)
{
if (cornerOrient[index + i] == "ВНУТ")
{
rotateAngle += 90;
}
if (cornerOrient[index + i] == "ВНЕШ")
{
rotateAngle -= 90;
}
rotates[index + i] = rotateAngle;
}
for (int i = 0; i < anglesPart1.Count(); i++)
{
if (cornerOrient[i] == "ВНУТ")
{
rotateAngle += 90;
}
if (cornerOrient[i] == "ВНЕШ")
{
rotateAngle -= 90;
}
rotates[i] = rotateAngle;
}
/*
string sRotates = null;
foreach (int r in rotates)
{
sRotates += r + "\n";
}
TaskDialog.Show("Углы поворота", sRotates);
*/
List<int> filteredInternalRotates = new List<int>();
//string s = null;
for (int i = 0; i < cornerOrient.Count(); i++)
{
if (cornerOrient[i] == "ВНУТ")
{
filteredInternalRotates.Add(rotates[i]);
//s += rotates[i] + "\n";
}
}
//TaskDialog.Show("Углы поворота внутренних углов", s);
return filteredInternalRotates;
}
/// <summary>
/// Возвращает список координат внутренних углов помещения
/// </summary>
public static List<Point> GetInnerCornerList(List<Line> boundary, List<string> orientations)
{
List<Point> startPointList = new List<Point>();
for (int i = 0; i < boundary.Count(); i++)
{
if (orientations[i] == "ВНУТ")
{
startPointList.Add(Point.Create(boundary[i].GetEndPoint(0)));
}
}
return startPointList;
}
/// <summary>
/// Находит все экземпляры указанного типа
/// </summary>
/// <param name="doc"></param>
/// <param name="typeName"></param>
/// <returns></returns>
public static List<Element> GetAllFamilyTypeInstances(Document doc, string typeName)
{
// Находит все типоразмеры семейств в текущем документе
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector = collector.OfClass(typeof(FamilySymbol));
// Находит среди всех типоразмеров типоразмеры с заданным именем "typeName" и определяет Id первого из списка
var query = from element in collector where element.Name == typeName select element;
List<Element> famSyms = query.ToList<Element>();
ElementId symbolId = famSyms[0].Id;
// Создает FamilyInstance фильтр по найденному FamilySymbol Id
FamilyInstanceFilter filter = new FamilyInstanceFilter(doc, symbolId);
// Применяет фильтр к элементам из текущего документа
collector = new FilteredElementCollector(doc);
List<Element> familyInstances = collector.WherePasses(filter).ToElements().ToList();
return familyInstances;
}
/// <summary>
/// Удаляет в текущем документе указанный список элементов
/// </summary>
public static void DeleteElements(Document document, List<Element> elements)
{
// Delete all the selected elements via the set of elements
ICollection<ElementId> elementsId = (from e in elements select e.Id).ToList();
ICollection<ElementId> deletedIdSet = document.Delete(elementsId);
}
}
}