/src/contrib/geom-5.1.2.7/src/BlockFix/BlockFix.cpp

http://pythonocc.googlecode.com/ · C++ · 215 lines · 137 code · 34 blank · 44 comment · 17 complexity · 178613f09cf952e5decac611cb948113 MD5 · raw file

  1. // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
  2. //
  3. // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
  4. // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
  5. //
  6. // This library is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU Lesser General Public
  8. // License as published by the Free Software Foundation; either
  9. // version 2.1 of the License.
  10. //
  11. // This library is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. // Lesser General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Lesser General Public
  17. // License along with this library; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. //
  20. // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
  21. //
  22. // File: BlockFix.cxx
  23. // Created: Tue Dec 7 11:59:05 2004
  24. // Author: Pavel DURANDIN
  25. //
  26. #include <BlockFix.hxx>
  27. #include <TopoDS_Shape.hxx>
  28. #include <TopTools_DataMapOfShapeShape.hxx>
  29. #include <ShapeCustom.hxx>
  30. #include <BRepTools.hxx>
  31. #include <ShapeBuild_ReShape.hxx>
  32. #include <TopoDS_Face.hxx>
  33. #include <TopExp_Explorer.hxx>
  34. #include <TopoDS.hxx>
  35. #include <TopLoc_Location.hxx>
  36. #include <Geom_Surface.hxx>
  37. #include <Geom_CylindricalSurface.hxx>
  38. #include <Geom_ConicalSurface.hxx>
  39. #include <ShapeFix_Wire.hxx>
  40. #include <TopoDS_Wire.hxx>
  41. #include <BRepTools_Modifier.hxx>
  42. #include <Geom_SphericalSurface.hxx>
  43. #include <Geom_ToroidalSurface.hxx>
  44. #include <BRep_Tool.hxx>
  45. #include <TopoDS_Edge.hxx>
  46. #include <Geom2d_Curve.hxx>
  47. #include <BRep_Builder.hxx>
  48. #include <ShapeAnalysis_Edge.hxx>
  49. #include <ShapeFix_Edge.hxx>
  50. #include <ShapeFix.hxx>
  51. #include <ShapeFix_Face.hxx>
  52. #include <ShapeAnalysis.hxx>
  53. #include <TColgp_SequenceOfPnt2d.hxx>
  54. #include <ShapeAnalysis_Curve.hxx>
  55. #include <TopoDS_Vertex.hxx>
  56. #include <ShapeBuild_Edge.hxx>
  57. #include <BlockFix_SphereSpaceModifier.hxx>
  58. #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
  59. #include <TopTools_MapOfShape.hxx>
  60. #include <BlockFix_PeriodicSurfaceModifier.hxx>
  61. #include <TopoDS_Solid.hxx>
  62. //=======================================================================
  63. //function : FixResult
  64. //purpose : auxilary
  65. //=======================================================================
  66. static void FixResult(const TopoDS_Shape& result,
  67. Handle(ShapeBuild_ReShape)& Context,
  68. const Standard_Real Tol)
  69. {
  70. for (TopExp_Explorer ex_f(result,TopAbs_FACE); ex_f.More(); ex_f.Next()) {
  71. TopoDS_Shape aShape = Context->Apply(ex_f.Current().Oriented(TopAbs_FORWARD));
  72. // face coud not be dropped or splitted on this step
  73. TopoDS_Face aFace = TopoDS::Face(aShape);
  74. TopLoc_Location L;
  75. Handle(Geom_Surface) Surf = BRep_Tool::Surface(aFace,L);
  76. if( Surf->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ||
  77. Surf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ) {
  78. Standard_Integer nbWires = 0;
  79. for (TopExp_Explorer ex_w(aFace,TopAbs_WIRE); ex_w.More(); ex_w.Next()) {
  80. nbWires++;
  81. Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(TopoDS::Wire(ex_w.Current()),
  82. aFace,
  83. Precision::Confusion());
  84. sfw->FixReorder();
  85. if(sfw->StatusReorder ( ShapeExtend_FAIL ))
  86. continue;
  87. sfw->SetPrecision(2.*Tol);
  88. sfw->FixShifted();
  89. Standard_Boolean isDone = sfw->LastFixStatus ( ShapeExtend_DONE );
  90. isDone |= sfw->FixDegenerated();
  91. // remove degenerated edges from not degenerated points
  92. ShapeAnalysis_Edge sae;
  93. Handle(ShapeExtend_WireData) sewd = sfw->WireData();
  94. Standard_Integer i;
  95. for( i = 1; i<=sewd->NbEdges();i++) {
  96. TopoDS_Edge E = sewd->Edge(i);
  97. if(BRep_Tool::Degenerated(E)&&!sae.HasPCurve(E,aFace)) {
  98. sewd->Remove(i);
  99. isDone = Standard_True;
  100. i--;
  101. }
  102. }
  103. //isDone |= sfw->FixLacking(); // commented by skl 22.03.2005 (PAL8395)
  104. // remove neighbour seam edges
  105. if(isDone) {
  106. for( i = 1; i<sewd->NbEdges();i++) {
  107. if(sewd->IsSeam(i) && sewd->IsSeam(i+1)) {
  108. isDone = Standard_True;
  109. sewd->Remove(i);
  110. sewd->Remove(i);
  111. i--;
  112. }
  113. }
  114. if(sewd->IsSeam(1) && sewd->IsSeam(sewd->NbEdges())) {
  115. sewd->Remove(1);
  116. sewd->Remove(sewd->NbEdges());
  117. }
  118. }
  119. if(isDone) {
  120. TopoDS_Wire ResWire = sfw->Wire();
  121. Context->Replace(ex_w.Current(), ResWire);
  122. };
  123. }
  124. // Implement fix orientation in case of several wires
  125. if(nbWires > 1) {
  126. TopoDS_Face aFixedFace = TopoDS::Face(Context->Apply(aFace));
  127. Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFixedFace);
  128. if(sff->FixOrientation())
  129. Context->Replace(aFixedFace,sff->Face());
  130. }
  131. }
  132. }
  133. }
  134. //=======================================================================
  135. //function : ConvertToAnalytical
  136. //purpose :
  137. //=======================================================================
  138. TopoDS_Shape BlockFix::RotateSphereSpace (const TopoDS_Shape& S,
  139. const Standard_Real Tol)
  140. {
  141. // Create a modification description
  142. Handle(BlockFix_SphereSpaceModifier) SR = new BlockFix_SphereSpaceModifier;
  143. SR->SetTolerance(Tol);
  144. TopTools_DataMapOfShapeShape context;
  145. BRepTools_Modifier MD;
  146. TopoDS_Shape result = ShapeCustom::ApplyModifier ( S, SR, context,MD );
  147. Handle(ShapeBuild_ReShape) RS = new ShapeBuild_ReShape;
  148. FixResult(result,RS,Tol);
  149. result = RS->Apply(result);
  150. ShapeFix_Edge sfe;
  151. for(TopExp_Explorer exp(result,TopAbs_EDGE); exp.More(); exp.Next()) {
  152. TopoDS_Edge E = TopoDS::Edge(exp.Current());
  153. sfe.FixVertexTolerance (E);
  154. }
  155. ShapeFix::SameParameter(result,Standard_False);
  156. return result;
  157. }
  158. //=======================================================================
  159. //function : FixRanges
  160. //purpose :
  161. //=======================================================================
  162. TopoDS_Shape BlockFix::FixRanges (const TopoDS_Shape& S,
  163. const Standard_Real Tol)
  164. {
  165. // Create a modification description
  166. Handle(BlockFix_PeriodicSurfaceModifier) SR = new BlockFix_PeriodicSurfaceModifier;
  167. SR->SetTolerance(Tol);
  168. TopTools_DataMapOfShapeShape context;
  169. BRepTools_Modifier MD;
  170. TopoDS_Shape result = ShapeCustom::ApplyModifier ( S, SR, context,MD );
  171. Handle(ShapeBuild_ReShape) RS = new ShapeBuild_ReShape;
  172. FixResult(result,RS,Tol);
  173. result = RS->Apply(result);
  174. ShapeFix_Edge sfe;
  175. for(TopExp_Explorer exp(result,TopAbs_EDGE); exp.More(); exp.Next()) {
  176. TopoDS_Edge E = TopoDS::Edge(exp.Current());
  177. sfe.FixVertexTolerance (E);
  178. }
  179. ShapeFix::SameParameter(result,Standard_False);
  180. return result;
  181. }