-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNode.cs
207 lines (172 loc) · 4.85 KB
/
Node.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
using Autodesk.Revit.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Diagnostics;
namespace RevitGltfExporter
{
public class Node
{
private int _id;
[JsonIgnore]
public int id
{
get => _id;
set
{
if (isMesh && !_meshId.HasValue) _meshId = _id;
_id = value;
}
}
private string _name;
[JsonIgnore]
public string shortname
{
get
{
return _name;
}
}
[JsonIgnore]
public string uuid;
[JsonIgnore]
public NodeType type;
[JsonIgnore]
public Transform transform
{
set; get;
}
public int? camera;
private List<Node> _children;
public bool hasChild()
{
return (_children != null) && 0 < (_children.Count);
}
public int? mesh
{
get
{
if (isMesh) return Gltf.Instance.indexOfMesh(_meshId.HasValue ? _meshId.Value : id);
return null;
}
}
private int? _meshId;
[JsonIgnore]
public bool isMesh = false;
public object extras
{
get
{
if (type != NodeType.Element) return null;
if (-1 == id) return null;
return new Dictionary<string, object>() { { "id", id }, {"uuid", uuid } };
}
}
public string name
{
get
{
if ( id == -1 ) return _name;
return string.Format("{0}-{1}", _name, id);
}
set
{
_name = value;
}
}
[JsonIgnore]
public bool isEmpty
{
get
{
return ((!isMesh) && (null == _children) && (type != NodeType.Camera));
}
}
public double[] matrix
{
get
{
if ((null == transform) || transform.IsIdentity || (transform.Determinant == 0)) //Determinat is 0, the fransform could not decompose to trs, probably, should ignore the node
return null;
return new double[16] { transform.BasisX.X, transform.BasisX.Y, transform.BasisX.Z, 0, transform.BasisY.X, transform.BasisY.Y, transform.BasisY.Z, 0, transform.BasisZ.X, transform.BasisZ.Y, transform.BasisZ.Z, 0, transform.Origin.X, transform.Origin.Y, transform.Origin.Z, 1 };
}
}
public Node(int id, string uuid, string name, NodeType type, Transform transform = null)
{
this._id = id;
this.uuid = uuid;
this._name = name;
this.type = type;
this.transform = transform;
}
public Node(Node node)
{
this._id = node.id;
this.uuid = node.uuid;
this._name = node._name;
this.type = node.type;
this.transform = node.transform;
this._meshId = node._meshId;
this.isMesh = node.isMesh;
if (!node.hasChild()) return;
_children = new List<Node>();
foreach (Node child in node.getChildren())
{
_children.Add(new Node(child));
}
}
public List<Node> toList()
{
List<Node> ret = new List<Node>();
ret.Add(this);
if (!this.hasChild()) return ret;
foreach (Node child in this.getChildren())
{
ret.AddRange(child.toList());
}
return ret;
}
public Node AddChild(Node node)
{
if (null == _children)
_children = new List<Node>();
_children.Add(node);
return this;
}
[JsonIgnore]
public int index;
public void filterEmptyChildren()
{
if (null == _children) return;
_children = _children.Where((node) => !(node as Node).isEmpty).ToList();
}
public Node GetFirstChild()
{
return _children[0];
}
List<Node> getChildren()
{
return _children;
}
public List<int> children
{
get
{
if ((null == _children) || (_children.Count == 0)) return null;
return _children.Select(node => (node as Node).index).ToList();
}
}
}
[JsonConverter(typeof(StringEnumConverter))]
public enum NodeType
{
Element,
FamilyInstance,
Link,
Camera
}
}