Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[modules/shaper.git] / src / Model / Model_AttributeReference.cpp
index e65cb91c2e2f6a32cf6537c128c3f62a430992f0..91de64eb567de9f94889a6274c98f8ae8c6dcebc 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 // File:        ModelAPI_AttributeReference.cxx
 // Created:     2 Apr 2014
 // Author:      Mikhail PONIKAROV
@@ -20,9 +22,9 @@ void Model_AttributeReference::setValue(ObjectPtr theObject)
   if(!theObject)
     return;
   if (!myIsInitialized || value() != theObject) {
-      std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
-          theObject->data());
-      TDF_Label anObjLab = aData->label().Father(); // object label
+    std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
+        theObject->data());
+    TDF_Label anObjLab = aData->label().Father(); // object label
 
     if (owner()->document() == theObject->document()) { // same document, use reference attribute
 
@@ -30,8 +32,8 @@ void Model_AttributeReference::setValue(ObjectPtr theObject)
         std::dynamic_pointer_cast<Model_Document>(owner()->document());
       myRef->Set(anObjLab);  // references to the object label
        // remove external link attributes (if any)
-      anObjLab.ForgetAttribute(TDataStd_Comment::GetID());
-      anObjLab.ForgetAttribute(TDataStd_AsciiString::GetID());
+      myRef->Label().ForgetAttribute(TDataStd_Comment::GetID());
+      myRef->Label().ForgetAttribute(TDataStd_AsciiString::GetID());
     } else { // different document: store the document name (comment) and entry (string): external
       // if these attributes exist, the link is external: keep reference to access the label
       TDataStd_Comment::Set(myRef->Label(), theObject->document()->id().c_str());
@@ -39,6 +41,12 @@ void Model_AttributeReference::setValue(ObjectPtr theObject)
       TDF_Tool::Entry(anObjLab, anEntry);
       TDataStd_AsciiString::Set(myRef->Label(), anEntry);
     }
+    // do it before the transaction finish to make just created/removed objects know dependencies
+    // and reference from composite feature is removed automatically
+    FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
+    if (anOwnerFeature.get()) {
+      aData->addBackReference(anOwnerFeature, id());
+    }
 
     owner()->data()->sendAttributeUpdated(this);
   }
@@ -67,7 +75,9 @@ ObjectPtr Model_AttributeReference::value()
           owner()->document());
       if (aDoc) {
         TDF_Label aRefLab = myRef->Get();
-        return aDoc->object(aRefLab);
+        if (!aRefLab.IsNull()) {  // it may happen with old document, issue #285
+          return aDoc->object(aRefLab);
+        }
       }
     }
   }