]> SALOME platform Git repositories - modules/shaper.git/blobdiff - src/Model/Model_SelectionNaming.cpp
Salome HOME
Support of all types of selection in python names import
[modules/shaper.git] / src / Model / Model_SelectionNaming.cpp
index 1b1c51e9ec70901a076e22a19d15503fb263dae5..78c167e9f4628d7147a77ef3a4a16119f14a3d92 100644 (file)
@@ -30,6 +30,7 @@
 #include <ModelAPI_CompositeFeature.h>
 #include <ModelAPI_ResultBody.h>
 #include <GeomAPI_Wire.h>
+#include <GeomAPI_Edge.h>
 
 #include <TopoDS_Iterator.hxx>
 #include <TopoDS.hxx>
@@ -50,6 +51,7 @@
 #include <TNaming_SameShapeIterator.hxx>
 #include <TDataStd_Name.hxx>
 #include <TColStd_MapOfTransient.hxx>
+#include <Precision.hxx>
 #include <algorithm>
 #include <stdexcept>
 
@@ -256,46 +258,6 @@ const TopoDS_Shape findCommonShape(
   return aSharedShape;
 }
 
-// searches theType shape that contains theConnectionType sub-shapes in each shape from the List,
-// so, implements the neighbours searching
-/*
-const TopoDS_Shape findCommonShapeByNB(const TopAbs_ShapeEnum theType,
-  const TopAbs_ShapeEnum theConnectionType, const TopTools_ListOfShape& theList)
-{
-TopTools_MapOfShape aCheckedShapes; // already checked shapes of type theType
-  TopoDS_Shape aResult; // theType result shape
-  for(TopTools_ListIteratorOfListOfShape anIt(theList); anIt.More(); anIt.Next()) { // iterate all
-    for(TopExp_Explorer anExp(anIt.ChangeValue(), theType); anExp.More(); anExp.Next()) {
-      if (aCheckedShapes.Contains(anExp.Current()))
-        continue; // already checked
-      aCheckedShapes.Add(anExp.Current());
-      TopTools_MapOfShape aConnectors; // all connectors of the checked theType shape
-      for(TopExp_Explorer aCExp(anExp.Current(), theConnectionType); aCExp.More(); aCExp.Next()) {
-        aConnectors.Add(aCExp.Current());
-      }
-      // check that all shapes from the List contain the connector sub-shapes
-      bool aFound = true;
-      for(TopTools_ListIteratorOfListOfShape anIt2(theList); anIt2.More() && aFound; anIt2.Next()) {
-        if (anIt2.Value().IsSame(anIt.Value()))
-          continue;
-        aFound = false;
-        for(TopExp_Explorer anE(anIt2.ChangeValue(), theConnectionType); anE.More(); anE.Next()) {
-          if (aConnectors.Contains(anE.Current())) {
-            aFound = true;
-            break;
-          }
-        }
-      }
-      if (aFound) {
-        if (!aResult.IsNull()) // more than one result
-          return TopoDS_Shape();
-        aResult = anExp.Current();
-      }
-    }
-  }
-  return aResult;
-}*/
-
 std::string Model_SelectionNaming::vertexNameByEdges(TopoDS_Shape theContext, TopoDS_Shape theSub,
   std::shared_ptr<Model_Document> theDoc, ResultPtr& theContextRes, const bool theAnotherDoc)
 {
@@ -622,11 +584,11 @@ size_t ParseName(const std::string& theSubShapeName,   std::list<std::string>& t
 
 std::string getContextName(const std::string& theSubShapeName)
 {
-  std::string aName;
-  std::string::size_type n = theSubShapeName.find('/');
-  if (n == std::string::npos) return theSubShapeName;
-  aName = theSubShapeName.substr(0, n);
-  return aName;
+    std::string aName;
+    std::string::size_type n = theSubShapeName.find('/');
+    if (n == std::string::npos) return theSubShapeName;
+    aName = theSubShapeName.substr(0, n);
+    return aName;
 }
 
 /// Parses naming name of sketch sub-elements: takes indices and orientation
@@ -700,11 +662,70 @@ int Model_SelectionNaming::edgeOrientation(const TopoDS_Shape& theContext, TopoD
   return 0; // unknown
 }
 
+int Model_CurvesHasher::HashCode(const Handle(Geom_Curve)& theCurve, const Standard_Integer Upper)
+{
+  double aFirstParam = theCurve->FirstParameter();
+  if (aFirstParam < -1.e+100 || aFirstParam > 1.e+100)
+    aFirstParam = 0;
+  double aLastParam = theCurve->LastParameter();
+  if (aLastParam < -1.e+100 || aLastParam > 1.e+100)
+    aLastParam = 2;
+  else aLastParam = (aLastParam + aFirstParam) / 2.; // to avoid in periodic same first and last
+
+  gp_XYZ aCoordSum = theCurve->Value(aFirstParam).XYZ() + theCurve->Value(aLastParam).XYZ();
+  return ::HashCode(aCoordSum.X() + aCoordSum.Y() / 123. + aCoordSum.Z() / 123456., Upper);
+}
+bool Model_CurvesHasher::IsEqual(const Handle(Geom_Curve)& theC1, const Handle(Geom_Curve)& theC2)
+{
+  if (theC1->DynamicType() != theC2->DynamicType())
+    return false;
+  double aFirstParam1 = theC1->FirstParameter();
+  if (aFirstParam1 < -1.e+100 || aFirstParam1 > 1.e+100)
+    aFirstParam1 = 0;
+  double aFirstParam2 = theC2->FirstParameter();
+  if (aFirstParam2 < -1.e+100 || aFirstParam2 > 1.e+100)
+    aFirstParam2 = 0;
+  if (fabs(aFirstParam1 - aFirstParam2) > 1.e-9)
+    return false;
+
+  double aLastParam1 = theC1->LastParameter();
+  if (aLastParam1 < -1.e+100 || aLastParam1 > 1.e+100)
+    aLastParam1 = 2.;
+  else aLastParam1 = (aLastParam1 + aFirstParam1) / 2.; // to avoid in periodic same first and last
+  double aLastParam2 = theC2->LastParameter();
+  if (aLastParam2 < -1.e+100 || aLastParam2 > 1.e+100)
+    aLastParam2 = 2.;
+  else aLastParam2 = (aLastParam2 + aFirstParam2) / 2.; // to avoid in periodic same first and last
+
+  if (fabs(aLastParam1 - aLastParam2) > 1.e-9)
+    return false;
+
+  return theC1->Value(aFirstParam1).IsEqual(theC2->Value(aFirstParam2), Precision::Confusion()) &&
+    theC1->Value(aLastParam1).IsEqual(theC2->Value(aLastParam2), Precision::Confusion());
+}
+
+int Model_EdgesHasher::HashCode(const TopoDS_Edge& theEdge, const Standard_Integer Upper)
+{
+  Standard_Real aFirst, aLast;
+  Handle(Geom_Curve) aCurve = BRep_Tool::Curve(theEdge, aFirst, aLast);
+  return Model_CurvesHasher::HashCode(aCurve, Upper);
+}
+
+bool Model_EdgesHasher::IsEqual(const TopoDS_Edge& theE1, const TopoDS_Edge& theE2)
+{
+  GeomEdgePtr aSh1(new GeomAPI_Edge);
+  aSh1->setImpl(new TopoDS_Shape(theE1));
+  GeomEdgePtr aSh2(new GeomAPI_Edge);
+  aSh2->setImpl(new TopoDS_Shape(theE2));
+  return aSh1->isEqual(aSh2);
+}
+
 std::shared_ptr<GeomAPI_Shape> Model_SelectionNaming::findAppropriateFace(
   std::shared_ptr<ModelAPI_Result>& theConstr,
-  NCollection_DataMap<Handle(Geom_Curve), int>& theCurves, const bool theIsWire)
+  NCollection_DataMap<Handle(Geom_Curve), int, Model_CurvesHasher>& theCurves, const bool theIsWire)
 {
   int aBestFound = 0; // best number of found edges (not percentage: issue 1019)
+  int aBestNotFound = 1000000; // best number of not found edges (must be minimum)
   int aBestOrient = 0; // for the equal "BestFound" additional parameter is orientation
   std::shared_ptr<GeomAPI_Shape> aResult;
   ResultConstructionPtr aConstructionContext =
@@ -726,7 +747,7 @@ std::shared_ptr<GeomAPI_Shape> Model_SelectionNaming::findAppropriateFace(
     std::list<TopoDS_Shape>::iterator aFW = aFacesWires.begin();
     for(; aFW != aFacesWires.end(); aFW++) {
       TopExp_Explorer anEdgesExp(*aFW, TopAbs_EDGE);
-      TColStd_MapOfTransient alreadyProcessed; // to avoid counting edges with same curved (841)
+      TColStd_MapOfTransient alreadyProcessed; // to avoid counting edges with same curves (841)
       for(; anEdgesExp.More(); anEdgesExp.Next()) {
         TopoDS_Edge anEdge = TopoDS::Edge(anEdgesExp.Current());
         if (!anEdge.IsNull()) {
@@ -747,18 +768,29 @@ std::shared_ptr<GeomAPI_Shape> Model_SelectionNaming::findAppropriateFace(
           }
         }
       }
+      if (theIsWire && aFound + aNotFound != 0) {
+        if (aBestNotFound > aNotFound || (aBestNotFound == aNotFound && aFound > aBestFound) ||
+          (aBestNotFound == aNotFound && aFound == aBestFound && aSameOrientation > aBestOrient)) {
+          aBestFound = aFound;
+          aBestOrient = aSameOrientation;
+          aBestNotFound = aNotFound;
+          std::shared_ptr<GeomAPI_Wire> aWire(new GeomAPI_Wire);
+          aWire->setImpl(new TopoDS_Shape(*aFW));
+          aResult = aWire;
+        }
+        aFound = 0;
+        aNotFound = 0;
+        aSameOrientation = 0;
+      }
+    }
+    if (!theIsWire) {
       if (aFound + aNotFound != 0) {
-        if (aFound > aBestFound ||
-          (aFound == aBestFound && aSameOrientation > aBestOrient)) {
-            aBestFound = aFound;
-            aBestOrient = aSameOrientation;
-            if (theIsWire) {
-              std::shared_ptr<GeomAPI_Wire> aWire(new GeomAPI_Wire);
-              aWire->setImpl(new TopoDS_Shape(*aFW));
-              aResult = aWire;
-            } else {
-              aResult = aConstructionContext->face(aFaceIndex);
-            }
+        if (aBestNotFound > aNotFound || (aBestNotFound == aNotFound && aFound > aBestFound) ||
+          (aBestNotFound == aNotFound && aFound == aBestFound && aSameOrientation > aBestOrient)) {
+          aBestFound = aFound;
+          aBestOrient = aSameOrientation;
+          aBestNotFound = aNotFound;
+          aResult = aConstructionContext->face(aFaceIndex);
         }
       }
     }
@@ -774,6 +806,8 @@ std::string Model_SelectionNaming::shortName(
   aName.erase(std::remove(aName.begin(), aName.end(), '-'), aName.end());
   aName.erase(std::remove(aName.begin(), aName.end(), '/'), aName.end());
   aName.erase(std::remove(aName.begin(), aName.end(), '&'), aName.end());
+  if (aName.empty())
+    return "";
   // remove the last 's', 'e', 'f' and 'r' symbols:
   // they are used as markers of start/end/forward/rewersed indicators
   static const std::string aSyms("sefr");
@@ -1048,7 +1082,7 @@ bool Model_SelectionNaming::selectSubShape(const std::string& theType,
             return false;
 
           // curves and orientations of edges
-          NCollection_DataMap<Handle(Geom_Curve), int> allCurves;
+          NCollection_DataMap<Handle(Geom_Curve), int, Model_CurvesHasher> allCurves;
           const int aSubNum = aComposite->numberOfSubs();
           for(int a = 0; a < aSubNum; a++) {
             int aSubID = aComposite->subFeatureId(a);
@@ -1084,7 +1118,7 @@ bool Model_SelectionNaming::selectSubShape(const std::string& theType,
             return false;
 
            // curves and orientations of edges
-          NCollection_DataMap<Handle(Geom_Curve), int> allCurves;
+          NCollection_DataMap<Handle(Geom_Curve), int, Model_CurvesHasher> allCurves;
           const int aSubNum = aComposite->numberOfSubs();
           for(int a = 0; a < aSubNum; a++) {
             int aSubID = aComposite->subFeatureId(a);