Salome HOME
Restore selection in the viewer by multi selector widget activation.
[modules/shaper.git] / src / Model / Model_Data.cpp
index f280f8fc6f3670e3709a5ca9eabb77a504c627e5..e2ef4c7477e1357cfae8ae05164661d08040f092 100644 (file)
@@ -15,6 +15,7 @@
 #include <Model_AttributeString.h>
 #include <Model_AttributeSelection.h>
 #include <Model_AttributeSelectionList.h>
+#include <Model_AttributeIntArray.h>
 #include <Model_Events.h>
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_Result.h>
 #include <Events_Error.h>
 
 #include <TDataStd_Name.hxx>
+#include <TDataStd_AsciiString.hxx>
+#include <TDF_AttributeIterator.hxx>
+#include <TDF_ChildIterator.hxx>
+#include <TDF_RelocationTable.hxx>
 
 #include <string>
 
@@ -66,8 +71,9 @@ void Model_Data::setName(const std::string& theName)
   }
 }
 
-void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
+AttributePtr Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
 {
+  AttributePtr aResult;
   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
   ModelAPI_Attribute* anAttr = 0;
   if (theAttrType == ModelAPI_AttributeDocRef::typeId()) {
@@ -90,6 +96,8 @@ void Model_Data::addAttribute(const std::string& theID, const std::string theAtt
     anAttr = new Model_AttributeRefAttr(anAttrLab);
   } else if (theAttrType == ModelAPI_AttributeRefList::typeId()) {
     anAttr = new Model_AttributeRefList(anAttrLab);
+  } else if (theAttrType == ModelAPI_AttributeIntArray::typeId()) {
+    anAttr = new Model_AttributeIntArray(anAttrLab);
   } 
   // create also GeomData attributes here because only here the OCAF strucure is known
   else if (theAttrType == GeomData_Point::typeId()) {
@@ -100,12 +108,14 @@ void Model_Data::addAttribute(const std::string& theID, const std::string theAtt
     anAttr = new GeomData_Point2D(anAttrLab);
   }
   if (anAttr) {
-    myAttrs[theID] = std::shared_ptr<ModelAPI_Attribute>(anAttr);
+    aResult = std::shared_ptr<ModelAPI_Attribute>(anAttr);
+    myAttrs[theID] = aResult;
     anAttr->setObject(myObject);
     anAttr->setID(theID);
   } else {
     Events_Error::send("Can not create unknown type of attribute " + theAttrType);
   }
+  return aResult;
 }
 
 // macro for gthe generic returning of the attribute by the ID
@@ -130,6 +140,7 @@ GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
+GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeIntArray, intArray);
 
 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
 {
@@ -229,10 +240,22 @@ ModelAPI_ExecState Model_Data::execState()
   return ModelAPI_StateMustBeUpdated; // default value
 }
 
-void Model_Data::setError(const std::string& theError)
+void Model_Data::setError(const std::string& theError, bool theSend)
 {
   execState(ModelAPI_StateExecFailed);
-  Events_Error::send(theError);
+  if (theSend) {
+    Events_Error::send(theError);
+  }
+  TDataStd_AsciiString::Set(myLab, theError.c_str());
+}
+
+std::string Model_Data::error() const
+{
+  Handle(TDataStd_AsciiString) anErrorAttr;
+  if (myLab.FindAttribute(TDataStd_AsciiString::GetID(), anErrorAttr)) {
+    return std::string(anErrorAttr->Get().ToCString());
+  }
+  return std::string();
 }
 
 int Model_Data::featureId() const
@@ -249,10 +272,26 @@ void Model_Data::eraseBackReferences()
     aRes->setIsConcealed(false);
 }
 
-void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID)
+void Model_Data::removeBackReference(FeaturePtr theFeature, std::string theAttrID)
 {
+  AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
+  if (myRefsToMe.find(anAttribute) == myRefsToMe.end())
+    return;
+
+  myRefsToMe.erase(anAttribute);
+}
+
+void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID, 
+   const bool theApplyConcealment)
+{
+  // do not add the same attribute twice
+  AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
+  if (myRefsToMe.find(anAttribute) != myRefsToMe.end())
+    return;
+
   myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
-  if (ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
+  if (theApplyConcealment && 
+      ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
     std::shared_ptr<ModelAPI_Result> aRes = 
       std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
     if (aRes) {
@@ -297,3 +336,36 @@ void Model_Data::referencesToObjects(
     }
   }
 }
+
+/// makes copy of all attributes on the given label and all sub-labels
+static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
+  TDF_AttributeIterator anAttrIter(theSource);
+  for(; anAttrIter.More(); anAttrIter.Next()) {
+    Handle(TDF_Attribute) aTargetAttr;
+    if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
+      // create a new attribute if not yet exists in the destination
+           aTargetAttr = anAttrIter.Value()->NewEmpty();
+      theDestination.AddAttribute(aTargetAttr);
+    }
+    Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map
+    anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
+  }
+  // copy the sub-labels content
+  TDF_ChildIterator aSubLabsIter(theSource);
+  for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
+    copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
+  }
+}
+
+void Model_Data::copyTo(std::shared_ptr<ModelAPI_Data> theTarget)
+{
+  TDF_Label aTargetRoot = std::dynamic_pointer_cast<Model_Data>(theTarget)->label();
+  copyAttrs(myLab, aTargetRoot);
+  // make initialized the initialized attributes
+  std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator aMyIter = myAttrs.begin();
+  for(; aMyIter != myAttrs.end(); aMyIter++) {
+    if (aMyIter->second->isInitialized()) {
+      theTarget->attribute(aMyIter->first)->setInitialized();
+    }
+  }
+}