|
|
|
|
|
#ifndef NMDFNTEXTFORMAT_H
|
|
|
|
|
|
#define NMDFNTEXTFORMAT_H
|
|
|
|
|
|
|
|
|
|
|
|
#include "nmDfnData.h"
|
|
|
|
|
|
#include "nmDfnUnits.h"
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
|
#include <QAtomicInt>
|
|
|
|
|
|
#include <QMap>
|
|
|
|
|
|
#include <QList>
|
|
|
|
|
|
#include <QStringList>
|
|
|
|
|
|
|
|
|
|
|
|
// DFN 文件格式与字段解释;只收发字符串和值记录,不访问工程数据。
|
|
|
|
|
|
namespace nmDfnText
|
|
|
|
|
|
{
|
|
|
|
|
|
// 错误由导入窗口呈现,解析器不创建弹窗。
|
|
|
|
|
|
inline QString text(const char* pText) { return QCoreApplication::translate("nmDfnText", pText); }
|
|
|
|
|
|
// 识别稳定字段名;旧 Wf/o 没有实际模型字段,按未使用列处理。
|
|
|
|
|
|
inline QString field(const QString& sName)
|
|
|
|
|
|
{
|
|
|
|
|
|
const QString sField = sName.trimmed();
|
|
|
|
|
|
const QStringList listFields = QStringList() << "Name" << "X1" << "Y1" << "X2" << "Y2" << "Fc" << "Dw" << "Model" << "Visible";
|
|
|
|
|
|
for (int i = 0; i < listFields.size(); ++i)
|
|
|
|
|
|
if (sField.compare(listFields[i], Qt::CaseInsensitive) == 0) return listFields[i];
|
|
|
|
|
|
if (sField.compare("dFc", Qt::CaseInsensitive) == 0) return "Fc";
|
|
|
|
|
|
if (sField.compare("dW", Qt::CaseInsensitive) == 0) return "Dw";
|
|
|
|
|
|
return "Ignore";
|
|
|
|
|
|
}
|
|
|
|
|
|
// 每个字段的基准单位;名称、枚举和无量纲字段无需单位选择。
|
|
|
|
|
|
inline QString baseUnit(const QString& sField)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (sField == "X1" || sField == "Y1" || sField == "X2" || sField == "Y2") return "m";
|
|
|
|
|
|
return sField == "Fc" ? QString("mD.m") : QString();
|
|
|
|
|
|
}
|
|
|
|
|
|
// 解析文本,支持引号、双引号转义和引号内换行,显式分隔符之间的空值不会错列。
|
|
|
|
|
|
inline bool parse(const QString& sText, bool bSpace, bool bTab, const QString& sSeparators,
|
|
|
|
|
|
QList<QStringList>& listRows, QString& sError, QAtomicInt* pCanceled = NULL)
|
|
|
|
|
|
{
|
|
|
|
|
|
listRows.clear();
|
|
|
|
|
|
sError.clear();
|
|
|
|
|
|
QString sCell;
|
|
|
|
|
|
QStringList listRow;
|
|
|
|
|
|
bool bQuoted = false, bAfterQuote = false;
|
|
|
|
|
|
QString sInput = sText;
|
|
|
|
|
|
if (!sInput.isEmpty() && sInput[0] == QChar(0xfeff)) sInput.remove(0, 1);
|
|
|
|
|
|
// 有明确分隔符时空格是字段内文字,保留含空格的名称和模型标识。
|
|
|
|
|
|
bool bExplicit = false, bInsideQuotes = false;
|
|
|
|
|
|
for (int i = 0; i < sInput.size(); ++i) {
|
|
|
|
|
|
if ((i & 4095) == 0 && pCanceled && pCanceled->fetchAndAddOrdered(0)) return false;
|
|
|
|
|
|
if (sInput[i] == '"') {
|
|
|
|
|
|
if (bInsideQuotes && i + 1 < sInput.size() && sInput[i + 1] == '"') ++i;
|
|
|
|
|
|
else bInsideQuotes = !bInsideQuotes;
|
|
|
|
|
|
} else if (!bInsideQuotes && ((bTab && sInput[i] == '\t') || sSeparators.contains(sInput[i]))) {
|
|
|
|
|
|
bExplicit = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
for (int i = 0; i <= sInput.size(); ++i) {
|
|
|
|
|
|
if ((i & 4095) == 0 && pCanceled && pCanceled->fetchAndAddOrdered(0)) return false;
|
|
|
|
|
|
const bool bEnd = i == sInput.size();
|
|
|
|
|
|
const QChar c = bEnd ? QChar('\n') : sInput[i];
|
|
|
|
|
|
if (bQuoted) {
|
|
|
|
|
|
if (bEnd) { sError = text(QT_TRANSLATE_NOOP("nmDfnText", "Unclosed quoted field.")); return false; }
|
|
|
|
|
|
if (c == '"') {
|
|
|
|
|
|
if (i + 1 < sInput.size() && sInput[i + 1] == '"') { sCell += '"'; ++i; }
|
|
|
|
|
|
else { bQuoted = false; bAfterQuote = true; }
|
|
|
|
|
|
} else sCell += c;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
const bool bLineEnd = c == '\n' || c == '\r';
|
|
|
|
|
|
const bool bExplicitDelimiter = (bTab && c == '\t') || sSeparators.contains(c);
|
|
|
|
|
|
const bool bSpaceDelimiter = bSpace && !bExplicit && c == ' ';
|
|
|
|
|
|
if (bLineEnd || bExplicitDelimiter || bSpaceDelimiter) {
|
|
|
|
|
|
if (bSpaceDelimiter && sCell.isEmpty() && !bAfterQuote) continue;
|
|
|
|
|
|
listRow << sCell.trimmed();
|
|
|
|
|
|
sCell.clear();
|
|
|
|
|
|
bAfterQuote = false;
|
|
|
|
|
|
if (bLineEnd) {
|
|
|
|
|
|
bool bHasValue = false;
|
|
|
|
|
|
for (int j = 0; j < listRow.size(); ++j) bHasValue = bHasValue || !listRow[j].isEmpty();
|
|
|
|
|
|
// 保留中间空行的位置,确保预览行号和错误行号不因空行被压缩。
|
|
|
|
|
|
if (bHasValue || !bEnd) listRows << listRow;
|
|
|
|
|
|
listRow.clear();
|
|
|
|
|
|
if (c == '\r' && i + 1 < sInput.size() && sInput[i + 1] == '\n') ++i;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (c == '"' && sCell.trimmed().isEmpty() && !bAfterQuote) {
|
|
|
|
|
|
sCell.clear(); bQuoted = true;
|
|
|
|
|
|
} else if (bAfterQuote) {
|
|
|
|
|
|
if (!c.isSpace()) { sError = text(QT_TRANSLATE_NOOP("nmDfnText", "Unexpected characters after a quoted field.")); return false; }
|
|
|
|
|
|
} else sCell += c;
|
|
|
|
|
|
}
|
|
|
|
|
|
while (!listRows.isEmpty() && listRows.last().join("").trimmed().isEmpty()) listRows.removeLast();
|
|
|
|
|
|
sError.clear();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 全量验证映射和记录,成功后才替换结果;必需坐标每种只能映射一次。
|
|
|
|
|
|
inline bool records(const QList<QStringList>& listRows, const QStringList& listFields,
|
|
|
|
|
|
const QStringList& listUnits, QVector<nmDfnRecord>& vecResult, QString& sError, int nFirstRow = 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
QMap<QString, int> mapColumns;
|
|
|
|
|
|
for (int i = 0; i < listFields.size(); ++i) {
|
|
|
|
|
|
const QString sField = listFields[i];
|
|
|
|
|
|
if (sField == "Ignore") continue;
|
|
|
|
|
|
if (mapColumns.contains(sField)) { sError = text(QT_TRANSLATE_NOOP("nmDfnText", "Field %1 is assigned more than once.")).arg(sField); return false; }
|
|
|
|
|
|
mapColumns.insert(sField, i);
|
|
|
|
|
|
}
|
|
|
|
|
|
const QStringList listRequired = QStringList() << "X1" << "Y1" << "X2" << "Y2";
|
|
|
|
|
|
for (int i = 0; i < listRequired.size(); ++i)
|
|
|
|
|
|
if (!mapColumns.contains(listRequired[i])) { sError = text(QT_TRANSLATE_NOOP("nmDfnText", "Assign X1, Y1, X2 and Y2 before importing.")); return false; }
|
|
|
|
|
|
if (nFirstRow < 0 || nFirstRow >= listRows.size()) { sError = text(QT_TRANSLATE_NOOP("nmDfnText", "No data rows found.")); return false; }
|
|
|
|
|
|
// 每个数字列只查一次单位组,避免大文件中逐单元格重复解析单位名称。
|
|
|
|
|
|
QVector<nmDfnUnitConversion> vecConversions(listFields.size());
|
|
|
|
|
|
for (int nColumn = 0; nColumn < listFields.size(); ++nColumn) {
|
|
|
|
|
|
const QString sBase = baseUnit(listFields[nColumn]);
|
|
|
|
|
|
if (!sBase.isEmpty() && !vecConversions[nColumn].configure(listUnits.value(nColumn), sBase)) {
|
|
|
|
|
|
sError = text(QT_TRANSLATE_NOOP("nmDfnText", "Unsupported unit at column %1.")).arg(nColumn + 1);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
QVector<nmDfnRecord> vecRecords;
|
|
|
|
|
|
vecRecords.reserve(listRows.size() - nFirstRow);
|
|
|
|
|
|
for (int nRow = nFirstRow; nRow < listRows.size(); ++nRow) {
|
|
|
|
|
|
bool bEmpty = true;
|
|
|
|
|
|
for (int i = 0; i < listRows[nRow].size(); ++i)
|
|
|
|
|
|
if (!listRows[nRow][i].trimmed().isEmpty()) { bEmpty = false; break; }
|
|
|
|
|
|
if (bEmpty) continue;
|
|
|
|
|
|
nmDfnRecord oRecord;
|
|
|
|
|
|
oRecord.m_sName = QString("DFN_%1").arg(vecRecords.size() + 1);
|
|
|
|
|
|
// 兼容旧导入行为:未提供 Fc 列时沿用 0.1,不改变生成器的默认物性。
|
|
|
|
|
|
oRecord.m_dConductivity = 0.1;
|
|
|
|
|
|
double coordinates[4] = {0.0, 0.0, 0.0, 0.0};
|
|
|
|
|
|
for (int nColumn = 0; nColumn < listFields.size(); ++nColumn) {
|
|
|
|
|
|
const QString sField = listFields[nColumn];
|
|
|
|
|
|
if (sField == "Ignore") continue;
|
|
|
|
|
|
const QString sValue = listRows[nRow].value(nColumn).trimmed();
|
|
|
|
|
|
if (sField == "Name") { if (!sValue.isEmpty()) oRecord.m_sName = sValue; continue; }
|
|
|
|
|
|
if (sField == "Model") {
|
|
|
|
|
|
if (sValue.compare("Finite", Qt::CaseInsensitive) == 0 || sValue.compare("Finite Conductivity", Qt::CaseInsensitive) == 0)
|
|
|
|
|
|
oRecord.m_bFiniteConductivity = true;
|
|
|
|
|
|
else if (sValue.compare("Infinite", Qt::CaseInsensitive) == 0 || sValue.compare("Infinite Conductivity", Qt::CaseInsensitive) == 0)
|
|
|
|
|
|
oRecord.m_bFiniteConductivity = false;
|
|
|
|
|
|
else { sError = text(QT_TRANSLATE_NOOP("nmDfnText", "Invalid flow model at row %1.")).arg(nRow + 1); return false; }
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
bool bOk = false;
|
|
|
|
|
|
double dValue = sValue.toDouble(&bOk);
|
|
|
|
|
|
if (!bOk || !qIsFinite(dValue)) { sError = text(QT_TRANSLATE_NOOP("nmDfnText", "Invalid number at row %1, column %2.")).arg(nRow + 1).arg(nColumn + 1); return false; }
|
|
|
|
|
|
const QString sBase = baseUnit(sField);
|
|
|
|
|
|
if (!sBase.isEmpty() && !vecConversions[nColumn].convert(dValue, dValue)) {
|
|
|
|
|
|
sError = text(QT_TRANSLATE_NOOP("nmDfnText", "Unsupported unit at column %1.")).arg(nColumn + 1); return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
const int nCoordinate = listRequired.indexOf(sField);
|
|
|
|
|
|
if (nCoordinate >= 0) coordinates[nCoordinate] = dValue;
|
|
|
|
|
|
else if (sField == "Fc") oRecord.m_dConductivity = dValue;
|
|
|
|
|
|
else if (sField == "Dw") oRecord.m_dStorageRatio = dValue;
|
|
|
|
|
|
else if (sField == "Visible") {
|
|
|
|
|
|
if (dValue != 0.0 && dValue != 1.0) { sError = text(QT_TRANSLATE_NOOP("nmDfnText", "Visibility must be 0 or 1 at row %1.")).arg(nRow + 1); return false; }
|
|
|
|
|
|
oRecord.m_bVisible = dValue == 1.0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
oRecord.m_oLine = QLineF(coordinates[0], coordinates[1], coordinates[2], coordinates[3]);
|
|
|
|
|
|
if (!oRecord.isValid()) { sError = text(QT_TRANSLATE_NOOP("nmDfnText", "Invalid geometry or physical parameters at row %1.")).arg(nRow + 1); return false; }
|
|
|
|
|
|
vecRecords.append(oRecord);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (vecRecords.isEmpty()) { sError = text(QT_TRANSLATE_NOOP("nmDfnText", "No data rows found.")); return false; }
|
|
|
|
|
|
vecResult = vecRecords;
|
|
|
|
|
|
sError.clear();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
// CSV 字段转义,名称中的逗号、引号和换行可完整往返。
|
|
|
|
|
|
inline QString quote(QString sValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
sValue.replace("\"", "\"\"");
|
|
|
|
|
|
return "\"" + sValue + "\"";
|
|
|
|
|
|
}
|
|
|
|
|
|
// 完整精度输出十七位有效数字;简洁输出只影响文件文本,不修改工作副本。
|
|
|
|
|
|
inline QString serialize(const QVector<nmDfnRecord>& vecRecords, bool bFullPrecision)
|
|
|
|
|
|
{
|
|
|
|
|
|
QStringList listLines;
|
|
|
|
|
|
listLines << "Name,X1,Y1,X2,Y2,Fc,Dw,Model,Visible" << ",m,m,m,m,mD.m,,,";
|
|
|
|
|
|
for (int i = 0; i < vecRecords.size(); ++i) {
|
|
|
|
|
|
const nmDfnRecord& oRecord = vecRecords[i];
|
|
|
|
|
|
QStringList row;
|
|
|
|
|
|
row << quote(oRecord.m_sName);
|
|
|
|
|
|
const double values[] = {oRecord.m_oLine.x1(), oRecord.m_oLine.y1(), oRecord.m_oLine.x2(),
|
|
|
|
|
|
oRecord.m_oLine.y2(), oRecord.m_dConductivity, oRecord.m_dStorageRatio};
|
|
|
|
|
|
for (int j = 0; j < 6; ++j) row << nmDfnUi::number(values[j], bFullPrecision);
|
|
|
|
|
|
row << (oRecord.m_bFiniteConductivity ? "Finite" : "Infinite") << (oRecord.m_bVisible ? "1" : "0");
|
|
|
|
|
|
listLines << row.join(",");
|
|
|
|
|
|
}
|
|
|
|
|
|
return listLines.join("\r\n") + "\r\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif // NMDFNTEXTFORMAT_H
|