Salome HOME
Fix for issue #980
[modules/shaper.git] / src / Model / Model_Data.cpp
index 3e7a5407e491dd05745450173a4c7200100fa7d0..8d21b7a7834e5a8ed8d12fcf21fa41fb80dc7af1 100644 (file)
@@ -25,6 +25,7 @@
 #include <ModelAPI_Session.h>
 #include <ModelAPI_ResultPart.h>
 #include <ModelAPI_Tools.h>
+#include <Model_Validator.h>
 
 #include <GeomDataAPI_Point.h>
 #include <GeomDataAPI_Point2D.h>
@@ -41,6 +42,7 @@
 #include <TDF_AttributeIterator.hxx>
 #include <TDF_ChildIterator.hxx>
 #include <TDF_RelocationTable.hxx>
+#include <TColStd_HArray1OfByte.hxx>
 
 #include <string>
 
@@ -52,6 +54,8 @@
 static const int kFlagInHistory = 0;
 //                             1 - is displayed or not
 static const int kFlagDisplayed = 1;
+//                             2 - is deleted (for results) or not
+static const int kFlagDeleted = 2;
 
 // invalid data
 const static std::shared_ptr<ModelAPI_Data> kInvalid(new Model_Data());
@@ -66,9 +70,19 @@ void Model_Data::setLabel(TDF_Label theLab)
   // set or get the default flags
   if (!myLab.FindAttribute(TDataStd_BooleanArray::GetID(), myFlags)) {
     // set default values if not found
-    myFlags = TDataStd_BooleanArray::Set(myLab, 0, 1);
+    myFlags = TDataStd_BooleanArray::Set(myLab, 0, 2);
     myFlags->SetValue(kFlagInHistory, Standard_True); // is in history by default is true
     myFlags->SetValue(kFlagDisplayed, Standard_True); // is displayed by default is true
+    myFlags->SetValue(kFlagDeleted, Standard_False); // is deleted by default is false
+  } else if (myFlags->Length() != 3) { // for old formats support
+    Standard_Boolean aFlag0 = myFlags->Upper() >= 0 ? myFlags->Value(0) : Standard_True;
+    Standard_Boolean aFlag1 = myFlags->Upper() >= 1 ? myFlags->Value(1) : Standard_True;
+    Standard_Boolean aFlag2 = myFlags->Upper() >= 2 ? myFlags->Value(2) : Standard_True;
+    Handle(TColStd_HArray1OfByte) aNewArray = new TColStd_HArray1OfByte(0, 2);
+    myFlags->SetInternalArray(aNewArray);
+    myFlags->SetValue(0, aFlag0); 
+    myFlags->SetValue(1, aFlag1); 
+    myFlags->SetValue(2, aFlag2); 
   }
 }
 
