]> SALOME platform Git repositories - modules/geom.git/blobdiff - src/GEOMImpl/GEOMImpl_IShapesOperations.cxx
Salome HOME
Merge branch 'master' into V7_5_BR
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IShapesOperations.cxx
index 267e6e3169a9269b32b113384d7cd12fc756db24..75a2b46d4de397113a72d2c3417179e1e41b8f7a 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -42,7 +42,7 @@
 #include "GEOMImpl_Block6Explorer.hxx"
 #include "GEOMImpl_IHealingOperations.hxx"
 
-#include <GEOMImpl_Gen.hxx>
+#include "GEOMImpl_Gen.hxx"
 
 #include "GEOM_Function.hxx"
 #include "GEOM_ISubShape.hxx"
@@ -63,9 +63,9 @@
 
 #include <Basics_OCCTVersion.hxx>
 
-#include "utilities.h"
-#include "OpUtil.hxx"
-#include "Utils_ExceptHandlers.hxx"
+#include <utilities.h>
+#include <OpUtil.hxx>
+#include <Utils_ExceptHandlers.hxx>
 
 #include <TFunction_DriverTable.hxx>
 #include <TFunction_Driver.hxx>
@@ -197,9 +197,7 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeEdge
 
   //Compute the Edge value
   try {
-#if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
-#endif
     if (!GetSolver()->ComputeFunction(aFunction)) {
       SetErrorCode("Vector driver failed");
       return NULL;
@@ -258,9 +256,7 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeEdgeOnCurveByLength
 
   //Compute the Edge value
   try {
-#if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
-#endif
     if (!GetSolver()->ComputeFunction(aFunction)) {
       SetErrorCode("Vector driver failed");
       return NULL;
@@ -316,9 +312,7 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeEdgeWire
 
   //Compute the Edge value
   try {
-#if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
-#endif
     if (!GetSolver()->ComputeFunction(aFunction)) {
       SetErrorCode("Shape driver failed");
       return NULL;
@@ -392,9 +386,7 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeWire
 
   //Compute the shape
   try {
-#if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
-#endif
     if (!GetSolver()->ComputeFunction(aFunction)) {
       SetErrorCode("Shape driver failed");
       return NULL;
@@ -459,9 +451,7 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeFace (Handle(GEOM_Object) th
   //Compute the Face value
   Standard_Boolean isWarning = Standard_False;
   try {
-#if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
-#endif
     if (!GetSolver()->ComputeFunction(aFunction)) {
       SetErrorCode("Shape driver failed to compute a face");
       return NULL;
@@ -530,9 +520,7 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeFaceWires
   //Compute the shape
   Standard_Boolean isWarning = Standard_False;
   try {
-#if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
-#endif
     if (!GetSolver()->ComputeFunction(aFunction)) {
       SetErrorCode("Shape driver failed");
       return NULL;
@@ -643,9 +631,7 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeShape
 
   //Compute the shape
   try {
-#if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
-#endif
     if (!GetSolver()->ComputeFunction(aFunction)) {
       SetErrorCode("Shape driver failed");
       return NULL;
@@ -712,9 +698,7 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeGlueFaces
   //Compute the sub-shape value
   Standard_Boolean isWarning = Standard_False;
   try {
-#if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
-#endif
     if (!GetSolver()->ComputeFunction(aFunction)) {
       SetErrorCode("Shape driver failed to glue faces");
       return NULL;
@@ -869,9 +853,7 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeGlueFacesByList
   //Compute the sub-shape value
   Standard_Boolean isWarning = Standard_False;
   try {
-#if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
-#endif
     if (!GetSolver()->ComputeFunction(aFunction)) {
       SetErrorCode("Shape driver failed to glue faces");
       return NULL;
@@ -944,9 +926,7 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeGlueEdges
   //Compute the sub-shape value
   Standard_Boolean isWarning = Standard_False;
   try {
-#if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
-#endif
     if (!GetSolver()->ComputeFunction(aFunction)) {
       SetErrorCode("Shape driver failed to glue edges");
       return NULL;
@@ -1107,9 +1087,7 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeGlueEdgesByList
   //Compute the sub-shape value
   Standard_Boolean isWarning = Standard_False;
   try {
-#if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
-#endif
     if (!GetSolver()->ComputeFunction(aFunction)) {
       SetErrorCode("Shape driver failed to glue edges");
       return NULL;
@@ -1151,9 +1129,38 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeGlueEdgesByList
  *  GetExistingSubObjects
  */
 //=============================================================================
-Handle(TColStd_HSequenceOfTransient) GEOMImpl_IShapesOperations::GetExistingSubObjects
-                                          (Handle(GEOM_Object)    theShape,
-                                           const Standard_Boolean theGroupsOnly)
+Handle(TColStd_HSequenceOfTransient)
+GEOMImpl_IShapesOperations::GetExistingSubObjects(Handle(GEOM_Object)    theShape,
+                                                 const Standard_Boolean theGroupsOnly)
+{
+  // note: this method does not return fields
+
+  Standard_Integer types = theGroupsOnly ? Groups : Groups|SubShapes;
+  Handle(TColStd_HSequenceOfTransient) results = GetExistingSubObjects(theShape, types);
+
+  if (results->Length() > 0) {
+    //Make a Python command
+    TCollection_AsciiString anAsciiList;
+    for (int i = 1; i <= results->Length(); i++)
+    {
+      Handle(GEOM_BaseObject) obj = Handle(GEOM_BaseObject)::DownCast( results->Value(i));
+      obj->GetEntryString();
+      if ( i < results->Length() )
+       anAsciiList += ",";
+    }
+    
+    GEOM::TPythonDump pd (theShape->GetLastFunction(), /*append=*/true);
+    pd << "[" << anAsciiList.ToCString();
+    pd << "] = geompy.GetExistingSubObjects(";
+    pd << theShape << ", " << (bool)theGroupsOnly << ")";
+  }
+
+  return results;
+}
+
+Handle(TColStd_HSequenceOfTransient)
+GEOMImpl_IShapesOperations::GetExistingSubObjects(Handle(GEOM_Object)    theShape,
+                                                 const Standard_Integer theTypes)
 {
   SetErrorCode(KO);
 
@@ -1171,22 +1178,21 @@ Handle(TColStd_HSequenceOfTransient) GEOMImpl_IShapesOperations::GetExistingSubO
 
   SetErrorCode(KO);
 
-  TCollection_AsciiString anAsciiList;
-
   TDataStd_ListIteratorOfListOfExtendedString anIt (aListEntries);
   for (; anIt.More(); anIt.Next()) {
     TCollection_ExtendedString anEntry = anIt.Value();
     Standard_Integer aStrLen = anEntry.LengthOfCString();
     char* anEntryStr = new char[aStrLen+1];
     anEntry.ToUTF8CString(anEntryStr);
-    Handle(GEOM_Object) anObj = GetEngine()->GetObject(GetDocID(), anEntryStr, false);
-    if (!anObj.IsNull()) {
-      if (!theGroupsOnly || anObj->GetType() == GEOM_GROUP) {
+    Handle(GEOM_BaseObject) anObj = GetEngine()->GetObject(GetDocID(), anEntryStr, false);
+    if (!anObj.IsNull() ) {
+      bool isGroup    = anObj->IsKind(STANDARD_TYPE(GEOM_Object)) && anObj->GetType() == GEOM_GROUP;
+      bool isSubShape = anObj->IsKind(STANDARD_TYPE(GEOM_Object)) && anObj->GetType() != GEOM_GROUP;
+      bool isField    = anObj->IsKind(STANDARD_TYPE(GEOM_Field));
+      if (theTypes & Groups    && isGroup ||
+         theTypes & SubShapes && isSubShape ||
+         theTypes & Fields    && isField) {
         aSeq->Append(anObj);
-
-        // for python command
-        anAsciiList += anEntryStr;
-        anAsciiList += ",";
       }
     }
     delete [] anEntryStr;
@@ -1197,14 +1203,6 @@ Handle(TColStd_HSequenceOfTransient) GEOMImpl_IShapesOperations::GetExistingSubO
     return aSeq;
   }
 
-  //Make a Python command
-  anAsciiList.Trunc(anAsciiList.Length() - 1);
-
-  GEOM::TPythonDump pd (aMainShape, /*append=*/true);
-  pd << "[" << anAsciiList.ToCString();
-  pd << "] = geompy.GetExistingSubObjects(";
-  pd << theShape << ", " << (bool)theGroupsOnly << ")";
-
   SetErrorCode(OK);
 
   return aSeq;
@@ -1787,9 +1785,7 @@ Standard_Integer GEOMImpl_IShapesOperations::NumberOfSubShapes
   */
 
   try {
-#if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
-#endif
     int iType, nbTypes [TopAbs_SHAPE];
     for (iType = 0; iType < TopAbs_SHAPE; ++iType)
       nbTypes[iType] = 0;
@@ -1859,9 +1855,7 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::ReverseShape(Handle(GEOM_Object)
 
   //Compute the sub-shape value
   try {
-#if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
-#endif
     if (!GetSolver()->ComputeFunction(aFunction)) {
       SetErrorCode("Shape driver failed to reverse shape");
       return NULL;
@@ -2670,9 +2664,7 @@ Handle(TColStd_HSequenceOfInteger)
   // Compute tolerance
   Standard_Real T, VertMax = -RealLast();
   try {
-#if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
-#endif
     for (TopExp_Explorer ExV (theShape, TopAbs_VERTEX); ExV.More(); ExV.Next()) {
       TopoDS_Vertex Vertex = TopoDS::Vertex(ExV.Current());
       T = BRep_Tool::Tolerance(Vertex);
@@ -3848,87 +3840,46 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::GetInPlace (Handle(GEOM_Object)
 
   TopoDS_Shape aWhere = theShapeWhere->GetValue();
   TopoDS_Shape aWhat  = theShapeWhat->GetValue();
-  TopoDS_Shape aPntShape;
-  TopoDS_Vertex aVertex;
 
   if (aWhere.IsNull() || aWhat.IsNull()) {
     SetErrorCode("Error: aWhere and aWhat TopoDS_Shape are Null.");
     return NULL;
   }
 
-  Handle(GEOM_Function) aWhereFunction = theShapeWhere->GetLastFunction();
-  if (aWhereFunction.IsNull()) {
-    SetErrorCode("Error: aWhereFunction is Null.");
-    return NULL;
-  }
-
-  TopTools_IndexedMapOfShape aWhereIndices;
-  TopExp::MapShapes(aWhere, aWhereIndices);
-
-  TopAbs_ShapeEnum iType = TopAbs_SOLID;
-  Standard_Real    dl_l = 1e-3;
-  Standard_Real    min_l, Tol_0D, Tol_1D, Tol_2D, Tol_3D, Tol_Mass;
-  Standard_Real    aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
-  Bnd_Box          BoundingBox;
-  gp_Pnt           aPnt, aPnt_aWhat, tab_Pnt[2];
-  GProp_GProps     aProps;
+  // Compute confusion tolerance.
+  Standard_Real    aTolConf = Precision::Confusion();
+  Standard_Integer i;
 
-  // Find the iType of the aWhat shape
-  iType = GEOMUtils::GetTypeOfSimplePart(aWhat);
-  if (iType == TopAbs_SHAPE) {
-    SetErrorCode("Error: An attempt to extract a shape of not supported type.");
-    return NULL;
-  }
+  for (i = 0; i < 2; ++i) {
+    TopExp_Explorer anExp(i == 0 ? aWhere : aWhat, TopAbs_VERTEX);
 
-  TopExp_Explorer Exp_aWhat  ( aWhat,  iType );
-  TopExp_Explorer Exp_aWhere ( aWhere, iType );
-  TopExp_Explorer Exp_Edge   ( aWhere, TopAbs_EDGE );
+    for (; anExp.More(); anExp.Next()) {
+      const TopoDS_Vertex aVtx = TopoDS::Vertex(anExp.Current());
+      const Standard_Real aTolVtx = BRep_Tool::Tolerance(aVtx);
 
-  // Find the shortest edge in theShapeWhere shape
-  BRepBndLib::Add(aWhere, BoundingBox);
-  BoundingBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
-  min_l = fabs(aXmax - aXmin);
-  if( min_l < fabs(aYmax - aYmin) ) min_l = fabs(aYmax - aYmin);
-  if( min_l < fabs(aZmax - aZmin) ) min_l = fabs(aZmax - aZmin);
-  min_l /= dl_l;
-  // Mantis issue 0020908 BEGIN
-  if (!Exp_Edge.More()) {
-    min_l = Precision::Confusion();
-  }
-  // Mantis issue 0020908 END
-  for ( Standard_Integer nbEdge = 0; Exp_Edge.More(); Exp_Edge.Next(), nbEdge++ ) {
-    TopExp_Explorer Exp_Vertex( Exp_Edge.Current(), TopAbs_VERTEX);
-    for ( Standard_Integer nbVertex = 0; Exp_Vertex.More(); Exp_Vertex.Next(), nbVertex++ ) {
-      aPnt = BRep_Tool::Pnt( TopoDS::Vertex( Exp_Vertex.Current() ) );
-      tab_Pnt[nbVertex] = aPnt;
-    }
-    if ( ! tab_Pnt[0].IsEqual(tab_Pnt[1], dl_l) ) {
-      BRepGProp::LinearProperties(Exp_Edge.Current(), aProps);
-      if ( aProps.Mass() < min_l ) min_l = aProps.Mass();
+      if (aTolVtx > aTolConf) {
+        aTolConf = aTolVtx;
+      }
     }
   }
 
-  // Compute tolerances
-  Tol_0D = dl_l;
-  Tol_1D = dl_l * min_l;
-  Tol_2D = dl_l * ( min_l * min_l) * ( 2. + dl_l);
-  Tol_3D = dl_l * ( min_l * min_l * min_l ) * ( 3. + (3 * dl_l) + (dl_l * dl_l) );
+  // Compute mass tolerance.
+  Bnd_Box       aBoundingBox;
+  Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
+  Standard_Real aMassTol;
 
-  if (Tol_0D < Precision::Confusion()) Tol_0D = Precision::Confusion();
-  if (Tol_1D < Precision::Confusion()) Tol_1D = Precision::Confusion();
-  if (Tol_2D < Precision::Confusion()) Tol_2D = Precision::Confusion();
-  if (Tol_3D < Precision::Confusion()) Tol_3D = Precision::Confusion();
-
-  Tol_Mass = Tol_3D;
-  if ( iType == TopAbs_VERTEX )    Tol_Mass = Tol_0D;
-  else if ( iType == TopAbs_EDGE ) Tol_Mass = Tol_1D;
-  else if ( iType == TopAbs_FACE ) Tol_Mass = Tol_2D;
+  BRepBndLib::Add(aWhere, aBoundingBox);
+  BRepBndLib::Add(aWhat,  aBoundingBox);
+  aBoundingBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
+  aMassTol = Max(aXmax - aXmin, aYmax - aYmin);
+  aMassTol = Max(aMassTol, aZmax - aZmin);
+  aMassTol *= aTolConf;
 
   // Searching for the sub-shapes inside the ShapeWhere shape
   GEOMAlgo_GetInPlace aGIP;
-  aGIP.SetTolerance(Tol_1D);
-  aGIP.SetTolMass(Tol_Mass);
-  aGIP.SetTolCG(Tol_1D);
+  aGIP.SetTolerance(aTolConf);
+  aGIP.SetTolMass(aMassTol);
+  aGIP.SetTolCG(aTolConf);
 
   aGIP.SetArgument(aWhat);
   aGIP.SetShapeWhere(aWhere);
@@ -3940,24 +3891,26 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::GetInPlace (Handle(GEOM_Object)
     return NULL;
   }
 
-  // aGIP.IsFound() returns true only when the whole theShapeWhat
-  // is found (as one shape or several parts). But we are also interested
-  // in the partial result, that is why this check is commented.
-  //if (!aGIP.IsFound()) {
-  //  SetErrorCode(NOT_FOUND_ANY);
-  //  return NULL;
-  //}
+  // Add direct result.
+  TopTools_ListOfShape        aLSA;
+  const TopoDS_Shape         &aShapeResult = aGIP.Result();
+  TopTools_MapOfShape         aMFence;
+  TopTools_IndexedMapOfShape  aWhereIndices;
 
-  const TopTools_DataMapOfShapeListOfShape& aDMSLS = aGIP.Images();
-  if (!aDMSLS.IsBound(aWhat)) {
-    SetErrorCode(NOT_FOUND_ANY);
-    return NULL;
+  TopExp::MapShapes(aWhere, aWhereIndices);
+
+  if (aShapeResult.IsNull() == Standard_False) {
+    TopoDS_Iterator anIt(aShapeResult);
+
+    for (; anIt.More(); anIt.Next()) {
+      const TopoDS_Shape &aPart = anIt.Value();
+
+      if(aWhereIndices.Contains(aPart) && aMFence.Add(aPart)) {
+        aLSA.Append(aPart);
+      }
+    }
   }
 
-  // the list of shapes aLSA contains the shapes
-  // of the Shape For Search that corresponds
-  // to the Argument aWhat
-  const TopTools_ListOfShape& aLSA = aDMSLS.Find(aWhat);
   if (aLSA.Extent() == 0) {
     SetErrorCode(NOT_FOUND_ANY); // Not found any Results
     return NULL;
@@ -4265,13 +4218,40 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::GetInPlaceByHistory
     return NULL;
   }
 
-  Handle(TColStd_HArray1OfInteger) aModifiedArray =
-    new TColStd_HArray1OfInteger (1, aModifiedList.Extent());
+  Standard_Integer nbFound = aModifiedList.Extent();
   TColStd_ListIteratorOfListOfInteger anIterModif (aModifiedList);
-  for (Standard_Integer imod = 1; anIterModif.More(); anIterModif.Next(), imod++) {
-    aModifiedArray->SetValue(imod, anIterModif.Value());
+  if ( nbFound > 1 )
+  {
+    // remove sub-shapes inappropriate for group creation
+    TopAbs_ShapeEnum subType = TopAbs_SHAPE;
+    while ( anIterModif.More() ) {
+      TopAbs_ShapeEnum type = aWhereIndices( anIterModif.Value() ).ShapeType();
+      bool okForGroup = ( type == TopAbs_VERTEX || type == TopAbs_EDGE ||
+                          type == TopAbs_FACE   || type == TopAbs_SOLID );
+      if ( okForGroup ) {
+        if ( subType == TopAbs_SHAPE )
+          subType = type;
+        else
+          okForGroup = ( subType == type );
+      }
+      if ( okForGroup )
+        anIterModif.Next();
+      else
+        aModifiedList.Remove( anIterModif );
+      nbFound -= ( !okForGroup );
+    }
+    if ( nbFound == 0 ) {
+      SetErrorCode("Error: result found but it's type is inappropriate for group creation.");
+      return NULL;
+    }
   }
 
+  Handle(TColStd_HArray1OfInteger) aModifiedArray =
+    new TColStd_HArray1OfInteger( 1, nbFound );
+  anIterModif.Initialize(aModifiedList);
+  for (Standard_Integer imod = 1; anIterModif.More(); anIterModif.Next(), imod++)
+    aModifiedArray->SetValue(imod, anIterModif.Value());
+
   //Add a new object
   Handle(GEOM_Object) aResult = GetEngine()->AddSubShape(theShapeWhere, aModifiedArray);
   if (aResult.IsNull()) {
@@ -4295,7 +4275,7 @@ Handle(GEOM_Object) GEOMImpl_IShapesOperations::GetInPlaceByHistory
   Handle(GEOM_Function) aFunction = aResult->GetFunction(1);
 
   GEOM::TPythonDump(aFunction) << aResult << " = geompy.GetInPlaceByHistory("
-    << theShapeWhere << ", " << theShapeWhat << ")";
+                               << theShapeWhere << ", " << theShapeWhat << ")";
 
   SetErrorCode(OK);
   return aResult;