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