|
|
|
|
|
#ifndef NMDFNSESSION_H
|
|
|
|
|
|
#define NMDFNSESSION_H
|
|
|
|
|
|
|
|
|
|
|
|
#include "nmDfnData.h"
|
|
|
|
|
|
#include "nmDataAnalyzeManager.h"
|
|
|
|
|
|
#include "nmDataFracture.h"
|
|
|
|
|
|
#include "nmDataOutline.h"
|
|
|
|
|
|
#include "nmGuiPlot.h"
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
|
#include <QByteArray>
|
|
|
|
|
|
#include <QPointer>
|
|
|
|
|
|
#include <QSet>
|
|
|
|
|
|
#include <rapidjson/stringbuffer.h>
|
|
|
|
|
|
#include <rapidjson/writer.h>
|
|
|
|
|
|
|
|
|
|
|
|
// DFN 编辑会话负责所属分析、快照及提交,四个业务窗口不直接增删实时裂缝。
|
|
|
|
|
|
class nmDfnSession
|
|
|
|
|
|
{
|
|
|
|
|
|
public:
|
|
|
|
|
|
// 借用入口地图及其绑定分析;QPointer 在分析或地图关闭后自动失效。
|
|
|
|
|
|
explicit nmDfnSession(nmGuiPlot* pPlot)
|
|
|
|
|
|
: m_pPlot(pPlot), m_pManager(pPlot ? pPlot->boundDataManager() : nullptr) {}
|
|
|
|
|
|
// 读取真实裂缝数据和边界,遇到旧数据不支持的几何时明确失败,避免截断保存。
|
|
|
|
|
|
bool initialize(QString& sError)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!available()) return fail(sError, QT_TRANSLATE_NOOP("nmDfnSession", "The analysis is no longer available."));
|
|
|
|
|
|
m_vecOriginal.clear();
|
|
|
|
|
|
m_vecRecords.clear();
|
|
|
|
|
|
const QVector<nmDataFracture*> vecData = m_pManager->getDFNFractureDataList();
|
|
|
|
|
|
for (int i = 0; i < vecData.size(); ++i) {
|
|
|
|
|
|
nmDataFracture* pData = vecData[i];
|
|
|
|
|
|
if (!pData || pData->getFracturePoints().size() != 2)
|
|
|
|
|
|
return fail(sError, QT_TRANSLATE_NOOP("nmDfnSession", "DFN contains unsupported geometry. The original data has been preserved."));
|
|
|
|
|
|
nmDfnRecord oRecord;
|
|
|
|
|
|
oRecord.m_sName = pData->getFractureName();
|
|
|
|
|
|
const QVector<QPointF> vecPoints = pData->getFracturePoints();
|
|
|
|
|
|
oRecord.m_oLine = QLineF(vecPoints[0], vecPoints[1]);
|
|
|
|
|
|
oRecord.m_dConductivity = pData->getFractureDfc().getValue().toDouble();
|
|
|
|
|
|
oRecord.m_dStorageRatio = pData->getFractureDw().getValue().toDouble();
|
|
|
|
|
|
const QString sModel = pData->getFractureFlowModel().getValue().toString();
|
|
|
|
|
|
const QString sInfinite = QCoreApplication::translate("nmTranslationManager", "Infinite Conductivity");
|
|
|
|
|
|
oRecord.m_bFiniteConductivity = sModel != "Infinite Conductivity" && sModel != sInfinite
|
|
|
|
|
|
&& sModel != QString::fromWCharArray(L"无限导流");
|
|
|
|
|
|
oRecord.m_bVisible = pData->getPlotVisible();
|
|
|
|
|
|
oRecord.m_nOriginalIndex = i;
|
|
|
|
|
|
m_vecOriginal.append(*pData);
|
|
|
|
|
|
m_vecRecords.append(oRecord);
|
|
|
|
|
|
}
|
|
|
|
|
|
m_vecInitialRecords = m_vecRecords;
|
|
|
|
|
|
nmDataOutline* pOutline = m_pManager->getOutlineData();
|
|
|
|
|
|
if (pOutline) {
|
|
|
|
|
|
m_oBoundary.m_bCircle = pOutline->getOutlineType() == NM_Round_Outline_Type;
|
|
|
|
|
|
m_oBoundary.m_oCenter = pOutline->getCenter();
|
|
|
|
|
|
m_oBoundary.m_dRadius = pOutline->getRadius();
|
|
|
|
|
|
m_oBoundary.m_oPolygon = QPolygonF(pOutline->getOutlinePoints());
|
|
|
|
|
|
}
|
|
|
|
|
|
sError.clear();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 返回可编辑的工作副本,导入、生成和删除仅改变此容器。
|
|
|
|
|
|
QVector<nmDfnRecord>& records() { return m_vecRecords; }
|
|
|
|
|
|
// 返回生成用边界副本,不把实时边界指针交给子窗口。
|
|
|
|
|
|
const nmDfnBoundary& boundary() const { return m_oBoundary; }
|
|
|
|
|
|
// 提交前核对上下文、外部变化和全部记录;无修改时不使网格或结果失效。
|
|
|
|
|
|
bool commit(QString& sError)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!available()) return fail(sError, QT_TRANSLATE_NOOP("nmDfnSession", "The analysis is no longer available."));
|
|
|
|
|
|
if (m_vecRecords == m_vecInitialRecords) { sError.clear(); return true; }
|
|
|
|
|
|
const QVector<nmDataFracture*> vecCurrent = m_pManager->getDFNFractureDataList();
|
|
|
|
|
|
if (vecCurrent.size() != m_vecOriginal.size())
|
|
|
|
|
|
return fail(sError, QT_TRANSLATE_NOOP("nmDfnSession", "DFN data changed outside this window. Reopen the window before saving."));
|
|
|
|
|
|
for (int i = 0; i < vecCurrent.size(); ++i)
|
|
|
|
|
|
if (!vecCurrent[i] || fingerprint(*vecCurrent[i]) != fingerprint(m_vecOriginal[i]))
|
|
|
|
|
|
return fail(sError, QT_TRANSLATE_NOOP("nmDfnSession", "DFN data changed outside this window. Reopen the window before saving."));
|
|
|
|
|
|
QVector<nmDataFracture> vecNext;
|
|
|
|
|
|
QSet<QString> setNames;
|
|
|
|
|
|
const QVector<nmDataFracture*> vecAll = m_pManager->getFractureDataList();
|
|
|
|
|
|
for (int i = 0; i < vecAll.size(); ++i)
|
|
|
|
|
|
if (vecAll[i] && vecAll[i]->getFractureType().getValue().toString() != "DFN") setNames.insert(vecAll[i]->getFractureName());
|
|
|
|
|
|
bool bGeometryChanged = m_vecRecords.size() != m_vecInitialRecords.size();
|
|
|
|
|
|
for (int i = 0; i < m_vecRecords.size(); ++i) {
|
|
|
|
|
|
const nmDfnRecord& oRecord = m_vecRecords[i];
|
|
|
|
|
|
if (!oRecord.isValid()) return fail(sError, QT_TRANSLATE_NOOP("nmDfnSession", "DFN contains invalid coordinates or physical parameters."));
|
|
|
|
|
|
if (!bGeometryChanged) {
|
|
|
|
|
|
const QLineF& a = oRecord.m_oLine;
|
|
|
|
|
|
const QLineF& b = m_vecInitialRecords[i].m_oLine;
|
|
|
|
|
|
bGeometryChanged = a.x1() != b.x1() || a.y1() != b.y1() || a.x2() != b.x2() || a.y2() != b.y2();
|
|
|
|
|
|
}
|
|
|
|
|
|
nmDataFracture oData;
|
|
|
|
|
|
// 删除部分旧记录时保留剩余对象的完整属性元数据,不能用显示文本重建。
|
|
|
|
|
|
if (oRecord.m_nOriginalIndex >= 0 && oRecord.m_nOriginalIndex < m_vecOriginal.size())
|
|
|
|
|
|
oData = m_vecOriginal[oRecord.m_nOriginalIndex];
|
|
|
|
|
|
QString sName = oRecord.m_sName.trimmed();
|
|
|
|
|
|
if (sName.isEmpty()) sName = QString("DFN_%1").arg(i + 1);
|
|
|
|
|
|
const QString sBaseName = sName;
|
|
|
|
|
|
for (int nSuffix = 2; setNames.contains(sName); ++nSuffix) sName = sBaseName + QString("_%1").arg(nSuffix);
|
|
|
|
|
|
setNames.insert(sName);
|
|
|
|
|
|
oData.setFractureName(sName);
|
|
|
|
|
|
oData.setFracturePoints(QVector<QPointF>() << oRecord.m_oLine.p1() << oRecord.m_oLine.p2());
|
|
|
|
|
|
nmDataAttribute oAttribute = oData.getFractureFlowModel();
|
|
|
|
|
|
oAttribute.setValue(QString::fromLatin1(oRecord.m_bFiniteConductivity ? "Finite Conductivity" : "Infinite Conductivity"));
|
|
|
|
|
|
oData.setFractureFlowModel(oAttribute);
|
|
|
|
|
|
oAttribute = oData.getFractureDfc(); oAttribute.setValue(oRecord.m_dConductivity); oData.setFractureDfc(oAttribute);
|
|
|
|
|
|
oAttribute = oData.getFractureDw(); oAttribute.setValue(oRecord.m_dStorageRatio); oData.setFractureDw(oAttribute);
|
|
|
|
|
|
oData.setPlotVisible(oRecord.m_bVisible);
|
|
|
|
|
|
vecNext.append(oData);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 解除旧图元绑定后再释放旧数据,避免 Binder 在重绘期间访问悬空裂缝指针。
|
|
|
|
|
|
m_pPlot->clearDFNPlotObjects();
|
|
|
|
|
|
const bool bSaved = m_pManager->replaceDFNFractureData(vecNext);
|
|
|
|
|
|
m_pPlot->initFractureObjsFromData(m_pManager, true);
|
|
|
|
|
|
if (!bSaved) return fail(sError, QT_TRANSLATE_NOOP("nmDfnSession", "Unable to save DFN data."));
|
|
|
|
|
|
if (bGeometryChanged) m_pManager->notifyDataChanged();
|
|
|
|
|
|
else m_pManager->invalidatePebiResults();
|
|
|
|
|
|
sError.clear();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
private:
|
|
|
|
|
|
// 既检查生命周期,也检查地图没有被重新绑定到另一个分析。
|
|
|
|
|
|
bool available() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_pManager && m_pPlot && m_pPlot->m_pPlot && m_pPlot->boundDataManager() == m_pManager;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 用现有序列化内容检查外部修改,覆盖名称、单位元数据和可见性。
|
|
|
|
|
|
static QByteArray fingerprint(const nmDataFracture& oData)
|
|
|
|
|
|
{
|
|
|
|
|
|
rapidjson::Document oDocument;
|
|
|
|
|
|
rapidjson::Value oValue = oData.ToJsonValue(oDocument.GetAllocator());
|
|
|
|
|
|
rapidjson::StringBuffer oBuffer;
|
|
|
|
|
|
rapidjson::Writer<rapidjson::StringBuffer> oWriter(oBuffer);
|
|
|
|
|
|
oValue.Accept(oWriter);
|
|
|
|
|
|
return QByteArray(oBuffer.GetString(), static_cast<int>(oBuffer.GetSize()));
|
|
|
|
|
|
}
|
|
|
|
|
|
// 业务错误由主窗口统一展示,数据层不依赖弹窗组件。
|
|
|
|
|
|
static bool fail(QString& sError, const char* pText)
|
|
|
|
|
|
{
|
|
|
|
|
|
sError = QCoreApplication::translate("nmDfnSession", pText);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
QPointer<nmGuiPlot> m_pPlot; // 所属地图弱引用。
|
|
|
|
|
|
QPointer<nmDataAnalyzeManager> m_pManager; // 所属分析弱引用。
|
|
|
|
|
|
QVector<nmDataFracture> m_vecOriginal; // 原始完整属性副本,用于冲突检查及属性保留。
|
|
|
|
|
|
QVector<nmDfnRecord> m_vecInitialRecords; // 打开窗口时的业务值,用于判断是否修改。
|
|
|
|
|
|
QVector<nmDfnRecord> m_vecRecords; // 工作副本,取消时直接丢弃。
|
|
|
|
|
|
nmDfnBoundary m_oBoundary; // 生成用边界快照。
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // NMDFNSESSION_H
|