Power System Platform  2026w34a-beta
Loading...
Searching...
No Matches
FileHanding Class Reference

Save and opens the projects created on disk. More...

#include <FileHanding.h>

Collaboration diagram for FileHanding:

Public Member Functions

 FileHanding (Workspace *workspace)
 FileHanding (ControlEditor *controlEditor)
void SetWorkspace (Workspace *workspace)
void SetControlEditor (ControlEditor *controlEditor)
void SaveProject (wxFileName path)
bool OpenProject (wxFileName path)
void SaveControl (wxFileName path)
bool OpenControl (wxFileName path, std::vector< std::shared_ptr< ControlElement > > &ctrlElementList, std::vector< std::shared_ptr< ConnectionLine > > &ctrlConnectionList)
void SaveControlElements (rapidxml::xml_document<> &doc, rapidxml::xml_node<> *elementsNode, ControlElementContainer *ctrlContainer=nullptr)
bool OpenControlElements (rapidxml::xml_document<> &doc, rapidxml::xml_node<> *elementsNode, ControlElementContainer *ctrlContainer=nullptr)

Protected Attributes

Workspace * m_workspace = nullptr
ControlEditor * m_controlEditor = nullptr

Detailed Description

Save and opens the projects created on disk.

Author
Thales Lima Oliveira thale.nosp@m.s@uf.nosp@m.u.br
Date
05/10/2017

Definition at line 42 of file FileHanding.h.

Constructor & Destructor Documentation

◆ FileHanding() [1/3]

FileHanding::FileHanding ( )

Definition at line 25 of file FileHanding.cpp.

25{}

◆ FileHanding() [2/3]

FileHanding::FileHanding ( Workspace * workspace)

Definition at line 23 of file FileHanding.cpp.

23{ m_workspace = workspace; }

◆ FileHanding() [3/3]

FileHanding::FileHanding ( ControlEditor * controlEditor)

Definition at line 24 of file FileHanding.cpp.

24{ m_controlEditor = controlEditor; }

◆ ~FileHanding()

FileHanding::~FileHanding ( )

Definition at line 22 of file FileHanding.cpp.

22{}

Member Function Documentation

◆ OpenControl()

bool FileHanding::OpenControl ( wxFileName path,
std::vector< std::shared_ptr< ControlElement > > & ctrlElementList,
std::vector< std::shared_ptr< ConnectionLine > > & ctrlConnectionList )

Definition at line 758 of file FileHanding.cpp.

761{
762 rapidxml::xml_document<> doc;
763 rapidxml::file<> xmlFile(path.GetFullPath().mb_str());
764
765 doc.parse<0>(xmlFile.data());
766
767 auto projectNode = doc.first_node("Control");
768 if (!projectNode) return false;
769 // auto nameNode = projectNode->first_node("Name");
770 // if(!nameNode) return false;
771 // m_controlEditor->SetName(nameNode->value());
772
773 // Open elements
774 auto elementsNode = projectNode->first_node("ControlElements");
775 if (!elementsNode) return false;
776
777 // auto elementsNode = XMLParser::AppendNode(doc, rootNode, "ControlElements");
778 auto ctrlElementContainer = new ControlElementContainer();
779 if (!OpenControlElements(doc, elementsNode, ctrlElementContainer)) return false;
780
781 ctrlElementList.clear();
782 ctrlConnectionList.clear();
783 auto elementRawList = ctrlElementContainer->GetControlElementsList();
784 for (auto& element : elementRawList) {
785 ctrlElementList.emplace_back(element);
786 }
787 auto connectionRawList = ctrlElementContainer->GetConnectionLineList();
788 for (auto& connection : connectionRawList) {
789 ctrlConnectionList.emplace_back(connection);
790 }
791 //ctrlElementList = ctrlElementContainer->GetControlElementsList();
792 //ctrlConnectionList = ctrlElementContainer->GetConnectionLineList();
793 return true;
794}

◆ OpenControlElements()

bool FileHanding::OpenControlElements ( rapidxml::xml_document<> & doc,
rapidxml::xml_node<> * elementsNode,
ControlElementContainer * ctrlContainer = nullptr )

