Salome HOME
Update copyright
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Gluer2_1.cxx
1 // Copyright (C) 2007-2011  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:        GEOMAlgo_Gluer2_1.cxx
23 // Created:     
24 // Author:      Peter KURNEV
25 //              <peter@PREFEX>
26 //
27 #include <GEOMAlgo_Gluer2.hxx>
28
29 #include <gp_XYZ.hxx>
30 #include <gp_Pnt.hxx>
31
32 #include <TopLoc_Location.hxx>
33
34 #include <Geom_Surface.hxx>
35
36 #include <TopAbs_ShapeEnum.hxx>
37
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Vertex.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Wire.hxx>
42 #include <TopoDS_Face.hxx>
43 #include <TopoDS_Iterator.hxx>
44
45 #include <BRep_Tool.hxx>
46 #include <BRep_Builder.hxx>
47
48 #include <BRepTools.hxx>
49 #include <TopExp.hxx>
50
51 #include <TopTools_MapOfShape.hxx>
52 #include <TopTools_ListIteratorOfListOfShape.hxx>
53 #include <TopTools_ListOfShape.hxx>
54
55 #include <BOPTools_Tools.hxx>
56 #include <BOPTools_Tools2D.hxx>
57 #include <BOPTools_Tools3D.hxx>
58
59 #include <GEOMAlgo_Tools.hxx>
60
61
62 //=======================================================================
63 //function : MakeBRepShapes
64 //purpose  : 
65 //=======================================================================
66 void GEOMAlgo_Gluer2::MakeBRepShapes(const TopoDS_Shape& theS, 
67                                     TopoDS_Shape& theSnew)
68 {
69   TopAbs_ShapeEnum aType;
70   //
71   aType=theS.ShapeType();
72   if (aType==TopAbs_EDGE) {
73     TopoDS_Edge aEE, aEEnew;
74     //
75     aEE=*((TopoDS_Edge*)&theS);
76     MakeEdge(aEE, aEEnew);
77     if (myErrorStatus) {
78       return;
79     }
80     //
81     theSnew=aEEnew;
82   }
83   else if (aType==TopAbs_FACE) {
84     TopoDS_Face aFF, aFFnew;
85     //
86     aFF=*((TopoDS_Face*)&theS);
87     MakeFace(aFF, aFFnew);
88     if (myErrorStatus) {
89       return;
90     }
91     //
92     theSnew=aFFnew;
93   }
94 }
95 //=======================================================================
96 //function : MakeFace
97 //purpose  : 
98 //=======================================================================
99 void GEOMAlgo_Gluer2::MakeFace(const TopoDS_Face& theF, 
100                                TopoDS_Face& theFnew)
101 {
102   Standard_Boolean bIsToReverse, bIsUPeriodic;
103   Standard_Real aTol, aUMin, aUMax, aVMin, aVMax;
104   TopAbs_Orientation aOrE;
105   Handle(Geom_Surface) aS;
106   TopLoc_Location aLoc;
107   TopoDS_Shape aW, aWr;
108   TopoDS_Edge aEx;
109   TopoDS_Face aFF, aFnew;
110   TopoDS_Iterator aItW, aItE;
111   BRep_Builder aBB;
112   TopTools_ListOfShape aLEr;
113   TopTools_ListIteratorOfListOfShape aItLE;
114   //
115   myErrorStatus=0;
116   //
117   aFF=theF;
118   aFF.Orientation(TopAbs_FORWARD);
119   //
120   aTol=BRep_Tool::Tolerance(aFF);
121   aS=BRep_Tool::Surface(aFF, aLoc);
122   bIsUPeriodic=GEOMAlgo_Tools::IsUPeriodic(aS);
123   BRepTools::UVBounds(aFF, aUMin, aUMax, aVMin, aVMax);
124   //
125   aBB.MakeFace (aFnew, aS, aLoc, aTol);
126   //
127   aItW.Initialize(aFF);
128   for (; aItW.More(); aItW.Next()) {
129     const TopoDS_Shape& aW=aItW.Value();
130     //
131     if (!myOrigins.IsBound(aW)) {
132       aBB.Add(aFnew, aW);
133       continue;
134     }
135     //
136     aWr=myOrigins.Find(aW);
137     //
138     // clear contents of Wr
139     aLEr.Clear();
140     aItE.Initialize(aWr);
141     for (; aItE.More(); aItE.Next()) {
142       const TopoDS_Shape& aEr=aItE.Value();
143       aLEr.Append(aEr);
144     }
145     //
146     aItLE.Initialize(aLEr);
147     for (; aItLE.More(); aItLE.Next()) {
148       const TopoDS_Shape& aEr=aItLE.Value();
149       aBB.Remove(aWr, aEr);
150     }
151     //
152     // refill contents of Wr
153     aItE.Initialize(aW);
154     for (; aItE.More(); aItE.Next()) {
155       const TopoDS_Edge& aE=*((TopoDS_Edge*)(&aItE.Value()));
156       //
157       aEx=aE;
158       if (myOrigins.IsBound(aE)) {
159         aEx=*((TopoDS_Edge*)(&myOrigins.Find(aE)));
160       }
161       //
162       if (!BRep_Tool::Degenerated(aEx)) {
163         aEx.Orientation(TopAbs_FORWARD);
164         if (bIsUPeriodic) {
165           GEOMAlgo_Tools::RefinePCurveForEdgeOnFace(aEx, aFF, aUMin, aUMax);
166         }
167         BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aEx, aFF);
168         //  
169         bIsToReverse=BOPTools_Tools3D::IsSplitToReverse1(aEx, aE, myContext);
170         if (bIsToReverse) {
171           aEx.Reverse();
172         }
173       }
174       else {
175         aEx.Orientation(aE.Orientation());
176       }
177       aBB.Add(aWr, aEx);
178     }// for (; aItE.More(); aItE.Next()) {
179     //
180     aBB.Add(aFnew, aWr);
181   }// for (; aItW.More(); aItW.Next()) {
182   theFnew=aFnew;
183 }
184 //=======================================================================
185 //function : MakeEdge
186 //purpose  : 
187 //=======================================================================
188 void GEOMAlgo_Gluer2::MakeEdge(const TopoDS_Edge& aE, 
189                                TopoDS_Edge& aNewEdge)
190 {
191   myErrorStatus=0;
192   //
193   Standard_Boolean bIsDE;
194   Standard_Real aT1, aT2;
195   TopoDS_Vertex aV1, aV2, aVR1, aVR2;
196   TopoDS_Edge aEx;
197   //
198   bIsDE=BRep_Tool::Degenerated(aE);
199   //
200   aEx=aE;
201   aEx.Orientation(TopAbs_FORWARD);
202   //
203   TopExp::Vertices(aEx, aV1, aV2);
204   //
205   aT1=BRep_Tool::Parameter(aV1, aEx);
206   aT2=BRep_Tool::Parameter(aV2, aEx);
207   //
208   aVR1=aV1;
209   if (myOrigins.IsBound(aV1)) {
210     aVR1=*((TopoDS_Vertex*)&myOrigins.Find(aV1));
211   }
212   aVR1.Orientation(TopAbs_FORWARD);
213   //
214   aVR2=aV2;
215   if (myOrigins.IsBound(aV2)) {
216     aVR2=*((TopoDS_Vertex*)&myOrigins.Find(aV2));
217   }
218   aVR2.Orientation(TopAbs_REVERSED);
219   //
220   if (!bIsDE) {
221     BOPTools_Tools::MakeSplitEdge(aEx, aVR1, aT1, aVR2, aT2, aNewEdge); 
222   }
223   else {
224     Standard_Real aTol;
225     BRep_Builder aBB;
226     TopoDS_Edge E;
227     //TopAbs_Orientation anOrE;
228     //
229     //anOrE=aE.Orientation();
230     aTol=BRep_Tool::Tolerance(aE);
231     //
232     E=aEx;
233     E.EmptyCopy();
234     //
235     aBB.Add  (E, aVR1);
236     aBB.Add  (E, aVR2);
237     aBB.Range(E, aT1, aT2);
238     aBB.Degenerated(E, Standard_True);
239     aBB.UpdateEdge(E, aTol);
240     //
241     aNewEdge=E;
242   }
243 }
244 //=======================================================================
245 //function : MakeVertex
246 //purpose  : 
247 //=======================================================================
248 void GEOMAlgo_Gluer2::MakeVertex(const TopTools_ListOfShape& aLV, 
249                                  TopoDS_Vertex& aNewVertex)
250 {
251   Standard_Integer aNbV;
252   Standard_Real aTolV, aD, aDmax;
253   gp_XYZ aGC;
254   gp_Pnt aP3D, aPGC;
255   TopoDS_Vertex aVx;
256   BRep_Builder aBB;
257   TopTools_ListIteratorOfListOfShape aIt;
258   //
259   aNbV=aLV.Extent();
260   if (!aNbV) {
261     return;
262   }
263   //
264   // center of gravity
265   aGC.SetCoord(0.,0.,0.);
266   aIt.Initialize(aLV);
267   for (; aIt.More(); aIt.Next()) {
268     aVx=*((TopoDS_Vertex*)(&aIt.Value()));
269     aP3D=BRep_Tool::Pnt(aVx);
270     aGC+=aP3D.XYZ();
271   }
272   aGC/=(Standard_Real)aNbV;
273   aPGC.SetXYZ(aGC);
274   //
275   // tolerance value
276   aDmax=-1.;
277   aIt.Initialize(aLV);
278   for (; aIt.More(); aIt.Next()) {
279     aVx=*((TopoDS_Vertex*)(&aIt.Value()));
280     aP3D=BRep_Tool::Pnt(aVx);
281     aTolV=BRep_Tool::Tolerance(aVx);
282     aD=aPGC.Distance(aP3D)+aTolV;
283     if (aD>aDmax) {
284       aDmax=aD;
285     }
286   }
287   //
288   aBB.MakeVertex (aNewVertex, aPGC, aDmax);
289 }
290 //=======================================================================
291 //function : MapBRepShapes
292 //purpose  : 
293 //=======================================================================
294 void GEOMAlgo_Gluer2::MapBRepShapes(const TopoDS_Shape& aS,
295                                     TopTools_MapOfShape& aM)
296 {
297   //Standard_Boolean bHasBRep;
298   TopAbs_ShapeEnum aType;
299   TopoDS_Iterator aIt;
300   //
301   aType=aS.ShapeType();
302   if (aType==TopAbs_VERTEX ||
303       aType==TopAbs_EDGE ||
304       aType==TopAbs_FACE) {
305     aM.Add(aS);
306   }
307   //
308   aIt.Initialize(aS);
309   for(; aIt.More(); aIt.Next()) {
310     const TopoDS_Shape& aSx=aIt.Value();
311     aType=aSx.ShapeType();
312     GEOMAlgo_Gluer2::MapBRepShapes(aSx, aM);
313   }
314 }
315
316 //
317 // ErrorStatus
318 //
319 // 40   - GEOMAlgo_GlueDetector is failed