- aData->removeBackReference(*aCurrentIter);
- aCurrentIter = aData->refsToMe().begin(); // reinitialize iteration after delete
+ // for external references from other documents this system is not working: refs are collected from
+ // different Model_Objects, so before remove check this external object exists and still referenced
+ bool aLeaveIt = false;
+ if ((*aCurrentIter)->owner().get() && (*aCurrentIter)->owner()->document() != myDoc &&
+ (*aCurrentIter)->owner()->data().get() && (*aCurrentIter)->owner()->data()->isValid()) {
+ std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
+ (*aCurrentIter)->owner()->data()->referencesToObjects(aRefs);
+ std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > >::iterator
+ aRefIter = aRefs.begin();
+ for(; aRefIter != aRefs.end(); aRefIter++) {
+ if ((*aCurrentIter)->id() == aRefIter->first) {
+ std::list<std::shared_ptr<ModelAPI_Object> >::iterator anOIt;
+ for(anOIt = aRefIter->second.begin(); anOIt != aRefIter->second.end(); anOIt++) {
+ if (*anOIt == theObject) {
+ aLeaveIt = true;
+ }
+ }
+ }
+ }
+ }
+ if (!aLeaveIt) {
+ aData->removeBackReference(*aCurrentIter);
+ aCurrentIter = aData->refsToMe().begin(); // reinitialize iteration after delete
+ } else aCurrentIter++;