#include "workspace/WorkSpaceXMLParse.h" #include "workspace/WorkSpace.h" #include "entities/EntitiesManager.h" #include "common/SpdLogger.h" #include "workspace/WorkSpaceManager.h" #include "utils/StringUtils.h" WorkSpaceXMLParse::WorkSpaceXMLParse(WorkSpace* workspace, QObject* parent) noexcept : QObject(parent) , workSpace_(workspace) { } bool WorkSpaceXMLParse::Save(const QString& path) { path_ = path; return false; } bool WorkSpaceXMLParse::ParseScene(const tinyxml2::XMLElement* element) { if (nullptr == element) { LOG_WARN("element is nullptr"); return false; } const tinyxml2::XMLAttribute* current = element->FirstAttribute(); bool flag = false; while (nullptr != current) { const char* eleName = current->Name(); if (0 == strcmp(eleName, "name")) { const char* name = current->Value(); workSpace_->SetName(name); flag = true; } else if (0 == strcmp(eleName, "describe")) { const char* value = current->Value(); workSpace_->SetDescribe(value); flag = true; } else if (0 == strcmp(eleName, "uuid")) { const char* value = current->Value(); workSpace_->SetUUid(value); flag = true; } else if (0 == strcmp(eleName, "viewpoint")) { const char* value = current->Value(); workSpace_->SetHomeViewpoint(StringUtils::StringToViewpoint("home", value)); flag = true; } current = current->Next(); } return flag; } bool WorkSpaceXMLParse::ParseTimestep(const tinyxml2::XMLElement* element) { if (nullptr == element) { LOG_WARN("element is nullptr"); return false; } const char* path = element->Attribute("path"); if (nullptr == path) { LOG_WARN("element not has path"); return false; } return workSpace_->SetTimestepPath(path); } bool WorkSpaceXMLParse::ParseLamp(const tinyxml2::XMLElement* element) { if (nullptr == element) { LOG_WARN("element is nullptr"); return false; } const char* path = element->Attribute("path"); if (nullptr == path) { LOG_WARN("element not has path"); return false; } return workSpace_->SetLampPath(path); } bool WorkSpaceXMLParse::ParseCommond(const tinyxml2::XMLElement* element) { if (nullptr == element) { LOG_WARN("commond element is nullptr"); return false; } const char* path = element->Attribute("path"); if (nullptr == path) { LOG_WARN("commond element not has path"); return false; } // Set the command file path using the filename stored in XML workSpace_->commondPath_ = path; return true; } bool WorkSpaceXMLParse::ParseFiles(const tinyxml2::XMLElement* element) { if (nullptr == element) { LOG_WARN("element is nullptr"); return false; } QList fileTypes; const tinyxml2::XMLElement* typeElement = element->FirstChildElement("type"); while (nullptr != typeElement) { const char* name = typeElement->Attribute("name"); int count = 0; typeElement->QueryIntAttribute("count", &count); if (nullptr != name && count > 0) { FileTypeData fileTypeData; fileTypeData.typeName = QString::fromLocal8Bit(name); fileTypeData.count = count; QString typeName = QString::fromLocal8Bit(name); // Parse chart elements within this type const tinyxml2::XMLElement* chartElement = typeElement->FirstChildElement("chart"); while (nullptr != chartElement) { std::shared_ptr chartData; // Create appropriate chart type based on the file type if (typeName == FileEntryTypeToString(FileEntryType::Curve)) { auto curveChart = std::make_shared(); // Parse curve-specific attributes if (const char* chartName = chartElement->Attribute("name")) { curveChart->name = QString::fromLocal8Bit(chartName); } if (const char* path = chartElement->Attribute("path")) { curveChart->path = QString::fromLocal8Bit(path); } if (const char* xTitle = chartElement->Attribute("xTitle")) { curveChart->xTitle = QString::fromLocal8Bit(xTitle); } if (const char* yTitle = chartElement->Attribute("yTitle")) { curveChart->yTitle = QString::fromLocal8Bit(yTitle); } if (const char* xMin = chartElement->Attribute("xMin")) { curveChart->xMin = QString::fromLocal8Bit(xMin).toDouble(); } if (const char* xMax = chartElement->Attribute("xMax")) { curveChart->xMax = QString::fromLocal8Bit(xMax).toDouble(); } if (const char* yMin = chartElement->Attribute("yMin")) { curveChart->yMin = QString::fromLocal8Bit(yMin).toDouble(); } if (const char* yMax = chartElement->Attribute("yMax")) { curveChart->yMax = QString::fromLocal8Bit(yMax).toDouble(); } if (const char* xCount = chartElement->Attribute("xCount")) { curveChart->xCount = QString::fromLocal8Bit(xCount).toInt(); } if (const char* t = chartElement->Attribute("t")) { curveChart->t = QString::fromLocal8Bit(t); } // Parse curve elements const tinyxml2::XMLElement* curveElement = chartElement->FirstChildElement("curve"); while (nullptr != curveElement) { CurveData curveData; if (const char* curveName = curveElement->Attribute("name")) { curveData.name = QString::fromLocal8Bit(curveName); } if (const char* color = curveElement->Attribute("color")) { curveData.color = QString::fromLocal8Bit(color); } if (const char* start = curveElement->Attribute("start")) { curveData.start = QString::fromLocal8Bit(start).toDouble(); } if (const char* stop = curveElement->Attribute("stop")) { curveData.stop = QString::fromLocal8Bit(stop).toDouble(); } curveChart->curves.append(curveData); curveElement = curveElement->NextSiblingElement("curve"); } chartData = curveChart; } else if (typeName == FileEntryTypeToString(FileEntryType::Surface)) { auto surfaceChart = std::make_shared(); // Parse surface-specific attributes if (const char* chartName = chartElement->Attribute("name")) { surfaceChart->name = QString::fromLocal8Bit(chartName); } if (const char* chartName = chartElement->Attribute("Name")) { // Handle both cases surfaceChart->name = QString::fromLocal8Bit(chartName); } if (const char* path = chartElement->Attribute("path")) { surfaceChart->path = QString::fromLocal8Bit(path); } if (const char* xTitle = chartElement->Attribute("xTitle")) { surfaceChart->xTitle = QString::fromLocal8Bit(xTitle); } if (const char* yTitle = chartElement->Attribute("yTitle")) { surfaceChart->yTitle = QString::fromLocal8Bit(yTitle); } if (const char* zTitle = chartElement->Attribute("zTitle")) { surfaceChart->zTitle = QString::fromLocal8Bit(zTitle); } if (const char* xMin = chartElement->Attribute("xMin")) { surfaceChart->xMin = QString::fromLocal8Bit(xMin).toDouble(); } if (const char* xMax = chartElement->Attribute("xMax")) { surfaceChart->xMax = QString::fromLocal8Bit(xMax).toDouble(); } if (const char* yMin = chartElement->Attribute("yMin")) { surfaceChart->yMin = QString::fromLocal8Bit(yMin).toDouble(); } if (const char* yMax = chartElement->Attribute("yMax")) { surfaceChart->yMax = QString::fromLocal8Bit(yMax).toDouble(); } if (const char* zMin = chartElement->Attribute("zMin")) { surfaceChart->zMin = QString::fromLocal8Bit(zMin).toDouble(); } if (const char* zMax = chartElement->Attribute("zMax")) { surfaceChart->zMax = QString::fromLocal8Bit(zMax).toDouble(); } if (const char* xCount = chartElement->Attribute("xCount")) { surfaceChart->xCount = QString::fromLocal8Bit(xCount).toInt(); } if (const char* yCount = chartElement->Attribute("yCount")) { surfaceChart->yCount = QString::fromLocal8Bit(yCount).toInt(); } if (const char* zCount = chartElement->Attribute("zCount")) { surfaceChart->zCount = QString::fromLocal8Bit(zCount).toInt(); } if (const char* t = chartElement->Attribute("t")) { surfaceChart->t = QString::fromLocal8Bit(t); } // Parse curve elements const tinyxml2::XMLElement* curveElement = chartElement->FirstChildElement("curve"); while (nullptr != curveElement) { SurfaceCurveData curveData; if (const char* curveName = curveElement->Attribute("name")) { curveData.name = QString::fromLocal8Bit(curveName); } if (const char* curveName = curveElement->Attribute("Name")) { // Handle both cases curveData.name = QString::fromLocal8Bit(curveName); } if (const char* color = curveElement->Attribute("color")) { curveData.color = QString::fromLocal8Bit(color); } if (const char* color = curveElement->Attribute("Color")) { // Handle both cases curveData.color = QString::fromLocal8Bit(color); } if (const char* start = curveElement->Attribute("start")) { curveData.start = QString::fromLocal8Bit(start).toDouble(); } if (const char* start = curveElement->Attribute("Start")) { // Handle both cases curveData.start = QString::fromLocal8Bit(start).toDouble(); } if (const char* stop = curveElement->Attribute("stop")) { curveData.stop = QString::fromLocal8Bit(stop).toDouble(); } if (const char* stop = curveElement->Attribute("Stop")) { // Handle both cases curveData.stop = QString::fromLocal8Bit(stop).toDouble(); } if (const char* x = curveElement->Attribute("x")) { curveData.x = QString::fromLocal8Bit(x).toDouble(); } if (const char* y = curveElement->Attribute("y")) { curveData.y = QString::fromLocal8Bit(y).toDouble(); } if (const char* z = curveElement->Attribute("z")) { curveData.z = QString::fromLocal8Bit(z).toDouble(); } surfaceChart->curves.append(curveData); curveElement = curveElement->NextSiblingElement("curve"); } chartData = surfaceChart; } else if (typeName == FileEntryTypeToString(FileEntryType::Table)) { auto tableChart = std::make_shared(); // Parse table-specific attributes if (const char* chartName = chartElement->Attribute("name")) { tableChart->name = QString::fromLocal8Bit(chartName); } if (const char* chartName = chartElement->Attribute("Name")) { // Handle both cases tableChart->name = QString::fromLocal8Bit(chartName); } if (const char* path = chartElement->Attribute("path")) { tableChart->path = QString::fromLocal8Bit(path); } if (const char* head = chartElement->Attribute("head")) { tableChart->head = QString::fromLocal8Bit(head); } if (const char* t = chartElement->Attribute("t")) { tableChart->t = QString::fromLocal8Bit(t); } // Parse curve elements const tinyxml2::XMLElement* curveElement = chartElement->FirstChildElement("curve"); while (nullptr != curveElement) { TableCurveData curveData; if (const char* curveName = curveElement->Attribute("name")) { curveData.name = QString::fromLocal8Bit(curveName); } if (const char* curveName = curveElement->Attribute("Name")) { // Handle both cases curveData.name = QString::fromLocal8Bit(curveName); } if (const char* color = curveElement->Attribute("color")) { curveData.color = QString::fromLocal8Bit(color); } if (const char* data = curveElement->Attribute("data")) { curveData.data = QString::fromLocal8Bit(data); } tableChart->curves.append(curveData); curveElement = curveElement->NextSiblingElement("curve"); } chartData = tableChart; } else if (typeName == FileEntryTypeToString(FileEntryType::Light)) { auto lightChart = std::make_shared(); // Parse light-specific attributes if (const char* chartName = chartElement->Attribute("name")) { lightChart->name = QString::fromLocal8Bit(chartName); } if (const char* path = chartElement->Attribute("path")) { lightChart->path = QString::fromLocal8Bit(path); } if (const char* openColor = chartElement->Attribute("openColor")) { lightChart->openColor = QString::fromLocal8Bit(openColor); } if (const char* closeColor = chartElement->Attribute("closeColor")) { lightChart->closeColor = QString::fromLocal8Bit(closeColor); } if (const char* t = chartElement->Attribute("t")) { lightChart->t = QString::fromLocal8Bit(t); } // Parse curve elements const tinyxml2::XMLElement* curveElement = chartElement->FirstChildElement("curve"); while (nullptr != curveElement) { LightCurveData curveData; if (const char* curveName = curveElement->Attribute("name")) { curveData.name = QString::fromLocal8Bit(curveName); } if (const char* data = curveElement->Attribute("data")) { curveData.data = QString::fromLocal8Bit(data); } lightChart->curves.append(curveData); curveElement = curveElement->NextSiblingElement("curve"); } chartData = lightChart; } if (chartData) { fileTypeData.charts.append(chartData); } chartElement = chartElement->NextSiblingElement("chart"); } fileTypes.append(fileTypeData); // Also create file entries for backward compatibility FileEntryType enumType; if (FileEntryTypeFromString(name, enumType)) { for (int i = 0; i < count; ++i) { workSpace_->CreateFileEntry(enumType); } } } typeElement = typeElement->NextSiblingElement("type"); } // Store the parsed chart data workSpace_->SetFileTypeData(fileTypes); return true; } bool WorkSpaceXMLParse::ParseEntities(const tinyxml2::XMLElement* element) { if (nullptr == element) { LOG_WARN("element is nullptr"); return false; } const char* eleName = element->Name(); if (0 == strcmp(eleName, "entities")) { return EntitiesManager::Get().Parse(element, workSpace_); } LOG_WARN("element not has entities"); return false; } bool WorkSpaceXMLParse::ParseChart(const tinyxml2::XMLElement* element) { if (nullptr == element) { LOG_WARN("element is nullptr"); return false; } const tinyxml2::XMLElement* xmlElement = element->FirstChildElement(); while (nullptr != xmlElement) { const char* name = xmlElement->Name(); if (0 == strcmp(name, "Wave")) { QVariantMap varChart; const tinyxml2::XMLAttribute* attribute = xmlElement->FirstAttribute(); while (nullptr != attribute) { workSpace_->SetWavePath(QString::fromLocal8Bit(attribute->Value())); attribute = attribute->Next(); } } else if (0 == strcmp(name, "Report")) { QVariantMap varChart; const tinyxml2::XMLAttribute* attribute = xmlElement->FirstAttribute(); while (nullptr != attribute) { workSpace_->SetReportPath(QString::fromLocal8Bit(attribute->Value())); attribute = attribute->Next(); } } else if (0 == strcmp(name, "RD")) { QVariantMap varChart; const tinyxml2::XMLAttribute* attribute = xmlElement->FirstAttribute(); while (nullptr != attribute) { workSpace_->SetRDPath(QString::fromLocal8Bit(attribute->Value())); attribute = attribute->Next(); } } else if (0 == strcmp(name, "SimMatlab")) { QVariantMap varChart; const tinyxml2::XMLAttribute* attribute = xmlElement->FirstAttribute(); while (nullptr != attribute) { workSpace_->SetSimMatlab(QString::fromLocal8Bit(attribute->Value())); attribute = attribute->Next(); } } xmlElement = xmlElement->NextSiblingElement(); } return true; } bool WorkSpaceXMLParse::ParseSimMatlab(const tinyxml2::XMLElement* element) { if (nullptr == element) { LOG_WARN("element is nullptr"); return false; } const tinyxml2::XMLElement* xmlElement = element->FirstChildElement(); while (nullptr != xmlElement) { const char* name = xmlElement->Name(); if (0 == strcmp(name, "SimMatlab")) { QVariantMap varChart; const tinyxml2::XMLAttribute* attribute = xmlElement->FirstAttribute(); while (nullptr != attribute) { WorkSpaceManager::Get().GetCurrent()->SetSimMatlab(QString::fromLocal8Bit(attribute->Value())); attribute = attribute->Next(); } } xmlElement = xmlElement->NextSiblingElement(); } return true; } bool WorkSpaceXMLParse::ParseReport(const tinyxml2::XMLElement* element) { QString strFile = ""; int iBatch = 0; const tinyxml2::XMLAttribute* attribute = element->FirstAttribute(); while (nullptr != attribute) { if (0 == strcmp(attribute->Name(), "file")) { strFile = QString::fromLocal8Bit(attribute->Value()); } else if (0 == strcmp(attribute->Name(), "batch")) { iBatch = atoi(attribute->Value()); } attribute = attribute->Next(); } return true; } bool WorkSpaceXMLParse::Load(const QString& dyt) { std::string path = dyt.toLocal8Bit().constData(); LOG_INFO("load path:{}", path); tinyxml2::XMLDocument xmlDocument; tinyxml2::XMLError error = xmlDocument.LoadFile(path.c_str()); if (tinyxml2::XMLError::XML_SUCCESS != error) { LOG_INFO("load feiled:{}", static_cast(error)); return false; } const tinyxml2::XMLElement* root = xmlDocument.RootElement(); if (!ParseScene(root)) { return false; } const tinyxml2::XMLElement* xmlElement = root->FirstChildElement(); while (nullptr != xmlElement) { const char* name = xmlElement->Name(); if (0 == strcmp(name, "entities")) { ParseEntities(xmlElement); } else if (0 == strcmp(name, "timestep")) { ParseTimestep(xmlElement); } else if (0 == strcmp(name, "lamp")) { ParseLamp(xmlElement); } else if (0 == strcmp(name, "commond")) { ParseCommond(xmlElement); } else if (0 == strcmp(name, "charts")) { ParseChart(xmlElement); } else if (0 == strcmp(name, "ReportInfo")) { ParseReport(xmlElement); } else if (0 == strcmp(name, "SimMatlab")) { ParseSimMatlab(xmlElement); } else if (0 == strcmp(name, "files")) { ParseFiles(xmlElement); } xmlElement = xmlElement->NextSiblingElement(); } return true; }