#include "SurfaceInterpreter.h" #include "Constants.h" #include "DataManager.h" #include #include #include // For mathematical operations like std::abs and std::sqrt #include // For std::numeric_limits #include #include "DataManagerGlobal.h" #include "Geometry_M.h" #include "Common/tangible_string_helper.h" SurfaceInterpreter::SurfaceInterpreter() { } void SurfaceInterpreter::ExecuteCommand(std::vector &commandLines, Surface_M &surface) { std::string firstWord = StringHelper::toUpper(StringHelper::FirstWord(commandLines[0])); if (firstWord == "SURF") surface.m_Type = 0; else if (firstWord == "LOFT") surface.m_Type = 1; else if (firstWord == "SWEEPSURF") surface.m_Type = 2; else if (firstWord == "ROTATIONSURF") surface.m_Type = 3; else if (firstWord == "PIPE") surface.m_Type = 4; else if (firstWord == "SPHERESURF") surface.m_Type = 5; std::string name; std::string cmd = commandLines[0]; if (!ParseName(surface, name, cmd)) throw std::runtime_error("命令执行失败\n Error: 重复命名,不能覆盖原名称对象.\n 位置:行 " + commandLines[0] + "\n 后续命令未执行"); // 解决命名重复的问题 surface.m_Name = QString::fromStdString(name); LineDone(commandLines); surface.m_Src.resize(ModelData::ARRAY_SIZE_NORMAL); switch (surface.m_Type) { case 0: case 1: { if (commandLines.empty()) throw std::runtime_error("MissingParameterException: Missing parameters."); std::vector lineParaThr = StringHelper::GetWords(commandLines[0]); if (StringHelper::toUpper(lineParaThr[0]) != "THR" || lineParaThr.size() < 2) throw std::runtime_error("ErrorParameterException: Invalid parameters."); if (surface.m_Type == 0) { std::vector lId; for (size_t i = 1; i < lineParaThr.size(); ++i) { int crvid = DataManager::GetObjectIDByName(lineParaThr[i]); if (crvid == -1) throw std::runtime_error("ObjectNotFoundException: " + lineParaThr[i]); if (std::find(lId.begin(), lId.end(), crvid) != lId.end()) throw std::runtime_error("ErrorSyntaxException: Duplicate line: " + lineParaThr[i] + "."); lId.push_back(crvid); } for (size_t k = 0; k < lId.size(); ++k) { surface.m_Src[k] = lId[k]; } LineDone(commandLines); if (!commandLines.empty()) { std::vector lineParaExp = StringHelper::GetWords(commandLines[0]); if (StringHelper::toUpper(lineParaExp[0]) != "EXP" || lineParaExp.size() < 2) throw std::runtime_error("ErrorParameterException: Invalid parameters."); std::vector neglId; for (size_t i = 1; i < lineParaExp.size(); ++i) { int crvid = DataManager::GetObjectIDByName(lineParaExp[i]); if (crvid == -1) throw std::runtime_error("ObjectNotFoundException: " + lineParaExp[i]); if (std::find(neglId.begin(), neglId.end(), crvid) != neglId.end()) throw std::runtime_error("ErrorSyntaxException: Duplicate line: " + lineParaExp[i] + "."); neglId.push_back(crvid); } for (size_t k = 0; k < neglId.size(); ++k) { surface.m_Src[255 - k] = neglId[k]; } LineDone(commandLines); } } else { for (size_t i = 1; i < lineParaThr.size(); ++i) { int crvid = DataManager::GetObjectIDByName(lineParaThr[i]); if (crvid == -1) throw std::runtime_error("ObjectNotFoundException: " + lineParaThr[i]); if (std::find(surface.m_Src.begin(), surface.m_Src.end(), crvid) != surface.m_Src.end()) throw std::runtime_error("ErrorSyntaxException: Duplicate line: " + lineParaThr[i] + "."); surface.m_Src[i - 1] = crvid; } if (surface.m_Type == 1 && lineParaThr.size() < 3) throw std::runtime_error("ErrorSyntaxException: Loft line count must be at least 2."); } break; } case 2: { if (commandLines.empty()) throw std::runtime_error("ErrorParameterException: Invalid parameters."); std::vector lineParabase = StringHelper::GetWords(commandLines[0]); if (StringHelper::toUpper(lineParabase[0]) != "BASE" || lineParabase.size() != 2) throw std::runtime_error("ErrorParameterException: Invalid parameters."); int baseId = DataManager::GetObjectIDByName(lineParabase[1]); if (baseId == -1) throw std::runtime_error("ObjectNotFoundException: " + lineParabase[1]); surface.m_Src[0] = baseId; LineDone(commandLines); if (commandLines.empty()) throw std::runtime_error("MissingParameterException: Missing parameters."); std::vector lineParaGen = StringHelper::GetWords(commandLines[0]); if (StringHelper::toUpper(lineParaGen[0]) != "GENERATE" || lineParaGen.size() != 2) throw std::runtime_error("ErrorParameterException: Invalid parameters."); int genId = DataManager::GetObjectIDByName(lineParaGen[1]); if (genId == -1) throw std::runtime_error("ObjectNotFoundException: " + lineParaGen[1]); surface.m_Src[1] = genId; LineDone(commandLines); break; } case 3: { if (commandLines.empty()) throw std::runtime_error("MissingParameterException: Missing parameters."); std::vector lineParaAXIS = StringHelper::GetWords(commandLines[0]); if (StringHelper::toUpper(lineParaAXIS[0]) != "AXIS" || lineParaAXIS.size() != 3) throw std::runtime_error("ErrorParameterException: Invalid parameters."); Point3D_M p1, p2; if (!ParsePointLoc(p1, lineParaAXIS[1])) throw std::runtime_error("ErrorParameterException: Invalid parameters."); surface.m_OX = p1.m_X; surface.m_OY = p1.m_Y; surface.m_OZ = p1.m_Z; if (!ParsePointLoc(p2, lineParaAXIS[2])) throw std::runtime_error("ErrorParameterException: Invalid parameters."); surface.m_PX = p2.m_X; surface.m_PY = p2.m_Y; surface.m_PZ = p2.m_Z; LineDone(commandLines); if (commandLines.empty()) throw std::runtime_error("MissingParameterException: Missing parameters."); std::vector lineParaBS = StringHelper::GetWords(commandLines[0]); if (StringHelper::toUpper(lineParaBS[0]) != "BASE" || lineParaBS.size() != 2) throw std::runtime_error("ErrorParameterException: Invalid parameters."); int bs = DataManager::GetObjectIDByName(lineParaBS[1]); if (bs == -1) throw std::runtime_error("ObjectNotFoundException: " + lineParaBS[1]); surface.m_Src[0] = bs; LineDone(commandLines); surface.m_AR = 360; if (!commandLines.empty()) { std::vector lineParaAGL = StringHelper::GetWords(commandLines[0]); if (StringHelper::toUpper(lineParaAGL[0]) == "ANGLE") { if (lineParaAGL.size() != 2) throw std::runtime_error("ErrorParameterException: Invalid parameters."); surface.m_AR = std::stod(lineParaAGL[1]); if (surface.m_AR <= 0 || surface.m_AR > 360) throw std::runtime_error("旋转角度(ANGLE)范围应为(0,360]."); LineDone(commandLines); } } break; } case 4: { if (commandLines.empty()) throw std::runtime_error("MissingParameterException: Missing parameters."); std::vector lineParaGNR = StringHelper::GetWords(commandLines[0]); if (StringHelper::toUpper(lineParaGNR[0]) != "GENERATE" || lineParaGNR.size() != 2) throw std::runtime_error("ErrorParameterException: Invalid parameters."); int gnr = DataManager::GetObjectIDByName(lineParaGNR[1]); if (gnr == -1) throw std::runtime_error("ObjectNotFoundException: " + lineParaGNR[1]); surface.m_Src[0] = gnr; LineDone(commandLines); if (commandLines.empty()) throw std::runtime_error("MissingParameterException: Missing parameters."); std::vector lineParaR = StringHelper::GetWords(commandLines[0]); if (StringHelper::toUpper(lineParaR[0]) != "R" || lineParaR.size() != 2) throw std::runtime_error("ErrorParameterException: Invalid parameters."); surface.m_AR = std::stod(lineParaR[1]); if (surface.m_AR <= 0) throw std::runtime_error("ErrorSyntaxException: 管道半径(R)应大于0."); LineDone(commandLines); break; } case 5: { if (commandLines.empty()) throw std::runtime_error("MissingParameterException: Missing parameters."); std::vector lineParaCENTER = StringHelper::GetWords(commandLines[0]); if (StringHelper::toUpper(lineParaCENTER[0]) != "CENTER" || lineParaCENTER.size() != 3) throw std::runtime_error("ErrorParameterException: Invalid parameters."); Point3D_M center; if (!ParsePointLoc(center, lineParaCENTER[1])) throw std::runtime_error("ErrorParameterException: Invalid parameters."); surface.m_OX = center.m_X; surface.m_OY = center.m_Y; surface.m_OZ = center.m_Z; if (!ParsePointLoc(center, lineParaCENTER[2])) throw std::runtime_error("ErrorParameterException: Invalid parameters."); surface.m_PX = center.m_X; surface.m_PY = center.m_Y; surface.m_PZ = center.m_Z; if (std::abs(surface.m_OX - surface.m_PX) + std::abs(surface.m_OY - surface.m_PY) + std::abs(surface.m_OZ - surface.m_PZ) == 0) throw std::runtime_error("圆心不能重合。"); LineDone(commandLines); if (commandLines.empty()) throw std::runtime_error("MissingParameterException: Missing parameters."); std::vector lineParaRO = StringHelper::GetWords(commandLines[0]); if (StringHelper::toUpper(lineParaRO[0]) != "RH" || lineParaRO.size() != 3) throw std::runtime_error("ErrorParameterException: Invalid parameters."); surface.m_AR = std::stod(lineParaRO[1]); surface.m_H = std::stod(lineParaRO[2]); if (surface.m_AR > 0) { if (lineParaCENTER.size() == 2) throw std::runtime_error("ErrorSyntaxException: 半径(R)应大于0."); } else if (surface.m_AR < 0) { throw std::runtime_error("ErrorSyntaxException: 半径要大于等于0。"); } if (surface.m_H <= 0) throw std::runtime_error("ErrorSyntaxException: 高度(H)应大于0."); LineDone(commandLines); break; } default: break; } Add2ModelOrInteractWithIdtf(&surface); }