- if (!myIsInitialized || myID->Get().Length() != 0 || object() != theObject) {
- boost::shared_ptr<Model_Data> aData =
- boost::dynamic_pointer_cast<Model_Data>(theObject->data());
- myRef->Set(aData->label());
- myID->Set(""); // feature is identified by the empty ID
+ // the back reference from the previous object to the attribute should be removed
+ ObjectPtr anObject = object();
+ if (theObject.get() && (!myIsInitialized || myID->Get().Length() != 0 || object() != theObject)) {
+ REMOVE_BACK_REF(anObject);
+
+ std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
+ theObject->data());
+ myRef->Set(aData->label().Father());
+ myID->Set(""); // feature is identified by the empty ID
+
+ // 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(), false);
+ }
+ ADD_BACK_REF(theObject);
+ owner()->data()->sendAttributeUpdated(this);
+ } else if (theObject.get() == NULL) {
+ REMOVE_BACK_REF(anObject);
+ myRef->Set(myRef->Label()); // reference to itself means that object is null
+ myID->Set(""); // feature is identified by the empty ID