From 26d4e526ae2c0ae52e880908f76ab71d1c25e9d9 Mon Sep 17 00:00:00 2001 From: spo Date: Wed, 5 Aug 2015 17:49:38 +0300 Subject: [PATCH] Issue #763 - It is possible to remove parameter which is used in features -- Fix complex cases --- src/Model/Model_Data.cpp | 11 ++-- src/ModelAPI/ModelAPI_Tools.cpp | 49 ++++++++------- src/ModelAPI/ModelAPI_Tools.h | 18 +++--- .../ParametersPlugin_EvalListener.cpp | 61 +++++++------------ .../ParametersPlugin_EvalListener.h | 4 +- .../ParametersPlugin_Parameter.cpp | 3 +- 6 files changed, 69 insertions(+), 77 deletions(-) diff --git a/src/Model/Model_Data.cpp b/src/Model/Model_Data.cpp index 3e7a5407e..c4aef0f96 100644 --- a/src/Model/Model_Data.cpp +++ b/src/Model/Model_Data.cpp @@ -427,7 +427,8 @@ std::set usedParameters(const AttributePoint2DPtr& theAttribute) return anUsedParameters; } -std::list findVariables(const std::set& theParameters) +std::list findVariables(const std::set& theParameters, + const DocumentPtr& theDocument) { std::list aResult; std::set::const_iterator aParamIt = theParameters.cbegin(); @@ -435,7 +436,7 @@ std::list findVariables(const std::set& thePara const std::string& aName = *aParamIt; double aValue; ResultParameterPtr aParam; - if (ModelAPI_Tools::findVariable(aName, aValue, aParam)) + if (ModelAPI_Tools::findVariable(aName, aValue, aParam, theDocument)) aResult.push_back(aParam); } return aResult; @@ -480,19 +481,19 @@ void Model_Data::referencesToObjects( AttributeDoublePtr anAttribute = std::dynamic_pointer_cast(anAttr->second); std::set anUsedParameters = anAttribute->usedParameters(); - std::list aParameters = findVariables(anUsedParameters); + std::list aParameters = findVariables(anUsedParameters, aMyFeature->document()); aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end()); } else if (aType == GeomDataAPI_Point::typeId()) { // point attribute AttributePointPtr anAttribute = std::dynamic_pointer_cast(anAttr->second); std::set anUsedParameters = usedParameters(anAttribute); - std::list aParameters = findVariables(anUsedParameters); + std::list aParameters = findVariables(anUsedParameters, aMyFeature->document()); aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end()); } else if (aType == GeomDataAPI_Point2D::typeId()) { // point attribute AttributePoint2DPtr anAttribute = std::dynamic_pointer_cast(anAttr->second); std::set anUsedParameters = usedParameters(anAttribute); - std::list aParameters = findVariables(anUsedParameters); + std::list aParameters = findVariables(anUsedParameters, aMyFeature->document()); aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end()); } else continue; // nothing to do, not reference diff --git a/src/ModelAPI/ModelAPI_Tools.cpp b/src/ModelAPI/ModelAPI_Tools.cpp index 09fc2c0be..a8e174d12 100644 --- a/src/ModelAPI/ModelAPI_Tools.cpp +++ b/src/ModelAPI/ModelAPI_Tools.cpp @@ -37,32 +37,21 @@ std::shared_ptr shape(const ResultPtr& theResult) return theResult->shape(); } -bool findVariable(const std::string& theName, double& outValue, ResultParameterPtr& theParam) +ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup, const std::string& theName) { - SessionPtr aSession = ModelAPI_Session::get(); - std::list aDocList; - DocumentPtr aDocument = aSession->activeDocument(); - DocumentPtr aRootDocument = aSession->moduleDocument(); - aDocList.push_back(aDocument); - if (aDocument != aRootDocument) { - aDocList.push_back(aRootDocument); + for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) { + ObjectPtr anObject = theDocument->object(theGroup, anIndex); + if (anObject->data()->name() == theName) + return anObject; } - for(std::list::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) { - ObjectPtr aParamObj = (*it)->objectByName(ModelAPI_ResultParameter::group(), theName); - theParam = std::dynamic_pointer_cast(aParamObj); - if(!theParam.get()) - continue; - AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE()); - outValue = aValueAttribute->value(); - return true; - } - return false; + // not found + return ObjectPtr(); } -bool findVariable(const DocumentPtr& theDocument, const std::string& theName, - double& outValue, ResultParameterPtr& theParam) +bool findVariable(const DocumentPtr& theDocument, + const std::string& theName, double& outValue, ResultParameterPtr& theParam) { - ObjectPtr aParamObj = theDocument->objectByName(ModelAPI_ResultParameter::group(), theName); + ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName); theParam = std::dynamic_pointer_cast(aParamObj); if (!theParam.get()) return false; @@ -71,6 +60,24 @@ bool findVariable(const DocumentPtr& theDocument, const std::string& theName, return true; } +bool findVariable(const std::string& theName, double& outValue, ResultParameterPtr& theParam, + const DocumentPtr& theDocument /*= DocumentPtr()*/) +{ + SessionPtr aSession = ModelAPI_Session::get(); + std::list aDocList; + DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument(); + DocumentPtr aRootDocument = aSession->moduleDocument(); + aDocList.push_back(aDocument); + if (aDocument != aRootDocument) { + aDocList.push_back(aRootDocument); + } + for(std::list::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) { + if (findVariable(*it, theName, outValue, theParam)) + return true; + } + return false; +} + static std::map > myColorMap; void appendValues(std::vector& theRGB, const int theRed, const int theGreen, const int theBlue) diff --git a/src/ModelAPI/ModelAPI_Tools.h b/src/ModelAPI/ModelAPI_Tools.h index f14029a0c..11e4b6ff0 100644 --- a/src/ModelAPI/ModelAPI_Tools.h +++ b/src/ModelAPI/ModelAPI_Tools.h @@ -24,19 +24,19 @@ namespace ModelAPI_Tools { MODELAPI_EXPORT std::shared_ptr shape(const ResultPtr& theResult); /*! - * Searches for variable with name \param theName in the active document (Part), when - * in the root document (PartSet). If found, set it value in the \param outValue - * and returns true. + * Searches for variable with name \param theName in \param theDocument. + * If found, set it value in the \param outValue and returns true. */ -MODELAPI_EXPORT bool findVariable(const std::string& theName, double& outValue, - ResultParameterPtr& theParam); +MODELAPI_EXPORT bool findVariable(const DocumentPtr& theDocument, + const std::string& theName, double& outValue, ResultParameterPtr& theParam); /*! - * Searches for variable with name \param theName in the document. - * If found, set it value in the \param outValue and returns true. + * Searches for variable with name \param theName in the active document (Part), when + * in the root document (PartSet). If found, set it value in the \param outValue + * and returns true. If \param theDocument is empty active document is used. */ -MODELAPI_EXPORT bool findVariable(const DocumentPtr& theDocument, const std::string& theName, - double& outValue, ResultParameterPtr& theParam); +MODELAPI_EXPORT bool findVariable(const std::string& theName, double& outValue, ResultParameterPtr& theParam, + const DocumentPtr& theDocument = DocumentPtr()); /*! * Returns the values of the next random color. The values are in range [0, 255] diff --git a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp index b5acdb33e..8e826aa16 100644 --- a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp +++ b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp @@ -13,12 +13,13 @@ #include +#include #include +#include #include #include #include #include -#include #include #include @@ -62,8 +63,8 @@ void ParametersPlugin_EvalListener::processEvent( } } -double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression, - std::string& theError) +double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression, std::string& theError, + const std::shared_ptr& theDocument) { std::list anExprParams = myInterp->compile(theExpression); // find expression's params in the model @@ -72,7 +73,7 @@ double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression, for ( ; it != anExprParams.end(); it++) { double aValue; ResultParameterPtr aParamRes; - if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes)) continue; + if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes, theDocument)) continue; std::ostringstream sstream; sstream << aValue; @@ -100,7 +101,7 @@ void ParametersPlugin_EvalListener::processEvaluationEvent( AttributeDoublePtr anAttribute = std::dynamic_pointer_cast(aMessage->attribute()); std::string anError; - double aValue = evaluate(anAttribute->text(), anError); + double aValue = evaluate(anAttribute->text(), anError, anAttribute->owner()->document()); bool isValid = anError.empty(); if (isValid) anAttribute->setCalculatedValue(aValue); @@ -123,7 +124,7 @@ void ParametersPlugin_EvalListener::processEvaluationEvent( }; for (int i = 0; i < 3; ++i) { std::string anError; - double aValue = evaluate(aText[i], anError); + double aValue = evaluate(aText[i], anError, anAttribute->owner()->document()); bool isValid = anError.empty(); if (isValid) aCalculatedValue[i] = aValue; anAttribute->setUsedParameters(i, isValid ? toSet(myInterp->compile(aText[i])) : std::set()); @@ -147,7 +148,7 @@ void ParametersPlugin_EvalListener::processEvaluationEvent( }; for (int i = 0; i < 2; ++i) { std::string anError; - double aValue = evaluate(aText[i], anError); + double aValue = evaluate(aText[i], anError, anAttribute->owner()->document()); bool isValid = anError.empty(); if (isValid) aCalculatedValue[i] = aValue; anAttribute->setUsedParameters(i, isValid ? toSet(myInterp->compile(aText[i])) : std::set()); @@ -313,44 +314,24 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent( return; } - // List of documents to process - std::list aDocList; - SessionPtr aSession = ModelAPI_Session::get(); - DocumentPtr aDocument = aSession->activeDocument(); - DocumentPtr aRootDocument = aSession->moduleDocument(); - aDocList.push_back(aDocument); - if (aDocument != aRootDocument) { - aDocList.push_back(aRootDocument); - } - - // Find all features - for (std::list::const_iterator aDicumentIt = aDocList.begin(); - aDicumentIt != aDocList.end(); ++aDicumentIt) { - const DocumentPtr& aDocument = *aDicumentIt; - std::list aFeatures = aDocument->allFeatures(); - std::list::iterator aFeatureIt = aFeatures.begin(); - for (; aFeatureIt != aFeatures.end(); ++aFeatureIt) { - const FeaturePtr& aFeature = *aFeatureIt; - - // If Parameter feature then rename its expression + std::set > anAttributes = + aResultParameter->data()->refsToMe(); + std::set >::const_iterator anAttributeIt = + anAttributes.cbegin(); + for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) { + const AttributePtr& anAttribute = *anAttributeIt; + AttributeRefListPtr anAttributeRefList = + std::dynamic_pointer_cast(anAttribute); + if (anAttributeRefList.get()) { std::shared_ptr aParameter = - std::dynamic_pointer_cast(aFeature); - if (aParameter.get()) { + std::dynamic_pointer_cast( + anAttributeRefList->owner()); + if (aParameter.get()) // Rename renameInParameter(aParameter, aMessage->oldName(), aMessage->newName()); - continue; - } - - // Find all attributes - std::list anAttributes = aFeature->data()->attributes(std::string()); - std::list::const_iterator anAttributeIt = anAttributes.begin(); - for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) { - const AttributePtr& anAttribute = *anAttributeIt; - + } else // Rename renameInAttribute(anAttribute, aMessage->oldName(), aMessage->newName()); - } - } } } diff --git a/src/ParametersPlugin/ParametersPlugin_EvalListener.h b/src/ParametersPlugin/ParametersPlugin_EvalListener.h index bd9e6385f..b9004c3d3 100644 --- a/src/ParametersPlugin/ParametersPlugin_EvalListener.h +++ b/src/ParametersPlugin/ParametersPlugin_EvalListener.h @@ -12,6 +12,7 @@ #include class ModelAPI_Attribute; +class ModelAPI_Document; class ParametersPlugin_Parameter; class ParametersPlugin_PyInterp; @@ -24,7 +25,8 @@ class PARAMETERSPLUGIN_EXPORT ParametersPlugin_EvalListener : public Events_List virtual void processEvent(const std::shared_ptr& theMessage); protected: - double evaluate(const std::string& theExpression, std::string& theError); + double evaluate(const std::string& theExpression, std::string& theError, + const std::shared_ptr& theDocument); void processEvaluationEvent(const std::shared_ptr& theMessage); void processObjectRenamedEvent(const std::shared_ptr& theMessage); diff --git a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp index 2ec43200f..54dc39581 100644 --- a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp +++ b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp @@ -35,6 +35,7 @@ void ParametersPlugin_Parameter::initAttributes() data()->addAttribute(VARIABLE_ID(), ModelAPI_AttributeString::typeId()); data()->addAttribute(EXPRESSION_ID(), ModelAPI_AttributeString::typeId()); data()->addAttribute(ARGUMENTS_ID(), ModelAPI_AttributeRefList::typeId()); + data()->reflist(ARGUMENTS_ID())->setIsArgument(false); ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ARGUMENTS_ID()); } @@ -105,7 +106,7 @@ double ParametersPlugin_Parameter::evaluate(const std::string& theExpression, st for ( ; it != anExprParams.end(); it++) { double aValue; ResultParameterPtr aParamRes; - if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes)) continue; + if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes, document())) continue; aParamsList.push_back(aParamRes); std::ostringstream sstream; -- 2.39.2