- list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
- list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
- bool isRemoved = false;
- bool aHasEmtpyFeature = false;
- for(; aSubIt != aLastIt && !isRemoved; aSubIt++) {
- std::shared_ptr<ModelAPI_Feature> aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aSubIt);
- if (aFeature.get() != NULL && aFeature == theFeature) {
- data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(aFeature);
- isRemoved = true;
- }
- else if (aFeature.get() == NULL)
- aHasEmtpyFeature = true;
- }
- // if the object is not found in the sketch sub-elements, that means that the object is removed already.
- // Find the first empty element and remove it
- if (!isRemoved && aHasEmtpyFeature)
- data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(ObjectPtr());
+ // to keep the persistent sub-elements indexing, do not remove elements from list,
+ // but substitute by nulls
+ reflist(SketchPlugin_Sketch::FEATURES_ID())->substitute(theFeature, ObjectPtr());