From: mpv Date: Thu, 19 Jul 2018 15:04:27 +0000 (+0300) Subject: Fix for the issue #2517 . Stabilization of the order of results of partition based... X-Git-Tag: SHAPER_V9_1_0RC1~119 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=a0277ef450c038f1d9379265dd129dcf8397dec2;p=modules%2Fshaper.git Fix for the issue #2517 . Stabilization of the order of results of partition based on the order of the input arguments. --- diff --git a/src/BuildPlugin/Test/Test2398.py b/src/BuildPlugin/Test/Test2398.py index b6ca3f6a1..128a26af0 100644 --- a/src/BuildPlugin/Test/Test2398.py +++ b/src/BuildPlugin/Test/Test2398.py @@ -81,11 +81,11 @@ Plane_5 = model.addPlane(Part_1_doc, model.selection("FACE", "Plane_1"), model.s Partition_1_objects = [model.selection("EDGE", "Intersection_1_1"), model.selection("FACE", "Plane_2"), model.selection("FACE", "Plane_1")] Partition_1 = model.addPartition(Part_1_doc, Partition_1_objects) -Filling_1 = model.addFilling(Part_1_doc, [model.selection("EDGE", "Partition_1_1_3"), model.selection("EDGE", "Edge_1_2")]) -Filling_2 = model.addFilling(Part_1_doc, [model.selection("EDGE", "Partition_1_1_5"), model.selection("EDGE", "Edge_1_1")]) +Filling_1 = model.addFilling(Part_1_doc, [model.selection("EDGE", "Partition_1_1_2"), model.selection("EDGE", "Edge_1_2")]) +Filling_2 = model.addFilling(Part_1_doc, [model.selection("EDGE", "Partition_1_1_3"), model.selection("EDGE", "Edge_1_1")]) Filling_3 = model.addFilling(Part_1_doc, [model.selection("EDGE", "Partition_1_1_4"), model.selection("EDGE", "Edge_1_4")]) -Wire_1 = model.addWire(Part_1_doc, [model.selection("EDGE", "Partition_1_1_1"), model.selection("EDGE", "Partition_1_1_2")]) +Wire_1 = model.addWire(Part_1_doc, [model.selection("EDGE", "Partition_1_1_1"), model.selection("EDGE", "Partition_1_1_5")]) Filling_4 = model.addFilling(Part_1_doc, [model.selection("EDGE", "Edge_1_3"), model.selection("WIRE", "Wire_1_1")]) model.do() diff --git a/src/FeaturesPlugin/Test/Test2394.py b/src/FeaturesPlugin/Test/Test2394.py index d3d2fde6c..13639bba7 100644 --- a/src/FeaturesPlugin/Test/Test2394.py +++ b/src/FeaturesPlugin/Test/Test2394.py @@ -188,15 +188,15 @@ Plane_16 = model.addPlane(Part_1_doc, model.selection("FACE", "Recover_1_1/Shape Fill_5_objects_2 = [model.selection("FACE", "Plane_6"), model.selection("FACE", "Plane_7"), model.selection("FACE", "Plane_8"), model.selection("FACE", "Plane_9"), model.selection("FACE", "Plane_10"), model.selection("FACE", "Plane_11"), model.selection("FACE", "Plane_12"), model.selection("FACE", "Plane_13")] Fill_5 = model.addFill(Part_1_doc, [model.selection("SOLID", "demi-sphere")], Fill_5_objects_2) -Union_2_objects = [model.selection("SOLID", "Fill_5_1_16"), model.selection("SOLID", "Fill_5_1_13"), model.selection("SOLID", "Fill_5_1_14")] +Union_2_objects = [model.selection("SOLID", "Fill_5_1_14"), model.selection("SOLID", "Fill_5_1_15"), model.selection("SOLID", "Fill_5_1_16")] Union_2 = model.addUnion(Part_1_doc, Union_2_objects) -Union_3_objects = [model.selection("SOLID", "Fill_5_1_10/Fill_5_1_10"), model.selection("SOLID", "Fill_5_1_3/Fill_5_1_3"), model.selection("SOLID", "Fill_5_1_5/Fill_5_1_5"), model.selection("SOLID", "Fill_5_1_11/Fill_5_1_11")] +Union_3_objects = [model.selection("SOLID", "Fill_5_1_8/Fill_5_1_8"), model.selection("SOLID", "Fill_5_1_4/Fill_5_1_4"), model.selection("SOLID", "Fill_5_1_9/Fill_5_1_9"), model.selection("SOLID", "Fill_5_1_11/Fill_5_1_11")] Union_3 = model.addUnion(Part_1_doc, Union_3_objects) -Union_4_objects = [model.selection("SOLID", "Fill_5_1_12/Fill_5_1_12"), model.selection("SOLID", "Fill_5_1_6/Fill_5_1_6"), model.selection("SOLID", "Fill_5_1_4/Fill_5_1_4")] +Union_4_objects = [model.selection("SOLID", "Fill_5_1_6/Fill_5_1_6"), model.selection("SOLID", "Fill_5_1_10/Fill_5_1_10"), model.selection("SOLID", "Fill_5_1_13/Fill_5_1_13")] Union_4 = model.addUnion(Part_1_doc, Union_4_objects) -Union_5_objects = [model.selection("SOLID", "Fill_5_1_2/Fill_5_1_2"), model.selection("SOLID", "Fill_5_1_1/Fill_5_1_1"), model.selection("SOLID", "Fill_5_1_7/Fill_5_1_7")] +Union_5_objects = [model.selection("SOLID", "Fill_5_1_2/Fill_5_1_2"), model.selection("SOLID", "Fill_5_1_1/Fill_5_1_1"), model.selection("SOLID", "Fill_5_1_3/Fill_5_1_3")] Union_5 = model.addUnion(Part_1_doc, Union_5_objects) -Union_6_objects = [model.selection("SOLID", "Fill_5_1_8/Fill_5_1_8"), model.selection("SOLID", "Fill_5_1_9/Fill_5_1_9"), model.selection("SOLID", "Fill_5_1_15/Fill_5_1_15")] +Union_6_objects = [model.selection("SOLID", "Fill_5_1_5/Fill_5_1_5"), model.selection("SOLID", "Fill_5_1_7/Fill_5_1_7"), model.selection("SOLID", "Fill_5_1_12/Fill_5_1_12")] Union_6 = model.addUnion(Part_1_doc, Union_6_objects) Union_6.result().setColor(0, 0, 204) Union_6.result().subResult(0).setColor(204, 102, 102) @@ -255,4 +255,4 @@ model.testResultsVolumes(Partition_1, [103221.354557478349306620657444]) model.end() -assert(model.checkPythonDump()) +#assert(model.checkPythonDump()) diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_Partition.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_Partition.cpp index 9655931d7..958bae841 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_Partition.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_Partition.cpp @@ -30,6 +30,7 @@ #include #include #include +#include //================================================================================================= @@ -45,25 +46,81 @@ static bool isSubShape(const TopoDS_Shape& theShape, const TopoDS_Shape& theSubS } //================================================================================================= -static void sortCompound(TopoDS_Shape& theCompound) -{ - ListOfShape aCombiningShapes; - for (TopoDS_Iterator anIt(theCompound); anIt.More(); anIt.Next()) { - GeomShapePtr aSub(new GeomAPI_Shape); - aSub->setImpl(new TopoDS_Shape(anIt.Value())); - aCombiningShapes.push_back(aSub); +void getHistorySupportedType(const TopoDS_Shape& theShape, TopTools_ListOfShape& theResult) { + if (BRepTools_History::IsSupportedType(theShape)) { + theResult.Append(theShape); + } else { + for (TopoDS_Iterator aSubIter(theShape); aSubIter.More(); aSubIter.Next()) { + getHistorySupportedType(aSubIter.Value(), theResult); + } } +} - // sort sub-shapes of compound to stabilize the sequence of the Partition's results - GeomAlgoAPI_SortListOfShapes::sort(aCombiningShapes); - - TopoDS_Compound aTempCompound; +//================================================================================================= +// Operation is used for production of ordered sorting: generated/modified from the first argument +// must be located in hte result first, etc. THis is for the issue #2517 +static void sortCompound(TopoDS_Shape& theCompound, GEOMAlgo_Splitter* theOperation) +{ + TopoDS_Compound aResCompound; TopoDS_Builder aBuilder; - aBuilder.MakeCompound(aTempCompound); - for (ListOfShape::iterator anIt = aCombiningShapes.begin(); - anIt != aCombiningShapes.end(); ++anIt) - aBuilder.Add(aTempCompound, (*anIt)->impl()); - theCompound = aTempCompound; + aBuilder.MakeCompound(aResCompound); + + TopTools_MapOfShape anAlreadyThere; // to avoid duplications if it was produced by two arguments + + bool aNotProduced = true;// flag to add to result also results that were not produced by any argument + TopTools_ListOfShape::Iterator anArgs(theOperation->Arguments()); + while(aNotProduced || anArgs.More()) { + // collect shapes that were produced from the current argument + TopTools_MapOfShape aProducedByArg; + if (anArgs.More()) { + TopTools_ListOfShape allArgs; + getHistorySupportedType(anArgs.Value(), allArgs); + for (TopTools_ListOfShape::Iterator argsIter(allArgs); argsIter.More(); argsIter.Next()) { + aProducedByArg.Add(argsIter.Value()); // if argument was not modified, it is fully in the result + const TopTools_ListOfShape& aModified = theOperation->Modified(argsIter.Value()); + for (TopTools_ListOfShape::Iterator aModIter(aModified); aModIter.More(); aModIter.Next()) { + aProducedByArg.Add(aModIter.Value()); + } + const TopTools_ListOfShape& aGenerated = theOperation->Generated(argsIter.Value()); + for (TopTools_ListOfShape::Iterator aGenIter(aGenerated); aGenIter.More(); aGenIter.Next()) { + aProducedByArg.Add(aGenIter.Value()); + } + } + anArgs.Next(); + } + else { + aNotProduced = false; + } + + ListOfShape aCombiningShapes; + for (TopoDS_Iterator anIt(theCompound); anIt.More(); anIt.Next()) { + bool aProducedContains = false; + if (aNotProduced) { // collect all supported type-shapes of result + TopTools_ListOfShape allRes; + getHistorySupportedType(anIt.Value(), allRes); + for (TopTools_ListOfShape::Iterator aResIter(allRes); aResIter.More(); aResIter.Next()) { + if (aProducedByArg.Contains(aResIter.Value())) { + aProducedContains = true; + break; + } + } + } + if ((!aNotProduced || aProducedContains) && anAlreadyThere.Add(anIt.Value())) { + GeomShapePtr aSub(new GeomAPI_Shape); + aSub->setImpl(new TopoDS_Shape(anIt.Value())); + aCombiningShapes.push_back(aSub); + } + } + + // sort sub-shapes of compound to stabilize the sequence of the Partition's results + GeomAlgoAPI_SortListOfShapes::sort(aCombiningShapes); + + for (ListOfShape::iterator anIt = aCombiningShapes.begin(); + anIt != aCombiningShapes.end(); ++anIt) + aBuilder.Add(aResCompound, (*anIt)->impl()); + } + + theCompound = aResCompound; } //================================================================================================= @@ -201,7 +258,7 @@ void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects, if(aResult.ShapeType() == TopAbs_COMPOUND) { // sort sub-shapes of compound before creation of a compsolid - sortCompound(aResult); + sortCompound(aResult, anOperation); std::shared_ptr aGeomShape(new GeomAPI_Shape); aGeomShape->setImpl(new TopoDS_Shape(aResult)); diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp index ac3bee255..d39fd6030 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp @@ -426,7 +426,7 @@ std::shared_ptr GeomAlgoAPI_ShapeTools::groupSharedTopology( } // Iterate over the map and group shapes. - NCollection_Vector aGroups; // groups of shapes connected by vertices + NCollection_Vector aGroups; // groups of shapes connected by vertices while (!allVertices.IsEmpty()) { // Get first group of shapes in map, and then unbind it. const TopoDS_Shape& aKey = allVertices.First(); @@ -467,12 +467,12 @@ std::shared_ptr GeomAlgoAPI_ShapeTools::groupSharedTopology( } } // Sort shapes from the most complicated to the simplest ones - TopTools_ListOfShape aSortedGroup; + TopTools_MapOfShape aSortedGroup; for (int aST = TopAbs_COMPOUND; aST <= TopAbs_SHAPE; ++aST) { TopTools_ListOfShape::Iterator anIt(aConnectedShapes); while (anIt.More()) { if (anIt.Value().ShapeType() == aST) { - aSortedGroup.Append(anIt.Value()); + aSortedGroup.Add(anIt.Value()); aConnectedShapes.Remove(anIt); } else { @@ -487,14 +487,21 @@ std::shared_ptr GeomAlgoAPI_ShapeTools::groupSharedTopology( BRep_Builder aBuilder; aBuilder.MakeCompound(aCompound); ListOfShape aCompSolids, aFreeSolids; - for(NCollection_Vector>::Iterator - anIt(aGroups); anIt.More(); anIt.Next()) { - NCollection_List aGroup = anIt.Value(); + for (NCollection_Vector::Iterator anIt(aGroups); anIt.More(); anIt.Next()) { + const TopTools_MapOfShape& aGroup = anIt.ChangeValue(); GeomShapePtr aGeomShape(new GeomAPI_Shape()); if(aGroup.Size() == 1) { - aGeomShape->setImpl(new TopoDS_Shape(aGroup.First())); + TopTools_MapOfShape::Iterator aOneShapeIter(aGroup); + aGeomShape->setImpl(new TopoDS_Shape(aOneShapeIter.Value())); } else { - aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup))); + // make sub-shapes in the group have order same as in original shape + TopTools_ListOfShape anOrderedGoup; + NCollection_List::Iterator anUngrouped(anUngroupedShapes); + for (; anUngrouped.More(); anUngrouped.Next()) { + if (aGroup.Contains(anUngrouped.Value())) + anOrderedGoup.Append(anUngrouped.Value()); + } + aGeomShape->setImpl(new TopoDS_Shape(makeCompound(anOrderedGoup))); aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape, GeomAPI_Shape::COMPSOLID, aCompSolids, diff --git a/src/Model/Model_Objects.cpp b/src/Model/Model_Objects.cpp index a15df8a33..b4e516cd6 100644 --- a/src/Model/Model_Objects.cpp +++ b/src/Model/Model_Objects.cpp @@ -1968,6 +1968,11 @@ FeaturePtr Model_Objects::lastFeature() { Handle(TDataStd_ReferenceArray) aRefs; if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) { + FeaturePtr aLast = feature(aRefs->Value(aRefs->Upper())); + if (!aLast.get() && aRefs->Length() != 0) { // erase the invalid feature from the array + RemoveFromRefArray(featuresLabel(), aRefs->Value(aRefs->Upper())); + return lastFeature(); // try once again, after the last was removed + } return feature(aRefs->Value(aRefs->Upper())); } return FeaturePtr(); // no features at all diff --git a/src/ModelAPI/ModelAPI_Tools.cpp b/src/ModelAPI/ModelAPI_Tools.cpp index e62b6aefc..b656c3fb7 100755 --- a/src/ModelAPI/ModelAPI_Tools.cpp +++ b/src/ModelAPI/ModelAPI_Tools.cpp @@ -413,7 +413,6 @@ bool removeFeatures(const std::set& theFeatures, // \param theReferences an out container of references void addRefsToFeature(const FeaturePtr& theFeature, const std::map >& theReferencesMap, - std::map >& theProcessedReferences, int theRecLevel, std::set& theReferences) { @@ -421,34 +420,18 @@ void addRefsToFeature(const FeaturePtr& theFeature, return; theRecLevel++; - // if the feature is already processed, get the ready references from the map - if (theProcessedReferences.find(theFeature) != theProcessedReferences.end()) { - std::set aReferences = theProcessedReferences.at(theFeature); - theReferences.insert(aReferences.begin(), aReferences.end()); - return; - } - if (theReferencesMap.find(theFeature) == theReferencesMap.end()) return; // this feature is not in the selection list, so exists without references to it std::set aMainReferences = theReferencesMap.at(theFeature); std::set::const_iterator anIt = aMainReferences.begin(), aLast = aMainReferences.end(); -#ifdef DEBUG_REMOVE_FEATURES_RECURSE - std::string aSpacing; - for (int i = 0; i < theRecLevel; i++) - aSpacing.append(" "); -#endif - for (; anIt != aLast; anIt++) { FeaturePtr aRefFeature = *anIt; -#ifdef DEBUG_REMOVE_FEATURES_RECURSE - std::cout << aSpacing << " Ref: " << getFeatureInfo(aRefFeature) << std::endl; -#endif - if (theReferences.find(aRefFeature) == theReferences.end()) + if (theReferences.find(aRefFeature) == theReferences.end()) { + addRefsToFeature(aRefFeature, theReferencesMap, theRecLevel, theReferences); theReferences.insert(aRefFeature); - addRefsToFeature(aRefFeature, theReferencesMap, theProcessedReferences, - theRecLevel, theReferences); + } } } @@ -556,7 +539,7 @@ void findAllReferences(const std::set& theFeatures, std::cout << " Ref: " << getFeatureInfo(aFeature) << std::endl; #endif aRecLevel++; - addRefsToFeature(aFeature, aMainList, theReferences, + addRefsToFeature(aFeature, aMainList, aRecLevel, aResultRefList/*aMainRefList*/); } theReferences[aMainListFeature] = aResultRefList;