Definition at line 912 of file FileHanding.cpp.

915{
916 std::vector< std::shared_ptr<ControlElement> > elementList;
917 std::vector< std::shared_ptr<ConnectionLine> > connectionList;
918
919 //{ Constant
920 auto constListNode = elementsNode->first_node("ConstantList");
921 if (constListNode) {
922 auto constNode = constListNode->first_node("Constant");
923 while (constNode) {
924 int id = XMLParser::GetAttributeValueInt(constNode, "ID");
925 Constant* constant = new Constant(id);
926
927 if (!constant->OpenElement(constNode)) return false;
928 elementList.emplace_back(constant);
929
930 constNode = constNode->next_sibling("Constant");
931 }
932 }
933 //}
934
935 //{ Exponential
936 auto expListNode = elementsNode->first_node("ExponentialList");
937 if (expListNode) {
938 auto expNode = expListNode->first_node("Exponential");
939 while (expNode) {
940 int id = XMLParser::GetAttributeValueInt(expNode, "ID");
941 Exponential* exponential = new Exponential(id);
942
943 if (!exponential->OpenElement(expNode)) return false;
944 elementList.emplace_back(exponential);
945
946 expNode = expNode->next_sibling("Exponential");
947 }
948 }
949 //}
950
951 //{ Gain
952 auto gainListNode = elementsNode->first_node("GainList");
953 if (gainListNode) {
954 auto gainNode = gainListNode->first_node("Gain");
955 while (gainNode) {
956 int id = XMLParser::GetAttributeValueInt(gainNode, "ID");
957 Gain* gain = new Gain(id);
958
959 if (!gain->OpenElement(gainNode)) return false;
960 elementList.emplace_back(gain);
961
962 gainNode = gainNode->next_sibling("Gain");
963 }
964 }
965 //}
966
967 //{ IO
968 auto ioListNode = elementsNode->first_node("IOList");
969 if (ioListNode) {
970 auto ioNode = ioListNode->first_node("IO");
971 while (ioNode) {
972 int id = XMLParser::GetAttributeValueInt(ioNode, "ID");
973 int ioFlags = XMLParser::GetNodeValueInt(ioNode, "IOFlags");
974
975 IOControl* io = new IOControl(ioFlags, id);
976
977 if (!io->OpenElement(ioNode)) return false;
978 elementList.emplace_back(io);
979
980 ioNode = ioNode->next_sibling("IO");
981 }
982 }
983 //}
984
985 //{ Limiter
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);
992
993 if (!limiter->OpenElement(limiterNode)) return false;
994 elementList.emplace_back(limiter);
995
996 limiterNode = limiterNode->next_sibling("Limiter");
997 }
998 }
999 //}
1000
1001 //{ Multiplier
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);
1008
1009 if (!multiplier->OpenElement(multiplierNode)) return false;
1010 elementList.emplace_back(multiplier);
1011
1012 multiplierNode = multiplierNode->next_sibling("Multiplier");
1013 }
1014 }
1015 //}
1016
1017 //{ Divider
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);
1024
1025 if (!divider->OpenElement(dividerNode)) return false;
1026 elementList.emplace_back(divider);
1027
1028 dividerNode = dividerNode->next_sibling("Divider");
1029 }
1030 }
1031 //}
1032
1033 //{ Rate limiter
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);
1040
1041 if (!limiter->OpenElement(rateLimiterNode)) return false;
1042 elementList.emplace_back(limiter);
1043
1044 rateLimiterNode = rateLimiterNode->next_sibling("RateLimiter");
1045 }
1046 }
1047 //}
1048
1049 //{ Sum
1050 auto sumListNode = elementsNode->first_node("SumList");
1051 if (sumListNode) {
1052 auto sumNode = sumListNode->first_node("Sum");
1053 while (sumNode) {
1054 int id = XMLParser::GetAttributeValueInt(sumNode, "ID");
1055 Sum* sum = new Sum(id);
1056
1057 if (!sum->OpenElement(sumNode)) return false;
1058 elementList.emplace_back(sum);
1059
1060 sumNode = sumNode->next_sibling("Sum");
1061 }
1062 }
1063 //}
1064
1065 //{ Math expression
1066 auto mathListNode = elementsNode->first_node("MathExprList");
1067 if (mathListNode) {
1068 auto mathExprNode = mathListNode->first_node("MathExpr");
1069 while (mathExprNode) {
1070 int id = XMLParser::GetAttributeValueInt(mathExprNode, "ID");
1071 MathExpression* mathExpr = new MathExpression(id);
1072
1073 if (!mathExpr->OpenElement(mathExprNode)) return false;
1074 elementList.emplace_back(mathExpr);
1075
1076 mathExprNode = mathExprNode->next_sibling("MathExpr");
1077 }
1078 }
1079 //}
1080
1081 //{ Transfer function
1082 auto tfListNode = elementsNode->first_node("TransferFunctionList");
1083 if (tfListNode) {
1084 auto tfNode = tfListNode->first_node("TransferFunction");
1085 while (tfNode) {
1086 int id = XMLParser::GetAttributeValueInt(tfNode, "ID");
1087 TransferFunction* tf = new TransferFunction(id);
1088
1089 if (!tf->OpenElement(tfNode)) return false;
1090 elementList.emplace_back(tf);
1091
1092 tfNode = tfNode->next_sibling("TransferFunction");
1093 }
1094 }
1095 //}
1096
1097 //{ Connection line
1098 auto connectionListNode = elementsNode->first_node("ConnectionList");
1099 if (connectionListNode) {
1100 auto connNode = connectionListNode->first_node("Connection");
1101 while (connNode) {
1102 ConnectionLine* cLine = nullptr;
1103 int id = XMLParser::GetAttributeValueInt(connNode, "ID");
1104
1105 auto cadPropNode = connNode->first_node("CADProperties");
1106 if (!cadPropNode) return false;
1107 double offset = XMLParser::GetNodeValueDouble(cadPropNode, "Offset");
1108
1109 auto parentList = connNode->first_node("ParentList");
1110 if (!parentList) return false;
1111
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");
1117
1118 ControlElement* element = ControlElement::GetControlElementFromID(elementList, elementID);
1119 Node* node = element->GetNodeList()[nodeID];
1120
1121 if (firstNode) cLine = new ConnectionLine(node, id);
1122 cLine->AddParent(element);
1123 element->AddChild(cLine);
1124 if (!firstNode) cLine->AppendNode(node, element);
1125
1126 if (firstNode) firstNode = false;
1127 parentNode = parentNode->next_sibling("Parent");
1128 }
1129
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);
1138 parent->AddChild(cLine);
1139 }
1140 }
1141 }
1142
1143 cLine->SetOffset(offset);
1144 cLine->UpdatePoints();
1145 connectionList.emplace_back(cLine);
1146 connNode = connNode->next_sibling("Connection");
1147 }
1148 } //}
1149
1150 ctrlContainer->FillContainer(elementList, connectionList);
1151 return true;
1152}
virtual int GetID() const
Get the element ID.
Definition Element.h:275
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,...
Definition Element.h:243
virtual void AddChild(Element *child)
Add a child to the child list.
Definition Element.cpp:581

