-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathRoof.cs
More file actions
300 lines (247 loc) · 9.73 KB
/
Roof.cs
File metadata and controls
300 lines (247 loc) · 9.73 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
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
using System;
using System.Collections.Generic;
using Autodesk.DesignScript.Geometry;
using Autodesk.DesignScript.Runtime;
using Autodesk.Revit.DB;
using Revit.GeometryConversion;
using RevitServices.Persistence;
using RevitServices.Transactions;
using Curve = Autodesk.DesignScript.Geometry.Curve;
using Pt = Autodesk.DesignScript.Geometry.Point;
namespace Revit.Elements
{
/// <summary>
/// MassLevelData
/// </summary>
public class Roof : Element
{
#region Internal properties
/// <summary>
/// An internal handle on the RoofBase
/// </summary>
internal Autodesk.Revit.DB.RoofBase InternalRoof
{
get; private set;
}
/// <summary>
/// Reference to the Element
/// </summary>
[SupressImportIntoVM]
public override Autodesk.Revit.DB.Element InternalElement
{
get { return InternalRoof; }
}
#endregion
#region Private constructors
/// <summary>
/// Private constructor
/// </summary>
private Roof(Autodesk.Revit.DB.RoofBase roof)
{
SafeInit(() => InitRoof(roof), true);
}
/// <summary>
/// Private constructor
/// </summary>
private Roof(CurveArray curves, Autodesk.Revit.DB.ReferencePlane reference, Autodesk.Revit.DB.Level level, Autodesk.Revit.DB.RoofType roofType, double extrusionStart, double extrusionEnd)
{
SafeInit(() => InitRoof(curves, reference, level, roofType, extrusionStart, extrusionEnd));
}
private Roof(CurveArray curves, Autodesk.Revit.DB.Level level, Autodesk.Revit.DB.RoofType roofType)
{
SafeInit(() => InitRoof(curves, level, roofType));
}
#endregion
#region Helpers for private constructors
/// <summary>
/// Initialize a roof element
/// </summary>
private void InitRoof(Autodesk.Revit.DB.RoofBase roof)
{
InternalSetRoof(roof);
}
/// <summary>
/// Initialize a roof element
/// </summary>
private void InitRoof(CurveArray curves, Autodesk.Revit.DB.ReferencePlane reference, Autodesk.Revit.DB.Level level, Autodesk.Revit.DB.RoofType roofType, double extrusionStart, double extrusionEnd)
{
TransactionManager.Instance.EnsureInTransaction(Document);
Autodesk.Revit.DB.RoofBase roof = ElementBinder.GetElementFromTrace<Autodesk.Revit.DB.RoofBase>(Document);
if (roof == null)
{
roof = Document.Create.NewExtrusionRoof(curves, reference, level, roofType, extrusionStart, extrusionEnd);
}
else
{
roof = Document.Create.NewExtrusionRoof(curves, reference, level, roofType, extrusionStart, extrusionEnd);
}
InternalSetRoof(roof);
TransactionManager.Instance.TransactionTaskDone();
ElementBinder.CleanupAndSetElementForTrace(Document, InternalRoof);
}
/// <summary>
/// Initialize a roof element
/// </summary>
private void InitRoof(CurveArray curves, Autodesk.Revit.DB.Level level, Autodesk.Revit.DB.RoofType roofType)
{
TransactionManager.Instance.EnsureInTransaction(Document);
Autodesk.Revit.DB.RoofBase roof = ElementBinder.GetElementFromTrace<Autodesk.Revit.DB.RoofBase>(Document);
if (roof == null)
{
ModelCurveArray footprint = new ModelCurveArray();
roof = Document.Create.NewFootPrintRoof(curves, level, roofType, out footprint);
}
else
{
ModelCurveArray footprint = new ModelCurveArray();
roof = Document.Create.NewFootPrintRoof(curves, level, roofType, out footprint);
}
InternalSetRoof(roof);
TransactionManager.Instance.TransactionTaskDone();
ElementBinder.CleanupAndSetElementForTrace(Document, InternalRoof);
}
#endregion
#region Private mutators
/// <summary>
/// Set the InternalRoof property and the associated element id and unique id
/// </summary>
/// <param name="roof"></param>
private void InternalSetRoof(Autodesk.Revit.DB.RoofBase roof)
{
InternalRoof = roof;
InternalElementId = roof.Id;
InternalUniqueId = roof.UniqueId;
}
#endregion
#region Public static constructors
/// <summary>
/// Create a Revit Roof given its curve outline and Level
/// </summary>
/// <param name="outline"></param>
/// <param name="roofType"></param>
/// <param name="level"></param>
/// <returns>The Roof</returns>
public static Roof ByOutlineTypeAndLevel(Curve[] outline, RoofType roofType, Level level)
{
var polycurve = PolyCurve.ByJoinedCurves(outline);
if (!polycurve.IsClosed)
{
throw new ArgumentException(Properties.Resources.OpenInputPolyCurveError);
}
var ca = new CurveArray();
polycurve.Curves().ForEach(x => {
ca.Append(x.ToRevitType());
x.Dispose();
});
var roof = new Roof(ca, level.InternalLevel,roofType.InternalRoofType);
if (!TransactionManager.Instance.DisableTransactions)
DocumentManager.Regenerate();
return roof;
}
/// <summary>
/// Extrude Roof by Outline, Referenceplane
/// </summary>
/// <param name="outline"></param>
/// <param name="roofType"></param>
/// <param name="level"></param>
/// <param name="plane"></param>
/// <param name="extrusionStart"></param>
/// <param name="extrusionEnd"></param>
/// <returns></returns>
public static Roof ByOutlineExtrusionTypeAndLevel(PolyCurve outline, RoofType roofType, Level level, ReferencePlane plane, double extrusionStart, double extrusionEnd)
{
if (outline.IsClosed)
{
throw new ArgumentException(Properties.Resources.CloseInputPolyCurveError);
}
var ca = new CurveArray();
outline.Curves().ForEach(x => {
ca.Append(x.ToRevitType());
x.Dispose();
});
var roof = new Roof(ca, plane.InternalReferencePlane, level.InternalLevel, roofType.InternalRoofType, extrusionStart, extrusionEnd);
if (!TransactionManager.Instance.DisableTransactions)
DocumentManager.Regenerate();
return roof;
}
#endregion
#region Public Properties
/// <summary>
/// Get Slab Shape Points
/// </summary>
public IEnumerable<Pt> Points
{
get
{
if (this.InternalRoof.SlabShapeEditor == null)
{
throw new Exception(Properties.Resources.InvalidShapeEditor);
}
TransactionManager.Instance.EnsureInTransaction(DocumentManager.Instance.CurrentDBDocument);
this.InternalRoof.SlabShapeEditor.Enable();
TransactionManager.Instance.TransactionTaskDone();
List<Pt> points = new List<Pt>();
foreach (SlabShapeVertex v in this.InternalRoof.SlabShapeEditor.SlabShapeVertices)
{
points.Add(v.Position.ToPoint());
}
return points;
}
}
/// <summary>
/// Add Point to Slab Shape
/// </summary>
public void AddPoint(Pt point)
{
if (this.InternalRoof.SlabShapeEditor == null)
{
throw new Exception(Properties.Resources.InvalidShapeEditor);
}
TransactionManager.Instance.EnsureInTransaction(DocumentManager.Instance.CurrentDBDocument);
this.InternalRoof.SlabShapeEditor.Enable();
this.InternalRoof.SlabShapeEditor.DrawPoint(point.ToXyz());
TransactionManager.Instance.TransactionTaskDone();
}
/// <summary>
/// Move existing point by offset
/// </summary>
public void MovePoint(Pt point, double offset)
{
if (this.InternalRoof.SlabShapeEditor == null)
{
throw new Exception(Properties.Resources.InvalidShapeEditor);
}
SlabShapeVertex vertex = null;
foreach (SlabShapeVertex v in this.InternalRoof.SlabShapeEditor.SlabShapeVertices)
{
if (point.IsAlmostEqualTo(v.Position.ToPoint()))
{
vertex = v;
}
}
if (vertex != null && offset != 0)
{
TransactionManager.Instance.EnsureInTransaction(DocumentManager.Instance.CurrentDBDocument);
this.InternalRoof.SlabShapeEditor.Enable();
this.InternalRoof.SlabShapeEditor.ModifySubElement(vertex, offset);
TransactionManager.Instance.TransactionTaskDone();
}
}
#endregion
#region Internal static constructors
/// <summary>
/// Create a Roof from a user selected Element.
/// </summary>
/// <param name="roof"></param>
/// <param name="isRevitOwned"></param>
/// <returns></returns>
internal static Roof FromExisting(Autodesk.Revit.DB.RoofBase roof, bool isRevitOwned)
{
return new Roof(roof)
{
IsRevitOwned = isRevitOwned
};
}
#endregion
}
}