-
Notifications
You must be signed in to change notification settings - Fork 636
/
Copy pathWorkspaceInfo.cs
331 lines (299 loc) · 13.8 KB
/
WorkspaceInfo.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System;
using System.Diagnostics;
using System.Globalization;
using System.Xml;
using Dynamo.Logging;
using Dynamo.Models;
using Dynamo.Utilities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Dynamo.Graph.Workspaces
{
/// <summary>
/// Contains sufficient data to create a <see cref="WorkspaceModel"/> object
/// </summary>
public class WorkspaceInfo
{
//in dynamo 1.x the workspace name for home workspaces was always "Home" no matter what the language was.
private const string dynamo1HomeWorkspaceNameString = "Home";
public WorkspaceInfo(string id, string name, string description, RunType runType)
{
ID = id;
Name = name;
Description = description;
Zoom = 1.0;
X = 0;
Y = 0;
RunType = runType;
RunPeriod = RunSettings.DefaultRunPeriod;
HasRunWithoutCrash = true;
}
/// <summary>
/// Initializes a new instance of the <see cref="WorkspaceInfo"/> class
/// with default workspace data.
/// </summary>
public WorkspaceInfo()
{
Zoom = 1.0;
X = 0;
Y = 0;
RunType = RunType.Automatic;
RunPeriod = RunSettings.DefaultRunPeriod;
HasRunWithoutCrash = true;
}
internal static bool FromXmlDocument(XmlDocument xmlDoc, string path, bool isTestMode,
bool forceManualExecutionMode, ILogger logger, out WorkspaceInfo workspaceInfo)
{
try
{
string funName = null;
double cx = 0;
double cy = 0;
double zoom = 1.0;
double scaleFactor = 1.0;
string id = "";
string category = "";
string description = "";
string version = "";
var runType = RunType.Manual;
int runPeriod = RunSettings.DefaultRunPeriod;
bool hasRunWithoutCrash = false;
bool isVisibleInDynamoLibrary = true;
var topNode = xmlDoc.GetElementsByTagName("Workspace");
// legacy support
if (topNode.Count == 0)
{
topNode = xmlDoc.GetElementsByTagName("dynWorkspace");
}
// load the header
foreach (XmlNode node in topNode)
{
foreach (XmlAttribute att in node.Attributes)
{
if (att.Name.Equals("X"))
cx = double.Parse(att.Value, CultureInfo.InvariantCulture);
else if (att.Name.Equals("Y"))
cy = double.Parse(att.Value, CultureInfo.InvariantCulture);
else if (att.Name.Equals("zoom"))
zoom = double.Parse(att.Value, CultureInfo.InvariantCulture);
else if (att.Name.Equals("ScaleFactor"))
scaleFactor = double.Parse(att.Value, CultureInfo.InvariantCulture);
else if (att.Name.Equals("Name"))
funName = att.Value;
else if (att.Name.Equals("ID"))
id = att.Value;
else if (att.Name.Equals("Category"))
category = att.Value;
else if (att.Name.Equals("Description"))
description = att.Value;
else if (att.Name.Equals("HasRunWithoutCrash"))
hasRunWithoutCrash = bool.Parse(att.Value);
else if (att.Name.Equals("IsVisibleInDynamoLibrary"))
isVisibleInDynamoLibrary = bool.Parse(att.Value);
else if (att.Name.Equals("Version"))
version = att.Value;
else if (att.Name.Equals("RunType"))
{
if (forceManualExecutionMode || !Enum.TryParse(att.Value, false, out runType))
{
runType = RunType.Manual;
}
}
else if (att.Name.Equals("RunPeriod"))
runPeriod = Int32.Parse(att.Value);
}
}
// we have a dyf and it lacks an ID field, we need to assign it
// a deterministic guid based on its name. By doing it deterministically,
// files remain compatible
if (string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(funName) && funName != dynamo1HomeWorkspaceNameString)
{
id = GuidUtility.Create(GuidUtility.UrlNamespace, funName).ToString();
}
workspaceInfo = new WorkspaceInfo
{
ID = id,
Name = funName,
X = cx,
Y = cy,
Zoom = zoom,
ScaleFactor = scaleFactor,
FileName = path,
Category = category,
Description = description,
Version = version,
RunType = runType,
RunPeriod = runPeriod,
HasRunWithoutCrash = hasRunWithoutCrash,
IsVisibleInDynamoLibrary = isVisibleInDynamoLibrary
};
return true;
}
catch (Exception ex)
{
logger.Log(Properties.Resources.OpenWorkbenchError);
logger.Log(ex);
Debug.WriteLine(ex.Message + ":" + ex.StackTrace);
//TODO(Steve): Need a better way to handle this kind of thing. -- MAGN-5712
if (isTestMode)
throw; // Rethrow for NUnit.
workspaceInfo = null;
return false;
}
}
/// <summary>
/// Return a boolean indicating if successfully deserialized workspace info object from Json file
/// </summary>
/// <param name="jsonDoc">Target Josn</param>
/// <param name="path">Target path</param>
/// <param name="isTestMode">Boolean indicating if Dynamo is running in Test Mode</param>
/// <param name="forceManualExecutionMode">Boolean indicating if forcing manual mode</param>
/// <param name="logger">Dynamo logger</param>
/// <param name="workspaceInfo">Return object</param>
/// <returns>A boolean indicating success</returns>
internal static bool FromJsonDocument(String jsonDoc, string path, bool isTestMode,
bool forceManualExecutionMode, ILogger logger, out WorkspaceInfo workspaceInfo)
{
var jObject = (JObject)JsonConvert.DeserializeObject(jsonDoc);
try
{
double cx = 0;
double cy = 0;
double zoom = 1.0;
double scaleFactor = 1.0;
string version = "";
var runType = RunType.Manual;
int runPeriod = RunSettings.DefaultRunPeriod;
bool hasRunWithoutCrash = false;
bool isVisibleInDynamoLibrary = true;
JToken value;
string funName = jObject.TryGetValue("Name", out value)? value.ToString(): "";
string id = jObject.TryGetValue("Uuid", out value) ? value.ToString() : "";
string category = jObject.TryGetValue("Category", out value) ? value.ToString() : "";
string description = jObject.TryGetValue("Description", out value) ? value.ToString() : "";
// we have a dyf and it lacks an ID field, we need to assign it
// a deterministic guid based on its name. By doing it deterministically,
// files remain compatible
//TODO(mjk) we should get rid of this and throw instead or use the isCustomNode flag to determine this
//since non home names are now valid in json format.
if (string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(funName) && funName != dynamo1HomeWorkspaceNameString)
{
id = GuidUtility.Create(GuidUtility.UrlNamespace, funName).ToString();
}
// Parse the following info when graph contains a "View" block
if (jObject.TryGetValue("View", out value))
{
JObject viewObject = value.ToObject<JObject>();
Double.TryParse((viewObject.TryGetValue("X", out value) ? value.ToString(): "0"), out cx);
Double.TryParse((viewObject.TryGetValue("Y", out value) ? value.ToString() : "0"), out cy);
Double.TryParse((viewObject.TryGetValue("Zoom", out value) ? value.ToString() : "1.0"), out zoom);
// Parse the following info when "View" block contains a "Dynamo" block
if (viewObject.TryGetValue("Dynamo", out value))
{
JObject dynamoObject = value.ToObject<JObject>();
Double.TryParse((dynamoObject.TryGetValue("ScaleFactor", out value) ? value.ToString(): "1.0"), out scaleFactor);
Boolean.TryParse((dynamoObject.TryGetValue("HasRunWithoutCrash", out value) ? value.ToString(): "false"), out hasRunWithoutCrash);
Boolean.TryParse((dynamoObject.TryGetValue("IsVisibleInDynamoLibrary", out value) ? value.ToString(): "true"), out isVisibleInDynamoLibrary);
version = dynamoObject.TryGetValue("Version", out value)? value.ToString() : "";
if (forceManualExecutionMode || !Enum.TryParse((dynamoObject.TryGetValue("RunType", out value)? value.ToString(): "false"), false, out runType))
{
runType = RunType.Manual;
}
Int32.TryParse((dynamoObject.TryGetValue("RunPeriod", out value)? value.ToString() : RunSettings.DefaultRunPeriod.ToString()), out runPeriod);
}
}
workspaceInfo = new WorkspaceInfo
{
ID = id,
Name = funName,
X = cx,
Y = cy,
Zoom = zoom,
ScaleFactor = scaleFactor,
FileName = path,
Category = category,
Description = description,
Version = version,
RunType = runType,
RunPeriod = runPeriod,
HasRunWithoutCrash = hasRunWithoutCrash,
IsVisibleInDynamoLibrary = isVisibleInDynamoLibrary
};
return true;
}
catch (Exception ex)
{
logger.Log(Properties.Resources.OpenWorkbenchError);
logger.Log(ex);
Debug.WriteLine(ex.Message + ":" + ex.StackTrace);
if (isTestMode)
throw; // Rethrow for NUnit.
workspaceInfo = null;
return false;
}
}
/// <summary>
/// Returns version of Dynamo where the workspace was created
/// </summary>
public string Version { get; internal set; }
/// <summary>
/// Returns description of the workspace
/// </summary>
public string Description { get; internal set; }
/// <summary>
/// Returns full category name of custom node
/// </summary>
public string Category { get; internal set; }
/// <summary>
/// Returns X coordinate of top left corner of visible workspace part
/// </summary>
public double X { get; internal set; }
/// <summary>
/// Returns Y coordinate of top left corner of visible workspace part
/// </summary>
public double Y { get; internal set; }
/// <summary>
/// Returns zoom value of the workspace
/// </summary>
public double Zoom { get; internal set; }
/// <summary>
/// Returns the scale factor for ProtoGeometry geometries
/// </summary>
public double ScaleFactor { get; internal set; }
/// <summary>
/// Returns name of the workspace
/// </summary>
public string Name { get; internal set; }
/// <summary>
/// Returns <see cref="System.Guid"/> identifier string value of custom node workspace
/// </summary>
public string ID { get; internal set; }
/// <summary>
/// Returns file name of the workspace
/// </summary>
public string FileName { get; internal set; }
/// <summary>
/// Returns run type of the home workspace
/// </summary>
public RunType RunType { get; internal set; }
/// <summary>
/// Returns run period value of the home workspace if RunType is Periodic
/// </summary>
public int RunPeriod { get; internal set; }
/// <summary>
/// Indicates if the workspace was executed successfully at last time
/// </summary>
public bool HasRunWithoutCrash { get; internal set; }
/// <summary>
/// Indicates if the custom node is visible node library
/// </summary>
public bool IsVisibleInDynamoLibrary { get; internal set; }
/// <summary>
/// Indicates whether the workspace is custom node or home workspace
/// </summary>
public bool IsCustomNodeWorkspace
{
get { return !string.IsNullOrEmpty(ID); }
}
}
}