1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // Created: Tue Dec 7 11:59:05 2004
25 // Author: Pavel DURANDIN
27 #include <BlockFix.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopTools_DataMapOfShapeShape.hxx>
30 #include <ShapeCustom.hxx>
31 #include <BRepTools.hxx>
32 #include <ShapeBuild_ReShape.hxx>
33 #include <TopoDS_Face.hxx>
34 #include <TopExp_Explorer.hxx>
36 #include <TopLoc_Location.hxx>
37 #include <Geom_Surface.hxx>
38 #include <Geom_CylindricalSurface.hxx>
39 #include <Geom_ConicalSurface.hxx>
40 #include <ShapeFix_Wire.hxx>
41 #include <TopoDS_Wire.hxx>
42 #include <BRepTools_Modifier.hxx>
43 #include <Geom_SphericalSurface.hxx>
44 #include <Geom_ToroidalSurface.hxx>
45 #include <BRep_Tool.hxx>
46 #include <TopoDS_Edge.hxx>
47 #include <Geom2d_Curve.hxx>
48 #include <BRep_Builder.hxx>
49 #include <ShapeAnalysis_Edge.hxx>
50 #include <ShapeFix_Edge.hxx>
51 #include <ShapeFix.hxx>
52 #include <ShapeFix_Face.hxx>
53 #include <ShapeAnalysis.hxx>
55 #include <TColgp_SequenceOfPnt2d.hxx>
56 #include <ShapeAnalysis_Curve.hxx>
57 #include <TopoDS_Vertex.hxx>
58 #include <ShapeBuild_Edge.hxx>
60 #include <BlockFix_SphereSpaceModifier.hxx>
61 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
62 #include <TopTools_MapOfShape.hxx>
63 #include <BlockFix_PeriodicSurfaceModifier.hxx>
65 #include <TopoDS_Solid.hxx>
68 //=======================================================================
69 //function : FixResult
71 //=======================================================================
72 static void FixResult(const TopoDS_Shape& result,
73 Handle(ShapeBuild_ReShape)& Context,
74 const Standard_Real Tol)
76 for (TopExp_Explorer ex_f(result,TopAbs_FACE); ex_f.More(); ex_f.Next()) {
77 TopoDS_Shape aShape = Context->Apply(ex_f.Current().Oriented(TopAbs_FORWARD));
78 // face coud not be dropped or splitted on this step
79 TopoDS_Face aFace = TopoDS::Face(aShape);
81 Handle(Geom_Surface) Surf = BRep_Tool::Surface(aFace,L);
83 if( Surf->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ||
84 Surf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ) {
86 Standard_Integer nbWires = 0;
87 for (TopExp_Explorer ex_w(aFace,TopAbs_WIRE); ex_w.More(); ex_w.Next()) {
89 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(TopoDS::Wire(ex_w.Current()),
91 Precision::Confusion());
93 if(sfw->StatusReorder ( ShapeExtend_FAIL ))
96 sfw->SetPrecision(2.*Tol);
99 Standard_Boolean isDone = sfw->LastFixStatus ( ShapeExtend_DONE );
100 isDone |= sfw->FixDegenerated();
102 // remove degenerated edges from not degenerated points
103 ShapeAnalysis_Edge sae;
104 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
106 for( i = 1; i<=sewd->NbEdges();i++) {
107 TopoDS_Edge E = sewd->Edge(i);
108 if(BRep_Tool::Degenerated(E)&&!sae.HasPCurve(E,aFace)) {
110 isDone = Standard_True;
115 //isDone |= sfw->FixLacking(); // commented by skl 22.03.2005 (PAL8395)
117 // remove neighbour seam edges
119 for( i = 1; i<sewd->NbEdges();i++) {
120 if(sewd->IsSeam(i) && sewd->IsSeam(i+1)) {
121 isDone = Standard_True;
127 if(sewd->IsSeam(1) && sewd->IsSeam(sewd->NbEdges())) {
129 sewd->Remove(sewd->NbEdges());
135 TopoDS_Wire ResWire = sfw->Wire();
136 Context->Replace(ex_w.Current(), ResWire);
139 // Implement fix orientation in case of several wires
141 TopoDS_Face aFixedFace = TopoDS::Face(Context->Apply(aFace));
142 Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFixedFace);
143 if(sff->FixOrientation())
144 Context->Replace(aFixedFace,sff->Face());
155 //=======================================================================
156 //function : ConvertToAnalytical
158 //=======================================================================
160 TopoDS_Shape BlockFix::RotateSphereSpace (const TopoDS_Shape& S,
161 const Standard_Real Tol)
164 // Create a modification description
165 Handle(BlockFix_SphereSpaceModifier) SR = new BlockFix_SphereSpaceModifier;
166 SR->SetTolerance(Tol);
168 TopTools_DataMapOfShapeShape context;
169 BRepTools_Modifier MD;
170 TopoDS_Shape result = ShapeCustom::ApplyModifier ( S, SR, context,MD );
172 Handle(ShapeBuild_ReShape) RS = new ShapeBuild_ReShape;
173 FixResult(result,RS,Tol);
174 result = RS->Apply(result);
177 for(TopExp_Explorer exp(result,TopAbs_EDGE); exp.More(); exp.Next()) {
178 TopoDS_Edge E = TopoDS::Edge(exp.Current());
179 sfe.FixVertexTolerance (E);
182 ShapeFix::SameParameter(result,Standard_False);
187 //=======================================================================
188 //function : FixRanges
190 //=======================================================================
192 TopoDS_Shape BlockFix::FixRanges (const TopoDS_Shape& S,
193 const Standard_Real Tol)
195 // Create a modification description
196 Handle(BlockFix_PeriodicSurfaceModifier) SR = new BlockFix_PeriodicSurfaceModifier;
197 SR->SetTolerance(Tol);
199 TopTools_DataMapOfShapeShape context;
200 BRepTools_Modifier MD;
201 TopoDS_Shape result = ShapeCustom::ApplyModifier ( S, SR, context,MD );
203 Handle(ShapeBuild_ReShape) RS = new ShapeBuild_ReShape;
204 FixResult(result,RS,Tol);
205 result = RS->Apply(result);
208 for(TopExp_Explorer exp(result,TopAbs_EDGE); exp.More(); exp.Next()) {
209 TopoDS_Edge E = TopoDS::Edge(exp.Current());
210 sfe.FixVertexTolerance (E);
213 ShapeFix::SameParameter(result,Standard_False);