-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONInspector.cs
172 lines (168 loc) · 7.01 KB
/
JSONInspector.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
using Elements;
using Elements.Geometry;
using Elements.Geometry.Solids;
using Elements.Serialization.JSON;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
namespace JSONInspector
{
public static class JSONInspector
{
/// <summary>
/// The JSONInspector function.
/// </summary>
/// <param name="model">The input model.</param>
/// <param name="input">The arguments to the execution.</param>
/// <returns>A JSONInspectorOutputs instance containing computed results and the model with any new elements.</returns>
public static JSONInspectorOutputs Execute(Dictionary<string, Model> inputModels, JSONInspectorInputs input)
{
var output = new JSONInspectorOutputs();
if (!string.IsNullOrEmpty(input.JSON))
{
object deserializedAnon = null;
try
{
deserializedAnon = JsonConvert.DeserializeObject(input.JSON);
}
catch
{
// assume json was escaped and try to unescape it
try
{
static string Unescape(string str)
{
return str
.Replace("\\n", "\n")
.Replace("\\r", "\r")
.Replace("\\t", "\t")
.Replace("\\\"", "\"")
.Replace("\\\\", "\\");
}
deserializedAnon = JsonConvert.DeserializeObject(Unescape(input.JSON));
}
catch
{
output.Warnings.Add($"Could not deserialize {input.JSON}");
}
}
if (deserializedAnon == null)
{
return output;
}
// check if it's an array
if (deserializedAnon is JArray array)
{
foreach (var item in array)
{
if (item is JObject jObject)
{
ProcessObject(jObject, output);
}
}
}
else if (deserializedAnon is JObject jObject)
{
ProcessObject(jObject, output);
}
}
return output;
}
private static Random Random = new Random(11);
private static void ProcessObject(JObject item, JSONInspectorOutputs output)
{
try
{
// If it has "Transform" and "Elements" properties, it's a model. Use Model.FromJson to deserialize it.
if (item.ContainsKey("Transform") && item.ContainsKey("Elements"))
{
var model = Model.FromJson(item.ToString());
output.Model.AddElements(model.Elements.Values);
}
else if (item.ContainsKey("Perimeter")) // profile
{
var profile = JsonConvert.DeserializeObject<Profile>(item.ToString());
var geoElem = new GeometricElement
{
Representation = new Lamina(profile),
Material = Random.NextMaterial()
};
output.Model.AddElement(geoElem);
output.Model.AddElements(profile.ToModelCurves());
profile.Perimeter.Vertices.ToList().ForEach(v => output.Model.AddElement(RenderPoint(v)));
profile.Voids?.ToList()?.ForEach(v => v.Vertices.ToList().ForEach(vv => output.Model.AddElement(RenderPoint(vv))));
}
else if (item.ContainsKey("Id") && item.ContainsKey("discriminator") && item.ContainsKey("Representation"))
{
try
{
var element = JsonConvert.DeserializeObject<GeometricElement>(item.ToString());
output.Model.AddElement(element);
}
catch
{
output.Warnings.Add($"Could not deserialize {item["discriminator"]} with id {item["Id"]}");
}
}
else if (item.ContainsKey("Id") && item.ContainsKey("discriminator"))
{
try
{
var element = JsonConvert.DeserializeObject<GenericElement>(item.ToString());
element.discriminator = item["discriminator"].ToString();
element.AdditionalProperties["discriminator"] = item["discriminator"].ToString();
output.Model.AddElement(element);
}
catch
{
output.Warnings.Add($"Could not deserialize {item["discriminator"]} with id {item["Id"]}");
}
}
else if (item.ContainsKey("Vertices"))
{
var poly = JsonConvert.DeserializeObject<Polyline>(item.ToString());
output.Model.AddElement(new ModelCurve(poly));
}
else if (item.ContainsKey("X") && item.ContainsKey("Y") && item.ContainsKey("Z"))
{
var vec = JsonConvert.DeserializeObject<Vector3>(item.ToString());
output.Model.AddElement(RenderPoint(vec));
}
else if (item.ContainsKey("Matrix"))
{
var transform = JsonConvert.DeserializeObject<Transform>(item.ToString());
output.Model.AddElements(transform.ToModelCurves());
}
else if (item.ContainsKey("Min") && item.ContainsKey("Max"))
{
var bbox = JsonConvert.DeserializeObject<BBox3>(item.ToString());
output.Model.AddElements(bbox.ToModelCurves());
}
else
{
output.Warnings.Add($"Could not deserialize {item}");
}
}
catch
{
output.Warnings.Add($"Could not deserialize {item}");
}
}
private static MeshElement PointMesh = null;
private static Element RenderPoint(Vector3 vec)
{
if (PointMesh == null)
{
var m = Mesh.Sphere(0.1, 10);
PointMesh = new MeshElement(m, new Transform())
{
IsElementDefinition = true,
Material = BuiltInMaterials.XAxis
};
}
return PointMesh.CreateInstance(new Transform(vec), $"Point {vec.X:0.0}, {vec.Y:0.0}, {vec.Z:0.0}");
}
}
}