Salome HOME
Update copyrights 2014.
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Gluer2_1.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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
23 // File:     GEOMAlgo_Gluer2_1.cxx
24 // Author:   Peter KURNEV
25
26 #include <GEOMAlgo_Gluer2.hxx>
27
28 #include <gp_XYZ.hxx>
29 #include <gp_Pnt.hxx>
30
31 #include <TopLoc_Location.hxx>
32
33 #include <Geom_Surface.hxx>
34
35 #include <TopAbs_ShapeEnum.hxx>
36
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Vertex.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Wire.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopoDS_Iterator.hxx>
43
44 #include <BRep_Tool.hxx>
45 #include <BRep_Builder.hxx>
46
47 #include <BRepTools.hxx>
48 #include <TopExp.hxx>
49
50 #include <TopTools_MapOfShape.hxx>
51 #include <TopTools_ListIteratorOfListOfShape.hxx>
52 #include <TopTools_ListOfShape.hxx>
53
54 #include <BOPTools_AlgoTools.hxx>
55 #include <GEOMAlgo_AlgoTools.hxx>
56 #include <BOPTools_AlgoTools2D.hxx>
57
58 //=======================================================================
59 //function : MakeBRepShapes
60 //purpose  :
61 //=======================================================================
62 void GEOMAlgo_Gluer2::MakeBRepShapes(const TopoDS_Shape& theS,
63                                      TopoDS_Shape& theSnew)
64 {
65   TopAbs_ShapeEnum aType;
66   //
67   aType=theS.ShapeType();
68   if (aType==TopAbs_EDGE) {
69     TopoDS_Edge aEE, aEEnew;
70     //
71     aEE=*((TopoDS_Edge*)&theS);
72     MakeEdge(aEE, aEEnew);
73     if (myErrorStatus) {
74       return;
75     }
76     //
77     theSnew=aEEnew;
78   }
79   else if (aType==TopAbs_FACE) {
80     TopoDS_Face aFF, aFFnew;
81     //
82     aFF=*((TopoDS_Face*)&theS);
83     MakeFace(aFF, aFFnew);
84     if (myErrorStatus) {
85       return;
86     }
87     //
88     theSnew=aFFnew;
89   }
90 }
91 //=======================================================================
92 //function : MakeFace
93 //purpose  :
94 //=======================================================================
95 void GEOMAlgo_Gluer2::MakeFace(const TopoDS_Face& theF,
96                                TopoDS_Face& theFnew)
97 {
98   Standard_Boolean bIsToReverse, bIsUPeriodic;
99   Standard_Integer iRet;
100   Standard_Real aTol, aUMin, aUMax, aVMin, aVMax;
101   Handle(Geom_Surface) aS;
102   TopLoc_Location aLoc;
103   TopoDS_Shape aW, aWr;
104   TopoDS_Edge aEx;
105   TopoDS_Face aFF, aFnew;
106   TopoDS_Iterator aItW, aItE;
107   BRep_Builder aBB;
108   TopTools_ListOfShape aLEr;
109   TopTools_ListIteratorOfListOfShape aItLE;
110   //
111   myErrorStatus=0;
112   //
113   aFF=theF;
114   aFF.Orientation(TopAbs_FORWARD);
115   //
116   aTol=BRep_Tool::Tolerance(aFF);
117   aS=BRep_Tool::Surface(aFF, aLoc);
118   bIsUPeriodic=GEOMAlgo_AlgoTools::IsUPeriodic(aS);
119   BRepTools::UVBounds(aFF, aUMin, aUMax, aVMin, aVMax);
120   //
121   aBB.MakeFace (aFnew, aS, aLoc, aTol);
122   //
123   aItW.Initialize(aFF);
124   for (; aItW.More(); aItW.Next()) {
125     const TopoDS_Shape& aW=aItW.Value();
126     //
127     if (!myOrigins.IsBound(aW)) {
128       aBB.Add(aFnew, aW);
129       continue;
130     }
131     //
132     aWr=myOrigins.Find(aW);
133     //
134     // clear contents of Wr
135     aLEr.Clear();
136     aItE.Initialize(aWr);
137     for (; aItE.More(); aItE.Next()) {
138       const TopoDS_Shape& aEr=aItE.Value();
139       aLEr.Append(aEr);
140     }
141     //
142     aItLE.Initialize(aLEr);
143     for (; aItLE.More(); aItLE.Next()) {
144       const TopoDS_Shape& aEr=aItLE.Value();
145       aBB.Remove(aWr, aEr);
146     }
147     //
148     // refill contents of Wr
149     aItE.Initialize(aW);
150     for (; aItE.More(); aItE.Next()) {
151       const TopoDS_Edge& aE=*((TopoDS_Edge*)(&aItE.Value()));
152       //
153       aEx=aE;
154       if (myOrigins.IsBound(aE)) {
155         aEx=*((TopoDS_Edge*)(&myOrigins.Find(aE)));
156       }
157       //
158       if (!BRep_Tool::Degenerated(aEx)) {
159         aEx.Orientation(TopAbs_FORWARD);
160         if (bIsUPeriodic) {
161           GEOMAlgo_AlgoTools::RefinePCurveForEdgeOnFace(aEx, aFF, aUMin, aUMax);
162         }
163         //
164         //modified by NIZNHY-PKV Fri Feb 03 11:18:17 2012f
165         iRet=GEOMAlgo_AlgoTools::BuildPCurveForEdgeOnFace(aE, aEx, aFF, myContext);
166         if (iRet) {
167           continue;
168         }
169         //modified by NIZNHY-PKV Fri Feb 03 11:18:20 2012t
170         //
171         bIsToReverse=GEOMAlgo_AlgoTools::IsSplitToReverse(aEx, aE, myContext);
172         if (bIsToReverse) {
173           aEx.Reverse();
174         }
175       }
176       else {
177         aEx.Orientation(aE.Orientation());
178       }
179       aBB.Add(aWr, aEx);
180     }// for (; aItE.More(); aItE.Next()) {
181     //
182     aBB.Add(aFnew, aWr);
183   }// for (; aItW.More(); aItW.Next()) {
184   theFnew=aFnew;
185 }
186 //=======================================================================
187 //function : MakeEdge
188 //purpose  :
189 //=======================================================================
190 void GEOMAlgo_Gluer2::MakeEdge(const TopoDS_Edge& aE,
191                                TopoDS_Edge& aNewEdge)
192 {
193   myErrorStatus=0;
194   //
195   Standard_Boolean bIsDE;
196   Standard_Real aT1, aT2;
197   TopoDS_Vertex aV1, aV2, aVR1, aVR2;
198   TopoDS_Edge aEx;
199   //
200   bIsDE=BRep_Tool::Degenerated(aE);
201   //
202   aEx=aE;
203   aEx.Orientation(TopAbs_FORWARD);
204   //
205   TopExp::Vertices(aEx, aV1, aV2);
206   //
207   aT1=BRep_Tool::Parameter(aV1, aEx);
208   aT2=BRep_Tool::Parameter(aV2, aEx);
209   //
210   aVR1=aV1;
211   if (myOrigins.IsBound(aV1)) {
212     aVR1=*((TopoDS_Vertex*)&myOrigins.Find(aV1));
213   }
214   aVR1.Orientation(TopAbs_FORWARD);
215   //
216   aVR2=aV2;
217   if (myOrigins.IsBound(aV2)) {
218     aVR2=*((TopoDS_Vertex*)&myOrigins.Find(aV2));
219   }
220   aVR2.Orientation(TopAbs_REVERSED);
221   //
222   if (!bIsDE) {
223     BOPTools_AlgoTools::MakeSplitEdge(aEx, aVR1, aT1, aVR2, aT2, aNewEdge);
224   }
225   else {
226     Standard_Real aTol;
227     BRep_Builder aBB;
228     TopoDS_Edge E;
229     //
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