-
Notifications
You must be signed in to change notification settings - Fork 64
/
FormatCSC.cs
409 lines (346 loc) · 18.4 KB
/
FormatCSC.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Name: FormatCSC.cs
// Description: C# Code Generator
// Author: Tim Chipman
// Origination: Work performed for BuildingSmart by Constructivity.com LLC.
// Copyright: (c) 2012 BuildingSmart International Ltd.
// License: http://www.buildingsmart-tech.org/legal
using System;
using System.Collections.Generic;
using System.Text;
using IfcDoc.Schema.DOC;
namespace IfcDoc.Format.CSC
{
internal class FormatCSC : IDisposable
{
string m_filename;
DocProject m_project;
DocDefinition m_definition;
/// <summary>
/// Generates folder of definitions
/// </summary>
/// <param name="path"></param>
public static void GenerateCode(DocProject project, string path)
{
foreach (DocSection docSection in project.Sections)
{
foreach(DocSchema docSchema in docSection.Schemas)
{
foreach(DocType docType in docSchema.Types)
{
using (FormatCSC format = new FormatCSC(path + @"\" + docSchema.Name + @"\" + docType.Name + ".cs"))
{
format.Instance = project;
format.Definition = docType;
format.Save();
}
}
foreach (DocEntity docType in docSchema.Entities)
{
using (FormatCSC format = new FormatCSC(path + @"\" + docSchema.Name + @"\" + docType.Name + ".cs"))
{
format.Instance = project;
format.Definition = docType;
format.Save();
}
}
}
}
}
public FormatCSC(string filename)
{
this.m_filename = filename;
}
public DocProject Instance
{
get
{
return this.m_project;
}
set
{
this.m_project = value;
}
}
/// <summary>
/// Optional definition to save, or null for all definitions in project.
/// </summary>
public DocDefinition Definition
{
get
{
return this.m_definition;
}
set
{
this.m_definition = value;
}
}
public void Save()
{
string dirpath = System.IO.Path.GetDirectoryName(this.m_filename);
if (!System.IO.Directory.Exists(this.m_filename))
{
System.IO.Directory.CreateDirectory(dirpath);
}
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(this.m_filename))
{
writer.WriteLine("// This file was automatically generated from IFCDOC at www.buildingsmart-tech.org.");
writer.WriteLine("// IFC content is copyright (C) 1996-2013 BuildingSMART International Ltd.");
writer.WriteLine();
writer.WriteLine("using System;");
writer.WriteLine();
if (this.m_definition != null)
{
writer.WriteLine("namespace BuildingSmart.IFC");
writer.WriteLine("{");
if (this.m_definition is DocDefined)
{
DocDefined docDefined = (DocDefined)this.m_definition;
writer.WriteLine("\tpublic struct " + this.m_definition.Name);
writer.WriteLine("\t{");
writer.WriteLine("\t\t" + docDefined.DefinedType + " Value;");
writer.WriteLine("\t}");
}
else if (this.m_definition is DocSelect)
{
writer.WriteLine("\tpublic interface " + this.m_definition.Name);
writer.WriteLine("\t{");
writer.WriteLine("\t}");
}
else if (this.m_definition is DocEnumeration)
{
DocEnumeration docEnumumeration = (DocEnumeration)this.m_definition;
writer.WriteLine("\tpublic enum " + this.m_definition.Name);
writer.WriteLine("\t{");
int counter = 0;
foreach (DocConstant docConstant in docEnumumeration.Constants)
{
counter++;
int val = counter;
if (docConstant.Name.Equals("NOTDEFINED"))
{
val = 0;
}
else if (docConstant.Name.Equals("USERDEFINED"))
{
val = -1;
}
writer.WriteLine("\t\t" + docConstant.Name + " = " + val + ",");
}
writer.WriteLine("\t}");
}
else if (this.m_definition is DocEntity)
{
DocEntity docEntity = (DocEntity)this.m_definition;
string basedef = docEntity.BaseDefinition;
if (String.IsNullOrEmpty(basedef))
{
basedef = "IfcBase";
}
writer.WriteLine("\tpublic partial class " + this.m_definition.Name + " : " + basedef);
writer.WriteLine("\t{");
// fields
foreach (DocAttribute docAttribute in docEntity.Attributes)
{
switch (docAttribute.GetAggregation())
{
case DocAggregationEnum.SET:
writer.WriteLine("\t\tprivate ICollection<" + docAttribute.DefinedType + "> _" + docAttribute.Name + ";");
break;
case DocAggregationEnum.LIST:
writer.WriteLine("\t\tprivate IList<" + docAttribute.DefinedType + "> _" + docAttribute.Name + ";");
break;
default:
writer.WriteLine("\t\tprivate " + docAttribute.DefinedType + " _" + docAttribute.Name + ";");
break;
}
}
// constructor
writer.WriteLine();
writer.WriteLine("\t\tpublic " + docEntity.Name + "()");
writer.WriteLine("\t\t{");
//... values...
writer.WriteLine("\t\t}");
// properties
foreach (DocAttribute docAttribute in docEntity.Attributes)
{
writer.WriteLine();
switch(docAttribute.GetAggregation())
{
case DocAggregationEnum.SET:
writer.WriteLine("\t\tpublic ICollection<" + docAttribute.DefinedType + "> " + docAttribute.Name);
break;
case DocAggregationEnum.LIST:
writer.WriteLine("\t\tpublic IList<" + docAttribute.DefinedType + "> " + docAttribute.Name);
break;
default:
writer.WriteLine("\t\tpublic " + docAttribute.DefinedType + " " + docAttribute.Name);
break;
}
writer.WriteLine("\t\t{");
writer.WriteLine("\t\t\tget");
writer.WriteLine("\t\t\t{");
writer.WriteLine("\t\t\t\treturn this._" + docAttribute.Name + ";");
writer.WriteLine("\t\t\t}");
if (docAttribute.GetAggregation() == DocAggregationEnum.NONE)
{
writer.WriteLine("\t\t\tset");
writer.WriteLine("\t\t\t{");
writer.WriteLine("\t\t\t\tthis.OnBeforePropertyChange(\"" + docAttribute.Name + "\");");
writer.WriteLine("\t\t\t\tthis._" + docAttribute.Name + " = value;");
writer.WriteLine("\t\t\t\tthis.OnAfterPropertyChange(\"" + docAttribute.Name + "\");");
writer.WriteLine("\t\t\t}");
}
writer.WriteLine("\t\t}");
}
// serialization
writer.WriteLine();
writer.WriteLine("\t\tprivate void GetObjectData(SerializationInfo info, StreamingContext context)");
writer.WriteLine("\t\t{");
foreach (DocAttribute docAttribute in docEntity.Attributes)
{
if (docAttribute.Inverse == null && docAttribute.Derived == null)
{
writer.WriteLine("\t\t\tinfo.AddValue(\"" + docAttribute.Name + "\", this._" + docAttribute.Name + ");");
}
}
writer.WriteLine("\t\t}");
writer.WriteLine();
writer.WriteLine("\t\tprivate void SetObjectData(SerializationInfo info, StreamingContext context)");
writer.WriteLine("\t\t{");
foreach (DocAttribute docAttribute in docEntity.Attributes)
{
if (docAttribute.Inverse == null && docAttribute.Derived == null)
{
string method = "GetValue";
writer.WriteLine("\t\t\tthis._" + docAttribute.Name + " = info." + method + "(\"" + docAttribute.Name + "\");");
}
}
writer.WriteLine("\t\t}");
writer.WriteLine("\t}");
}
writer.WriteLine("}");
}
else
{
writer.WriteLine("namespace BuildingSmart.IFC.Properties");
writer.WriteLine("{");
writer.WriteLine();
Dictionary<string, string[]> mapEnums = new Dictionary<string, string[]>();
foreach (DocSection docSection in this.m_project.Sections)
{
foreach (DocSchema docSchema in docSection.Schemas)
{
foreach (DocPropertySet docPset in docSchema.PropertySets)
{
writer.WriteLine(" /// <summary>");
writer.WriteLine(" /// " + docPset.Documentation.Replace('\r', ' ').Replace('\n', ' '));
writer.WriteLine(" /// </summary>");
writer.WriteLine(" public class " + docPset.Name + " : Pset");
writer.WriteLine(" {");
foreach (DocProperty docProperty in docPset.Properties)
{
writer.WriteLine(" /// <summary>");
writer.WriteLine(" /// " + docProperty.Documentation.Replace('\r', ' ').Replace('\n', ' '));
writer.WriteLine(" /// </summary>");
switch (docProperty.PropertyType)
{
case DocPropertyTemplateTypeEnum.P_SINGLEVALUE:
writer.WriteLine(" public " + docProperty.PrimaryDataType + " " + docProperty.Name + " { get { return this.GetValue<" + docProperty.PrimaryDataType + ">(\"" + docProperty.Name + "\"); } set { this.SetValue<" + docProperty.PrimaryDataType + ">(\"" + docProperty.Name + "\", value); } }");
break;
case DocPropertyTemplateTypeEnum.P_ENUMERATEDVALUE:
// record enum for later
{
string[] parts = docProperty.SecondaryDataType.Split(':');
if (parts.Length == 2)
{
string typename = parts[0];
if (!mapEnums.ContainsKey(typename))
{
string[] enums = parts[1].Split(',');
mapEnums.Add(typename, enums);
writer.WriteLine(" public " + typename + " " + docProperty.Name + " { get { return this.GetValue<" + typename + ">(\"" + docProperty.Name + "\"); } set { this.SetValue<" + typename + ">(\"" + docProperty.Name + "\", value); } }");
}
}
}
break;
case DocPropertyTemplateTypeEnum.P_BOUNDEDVALUE:
writer.WriteLine(" public PBound<" + docProperty.PrimaryDataType + "> " + docProperty.Name + " { get { return this.GetBound<" + docProperty.PrimaryDataType + ">(\"" + docProperty.Name + "\"); } }");
break;
case DocPropertyTemplateTypeEnum.P_LISTVALUE:
break;
case DocPropertyTemplateTypeEnum.P_TABLEVALUE:
writer.WriteLine(" public PTable<" + docProperty.PrimaryDataType + ", " + docProperty.SecondaryDataType + "> " + docProperty.Name + " { get { return this.GetTable<" + docProperty.PrimaryDataType + ", " + docProperty.SecondaryDataType + ">(\"" + docProperty.Name + "\"); } }");
break;
case DocPropertyTemplateTypeEnum.P_REFERENCEVALUE:
if (docProperty.PrimaryDataType.Equals("IfcTimeSeries"))
{
string datatype = docProperty.SecondaryDataType;
if (String.IsNullOrEmpty(datatype))
{
datatype = "IfcReal";
}
writer.WriteLine(" public PTimeSeries<" + datatype + "> " + docProperty.Name + " { get { return this.GetTimeSeries<" + datatype + ">(\"" + docProperty.Name + "\"); } }");
}
// ... TBD
break;
case DocPropertyTemplateTypeEnum.COMPLEX:
//... TBD
break;
}
}
writer.WriteLine(" }");
writer.WriteLine();
}
}
}
// enums
foreach (string strEnum in mapEnums.Keys)
{
string[] enums = mapEnums[strEnum];
writer.WriteLine(" /// <summary>");
writer.WriteLine(" /// </summary>");
writer.WriteLine(" public enum " + strEnum);
writer.WriteLine(" {");
int counter = 0;
foreach (string val in enums)
{
int num = 0;
string id = val.ToUpper().Trim('.').Replace('-', '_');
switch (id)
{
case "OTHER":
num = -1;
break;
case "NOTKNOWN":
num = -2;
break;
case "UNSET":
num = 0;
break;
default:
counter++;
num = counter;
break;
}
if (id[0] >= '0' && id[0] <= '9')
{
id = "_" + id; // avoid numbers
}
writer.WriteLine(" /// <summary></summary>");
writer.WriteLine(" " + id + " = " + num + ",");
}
writer.WriteLine(" }");
writer.WriteLine();
}
writer.WriteLine("}");
}
}
}
#region IDisposable Members
public void Dispose()
{
}
#endregion
}
}