22FileHanding::~FileHanding() {}
23FileHanding::FileHanding(
Workspace* workspace) { m_workspace = workspace; }
24FileHanding::FileHanding(
ControlEditor* controlEditor) { m_controlEditor = controlEditor; }
25FileHanding::FileHanding() {}
26void FileHanding::SaveProject(wxFileName path)
37 wxFFile writeProjectsFile(path.GetFullPath(),
"wb");
38 if (writeProjectsFile.IsOpened())
40 writeProjectsFile.Write(
"<Projects>\n</Projects>");
41 writeProjectsFile.Close();
45 wxMessageBox(_(
"Could not create or clear the file:\n") + path.GetFullPath(), _(
"Error"), wxICON_ERROR | wxOK);
50 wxFFile readFile(path.GetFullPath(),
"rb");
51 if (!readFile.IsOpened())
53 wxMessageBox(_(
"Could not open the file:\n") + path.GetFullPath(), _(
"Error"), wxICON_ERROR | wxOK);
59 if (!readFile.ReadAll(&fileContent))
61 wxMessageBox(_(
"Error reading the XML file:\n") + path.GetFullPath(), _(
"Error"), wxICON_ERROR | wxOK);
66 std::string utf8Str = fileContent.ToStdString(wxConvUTF8);
67 std::vector<char> xmlData(utf8Str.begin(), utf8Str.end());
68 xmlData.push_back(
'\0');
71 rapidxml::xml_document<> doc;
74 doc.parse<0>(xmlData.data());
76 catch (
const std::exception& e)
78 wxMessageBox(_(
"XML Parsing Error: ") + wxString::FromUTF8(e.what()), _(
"Error"), wxICON_ERROR | wxOK);
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);
89 rapidxml::xml_node<>* rootNode = doc.allocate_node(rapidxml::node_element,
"Project");
90 doc.append_node(rootNode);
92 rapidxml::xml_node<>* projectNameNode = XMLParser::AppendNode(doc, rootNode,
"Name");
93 XMLParser::SetNodeValue(doc, projectNameNode, path.GetName());
96 PropertiesData* properties = m_workspace->GetProperties();
97 auto propertiesNode = XMLParser::AppendNode(doc, rootNode,
"Properties");
99 SimulationData simulationData = properties->GetSimulationPropertiesData();
100 auto simulationPropNode = XMLParser::XMLParser::AppendNode(doc, propertiesNode,
"SimulationProperties");
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);
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);
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);
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);
169 auto elementsNode = XMLParser::AppendNode(doc, rootNode,
"Elements");
172 ElectricCalculation allElements;
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);
189 auto capacitorsNode = XMLParser::AppendNode(doc, elementsNode,
"CapacitorList");
192 for (
auto* capacitor : capacitorList) {
193 if (capacitor->IsInserted()) {
194 capacitor->SetID(elementID);
195 capacitor->SaveElement(doc, capacitorsNode);
202 auto indMotorsNode = XMLParser::AppendNode(doc, elementsNode,
"IndMotorList");
205 for (
auto* indMotor : indMotorList) {
206 if (indMotor->IsInserted()) {
207 indMotor->SetID(elementID);
208 indMotor->SaveElement(doc, indMotorsNode);
215 auto inductorsNode = XMLParser::AppendNode(doc, elementsNode,
"InductorList");
218 for (
auto* inductor : inductorList) {
219 if (inductor->IsInserted()) {
220 inductor->SetID(elementID);
221 inductor->SaveElement(doc, inductorsNode);
228 auto linesNode = XMLParser::AppendNode(doc, elementsNode,
"LineList");
231 for (
auto* line : lineList) {
232 if (line->IsInserted()) {
233 line->SetID(elementID);
234 line->SaveElement(doc, linesNode);
241 auto loadsNode = XMLParser::AppendNode(doc, elementsNode,
"LoadList");
244 for (
auto* load : loadList) {
245 if (load->IsInserted()) {
246 load->SetID(elementID);
247 load->SaveElement(doc, loadsNode);
254 auto syncGeneratorsNode = XMLParser::AppendNode(doc, elementsNode,
"SyncGeneratorList");
257 for (
auto syncGen : syncGeneratorList) {
258 if (syncGen->IsInserted()) {
259 syncGen->SetID(elementID);
260 auto elementNode = syncGen->SaveElement(doc, syncGeneratorsNode);
263 auto data = syncGen->GetElectricalData();
264 auto electricalProp = elementNode->first_node(
"ElectricalProperties");
265 auto stability = electricalProp->first_node(
"Stability");
267 auto avr = XMLParser::AppendNode(doc, stability,
"AVR");
268 if (data.avr) SaveControlElements(doc, avr, data.avr);
270 auto speedGov = XMLParser::AppendNode(doc, stability,
"SpeedGovernor");
271 if (data.speedGov) SaveControlElements(doc, speedGov, data.speedGov);
279 auto syncMotorsNode = XMLParser::AppendNode(doc, elementsNode,
"SyncMotorList");
282 for (
auto* syncMotor : syncMotorList) {
283 if (syncMotor->IsInserted()) {
284 syncMotor->SetID(elementID);
285 syncMotor->SaveElement(doc, syncMotorsNode);
292 auto transformersNode = XMLParser::AppendNode(doc, elementsNode,
"TransformerList");
295 for (
auto* transformer : transformerList) {
296 if (transformer->IsInserted()) {
297 transformer->SetID(elementID);
298 transformer->SaveElement(doc, transformersNode);
305 auto harmCurrentNode = XMLParser::AppendNode(doc, elementsNode,
"HarmCurrentList");
308 for (
auto* harmCurrent : harmCurrentList) {
309 if (harmCurrent->IsInserted()) {
310 harmCurrent->SetID(elementID);
311 harmCurrent->SaveElement(doc, harmCurrentNode);
318 auto emtElementNode = XMLParser::AppendNode(doc, elementsNode,
"EMTElementList");
321 for (
auto* emtElement : emtElementList) {
322 if (emtElement->IsInserted()) {
323 emtElement->SetID(elementID);
324 emtElement->SaveElement(doc, emtElementNode);
331 auto textsNode = XMLParser::AppendNode(doc, elementsNode,
"TextList");
332 auto textList = m_workspace->GetTextList();
334 for (
auto& text : textList) {
335 if (text->IsInserted()) {
336 text->SetID(elementID);
337 text->SaveElement(doc, textsNode);
344 std::string xmlOutput;
345 rapidxml::print(std::back_inserter(xmlOutput), doc, 0);
348 wxFFile saveFile(path.GetFullPath(),
"wb");
349 if (!saveFile.IsOpened())
351 wxMessageBox(_(
"Could not save the file:\n") + path.GetFullPath(), _(
"Error"), wxICON_ERROR | wxOK);
355 saveFile.Write(xmlOutput);
359bool FileHanding::OpenProject(wxFileName path)
361 rapidxml::xml_document<> doc;
362 wxString fullPath = path.GetFullPath();
365 wxFFile file(fullPath,
"rb");
366 if (!file.IsOpened())
368 wxMessageBox(_(
"Could not open the file:\n") + fullPath, _(
"Error"), wxICON_ERROR | wxOK);
373 wxString fileContent;
374 if (!file.ReadAll(&fileContent, wxConvAuto()))
376 wxMessageBox(_(
"Error reading the XML file:\n") + fullPath, _(
"Error"), wxICON_ERROR | wxOK);
381 std::string utf8Str = fileContent.ToStdString(wxConvUTF8);
384 std::vector<char> xmlData(utf8Str.begin(), utf8Str.end());
385 xmlData.push_back(
'\0');
390 doc.parse<0>(xmlData.data());
392 catch (
const std::exception& e)
394 wxMessageBox(_(
"XML Parsing Error: ") + wxString::FromUTF8(e.what()), _(
"Error"), wxICON_ERROR | wxOK);
404 auto projectNode = doc.first_node(
"Project");
405 if (!projectNode)
return false;
406 auto nameNode = projectNode->first_node(
"Name");
407 if (!nameNode)
return false;
408 m_workspace->SetName(nameNode->value());
410 PropertiesData* propData = m_workspace->GetProperties();
411 SimulationData simData = propData->GetSimulationPropertiesData();
414 auto propertiesNode = projectNode->first_node(
"Properties");
415 if (propertiesNode) {
416 auto simPropertiesNode = propertiesNode->first_node(
"SimulationProperties");
417 if (simPropertiesNode) {
419 auto general = simPropertiesNode->first_node(
"General");
420 simData.basePower = XMLParser::GetNodeValueDouble(general,
"BasePower");
421 simData.basePowerUnit =
422 static_cast<ElectricalUnit
>(XMLParser::GetAttributeValueInt(general,
"BasePower",
"UnitID"));
423 auto contCalc = general->first_node(
"ContinuousCalculation");
424 simData.faultAfterPowerFlow = XMLParser::GetNodeValueInt(contCalc,
"Fault");
425 simData.scPowerAfterPowerFlow = XMLParser::GetNodeValueInt(contCalc,
"SCPower");
426 int harmDistortionAfterPowerFlow = XMLParser::GetNodeValueInt(contCalc,
"HarmonicDistortion");
427 simData.harmDistortionAfterPowerFlow = harmDistortionAfterPowerFlow != -1 ? harmDistortionAfterPowerFlow :
false;
430 auto powerFlow = simPropertiesNode->first_node(
"PowerFlow");
431 simData.powerFlowMethod =
432 static_cast<PowerFlowMethod
>(XMLParser::GetNodeValueInt(powerFlow,
"SolutionMethod"));
433 simData.accFator = XMLParser::GetNodeValueDouble(powerFlow,
"AccFactor");
434 simData.powerFlowTolerance = XMLParser::GetNodeValueDouble(powerFlow,
"Tolerance");
435 simData.powerFlowMaxIterations = XMLParser::GetNodeValueInt(powerFlow,
"MaxIterations");
436 simData.initAngle = XMLParser::GetNodeValueDouble(powerFlow,
"SlackAngle");
439 auto stability = simPropertiesNode->first_node(
"Stability");
440 simData.timeStep = XMLParser::GetNodeValueDouble(stability,
"TimeStep");
441 simData.stabilitySimulationTime = XMLParser::GetNodeValueDouble(stability,
"SimulationTime");
442 simData.stabilityFrequency = XMLParser::GetNodeValueDouble(stability,
"Frequency");
443 simData.stabilityTolerance = XMLParser::GetNodeValueDouble(stability,
"Tolerance");
444 simData.stabilityMaxIterations = XMLParser::GetNodeValueDouble(stability,
"MaxIterations");
445 simData.controlTimeStepRatio = XMLParser::GetNodeValueInt(stability,
"ControlStepRatio");
446 simData.plotTime = XMLParser::GetNodeValueDouble(stability,
"PlotStep");
447 simData.useCOI = XMLParser::GetNodeValueInt(stability,
"UseCOI");
450 auto compLoads = simPropertiesNode->first_node(
"ZIPLoad");
451 simData.useCompLoads = XMLParser::GetNodeValueInt(compLoads,
"UseCompositeLoad");
452 auto activePowerComp = compLoads->first_node(
"ActivePowerComposition");
453 simData.constImpedanceActive = XMLParser::GetNodeValueDouble(activePowerComp,
"ConstantImpedance");
454 simData.constCurrentActive = XMLParser::GetNodeValueDouble(activePowerComp,
"ConstantCurrent");
455 simData.constPowerActive = XMLParser::GetNodeValueDouble(activePowerComp,
"ConstantPower");
456 auto reactivePowerComp = compLoads->first_node(
"ReactivePowerComposition");
457 simData.constImpedanceReactive = XMLParser::GetNodeValueDouble(reactivePowerComp,
"ConstantImpedance");
458 simData.constCurrentReactive = XMLParser::GetNodeValueDouble(reactivePowerComp,
"ConstantCurrent");
459 simData.constPowerReactive = XMLParser::GetNodeValueDouble(reactivePowerComp,
"ConstantPower");
460 auto uvLimit = compLoads->first_node(
"UndervoltageLimit");
461 simData.underVoltageConstCurrent = XMLParser::GetNodeValueDouble(uvLimit,
"ConstantCurrent");
462 simData.underVoltageConstPower = XMLParser::GetNodeValueDouble(uvLimit,
"ConstantPower");
467 propData->SetSimulationPropertiesData(simData);
470 auto elementsNode = projectNode->first_node(
"Elements");
471 if (!elementsNode)
return false;
472 std::vector<Element*> elementList;
474 std::vector<Bus*> busList;
475 std::vector<Capacitor*> capacitorList;
476 std::vector<IndMotor*> indMotorList;
477 std::vector<Inductor*> inductorList;
478 std::vector<Line*> lineList;
479 std::vector<Load*> loadList;
480 std::vector<SyncGenerator*> syncGeneratorList;
481 std::vector<SyncMotor*> syncMotorList;
482 std::vector<Transformer*> transformerList;
483 std::vector<HarmCurrent*> harmCurrentList;
484 std::vector<EMTElement*> emtElementList;
486 std::vector< std::shared_ptr<Text> > textList;
489 std::vector<Element*> parentList;
492 auto busListNode = elementsNode->first_node(
"BusList");
493 if (!busListNode)
return false;
494 auto busNode = busListNode->first_node(
"Bus");
496 Bus* bus =
new Bus();
498 if (!bus->OpenElement(busNode))
return false;
499 elementList.push_back(bus);
500 busList.push_back(bus);
501 parentList.push_back(bus);
503 busNode = busNode->next_sibling(
"Bus");
507 auto capacitorListNode = elementsNode->first_node(
"CapacitorList");
508 if (!capacitorListNode)
return false;
509 auto capacitorNode = capacitorListNode->first_node(
"Capacitor");
510 while (capacitorNode) {
511 Capacitor* capacitor =
new Capacitor();
513 if (!capacitor->OpenElement(capacitorNode, parentList))
return false;
514 elementList.push_back(capacitor);
515 capacitorList.push_back(capacitor);
517 capacitorNode = capacitorNode->next_sibling(
"Capacitor");
521 auto indMotorListNode = elementsNode->first_node(
"IndMotorList");
522 if (!indMotorListNode)
return false;
523 auto indMotorNode = indMotorListNode->first_node(
"IndMotor");
524 while (indMotorNode) {
525 IndMotor* indMotor =
new IndMotor();
527 if (!indMotor->OpenElement(indMotorNode, parentList))
return false;
528 elementList.push_back(indMotor);
529 indMotorList.push_back(indMotor);
531 indMotorNode = indMotorNode->next_sibling(
"IndMotor");
535 auto inductorListNode = elementsNode->first_node(
"InductorList");
536 if (!inductorListNode)
return false;
537 auto inductorNode = inductorListNode->first_node(
"Inductor");
538 while (inductorNode) {
539 Inductor* inductor =
new Inductor();
541 if (!inductor->OpenElement(inductorNode, parentList))
return false;
542 elementList.push_back(inductor);
543 inductorList.push_back(inductor);
545 inductorNode = inductorNode->next_sibling(
"Inductor");
549 auto lineListNode = elementsNode->first_node(
"LineList");
550 if (!lineListNode)
return false;
551 auto lineNode = lineListNode->first_node(
"Line");
553 Line* line =
new Line();
555 if (!line->OpenElement(lineNode, parentList))
return false;
556 elementList.push_back(line);
557 lineList.push_back(line);
559 lineNode = lineNode->next_sibling(
"Line");
563 auto loadListNode = elementsNode->first_node(
"LoadList");
564 if (!loadListNode)
return false;
565 auto loadNode = loadListNode->first_node(
"Load");
567 Load* load =
new Load();
569 if (!load->OpenElement(loadNode, parentList))
return false;
570 elementList.push_back(load);
571 loadList.push_back(load);
573 loadNode = loadNode->next_sibling(
"Load");
577 auto syncGeneratorListNode = elementsNode->first_node(
"SyncGeneratorList");
578 if (!syncGeneratorListNode)
return false;
579 auto syncGeneratorNode = syncGeneratorListNode->first_node(
"SyncGenerator");
581 while (syncGeneratorNode) {
582 SyncGenerator* syncGenerator =
new SyncGenerator();
584 if (!syncGenerator->OpenElement(syncGeneratorNode, parentList))
return false;
587 auto data = syncGenerator->GetElectricalData();
588 auto electricalProp = syncGeneratorNode->first_node(
"ElectricalProperties");
589 auto stability = electricalProp->first_node(
"Stability");
591 auto avr = stability->first_node(
"AVR");
592 if (!avr)
return false;
593 if (!OpenControlElements(doc, avr, data.avr))
return false;
595 auto speedGov = stability->first_node(
"SpeedGovernor");
596 if (!speedGov)
return false;
597 if (!OpenControlElements(doc, speedGov, data.speedGov))
return false;
599 syncGenerator->SetElectricalData(data);
601 elementList.push_back(syncGenerator);
602 syncGeneratorList.push_back(syncGenerator);
603 syncGeneratorNode = syncGeneratorNode->next_sibling(
"SyncGenerator");
607 auto syncMotorListNode = elementsNode->first_node(
"SyncMotorList");
608 if (!syncMotorListNode)
return false;
609 auto syncMotorNode = syncMotorListNode->first_node(
"SyncMotor");
610 while (syncMotorNode) {
611 SyncMotor* syncMotor =
new SyncMotor();
613 if (!syncMotor->OpenElement(syncMotorNode, parentList))
return false;
614 elementList.push_back(syncMotor);
615 syncMotorList.push_back(syncMotor);
617 syncMotorNode = syncMotorNode->next_sibling(
"SyncMotor");
621 auto transformerListNode = elementsNode->first_node(
"TransformerList");
622 if (!transformerListNode)
return false;
623 auto transfomerNode = transformerListNode->first_node(
"Transfomer");
624 while (transfomerNode) {
625 Transformer* transformer =
new Transformer();
627 if (!transformer->OpenElement(transfomerNode, parentList))
return false;
628 elementList.push_back(transformer);
629 transformerList.push_back(transformer);
631 transfomerNode = transfomerNode->next_sibling(
"Transfomer");
635 auto harmCurrentListNode = elementsNode->first_node(
"HarmCurrentList");
636 if (harmCurrentListNode) {
637 auto harmCurrentNode = harmCurrentListNode->first_node(
"HarmCurrent");
638 while (harmCurrentNode) {
639 HarmCurrent* harmCurrent =
new HarmCurrent();
641 if (!harmCurrent->OpenElement(harmCurrentNode, parentList))
return false;
642 elementList.push_back(harmCurrent);
643 harmCurrentList.push_back(harmCurrent);
645 harmCurrentNode = harmCurrentNode->next_sibling(
"HarmCurrent");
649 auto emtElementListNode = elementsNode->first_node(
"EMTElementList");
650 if (emtElementListNode) {
651 auto emtElementNode = emtElementListNode->first_node(
"EMTElement");
652 while (emtElementNode) {
653 EMTElement* emtElement =
new EMTElement();
655 if (!emtElement->OpenElement(emtElementNode, parentList))
return false;
656 elementList.push_back(emtElement);
657 emtElementList.push_back(emtElement);
659 emtElementNode = emtElementNode->next_sibling(
"EMTElement");
663 m_workspace->SetElementList(elementList);
666 auto textListNode = elementsNode->first_node(
"TextList");
667 if (!textListNode)
return false;
668 auto textNode = textListNode->first_node(
"Text");
671 auto text = std::make_shared<Text>();
673 if (!text->OpenElement(textNode))
return true;
675 switch (text->GetElementTypeText()) {
679 Bus* bus = busList[text->GetElementNumber()];
680 text->SetElement(bus);
682 case TYPE_CAPACITOR: {
683 Capacitor* capacitor = capacitorList[text->GetElementNumber()];
684 text->SetElement(capacitor);
686 case TYPE_IND_MOTOR: {
687 IndMotor* indMotor = indMotorList[text->GetElementNumber()];
688 text->SetElement(indMotor);
690 case TYPE_INDUCTOR: {
691 Inductor* inductor = inductorList[text->GetElementNumber()];
692 text->SetElement(inductor);
695 Line* line = lineList[text->GetElementNumber()];
696 text->SetElement(line);
699 Load* load = loadList[text->GetElementNumber()];
700 text->SetElement(load);
702 case TYPE_SYNC_GENERATOR: {
703 SyncGenerator* syncGenerator = syncGeneratorList[text->GetElementNumber()];
704 text->SetElement(syncGenerator);
706 case TYPE_SYNC_MOTOR: {
707 SyncMotor* syncMotor = syncMotorList[text->GetElementNumber()];
708 text->SetElement(syncMotor);
710 case TYPE_TRANSFORMER: {
711 Transformer* transformer = transformerList[text->GetElementNumber()];
712 text->SetElement(transformer);
714 case TYPE_HARMCURRENT: {
715 HarmCurrent* harmCurrent = harmCurrentList[text->GetElementNumber()];
716 text->SetElement(harmCurrent);
720 textList.push_back(text);
721 textNode = textNode->next_sibling(
"Text");
724 m_workspace->SetTextList(textList);
728void FileHanding::SaveControl(wxFileName path)
731 std::ofstream writeProjectsFile(path.GetFullPath().mb_str());
732 writeProjectsFile.close();
734 rapidxml::xml_document<> doc;
735 rapidxml::file<> xmlFile(path.GetFullPath().mb_str());
736 doc.parse<0>(xmlFile.data());
738 rapidxml::xml_node<>* decl = doc.allocate_node(rapidxml::node_declaration);
739 rapidxml::xml_attribute<>* ver = doc.allocate_attribute(
"version",
"1.0");
740 rapidxml::xml_attribute<>* encoding = doc.allocate_attribute(
"encoding",
"utf-8");
741 decl->append_attribute(ver);
742 decl->append_attribute(encoding);
743 doc.append_node(decl);
745 rapidxml::xml_node<>* rootNode = doc.allocate_node(rapidxml::node_element,
"Control");
746 doc.append_node(rootNode);
748 rapidxml::xml_node<>* projectNameNode = XMLParser::AppendNode(doc, rootNode,
"Name");
749 XMLParser::SetNodeValue(doc, projectNameNode, path.GetName());
751 auto elementsNode = XMLParser::AppendNode(doc, rootNode,
"ControlElements");
752 SaveControlElements(doc, elementsNode);
753 std::ofstream writeXML(path.GetFullPath().mb_str());
758bool FileHanding::OpenControl(wxFileName path,
759 std::vector< std::shared_ptr<ControlElement> >& ctrlElementList,
760 std::vector< std::shared_ptr<ConnectionLine> >& ctrlConnectionList)
762 rapidxml::xml_document<> doc;
763 rapidxml::file<> xmlFile(path.GetFullPath().mb_str());
765 doc.parse<0>(xmlFile.data());
767 auto projectNode = doc.first_node(
"Control");
768 if (!projectNode)
return false;
774 auto elementsNode = projectNode->first_node(
"ControlElements");
775 if (!elementsNode)
return false;
778 auto ctrlElementContainer =
new ControlElementContainer();
779 if (!OpenControlElements(doc, elementsNode, ctrlElementContainer))
return false;
781 ctrlElementList.clear();
782 ctrlConnectionList.clear();
783 auto elementRawList = ctrlElementContainer->GetControlElementsList();
784 for (
auto& element : elementRawList) {
785 ctrlElementList.emplace_back(element);
787 auto connectionRawList = ctrlElementContainer->GetConnectionLineList();
788 for (
auto& connection : connectionRawList) {
789 ctrlConnectionList.emplace_back(connection);
796void FileHanding::SaveControlElements(rapidxml::xml_document<>& doc,
797 rapidxml::xml_node<>* elementsNode,
800 if (!ctrlContainer) {
801 ctrlContainer =
new ControlElementContainer();
802 ctrlContainer->FillContainer(m_controlEditor);
806 auto constsNode = XMLParser::AppendNode(doc, elementsNode,
"ConstantList");
807 auto constList = ctrlContainer->GetConstantList();
808 for (
auto it = constList.begin(), itEnd = constList.end(); it != itEnd; ++it) {
809 (*it)->SaveElement(doc, constsNode);
813 auto expsNode = XMLParser::AppendNode(doc, elementsNode,
"ExponentialList");
814 auto expList = ctrlContainer->GetExponentialList();
815 for (
auto it = expList.begin(), itEnd = expList.end(); it != itEnd; ++it) { (*it)->SaveElement(doc, expsNode); }
818 auto gainsNode = XMLParser::AppendNode(doc, elementsNode,
"GainList");
819 auto gainList = ctrlContainer->GetGainList();
820 for (
auto it = gainList.begin(), itEnd = gainList.end(); it != itEnd; ++it) {
821 (*it)->SaveElement(doc, gainsNode);
825 auto iosNode = XMLParser::AppendNode(doc, elementsNode,
"IOList");
826 auto ioList = ctrlContainer->GetIOControlList();
827 for (
auto it = ioList.begin(), itEnd = ioList.end(); it != itEnd; ++it) { (*it)->SaveElement(doc, iosNode); }
830 auto limitersNode = XMLParser::AppendNode(doc, elementsNode,
"LimiterList");
831 auto limiterList = ctrlContainer->GetLimiterList();
832 for (
auto it = limiterList.begin(), itEnd = limiterList.end(); it != itEnd; ++it) {
833 (*it)->SaveElement(doc, limitersNode);
837 auto multipliersNode = XMLParser::AppendNode(doc, elementsNode,
"MultiplierList");
838 auto multiplierList = ctrlContainer->GetMultiplierList();
839 for (
auto it = multiplierList.begin(), itEnd = multiplierList.end(); it != itEnd; ++it) {
840 (*it)->SaveElement(doc, multipliersNode);
844 auto dividersNode = XMLParser::AppendNode(doc, elementsNode,
"DividerList");
845 auto dividersList = ctrlContainer->GetDividerList();
846 for (
auto it = dividersList.begin(), itEnd = dividersList.end(); it != itEnd; ++it) {
847 (*it)->SaveElement(doc, dividersNode);
851 auto rateLimitersNode = XMLParser::AppendNode(doc, elementsNode,
"RateLimiterList");
852 auto rateLimiterList = ctrlContainer->GetRateLimiterList();
853 for (
auto it = rateLimiterList.begin(), itEnd = rateLimiterList.end(); it != itEnd; ++it) {
854 (*it)->SaveElement(doc, rateLimitersNode);
858 auto sumsNode = XMLParser::AppendNode(doc, elementsNode,
"SumList");
859 auto sumList = ctrlContainer->GetSumList();
860 for (
auto it = sumList.begin(), itEnd = sumList.end(); it != itEnd; ++it) { (*it)->SaveElement(doc, sumsNode); }
863 auto mathExprsNode = XMLParser::AppendNode(doc, elementsNode,
"MathExprList");
864 auto mathExprList = ctrlContainer->GetMathExprList();
865 for (
auto it = mathExprList.begin(), itEnd = mathExprList.end(); it != itEnd; ++it) {
866 (*it)->SaveElement(doc, mathExprsNode);
870 auto tfsNode = XMLParser::AppendNode(doc, elementsNode,
"TransferFunctionList");
871 auto tfList = ctrlContainer->GetTFList();
872 for (
auto it = tfList.begin(), itEnd = tfList.end(); it != itEnd; ++it) { (*it)->SaveElement(doc, tfsNode); }
875 auto cLinesNode = XMLParser::AppendNode(doc, elementsNode,
"ConnectionList");
876 auto connLineList = ctrlContainer->GetConnectionLineList();
877 for (
auto it = connLineList.begin(), itEnd = connLineList.end(); it != itEnd; ++it) {
878 ConnectionLine* cLine = it->get();
879 auto cLineNode = XMLParser::AppendNode(doc, cLinesNode,
"Connection");
880 XMLParser::SetNodeAttribute(doc, cLineNode,
"ID", cLine->
GetID());
883 auto cadProp = XMLParser::AppendNode(doc, cLineNode,
"CADProperties");
884 auto offset = XMLParser::AppendNode(doc, cadProp,
"Offset");
885 XMLParser::SetNodeValue(doc, offset, cLine->GetOffset());
888 auto parentsNode = XMLParser::AppendNode(doc, cLineNode,
"ParentList");
891 for (
auto itP = parentList.begin(), itPEnd = parentList.end(); itP != itPEnd; ++itP) {
892 Element* parent = *itP;
893 auto parentNode = XMLParser::AppendNode(doc, parentsNode,
"Parent");
894 auto elementID = XMLParser::AppendNode(doc, parentNode,
"ElementID");
895 XMLParser::SetNodeValue(doc, elementID, parent->
GetID());
896 auto nodeID = XMLParser::AppendNode(doc, parentNode,
"NodeID");
897 XMLParser::SetNodeValue(doc, nodeID, cLine->GetNodeList()[nodeIndex]->GetID());
901 auto parentLine = XMLParser::AppendNode(doc, cLineNode,
"ParentLine");
902 if (cLine->GetParentLine()) {
903 ConnectionLine* parent = cLine->GetParentLine();
904 XMLParser::SetNodeAttribute(doc, parentLine,
"ID", parent->
GetID());
907 XMLParser::SetNodeAttribute(doc, parentLine,
"ID", -1);
912bool FileHanding::OpenControlElements(rapidxml::xml_document<>& doc,
913 rapidxml::xml_node<>* elementsNode,
916 std::vector< std::shared_ptr<ControlElement> > elementList;
917 std::vector< std::shared_ptr<ConnectionLine> > connectionList;
920 auto constListNode = elementsNode->first_node(
"ConstantList");
922 auto constNode = constListNode->first_node(
"Constant");
924 int id = XMLParser::GetAttributeValueInt(constNode,
"ID");
925 Constant* constant =
new Constant(
id);
927 if (!constant->OpenElement(constNode))
return false;
928 elementList.emplace_back(constant);
930 constNode = constNode->next_sibling(
"Constant");
936 auto expListNode = elementsNode->first_node(
"ExponentialList");
938 auto expNode = expListNode->first_node(
"Exponential");
940 int id = XMLParser::GetAttributeValueInt(expNode,
"ID");
941 Exponential* exponential =
new Exponential(
id);
943 if (!exponential->OpenElement(expNode))
return false;
944 elementList.emplace_back(exponential);
946 expNode = expNode->next_sibling(
"Exponential");
952 auto gainListNode = elementsNode->first_node(
"GainList");
954 auto gainNode = gainListNode->first_node(
"Gain");
956 int id = XMLParser::GetAttributeValueInt(gainNode,
"ID");
957 Gain* gain =
new Gain(
id);
959 if (!gain->OpenElement(gainNode))
return false;
960 elementList.emplace_back(gain);
962 gainNode = gainNode->next_sibling(
"Gain");
968 auto ioListNode = elementsNode->first_node(
"IOList");
970 auto ioNode = ioListNode->first_node(
"IO");
972 int id = XMLParser::GetAttributeValueInt(ioNode,
"ID");
973 int ioFlags = XMLParser::GetNodeValueInt(ioNode,
"IOFlags");
975 IOControl* io =
new IOControl(ioFlags,
id);
977 if (!io->OpenElement(ioNode))
return false;
978 elementList.emplace_back(io);
980 ioNode = ioNode->next_sibling(
"IO");
986 auto limiterListNode = elementsNode->first_node(
"LimiterList");
987 if (limiterListNode) {
988 auto limiterNode = limiterListNode->first_node(
"Limiter");
989 while (limiterNode) {
990 int id = XMLParser::GetAttributeValueInt(limiterNode,
"ID");
991 Limiter* limiter =
new Limiter(
id);
993 if (!limiter->OpenElement(limiterNode))
return false;
994 elementList.emplace_back(limiter);
996 limiterNode = limiterNode->next_sibling(
"Limiter");
1002 auto multiplierListNode = elementsNode->first_node(
"MultiplierList");
1003 if (multiplierListNode) {
1004 auto multiplierNode = multiplierListNode->first_node(
"Multiplier");
1005 while (multiplierNode) {
1006 int id = XMLParser::GetAttributeValueInt(multiplierNode,
"ID");
1007 Multiplier* multiplier =
new Multiplier(
id);
1009 if (!multiplier->OpenElement(multiplierNode))
return false;
1010 elementList.emplace_back(multiplier);
1012 multiplierNode = multiplierNode->next_sibling(
"Multiplier");
1018 auto dividerListNode = elementsNode->first_node(
"DividerList");
1019 if (dividerListNode) {
1020 auto dividerNode = dividerListNode->first_node(
"Divider");
1021 while (dividerNode) {
1022 int id = XMLParser::GetAttributeValueInt(dividerNode,
"ID");
1023 Divider* divider =
new Divider(
id);
1025 if (!divider->OpenElement(dividerNode))
return false;
1026 elementList.emplace_back(divider);
1028 dividerNode = dividerNode->next_sibling(
"Divider");
1034 auto rateLimiterListNode = elementsNode->first_node(
"RateLimiterList");
1035 if (rateLimiterListNode) {
1036 auto rateLimiterNode = rateLimiterListNode->first_node(
"RateLimiter");
1037 while (rateLimiterNode) {
1038 int id = XMLParser::GetAttributeValueInt(rateLimiterNode,
"ID");
1039 RateLimiter* limiter =
new RateLimiter(
id);
1041 if (!limiter->OpenElement(rateLimiterNode))
return false;
1042 elementList.emplace_back(limiter);
1044 rateLimiterNode = rateLimiterNode->next_sibling(
"RateLimiter");
1050 auto sumListNode = elementsNode->first_node(
"SumList");
1052 auto sumNode = sumListNode->first_node(
"Sum");
1054 int id = XMLParser::GetAttributeValueInt(sumNode,
"ID");
1055 Sum* sum =
new Sum(
id);
1057 if (!sum->OpenElement(sumNode))
return false;
1058 elementList.emplace_back(sum);
1060 sumNode = sumNode->next_sibling(
"Sum");
1066 auto mathListNode = elementsNode->first_node(
"MathExprList");
1068 auto mathExprNode = mathListNode->first_node(
"MathExpr");
1069 while (mathExprNode) {
1070 int id = XMLParser::GetAttributeValueInt(mathExprNode,
"ID");
1071 MathExpression* mathExpr =
new MathExpression(
id);
1073 if (!mathExpr->OpenElement(mathExprNode))
return false;
1074 elementList.emplace_back(mathExpr);
1076 mathExprNode = mathExprNode->next_sibling(
"MathExpr");
1082 auto tfListNode = elementsNode->first_node(
"TransferFunctionList");
1084 auto tfNode = tfListNode->first_node(
"TransferFunction");
1086 int id = XMLParser::GetAttributeValueInt(tfNode,
"ID");
1087 TransferFunction* tf =
new TransferFunction(
id);
1089 if (!tf->OpenElement(tfNode))
return false;
1090 elementList.emplace_back(tf);
1092 tfNode = tfNode->next_sibling(
"TransferFunction");
1098 auto connectionListNode = elementsNode->first_node(
"ConnectionList");
1099 if (connectionListNode) {
1100 auto connNode = connectionListNode->first_node(
"Connection");
1102 ConnectionLine* cLine =
nullptr;
1103 int id = XMLParser::GetAttributeValueInt(connNode,
"ID");
1105 auto cadPropNode = connNode->first_node(
"CADProperties");
1106 if (!cadPropNode)
return false;
1107 double offset = XMLParser::GetNodeValueDouble(cadPropNode,
"Offset");
1109 auto parentList = connNode->first_node(
"ParentList");
1110 if (!parentList)
return false;
1112 auto parentNode = parentList->first_node(
"Parent");
1113 bool firstNode =
true;
1114 while (parentNode) {
1115 int elementID = XMLParser::GetNodeValueInt(parentNode,
"ElementID");
1116 int nodeID = XMLParser::GetNodeValueInt(parentNode,
"NodeID");
1118 ControlElement* element = ControlElement::GetControlElementFromID(elementList, elementID);
1119 Node* node = element->GetNodeList()[nodeID];
1121 if (firstNode) cLine =
new ConnectionLine(node,
id);
1124 if (!firstNode) cLine->AppendNode(node, element);
1126 if (firstNode) firstNode =
false;
1127 parentNode = parentNode->next_sibling(
"Parent");
1130 auto parentLine = connNode->first_node(
"ParentLine");
1131 if (!parentLine)
return false;
1132 int parentLineID = XMLParser::GetAttributeValueInt(parentLine,
"ID");
1133 if (parentLineID != -1) {
1134 for (
auto it = connectionList.begin(), itEnd = connectionList.end(); it != itEnd; ++it) {
1135 ConnectionLine* parent = it->get();
1136 if (parent->
GetID() == parentLineID) {
1137 cLine->SetParentLine(parent);
1143 cLine->SetOffset(offset);
1144 cLine->UpdatePoints();
1145 connectionList.emplace_back(cLine);
1146 connNode = connNode->next_sibling(
"Connection");
1150 ctrlContainer->FillContainer(elementList, connectionList);
Class that can contain all control elements. Can identify (using RTTI) the elements from a generic li...
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).
virtual std::vector< Element * > GetParentList() const
Get the parent list.
virtual int GetID() const
Get the element ID.
virtual bool AddParent(Element *parent, wxPoint2DDouble position, bool isOpening=false)
Add a parent to the element. This method must be used on power elements that connect to a bus,...
virtual void AddChild(Element *child)
Add a child to the child list.
This class manages the graphical and power elements. It is responsible for handling the user's intera...