From 2dd2d298e495339106f3d94e0d2297213a5de73a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me?= Date: Sat, 21 Nov 2020 18:05:59 +0100 Subject: [PATCH] Clean code --- src/BuildAPI/BuildAPI_Interpolation.cpp | 25 +++++++-------- src/BuildPlugin/BuildPlugin_Interpolation.cpp | 31 ++++++++----------- src/BuildPlugin/BuildPlugin_Interpolation.h | 5 ++- src/BuildPlugin/BuildPlugin_Plugin.cpp | 3 -- src/BuildPlugin/BuildPlugin_Validators.cpp | 2 +- src/BuildPlugin/Test/TestInterpolation.py | 6 ++-- src/BuildPlugin/interpolation_widget.xml | 1 - .../InitializationPlugin_EvalListener.cpp | 31 ++++++++++--------- src/ModelAPI/ModelAPI_Events.h | 12 +++---- 9 files changed, 55 insertions(+), 61 deletions(-) diff --git a/src/BuildAPI/BuildAPI_Interpolation.cpp b/src/BuildAPI/BuildAPI_Interpolation.cpp index 353eff358..f651255be 100644 --- a/src/BuildAPI/BuildAPI_Interpolation.cpp +++ b/src/BuildAPI/BuildAPI_Interpolation.cpp @@ -31,14 +31,14 @@ BuildAPI_Interpolation::BuildAPI_Interpolation(const std::shared_ptr& theBaseObjects, - const ModelHighAPI_Selection& theStartTangent, - const ModelHighAPI_Selection& theEndTangent, - const bool theIsClosed, - const bool theIsToReorder) + const std::list& theBaseObjects, + const ModelHighAPI_Selection& theStartTangent, + const ModelHighAPI_Selection& theEndTangent, + const bool theIsClosed, + const bool theIsToReorder) : ModelHighAPI_Interface(theFeature) { - if(initialize()) { + if (initialize()) { fillAttribute(BuildPlugin_Interpolation::CREATION_METHOD_BY_SELECTION_ID(),mycreationmethod); setUseTangents(true); setTangents(theStartTangent, theEndTangent); @@ -49,11 +49,10 @@ BuildAPI_Interpolation::BuildAPI_Interpolation(const FeaturePtr& theFeature, } //================================================================================================== -BuildAPI_Interpolation::BuildAPI_Interpolation( - const FeaturePtr& theFeature, - const std::list& theBaseObjects, - const bool theIsClosed, - const bool theIsToReorder) +BuildAPI_Interpolation::BuildAPI_Interpolation(const FeaturePtr& theFeature, + const std::list& theBaseObjects, + const bool theIsClosed, + const bool theIsToReorder) : ModelHighAPI_Interface(theFeature) { if (initialize()) { @@ -137,7 +136,7 @@ void BuildAPI_Interpolation::dump(ModelHighAPI_Dumper& theDumper) const FeaturePtr aBase = feature(); std::string aPartName = theDumper.name(aBase->document()); - if( aBase->string(BuildPlugin_Interpolation::CREATION_METHOD_ID())->value() == + if (aBase->string(BuildPlugin_Interpolation::CREATION_METHOD_ID())->value() == BuildPlugin_Interpolation::CREATION_METHOD_BY_SELECTION_ID() ) { AttributeSelectionListPtr anAttrBaseObjects = @@ -158,7 +157,7 @@ void BuildAPI_Interpolation::dump(ModelHighAPI_Dumper& theDumper) const } theDumper << closed() << ", " << reorder() << ")" << std::endl; - }else{ + } else { theDumper << aBase << " = model.addInterpolation(" << aPartName ; AttributeStringPtr XtAttr = xt(); diff --git a/src/BuildPlugin/BuildPlugin_Interpolation.cpp b/src/BuildPlugin/BuildPlugin_Interpolation.cpp index 59e91ca81..2397c8331 100644 --- a/src/BuildPlugin/BuildPlugin_Interpolation.cpp +++ b/src/BuildPlugin/BuildPlugin_Interpolation.cpp @@ -31,7 +31,6 @@ #include #include -#include #include #include @@ -46,13 +45,7 @@ #include #include #include -#include - -#include - #include - -#include #include @@ -98,7 +91,7 @@ void BuildPlugin_Interpolation::initAttributes() data()->reflist(ARGUMENTS_ID())->setIsArgument(false); ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ARGUMENTS_ID()); - if( string( XT_ID())->value() == "" + if ( string( XT_ID())->value() == "" && string( YT_ID())->value() == "" && string( ZT_ID())->value() == "") { @@ -112,9 +105,10 @@ void BuildPlugin_Interpolation::initAttributes() } } +//================================================================================================= void BuildPlugin_Interpolation::attributeChanged(const std::string& theID) { - if( (theID == XT_ID() + if ((theID == XT_ID() || theID == YT_ID() || theID == ZT_ID() || theID == MINT_ID() @@ -128,6 +122,7 @@ void BuildPlugin_Interpolation::attributeChanged(const std::string& theID) } } +//================================================================================================= void BuildPlugin_Interpolation::updateCoordinates() { std::wstring exp; @@ -135,7 +130,7 @@ void BuildPlugin_Interpolation::updateCoordinates() double aMaxt = real(MAXT_ID())->value(); int aNbrStep = integer(NUMSTEP_ID())->value(); - if ( aMaxt < aMint ) { + if (aMaxt < aMint) { setError("The minimum value of the parameter must be less than maximum value !!!" ); } @@ -143,7 +138,7 @@ void BuildPlugin_Interpolation::updateCoordinates() string(VARIABLE_ID())->setValue("t"); tables(VALUE_ID())->setSize(aNbrStep+1,4); - for( int step = 0; step <= aNbrStep; step++ ){ + for (int step = 0; step <= aNbrStep; step++ ){ ModelAPI_AttributeTables::Value aVal; aVal.myDouble = step * aScale + aMint; tables(VALUE_ID())->setValue(aVal,step,0); @@ -183,7 +178,7 @@ static GeomDirPtr selectionToDir(const AttributeSelectionPtr& theSelection) //================================================================================================= void BuildPlugin_Interpolation::execute() { - if( string(CREATION_METHOD_ID())->value() == CREATION_METHOD_BY_SELECTION_ID() ) + if (string(CREATION_METHOD_ID())->value() == CREATION_METHOD_BY_SELECTION_ID()) { // Get closed flag value bool isClosed = boolean(CLOSED_ID())->value(); @@ -244,9 +239,8 @@ void BuildPlugin_Interpolation::execute() setResult(aResultBody); - }else - { - if( string( XT_ID())->value() == "" + } else { + if (string( XT_ID())->value() == "" ||string( YT_ID())->value() == "" ||string( ZT_ID())->value() == "" ||tables(VALUE_ID())->rows()== 0 ) @@ -258,7 +252,7 @@ void BuildPlugin_Interpolation::execute() AttributeTablesPtr aTable = tables( VALUE_ID() ); std::list > aCoordPoints; - for( int step = 0; step < aTable->rows() ; step++ ){ + for (int step = 0; step < aTable->rows(); step++){ std::vector aCoordPoint; ModelAPI_AttributeTables::Value value; //x @@ -278,7 +272,7 @@ void BuildPlugin_Interpolation::execute() std::list aVertices; std::list >::const_iterator anItCoordPoints = aCoordPoints.begin(); - for( ; anItCoordPoints != aCoordPoints.end(); ++anItCoordPoints ){ + for (; anItCoordPoints != aCoordPoints.end(); ++anItCoordPoints){ GeomVertexPtr aVertex = GeomAlgoAPI_PointBuilder::vertex( (*anItCoordPoints)[0], @@ -309,7 +303,8 @@ void BuildPlugin_Interpolation::execute() } } -void BuildPlugin_Interpolation::evaluate(std::string& theError) +//================================================================================================= +void BuildPlugin_Interpolation::evaluate(std::string& theError) { FeaturePtr aMyPtr = std::dynamic_pointer_cast(data()->owner()); std::shared_ptr aProcessMessage = diff --git a/src/BuildPlugin/BuildPlugin_Interpolation.h b/src/BuildPlugin/BuildPlugin_Interpolation.h index 6697162bb..1eaa6116d 100644 --- a/src/BuildPlugin/BuildPlugin_Interpolation.h +++ b/src/BuildPlugin/BuildPlugin_Interpolation.h @@ -186,15 +186,18 @@ public: /// Creates a new part document if needed. BUILDPLUGIN_EXPORT virtual void execute(); + /// Called on change of any argument-attribute of this object. + /// \param[in] theID identifier of changed attribute. BUILDPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID); protected: /// Evaluates the expression x(t), y(t),z(t) in value table. void evaluate(std::string& theError); - //Update coordinates x,y,z + ///Update coordinates x,y,z void updateCoordinates(); + ///Output error for python interpreter std::string outErrorMessage; }; diff --git a/src/BuildPlugin/BuildPlugin_Plugin.cpp b/src/BuildPlugin/BuildPlugin_Plugin.cpp index 6614d0684..8cfc1132f 100644 --- a/src/BuildPlugin/BuildPlugin_Plugin.cpp +++ b/src/BuildPlugin/BuildPlugin_Plugin.cpp @@ -36,9 +36,6 @@ #include #include - - - // the only created instance of this plugin static BuildPlugin_Plugin* MY_INSTANCE = new BuildPlugin_Plugin(); diff --git a/src/BuildPlugin/BuildPlugin_Validators.cpp b/src/BuildPlugin/BuildPlugin_Validators.cpp index be26b1196..f0657537d 100644 --- a/src/BuildPlugin/BuildPlugin_Validators.cpp +++ b/src/BuildPlugin/BuildPlugin_Validators.cpp @@ -573,7 +573,7 @@ bool BuildPlugin_ValidatorBaseForVertex::isValid(const AttributePtr& theAttribut return true; } - +//================================================================================================= bool BuildPlugin_ValidatorExpressionInterpolation::isValid(const AttributePtr& theAttribute, const std::list& /*theArguments*/, Events_InfoMessage& theError) const diff --git a/src/BuildPlugin/Test/TestInterpolation.py b/src/BuildPlugin/Test/TestInterpolation.py index a06488579..d2460699f 100644 --- a/src/BuildPlugin/Test/TestInterpolation.py +++ b/src/BuildPlugin/Test/TestInterpolation.py @@ -178,9 +178,9 @@ Part_5 = model.addPart(partSet) Part_5_doc = Part_5.document() Interpolation_11 = model.addInterpolation(Part_5_doc, "sin(t)","cos(t)","t", 0, 100, 10) - -model.checkResult(Interpolation_11, model, 1, [0], [0], [0], [1], [2]) - +model.do() +model.testNbResults(Interpolation_11, 1) +model.end() # ============================================================================= # Test 13. Check Python dump # ============================================================================= diff --git a/src/BuildPlugin/interpolation_widget.xml b/src/BuildPlugin/interpolation_widget.xml index 21ccd2c84..b5036905f 100644 --- a/src/BuildPlugin/interpolation_widget.xml +++ b/src/BuildPlugin/interpolation_widget.xml @@ -36,7 +36,6 @@ diff --git a/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp b/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp index 4699cc1bd..56193ea49 100644 --- a/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp +++ b/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp @@ -49,7 +49,7 @@ #include #include -//------------------------------------------------------------------------------ +//================================================================================================= // Tools std::wstring toString(double theValue) @@ -67,7 +67,7 @@ std::set toSet(const std::list& theContainer) return std::set(theContainer.begin(), theContainer.end()); } -//------------------------------------------------------------------------------ +//================================================================================================= InitializationPlugin_EvalListener::InitializationPlugin_EvalListener() { @@ -82,10 +82,12 @@ InitializationPlugin_EvalListener::InitializationPlugin_EvalListener() myInterp->initialize(); } +//================================================================================================= InitializationPlugin_EvalListener::~InitializationPlugin_EvalListener() { } +//================================================================================================= void InitializationPlugin_EvalListener::processEvent( const std::shared_ptr& theMessage) { @@ -143,7 +145,7 @@ void InitializationPlugin_EvalListener::processEvent( Locale::Convert::toWString(anExprAttr->value()); anExpZ.erase(std::remove(anExpZ.begin(),anExpZ.end(), ' '), anExpZ.end()); - for(int step =0; step < anValueAttr->rows(); step++ ){ + for (int step =0; step < anValueAttr->rows(); step++){ aVal.myDouble = evaluate(anVar, anValueAttr->value(step,0).myDouble, aParam, @@ -151,9 +153,8 @@ void InitializationPlugin_EvalListener::processEvent( anError, aParamsList, anIsFirstTime); - if(!anError.empty()) break; + if (!anError.empty()) break; anValueAttr->setValue(aVal,step,1); - aVal.myDouble = evaluate(anVar, anValueAttr->value(step,0).myDouble, aParam, @@ -161,9 +162,8 @@ void InitializationPlugin_EvalListener::processEvent( anError, aParamsList, anIsFirstTime); - if(!anError.empty()) break; + if (!anError.empty()) break; anValueAttr->setValue(aVal,step,2); - aVal.myDouble = evaluate(anVar, anValueAttr->value(step,0).myDouble, aParam, @@ -171,16 +171,16 @@ void InitializationPlugin_EvalListener::processEvent( anError, aParamsList, anIsFirstTime); - if(!anError.empty()) break; + if (!anError.empty()) break; anValueAttr->setValue(aVal,step,3); - if ( anIsFirstTime ) + if (anIsFirstTime) anIsFirstTime = false; } - aMsg->setResults(aParamsList, anError); } } +//================================================================================================= double InitializationPlugin_EvalListener::evaluate( std::wstring& theVariable, double theValueVariable, @@ -197,9 +197,8 @@ double InitializationPlugin_EvalListener::evaluate( std::list anExprParams = myInterp->compile(theExpression); // find expression's params in the model - std::list::iterator it = anExprParams.begin(); - for ( ; it != anExprParams.end(); it++) { + for (; it != anExprParams.end(); it++) { double aValue; ResultParameterPtr aParamRes; // If variable does not exist python interpreter will generate an error. @@ -207,11 +206,11 @@ double InitializationPlugin_EvalListener::evaluate( *it, aValue, aParamRes, theParameter->document())) continue; - if( theIsFirstTime ) + if (theIsFirstTime) { std::list::iterator anIter = std::find(theParamsList.begin(),theParamsList.end(), aParamRes ); - if(anIter == theParamsList.end()) + if (anIter == theParamsList.end()) theParamsList.push_back(aParamRes); } @@ -223,7 +222,7 @@ double InitializationPlugin_EvalListener::evaluate( return result; } - +//================================================================================================= double InitializationPlugin_EvalListener::evaluate(FeaturePtr theParameter, const std::wstring& theExpression, std::string& theError, std::list >& theParamsList, @@ -253,6 +252,7 @@ double InitializationPlugin_EvalListener::evaluate(FeaturePtr theParameter, return result; } +//================================================================================================= void InitializationPlugin_EvalListener::processEvaluationEvent( const std::shared_ptr& theMessage) { @@ -341,6 +341,7 @@ void InitializationPlugin_EvalListener::processEvaluationEvent( } } +//================================================================================================= void InitializationPlugin_EvalListener::initDataModel() { myInterp->runString("import salome_iapp;salome_iapp.register_module_in_study(\"Shaper\")"); diff --git a/src/ModelAPI/ModelAPI_Events.h b/src/ModelAPI/ModelAPI_Events.h index 06972aa17..e11ab4b06 100644 --- a/src/ModelAPI/ModelAPI_Events.h +++ b/src/ModelAPI/ModelAPI_Events.h @@ -357,7 +357,7 @@ class ModelAPI_BuildEvalMessage : public Events_Message FeaturePtr myParam; ///< parameters that should be evaluated bool myIsProcessed; ///< true if results were set std::string myError; ///< error of processing, empty if there is no error - /// result of processing, list of parameters in expression found + /// result of processing, list of parameters in expression found std::list > myParamsList; public: @@ -375,8 +375,8 @@ class ModelAPI_BuildEvalMessage : public Events_Message send(FeaturePtr theParameter, const void* theSender) { std::shared_ptr aMessage = - std::shared_ptr( - new ModelAPI_BuildEvalMessage(eventId(), theSender)); + std::shared_ptr( + new ModelAPI_BuildEvalMessage(eventId(), theSender)); aMessage->setParameter(theParameter); Events_Loop::loop()->send(aMessage); return aMessage; @@ -393,9 +393,9 @@ class ModelAPI_BuildEvalMessage : public Events_Message MODELAPI_EXPORT void setParameter(FeaturePtr theParam); /// Sets the results of processing MODELAPI_EXPORT void setResults( - const std::list >& theParamsList, - const std::string& theError); - /// Returns the results of processing: list of parameters found in the expression + const std::list >& theParamsList, + const std::string& theError); + /// Returns the results of processing: list of parameters found in the expression MODELAPI_EXPORT const std::list >& params() const; /// Returns true if the expression is processed MODELAPI_EXPORT bool isProcessed(); -- 2.39.2