◆ OpenProject()

bool FileHanding::OpenProject ( wxFileName path)

Definition at line 359 of file FileHanding.cpp.

360{
361 rapidxml::xml_document<> doc;
362 wxString fullPath = path.GetFullPath();
363
364 // 1. Open the file safely, supporting accented paths on Windows
365 wxFFile file(fullPath, "rb");
366 if (!file.IsOpened())
367 {
368 wxMessageBox(_("Could not open the file:\n") + fullPath, _("Error"), wxICON_ERROR | wxOK);
369 return false;
370 }
371
372 // 2. Read the entire content into a wxString with automatic encoding detection (UTF-8, UTF-16, BOM)
373 wxString fileContent;
374 if (!file.ReadAll(&fileContent, wxConvAuto()))
375 {
376 wxMessageBox(_("Error reading the XML file:\n") + fullPath, _("Error"), wxICON_ERROR | wxOK);
377 return false;
378 }
379
380 // 3. Convert the wxString content to a clean UTF-8 std::string buffer
381 std::string utf8Str = fileContent.ToStdString(wxConvUTF8);
382
383 // 4. Copy to a mutable std::vector<char> required by RapidXML (in-place parsing)
384 std::vector<char> xmlData(utf8Str.begin(), utf8Str.end());
385 xmlData.push_back('\0'); // Ensure null-termination
386
387 try
388 {
389 // 5. Parse the document safely
390 doc.parse<0>(xmlData.data());
391 }
392 catch (const std::exception& e)
393 {
394 wxMessageBox(_("XML Parsing Error: ") + wxString::FromUTF8(e.what()), _("Error"), wxICON_ERROR | wxOK);
395 return false;
396 }
397
398 // Parse the document
399 //doc.parse<0>(xmlData.data());
400
401 //rapidxml::file<> xmlFile(path.GetFullPath().mb_str());
402 //doc.parse<0>(xmlFile.data());
403
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());
409
410 PropertiesData* propData = m_workspace->GetProperties();
411 SimulationData simData = propData->GetSimulationPropertiesData();
412
413 //{ Properties data
414 auto propertiesNode = projectNode->first_node("Properties");
415 if (propertiesNode) {
416 auto simPropertiesNode = propertiesNode->first_node("SimulationProperties");
417 if (simPropertiesNode) {
418 // General
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;
428
429 // Power flow
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");
437
438 // Stability
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");
448
449 // ZIP load
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");
463 }
464 }
465 //}
466
467 propData->SetSimulationPropertiesData(simData);
468
469 // Open elements
470 auto elementsNode = projectNode->first_node("Elements");
471 if (!elementsNode) return false;
472 std::vector<Element*> elementList;
473 // Save lists individually to get parents
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;
485 //std::vector<Text*> textList;
486 std::vector< std::shared_ptr<Text> > textList;
487
488 // List of parents
489 std::vector<Element*> parentList;
490
491 //{ Bus
492 auto busListNode = elementsNode->first_node("BusList");
493 if (!busListNode) return false;
494 auto busNode = busListNode->first_node("Bus");
495 while (busNode) {
496 Bus* bus = new Bus();
497
498 if (!bus->OpenElement(busNode)) return false;
499 elementList.push_back(bus);
500 busList.push_back(bus);
501 parentList.push_back(bus);
502
503 busNode = busNode->next_sibling("Bus");
504 } //}
505
506 //{ Capacitor
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();
512
513 if (!capacitor->OpenElement(capacitorNode, parentList)) return false;
514 elementList.push_back(capacitor);
515 capacitorList.push_back(capacitor);
516
517 capacitorNode = capacitorNode->next_sibling("Capacitor");
518 } //}
519
520 //{ IndMotor
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();
526
527 if (!indMotor->OpenElement(indMotorNode, parentList)) return false;
528 elementList.push_back(indMotor);
529 indMotorList.push_back(indMotor);
530
531 indMotorNode = indMotorNode->next_sibling("IndMotor");
532 } //}
533
534 //{ Inductor
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();
540
541 if (!inductor->OpenElement(inductorNode, parentList)) return false;
542 elementList.push_back(inductor);
543 inductorList.push_back(inductor);
544
545 inductorNode = inductorNode->next_sibling("Inductor");
546 } //}
547
548 //{ Line
549 auto lineListNode = elementsNode->first_node("LineList");
550 if (!lineListNode) return false;
551 auto lineNode = lineListNode->first_node("Line");
552 while (lineNode) {
553 Line* line = new Line();
554
555 if (!line->OpenElement(lineNode, parentList)) return false;
556 elementList.push_back(line);
557 lineList.push_back(line);
558
559 lineNode = lineNode->next_sibling("Line");
560 } //}
561
562 //{ Load
563 auto loadListNode = elementsNode->first_node("LoadList");
564 if (!loadListNode) return false;
565 auto loadNode = loadListNode->first_node("Load");
566 while (loadNode) {
567 Load* load = new Load();
568
569 if (!load->OpenElement(loadNode, parentList)) return false;
570 elementList.push_back(load);
571 loadList.push_back(load);
572
573 loadNode = loadNode->next_sibling("Load");
574 } //}
575
576 //{ SyncGenerator
577 auto syncGeneratorListNode = elementsNode->first_node("SyncGeneratorList");
578 if (!syncGeneratorListNode) return false;
579 auto syncGeneratorNode = syncGeneratorListNode->first_node("SyncGenerator");
580
581 while (syncGeneratorNode) {
582 SyncGenerator* syncGenerator = new SyncGenerator();
583
584 if (!syncGenerator->OpenElement(syncGeneratorNode, parentList)) return false;
585
586 // Open controls.
587 auto data = syncGenerator->GetElectricalData();
588 auto electricalProp = syncGeneratorNode->first_node("ElectricalProperties");
589 auto stability = electricalProp->first_node("Stability");
590
591 auto avr = stability->first_node("AVR");
592 if (!avr) return false;
593 if (!OpenControlElements(doc, avr, data.avr)) return false;
594
595 auto speedGov = stability->first_node("SpeedGovernor");
596 if (!speedGov) return false;
597 if (!OpenControlElements(doc, speedGov, data.speedGov)) return false;
598
599 syncGenerator->SetElectricalData(data);
600
601 elementList.push_back(syncGenerator);
602 syncGeneratorList.push_back(syncGenerator);
603 syncGeneratorNode = syncGeneratorNode->next_sibling("SyncGenerator");
604 } //}
605
606 //{ SyncMotor
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();
612
613 if (!syncMotor->OpenElement(syncMotorNode, parentList)) return false;
614 elementList.push_back(syncMotor);
615 syncMotorList.push_back(syncMotor);
616
617 syncMotorNode = syncMotorNode->next_sibling("SyncMotor");
618 } //}
619
620 //{ Transformer
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();
626
627 if (!transformer->OpenElement(transfomerNode, parentList)) return false;
628 elementList.push_back(transformer);
629 transformerList.push_back(transformer);
630
631 transfomerNode = transfomerNode->next_sibling("Transfomer");
632 } //}
633
634 //{ HarmCurrent
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();
640
641 if (!harmCurrent->OpenElement(harmCurrentNode, parentList)) return false;
642 elementList.push_back(harmCurrent);
643 harmCurrentList.push_back(harmCurrent);
644
645 harmCurrentNode = harmCurrentNode->next_sibling("HarmCurrent");
646 }
647 } //}
648 //{ 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();
654
655 if (!emtElement->OpenElement(emtElementNode, parentList)) return false;
656 elementList.push_back(emtElement);
657 emtElementList.push_back(emtElement);
658
659 emtElementNode = emtElementNode->next_sibling("EMTElement");
660 }
661 } //}
662
663 m_workspace->SetElementList(elementList);
664
665 //{ Text
666 auto textListNode = elementsNode->first_node("TextList");
667 if (!textListNode) return false;
668 auto textNode = textListNode->first_node("Text");
669 while (textNode) {
670 //Text* text = new Text();
671 auto text = std::make_shared<Text>();
672
673 if (!text->OpenElement(textNode)) return true;
674
675 switch (text->GetElementTypeText()) {
676 case TYPE_NONE:
677 break;
678 case TYPE_BUS: {
679 Bus* bus = busList[text->GetElementNumber()];
680 text->SetElement(bus);
681 } break;
682 case TYPE_CAPACITOR: {
683 Capacitor* capacitor = capacitorList[text->GetElementNumber()];
684 text->SetElement(capacitor);
685 } break;
686 case TYPE_IND_MOTOR: {
687 IndMotor* indMotor = indMotorList[text->GetElementNumber()];
688 text->SetElement(indMotor);
689 } break;
690 case TYPE_INDUCTOR: {
691 Inductor* inductor = inductorList[text->GetElementNumber()];
692 text->SetElement(inductor);
693 } break;
694 case TYPE_LINE: {
695 Line* line = lineList[text->GetElementNumber()];
696 text->SetElement(line);
697 } break;
698 case TYPE_LOAD: {
699 Load* load = loadList[text->GetElementNumber()];
700 text->SetElement(load);
701 } break;
702 case TYPE_SYNC_GENERATOR: {
703 SyncGenerator* syncGenerator = syncGeneratorList[text->GetElementNumber()];
704 text->SetElement(syncGenerator);
705 } break;
706 case TYPE_SYNC_MOTOR: {
707 SyncMotor* syncMotor = syncMotorList[text->GetElementNumber()];
708 text->SetElement(syncMotor);
709 } break;
710 case TYPE_TRANSFORMER: {
711 Transformer* transformer = transformerList[text->GetElementNumber()];
712 text->SetElement(transformer);
713 } break;
714 case TYPE_HARMCURRENT: {
715 HarmCurrent* harmCurrent = harmCurrentList[text->GetElementNumber()];
716 text->SetElement(harmCurrent);
717 } break;
718 }
719
720 textList.push_back(text);
721 textNode = textNode->next_sibling("Text");
722 } //}
723
724 m_workspace->SetTextList(textList);
725 return true;
726}

