-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMainForm.cs
293 lines (256 loc) · 11.3 KB
/
MainForm.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.IO.Compression;
namespace AutoCADIODemo
{
public partial class MainForm : Form
{
private Dictionary<String, String> storedResults;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
storedResults = new Dictionary<string, string>();
// Initial setup to get started
Autodesk.AcadIOUtils.SetupAutoCADIOContainer(Properties.Settings.Default.AutoCADIOClientId, Properties.Settings.Default.AutoCADIOClientSecret);
// Bucket name that we will use in Amazon S3 for uploading our drawings
Autodesk.GeneralUtils.S3BucketName = Properties.Settings.Default.S3BucketName;
// Loads the drawing names in list
LoadDrawingsList();
// Loads the activity names in combobox
LoadActivitiesList();
}
// Loads the drawing names in list
private void LoadActivitiesList()
{
ActivityComboBox.Items.Clear();
Dictionary<String, String> activityDetails = Autodesk.AcadIOUtils.GetActivityDetails();
if (activityDetails.Count > 0)
{
foreach (KeyValuePair<String, String> kp in activityDetails)
{
ActivityComboBox.Items.Add(kp.Key);
}
ActivityComboBox.SelectedIndex = 0;
}
}
// Loads the activity names in combobox
private void LoadDrawingsList()
{
DrawingsList.Items.Clear();
if (Directory.Exists( Properties.Settings.Default.DrawingsFolderPath) == false)
{
System.Windows.Forms.MessageBox.Show("Drawings folder path not found ! Please set the path using Settings.");
}
else
{
DirectoryInfo di = new DirectoryInfo( Properties.Settings.Default.DrawingsFolderPath);
FileInfo[] fi = di.GetFiles("*.dwg");
foreach (FileInfo info in fi)
{
//drawings.Add(info.FullName);
DrawingsList.Items.Add(Path.GetFileName(info.FullName));
}
//
// we do not want the preview to start immediately on loading.
// this could delay startup, so let user click on a drawing from the list.
//if (DrawingsList1.Items.Count > 0)
// DrawingsList1.SelectedIndex = 0;
}
}
/// <summary>
/// Generates preview image of the drawing file path passed to it as parameter using the PlotToPDF activity.
/// its result is displayed in a web browser.
/// </summary>
/// <param name="drawingPath"></param>
private void GetAutoCADIOResult(string drawingPath, String activityId, bool considerStoredResult)
{
try
{
Cursor.Current = Cursors.WaitCursor;
String storedResultUrl = String.Empty;
if(storedResults.ContainsKey(drawingPath))
storedResultUrl = storedResults[drawingPath];
if (considerStoredResult &&
! String.IsNullOrEmpty(storedResultUrl))
{
UpdateStatusText(String.Format("URL : {0}", storedResultUrl));
// Display stored result
String localFilePath = String.Empty;
if (Autodesk.GeneralUtils.Download(storedResultUrl, ref localFilePath))
{
if (storedResultUrl.Contains(".pdf"))
{
webBrowser.Url = new Uri(storedResultUrl);
UpdateStatusText(String.Format("Result downloaded to {0}", localFilePath));
}
else
{
UpdateWebbrowserMsg(String.Format("Result downloaded to <strong>{0}</strong>", localFilePath));
}
}
else
{
UpdateWebbrowserMsg(String.Format("Sorry, could not downloaded from <strong>{0}</strong>", storedResultUrl));
}
}
else
{
UpdateStatusText("Uploading drawing to S3");
// Step 1 : Upload the drawing to S3 storage
String hostDwgS3Url = Autodesk.GeneralUtils.UploadDrawingToS3(drawingPath);
if (String.IsNullOrEmpty(hostDwgS3Url))
return;
UpdateStatusText("Submitting workitem to AutoCADIO");
// Step 2 : Submit a AutoCADIO Workitem using the activity id
String resulturl = Autodesk.AcadIOUtils.SubmitWorkItem(activityId, hostDwgS3Url);
// Step 3 : Display the result in a web browser and download the result
if (String.IsNullOrEmpty(resulturl) == false)
{
UpdateStatusText(String.Format("URL : {0}", resulturl));
Download(resulturl);
// Store the result if needed
if (String.IsNullOrEmpty(storedResultUrl) == false)
storedResults.Remove(drawingPath);
storedResults.Add(drawingPath, resulturl);
}
else
UpdateStatusText("workitem result url is empty !");
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Cursor.Current = Cursors.Default;
}
}
// Downloads the result from the url and displays the result in a browser control
private void Download(String url)
{
try
{
// Load the url in web browser
String localFilePath = String.Empty;
if (Autodesk.GeneralUtils.Download(url, ref localFilePath))
{
if (!String.IsNullOrEmpty(localFilePath))
{
if (url.Contains(".pdf"))
{
webBrowser.Url = new Uri(url);
UpdateStatusText(String.Format("Result downloaded to {0}", localFilePath));
}
else
{
UpdateWebbrowserMsg(String.Format("Result downloaded to <strong>{0}</strong>", localFilePath));
}
}
else
{
UpdateWebbrowserMsg(String.Format("Sorry, could not downloaded from <strong>{0}</strong>", url));
}
}
}
catch (System.UriFormatException)
{
System.Windows.Forms.MessageBox.Show(url + "could not be loaded.");
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void UpdateStatusText(String msg)
{
StatusLabel.Text = msg;
}
private void UpdateWebbrowserMsg(String htmlmsg)
{
webBrowser.Navigate("about:blank");
webBrowser.Document.Write(String.Format("<html><body>{0}</body></html>", htmlmsg));
webBrowser.Refresh();
}
/// <summary>
/// when a different drawing is selected in the listbox, update the preview result.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DrawingsList_SelectedValueChanged(object sender, EventArgs e)
{
if (DrawingsList.SelectedIndex != -1)
{
// Get the currently selected drawing and get result for the selected activity from AutoCADIO
GetAutoCADIOResult(Path.Combine(Properties.Settings.Default.DrawingsFolderPath, DrawingsList.SelectedItem.ToString()), ActivityComboBox.SelectedItem.ToString(), false);
}
}
// Display settings form to set up the client id and client secret.
private void SettingsButton_Click(object sender, EventArgs e)
{
string autocadioclientid = Properties.Settings.Default.AutoCADIOClientId;
string autocadioclientsecret = Properties.Settings.Default.AutoCADIOClientSecret;
string drawingsFolderPath = Properties.Settings.Default.DrawingsFolderPath;
SettingsForm sf = new SettingsForm();
if (sf.ShowDialog() == DialogResult.OK)
{
if (drawingsFolderPath != Properties.Settings.Default.DrawingsFolderPath)
{
LoadDrawingsList();
storedResults.Clear();
}
drawingsFolderPath = Properties.Settings.Default.DrawingsFolderPath;
if (autocadioclientid != Properties.Settings.Default.AutoCADIOClientId ||
autocadioclientsecret != Properties.Settings.Default.AutoCADIOClientSecret)
{
Autodesk.AcadIOUtils.SetupAutoCADIOContainer(Properties.Settings.Default.AutoCADIOClientId, Properties.Settings.Default.AutoCADIOClientSecret);
}
}
}
// About this sample
private void AboutButton_Click(object sender, EventArgs e)
{
String sContent = "AutoCADIO Demo 1.0\nDeveloper Technical Services\nAutodesk";
System.Windows.Forms.MessageBox.Show(sContent, "About AutoCADIO Demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
// Get the result again for the selected activity / drawing
private void RefreshButton_Click(object sender, EventArgs e)
{
if (DrawingsList.SelectedIndex != -1)
{
// Get the currently selected drawing and get result for the selected activity from AutoCADIO
GetAutoCADIOResult(Path.Combine(Properties.Settings.Default.DrawingsFolderPath, DrawingsList.SelectedItem.ToString()), ActivityComboBox.SelectedItem.ToString(), false);
}
}
// Activity changed
private void ActivityComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
storedResults.Clear();
}
// Display the activity form to create / delete activities
private void ActivityBtn_Click(object sender, EventArgs e)
{
ActivitiesForm af = new ActivitiesForm();
af.ShowDialog();
LoadActivitiesList();
}
// Display the appPackage form to create / delete appPackages
private void PackageBtn_Click(object sender, EventArgs e)
{
AppPackageForm pf = new AppPackageForm();
pf.ShowDialog();
}
}
}