Salome HOME
Issue #763 - It is possible to remove parameter which is used in features -- Fix...
authorspo <sergey.pokhodenko@opencascade.com>
Wed, 5 Aug 2015 14:49:38 +0000 (17:49 +0300)
committerspo <sergey.pokhodenko@opencascade.com>
Thu, 6 Aug 2015 08:54:57 +0000 (11:54 +0300)
src/Model/Model_Data.cpp
src/ModelAPI/ModelAPI_Tools.cpp
src/ModelAPI/ModelAPI_Tools.h
src/ParametersPlugin/ParametersPlugin_EvalListener.cpp
src/ParametersPlugin/ParametersPlugin_EvalListener.h
src/ParametersPlugin/ParametersPlugin_Parameter.cpp

index 3e7a5407e491dd05745450173a4c7200100fa7d0..c4aef0f9644316c6289f3ae4d1e9508284f80fc7 100644 (file)
@@ -427,7 +427,8 @@ std::set<std::string> usedParameters(const AttributePoint2DPtr& theAttribute)
   return anUsedParameters;
 }
 
-std::list<ResultParameterPtr> findVariables(const std::set<std::string>& theParameters)
+std::list<ResultParameterPtr> findVariables(const std::set<std::string>& theParameters, 
+                                            const DocumentPtr& theDocument)
 {
   std::list<ResultParameterPtr> aResult;
   std::set<std::string>::const_iterator aParamIt = theParameters.cbegin();
@@ -435,7 +436,7 @@ std::list<ResultParameterPtr> findVariables(const std::set<std::string>& 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<ModelAPI_AttributeDouble>(anAttr->second);
       std::set<std::string> anUsedParameters = anAttribute->usedParameters();
-      std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
+      std::list<ResultParameterPtr> 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<GeomDataAPI_Point>(anAttr->second);
       std::set<std::string> anUsedParameters = usedParameters(anAttribute);
-      std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
+      std::list<ResultParameterPtr> 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<GeomDataAPI_Point2D>(anAttr->second);
       std::set<std::string> anUsedParameters = usedParameters(anAttribute);
-      std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
+      std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters, aMyFeature->document());
       aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
     } else
       continue; // nothing to do, not reference
index 09fc2c0bee522fdba7d74eb34378f6a0436c0bbf..a8e174d123f9f56e19a1d1632ca3674b3cd17b91 100644 (file)
@@ -37,32 +37,21 @@ std::shared_ptr<GeomAPI_Shape> 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<DocumentPtr> 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<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
-    ObjectPtr aParamObj = (*it)->objectByName(ModelAPI_ResultParameter::group(), theName);
-    theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(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<ModelAPI_ResultParameter>(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<DocumentPtr> 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<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
+    if (findVariable(*it, theName, outValue, theParam))
+      return true;
+  }
+  return false;
+}
+
 static std::map<int, std::vector<int> > myColorMap;
 
 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
index f14029a0cf46e7df1d3384a2f40c41ce2eecc048..11e4b6ff0a29d42803b89b2b38d52915f48eb710 100644 (file)
@@ -24,19 +24,19 @@ namespace ModelAPI_Tools {
 MODELAPI_EXPORT std::shared_ptr<GeomAPI_Shape> 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]
index b5acdb33e644bbc8cd633950b5f13414150be821..8e826aa1684e5db984c74e331ba6559fa4aa2129 100644 (file)
 
 #include <Events_Error.h>
 
+#include <ModelAPI_AttributeRefList.h>
 #include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeValidator.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Tools.h>
-#include <ModelAPI_AttributeValidator.h>
 
 #include <ModelAPI_AttributeDouble.h>
 #include <GeomDataAPI_Point.h>
@@ -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<ModelAPI_Document>& theDocument)
 {
   std::list<std::string> 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<ModelAPI_AttributeDouble>(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<std::string>());
@@ -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<std::string>());
@@ -313,44 +314,24 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent(
     return;
   }
 
-  // List of documents to process
-  std::list<DocumentPtr> 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<DocumentPtr>::const_iterator aDicumentIt = aDocList.begin(); 
-       aDicumentIt != aDocList.end(); ++aDicumentIt) {
-    const DocumentPtr& aDocument = *aDicumentIt;
-    std::list<FeaturePtr> aFeatures = aDocument->allFeatures();
-    std::list<FeaturePtr>::iterator aFeatureIt = aFeatures.begin();
-    for (; aFeatureIt != aFeatures.end(); ++aFeatureIt) {
-      const FeaturePtr& aFeature = *aFeatureIt;
-      
-      // If Parameter feature then rename its expression
+  std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes = 
+      aResultParameter->data()->refsToMe();
+  std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anAttributeIt =
+      anAttributes.cbegin();
+  for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) {
+    const AttributePtr& anAttribute = *anAttributeIt;
+    AttributeRefListPtr anAttributeRefList =
+        std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
+    if (anAttributeRefList.get()) {
       std::shared_ptr<ParametersPlugin_Parameter> aParameter =
-          std::dynamic_pointer_cast<ParametersPlugin_Parameter>(aFeature);
-      if (aParameter.get()) {
+          std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
+              anAttributeRefList->owner());
+      if (aParameter.get())
         // Rename
         renameInParameter(aParameter, aMessage->oldName(), aMessage->newName());
-        continue;
-      }      
-
-      // Find all attributes
-      std::list<AttributePtr> anAttributes = aFeature->data()->attributes(std::string());
-      std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
-      for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) {
-        const AttributePtr& anAttribute = *anAttributeIt;
-
+    } else
         // Rename
         renameInAttribute(anAttribute, aMessage->oldName(), aMessage->newName());
-      }
-    }
   }
 }
 
index bd9e6385fd9a34b7be362dff2253ca0486fdd2b1..b9004c3d32ff7e749f5bfc7c06c69de92884ef7d 100644 (file)
@@ -12,6 +12,7 @@
 #include <Events_Loop.h>
 
 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<Events_Message>& theMessage);
 
  protected:
-  double evaluate(const std::string& theExpression, std::string& theError);
+  double evaluate(const std::string& theExpression, std::string& theError, 
+                  const std::shared_ptr<ModelAPI_Document>& theDocument);
 
   void processEvaluationEvent(const std::shared_ptr<Events_Message>& theMessage);
   void processObjectRenamedEvent(const std::shared_ptr<Events_Message>& theMessage);
index 2ec43200f30894fe74f80e3268379ac19cbf91e7..54dc39581185a6892ddb35ce04310de7b19e044e 100644 (file)
@@ -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;