Salome HOME
This commit was generated by cvs2git to create tag 'TRIPOLI_323'.
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Gluer2_3.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_3.cxx
24 // Author: Peter KURNEV
25
26 #include <GEOMAlgo_Gluer2.hxx>
27
28 #include <TopAbs_ShapeEnum.hxx>
29
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Edge.hxx>
32
33 #include <BRep_Tool.hxx>
34
35 #include <TopExp.hxx>
36 #include <TopExp_Explorer.hxx>
37
38 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
39 #include <TopTools_DataMapOfShapeListOfShape.hxx>
40 #include <TopTools_ListOfShape.hxx>
41 #include <TopTools_MapOfShape.hxx>
42 #include <TopTools_MapIteratorOfMapOfShape.hxx>
43 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
44 #include <TopTools_ListIteratorOfListOfShape.hxx>
45 #include <TopTools_IndexedMapOfShape.hxx>
46
47 #include <NMTTools_CoupleOfShape.hxx>
48 #include <NMTTools_ListOfCoupleOfShape.hxx>
49 #include <NMTTools_ListIteratorOfListOfCoupleOfShape.hxx>
50 #include <NMTTools_IndexedDataMapOfShapeIndexedMapOfShape.hxx>
51 #include <NMTTools_Tools.hxx>
52
53 #include <GEOMAlgo_GlueDetector.hxx>
54
55
56 static
57   void MapShapes1(const TopoDS_Shape& aS,
58                   const TopAbs_ShapeEnum aType,
59                   TopTools_IndexedMapOfShape& aM);
60
61 //=======================================================================
62 //function : Detect
63 //purpose  :
64 //=======================================================================
65 void GEOMAlgo_Gluer2::Detect()
66 {
67   Standard_Boolean bCheckGeometry;
68   Standard_Integer iErr, aNbSD;
69   TopTools_ListIteratorOfListOfShape aItLS;
70   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItDMSLS;
71   //GEOMAlgo_GlueDetector aDetector;
72   //
73   myErrorStatus=0;
74   myWarningStatus=0;
75   //
76   myImagesDetected.Clear();
77   myOriginsDetected.Clear();
78   //
79   bCheckGeometry=Standard_True;
80   //
81   //modified by NIZNHY-PKV Tue Mar 13 13:33:35 2012f
82   myDetector.Clear();
83   myDetector.SetContext(myContext);
84   //modified by NIZNHY-PKV Tue Mar 13 13:33:38 2012t
85   myDetector.SetArgument(myArgument);
86   myDetector.SetTolerance(myTolerance);
87   myDetector.SetCheckGeometry(bCheckGeometry);
88   //
89   myDetector.Perform();
90   iErr=myDetector.ErrorStatus();
91   if (iErr) {
92     // Detector is failed
93     myErrorStatus=11;
94     return;
95   }
96   //modified by NIZNHY-PKV Tue Mar 13 13:40:36 2012f
97   iErr=myDetector.WarningStatus();
98   if (iErr) {
99     // Sticked shapes are detected
100     myWarningStatus=2;
101   }
102   //modified by NIZNHY-PKV Tue Mar 13 13:40:39 2012t
103   //
104   const TopTools_DataMapOfShapeListOfShape& aImages=myDetector.Images();
105   aItDMSLS.Initialize(aImages);
106   for (; aItDMSLS.More(); aItDMSLS.Next()) {
107     const TopoDS_Shape& aSkey=aItDMSLS.Key();
108     const TopTools_ListOfShape& aLSD=aItDMSLS.Value();
109     aNbSD=aLSD.Extent();
110     myImagesDetected.Bind(aSkey, aLSD);
111   }
112   //
113   aItDMSLS.Initialize(myImagesDetected);
114   for (; aItDMSLS.More(); aItDMSLS.Next()) {
115     const TopoDS_Shape& aSkey=aItDMSLS.Key();
116     const TopTools_ListOfShape& aLSD=aItDMSLS.Value();
117     aItLS.Initialize(aLSD);
118     for (; aItLS.More(); aItLS.Next()) {
119       const TopoDS_Shape& aSx=aItLS.Value();
120       myOriginsDetected.Bind(aSx, aSkey);
121     }
122   }
123 }
124 //=======================================================================
125 //function : PerformShapesToWork
126 //purpose  :
127 //=======================================================================
128 void GEOMAlgo_Gluer2::PerformShapesToWork()
129 {
130   Standard_Integer aNbSG, i, j, k, aNbC, aNb, aNbSD;
131   TopTools_ListIteratorOfListOfShape aItLS1, aItLS2;
132   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItDMSLS;
133   NMTTools_CoupleOfShape aCS;
134   NMTTools_ListOfCoupleOfShape aLCS;
135   NMTTools_ListIteratorOfListOfCoupleOfShape aItCS;
136   //
137   myErrorStatus=0;
138   myWarningStatus=0;
139   //
140   myImagesToWork.Clear();
141   myOriginsToWork.Clear();
142   //
143   aNbSD=myImagesDetected.Extent();
144   if (!aNbSD) {// no shapes to glue detected
145     myWarningStatus=1;
146     return;
147   }
148   //
149   aNbSG=myShapesToGlue.Extent();
150   if (!aNbSG) {
151     // glue all possible
152     myImagesToWork=myImagesDetected;
153     //
154     aItDMSLS.Initialize(myImagesToWork);
155     for (; aItDMSLS.More(); aItDMSLS.Next()) {
156       const TopoDS_Shape& aSkey=aItDMSLS.Key();
157       const TopTools_ListOfShape& aLSD=aItDMSLS.Value();
158       aItLS1.Initialize(aLSD);
159       for (; aItLS1.More(); aItLS1.Next()) {
160         const TopoDS_Shape& aSx=aItLS1.Value();
161         myOriginsToWork.Bind(aSx, aSkey);
162       }
163     }
164     return;
165   }// if (!aNbSG) {
166   //
167   // 1. Make pairs
168   aItDMSLS.Initialize(myShapesToGlue);
169   for (k=0; aItDMSLS.More(); aItDMSLS.Next(), ++k) {
170     //const TopoDS_Shape& aSkey=aItDMSLS.Key();
171     const TopTools_ListOfShape& aLSG=aItDMSLS.Value();
172     aItLS1.Initialize(aLSG);
173     for (i=0; aItLS1.More(); aItLS1.Next(), ++i) {
174       aItLS2.Initialize(aLSG);
175       for (j=0; aItLS2.More(); aItLS2.Next(), ++j) {
176         if (j>i) {
177           const TopoDS_Shape& aSG1=aItLS1.Value();
178           const TopoDS_Shape& aSG2=aItLS2.Value();
179           aCS.SetShape1(aSG1);
180           aCS.SetShape2(aSG2);
181           TreatPair(aCS, aLCS);
182         }
183       }
184     }
185   }
186   //
187   // 2. Find Chains
188   TopTools_ListOfShape aLSX;
189   NMTTools_IndexedDataMapOfShapeIndexedMapOfShape aMC;
190   //
191   NMTTools_Tools::FindChains(aLCS, aMC);
192   //
193   // 3. myImagesToWork, myOriginsToWork
194   aNbC=aMC.Extent();
195   for (i=1; i<=aNbC; ++i) {
196     const TopoDS_Shape& aSkey=aMC.FindKey(i);
197     const TopTools_IndexedMapOfShape& aM=aMC(i);
198     aLSX.Clear();
199     aNb=aM.Extent();
200     for (j=1; j<=aNb; ++j) {
201       const TopoDS_Shape& aS=aM(j);
202       aLSX.Append(aS);
203       myOriginsToWork.Bind(aS, aSkey);
204     }
205     myImagesToWork.Bind(aSkey, aLSX);
206   }
207 }
208 //=======================================================================
209 //function : TreatPair
210 //purpose  :
211 //=======================================================================
212 void GEOMAlgo_Gluer2::TreatPair(const NMTTools_CoupleOfShape& aCS,
213                                 NMTTools_ListOfCoupleOfShape& aLCS)
214 {
215   if (myErrorStatus) {
216     return;
217   }
218   //
219   Standard_Integer i, aNbS1, aNbS2, aNbS;
220   TopAbs_ShapeEnum aType, aTypeS;
221   TopTools_ListIteratorOfListOfShape aItLS;
222   TopTools_IndexedMapOfShape aMS1, aMS2;
223   TopTools_DataMapOfShapeListOfShape aDMSLS;
224   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItDMSLS;
225   NMTTools_CoupleOfShape aCSS;
226   //
227   // 1. Checking the pair on whether it can be glued at all
228   // 1.1
229   const TopoDS_Shape& aS1=aCS.Shape1();
230   if (!myOriginsDetected.IsBound(aS1)) {
231     myErrorStatus=30;
232     return;
233   }
234   const TopoDS_Shape& aSkey1=myOriginsDetected.Find(aS1);
235   // 1.2
236   const TopoDS_Shape& aS2=aCS.Shape2();
237   if (!myOriginsDetected.IsBound(aS2)) {
238     myErrorStatus=30;
239     return;
240   }
241   const TopoDS_Shape& aSkey2=myOriginsDetected.Find(aS2);
242   // 1.3
243   if (!aSkey1.IsSame(aSkey2)) {
244     myErrorStatus=33;
245     return;
246   }
247   //
248   // 2. Append the pair to the aLCS
249   aLCS.Append(aCS);
250   //
251   // 3. Treatment the sub-shapes of the pair
252   aType=aS1.ShapeType();
253   if (aType==TopAbs_VERTEX) {
254     return;
255   }
256   aTypeS=TopAbs_EDGE;
257   if (aType==aTypeS) {
258     aTypeS=TopAbs_VERTEX;
259   }
260   //
261   MapShapes1(aS1, aTypeS, aMS1);
262   MapShapes1(aS2, aTypeS, aMS2);
263   //
264   aNbS1=aMS1.Extent();
265   aNbS2=aMS2.Extent();
266   if (aNbS1!=aNbS2) {
267     myErrorStatus=31;
268     return;
269   }
270   //
271   // 1.
272   for (i=1; i<=aNbS1; ++i) {
273     const TopoDS_Shape& aSS1=aMS1(i);
274     if (aMS2.Contains(aSS1)) {
275       continue;
276     }
277     //
278     if (!myOriginsDetected.IsBound(aSS1)) {
279       myErrorStatus=30;
280       return;
281     }
282     //
283     const TopoDS_Shape& aSkey=myOriginsDetected.Find(aSS1);
284     if (aDMSLS.IsBound(aSkey)) {
285       TopTools_ListOfShape& aLS=aDMSLS.ChangeFind(aSkey);
286       aLS.Append(aSS1);
287     }
288     else {
289       TopTools_ListOfShape aLS;
290       //
291       aLS.Append(aSS1);
292       aDMSLS.Bind(aSkey, aLS);
293     }
294   }
295   //
296   // 2.
297   for (i=1; i<=aNbS2; ++i) {
298     const TopoDS_Shape& aSS2=aMS2(i);
299     if (aMS1.Contains(aSS2)) {
300       continue;
301     }
302     //
303     if (!myOriginsDetected.IsBound(aSS2)) {
304       myErrorStatus=30;
305       return;
306     }
307     //
308     const TopoDS_Shape& aSkey=myOriginsDetected.Find(aSS2);
309     if (aDMSLS.IsBound(aSkey)) {
310       TopTools_ListOfShape& aLS=aDMSLS.ChangeFind(aSkey);
311       aLS.Append(aSS2);
312     }
313     else {
314       TopTools_ListOfShape aLS;
315       //
316       aLS.Append(aSS2);
317       aDMSLS.Bind(aSkey, aLS);
318     }
319   }
320   //
321   // 3.
322   aItDMSLS.Initialize(aDMSLS);
323   for (; aItDMSLS.More(); aItDMSLS.Next()) {
324     //const TopoDS_Shape& aSkey=aItDMSLS.Key();
325     const TopTools_ListOfShape& aLS=aItDMSLS.Value();
326     aNbS=aLS.Extent();
327     if (aNbS!=2) {
328       myErrorStatus=32;
329       return;
330     }
331     //
332     const TopoDS_Shape& aSS1=aLS.First();
333     const TopoDS_Shape& aSS2=aLS.Last();
334     aCSS.SetShape1(aSS1);
335     aCSS.SetShape2(aSS2);
336     TreatPair(aCSS, aLCS);
337   }
338 }
339 //=======================================================================
340 //function : MapShapes1
341 //purpose  :
342 //=======================================================================
343 void MapShapes1(const TopoDS_Shape& aS,
344                const TopAbs_ShapeEnum aType,
345                TopTools_IndexedMapOfShape& aM)
346 {
347   TopExp_Explorer aExp;
348
349   aExp.Init (aS, aType);
350   for ( ;aExp.More(); aExp.Next()) {
351     const TopoDS_Shape aSx=aExp.Current();
352     if (aType==TopAbs_EDGE) {
353       const TopoDS_Edge& aEx=*((TopoDS_Edge*)&aSx);
354       if (BRep_Tool::Degenerated(aEx)) {
355         continue;
356       }
357     }
358     aM.Add(aSx);
359   }
360 }