27{
28
29
30
31
32
33
34
35
36
37 wxFFile writeProjectsFile(path.GetFullPath(), "wb");
38 if (writeProjectsFile.IsOpened())
39 {
40 writeProjectsFile.Write("<Projects>\n</Projects>");
41 writeProjectsFile.Close();
42 }
43 else
44 {
45 wxMessageBox(_("Could not create or clear the file:\n") + path.GetFullPath(), _("Error"), wxICON_ERROR | wxOK);
46 return;
47 }
48
49
50 wxFFile readFile(path.GetFullPath(), "rb");
51 if (!readFile.IsOpened())
52 {
53 wxMessageBox(_("Could not open the file:\n") + path.GetFullPath(), _("Error"), wxICON_ERROR | wxOK);
54 return;
55 }
56
57
58 wxString fileContent;
59 if (!readFile.ReadAll(&fileContent))
60 {
61 wxMessageBox(_("Error reading the XML file:\n") + path.GetFullPath(), _("Error"), wxICON_ERROR | wxOK);
62 return;
63 }
64
65
66 std::string utf8Str = fileContent.ToStdString(wxConvUTF8);
67 std::vector<char> xmlData(utf8Str.begin(), utf8Str.end());
68 xmlData.push_back('\0');
69
70
71 rapidxml::xml_document<> doc;
72 try
73 {
74 doc.parse<0>(xmlData.data());
75 }
76 catch (const std::exception& e)
77 {
78 wxMessageBox(_("XML Parsing Error: ") + wxString::FromUTF8(e.what()), _("Error"), wxICON_ERROR | wxOK);
79 return;
80 }
81
82 rapidxml::xml_node<>* decl = doc.allocate_node(rapidxml::node_declaration);
83 rapidxml::xml_attribute<>* ver = doc.allocate_attribute("version", "1.0");
84 rapidxml::xml_attribute<>* encoding = doc.allocate_attribute("encoding", "utf-8");
85 decl->append_attribute(ver);
86 decl->append_attribute(encoding);
87 doc.append_node(decl);
88
89 rapidxml::xml_node<>* rootNode = doc.allocate_node(rapidxml::node_element, "Project");
90 doc.append_node(rootNode);
91
92 rapidxml::xml_node<>* projectNameNode = XMLParser::AppendNode(doc, rootNode, "Name");
93 XMLParser::SetNodeValue(doc, projectNameNode, path.GetName());
94
95
96 PropertiesData* properties = m_workspace->GetProperties();
97 auto propertiesNode = XMLParser::AppendNode(doc, rootNode, "Properties");
98
99 SimulationData simulationData = properties->GetSimulationPropertiesData();
100 auto simulationPropNode = XMLParser::XMLParser::AppendNode(doc, propertiesNode, "SimulationProperties");
101
102 auto generalPropNode = XMLParser::AppendNode(doc, simulationPropNode, "General");
103 auto basePower = XMLParser::AppendNode(doc, generalPropNode, "BasePower");
104 XMLParser::SetNodeValue(doc, basePower, simulationData.basePower);
105 XMLParser::SetNodeAttribute(doc, basePower, "UnitID", static_cast<int>(simulationData.basePowerUnit));
106 auto contCalc = XMLParser::AppendNode(doc, generalPropNode, "ContinuousCalculation");
107 auto contCalcFault = XMLParser::AppendNode(doc, contCalc, "Fault");
108 XMLParser::SetNodeValue(doc, contCalcFault, simulationData.faultAfterPowerFlow);
109 auto contCalcSCPower = XMLParser::AppendNode(doc, contCalc, "SCPower");
110 XMLParser::SetNodeValue(doc, contCalcSCPower, simulationData.scPowerAfterPowerFlow);
111 auto contCalcTHD = XMLParser::AppendNode(doc, contCalc, "HarmonicDistortion");
112 XMLParser::SetNodeValue(doc, contCalcTHD, simulationData.harmDistortionAfterPowerFlow);
113
114 auto powerFlowPropNode = XMLParser::AppendNode(doc, simulationPropNode, "PowerFlow");
115 auto solutionMethod = XMLParser::AppendNode(doc, powerFlowPropNode, "SolutionMethod");
116 XMLParser::SetNodeValue(doc, solutionMethod, simulationData.powerFlowMethod);
117 auto accFactor = XMLParser::AppendNode(doc, powerFlowPropNode, "AccFactor");
118 XMLParser::SetNodeValue(doc, accFactor, simulationData.accFator);
119 auto pfTolerance = XMLParser::AppendNode(doc, powerFlowPropNode, "Tolerance");
120 XMLParser::SetNodeValue(doc, pfTolerance, simulationData.powerFlowTolerance);
121 auto pfMaxIter = XMLParser::AppendNode(doc, powerFlowPropNode, "MaxIterations");
122 XMLParser::SetNodeValue(doc, pfMaxIter, simulationData.powerFlowMaxIterations);
123 auto pfSlackAngle = XMLParser::AppendNode(doc, powerFlowPropNode, "SlackAngle");
124 XMLParser::SetNodeValue(doc, pfSlackAngle, simulationData.initAngle);
125
126 auto stabilityPropNode = XMLParser::AppendNode(doc, simulationPropNode, "Stability");
127 auto timeStep = XMLParser::AppendNode(doc, stabilityPropNode, "TimeStep");
128 XMLParser::SetNodeValue(doc, timeStep, simulationData.timeStep);
129 auto simTime = XMLParser::AppendNode(doc, stabilityPropNode, "SimulationTime");
130 XMLParser::SetNodeValue(doc, simTime, simulationData.stabilitySimulationTime);
131 auto freq = XMLParser::AppendNode(doc, stabilityPropNode, "Frequency");
132 XMLParser::SetNodeValue(doc, freq, simulationData.stabilityFrequency);
133 auto stabTolerance = XMLParser::AppendNode(doc, stabilityPropNode, "Tolerance");
134 XMLParser::SetNodeValue(doc, stabTolerance, simulationData.stabilityTolerance);
135 auto stabTMaxIter = XMLParser::AppendNode(doc, stabilityPropNode, "MaxIterations");
136 XMLParser::SetNodeValue(doc, stabTMaxIter, simulationData.stabilityMaxIterations);
137 auto controlRatio = XMLParser::AppendNode(doc, stabilityPropNode, "ControlStepRatio");
138 XMLParser::SetNodeValue(doc, controlRatio, simulationData.controlTimeStepRatio);
139 auto plotStep = XMLParser::AppendNode(doc, stabilityPropNode, "PlotStep");
140 XMLParser::SetNodeValue(doc, plotStep, simulationData.plotTime);
141 auto useCOI = XMLParser::AppendNode(doc, stabilityPropNode, "UseCOI");
142 XMLParser::SetNodeValue(doc, useCOI, simulationData.useCOI);
143
144 auto zipPropNode = XMLParser::AppendNode(doc, simulationPropNode, "ZIPLoad");
145 auto useCompLoads = XMLParser::AppendNode(doc, zipPropNode, "UseCompositeLoad");
146 XMLParser::SetNodeValue(doc, useCompLoads, simulationData.useCompLoads);
147 auto activePowerComp = XMLParser::AppendNode(doc, zipPropNode, "ActivePowerComposition");
148 auto pz = XMLParser::AppendNode(doc, activePowerComp, "ConstantImpedance");
149 XMLParser::SetNodeValue(doc, pz, simulationData.constImpedanceActive);
150 auto pi = XMLParser::AppendNode(doc, activePowerComp, "ConstantCurrent");
151 XMLParser::SetNodeValue(doc, pi, simulationData.constCurrentActive);
152 auto pp = XMLParser::AppendNode(doc, activePowerComp, "ConstantPower");
153 XMLParser::SetNodeValue(doc, pp, simulationData.constPowerActive);
154 auto reactivePowerComp = XMLParser::AppendNode(doc, zipPropNode, "ReactivePowerComposition");
155 auto qz = XMLParser::AppendNode(doc, reactivePowerComp, "ConstantImpedance");
156 XMLParser::SetNodeValue(doc, qz, simulationData.constImpedanceReactive);
157 auto qi = XMLParser::AppendNode(doc, reactivePowerComp, "ConstantCurrent");
158 XMLParser::SetNodeValue(doc, qi, simulationData.constCurrentReactive);
159 auto qp = XMLParser::AppendNode(doc, reactivePowerComp, "ConstantPower");
160 XMLParser::SetNodeValue(doc, qp, simulationData.constPowerReactive);
161 auto undervoltageLim = XMLParser::AppendNode(doc, zipPropNode, "UndervoltageLimit");
162 auto uvi = XMLParser::AppendNode(doc, undervoltageLim, "ConstantCurrent");
163 XMLParser::SetNodeValue(doc, uvi, simulationData.underVoltageConstCurrent);
164 auto uvp = XMLParser::AppendNode(doc, undervoltageLim, "ConstantPower");
165 XMLParser::SetNodeValue(doc, uvp, simulationData.underVoltageConstPower);
166
167
168
169 auto elementsNode = XMLParser::AppendNode(doc, rootNode, "Elements");
170
171
172 ElectricCalculation allElements;
174 int elementID = 0;
175
176
177 auto busesNode = XMLParser::AppendNode(doc, elementsNode, "BusList");
179 for (auto* bus : busList) {
180 if (bus->IsInserted()) {
181 bus->SetID(elementID);
182 bus->SaveElement(doc, busesNode);
183 elementID++;
184 }
185 }
186
187
188
189 auto capacitorsNode = XMLParser::AppendNode(doc, elementsNode, "CapacitorList");
191 elementID = 0;
192 for (auto* capacitor : capacitorList) {
193 if (capacitor->IsInserted()) {
194 capacitor->SetID(elementID);
195 capacitor->SaveElement(doc, capacitorsNode);
196 elementID++;
197 }
198 }
199
200
201
202 auto indMotorsNode = XMLParser::AppendNode(doc, elementsNode, "IndMotorList");
204 elementID = 0;
205 for (auto* indMotor : indMotorList) {
206 if (indMotor->IsInserted()) {
207 indMotor->SetID(elementID);
208 indMotor->SaveElement(doc, indMotorsNode);
209 elementID++;
210 }
211 }
212
213
214
215 auto inductorsNode = XMLParser::AppendNode(doc, elementsNode, "InductorList");
217 elementID = 0;
218 for (auto* inductor : inductorList) {
219 if (inductor->IsInserted()) {
220 inductor->SetID(elementID);
221 inductor->SaveElement(doc, inductorsNode);
222 elementID++;
223 }
224 }
225
226
227
228 auto linesNode = XMLParser::AppendNode(doc, elementsNode, "LineList");
230 elementID = 0;
231 for (auto* line : lineList) {
232 if (line->IsInserted()) {
233 line->SetID(elementID);
234 line->SaveElement(doc, linesNode);
235 elementID++;
236 }
237 }
238
239
240
241 auto loadsNode = XMLParser::AppendNode(doc, elementsNode, "LoadList");
243 elementID = 0;
244 for (auto* load : loadList) {
245 if (load->IsInserted()) {
246 load->SetID(elementID);
247 load->SaveElement(doc, loadsNode);
248 elementID++;
249 }
250 }
251
252
253
254 auto syncGeneratorsNode = XMLParser::AppendNode(doc, elementsNode, "SyncGeneratorList");
256 elementID = 0;
257 for (auto syncGen : syncGeneratorList) {
258 if (syncGen->IsInserted()) {
259 syncGen->SetID(elementID);
260 auto elementNode = syncGen->SaveElement(doc, syncGeneratorsNode);
261
262
263 auto data = syncGen->GetElectricalData();
264 auto electricalProp = elementNode->first_node("ElectricalProperties");
265 auto stability = electricalProp->first_node("Stability");
266
267 auto avr = XMLParser::AppendNode(doc, stability, "AVR");
268 if (data.avr) SaveControlElements(doc, avr, data.avr);
269
270 auto speedGov = XMLParser::AppendNode(doc, stability, "SpeedGovernor");
271 if (data.speedGov) SaveControlElements(doc, speedGov, data.speedGov);
272
273 elementID++;
274 }
275 }
276
277
278
279 auto syncMotorsNode = XMLParser::AppendNode(doc, elementsNode, "SyncMotorList");
281 elementID = 0;
282 for (auto* syncMotor : syncMotorList) {
283 if (syncMotor->IsInserted()) {
284 syncMotor->SetID(elementID);
285 syncMotor->SaveElement(doc, syncMotorsNode);
286 elementID++;
287 }
288 }
289
290
291
292 auto transformersNode = XMLParser::AppendNode(doc, elementsNode, "TransformerList");
294 elementID = 0;
295 for (auto* transformer : transformerList) {
296 if (transformer->IsInserted()) {
297 transformer->SetID(elementID);
298 transformer->SaveElement(doc, transformersNode);
299 elementID++;
300 }
301 }
302
303
304
305 auto harmCurrentNode = XMLParser::AppendNode(doc, elementsNode, "HarmCurrentList");
307 elementID = 0;
308 for (auto* harmCurrent : harmCurrentList) {
309 if (harmCurrent->IsInserted()) {
310 harmCurrent->SetID(elementID);
311 harmCurrent->SaveElement(doc, harmCurrentNode);
312 elementID++;
313 }
314 }
315
316
317
318 auto emtElementNode = XMLParser::AppendNode(doc, elementsNode, "EMTElementList");
320 elementID = 0;
321 for (auto* emtElement : emtElementList) {
322 if (emtElement->IsInserted()) {
323 emtElement->SetID(elementID);
324 emtElement->SaveElement(doc, emtElementNode);
325 elementID++;
326 }
327 }
328
329
330
331 auto textsNode = XMLParser::AppendNode(doc, elementsNode, "TextList");
332 auto textList = m_workspace->GetTextList();
333 elementID = 0;
334 for (auto& text : textList) {
335 if (text->IsInserted()) {
336 text->SetID(elementID);
337 text->SaveElement(doc, textsNode);
338 elementID++;
339 }
340 }
341
342
343
344 std::string xmlOutput;
345 rapidxml::print(std::back_inserter(xmlOutput), doc, 0);
346
347
348 wxFFile saveFile(path.GetFullPath(), "wb");
349 if (!saveFile.IsOpened())
350 {
351 wxMessageBox(_("Could not save the file:\n") + path.GetFullPath(), _("Error"), wxICON_ERROR | wxOK);
352 return;
353 }
354
355 saveFile.Write(xmlOutput);
356 saveFile.Close();
357}
const std::vector< IndMotor * > GetIndMotorList() const
Get the induction motors of the system (use GetElementsFromList first).
const std::vector< Load * > GetLoadList() const
Get the loads of the system (use GetElementsFromList first).
const std::vector< SyncMotor * > GetSyncMotorList() const
Get the synchronous motors of the system (use GetElementsFromList first).
const std::vector< SyncGenerator * > GetSyncGeneratorList() const
Get the synchronous generators of the system (use GetElementsFromList first).
const std::vector< Transformer * > GetTransformerList() const
Get the transformers of the system (use GetElementsFromList first).
const std::vector< Capacitor * > GetCapacitorList() const
Get the capacitors of the system (use GetElementsFromList first).
const std::vector< Inductor * > GetInductorList() const
Get the inductors of the system (use GetElementsFromList first).
const std::vector< Line * > GetLineList() const
Get the lines of the system (use GetElementsFromList first).
const std::vector< EMTElement * > GetEMTElementList() const
Get the electromagnetic element list of the system (use GetElementsFromList first).
const std::vector< Bus * > GetBusList() const
Get the buses of the system (use GetElementsFromList first).
virtual void GetElementsFromList(std::vector< Element * > elementList)
Separate the power elements from a generic list.
const std::vector< HarmCurrent * > GetHarmCurrentList() const
Get the harmonic current source of the system (use GetElementsFromList first).