-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExternalEventRunServices.cs
157 lines (131 loc) · 5.48 KB
/
ExternalEventRunServices.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
extern alias IFCExportUIOverride;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.UI;
using Bimbot.BimbotUI;
using Bimbot.Objects;
using Bimbot.Utils;
using IFCExportUIOverride::BIM.IFC.Export.UI;
// ReSharper disable UnusedMember.Global
namespace Bimbot.ExternalEvents
{
public class ExtEvntRunServices : IExternalEventHandler
{
public List<Service> services = new List<Service>();
public ExternalEventsContainer extEvents;
private CancellationTokenSource cts;
private BackgroundWorker bgw;
private Document doc;
public void Execute(UIApplication app)
{
doc = app.ActiveUIDocument.Document;
try
{
Dictionary<string, byte[]> ifcData = new Dictionary<string, byte[]>();
foreach (Service service in services)
{
// Skip creation of ifcData generation when this already exists
// (checked by ifcexportconfiguration name of the service)
//
if (ifcData.ContainsKey(service.IfcExportConfiguration))
continue;
// Export to IFC according to all used Ifc export configurations
IFCExportConfigurationsMapCustom configurationsMap = new IFCExportConfigurationsMapCustom();
foreach (IFCExportConfigurationCustom config in configurationsMap.Values)
{
if (config.Name.Equals(service.IfcExportConfiguration))
{
IFCExportOptions IFCOptions = new IFCExportOptions();
//Get the current view Id, or -1 if you want to export the entire model
config.ActiveViewId = -1;
//Update the IFCExportOptions
config.UpdateOptions(IFCOptions, null);
string filename = IfcUtils.ExportProjectToIFC(doc, IFCOptions);
ifcData.Add(config.Name, File.ReadAllBytes(filename));
File.Delete(filename);
}
}
}
BackgroundWorker bgw = new BackgroundWorker();
bgw.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_Completed);
bgw.RunWorkerAsync(new Tuple<Document, Dictionary<string, byte[]>>(doc, ifcData));
}
catch (Exception e)
{
MessageBox.Show("Failed to create IFC data to send to services.");
}
}
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
Tuple<Document, Dictionary<string,byte[]>> args = (Tuple<Document, Dictionary<string, byte[]>>)e.Argument;
cts = new CancellationTokenSource();
//RunServices(args.Item1, args.Item2, cts.Token);
Task task = RunServicesAsync(args.Item1, args.Item2, cts.Token);
task.Wait();
}
private void bgWorker_Completed(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show("A service has failed!");
}
else
{
Bimbot.RevitBimbot.UpdateDocument(doc);
MessageBox.Show("Finished all services!");
}
}
private async Task RunServicesAsync(Document doc, Dictionary<string,byte[]> data, CancellationToken ct)
{
Dictionary<Task<String>, Service> taskToService = new Dictionary<Task<String>, Service>();
List<Task<String>> tasks = new List<Task<String>>();
// Create list of tasks from services to run
foreach (Service curService in services)
{
//
if (!data.ContainsKey(curService.IfcExportConfiguration))
{
MessageBox.Show(curService.Name + " not executed due to unavailable \n" +
"ifc export configuration (" + curService.IfcExportConfiguration + ")");
break;
}
Task<string> task = curService.RunAsync(data[curService.IfcExportConfiguration]);
tasks.Add(task);
taskToService.Add(task, curService);
}
// Excute tasks (services) and handle the first to finish
while (tasks.Count > 0)
{
Task<String> firstFinishedTask = await Task.WhenAny(tasks);
Service curService = taskToService[firstFinishedTask];
tasks.Remove(firstFinishedTask);
taskToService.Remove(firstFinishedTask);
// MessageBox.Show("Finished task '" + curService.Name + "' with response: \n" + res.Substring(0, 300));
}
}
private void RunServices(Document doc, byte[] data, CancellationToken ct)
{
Dictionary<Task<String>, Service> taskToService = new Dictionary<Task<String>, Service>();
List<Task<String>> tasks = new List<Task<String>>();
// Create list of tasks from services to run
foreach (Service curService in services)
{
string res = curService.Run(data);
// MessageBox.Show("Finished task '" + curService.Name + "' with response: \n" + res.Substring(0, 300));
}
}
public string GetName()
{
return "Run Service";
}
}
}