@@ -108,7 +122,7 @@ AttributePtr Model_Data::addAttribute(const std::string& theID, const std::strin
     anAttr = new Model_AttributeInteger(anAttrLab);
   } else if (theAttrType == ModelAPI_AttributeDouble::typeId()) {
     Model_AttributeDouble* anAttribute = new Model_AttributeDouble(anAttrLab);
-    TDF_Label anExpressionLab = anAttrLab.FindChild(anAttrLab.NbChildren() + 1);
+    TDF_Label anExpressionLab = anAttrLab.FindChild(1);
     anAttribute->myExpression.reset(new Model_Expression(anExpressionLab));
     anAttribute->myIsInitialized = anAttribute->myIsInitialized && anAttribute->myExpression->isInitialized(); 
     anAttr = anAttribute;
@@ -133,7 +147,7 @@ AttributePtr Model_Data::addAttribute(const std::string& theID, const std::strin
   else if (theAttrType == GeomData_Point::typeId()) {
     GeomData_Point* anAttribute = new GeomData_Point(anAttrLab);
     for (int aComponent = 0; aComponent < GeomData_Point::NUM_COMPONENTS; ++aComponent) {
-      TDF_Label anExpressionLab = anAttrLab.FindChild(anAttrLab.NbChildren() + 1);
+      TDF_Label anExpressionLab = anAttrLab.FindChild(aComponent + 1);
       anAttribute->myExpression[aComponent].reset(new Model_Expression(anExpressionLab));
       anAttribute->myIsInitialized = anAttribute->myIsInitialized && anAttribute->myExpression[aComponent]->isInitialized(); 
     }
@@ -143,7 +157,7 @@ AttributePtr Model_Data::addAttribute(const std::string& theID, const std::strin
   } else if (theAttrType == GeomData_Point2D::typeId()) {
     GeomData_Point2D* anAttribute = new GeomData_Point2D(anAttrLab);
     for (int aComponent = 0; aComponent < GeomData_Point2D::NUM_COMPONENTS; ++aComponent) {
-      TDF_Label anExpressionLab = anAttrLab.FindChild(anAttrLab.NbChildren() + 1);
+      TDF_Label anExpressionLab = anAttrLab.FindChild(aComponent + 1);
       anAttribute->myExpression[aComponent].reset(new Model_Expression(anExpressionLab));
       anAttribute->myIsInitialized = anAttribute->myIsInitialized && anAttribute->myExpression[aComponent]->isInitialized(); 
     }
@@ -288,7 +302,12 @@ static Handle(TDataStd_IntegerArray) stateArray(TDF_Label& theLab)
 void Model_Data::execState(const ModelAPI_ExecState theState)
 {
   if (theState != ModelAPI_StateNothing) {
-    stateArray(myLab)->SetValue(STATE_INDEX_STATE, (int)theState);
+    if (stateArray(myLab)->Value(STATE_INDEX_STATE) != (int)theState) {
+      stateArray(myLab)->SetValue(STATE_INDEX_STATE, (int)theState);
+    }
+    // send signal even if the new value corresponds to the one in data model: undo issue 980
+    static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_ERROR_CHANGED);
+    ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent, false);
   }
 }
 
@@ -314,6 +333,13 @@ void Model_Data::setError(const std::string& theError, bool theSend)
     Events_Error::send(theError);
   }
   TDataStd_AsciiString::Set(myLab, theError.c_str());
+  static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_ERROR_CHANGED);
+  ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent, false);
+}
+
+void Model_Data::eraseErrorString()
+{
+  myLab.ForgetAttribute(TDataStd_AsciiString::GetID());
 }
 
 std::string Model_Data::error() const
@@ -392,7 +418,7 @@ void Model_Data::updateConcealmentFlag()
   // thus, no concealment references anymore => make not-concealed
   std::shared_ptr<ModelAPI_Result> aRes = 
     std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
-  if (aRes.get()) {
+  if (aRes.get() && aRes->isConcealed()) {
     aRes->setIsConcealed(false);
     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
     ModelAPI_EventCreator::get()->sendUpdated(aRes, anEvent);
@@ -400,8 +426,6 @@ void Model_Data::updateConcealmentFlag()
   }
 }
 
-#include <Model_Validator.h>
-
 std::set<std::string> set_union(const std::set<std::string>& theLeft, 
                                 const std::set<std::string>& theRight)
 {
@@ -427,7 +451,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 +460,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 +505,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
@@ -547,6 +572,16 @@ void Model_Data::setIsInHistory(const bool theFlag)
   return myFlags->SetValue(kFlagInHistory, theFlag);
 }
 
+bool Model_Data::isDeleted()
+{
+  return myFlags->Value(kFlagDeleted) == Standard_True;
+}
+
+void Model_Data::setIsDeleted(const bool theFlag)
+{
+  return myFlags->SetValue(kFlagDeleted, theFlag);
+}
+
 bool Model_Data::isDisplayed()
 {
   if (!myObject.get() || !myObject->document().get() || // object is in valid
@@ -582,7 +617,7 @@ std::shared_ptr<ModelAPI_Data> Model_Data::invalidData()
   return kInvalid;
 }
 
-bool Model_Data::isOwner(ModelAPI_Object* theOwner)
+std::shared_ptr<ModelAPI_Object> Model_Data::owner()
 {
-  return theOwner == myObject.get();
+  return myObject;
 }