◆ SaveControl()

void FileHanding::SaveControl ( wxFileName path)

Definition at line 728 of file FileHanding.cpp.

729{
730 // Same process present in SaveProject():
731 std::ofstream writeProjectsFile(path.GetFullPath().mb_str());
732 writeProjectsFile.close();
733
734 rapidxml::xml_document<> doc;
735 rapidxml::file<> xmlFile(path.GetFullPath().mb_str());
736 doc.parse<0>(xmlFile.data());
737
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);
744
745 rapidxml::xml_node<>* rootNode = doc.allocate_node(rapidxml::node_element, "Control");
746 doc.append_node(rootNode);
747
748 rapidxml::xml_node<>* projectNameNode = XMLParser::AppendNode(doc, rootNode, "Name");
749 XMLParser::SetNodeValue(doc, projectNameNode, path.GetName());
750
751 auto elementsNode = XMLParser::AppendNode(doc, rootNode, "ControlElements");
752 SaveControlElements(doc, elementsNode);
753 std::ofstream writeXML(path.GetFullPath().mb_str());
754 writeXML << doc;
755 writeXML.close();
756}

◆ SaveControlElements()

void FileHanding::SaveControlElements ( rapidxml::xml_document<> & doc,
rapidxml::xml_node<> * elementsNode,
ControlElementContainer * ctrlContainer = nullptr )

