Salome HOME
Merge remote-tracking branch 'remotes/origin/EDF_2020_Lot2'
[modules/shaper.git] / src / Model / Model_AttributeSelection.cpp
index 180fb85e58ac865b25f12a9dffd93be85e77cfe7..9219aca4b622c160400eeff90f99a628f7d03eda 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2020  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -148,8 +148,6 @@ bool Model_AttributeSelection::setValue(const ObjectPtr& theContext,
       isDegeneratedEdge = BRep_Tool::Degenerated(TopoDS::Edge(aSubShape)) == Standard_True;
   }
   if (!theContext.get() || isDegeneratedEdge) {
-    // to keep the reference attribute label
-    TDF_Label aRefLab = myRef.myRef->Label();
     aSelLab.ForgetAllAttributes(true);
     myRef.myRef = TDF_Reference::Set(aSelLab.Father(), aSelLab.Father());
     if (aToUnblock)
@@ -234,6 +232,8 @@ void Model_AttributeSelection::setValueCenter(
         anUpdated = !aSelLab.IsAttribute(kELLIPSE_CENTER2);
       TDataStd_UAttribute::Set(aSelLab, kELLIPSE_CENTER2);
       break;
+    default: // [to avoid compilation warning]
+      break;
     }
     if (anUpdated)
       owner()->data()->sendAttributeUpdated(this);
@@ -364,13 +364,13 @@ std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::internalValue(CenterTyp
         } else { // face with name is already in the data model, so try to take it by name
           Handle(TDataStd_Name) aName;
           if (aSelLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
-            std::string aSubShapeName(TCollection_AsciiString(aName->Get()).ToCString());
-            std::size_t aPartEnd = aSubShapeName.find('/');
-            if (aPartEnd != std::string::npos && aPartEnd != aSubShapeName.rfind('/')) {
-              std::string aNameInPart = aSubShapeName.substr(aPartEnd + 1);
-              int anIndex;
+            std::wstring aSubShapeName = Locale::Convert::toWString(aName->Get().ToExtString());
+            std::size_t aPartEnd = aSubShapeName.find(L'/');
+            if (aPartEnd != std::wstring::npos && aPartEnd != aSubShapeName.rfind(L'/')) {
+              std::wstring aNameInPart = aSubShapeName.substr(aPartEnd + 1);
+              int anInd;
               std::string aType; // to reuse already existing selection the type is not needed
-              return aPart->shapeInPart(Locale::Convert::toWString(aNameInPart), aType, anIndex);
+              return aPart->shapeInPart(aNameInPart, aType, anInd);
             }
           }
         }
@@ -559,7 +559,7 @@ void Model_AttributeSelection::split(
     aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
     setValue(theContext, aSubSh);
     for(aSub.Next(); aSub.More(); aSub.Next()) {
-      GeomShapePtr aSubSh(new GeomAPI_Shape);
+      aSubSh.reset(new GeomAPI_Shape);
       aSubSh->setImpl(new TopoDS_Shape(aSub.Value()));
       myParent->append(theContext, aSubSh);
     }
@@ -838,7 +838,7 @@ static ModelAPI_AttributeSelection::CenterType centerTypeByName(std::wstring& th
     centersMap().begin();
   for(; aPrefixIter != centersMap().end(); aPrefixIter++) {
     std::size_t aFound = theShapeName.find(aPrefixIter->second);
-    if (aFound != std::string::npos &&
+    if (aFound != std::wstring::npos &&
         aFound == theShapeName.size() - aPrefixIter->second.size()) {
       theShapeName = theShapeName.substr(0, aFound);
       return aPrefixIter->first;
@@ -872,8 +872,8 @@ void Model_AttributeSelection::selectSubShape(
     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) {
+    std::size_t aPartEnd = aSubShapeName.find(L'/');
+    if (aPartEnd != std::wstring::npos) {
       std::wstring aPartName = aSubShapeName.substr(0, aPartEnd);
       DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
       if (aPartName == Locale::Convert::toWString(aRootDoc->kind())) {
@@ -937,7 +937,7 @@ void Model_AttributeSelection::selectSubShape(
     }
 
     // the whole result selection check
-    if (aSubShapeName.find('/') == std::string::npos) {
+    if (aSubShapeName.find(L'/') == std::wstring::npos) {
       ObjectPtr aRes = aDoc->objectByName(ModelAPI_ResultConstruction::group(), aSubShapeName);
       if (!aRes.get()) {
         aRes = aDoc->objectByName(ModelAPI_ResultBody::group(), aSubShapeName);
@@ -1256,8 +1256,7 @@ void Model_AttributeSelection::computeValues(
     NCollection_DataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher> aSubs;
     TopTools_DataMapOfShapeShape::Iterator aContIter(aNewToOld);
     for(; aContIter.More(); aContIter.Next()) {
-      TopExp_Explorer aSubExp(aContIter.Key(), aValType);
-      for(; aSubExp.More(); aSubExp.Next()) {
+      for(aSubExp.Init(aContIter.Key(), aValType); aSubExp.More(); aSubExp.Next()) {
         if (!aSubs.IsBound(aSubExp.Current())) {
           aSubs.Bind(aSubExp.Current(), TopTools_MapOfShape());
         }
@@ -1322,7 +1321,8 @@ void Model_AttributeSelection::computeValues(
 
 void Model_AttributeSelection::concealedFeature(
   const FeaturePtr theFeature, const FeaturePtr theStop, const bool theCheckCopy,
-  std::list<FeaturePtr>& theConcealers, const ResultPtr theResultOfFeature)
+  std::list<FeaturePtr>& theConcealers, const ResultPtr theResultOfFeature,
+  const bool theCheckWholeFeature)
 {
   std::set<FeaturePtr> alreadyProcessed;
   alreadyProcessed.insert(theFeature);
@@ -1338,16 +1338,21 @@ void Model_AttributeSelection::concealedFeature(
   }
   std::list<ResultPtr>::const_iterator aRootIter = aRootRes.cbegin();
   for(; aRootIter != aRootRes.cend(); aRootIter++) {
-    std::list<ResultPtr> allRes;
-    allRes.push_back(*aRootIter);
+    std::list<DataPtr> allRes;
+    allRes.push_back((*aRootIter)->data());
     ResultBodyPtr aRootBody = ModelAPI_Tools::bodyOwner(*aRootIter, true);
     if (!aRootBody.get())
       aRootBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRootIter);
     if (aRootBody.get()) {
-      ModelAPI_Tools::allSubs(aRootBody, allRes);
-    }
-    for(std::list<ResultPtr>::iterator aRIter = allRes.begin(); aRIter != allRes.end(); aRIter++) {
-      const std::set<AttributePtr>& aRefs = (*aRIter)->data()->refsToMe();
+      std::list<ResultPtr> allSub;
+      ModelAPI_Tools::allSubs(aRootBody, allSub);
+      for(std::list<ResultPtr>::iterator anIt = allSub.begin(); anIt != allSub.end(); anIt++)
+        allRes.push_back((*anIt)->data());
+    }
+    if (theCheckWholeFeature)
+      allRes.push_back(theFeature->data());
+    for(std::list<DataPtr>::iterator aRIter = allRes.begin(); aRIter != allRes.end(); aRIter++) {
+      const std::set<AttributePtr>& aRefs = (*aRIter)->refsToMe();
       std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
       for (; aRef != aRefs.cend(); aRef++) {
         if (!aRef->get() || !(*aRef)->owner().get())
@@ -1694,7 +1699,6 @@ void Model_AttributeSelection::updateInHistory(bool& theRemove)
   if (!aPairIter.More())
     return;
   TopoDS_Shape aNewCShape = aPairIter.NewShape();
-  bool anIterate = true;
   // trying to update also the sub-shape selected
   GeomShapePtr aSubShape = value();
   if (aSubShape.get() && aSubShape->isEqual(aContext->shape()))
@@ -1708,10 +1712,8 @@ void Model_AttributeSelection::updateInHistory(bool& theRemove)
   TopTools_ListOfShape aValShapes;
   if (searchNewContext(aDoc, aNewCShape, aContext, aValShape, aContLab, aNewContexts, aValShapes))
   {
-    std::set<ResultPtr> allContexts, aSkippedContext;
-    std::list<ResultPtr>::iterator aNewContext = aNewContexts.begin();
-    for(; aNewContext != aNewContexts.end(); aNewContext++)
-      allContexts.insert(*aNewContext);
+    std::set<ResultPtr> allContexts(aNewContexts.begin(), aNewContexts.end());
+    std::set<ResultPtr> aSkippedContext;
 
     // if there exist context composite and sub-result(s), leave only sub(s)
     std::set<ResultPtr>::iterator aResIter = allContexts.begin();
@@ -1904,8 +1906,8 @@ bool Model_AttributeSelection::restoreContext(std::wstring theName,
 
   // remove the sub-value part if exists
   std::wstring aSubShapeName = aName;
-  std::string::size_type n = aName.find('/');
-  if (n != std::string::npos) {
+  std::wstring::size_type n = aName.find(L'/');
+  if (n != std::wstring::npos) {
     aName = aName.substr(0, n);
   }
 
@@ -1920,8 +1922,8 @@ bool Model_AttributeSelection::restoreContext(std::wstring theName,
       // 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) {
+      n = aName.find(L'/');
+      if (n != std::wstring::npos) {
         aName = aName.substr(0, n);
       }
     }
@@ -1936,8 +1938,8 @@ bool Model_AttributeSelection::restoreContext(std::wstring theName,
 
   // 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::wstring::size_type aSlash = aSubShapeName.rfind(L'/');
+    if (aSlash != std::wstring::npos) {
       std::wstring aCompName = aSubShapeName.substr(aSlash + 1);
       CompositeFeaturePtr aComposite =
         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aDoc->feature(aCont));
@@ -2037,6 +2039,7 @@ ResultPtr Model_AttributeSelection::newestContext(
     if (allRes.empty())
       allRes.push_back(aResult);
 
+    bool aFoundReferernce = false;
     for (std::list<ResultPtr>::iterator aSub = allRes.begin(); aSub != allRes.end(); aSub++) {
       ResultPtr aResCont = *aSub;
       ResultBodyPtr aResBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResCont);
@@ -2070,6 +2073,7 @@ ResultPtr Model_AttributeSelection::newestContext(
         for (; aResIter != aResults.end(); aResIter++) {
           if (!aResIter->get() || !(*aResIter)->data()->isValid() || (*aResIter)->isDisabled())
             continue;
+          aFoundReferernce = true;
           GeomShapePtr aShape = (*aResIter)->shape();
           if (aShape.get() && aShape->isSubShape(aSelectedShape, false)) {
             aResult = *aResIter; // found new context (produced from this) with same subshape
@@ -2079,6 +2083,11 @@ ResultPtr Model_AttributeSelection::newestContext(
         }
       }
     }
+    if (aFoundReferernce && !aFindNewContext &&
+        aResult->groupName() != ModelAPI_ResultConstruction::group()) {
+      // #19019 : result is concealed by object that contains no such sub-shape
+      return theCurrent;
+    }
   }
   // 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);
@@ -2095,10 +2104,16 @@ ResultPtr Model_AttributeSelection::newestContext(
       }
     }
   }
-  // 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;
+  // in case sketch line was selected for wire, but wire was concealed and there is no such line
+  // anymore, so, actually, the sketch element was selected (which is never concealed)
+  if (aResult != theCurrent && theCurrent->groupName() == ModelAPI_ResultConstruction::group()) {
+    std::list<FeaturePtr> aConcealers;
+    FeaturePtr aResFeature = aDoc->feature(aResult);
+    FeaturePtr aThisFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
+    concealedFeature(aResFeature, aThisFeature, false, aConcealers, aResult, true);
+    if (aConcealers.size())
+      aResult = theCurrent;
+  }
   return aResult;
 }