diff --git a/src/libs/vformat/vpatternrecipe.cpp b/src/libs/vformat/vpatternrecipe.cpp index 57f86b31d..6a2756448 100644 --- a/src/libs/vformat/vpatternrecipe.cpp +++ b/src/libs/vformat/vpatternrecipe.cpp @@ -37,6 +37,7 @@ #include "../vgeometry/vsplinepath.h" #include "../vgeometry/vcubicbezierpath.h" #include "../vtools/tools/drawTools/drawtools.h" +#include "../vpatterndb/calculator.h" namespace { @@ -88,7 +89,7 @@ VPatternRecipe::VPatternRecipe(VContainer *data, VAbstractPattern *pattern, QObj QDomElement recipeElement = createElement(QStringLiteral("recipe")); recipeElement.appendChild(createComment(FileComment())); - SetAttribute(recipeElement, QStringLiteral("version"), QStringLiteral("1.0.0")); + SetAttribute(recipeElement, QStringLiteral("version"), QStringLiteral("1.1.0")); recipeElement.appendChild(Prerequisite()); recipeElement.appendChild(Content()); @@ -252,6 +253,8 @@ QDomElement VPatternRecipe::Content() content.appendChild(Draft(draw)); } + content.appendChild(FinalMeasurements()); + return content; } @@ -402,6 +405,46 @@ QT_WARNING_POP throw VExceptionInvalidHistory(tr("Can't create history record for the tool.")); } +//--------------------------------------------------------------------------------------------------------------------- +QDomElement VPatternRecipe::FinalMeasurements() +{ + QDomElement recipeFinalMeasurements = createElement(QStringLiteral("finalMeasurements")); + + const QVector measurements = m_pattern->GetFinalMeasurements(); + + for (auto &m : measurements) + { + recipeFinalMeasurements.appendChild(FinalMeasurement(m)); + } + + return recipeFinalMeasurements; +} + +//--------------------------------------------------------------------------------------------------------------------- +QDomElement VPatternRecipe::FinalMeasurement(const VFinalMeasurement &fm) +{ + QDomElement recipeFinalMeasurement = createElement(QStringLiteral("finalMeasurement")); + + SetAttribute(recipeFinalMeasurement, QStringLiteral("description"), fm.description); + SetAttribute(recipeFinalMeasurement, QStringLiteral("name"), fm.name); + SetAttribute(recipeFinalMeasurement, QStringLiteral("formula"), fm.formula); // TODO: localize + + QScopedPointer cal(new Calculator()); + const qreal result = cal->EvalFormula(m_data->DataVariables(), fm.formula); + if (qIsInf(result) || qIsNaN(result)) + { + const QString errorMsg = QString("%1\n\n%1").arg(tr("Reading final measurements error."), + tr("Value for final measurtement '%1' is infinite or NaN. " + "Please, check your calculations.").arg(fm.name)); + qApp->IsPedantic() ? throw VException(errorMsg) : + qWarning() << VAbstractApplication::patternMessageSignature + errorMsg; + } + + SetAttribute(recipeFinalMeasurement, QStringLiteral("value"), result); + + return recipeFinalMeasurement; +} + //--------------------------------------------------------------------------------------------------------------------- QDomElement VPatternRecipe::BasePoint(const VToolRecord &record) { diff --git a/src/libs/vformat/vpatternrecipe.h b/src/libs/vformat/vpatternrecipe.h index 3beb05267..6c259689e 100644 --- a/src/libs/vformat/vpatternrecipe.h +++ b/src/libs/vformat/vpatternrecipe.h @@ -37,6 +37,7 @@ class VIncrement; class VToolRecord; class VFormula; class VAbstractOperation; +class VFinalMeasurement; class VPatternRecipe : public VDomDocument { @@ -65,6 +66,9 @@ private: QDomElement Draft(const QDomElement &draft); QDomElement Step(const VToolRecord &tool); + QDomElement FinalMeasurements(); + QDomElement FinalMeasurement(const VFinalMeasurement &fm); + QDomElement BasePoint(const VToolRecord &record); QDomElement EndLine(const VToolRecord &record); QDomElement Line(const VToolRecord &record);