-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectShapeUtils.cs
162 lines (142 loc) · 7.85 KB
/
DirectShapeUtils.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
using Autodesk.Revit.DB;
using System.Collections.Generic;
namespace ricaun.Revit.DB.Shape
{
/// <summary>
/// Utility class for creating DirectShape elements.
/// </summary>
public static class DirectShapeUtils
{
/// <summary>
/// DirectShape default BuiltInCategory
/// </summary>
public const BuiltInCategory ConstBuiltInCategory = BuiltInCategory.OST_GenericModel;
/// <summary>
/// Delete all DirectShape elements in the document with the specified category.
/// </summary>
/// <param name="document">The Revit document.</param>
/// <param name="builtInCategory">The category for the DirectShape element. The default value is <see cref="ConstBuiltInCategory"/>.</param>
/// <returns></returns>
public static ICollection<ElementId> DeleteDirectShape(this Document document,
BuiltInCategory builtInCategory = ConstBuiltInCategory)
{
var elementIds = new FilteredElementCollector(document)
.OfCategory(builtInCategory)
.OfClass(typeof(DirectShape))
.ToElementIds();
return document.Delete(elementIds);
}
/// <summary>
/// Creates a DirectShape element with the specified geometry and category.
/// </summary>
/// <param name="document">The Revit document.</param>
/// <param name="geometryObjects">The geometry objects for the DirectShape element.</param>
/// <param name="builtInCategory">The category for the DirectShape element. The default value is <see cref="ConstBuiltInCategory"/>.</param>
/// <returns>The created DirectShape element.</returns>
public static DirectShape CreateDirectShape(this Document document,
IEnumerable<GeometryObject> geometryObjects,
BuiltInCategory builtInCategory = ConstBuiltInCategory)
{
// Obtains the ElementId of the BuiltInCategory
ElementId categoryId = new ElementId(builtInCategory);
// Creates a DirectShape element
DirectShape ds = DirectShape.CreateElement(document, categoryId);
// Sets the geometry objects of the DirectShape element
ds.SetShape(new List<GeometryObject>(geometryObjects));
// Sets the name of the DirectShape element as the name of the category
ds.SetName(ds.Category.Name);
// Returns the created DirectShape element
return ds;
}
/// <summary>
/// Creates a DirectShape element with the specified geometry and category.
/// </summary>
/// <param name="document">The Revit document.</param>
/// <param name="geometryObject">The geometry object for the DirectShape element.</param>
/// <param name="builtInCategory">The category for the DirectShape element. The default value is <see cref="ConstBuiltInCategory"/>.</param>
/// <returns>The created DirectShape element.</returns>
public static DirectShape CreateDirectShape(this Document document,
GeometryObject geometryObject,
BuiltInCategory builtInCategory = ConstBuiltInCategory)
{
return CreateDirectShape(document, new[] { geometryObject }, builtInCategory);
}
/// <summary>
/// Creates a DirectShapeType type with the specified geometry and category.
/// </summary>
/// <param name="document">The Revit document.</param>
/// <param name="geometryObjects">The geometry objects for the DirectShape element.</param>
/// <param name="typeName">The name of the DirectShapeType. The default value is the Category Name.</param>
/// <param name="builtInCategory">The category for the DirectShape element. The default value is <see cref="ConstBuiltInCategory"/>.</param>
/// <returns>The created DirectShapeType type.</returns>
public static DirectShapeType CreateDirectShapeType(this Document document,
IEnumerable<GeometryObject> geometryObjects,
string typeName = null,
BuiltInCategory builtInCategory = ConstBuiltInCategory)
{
if (string.IsNullOrWhiteSpace(typeName))
typeName = Category.GetCategory(document, builtInCategory).Name;
DirectShapeType directShapeType =
DirectShapeType.Create(document, typeName, new ElementId(builtInCategory));
// Sets the geometry objects of the DirectShapeType type
directShapeType.SetShape(new List<GeometryObject>(geometryObjects));
// Store DirectShapeType in the library
DirectShapeLibrary.GetDirectShapeLibrary(document)
.AddDefinitionType(directShapeType.GetDefinitionId(), directShapeType.Id);
return directShapeType;
}
/// <summary>
/// Creates a DirectShapeType type with the specified geometry and category.
/// </summary>
/// <param name="document">The Revit document.</param>
/// <param name="geometryObject">The geometry object for the DirectShape element.</param>
/// <param name="typeName">The name of the DirectShapeType. The default value is the Category Name.</param>
/// <param name="builtInCategory">The category for the DirectShape element. The default value is <see cref="ConstBuiltInCategory"/>.</param>
/// <returns>The created DirectShapeType type.</returns>
public static DirectShapeType CreateDirectShapeType(this Document document,
GeometryObject geometryObject,
string typeName = null,
BuiltInCategory builtInCategory = ConstBuiltInCategory)
{
return CreateDirectShapeType(document, new[] { geometryObject }, typeName, builtInCategory);
}
/// <summary>
/// Create a DirectShape element with a DirectShapeType and Transform.
/// </summary>
/// <param name="document">The Revit document.</param>
/// <param name="directShapeType">The DirectShapeType type for the DirectShape element.</param>
/// <param name="transform">The tranform location and rotation for the DirectShape element.</param>
/// <returns>The created DirectShape element.</returns>
public static DirectShape CreateDirectShape(this Document document,
DirectShapeType directShapeType,
Transform transform = null)
{
transform = transform ?? Transform.Identity;
DirectShape directShape = DirectShape.CreateElementInstance(document, directShapeType.Id,
directShapeType.Category.Id, directShapeType.GetDefinitionId(), transform);
// Sets the name of the DirectShape element as the name of the category
directShape.SetName(directShape.Category.Name);
return directShape;
}
/// <summary>
/// Create a DirectShape element with a DirectShapeType and Transform.
/// </summary>
/// <param name="directShapeType">The DirectShapeType type for the DirectShape element.</param>
/// <param name="transform">The tranform location and rotation for the DirectShape element.</param>
/// <returns>The created DirectShape element.</returns>
public static DirectShape Create(this DirectShapeType directShapeType,
Transform transform = null)
{
return directShapeType.Document.CreateDirectShape(directShapeType, transform);
}
/// <summary>
/// Get the definition id of the DirectShapeType type using the Id of the type.
/// </summary>
/// <param name="directShapeType">The DirectShapeType type.</param>
/// <returns>The definition id of the DirectShapeType type.</returns>
public static string GetDefinitionId(this DirectShapeType directShapeType)
{
return directShapeType.Id.ToString();
}
}
}