Definition at line 796 of file FileHanding.cpp.

799{
800 if (!ctrlContainer) {
801 ctrlContainer = new ControlElementContainer();
802 ctrlContainer->FillContainer(m_controlEditor);
803 }
804
805 //{ Constant
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);
810 } //}
811
812 //{ Exponential
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); } //}
816
817 //{ Gain
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);
822 } //}
823
824 //{ IO
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); } //}
828
829 //{ Limiter
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);
834 } //}
835
836 //{ Multiplier
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);
841 } //}
842
843 //{ Divider
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);
848 } //}
849
850 //{ Rate limiter
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);
855 } //}
856
857 //{ Sum
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); } //}
861
862 //{ Math expression
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);
867 } //}
868
869 //{ Transfer function
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); } //}
873
874 //{ Connection line
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());
881
882 // CAD properties
883 auto cadProp = XMLParser::AppendNode(doc, cLineNode, "CADProperties");
884 auto offset = XMLParser::AppendNode(doc, cadProp, "Offset");
885 XMLParser::SetNodeValue(doc, offset, cLine->GetOffset());
886
887 // Parent list
888 auto parentsNode = XMLParser::AppendNode(doc, cLineNode, "ParentList");
889 auto parentList = cLine->GetParentList();
890 int nodeIndex = 0;
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());
898 nodeIndex++;
899 }
900
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());
905 }
906 else {
907 XMLParser::SetNodeAttribute(doc, parentLine, "ID", -1);
908 }
909 } //}
910}
virtual std::vector< Element * > GetParentList() const
Get the parent list.
Definition Element.h:567

