-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.cs
More file actions
261 lines (231 loc) · 10.1 KB
/
DataManager.cs
File metadata and controls
261 lines (231 loc) · 10.1 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
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace RevitAddIn_MyProject
{
/// <summary>
/// DataManager类用来存储、创建和编辑 空间元素集 和 区域元素集
/// </summary>
public class DataManager
{
public ExternalCommandData CommandData { get; }
private Document _ActiveDocument;
private Dictionary<int, List<SpaceZoneNode>> _SpaceDictionary;
private Dictionary<int, List<ZoneSpacesNode>> _ZoneDictionary;
private List<Level> _Levels;
private Phase _DefaultPhase;
private Level _CurrentLevel;
//给WPF MainWindow界面暴露的数据源
public Level CurrentLevel
{
get => _CurrentLevel;
set
{
_CurrentLevel = value;
UpdateObservableCollections();
}
}
public ObservableCollection<SpaceZoneNode> SpaceCollection { get; }
public ObservableCollection<ZoneSpacesNode> ZoneCollection { get; }
public ReadOnlyCollection<Level> Levels => new ReadOnlyCollection<Level>(_Levels);
//给WPF EditZoneWindow界面暴露的数据源
public ObservableCollection<SpaceZoneNode> AvailableSpaceCollection { get; }
public ObservableCollection<SpaceZoneNode> CurrentZoneSpaceCollection { get; }
public DataManager(ExternalCommandData commandData)
{
CommandData = commandData;
AvailableSpaceCollection = new ObservableCollection<SpaceZoneNode>();
CurrentZoneSpaceCollection = new ObservableCollection<SpaceZoneNode>();
_ActiveDocument = CommandData.Application.ActiveUIDocument.Document;
//默认阶段
Parameter parameter = _ActiveDocument.ActiveView.get_Parameter(BuiltInParameter.VIEW_PHASE);
ElementId phaseId = parameter.AsElementId();
_DefaultPhase = _ActiveDocument.GetElement(phaseId) as Phase;
//标高
_Levels = new List<Level>();
FilteredElementCollector levels = new FilteredElementCollector(_ActiveDocument).OfClass(typeof(Level));
_Levels = levels.Select(element => element as Level).ToList();
_CurrentLevel = _Levels[0];
//空间字典
_SpaceDictionary = new Dictionary<int, List<SpaceZoneNode>>();
foreach (Level level in _Levels)
{
_SpaceDictionary.Add(level.Id.IntegerValue, new List<SpaceZoneNode>());
}
UpdateSpaceDictionary();
SpaceCollection = new ObservableCollection<SpaceZoneNode>(_SpaceDictionary[_CurrentLevel.Id.IntegerValue]);
//区域字典
_ZoneDictionary = new Dictionary<int, List<ZoneSpacesNode>>();
foreach (Level level in _Levels)
{
_ZoneDictionary.Add(level.Id.IntegerValue, new List<ZoneSpacesNode>());
}
UpdateZoneDictionary();
ZoneCollection = new ObservableCollection<ZoneSpacesNode>(_ZoneDictionary[_CurrentLevel.Id.IntegerValue]);
}
public void UpdateZoneDictionary()
{
_ZoneDictionary[_CurrentLevel.Id.IntegerValue].Clear();
FilteredElementCollector zones = new FilteredElementCollector(_ActiveDocument).OfClass(typeof(Zone));
foreach (Zone zone in zones)
{
if (zone != null && _ActiveDocument.GetElement(zone.LevelId) != null)
{
ZoneSpacesNode zoneSpacesNode = new ZoneSpacesNode(zone);
_ZoneDictionary[zone.LevelId.IntegerValue].Add(zoneSpacesNode);
}
}
}
public void UpdateSpaceDictionary()
{
_SpaceDictionary[_CurrentLevel.Id.IntegerValue].Clear();
FilteredElementCollector spaces = new FilteredElementCollector(_ActiveDocument).WherePasses(new SpaceFilter());
foreach (Space space in spaces)
{
SpaceZoneNode spaceZoneNode = new SpaceZoneNode(space);
_SpaceDictionary[space.LevelId.IntegerValue].Add(spaceZoneNode);
}
}
public void CreateSpaces()
{
Document activeDocument = CommandData.Application.ActiveUIDocument.Document;
if (_DefaultPhase == null)
{
TaskDialog.Show("Revit","The phase of active view is null, you can't create spaces in null phase");
return;
}
try
{
if (activeDocument.ActiveView.ViewType == ViewType.FloorPlan)
{
//尝试一下其他的创建方法
ICollection<ElementId> newSpaceIDs = activeDocument.Create.NewSpaces2(
_CurrentLevel, _DefaultPhase, activeDocument.ActiveView);
if (newSpaceIDs == null || newSpaceIDs.Count == 0)
{
TaskDialog.Show("Revit", "There is no enclosed loop in " + _CurrentLevel.Name);
return;
}
//将创建出来的新空间添加到空间字典中
foreach (var newspaceID in newSpaceIDs)
{
Space newSpace = activeDocument.GetElement(newspaceID) as Space;
SpaceZoneNode spaceZoneNode = new SpaceZoneNode(newSpace);
_SpaceDictionary[_CurrentLevel.Id.IntegerValue].Add(spaceZoneNode);
SpaceCollection.Add(spaceZoneNode);
}
}
else
{
TaskDialog.Show("Revit", "You can't create spaces in this plan view");
}
}
catch (Exception ex)
{
TaskDialog.Show("Revit", ex.Message);
}
}
public void CreateZone()
{
Document activeDocument = CommandData.Application.ActiveUIDocument.Document;
if (_DefaultPhase == null)
{
TaskDialog.Show("Revit", "The phase of active view is null, you can't create zone in null phase");
return;
}
try
{
Zone newZone = activeDocument.Create.NewZone(_CurrentLevel, _DefaultPhase);
if (newZone != null)
{
ZoneSpacesNode zoneSpacesNode = new ZoneSpacesNode(newZone);
_ZoneDictionary[_CurrentLevel.Id.IntegerValue].Add(zoneSpacesNode);
ZoneCollection.Add(zoneSpacesNode);
}
}
catch (Exception ex)
{
TaskDialog.Show("Revit", ex.Message);
}
}
public void UpdateObservableCollections()
{
SpaceCollection.Clear();
foreach (SpaceZoneNode item in _SpaceDictionary[_CurrentLevel.Id.IntegerValue])
{
SpaceCollection.Add(item);
}
ZoneCollection.Clear();
foreach (ZoneSpacesNode item in _ZoneDictionary[_CurrentLevel.Id.IntegerValue])
{
ZoneCollection.Add(item);
}
}
public void UpdateAvailableSpaceCollection(ZoneSpacesNode currentZoneSpacesNode)
{
AvailableSpaceCollection.Clear();
foreach (SpaceZoneNode item in _SpaceDictionary[_CurrentLevel.Id.IntegerValue])
{
if (item.ZoneName != currentZoneSpacesNode.ZoneName)
{
AvailableSpaceCollection.Add(item);
}
}
}
public void UpdateCurrentZoneSpaceCollection(ZoneSpacesNode currentZoneSpacesNode)
{
CurrentZoneSpaceCollection.Clear();
foreach (Space item in currentZoneSpacesNode.SpaceList)
{
CurrentZoneSpaceCollection.Add(new SpaceZoneNode(item));
}
}
public void SelectedZoneAddSpaces(IList selectedSpaces, ZoneSpacesNode selectedZone)
{
//List<SpaceZoneNode> selectedSpaceList = new List<SpaceZoneNode>();
SpaceSet spaceSet = new SpaceSet();
//foreach (SpaceZoneNode item in selectedSpaces)
//{
// selectedSpaceList.Add(item);
// _ = spaceSet.Insert(item.Space);
//}
//foreach (SpaceZoneNode item in selectedSpaceList)
//{
// _ = AvailableSpaceCollection.Remove(item);
// CurrentZoneSpaceCollection.Add(item);
//}
for (int i = 0; i < selectedSpaces.Count; i++)
{
SpaceZoneNode spaceZoneNode = selectedSpaces[i] as SpaceZoneNode;
if (spaceZoneNode != null)
{
spaceSet.Insert(spaceZoneNode.Space);
AvailableSpaceCollection.Remove(spaceZoneNode);
CurrentZoneSpaceCollection.Add(spaceZoneNode);
}
}
_ = selectedZone.Zone.AddSpaces(spaceSet);
}
internal void SelectedZoneRemoveSpaces(IList selectedSpaces, ZoneSpacesNode selectedZone)
{
List<SpaceZoneNode> selectedSpaceList = new List<SpaceZoneNode>();
SpaceSet spaceSet = new SpaceSet();
foreach (SpaceZoneNode item in selectedSpaces)
{
selectedSpaceList.Add(item);
_ = spaceSet.Insert(item.Space);
}
foreach (SpaceZoneNode item in selectedSpaceList)
{
_ = CurrentZoneSpaceCollection.Remove(item);
AvailableSpaceCollection.Add(item);
}
_ = selectedZone.Zone.RemoveSpaces(spaceSet);
}
}
}