-
Notifications
You must be signed in to change notification settings - Fork 0
/
StepWriter.cs
257 lines (229 loc) · 8.87 KB
/
StepWriter.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
using StepParser.Items;
using StepParser.Syntax;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;
namespace StepParser
{
internal class StepWriter
{
private StepFile _file;
private bool _inlineReferences;
private Dictionary<StepRepresentationItem, int> _itemMap;
private XmlWriter _xmlWriter;
public StepWriter(StepFile stepFile, bool inlineReferences, XmlWriter xmlWriter)
{
_file = stepFile;
_itemMap = new Dictionary<StepRepresentationItem, int>();
_inlineReferences = inlineReferences;
_xmlWriter = xmlWriter;
SetRelationShip();
}
public void Save()
{
// output header
WriteHeader();
// data section
// Write Parts Section
_xmlWriter.WriteStartElement("Parts");
foreach (var item in _file.Items)
{
if (item.ItemType == StepItemType.ShapeDefinitionRepresentation)
{
item.WriteXML(_xmlWriter);
}
}
_xmlWriter.WriteEndElement();
// Write Group Section
_xmlWriter.WriteStartElement("Structure");
var rootGroups = GetRootGroups();
foreach (var item in rootGroups)
{
WriteGroup(item);
}
_xmlWriter.WriteEndElement();
// Write Orders
_xmlWriter.WriteStartElement("OrderList");
foreach (var item in _file.Items)
{
if (item.ItemType == StepItemType.ProductDefinition)
{
((StepProductDefinition)item).WriteXMLOroderPart(_xmlWriter);
}
}
_xmlWriter.WriteEndElement();
}
private void WriteHeader()
{
_xmlWriter.WriteStartElement("BasicData");
WriteXmlElement("FileName", _file.Name);
WriteXmlElement("ISO", StepFile.MagicHeader);
WriteXmlElement("Standard", _file.Description);
WriteXmlElement("DateTime", _file.Timestamp.ToString());
_xmlWriter.WriteEndElement();
}
private void WriteXmlElement(string name, string value)
{
_xmlWriter.WriteStartElement(name);
_xmlWriter.WriteString(value);
_xmlWriter.WriteEndElement();
}
private void WriteGroup(StepRepresentationItem item)
{
if (item.ItemType == StepItemType.ProductDefinition)
{
((StepProductDefinition)item).WriteXMLGroup(_xmlWriter);
}
}
private List<StepProductDefinition> GetRootGroups()
{
var rootGroups = new List<StepProductDefinition>();
foreach (var item in _file.Items)
{
bool isRoot = true;
if (item.ItemType == StepItemType.ProductDefinition)
{
foreach(var subItem in _file.Items)
{
if (subItem.ItemType == StepItemType.ProductDefinition)
{
if (((StepProductDefinition)subItem).HasProduct((StepProductDefinition)item))
{
isRoot = false;
}
}
}
if (isRoot == true)
{
rootGroups.Add((StepProductDefinition)item);
}
}
}
return rootGroups;
}
public void SetRelationShip()
{
for(int idx = 0; idx < _file.Items.Count; idx++)
{
if (_file.Items[idx].ItemType == StepItemType.ShapeDefinitionRepresentation)
{
var relationShip = GetShapeRelationShip((StepShapeDefinitionRepresentation)_file.Items[idx]);
if (relationShip != null)
{
((StepShapeDefinitionRepresentation)_file.Items[idx]).SetShapePresentationRelationShip(relationShip);
}
}
else if (_file.Items[idx].ItemType == StepItemType.AdvancedBrepShapeRepresentation)
{
List<StepStyledItem> styledItems = GetStyledItems((StepAdvancedBrepShapeRepresentation)_file.Items[idx]);
((StepAdvancedBrepShapeRepresentation)_file.Items[idx]).SetStyledItems(styledItems);
}
else if (_file.Items[idx].ItemType == StepItemType.ProductDefinition)
{
List<StepProductDefinition> nextComponents = GetNextComponent((StepProductDefinition)_file.Items[idx]);
((StepProductDefinition)_file.Items[idx]).Children = nextComponents;
}
}
}
public StepShapeRepresentationRelationShip GetShapeRelationShip(StepShapeDefinitionRepresentation item)
{
foreach (var eachItem in _file.Items)
{
if (eachItem.ItemType == StepItemType.ShapeRepresentationRelationship)
{
var itemInstance = (StepShapeRepresentationRelationShip)eachItem;
if (itemInstance.UsedRepresentation != null && itemInstance.UsedRepresentation.Id == item.UsedRepresentation.Id)
{
return itemInstance;
}
}
}
return null;
}
public List<StepStyledItem> GetStyledItems(StepAdvancedBrepShapeRepresentation item)
{
var styledItems = new List<StepStyledItem>();
if (item.UsedSolidBrepList != null && item.UsedSolidBrepList.Count > 0)
{
foreach(var solidBrep in item.UsedSolidBrepList)
{
foreach (var eachItem in _file.Items)
{
if (eachItem.ItemType == StepItemType.StyledItem)
{
var itemInstance = (StepStyledItem)eachItem;
if (itemInstance.UsedSolidBrep != null && itemInstance.UsedSolidBrep.Id == solidBrep.Id)
{
styledItems.Add(itemInstance);
}
}
}
}
}
return styledItems;
}
public List<StepProductDefinition> GetNextComponent(StepProductDefinition item)
{
var nextComponents = new List<StepProductDefinition>();
foreach (var eachItem in _file.Items)
{
if (eachItem.ItemType == StepItemType.NextAssemblyUsageOccurrence)
{
var itemInstance = (StepNextAssemblyUsageOccrrence)eachItem;
if (itemInstance.Parent != null && itemInstance.Parent.Id == item.Id)
{
nextComponents.Add(itemInstance.Child);
}
}
}
return nextComponents;
}
public StepSyntax GetItemSyntax(StepRepresentationItem item)
{
if (_inlineReferences)
{
var parameters = new StepSyntaxList(-1, -1, item.GetParameters(this));
return new StepSimpleItemSyntax(item.ItemType.GetItemTypeString(), parameters, item.Id);
}
else
{
return new StepEntityInstanceReferenceSyntax(_itemMap[item]);
}
}
public StepSyntax GetItemSyntaxOrAuto(StepRepresentationItem item)
{
return item == null
? new StepAutoSyntax()
: GetItemSyntax(item);
}
public static StepEnumerationValueSyntax GetBooleanSyntax(bool value)
{
var text = value ? "T" : "F";
return new StepEnumerationValueSyntax(text);
}
internal static IEnumerable<string> SplitStringIntoParts(string str, int maxLength = 256)
{
var parts = new List<string>();
if (str != null)
{
int offset = 0;
while (offset < str.Length)
{
var length = Math.Min(maxLength, str.Length - offset);
parts.Add(str.Substring(offset, length));
offset += length;
}
}
else
{
parts.Add(string.Empty);
}
return parts;
}
private string toCamel(string input)
{
return new CultureInfo("en").TextInfo.ToTitleCase(input.ToLower().Replace("_", " ")).Replace(" ", "");
}
}
}