◆ SaveProject()

void FileHanding::SaveProject ( wxFileName path)

Definition at line 26 of file FileHanding.cpp.

27{
28 // Erase the file (if exists or not) and write the initial data
29 //std::ofstream writeProjectsFile(path.GetFullPath().mb_str());
30 //writeProjectsFile.close();
31 //
32 //rapidxml::xml_document<> doc;
33 //rapidxml::file<> xmlFile(path.GetFullPath());
34 //doc.parse<0>(xmlFile.data());
35 // 1. Erase/create the file and write a minimal valid XML structure
36// (Note: RapidXML throws an error if the file is completely empty)
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 // 2. Open the file safely for reading (handling accented paths via wxFFile)
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 // 3. Read content with automatic encoding support
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 // 4. Convert to a clean UTF-8 std::string buffer for RapidXML
66 std::string utf8Str = fileContent.ToStdString(wxConvUTF8);
67 std::vector<char> xmlData(utf8Str.begin(), utf8Str.end());
68 xmlData.push_back('\0'); // Ensure null-termination
69
70 // 5. Parse the document safely
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 //{ Simulation properties
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 // Save all the data
172 ElectricCalculation allElements;
173 allElements.GetElementsFromList(m_workspace->GetElementList());
174 int elementID = 0;
175
176 //{ Buses
177 auto busesNode = XMLParser::AppendNode(doc, elementsNode, "BusList");
178 auto busList = allElements.GetBusList();
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 //{ Capacitor
189 auto capacitorsNode = XMLParser::AppendNode(doc, elementsNode, "CapacitorList");
190 auto capacitorList = allElements.GetCapacitorList();
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 //{ IndMotor
202 auto indMotorsNode = XMLParser::AppendNode(doc, elementsNode, "IndMotorList");
203 auto indMotorList = allElements.GetIndMotorList();
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 //{ Inductor
215 auto inductorsNode = XMLParser::AppendNode(doc, elementsNode, "InductorList");
216 auto inductorList = allElements.GetInductorList();
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 //{ Line
228 auto linesNode = XMLParser::AppendNode(doc, elementsNode, "LineList");
229 auto lineList = allElements.GetLineList();
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 //{ Load
241 auto loadsNode = XMLParser::AppendNode(doc, elementsNode, "LoadList");
242 auto loadList = allElements.GetLoadList();
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 //{ SyncGenerator
254 auto syncGeneratorsNode = XMLParser::AppendNode(doc, elementsNode, "SyncGeneratorList");
255 auto syncGeneratorList = allElements.GetSyncGeneratorList();
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 // Save controls
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 //{ SyncMotor
279 auto syncMotorsNode = XMLParser::AppendNode(doc, elementsNode, "SyncMotorList");
280 auto syncMotorList = allElements.GetSyncMotorList();
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 //{ Transfomer
292 auto transformersNode = XMLParser::AppendNode(doc, elementsNode, "TransformerList");
293 auto transformerList = allElements.GetTransformerList();
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 //{ HarmCurrent
305 auto harmCurrentNode = XMLParser::AppendNode(doc, elementsNode, "HarmCurrentList");
306 auto harmCurrentList = allElements.GetHarmCurrentList();
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 //{ EMTElement
318 auto emtElementNode = XMLParser::AppendNode(doc, elementsNode, "EMTElementList");
319 auto emtElementList = allElements.GetEMTElementList();
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 //{ Text
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 // 6. Serialize the modified DOM tree back to a std::string
344 std::string xmlOutput;
345 rapidxml::print(std::back_inserter(xmlOutput), doc, 0);
346
347 // 7. Save the final XML string back to the file safely (handling accented paths via wxFFile)
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).

◆ SetControlEditor()

void FileHanding::SetControlEditor ( ControlEditor * controlEditor)
inline

Definition at line 51 of file FileHanding.h.

51{ m_controlEditor = controlEditor; }

◆ SetWorkspace()

void FileHanding::SetWorkspace ( Workspace * workspace)
inline

Definition at line 50 of file FileHanding.h.

50{ m_workspace = workspace; }

Member Data Documentation

◆ m_controlEditor

ControlEditor* FileHanding::m_controlEditor = nullptr
protected

Definition at line 66 of file FileHanding.h.

◆ m_workspace

Workspace* FileHanding::m_workspace = nullptr
protected

Definition at line 65 of file FileHanding.h.


The documentation for this class was generated from the following files: