-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCreateSelectionFilter.snippet
159 lines (139 loc) · 5.49 KB
/
CreateSelectionFilter.snippet
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
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Create Selection Filter</Title>
<Author>Maycon Freitas</Author>
<Description>Creates a sample of a Selection Filter code implementing ISelectionFilter interface.</Description>
<Shortcut>rvtselfilt</Shortcut>
</Header>
<Snippet>
<Code Language="CSharp">
<![CDATA[/// <summary>
/// Class to implement ISelectionFilter
/// </summary>
public class $SelectionFilter$ : Autodesk.Revit.UI.Selection.ISelectionFilter
{
/// <summary>
/// Variable to receive the BuiltInCategory, in case of a single category filter.
/// </summary>
private BuiltInCategory _builtInCategory;
/// <summary>
/// Variable to receive a list of BuiltInCategories, in case of a multiple categories filter.
/// </summary>
private List<BuiltInCategory> _builtInCategoryList;
/// <summary>
/// Variable to receive a boolean to control if a reference to a piece of geometry is permitted to be selected.
/// </summary>
private bool _allowReference;
/// <summary>
/// Variable to receive a boolean to control if it's a single category filter or a multiple categories filter.
/// </summary>
private bool multiCategories = false;
/// <summary>
/// Constructor for a single category filter.
/// </summary>
/// <param name="builtInCategory">BuiltIn Category that will be allowed to select.</param>
/// <param name="allowReference">Boolean to control if a reference to a piece of geometry is permitted to be selected.</param>
public $SelectionFilter$(BuiltInCategory builtInCategory, bool allowReference = true)
{
this._builtInCategory = builtInCategory;
this._allowReference = allowReference;
this.multiCategories = false;$end$
}
/// <summary>
/// Constructor for a multiple categories filter.
/// </summary>
/// <param name="builtInCategory">List of BuiltIn Categories that will be allowed to select.</param>
/// <param name="allowReference">Boolean to control if a reference to a piece of geometry is permitted to be selected.</param>
public $SelectionFilter$(List<BuiltInCategory> builtInCategoryList, bool allowReference = true)
{
this._builtInCategoryList = builtInCategoryList;
this._allowReference = allowReference;
this.multiCategories = true;
}
/// <summary>
/// Method that filters the elements that are allows to select.
/// </summary>
/// <param name="elem">Current element.</param>
/// <returns>Returns true or false.</returns>
public bool AllowElement(Element elem)
{
if (this.multiCategories)
{
foreach (BuiltInCategory builtInCategory in this._builtInCategoryList)
{
if (MatchCategory(elem, builtInCategory)) return true;
}
}
else
{
if (MatchCategory(elem, this._builtInCategory)) return true;
}
return false;
}
/// <summary>
/// Method to control if a reference to a piece of geometry is permitted to be selected.
/// </summary>
/// <param name="reference">Current reference.</param>
/// <param name="position">XYZ of the reference.</param>
/// <returns>Returns true or false.</returns>
public bool AllowReference(Reference reference, XYZ position)
{
return _allowReference;
}
/// <summary>
/// Support method to match categories.
/// </summary>
/// <param name="element">Current element.</param>
/// <param name="builtInCategory">Anaysed BuiltIn Category.</param>
/// <returns>Returns true or false.</returns>
public bool MatchCategory(Element element, BuiltInCategory builtInCategory)
{
if (element != null)
{
try
{
if (element.Category.Id.IntegerValue == (int)builtInCategory)
{
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
if (element.Id.IntegerValue == (int)builtInCategory)
{
return true;
}
else
{
return false;
}
}
}
else
{
return false;
}
}
}]]>
</Code>
<Declarations>
<Literal>
<ID>SelectionFilter</ID>
<Default>SelectionFilter</Default>
<ToolTip>The name of the class that you're creating. The name of your Selection Filter.</ToolTip>
</Literal>
</Declarations>
<Imports>
<Import>
<Namespace>Autodesk.Revit.DB</Namespace>
</Import>
</Imports>
</Snippet>
</CodeSnippet>
</CodeSnippets>