Salome HOME
Fix for the issue #2808 : Documentation on the "Groups" panel. Added description...
[modules/shaper.git] / src / Model / Model_AttributeSelection.cpp
index 3d97c91c9066df87325328677f41cbbe59989750..5b382b22100a121cc88f7ba40c9777ad6cdb37d2 100644 (file)
@@ -178,18 +178,6 @@ bool Model_AttributeSelection::setValue(const ObjectPtr& theContext,
     std::shared_ptr<GeomAPI_Shape> aSubShape;
     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.
-      /*
-      TNaming_Builder aBuilder(aSelLab);
-      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());
-        */
-    }
   } else if (theContext->groupName() == ModelAPI_ResultPart::group()) {
     aSelLab.ForgetAllAttributes(true);
     TDataStd_UAttribute::Set(aSelLab, kPART_REF_ID);
@@ -781,7 +769,7 @@ std::string Model_AttributeSelection::namingName(const std::string& theDefaultNa
 
   Selector_Selector aSelector(aSelLab, baseDocumentLab());
   std::string aResult;
-  if (aSelector.restore(aCont->shape()->impl<TopoDS_Shape>()))
+  if (aCont->shape().get() && aSelector.restore(aCont->shape()->impl<TopoDS_Shape>()))
     aResult = aSelector.name(this);
   if (aCenterType != NOT_CENTER) {
     aResult += centersMap()[aCenterType];
@@ -1703,14 +1691,44 @@ ResultPtr Model_AttributeSelection::newestContext(
             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 (theAnyValue) { // searching the best sub-result by maximum number of references to orig
+          int aReferencesCount = 0;
+          ResultPtr aBestResult;
+          for (; aResIter != aResults.end(); aResIter++) {
+            if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
+              continue;
+            TDF_Label aCandidateLab =
+              std::dynamic_pointer_cast<Model_Data>((*aResIter)->data())->shapeLab();
+            Handle(TDF_Reference) aRef;
+            if (aCandidateLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
+              TDF_Label aRefLab = aRef->Get();
+              ResultPtr aRefRes = aDoc->resultByLab(aRefLab);
+              if (aRefRes.get() && aRefRes->shape().get() &&
+                  aRefRes->shape()->isEqual(aResult->shape())) {// it directly references to result
+                aResult = *aResIter; // found new context (produced from this) with same subshape
+                aFindNewContext = true; // continue searching further
+                break;
+              }
+            } else {
+              if (!aBestResult.get())
+                aBestResult = *aResIter;
+            }
+          }
+          if (aBestResult.get() && !aFindNewContext) { // the first good result for now
+            aResult = aBestResult; // found new context
+            aFindNewContext = true;
+          }
+        } else { // searching by sub-shape
+          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;
+            }
           }
         }
       }