-
Notifications
You must be signed in to change notification settings - Fork 1
/
BimbotDocument.cs
224 lines (184 loc) · 7.5 KB
/
BimbotDocument.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
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.UI;
using Bimbot.Bcf;
using Bimbot.Objects;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Bimbot
{
public class BimbotDocument: INotifyPropertyChanged
{
#region Static members and functions
private static Schema RvtSchema;
private static readonly Guid RvtGuid = new Guid("a54b4c89-0ee7-4fae-8fbd-b13b04f00a01"); //b13b04 = bimbot, f00 = bimbot data, a01 = version 0.1
private const String ProvidersField = "Providers";
private const String ServicesField = "Services";
private static Guid FindOrCreateSchema(Document doc)
{
// find schema
RvtSchema = Schema.Lookup(RvtGuid);
try
{
// Find or read subschemas needed
Guid provGuid = Provider.FindOrCreateSchema(doc);
Guid servGuid = Service.FindOrCreateSchema(doc);
// create schema if not found
if (RvtSchema == null)
{
// Start transaction
Transaction createSchema = new Transaction(doc, "Create Bimbot Schema");
createSchema.Start();
// Build schema
SchemaBuilder schemaBldr = new SchemaBuilder(RvtGuid);
// Set read and write access attributes and a name
schemaBldr.SetReadAccessLevel(AccessLevel.Application);
schemaBldr.SetWriteAccessLevel(AccessLevel.Application);
schemaBldr.SetApplicationGUID(RevitBimbot.ApplicationGuid);
schemaBldr.SetVendorId(RevitBimbot.VendorId);
schemaBldr.SetSchemaName("BimBotDocument");
// create a field to store services
FieldBuilder fieldBldr = schemaBldr.AddMapField(ProvidersField, typeof(string), typeof(Entity));
fieldBldr.SetDocumentation("Dictionary of providers by url of service(s) in this project.");
fieldBldr.SetSubSchemaGUID(provGuid);
fieldBldr = schemaBldr.AddArrayField(ServicesField, typeof(Entity));
fieldBldr.SetDocumentation("List of services assigned to this project.");
fieldBldr.SetSubSchemaGUID(servGuid);
RvtSchema = schemaBldr.Finish(); // register the Schema object
createSchema.Commit();
}
else
{
RvtSchema = Schema.Lookup(RvtGuid);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
return RvtGuid;
}
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
#endregion
private Document RvtDocument { get; }
public string Name { get; }
private Entity RvtEntity { get; set; }
public bool IsUpToDate;
public Dictionary<string, Provider> RegisteredProviders { get; }
public ObservableCollection<Service> AssignedServices { get; set; }
public ObservableCollection<Service> AvailableServices { get; set; }
public ObservableCollection<ResultItem> ResultItems { get; set; }
private List<Task> Tasks { get; }
private CancellationTokenSource cancellationToken;
public BimbotDocument(Document document)
{
RvtDocument = document;
Name = RvtDocument.PathName.Substring(RvtDocument.PathName.LastIndexOf('\\') + 1);
RegisteredProviders = new Dictionary<string, Provider>();
AssignedServices = new ObservableCollection<Service>();
ResultItems = new ObservableCollection<ResultItem>();
// Find or add Schema
FindOrCreateSchema(RvtDocument);
// Read the Bimbot data in the project
RvtEntity = RvtDocument.ProjectInformation.GetEntity(RvtSchema);
if (RvtEntity.IsValid())
{
IDictionary<string, Entity> providers = RvtEntity.Get<IDictionary<string, Entity>>(ProvidersField);
foreach (KeyValuePair<string, Entity> pair in providers)
{
Provider readProvider = new Provider(pair.Value);
RegisteredProviders.Add(pair.Key, readProvider);
}
IList<Entity> services = RvtEntity.Get<IList<Entity>>(ServicesField);
foreach (Entity serviceEnt in services)
{
Service readService = new Service(serviceEnt, RegisteredProviders);
readService.InitResults(ResultItems);
AssignedServices.Add(readService);
}
}
else
RvtEntity = null;
IsUpToDate = true;
}
public void AddService(Service service)
{
try
{
// Add the provider and service in memory
AssignedServices.Add(service);
service.InitResults(ResultItems);
// service.AssignOrCreateProvider(RegisteredProviders);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public void DelService(Service service)
{
// if (runningServices.Contains(service))
AssignedServices.Remove(service);
service.CleanResults();
//Provider is not removed since the registration remains valid even without services using it
}
public void WriteToRevit()
{
try
{
// Add the provider and service to Revit document
Transaction transaction = new Transaction(RvtDocument, "Write BimbotDocument to revit");
transaction.Start();
// Create the bimbot entity in the document if not exists
if (RvtEntity == null)
RvtEntity = new Entity(RvtSchema);
Dictionary<string, Entity> ProvidersMap = new Dictionary<string, Entity>();
foreach (KeyValuePair<string, Provider> pair in RegisteredProviders)
ProvidersMap.Add(pair.Key, pair.Value.GetUpdatedRevitEntity());
RvtEntity.Set<IDictionary<string, Entity>>(ProvidersField, ProvidersMap); // set the value for this entity
List<Entity> ServicesEntity = new List<Entity>();
foreach (Service service in AssignedServices)
ServicesEntity.Add(service.GetUpdatedRevitEntity());
RvtEntity.Set<IList<Entity>>(ServicesField, ServicesEntity);
RvtDocument.ProjectInformation.SetEntity(RvtEntity); // store the entity in the element
transaction.Commit();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public void Close()
{
// Set cancellationToken to cancel
if (cancellationToken != null)
cancellationToken.Cancel();
if (AssignedServices != null && AssignedServices.Count > 0)
AssignedServices.Clear();
if (Tasks != null && Tasks.Count > 0)
Tasks.Clear();
// Write any (not stored) data?
}
}
}