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