-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathXBimFederation.cs
304 lines (277 loc) · 11.4 KB
/
XBimFederation.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
//
// BIMRL (BIM Rule Language) Simplified Schema ETL (Extract, Transform, Load) library: this library transforms IFC data into BIMRL Simplified Schema for RDBMS.
// This work is part of the original author's Ph.D. thesis work on the automated rule checking in Georgia Institute of Technology
// Copyright (C) 2013 Wawan Solihin ([email protected])
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; If not, see <http://www.gnu.org/licenses/>.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using Xbim.Common.Geometry;
namespace BIMRLDiffModelsCmd
{
class XYZ
{
[JsonProperty("x")]
public double X { get; set; }
[JsonProperty("y")]
public double Y { get; set; }
[JsonProperty("z")]
public double Z { get; set; }
[JsonIgnore]
public static XYZ Zero { get { return new XYZ(0.0, 0.0, 0.0); } }
public XYZ(double XValue, double YValue, double ZValue)
{
X = XValue;
Y = YValue;
Z = ZValue;
}
}
class ModelTransformInfo
{
[JsonProperty("translation")]
public XYZ TranslationInfo { get; set; }
[JsonProperty("scale")]
public XYZ ScaleInfo { get; set; }
[JsonProperty("rotation")]
public XYZ RotationAxisInfo { get; set; }
[JsonProperty("angle")]
public double RotationAngle { get; set; }
public ModelTransformInfo()
{
// Initialize default values for the ModelTransformInfo
TranslationInfo = XYZ.Zero;
ScaleInfo = new XYZ(1.0, 1.0, 1.0);
RotationAxisInfo = new XYZ(0.0, 0.0, 1.0);
RotationAngle = 0.0;
}
}
class FederationModelItem
{
[JsonProperty("modelfilename")]
public string ModelFileName { get; set; }
[JsonProperty("modeltransform")]
public ModelTransformInfo ModelTransform { get; set; }
/// <summary>
/// This is an additional transform to alter the ref model relative to the rest relative to the (0,0,0), invloving Translation, Scale, and Rotation
/// </summary>
[JsonIgnore]
XbimMatrix3D additionalModelTransform
{
get
{
if (ModelTransform == null)
ModelTransform = new ModelTransformInfo();
XbimVector3D rotAxis = new XbimVector3D(ModelTransform.RotationAxisInfo.X, ModelTransform.RotationAxisInfo.Y, ModelTransform.RotationAxisInfo.Z);
rotAxis = rotAxis.Normalized();
//!!!! Not sure that this is correct (follow row? or column?)
double rx = ModelTransform.RotationAxisInfo.X;
double ry = ModelTransform.RotationAxisInfo.Y;
double rz = ModelTransform.RotationAxisInfo.Z;
double ra = (ModelTransform.RotationAngle / 180) * Math.PI;
double m11 = rx * rx + (1 - (rx * rx)) * Math.Cos(ra);
double m12 = rx * ry * (1 - Math.Cos(ra)) - rz * Math.Sin(ra);
double m13 = rx * rz * (1 - Math.Cos(ra)) + ry * Math.Sin(ra);
double m14 = 0.0;
double m21 = rx * ry * (1 - Math.Cos(ra)) + rz * Math.Sin(ra);
double m22 = ry * ry + (1 - (ry * ry)) * Math.Cos(ra);
double m23 = ry * rz * (1 - Math.Cos(ra)) - rx * Math.Sin(ra);
double m24 = 0.0;
double m31 = rx * rz * (1 - Math.Cos(ra)) - ry * Math.Sin(ra);
double m32 = ry * rz * (1 - Math.Cos(ra)) + rx * Math.Sin(ra);
double m33 = rz * rz + (1 - (rz * rz)) * Math.Cos(ra);
double m34 = 0.0;
double m41 = 0.0;
double m42 = 0.0;
double m43 = 0.0;
double m44 = 1.0;
XbimMatrix3D rotMtx = new XbimMatrix3D(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44);
XbimMatrix3D scaleMtx = XbimMatrix3D.CreateScale(ModelTransform.ScaleInfo.X, ModelTransform.ScaleInfo.Y, ModelTransform.ScaleInfo.Z);
XbimMatrix3D transMtx = XbimMatrix3D.CreateTranslation(ModelTransform.TranslationInfo.X, ModelTransform.TranslationInfo.Y, ModelTransform.TranslationInfo.Z);
XbimMatrix3D m3D = XbimMatrix3D.Multiply(rotMtx, scaleMtx);
m3D = XbimMatrix3D.Multiply(m3D, transMtx);
return m3D;
}
}
}
class XBimFederation
{
[JsonProperty("federationname")]
public string FederationName { get; set; }
[JsonProperty("description")]
public string FederationDescription { get; set; }
[JsonProperty("creationdate")]
public DateTime CreationDate { get; set; }
[JsonProperty("createdby")]
public string CreatedBy { get; set; }
[JsonProperty("modifieddate")]
public DateTime ModifiedDate { get; set; }
[JsonProperty("modifiedby")]
public string ModifiedBy { get; set; }
[JsonProperty("userelativepath")]
public bool UseRelativePath { get; set; }
[JsonProperty("membermodels")]
IDictionary<string, FederationModelItem> MemberModels { get; set; }
[JsonIgnore]
public string fedFilePath { get; set; }
public IList<FederationModelItem> GetMemberModelList()
{
string currDir = Directory.GetCurrentDirectory();
if (UseRelativePath)
{
string fedFileLoc = Path.GetDirectoryName(fedFilePath);
// If it is an empty string, it is located in the current directory
if (string.IsNullOrEmpty(fedFileLoc))
fedFileLoc = @".\";
Directory.SetCurrentDirectory(fedFileLoc);
}
IList<FederationModelItem> items = new List<FederationModelItem>();
foreach (KeyValuePair<string,FederationModelItem> modelItem in MemberModels)
{
if (UseRelativePath)
{
string fileName = Path.GetFullPath(modelItem.Value.ModelFileName);
FederationModelItem item = modelItem.Value;
item.ModelFileName = fileName;
items.Add(item);
}
else
items.Add(modelItem.Value);
}
if (UseRelativePath)
Directory.SetCurrentDirectory(currDir);
return items;
}
public XBimFederation(string federationName, string description)
{
FederationName = federationName;
FederationDescription = description;
MemberModels = new Dictionary<string,FederationModelItem>();
}
public void Add(string modelFile)
{
if (MemberModels.ContainsKey(modelFile))
return;
FederationModelItem mItem = new FederationModelItem();
mItem.ModelFileName = modelFile;
mItem.ModelTransform = new ModelTransformInfo();
MemberModels.Add(modelFile, mItem);
}
public void Add(string modelFile, ModelTransformInfo modelTrf)
{
if (MemberModels.ContainsKey(modelFile))
return;
FederationModelItem mItem = new FederationModelItem();
mItem.ModelFileName = modelFile;
mItem.ModelTransform = modelTrf;
MemberModels.Add(modelFile, mItem);
}
public void Remove(string modelFile)
{
string model = modelFile;
if (!MemberModels.ContainsKey(model))
{
if (UseRelativePath)
{
model = XBimFederation.relativePath(Path.GetDirectoryName(fedFilePath), model);
if (!MemberModels.ContainsKey(model))
return;
}
}
MemberModels.Remove(model);
}
public void Save(string fileName, bool firstTime=false)
{
if (firstTime)
{
CreationDate = DateTime.Now;
CreatedBy = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
}
else
{
ModifiedBy = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
ModifiedDate = DateTime.Now;
}
string fedModelInfo = JsonConvert.SerializeObject(this, Formatting.Indented);
using (StreamWriter file = File.CreateText(fileName))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, this);
}
}
[JsonIgnore]
public IList<string> MemberFileList
{
get
{
IList<string> fileList = new List<string>();
foreach (FederationModelItem item in GetMemberModelList())
{
fileList.Add(item.ModelFileName);
}
return fileList;
}
}
public ModelTransformInfo GetTransformAt(int index)
{
if (index > GetMemberModelList().Count())
return null;
return GetMemberModelList()[index].ModelTransform;
}
public static string relativePath(string refDir, string pathToUpdate)
{
if (string.IsNullOrEmpty(refDir) || string.IsNullOrEmpty(pathToUpdate))
return pathToUpdate;
string[] firstPathParts = refDir.Trim(Path.DirectorySeparatorChar).Split(Path.DirectorySeparatorChar);
string[] secondPathParts = pathToUpdate.Trim(Path.DirectorySeparatorChar).Split(Path.DirectorySeparatorChar);
int sameCounter = 0;
for (int i = 0; i < Math.Min(firstPathParts.Length, secondPathParts.Length); i++)
{
if (string.Compare(firstPathParts[i], secondPathParts[i], ignoreCase: true) != 0)
{
break;
}
sameCounter++;
}
if (sameCounter == 0)
{
return pathToUpdate;
}
string relativePath = String.Empty;
for (int i = sameCounter; i < firstPathParts.Length; i++)
{
if (i > sameCounter)
{
relativePath += Path.DirectorySeparatorChar;
}
relativePath += "..";
}
if (relativePath.Length == 0)
{
relativePath = ".";
}
for (int i = sameCounter; i < secondPathParts.Length; i++)
{
relativePath += Path.DirectorySeparatorChar;
relativePath += secondPathParts[i];
}
return relativePath;
}
}
}