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