Salome HOME
Issue #2593: CEA 2018-2 Geometrical Naming
[modules/shaper.git] / src / Model / Model_AttributeSelection.cpp
index 847380c449691f31186433aedfe75260bffdd2fd..7cf468c5bb7bd165788254ec06a5b9d51ceaa90b 100644 (file)
@@ -23,7 +23,6 @@
 #include "Model_Events.h"
 #include "Model_Data.h"
 #include "Model_Document.h"
-#include "Model_SelectionNaming.h"
 #include <Model_Objects.h>
 #include <Model_AttributeSelectionList.h>
 #include <Model_ResultConstruction.h>
 #include <ModelAPI_Tools.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Validator.h>
+#include <ModelGeomAlgo_Shape.h>
 #include <Events_InfoMessage.h>
 #include <GeomAPI_Edge.h>
+#include <GeomAPI_Pnt.h>
+#include <GeomAPI_ShapeIterator.h>
 #include <GeomAPI_Vertex.h>
+#include <GeomAPI_ShapeExplorer.h>
 #include <GeomAlgoAPI_CompoundBuilder.h>
+#include <GeomAlgoAPI_NExplode.h>
+#include <Selector_Selector.h>
 
-#include <TNaming_Selector.hxx>
 #include <TNaming_NamedShape.hxx>
 #include <TNaming_Tool.hxx>
 #include <TNaming_Builder.hxx>
 #include <TNaming_SameShapeIterator.hxx>
+#include <TNaming_NewShapeIterator.hxx>
 #include <TNaming_Iterator.hxx>
 #include <TDataStd_Integer.hxx>
 #include <TDataStd_UAttribute.hxx>
@@ -56,6 +61,7 @@
 #include <TopExp_Explorer.hxx>
 #include <BRep_Tool.hxx>
 #include <TopoDS.hxx>
+#include <TopoDS_Edge.hxx>
 #include <TopExp.hxx>
 #include <TDF_ChildIterator.hxx>
 #include <TDF_ChildIDIterator.hxx>
@@ -124,7 +130,7 @@ bool Model_AttributeSelection::setValue(const ObjectPtr& theContext,
     myRef.setValue(theContext);
   }
 
-  // do noth use naming if selected shape is result shape itself, but not sub-shape
+  // do not use naming if selected shape is result shape itself, but not sub-shape
   TDF_Label aSelLab = selectionLabel();
   aSelLab.ForgetAttribute(kSIMPLE_REF_ID);
   aSelLab.ForgetAttribute(kINVALID_SELECTION);
@@ -148,38 +154,57 @@ bool Model_AttributeSelection::setValue(const ObjectPtr& theContext,
       owner()->data()->blockSendAttributeUpdated(false);
     return false;
   }
-  if (theContext->groupName() == ModelAPI_ResultBody::group()) {
-    ResultBodyPtr aContextBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theContext);
+  bool isSelectBody = theContext->groupName() == ModelAPI_ResultBody::group();
+  if (!isSelectBody) {
+    ResultConstructionPtr aContextConstruction =
+      std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theContext);
+    isSelectBody = aContextConstruction.get() && !aContextConstruction->isInfinite();
+  }
+  if (isSelectBody) {
+    ResultPtr aContextResult = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
+    GeomShapePtr aContextShape = aContextResult->shape();
     // do not select the whole shape for body:it is already must be in the data framework
     // equal and null selected objects mean the same: object is equal to context,
-    if (aContextBody->shape().get() &&
-        (aContextBody->shape()->isEqual(theSubShape) || !theSubShape.get())) {
+    if (aContextShape.get() && (aContextShape->isEqual(theSubShape) || !theSubShape.get())) {
       aSelLab.ForgetAllAttributes(true);
       TDataStd_UAttribute::Set(aSelLab, kSIMPLE_REF_ID);
     } else {
-      selectBody(aContextBody, theSubShape);
+      selectBody(aContextResult, theSubShape);
     }
   } else if (theContext->groupName() == ModelAPI_ResultConstruction::group()) {
-    ResultConstructionPtr aContextConstruction =
-      std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theContext);
     aSelLab.ForgetAllAttributes(true); // to remove old selection data
     std::shared_ptr<Model_ResultConstruction> aConstruction =
       std::dynamic_pointer_cast<Model_ResultConstruction>(theContext);
     std::shared_ptr<GeomAPI_Shape> aSubShape;
-    if (theSubShape.get() && !aContextConstruction->shape()->isEqual(theSubShape))
+    if (theSubShape.get() && !aConstruction->shape()->isEqual(theSubShape))
       aSubShape = theSubShape; // the whole context
     if (aConstruction->isInfinite()) {
       // For correct naming selection, put the shape into the naming structure.
-      // It seems sub-shapes are not needed: only this shape is (and can be ) selected.
+      // It seems sub-shapes are not needed: only this shape is (and can be) selected.
+      /*
       TNaming_Builder aBuilder(aSelLab);
-      aBuilder.Generated(aContextConstruction->shape()->impl<TopoDS_Shape>());
+      aBuilder.Generated(aConstruction->shape()->impl<TopoDS_Shape>());
+      std::string anInfinitiveName = contextName(aConstruction);
+      TDataStd_Name::Set(aSelLab, anInfinitiveName.c_str());
+      std::dynamic_pointer_cast<Model_Document>(owner()->document())
+        ->addNamingName(aSelLab, anInfinitiveName.c_str());
+        */
     }
-    int anIndex = aConstruction->select(theSubShape, owner()->document());
-    TDataStd_Integer::Set(aSelLab, anIndex);
   } else if (theContext->groupName() == ModelAPI_ResultPart::group()) {
     aSelLab.ForgetAllAttributes(true);
     TDataStd_UAttribute::Set(aSelLab, kPART_REF_ID);
     selectPart(std::dynamic_pointer_cast<ModelAPI_Result>(theContext), theSubShape);
+  } else { // check the feature context: parent-Part of this feature should not be used
+    FeaturePtr aFeatureContext = std::dynamic_pointer_cast<ModelAPI_Feature>(theContext);
+    if (aFeatureContext.get()) {
+      if (owner()->document() != aFeatureContext->document()) {
+        aSelLab.ForgetAllAttributes(true);
+        myRef.setValue(ObjectPtr());
+        if (aToUnblock)
+          owner()->data()->blockSendAttributeUpdated(false);
+        return false;
+      }
+    }
   }
 
   owner()->data()->sendAttributeUpdated(this);
@@ -319,8 +344,8 @@ std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::internalValue(CenterTyp
   if (myRef.isInitialized()) {
     if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // it is just reference to shape, not sub-shape
       ResultPtr aContext = context();
-      if (!aContext.get())
-        return aResult; // empty result
+      if (!aContext.get() || aContext->groupName() == ModelAPI_ResultConstruction::group())
+        return aResult; // empty result, for whole construction selection also
       return aContext->shape();
     }
     if (aSelLab.IsAttribute(kPART_REF_ID)) {
@@ -353,20 +378,26 @@ std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::internalValue(CenterTyp
       if (aConstr->isInfinite())
         return aResult; // empty result
     }
-    // whole feature
-    FeaturePtr aFeature = contextFeature();
-    if (aFeature.get()) {
-      std::list<GeomShapePtr> allShapes;
-      std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
-      for (; aRes != aFeature->results().cend(); aRes++) {
-        if (aRes->get() && !(*aRes)->isDisabled()) {
-          GeomShapePtr aShape = (*aRes)->shape();
-          if (aShape.get() && !aShape->isNull()) {
-            allShapes.push_back(aShape);
+    if (!aConstr.get()) { // for construction context, return empty result as usual even
+      // the whole feature is selected
+      FeaturePtr aFeature = contextFeature();
+      if (aFeature.get()) {
+        std::list<GeomShapePtr> allShapes;
+        std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
+        for (; aRes != aFeature->results().cend(); aRes++) {
+          if (aRes->get() && !(*aRes)->isDisabled()) {
+            GeomShapePtr aShape = (*aRes)->shape();
+            if (aShape.get() && !aShape->isNull()) {
+              allShapes.push_back(aShape);
+            }
           }
         }
+        return GeomAlgoAPI_CompoundBuilder::compound(allShapes);
+      }
+    } else {
+      if (contextFeature().get()) {
+        return aResult; // for the whole sketch feature selected return null => all faces
       }
-      return GeomAlgoAPI_CompoundBuilder::compound(allShapes);
     }
 
     Handle(TNaming_NamedShape) aSelection;
@@ -375,12 +406,7 @@ std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::internalValue(CenterTyp
       aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
       aResult->setImpl(new TopoDS_Shape(aSelShape));
     } else if (aConstr) { // simple construction element: just shape of this construction element
-      Handle(TDataStd_Integer) anIndex;
-      if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
-        if (anIndex->Get() == 0) // it is just reference to construction, nothing is in value
-          return aResult;
-        return aConstr->shape(anIndex->Get(), owner()->document());
-      }
+      aResult = aConstr->shape();
     }
   }
   return aResult;
@@ -408,11 +434,7 @@ bool Model_AttributeSelection::isInitialized()
         std::shared_ptr<Model_ResultConstruction> aConstr =
           std::dynamic_pointer_cast<Model_ResultConstruction>(context());
         if (aConstr.get()) {
-          Handle(TDataStd_Integer) anIndex;
-          if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
-            // for the whole shape it may return null, so, if index exists, returns true
             return true;
-          }
         }
         // for the whole feature, a feature object
         FeaturePtr aFeat = contextFeature();
@@ -435,6 +457,10 @@ void Model_AttributeSelection::setID(const std::string theID)
 {
   myRef.setID(theID);
   ModelAPI_AttributeSelection::setID(theID);
+  FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
+  // TODO: check if parent list have geometrical selection flag.
+  myIsGeometricalSelection =
+    ModelAPI_Session::get()->validators()->isGeometricalSelection(aFeature->getKind(), id());
 }
 
 ResultPtr Model_AttributeSelection::context()
@@ -456,7 +482,7 @@ ResultPtr Model_AttributeSelection::context()
         if(aPart.get() && aPart->data() == aResult->data()) {
           ResultPtr aPartResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPart);
           FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
-          // check that this result is not this-feature result (it is forbidden t oselect itself)
+          // check that this result is not this-feature result (it is forbidden tselect itself)
           if(anOwnerFeature.get() && anOwnerFeature->firstResult() != aPartResult) {
             return aPartResult;
           }
@@ -479,10 +505,10 @@ FeaturePtr Model_AttributeSelection::contextFeature() {
   return std::dynamic_pointer_cast<ModelAPI_Feature>(myRef.value());
 }
 ObjectPtr Model_AttributeSelection::contextObject() {
-  ResultPtr aRes = context();
+  FeaturePtr aRes = contextFeature();
   if (aRes.get())
     return aRes;
-  return contextFeature();
+  return context();
 }
 
 
@@ -492,69 +518,6 @@ void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>&
   myRef.setObject(theObject);
 }
 
-TDF_LabelMap& Model_AttributeSelection::scope()
-{
-  if (myScope.IsEmpty()) { // create a new scope if not yet done
-    // gets all features with named shapes that are before this feature label (before in history)
-    DocumentPtr aMyDoc = owner()->document();
-    std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures = aMyDoc->allFeatures();
-    std::list<std::shared_ptr<ModelAPI_Feature> >::iterator aFIter = allFeatures.begin();
-    bool aMePassed = false;
-    CompositeFeaturePtr aComposite =
-      std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(owner());
-    FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
-    CompositeFeaturePtr aCompositeOwner, aCompositeOwnerOwner;
-    if (aFeature.get()) {
-      aCompositeOwner = ModelAPI_Tools::compositeOwner(aFeature);
-      if (aCompositeOwner.get()) {
-         aCompositeOwnerOwner = ModelAPI_Tools::compositeOwner(aCompositeOwner);
-      }
-    }
-    // for group Scope is not limitet: this is always up to date objects
-    // this causes problem in galeries.py
-    //bool isGroup = aFeature.get() && aFeature->getKind() == "Group";
-    for(; aFIter != allFeatures.end(); aFIter++) {
-      if (*aFIter == owner()) {  // the left features are created later (except subs of composite)
-        aMePassed = true;
-        continue;
-      }
-      //if (isGroup) aMePassed = false;
-      bool isInScope = !aMePassed;
-      if (!isInScope && aComposite.get()) {
-        // try to add sub-elements of composite if this is composite
-        if (aComposite->isSub(*aFIter))
-          isInScope = true;
-      }
-      // remove the composite-owner of this feature (sketch in extrusion-cut)
-      if (isInScope && (aCompositeOwner == *aFIter || aCompositeOwnerOwner == *aFIter))
-        isInScope = false;
-
-      if (isInScope && aFIter->get() && (*aFIter)->data()->isValid()) {
-        TDF_Label aFeatureLab = std::dynamic_pointer_cast<Model_Data>(
-          (*aFIter)->data())->label().Father();
-        TDF_ChildIDIterator aNSIter(aFeatureLab, TNaming_NamedShape::GetID(), true);
-        for(; aNSIter.More(); aNSIter.Next()) {
-          Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
-          if (!aNS.IsNull() && aNS->Evolution() != TNaming_SELECTED) {
-            myScope.Add(aNS->Label());
-          }
-        }
-      }
-    }
-    // also add all naming labels created for external constructions
-    std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(aMyDoc);
-    TDF_Label anExtDocLab = aDoc->extConstructionsLabel();
-    TDF_ChildIDIterator aNSIter(anExtDocLab, TNaming_NamedShape::GetID(), true);
-    for(; aNSIter.More(); aNSIter.Next()) {
-      Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aNSIter.Value());
-      if (!aNS.IsNull() && aNS->Evolution() != TNaming_SELECTED) {
-        myScope.Add(aNS->Label());
-      }
-    }
-  }
-  return myScope;
-}
-
 /// Sets the invalid flag if flag is false, or removes it if "true"
 /// Returns theFlag
 static bool setInvalidIfFalse(TDF_Label& theLab, const bool theFlag) {
@@ -596,6 +559,7 @@ bool Model_AttributeSelection::update()
 {
   FeaturePtr aContextFeature = contextFeature();
   if (aContextFeature.get()) {
+    owner()->data()->sendAttributeUpdated(this);  // send updated if "update" called in any way
     return true;
   }
   TDF_Label aSelLab = selectionLabel();
@@ -617,22 +581,30 @@ bool Model_AttributeSelection::update()
   }
 
   if (aContext->groupName() == ModelAPI_ResultBody::group()) {
-    // body: just a named shape, use selection mechanism from OCCT
-    TNaming_Selector aSelector(aSelLab);
+    // body: just a named shape, use topological selection mechanism
+    bool aResult = false;
     TopoDS_Shape anOldShape;
-    if (!aSelector.NamedShape().IsNull()) {
-      anOldShape = aSelector.NamedShape()->Get();
+    Handle(TNaming_NamedShape) aNS;
+    if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
+      anOldShape = aNS->Get();
+
+    Selector_Selector aSelector(aSelLab);
+    if (ModelAPI_Session::get()->moduleDocument() != owner()->document()) {
+      aSelector.setBaseDocument(std::dynamic_pointer_cast<Model_Document>
+        (ModelAPI_Session::get()->moduleDocument())->extConstructionsLabel());
+    }
+    if (aSelector.restore()) { // it is stored in old OCCT format, use TNaming_Selector
+      TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
+      aResult = aSelector.solve(aContextShape);
     }
-    bool aResult = aSelector.Solve(scope()) == Standard_True;
-    // must be before sending of updated attribute (1556)
     aResult = setInvalidIfFalse(aSelLab, aResult);
+
     TopoDS_Shape aNewShape;
-    if (!aSelector.NamedShape().IsNull()) {
-      aNewShape = aSelector.NamedShape()->Get();
-    }
-    if (anOldShape.IsNull() || aNewShape.IsNull() ||
-        !anOldShape.IsEqual(aSelector.NamedShape()->Get())) {
-      // shape type shoud not not changed: if shape becomes compound of such shapes, then split
+    if (aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
+      aNewShape = aNS->Get();
+
+    if (anOldShape.IsNull() || aNewShape.IsNull() || !anOldShape.IsEqual(aNewShape)) {
+      // shape type should not be changed: if shape becomes compound of such shapes, then split
       if (myParent && !anOldShape.IsNull() && !aNewShape.IsNull() &&
           anOldShape.ShapeType() != aNewShape.ShapeType() &&
           (aNewShape.ShapeType() == TopAbs_COMPOUND || aNewShape.ShapeType() == TopAbs_COMPSOLID))
@@ -645,22 +617,29 @@ bool Model_AttributeSelection::update()
   }
 
   if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
-    Handle(TDataStd_Integer) anIndex;
-    if (aSelLab.FindAttribute(TDataStd_Integer::GetID(), anIndex)) {
-      std::shared_ptr<Model_ResultConstruction> aConstructionContext =
-        std::dynamic_pointer_cast<Model_ResultConstruction>(aContext);
-      bool aModified = true;
-      bool aValid = aConstructionContext->update(anIndex->Get(), owner()->document(), aModified);
-      setInvalidIfFalse(aSelLab, aValid);
-      if (aConstructionContext->isInfinite()) {
-        // Update the selected shape.
-        TNaming_Builder aBuilder(aSelLab);
-        aBuilder.Generated(aConstructionContext->shape()->impl<TopoDS_Shape>());
+    bool aResult = true;
+    std::shared_ptr<Model_ResultConstruction> aConstructionContext =
+      std::dynamic_pointer_cast<Model_ResultConstruction>(aContext);
+    if (!aConstructionContext->isInfinite()) {
+      Selector_Selector aSelector(aSelLab);
+      if (ModelAPI_Session::get()->moduleDocument() != owner()->document()) {
+        aSelector.setBaseDocument(std::dynamic_pointer_cast<Model_Document>
+          (ModelAPI_Session::get()->moduleDocument())->extConstructionsLabel());
+      }
+
+      aResult = aSelector.restore();
+      TopoDS_Shape anOldShape = aSelector.value();
+      if (aResult) {
+        TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
+        aResult = aSelector.solve(aContextShape);
       }
-      if (aModified)
-        owner()->data()->sendAttributeUpdated(this);
-      return aValid;
+      aResult = setInvalidIfFalse(aSelLab, aResult);
+      if (aResult && !anOldShape.IsEqual(aSelector.value()))
+        owner()->data()->sendAttributeUpdated(this);  // send updated if shape is changed
+    } else {
+      owner()->data()->sendAttributeUpdated(this);  // send updated if "update" called in any way
     }
+    return aResult;
   }
   return setInvalidIfFalse(aSelLab, false); // unknown case
 }
@@ -669,10 +648,9 @@ void Model_AttributeSelection::selectBody(
   const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
 {
   // perform the selection
-  TNaming_Selector aSel(selectionLabel());
   TopoDS_Shape aContext;
 
-  ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theContext);//myRef.value()
+  ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(theContext);
   if (aBody) {
     aContext = aBody->shape()->impl<TopoDS_Shape>();
   } else {
@@ -686,84 +664,34 @@ void Model_AttributeSelection::selectBody(
     }
   }
 
-  // with "recover" feature the selected context may be not up to date (issue 1710)
-  Handle(TNaming_NamedShape) aResult;
-  TDF_Label aSelLab = selectionLabel();
-  TopoDS_Shape aNewContext = aContext;
-  bool isUpdated = true;
-  while(!aNewContext.IsNull() && isUpdated) {
-    // searching for the very last shape that was produced from this one
-    isUpdated = false;
-    if (!TNaming_Tool::HasLabel(aSelLab, aNewContext))
-      // to avoid crash of TNaming_SameShapeIterator if pure shape does not exists
-      break;
-    for(TNaming_SameShapeIterator anIter(aNewContext, aSelLab); anIter.More(); anIter.Next()) {
-      TDF_Label aNSLab = anIter.Label();
-      if (!scope().Contains(aNSLab))
-        continue;
-      Handle(TNaming_NamedShape) aNS;
-      if (aNSLab.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
-        for(TNaming_Iterator aShapesIter(aNS); aShapesIter.More(); aShapesIter.Next()) {
-          if (aShapesIter.Evolution() == TNaming_SELECTED)
-            continue; // don't use the selection evolution
-          if (!aShapesIter.OldShape().IsNull() && aShapesIter.OldShape().IsSame(aNewContext)) {
-             // found the original shape
-            aNewContext = aShapesIter.NewShape(); // go to the newer shape
-            isUpdated = true;
-            break;
-          }
-        }
-      }
-    }
-  }
-  if (aNewContext.IsNull()) { // a context is already deleted
-    setInvalidIfFalse(aSelLab, false);
-    Events_InfoMessage("Model_AttributeSelection", "Failed to select shape already deleted").send();
-    return;
-  }
+  if (!aContext.IsNull()) {
+    TDF_Label aSelLab = selectionLabel();
+    TopoDS_Shape aNewSub = theSubShape->impl<TopoDS_Shape>();
 
-  TopoDS_Shape aNewSub = theSubShape ? theSubShape->impl<TopoDS_Shape>() : aContext;
-  if (!aNewSub.IsEqual(aContext)) { // searching for subshape in the new context
-    bool isFound = false;
-    TopExp_Explorer anExp(aNewContext, aNewSub.ShapeType());
-    for(; anExp.More(); anExp.Next()) {
-      if (anExp.Current().IsSame(aNewSub)) {
-        isFound = true;
-        break;
-      }
+    bool aSelectorOk = true;
+    Selector_Selector aSel(aSelLab);
+    if (ModelAPI_Session::get()->moduleDocument() != owner()->document()) {
+      aSel.setBaseDocument(std::dynamic_pointer_cast<Model_Document>
+        (ModelAPI_Session::get()->moduleDocument())->extConstructionsLabel());
     }
-    if (!isFound) { // sub-shape is not found in the up-to-date instance of the context shape
-      // if context is sub-result of compound/compsolid, selection of sub-shape better propagate to
-      // the main result (which is may be modified); the case is in 1799
-      ResultBodyPtr aMain = ModelAPI_Tools::bodyOwner(theContext);
-      while(ModelAPI_Tools::bodyOwner(aMain).get())
-        aMain = ModelAPI_Tools::bodyOwner(theContext);
-      if (aMain.get()) {
-        selectBody(aMain, theSubShape);
-        return;
+    try {
+      aSelectorOk = aSel.select(aContext, aNewSub);
+      if (aSelectorOk) {
+        aSel.store();
+        aSelectorOk = aSel.solve(aContext);
       }
-      setInvalidIfFalse(aSelLab, false);
-      Events_InfoMessage("Model_AttributeSelection",
-        "Failed to select sub-shape already modified").send();
-      return;
+    } catch(...) {
+      aSelectorOk = false;
     }
-  }
-
-  /// fix for issue 411: result modified shapes must not participate in this selection mechanism
-  if (!aContext.IsNull()) {
-    FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
-    bool aEraseResults = false;
-    if (aFeatureOwner.get()) {
-      aEraseResults = !aFeatureOwner->results().empty();
-      if (aEraseResults) // erase results without flash deleted and redisplay: do it after Select
-        aFeatureOwner->removeResults(0, false, false);
+    Handle(TNaming_NamedShape) aSelectorShape;
+    if (aSelectorOk && aSelLab.FindAttribute(TNaming_NamedShape::GetID(), aSelectorShape))
+    {
+      TopoDS_Shape aShape = aSelectorShape->Get();
+      if (aShape.IsNull() || aShape.ShapeType() != aNewSub.ShapeType())
+        aSelectorOk = false;
     }
-    aSel.Select(aNewSub, aNewContext);
-
-    if (aEraseResults) { // flash after Select : in Groups it makes selection with shift working
-      static Events_Loop* aLoop = Events_Loop::loop();
-      static const Events_ID kDeletedEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
-      aLoop->flush(kDeletedEvent);
+    if (!aSelectorOk) {
+      setInvalidIfFalse(aSelLab, false);
     }
   }
 }
@@ -846,9 +774,39 @@ std::string Model_AttributeSelection::namingName(const std::string& theDefaultNa
     return "";
   }
 
-  Model_SelectionNaming aSelNaming(selectionLabel());
-  std::string aResult = aSelNaming.namingName(
-    aCont, aSubSh, theDefaultName, owner()->document() != aCont->document());
+  TDF_Label aSelLab = selectionLabel();
+  if (aSelLab.IsAttribute(kSIMPLE_REF_ID)) { // whole context, no value
+    return contextName(aCont);
+  }
+
+  // if it is in result of another part
+  if (aCont->groupName() == ModelAPI_ResultPart::group()) {
+    ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCont);
+    int anIndex;
+    GeomShapePtr aValue = value();
+    if (aValue.get())
+      return aPart->data()->name() + "/" + aPart->nameInPart(aValue, anIndex);
+    else
+      return aPart->data()->name();
+  }
+
+
+  // whole infinitive construction
+  if (aCont->groupName() == ModelAPI_ResultConstruction::group()) {
+    ResultConstructionPtr aConstr = std::dynamic_pointer_cast<Model_ResultConstruction>(aCont);
+    if (aConstr->isInfinite()) {
+      return contextName(aCont);
+    }
+  }
+
+  Selector_Selector aSelector(aSelLab);
+  if (ModelAPI_Session::get()->moduleDocument() != owner()->document()) {
+    aSelector.setBaseDocument(std::dynamic_pointer_cast<Model_Document>
+      (ModelAPI_Session::get()->moduleDocument())->extConstructionsLabel());
+  }
+  std::string aResult;
+  if (aSelector.restore())
+    aResult = aSelector.name(this);
   if (aCenterType != NOT_CENTER) {
     aResult += centersMap()[aCenterType];
   }
@@ -881,169 +839,192 @@ void Model_AttributeSelection::selectSubShape(
   CenterType aCenterType = theType[0] == 'v' || theType[0] == 'V' ? // only for vertex-type
     centerTypeByName(aSubShapeName) : NOT_CENTER;
   std::string aType = aCenterType == NOT_CENTER ? theType : "EDGE"; // search for edge now
+  static const GeomShapePtr anEmptyShape;
 
   // first iteration is selection by name without center prefix, second - in case of problem,
   // try with initial name
-  for(int aUseCenter = 1; aUseCenter >= 0; aUseCenter--) {
+  for(int aUseCenter = 1; aUseCenter >= 0; aUseCenter--)  {
     if (aUseCenter == 0 && aCenterType != NOT_CENTER) {
       aSubShapeName = theSubShapeName;
       aCenterType = NOT_CENTER;
       aType = theType;
     } else if (aUseCenter != 1) continue;
+    TopAbs_ShapeEnum aShapeType =  TopAbs_ShapeEnum(GeomAPI_Shape::shapeTypeByStr(aType));
 
+    std::shared_ptr<Model_Document> aDoc =
+      std::dynamic_pointer_cast<Model_Document>(owner()->document());
     // check this is Part-name: 2 delimiters in the name
     std::size_t aPartEnd = aSubShapeName.find('/');
-    if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
+    if (aPartEnd != std::string::npos) {
       std::string aPartName = aSubShapeName.substr(0, aPartEnd);
-      ObjectPtr aFound = owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
-      if (aFound.get()) { // found such part, so asking it for the name
-        ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
-        std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
-        int anIndex;
-        std::shared_ptr<GeomAPI_Shape> aSelected = aPart->shapeInPart(aNameInPart, aType, anIndex);
-        if (aSelected.get()) {
-          if (aCenterType != NOT_CENTER) {
-            if (!aSelected->isEdge())
-              continue;
-            std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aSelected));
-            setValueCenter(aPart, aSelectedEdge, aCenterType);
-          } else
-            setValue(aPart, aSelected);
-          TDataStd_Integer::Set(selectionLabel(), anIndex);
-          return;
+      DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
+      if (aPartName == aRootDoc->kind()) {
+        aDoc = std::dynamic_pointer_cast<Model_Document>(aRootDoc);
+        aSubShapeName = aSubShapeName.substr(aPartEnd + 1);
+      } else {
+        ObjectPtr aFound =
+          owner()->document()->objectByName(ModelAPI_ResultPart::group(), aPartName);
+        if (aFound.get()) { // found such part, so asking it for the name
+          ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFound);
+          std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
+          if (aNameInPart.empty()) { // whole part
+            setValue(aPart, anEmptyShape);
+            return;
+          }
+          int anIndex;
+          std::shared_ptr<GeomAPI_Shape> aSelected =
+            aPart->shapeInPart(aNameInPart, aType, anIndex);
+          if (aSelected.get()) {
+            if (aCenterType != NOT_CENTER) {
+              if (!aSelected->isEdge())
+                continue;
+              std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aSelected));
+              setValueCenter(aPart, aSelectedEdge, aCenterType);
+            } else
+              setValue(aPart, aSelected);
+            TDataStd_Integer::Set(selectionLabel(), anIndex);
+            return;
+          }
         }
       }
     }
 
-    std::shared_ptr<Model_Document> aDoc =
-      std::dynamic_pointer_cast<Model_Document>(owner()->document());
     // check this is a whole feature context
     if (aSubShapeName.size() > kWHOLE_FEATURE.size() &&
       aSubShapeName.substr(0, kWHOLE_FEATURE.size()) == kWHOLE_FEATURE) {
       std::string aFeatureName = aSubShapeName.substr(kWHOLE_FEATURE.size());
       ObjectPtr anObj = aDoc->objectByName(ModelAPI_Feature::group(), aFeatureName);
       if (anObj.get()) {
-        static const GeomShapePtr anEmptyShape;
         setValue(anObj, anEmptyShape);
         return;
       }
     }
 
-    Model_SelectionNaming aSelNaming(selectionLabel());
-    std::shared_ptr<GeomAPI_Shape> aShapeToBeSelected;
-    ResultPtr aCont;
-    if (aSelNaming.selectSubShape(aType, aSubShapeName, aDoc, aShapeToBeSelected, aCont)) {
-      // try to find the last context to find the up to date shape
-      if (aCont->shape().get() && !aCont->shape()->isNull() &&
-        aCont->groupName() == ModelAPI_ResultBody::group() && aDoc == owner()->document()) {
-        const TopoDS_Shape aConShape = aCont->shape()->impl<TopoDS_Shape>();
-        if (!aConShape.IsNull()) {
-          Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
-          if (!aNS.IsNull()) {
-            aNS = TNaming_Tool::CurrentNamedShape(aNS);
-            if (!aNS.IsNull() && scope().Contains(aNS->Label())) { // scope check is for 2228
-              TDF_Label aLab = aNS->Label();
-              if (aLab.Depth() % 2 == 0)
-                aLab = aLab.Father();
-              ObjectPtr anObj = aDoc->objects()->object(aLab);
-              while(!anObj.get() && aLab.Depth() > 5) {
-                aLab = aLab.Father().Father();
-                anObj = aDoc->objects()->object(aLab);
-              }
-
-              if (anObj.get()) {
-                ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
-                if (aRes)
-                  aCont = aRes;
-              }
-            }
-          }
-        }
+    // the whole result selection check
+    if (aSubShapeName.find('/') == std::string::npos) {
+      ObjectPtr aRes = aDoc->objectByName(ModelAPI_ResultConstruction::group(), aSubShapeName);
+      if (!aRes.get())
+        aRes = aDoc->objectByName(ModelAPI_ResultBody::group(), aSubShapeName);
+      if (aRes.get()) {
+        setValue(aRes, anEmptyShape);
+        return;
       }
-      // if compsolid is context, try to take sub-solid as context: like in GUI and scripts
-      if (aCont.get() && aShapeToBeSelected.get()) {
-        ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aCont);
-        if (aComp && aComp->numberOfSubs()) {
-          std::list<ResultPtr> allSubs;
-          ModelAPI_Tools::allSubs(aComp, allSubs);
-          std::list<ResultPtr>::iterator aS = allSubs.begin();
-          for(; aS != allSubs.end(); aS++) {
-            ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aS);
-            if (aSub && aSub->numberOfSubs() == 0 && aSub->shape().get() &&
-                aSub->shape()->isSubShape(aShapeToBeSelected)) {
-              aCont = aSub;
-              break;
-            }
+    }
+
+    Selector_Selector aSelector(aDoc->generalLabel());
+    if (ModelAPI_Session::get()->moduleDocument() != owner()->document()) {
+      aSelector.setBaseDocument(std::dynamic_pointer_cast<Model_Document>
+        (ModelAPI_Session::get()->moduleDocument())->extConstructionsLabel());
+    }
+    myRestoreDocument = aDoc;
+    TDF_Label aContextLabel = aSelector.restoreByName(aSubShapeName, aShapeType, this);
+    myRestoreDocument.reset();
+    if (!aContextLabel.IsNull()) {
+      ResultPtr aContext = aDoc->resultByLab(aContextLabel); // any label for document access
+      if (aContext.get() && aContext->shape().get()) {
+        TopoDS_Shape aContextShape = aContext->shape()->impl<TopoDS_Shape>();
+        if (aSelector.solve(aContextShape)) {
+          TopoDS_Shape aSelectorShape = aSelector.value();
+          GeomShapePtr aShapeToBeSelected(new GeomAPI_Shape);
+          aShapeToBeSelected->setImpl<TopoDS_Shape>(new TopoDS_Shape(aSelectorShape));
+          // make the context result the latest existing
+          aContext = newestContext(aContext, aShapeToBeSelected);
+          if (aCenterType != NOT_CENTER) {
+            if (!aShapeToBeSelected->isEdge())
+              continue;
+            std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aShapeToBeSelected));
+            setValueCenter(aContext, aSelectedEdge, aCenterType);
           }
+          else
+            setValue(aContext, aShapeToBeSelected);
+          return;
         }
       }
-      // try to find the latest active result that must be used instead of the selected
-      // to set the active context (like in GUI selection), not concealed one
-      bool aFindNewContext = true;
-      while(aFindNewContext && aCont.get()) {
-        aFindNewContext = false;
-        // take references to all results: root one, any sub
-        ResultBodyPtr aCompContext = ModelAPI_Tools::bodyOwner(aCont, true);
-        std::list<ResultPtr> allRes;
-        if (aCompContext.get()) {
-          ModelAPI_Tools::allSubs(aCompContext, allRes);
-          allRes.push_back(aCompContext);
-        } else {
-          allRes.push_back(aCont);
-        }
-        for(std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
-          ResultPtr aResCont = *aSub;
-          ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont);
-          // only lower and higher level subs are counted
-          if (aResBody.get() && aResBody->numberOfSubs() > 0 && aResBody != aCompContext)
-            continue;
-          const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
-          std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
-          for(; !aFindNewContext && aRef != aRefs.end(); aRef++) {
-            if (!aRef->get() || !(*aRef)->owner().get())
-              continue;
-            // concealed attribute only
-            FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
-            if (!ModelAPI_Session::get()->validators()->isConcealed(
-              aRefFeat->getKind(), (*aRef)->id()))
-              continue;
-            // search the feature result that contains sub-shape selected
-            std::list<std::shared_ptr<ModelAPI_Result> > aResults;
-
-            // take all sub-results or one result
-            std::list<ResultPtr> aRefFeatResults;
-            ModelAPI_Tools::allResults(aRefFeat, aRefFeatResults);
-            std::list<ResultPtr>::iterator aRefResIter = aRefFeatResults.begin();
-            for(; aRefResIter != aRefFeatResults.end(); aRefResIter++) {
-              ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefResIter);
-              if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
-                aResults.push_back(aBody);
-            }
-            std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = aResults.begin();
-            for(; aResIter != aResults.end(); aResIter++) {
-              if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
-                continue;
-              GeomShapePtr aShape = (*aResIter)->shape();
-              GeomShapePtr aSelectedShape =
-                aShapeToBeSelected.get() ? aShapeToBeSelected : aCont->shape();
-              if (aShape.get() && aShape->isSubShape(aSelectedShape, false)) {
-                aCont = *aResIter; // found new context (produced from this) with same subshape
-                aFindNewContext = true; // continue searching futher
-                break;
-              }
-            }
-          }
+    }
+  }
+  // invalid
+  TDF_Label aSelLab = selectionLabel();
+  setInvalidIfFalse(aSelLab, false);
+  reset();
+}
+
+void Model_AttributeSelection::selectSubShape(const std::string& theType,
+                                              const GeomPointPtr& thePoint)
+{
+  if (theType.empty() || !thePoint)
+    return;
+
+  // list of parent features
+  FeaturePtr anOwner = ModelAPI_Feature::feature(owner());
+  std::set<FeaturePtr> aParents = ModelAPI_Tools::getParents(anOwner);
+
+  int aSelectionIndex = 0;
+  GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(theType);
+  if (aType == GeomAPI_Shape::SHAPE) {
+    // possibly, the string consists of the type and the index,
+    // thus, try to separate them
+    size_t aUndersporePos = theType.find_first_of('_');
+    if (aUndersporePos != std::string::npos)
+      aType = GeomAPI_Shape::shapeTypeByStr(theType.substr(0, aUndersporePos));
+
+    if (aType != GeomAPI_Shape::SHAPE) {
+      for (std::string::const_iterator aChar = theType.begin() + aUndersporePos + 1;
+           aChar != theType.end(); ++aChar) {
+        if (std::isdigit(*aChar))
+          aSelectionIndex = aSelectionIndex * 10 + (*aChar - '0');
+        else {
+          aSelectionIndex = 1;
+          break;
         }
       }
+      aSelectionIndex -= 1;
+    }
+  }
 
-      if (aCenterType != NOT_CENTER) {
-        if (!aShapeToBeSelected->isEdge())
-          continue;
-        std::shared_ptr<GeomAPI_Edge> aSelectedEdge(new GeomAPI_Edge(aShapeToBeSelected));
-        setValueCenter(aCont, aSelectedEdge, aCenterType);
-      } else
-        setValue(aCont, aShapeToBeSelected);
-      return;
+  std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anAppropriate;
+
+  // collect features from PartSet and the current part
+  SessionPtr aSession = ModelAPI_Session::get();
+  std::list<FeaturePtr> aFeatures = aSession->moduleDocument()->allFeatures();
+  if (aSession->moduleDocument() != owner()->document()) {
+    std::list<FeaturePtr> aPartFeatures = owner()->document()->allFeatures();
+    aFeatures.insert(aFeatures.end(), aPartFeatures.begin(), aPartFeatures.end());
+  }
+  // Process results of all features from the last to the first
+  // to find appropriate sub-shape
+  for (std::list<FeaturePtr>::const_reverse_iterator anIt = aFeatures.rbegin();
+       anIt != aFeatures.rend(); ++anIt) {
+    // selection cannot be linked to the parent features
+    if (aParents.find(*anIt) != aParents.end())
+      continue;
+    // check the feature is a part of composite feature (like sketch elements),
+    // then do not process it, it will be processed in scope of composite feature
+    bool isSubOfComposite = false;
+    const std::set<AttributePtr>& aRefs = (*anIt)->data()->refsToMe();
+    for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
+         aRefIt != aRefs.end() && !isSubOfComposite; ++aRefIt) {
+      FeaturePtr aFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
+      CompositeFeaturePtr aCompFeature =
+          std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
+      isSubOfComposite = aCompFeature && aCompFeature->isSub(*anIt);
+    }
+    if (isSubOfComposite)
+      continue;
+
+    // process results of the current feature to find appropriate sub-shape
+    if (ModelGeomAlgo_Shape::findSubshapeByPoint(*anIt, thePoint, aType, anAppropriate)) {
+      std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anAppropriate.begin();
+      for (; aSelectionIndex > 0 && anApIt != anAppropriate.end(); --aSelectionIndex)
+        ++anApIt; // skip this shape, because one of the previous is selected
+
+      if (anApIt != anAppropriate.end()) {
+        if (anApIt->myCenterType == (int)ModelAPI_AttributeSelection::NOT_CENTER)
+          setValue(anApIt->myResult, anApIt->mySubshape);
+        else
+          setValueCenter(anApIt->myResult, anApIt->mySubshape->edge(),
+                         (ModelAPI_AttributeSelection::CenterType)anApIt->myCenterType);
+        return;
+      }
     }
   }
 
@@ -1052,6 +1033,23 @@ void Model_AttributeSelection::selectSubShape(
   reset();
 }
 
+void Model_AttributeSelection::selectSubShape(const std::string& theType,
+  const std::string& theContextName, const int theIndex)
+{
+  // selection of context by name
+  selectSubShape(theType, theContextName);
+  ResultPtr aContext = context();
+  if (aContext.get()) {
+    GeomShapePtr aContShape = aContext->shape();
+    if (aContShape.get()) {
+      GeomAlgoAPI_NExplode aNExp(aContShape, GeomAPI_Shape::shapeTypeByStr(theType));
+      GeomShapePtr aValue = aNExp.shape(theIndex);
+      if (aValue.get())
+        setValue(aContext, aValue);
+    }
+  }
+}
+
 int Model_AttributeSelection::Id()
 {
   int anID = 0;
@@ -1146,16 +1144,9 @@ void Model_AttributeSelection::computeValues(
 {
   bool aWasWholeContext = theValShape.IsNull();
   if (aWasWholeContext) {
-    //theShapes.Append(theValShape);
-    //return;
     theValShape = theOldContext->shape()->impl<TopoDS_Shape>();
   }
-  //TopoDS_Shape anOldContShape = theOldContext->shape()->impl<TopoDS_Shape>();
   TopoDS_Shape aNewContShape = theNewContext->shape()->impl<TopoDS_Shape>();
-  //if (anOldContShape.IsSame(theValShape)) { // full context shape substituted by new full context
-    //theShapes.Append(aNewContShape);
-    //return;
-  //}
   // if a new value is unchanged in the new context, do nothing: value is correct
   TopExp_Explorer aSubExp(aNewContShape, theValShape.ShapeType());
   for(; aSubExp.More(); aSubExp.Next()) {
@@ -1189,7 +1180,7 @@ void Model_AttributeSelection::computeValues(
     for(; aNewContIter != aNewToIterate.end(); aNewContIter++) {
       std::shared_ptr<Model_Data> aNewData =
         std::dynamic_pointer_cast<Model_Data>((*aNewContIter)->data());
-      TDF_Label aNewLab = aNewData->label();
+      TDF_Label aNewLab = aNewData->shapeLab();
       // searching for produced sub-shape fully on some label
       TDF_ChildIDIterator aNSIter(aNewLab, TNaming_NamedShape::GetID(), Standard_True);
       for(; aNSIter.More(); aNSIter.Next()) {
@@ -1222,7 +1213,7 @@ void Model_AttributeSelection::computeValues(
     }
   }
   if (aToFindPart == 2 && !aNewToOld.IsEmpty()) {
-    // map of sub-shapes -> number of occurences of these shapes in containers
+    // map of sub-shapes -> number of occurrences of these shapes in containers
     NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher> aSubs;
     TopTools_DataMapOfShapeShape::Iterator aContIter(aNewToOld);
     for(; aContIter.More(); aContIter.Next()) {
@@ -1283,33 +1274,31 @@ bool Model_AttributeSelection::searchNewContext(std::shared_ptr<Model_Document>
       if (!aModifierFeat.get())
         continue;
       FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
-      if (aModifierFeat == aThisFeature || theDoc->objects()->isLater(aModifierFeat, aThisFeature))
+      if (aModifierFeat == aThisFeature || !theDoc->isLaterByDep(aThisFeature, aModifierFeat))
         continue; // the modifier feature is later than this, so, should not be used
       FeaturePtr aCurrentModifierFeat = theDoc->feature(theContext);
       if (aCurrentModifierFeat == aModifierFeat ||
-        theDoc->objects()->isLater(aCurrentModifierFeat, aModifierFeat))
+        !theDoc->isLaterByDep(aModifierFeat, aCurrentModifierFeat))
         continue; // the current modifier is later than the found, so, useless
       Handle(TNaming_NamedShape) aNewNS;
       aModifIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNewNS);
       if (aNewNS->Evolution() == TNaming_MODIFY || aNewNS->Evolution() == TNaming_GENERATED) {
         aResults.insert(aModifierObj);
-        //TNaming_Iterator aPairIter(aNewNS);
-        //aResContShapes.Append(aPairIter.NewShape());
         aResContShapes.Append(aModifierObj->shape()->impl<TopoDS_Shape>());
       } else if (aNewNS->Evolution() == TNaming_DELETE) { // a shape was deleted => result is empty
         aResults.insert(ResultPtr());
-      } else { // not-precessed modification => don't support it
+      } else { // not-processed modification => don't support it
         continue;
       }
     }
   }
   if (aResults.empty())
     return false; // no modifications found, must stay the same
-  // iterate all results to find futher modifications
+  // iterate all results to find further modifications
   std::set<ResultPtr>::iterator aResIter = aResults.begin();
   for(; aResIter != aResults.end(); aResIter++) {
     if (aResIter->get() != NULL) {
-      // compute new values by two contextes: the old and the new
+      // compute new values by two contexts: the old and the new
       TopTools_ListOfShape aValShapes;
       computeValues(theContext, *aResIter, theValShape, aValShapes);
 
@@ -1324,7 +1313,7 @@ bool Model_AttributeSelection::searchNewContext(std::shared_ptr<Model_Document>
         if (searchNewContext(theDoc, aNewContShape, *aResIter, aNewValSh,
                              theAccessLabel, aNewRes, aNewUpdatedVal))
         {
-          // appeand new results instead of the current ones
+          // append new results instead of the current ones
           std::list<ResultPtr>::iterator aNewIter = aNewRes.begin();
           TopTools_ListIteratorOfListOfShape aNewUpdVal(aNewUpdatedVal);
           for(; aNewIter != aNewRes.end(); aNewIter++, aNewUpdVal.Next()) {
@@ -1353,7 +1342,7 @@ void Model_AttributeSelection::updateInHistory()
   std::shared_ptr<Model_Data> aContData = std::dynamic_pointer_cast<Model_Data>(aContext->data());
   if (!aContData.get() || !aContData->isValid())
     return;
-  TDF_Label aContLab = aContData->label(); // named shape where the selected context is located
+  TDF_Label aContLab = aContData->shapeLab(); // named shape where the selected context is located
   Handle(TNaming_NamedShape) aContNS;
   if (!aContLab.FindAttribute(TNaming_NamedShape::GetID(), aContNS)) {
     bool aFoundNewContext = true;
@@ -1370,13 +1359,14 @@ void Model_AttributeSelection::updateInHistory()
             // to avoid detection of part changes by local selection only
             AttributeSelectionPtr aSel =
               std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRef);
-            if (aSel.get() && !aSel->value()->isSame(aSel->context()->shape()))
+            if (aSel.get() && aSel->value().get() &&
+                !aSel->value()->isSame(aSel->context()->shape()))
               continue;
 
             FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
             if (aRefFeat.get() && aRefFeat != owner()) {
               FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
-              if (aDoc->objects()->isLater(aThisFeature, aRefFeat)) { // found better feature
+              if (!aDoc->isLaterByDep(aRefFeat, aThisFeature)) { // found better feature
                 aFoundNewContext = true;
                 aNewContext = aRefFeat->firstResult();
               }
@@ -1413,9 +1403,6 @@ void Model_AttributeSelection::updateInHistory()
   TopTools_ListOfShape aValShapes;
   if (searchNewContext(aDoc, aNewCShape, aContext, aValShape, aContLab, aNewContexts, aValShapes))
   {
-    // update scope to reset to a new one
-    myScope.Clear();
-
     std::list<ResultPtr>::iterator aNewCont = aNewContexts.begin();
     TopTools_ListIteratorOfListOfShape aNewValues(aValShapes);
     if (aNewCont == aNewContexts.end()) { // all results were deleted
@@ -1468,3 +1455,308 @@ void Model_AttributeSelection::setParent(Model_AttributeSelectionList* theParent
 {
   myParent = theParent;
 }
+
+std::string Model_AttributeSelection::contextName(const TDF_Label theSelectionLab)
+{
+  std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
+    std::dynamic_pointer_cast<Model_Document>(owner()->document());
+  FeaturePtr aFeatureOwner = aDoc->featureByLab(theSelectionLab);
+  bool aBaseDocumnetUsed = false;
+  if (!aFeatureOwner.get()) { // use module document
+    aDoc = std::dynamic_pointer_cast<Model_Document>(ModelAPI_Session::get()->moduleDocument());
+    aFeatureOwner = aDoc->featureByLab(theSelectionLab);
+    aBaseDocumnetUsed = true;
+  }
+  if (aFeatureOwner.get()) {
+    // if it is sub-element of the sketch, the context name is the name of the sketch
+    // searching also for result - real context
+    ResultPtr aResult;
+    FeaturePtr aComposite = ModelAPI_Tools::compositeOwner(aFeatureOwner);
+    if (aComposite.get() && aComposite->results().size() == 1 &&
+        aComposite->firstResult()->groupName() == ModelAPI_ResultConstruction::group()) {
+      aFeatureOwner = aComposite;
+      aResult = aFeatureOwner->firstResult();
+    } else {
+      aResult = aDoc->resultByLab(theSelectionLab);
+    }
+    if (aResult.get()) {
+      // this is to avoid duplicated names of results problem
+      std::string aContextName = aResult->data()->name();
+      // myLab corresponds to the current time
+      TDF_Label aCurrentLab = selectionLabel();
+      while(aCurrentLab.Depth() > 3)
+        aCurrentLab = aCurrentLab.Father();
+
+      int aNumInHistoryNames =
+        aDoc->numberOfNameInHistory(aResult, aCurrentLab);
+      while(aNumInHistoryNames > 1) { // add "_" before name the needed number of times
+        aContextName = "_" + aContextName;
+        aNumInHistoryNames--;
+      }
+      if (aBaseDocumnetUsed)
+        aContextName = aDoc->kind() + "/" + aContextName;
+      return aContextName;
+    }
+  }
+  return ""; // invalid case
+}
+
+/// This method restores by the context and value name the context label and
+/// sub-label where the value is. Returns true if it is valid.
+bool Model_AttributeSelection::restoreContext(std::string theName,
+  TDF_Label& theContext, TDF_Label& theValue)
+{
+  static const GeomShapePtr anEmptyShape; // to store context only
+  std::string aName = theName;
+  std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
+    std::dynamic_pointer_cast<Model_Document>(owner()->document());
+
+  // remove the sub-value part if exists
+  std::string aSubShapeName = aName;
+  std::string::size_type n = aName.find('/');
+  if (n != std::string::npos) {
+    aName = aName.substr(0, n);
+  }
+
+  if (aName.empty()) return false;
+  bool anUniqueContext = false;
+  ResultPtr aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
+  if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull()) {
+    // name in PartSet?
+    aDoc = std::dynamic_pointer_cast<Model_Document>(
+      ModelAPI_Session::get()->moduleDocument());
+    if (theName.find(aDoc->kind()) == 0) { // remove the document identifier from name if exists
+      aSubShapeName = theName.substr(aDoc->kind().size() + 1);
+      aName = aSubShapeName;
+      std::string::size_type n = aName.find('/');
+      if (n != std::string::npos) {
+        aName = aName.substr(0, n);
+      }
+    }
+    aCont = aDoc->findByName(aName, aSubShapeName, anUniqueContext);
+    if (!aCont.get() || !aCont->shape().get() || aCont->shape()->isNull())
+      return false;
+  }
+
+  // searching the sub-shape
+  static const ResultPtr anEmpty;
+  theValue = aDoc->findNamingName(aSubShapeName, anUniqueContext ? aCont : anEmpty);
+
+  // sketch sub-component shape and name is located in separated feature label, try the sub-name
+  if (theValue.IsNull() && aCont->groupName() == ModelAPI_ResultConstruction::group()) {
+    std::string::size_type aSlash = aSubShapeName.rfind('/');
+    if (aSlash != std::string::npos) {
+      std::string aCompName = aSubShapeName.substr(aSlash + 1);
+      CompositeFeaturePtr aComposite =
+        std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aDoc->feature(aCont));
+      if (aComposite.get() && aComposite->numberOfSubs()) {
+        const int aSubNum = aComposite->numberOfSubs();
+        for (int a = 0; a < aSubNum && theValue.IsNull(); a++) {
+          FeaturePtr aSub = aComposite->subFeature(a);
+          const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
+          std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
+          for (; aRes != aResults.cend() && theValue.IsNull(); aRes++) {
+            if ((*aRes)->data()->name() == aCompName) {
+              theValue = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
+              break;
+            } else { // any sub-label because the sketch line may be renamed, but not sub-vertices
+              TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>((*aRes)->data())->shapeLab();
+              TDF_ChildIDIterator aSubNames(aLab, TDataStd_Name::GetID());
+              for(; aSubNames.More(); aSubNames.Next()) {
+                if (Handle(TDataStd_Name)::DownCast(aSubNames.Value())->Get().
+                  IsEqual(aCompName.c_str())) {
+                  theValue = aSubNames.Value()->Label();
+                  break;
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+
+  if (aCont.get()) {
+    theContext = std::dynamic_pointer_cast<Model_Data>(aCont->data())->label();
+  }
+  return true;
+}
+
+TDF_Label Model_AttributeSelection::newestContext(const TDF_Label theCurrentContext) {
+  std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
+    std::dynamic_pointer_cast<Model_Document>(owner()->document());
+  ResultPtr aContext = aDoc->resultByLab(theCurrentContext);
+  if (aContext.get()) {
+    aContext = newestContext(aContext, GeomShapePtr(), true);
+    if (aContext.get())
+      return std::dynamic_pointer_cast<Model_Data>(aContext->data())->label();
+  }
+  return theCurrentContext; // nothing is changed
+}
+
+bool Model_AttributeSelection::isLater(
+  const TDF_Label theResult1, const TDF_Label theResult2) const
+{
+  std::shared_ptr<Model_Document> aDoc = myRestoreDocument.get() ? myRestoreDocument :
+    std::dynamic_pointer_cast<Model_Document>(owner()->document());
+  FeaturePtr aFeat1 = aDoc->featureByLab(theResult1);
+  if (!aFeat1.get())
+    return false;
+  FeaturePtr aFeat2 = aDoc->featureByLab(theResult2);
+  if (!aFeat2.get())
+    return false;
+  return aDoc->isLaterByDep(aFeat1, aFeat2);
+}
+
+ResultPtr Model_AttributeSelection::newestContext(
+  const ResultPtr theCurrent, const GeomShapePtr theValue, const bool theAnyValue)
+{
+  ResultPtr aResult = theCurrent;
+  GeomShapePtr aSelectedShape = theValue.get() ? theValue : theCurrent->shape();
+  std::shared_ptr<Model_Document> aDoc =
+    std::dynamic_pointer_cast<Model_Document>(owner()->document());
+  bool aFindNewContext = true;
+  while (aFindNewContext && aResult.get()) {
+    aFindNewContext = false;
+    // try to find the last context to find the up to date shape
+    TopoDS_Shape aConShape = aResult->shape()->impl<TopoDS_Shape>();
+    if (TNaming_Tool::HasLabel(selectionLabel(), aConShape)) {
+      Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aConShape, selectionLabel());
+      if (!aNS.IsNull()) {
+        aNS = TNaming_Tool::CurrentNamedShape(aNS);
+        if (!aNS.IsNull()
+            && isLater(selectionLabel(), aNS->Label())
+            && isLater(aNS->Label(),
+                       std::dynamic_pointer_cast<Model_Data>(aResult->data())->label()))
+        {
+          TDF_Label aLab = aNS->Label();
+          ResultPtr aRes = aDoc->resultByLab(aLab);
+          if (aRes.get()) {
+            if (theAnyValue || aRes->shape()->isSubShape(aSelectedShape)) {
+              aResult = aRes;
+              aFindNewContext = true;
+              continue;
+            }
+          }
+        }
+      }
+    }
+    if (theAnyValue) { // only for neighbors for now
+      // try to find modification of sub-shapes: the best number of matches
+      std::map<ResultPtr, int> aMatches; // result -> number of matches of shapes to find the best
+      TDF_Label aResLab = std::dynamic_pointer_cast<Model_Data>(aResult->data())->shapeLab();
+      TDF_ChildIDIterator aModifIter(aResLab, TNaming_NamedShape::GetID());
+      for(; aModifIter.More(); aModifIter.Next()) {
+        Handle(TNaming_NamedShape) aNS = Handle(TNaming_NamedShape)::DownCast(aModifIter.Value());
+        if (aNS->Evolution() == TNaming_MODIFY || aNS->Evolution() == TNaming_GENERATED) {
+          for(TNaming_Iterator aNSIter(aNS); aNSIter.More(); aNSIter.Next()) {
+            TNaming_NewShapeIterator aNewIter(aNSIter.NewShape(), aNS->Label());
+            for(; aNewIter.More(); aNewIter.Next()) {
+              TDF_Label aLab = aNewIter.Label();
+              if (isLater(aLab, aNS->Label()) && isLater(selectionLabel(), aLab)) {
+                ResultPtr aRes = aDoc->resultByLab(aLab);
+                if (aRes.get()) {
+                  if (aMatches.find(aRes) == aMatches.end())
+                    aMatches[aRes] = 0;
+                  aMatches[aRes]++; // found result, add matches
+                }
+              }
+            }
+          }
+        }
+      }
+      // searching for the best result-candidate
+      int aBest = 0;
+      ResultPtr aBestResult;
+      std::map<ResultPtr, int>::iterator aMatchIter = aMatches.begin();
+      for(; aMatchIter != aMatches.end(); aMatchIter++) {
+        if (aMatchIter->second > aBest) {
+          aBest = aMatchIter->second;
+          aBestResult = aMatchIter->first;
+        }
+      }
+      if (aBestResult.get()) {
+        aResult = aBestResult;
+        aFindNewContext = true;
+        continue;
+      }
+    }
+
+
+    // TestFillWireVertex.py - sketch constructions for wire may participate too
+    //if (aResult->groupName() == ModelAPI_ResultBody::group()) {
+      // try to search newer context by the concealment references
+      // take references to all results: root one, any sub
+    std::list<ResultPtr> allRes;
+    ResultPtr aCompContext;
+    ResultBodyPtr aCompBody = ModelAPI_Tools::bodyOwner(aResult, true);
+    if (aCompBody.get()) {
+      ModelAPI_Tools::allSubs(aCompBody, allRes);
+      allRes.push_back(aCompBody);
+      aCompContext = aCompBody;
+    }
+    if (allRes.empty())
+      allRes.push_back(aResult);
+
+    for (std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
+      ResultPtr aResCont = *aSub;
+      ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont);
+      if (aResBody.get() && aResBody->numberOfSubs() > 0 && aResBody != aCompContext)
+        continue; // only lower and higher level subs are counted
+      const std::set<AttributePtr>& aRefs = aResCont->data()->refsToMe();
+      std::set<AttributePtr>::const_iterator aRef = aRefs.begin();
+      for (; !aFindNewContext && aRef != aRefs.end(); aRef++) {
+        if (!aRef->get() || !(*aRef)->owner().get())
+          continue;
+        // concealed attribute only
+        FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
+        if (!ModelAPI_Session::get()->validators()->isConcealed(
+          aRefFeat->getKind(), (*aRef)->id()))
+          continue;
+        // search the feature result that contains sub-shape selected
+        std::list<std::shared_ptr<ModelAPI_Result> > aResults;
+
+        // take all sub-results or one result
+        std::list<ResultPtr> aRefFeatResults;
+        ModelAPI_Tools::allResults(aRefFeat, aRefFeatResults);
+        std::list<ResultPtr>::iterator aRefResIter = aRefFeatResults.begin();
+        for (; aRefResIter != aRefFeatResults.end(); aRefResIter++) {
+          ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRefResIter);
+          if (aBody.get() && aBody->numberOfSubs() == 0) // add only lower level subs
+            aResults.push_back(aBody);
+        }
+        std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = aResults.begin();
+        for (; aResIter != aResults.end(); aResIter++) {
+          if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
+            continue;
+          GeomShapePtr aShape = (*aResIter)->shape();
+          if (aShape.get() && (theAnyValue || aShape->isSubShape(aSelectedShape, false))) {
+            aResult = *aResIter; // found new context (produced from this) with same subshape
+            aFindNewContext = true; // continue searching further
+            break;
+          }
+        }
+      }
+    }
+  }
+  // if compsolid is context, try to take sub-solid as context: like in GUI and scripts
+  ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
+  if (aComp && aComp->numberOfSubs()) {
+    std::list<ResultPtr> allSubs;
+    ModelAPI_Tools::allSubs(aComp, allSubs);
+    std::list<ResultPtr>::iterator aS = allSubs.begin();
+    for (; aS != allSubs.end(); aS++) {
+      ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aS);
+      if (aSub && aSub->numberOfSubs() == 0 && aSub->shape().get() &&
+        (theAnyValue || aSub->shape()->isSubShape(aSelectedShape))) {
+        aResult = aSub;
+        break;
+      }
+    }
+  }
+  // in case sketch line was selected for wire, but wire was concealed and not such line anymore,
+  // so, actually, the sketch element was selected (which is never concealed)
+  if (aResult != theCurrent && aResult->isConcealed())
+    aResult = theCurrent;
+  return aResult;
+}