]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMAlgo_NEW/GEOMAlgo_Gluer2.cxx
Salome HOME
Internal issue 0022865: Restructurization of Partition packages.
[modules/geom.git] / src / GEOMAlgo_NEW / GEOMAlgo_Gluer2.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.cxx
23 // Author:      Peter KURNEV
24
25 #include <GEOMAlgo_Gluer2.hxx>
26
27 #include <TopAbs_ShapeEnum.hxx>
28
29 #include <TopoDS_Compound.hxx>
30 #include <TopoDS_Iterator.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <TopoDS_Vertex.hxx>
33
34 #include <BRep_Builder.hxx>
35 #include <TopExp.hxx>
36 #include <BRepLib.hxx>
37
38 #include <TopTools_MapOfShape.hxx>
39 #include <TopTools_MapIteratorOfMapOfShape.hxx>
40 #include <TopTools_DataMapOfShapeShape.hxx>
41 #include <TopTools_ListOfShape.hxx>
42 #include <TopTools_ListIteratorOfListOfShape.hxx>
43 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
45
46 #include <GEOMAlgo_GlueDetector.hxx>
47 #include <GEOMAlgo_Tools3D.hxx>
48
49 //=======================================================================
50 //function : GEOMAlgo_Gluer2
51 //purpose  : 
52 //=======================================================================
53 GEOMAlgo_Gluer2::GEOMAlgo_Gluer2()
54 :
55   GEOMAlgo_GluerAlgo(),
56   GEOMAlgo_BuilderShape()
57 {
58   myTolerance=0.0001;
59 }
60 //=======================================================================
61 //function : ~GEOMAlgo_Gluer2
62 //purpose  : 
63 //=======================================================================
64 GEOMAlgo_Gluer2::~GEOMAlgo_Gluer2()
65 {
66 }
67 //=======================================================================
68 //function : Clear
69 //purpose  : 
70 //=======================================================================
71 void GEOMAlgo_Gluer2::Clear()
72 {
73   myErrorStatus=0;
74   myWarningStatus=0;
75   //
76   GEOMAlgo_GluerAlgo::Clear();
77   //
78   myImagesDetected.Clear();
79   myOriginsDetected.Clear();
80   myShapesToGlue.Clear();
81   myImagesToWork.Clear();
82   myOriginsToWork.Clear();
83   myKeepNonSolids=Standard_False;
84 }
85 //=======================================================================
86 //function : SetShapesToGlue
87 //purpose  : 
88 //=======================================================================
89 void GEOMAlgo_Gluer2::SetShapesToGlue(const TopTools_DataMapOfShapeListOfShape& aM)
90 {
91   myShapesToGlue=aM;
92 }
93 //=======================================================================
94 //function : ShapesToGlue
95 //purpose  : 
96 //=======================================================================
97 const TopTools_DataMapOfShapeListOfShape& GEOMAlgo_Gluer2::ShapesToGlue()const
98 {
99   return myShapesToGlue;
100 }
101 //=======================================================================
102 //function : SetKeepNonSolids
103 //purpose  : 
104 //=======================================================================
105 void GEOMAlgo_Gluer2::SetKeepNonSolids(const Standard_Boolean aFlag)
106 {
107   myKeepNonSolids=aFlag;
108 }
109 //=======================================================================
110 //function : KeepNonSolids
111 //purpose  : 
112 //=======================================================================
113 Standard_Boolean GEOMAlgo_Gluer2::KeepNonSolids()const 
114 {
115   return myKeepNonSolids;
116 }
117 //=======================================================================
118 //function : ShapesDetected
119 //purpose  : 
120 //=======================================================================
121 const TopTools_DataMapOfShapeListOfShape& GEOMAlgo_Gluer2::ShapesDetected()const
122 {
123   return myImagesDetected;
124 }
125 //=======================================================================
126 //function : ImagesToWork
127 //purpose  : 
128 //=======================================================================
129 const TopTools_DataMapOfShapeListOfShape& GEOMAlgo_Gluer2::ImagesToWork()const
130 {
131   return myImagesToWork;
132 }
133 //=======================================================================
134 //function : Perform
135 //purpose  : 
136 //=======================================================================
137 void GEOMAlgo_Gluer2::Perform()
138 {
139   myErrorStatus=0;
140   myWarningStatus=0;
141   // 
142   CheckData();
143   if (myErrorStatus) {
144     return;
145   }
146   //
147   // Initialize the context
148   GEOMAlgo_GluerAlgo::Perform();
149   //
150   PerformShapesToWork();
151   if (myErrorStatus) {
152     return;
153   }
154   if (myWarningStatus==1) {// no shapes to glue
155     myShape=myArgument;
156     return;
157   }
158   //
159   FillVertices();
160   if (myErrorStatus) {
161     return;
162   }
163   //
164   FillEdges();
165   if (myErrorStatus) {
166     return;
167   }
168   //
169   FillWires();
170   if (myErrorStatus) {
171     return;
172   }
173   //
174   FillFaces();
175   if (myErrorStatus) {
176     return;
177   }
178   //
179   FillShells();
180   if (myErrorStatus) {
181     return;
182   }
183   //
184   FillSolids();
185   if (myErrorStatus) {
186     return;
187   }
188   //
189   FillCompSolids();
190   if (myErrorStatus) {
191     return;
192   }
193   //
194   FillCompounds();
195   if (myErrorStatus) {
196     return;
197   }
198   //
199   BuildResult();
200   if (myErrorStatus) {
201     return;
202   }
203   //
204   PrepareHistory();
205   if (myErrorStatus) {
206     return;
207   }
208   //
209   BRepLib::SameParameter(myShape, myTolerance, Standard_True);
210 }
211 //=======================================================================
212 //function : CheckData
213 //purpose  : 
214 //=======================================================================
215 void GEOMAlgo_Gluer2::CheckData()
216 {
217   Standard_Integer aNbSG, i;
218   TopAbs_ShapeEnum aType, aTypeX;
219   TopTools_ListIteratorOfListOfShape aItLS;
220   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItDMSLS;
221   //
222   myErrorStatus=0;
223   myWarningStatus=0;
224   //
225   aNbSG=myShapesToGlue.Extent();
226   if (aNbSG) {
227     // Check myShapesToGlue
228     aItDMSLS.Initialize(myShapesToGlue);
229     for (; aItDMSLS.More(); aItDMSLS.Next()) {
230       //const TopoDS_Shape& aSkey=aItDMSLS.Key();
231       const TopTools_ListOfShape& aLSG=aItDMSLS.Value();
232       aItLS.Initialize(aLSG);
233       for (i=0; aItLS.More(); aItLS.Next(), ++i) {
234         const TopoDS_Shape& aSG=aItLS.Value();
235         aTypeX=aSG.ShapeType();
236         if (!i) {
237           aType=aTypeX;
238           if (!(aType==TopAbs_VERTEX || 
239                 aType==TopAbs_EDGE || 
240                 aType==TopAbs_FACE)) {
241             myErrorStatus=21;// non-brep shapes
242             return;
243           }
244           continue;
245         }
246         if (aTypeX!=aType) {
247           myErrorStatus=20;// non-homogeneous shapes
248           return;
249         }
250       }
251     }
252   }// if (aNbSG) {
253 }
254 //=======================================================================
255 //function : FillEdges
256 //purpose  : 
257 //=======================================================================
258 void GEOMAlgo_Gluer2::FillEdges()
259 {
260   FillBRepShapes(TopAbs_EDGE);
261
262 //=======================================================================
263 //function : FillFaces
264 //purpose  : 
265 //=======================================================================
266 void GEOMAlgo_Gluer2::FillFaces()
267 {
268   FillBRepShapes(TopAbs_FACE);
269 }
270 //=======================================================================
271 //function : FillWires
272 //purpose  : 
273 //=======================================================================
274 void GEOMAlgo_Gluer2::FillWires()
275 {
276   FillContainers(TopAbs_WIRE);
277 }
278 //=======================================================================
279 //function : FillShells
280 //purpose  : 
281 //=======================================================================
282 void GEOMAlgo_Gluer2::FillShells()
283 {
284   FillContainers(TopAbs_SHELL);
285 }
286 //=======================================================================
287 //function : FillSolids
288 //purpose  : 
289 //=======================================================================
290 void GEOMAlgo_Gluer2::FillSolids()
291 {
292   FillContainers(TopAbs_SOLID);
293
294 //=======================================================================
295 //function : FillCompSolids
296 //purpose  : 
297 //=======================================================================
298 void GEOMAlgo_Gluer2::FillCompSolids()
299 {
300   FillContainers(TopAbs_COMPSOLID);
301
302 //=======================================================================
303 //function : FillVertices
304 //purpose  : 
305 //=======================================================================
306 void GEOMAlgo_Gluer2::FillVertices()
307 {
308   TopAbs_ShapeEnum aType;
309   TopoDS_Vertex aVnew;
310   TopTools_ListIteratorOfListOfShape aItLS;
311   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItDMSLS;
312   //
313   myErrorStatus=0;
314   myWarningStatus=0;
315   //
316   aItDMSLS.Initialize(myImagesToWork);
317   for (; aItDMSLS.More(); aItDMSLS.Next()) {
318     const TopoDS_Shape& aSkey=aItDMSLS.Key();
319     aType=aSkey.ShapeType();
320     if (aType!=TopAbs_VERTEX) {
321       continue;
322     }
323     //
324     const TopTools_ListOfShape& aLSD=aItDMSLS.Value();
325     //
326     GEOMAlgo_Gluer2::MakeVertex(aLSD, aVnew);
327     //
328     myImages.Bind(aVnew, aLSD);
329     //
330     aItLS.Initialize(aLSD);
331     for (; aItLS.More(); aItLS.Next()) {
332       const TopoDS_Shape& aV=aItLS.Value();
333       myOrigins.Bind(aV, aVnew);
334     }
335   }
336 }
337 //=======================================================================
338 //function : FillBRepShapes
339 //purpose  : 
340 //=======================================================================
341 void GEOMAlgo_Gluer2::FillBRepShapes(const TopAbs_ShapeEnum theType)
342
343   Standard_Boolean bHasImage, bIsToWork;
344   Standard_Integer i, aNbE;
345   TopoDS_Iterator aItS;
346   TopoDS_Shape aEnew;
347   TopTools_IndexedMapOfShape aME;
348   TopTools_MapOfShape aMFence;
349   TopTools_ListIteratorOfListOfShape aItLS;
350   //
351   myErrorStatus=0;
352   myWarningStatus=0;
353   //
354   TopExp::MapShapes(myArgument, theType, aME);
355   //
356   aNbE=aME.Extent();
357   for (i=1; i<=aNbE; ++i) {
358     const TopoDS_Shape& aE=aME(i);
359     //
360     if (!aMFence.Add(aE)) {
361       continue;
362     }
363     //
364     bIsToWork=myOriginsToWork.IsBound(aE);
365     bHasImage=HasImage(aE);
366     if (!bHasImage && !bIsToWork) {
367       continue;
368     }
369     //
370     MakeBRepShapes(aE, aEnew);
371     //
372     //myImages / myOrigins
373     if (bIsToWork) {
374       const TopoDS_Shape& aSkey=myOriginsToWork.Find(aE);
375       const TopTools_ListOfShape& aLSD=myImagesToWork.Find(aSkey);
376       //
377       myImages.Bind(aEnew, aLSD);
378       //
379       aItLS.Initialize(aLSD);
380       for (; aItLS.More(); aItLS.Next()) {
381         const TopoDS_Shape& aEx=aItLS.Value();
382         myOrigins.Bind(aEx, aEnew);
383         //
384         aMFence.Add(aEx);
385       }
386     }
387     else {
388       TopTools_ListOfShape aLSD;
389       //
390       aLSD.Append(aE);
391       myImages.Bind(aEnew, aLSD);
392       myOrigins.Bind(aE, aEnew);
393     }
394   }//for (i=1; i<=aNbF; ++i) {
395 }
396 //=======================================================================
397 //function : FillContainers
398 //purpose  : 
399 //=======================================================================
400 void GEOMAlgo_Gluer2::FillContainers(const TopAbs_ShapeEnum aType)
401 {
402   Standard_Boolean bHasImage, bToReverse;
403   Standard_Integer i, aNbW;
404   TopoDS_Shape aWnew, aEnew;
405   TopoDS_Iterator aItS;
406   BRep_Builder aBB;
407   TopTools_IndexedMapOfShape aMW;
408   TopTools_MapOfShape aMFence;
409   //
410   myErrorStatus=0;
411   myWarningStatus=0;
412   //
413   TopExp::MapShapes(myArgument, aType, aMW);
414   //
415   aNbW=aMW.Extent();
416   for (i=1; i<=aNbW; ++i) {
417     const TopoDS_Shape& aW=aMW(i);
418     //
419     if (!aMFence.Add(aW)) {
420       continue;
421     }
422     //
423     bHasImage=HasImage(aW);
424     if (!bHasImage) {
425       continue;
426     }
427     //
428     GEOMAlgo_Tools3D::MakeContainer(aType, aWnew);
429     //modified by NIZNHY-PKV Tue May 10 13:46:30 2011f
430     aWnew.Orientation(aW.Orientation());
431     //modified by NIZNHY-PKV Tue May 10 13:46:32 2011t
432     //
433     aItS.Initialize(aW);
434     for (; aItS.More(); aItS.Next()) {
435       const TopoDS_Shape& aE=aItS.Value();
436       if (myOrigins.IsBound(aE)) {
437         aEnew=myOrigins.Find(aE);
438         //
439         bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aEnew, aE, myContext);
440         if (bToReverse) {
441           aEnew.Reverse();
442         }
443         //
444         aBB.Add(aWnew, aEnew);
445       }
446       else {
447         aBB.Add(aWnew, aE);
448       }
449     }
450     //
451     //modified by NIZNHY-PKV Tue May 10 13:46:19 2011f
452     //aWnew.Orientation(aW.Orientation());
453     //modified by NIZNHY-PKV Tue May 10 13:46:22 2011t
454     //
455     //myImages / myOrigins
456     TopTools_ListOfShape aLSD;
457     //
458     aLSD.Append(aW);
459     myImages.Bind(aWnew, aLSD);
460     myOrigins.Bind(aW, aWnew);
461     //
462   }//for (i=1; i<=aNbE; ++i) {
463 }
464 //=======================================================================
465 //function : FillCompounds
466 //purpose  : 
467 //=======================================================================
468 void GEOMAlgo_Gluer2::FillCompounds()
469 {
470   TopAbs_ShapeEnum aType;
471   TopoDS_Iterator aItC;
472   //
473   myErrorStatus=0;
474   myWarningStatus=0;
475   //
476   aItC.Initialize(myArgument);
477   for (; aItC.More(); aItC.Next()) {
478     const TopoDS_Shape& aCx=aItC.Value();
479     aType=aCx.ShapeType();
480     if (aType==TopAbs_COMPOUND) {
481       FillCompound(aCx);
482     }
483   }
484 }
485 //=======================================================================
486 //function : FillCompound
487 //purpose  : 
488 //=======================================================================
489 void GEOMAlgo_Gluer2::FillCompound(const TopoDS_Shape& aC)
490 {
491   Standard_Boolean bHasImage;
492   TopAbs_ShapeEnum aType;
493   TopoDS_Shape aCnew, aCXnew;
494   TopoDS_Iterator aItC;
495   BRep_Builder aBB;
496   //
497   bHasImage=HasImage(aC);
498   if (!bHasImage) {
499     return;
500   }
501   //
502   GEOMAlgo_Tools3D::MakeContainer(TopAbs_COMPOUND, aCnew);
503   //
504   aItC.Initialize(aC);
505   for (; aItC.More(); aItC.Next()) {
506     const TopoDS_Shape& aCX=aItC.Value();
507     aType=aCX.ShapeType();
508     //
509     if (aType==TopAbs_COMPOUND) {
510       FillCompound(aCX);
511     }
512     //
513     if (myOrigins.IsBound(aCX)) {
514       aCXnew=myOrigins.Find(aCX);
515       aCXnew.Orientation(aCX.Orientation());
516       aBB.Add(aCnew, aCXnew);
517     }
518     else {
519       aBB.Add(aCnew, aCX);
520     }
521   }
522   //
523   //myImages / myOrigins
524   TopTools_ListOfShape aLSD;
525   //
526   aLSD.Append(aC);
527   myImages.Bind(aCnew, aLSD);
528   myOrigins.Bind(aC, aCnew);
529 }
530 //=======================================================================
531 //function : HasImage
532 //purpose  : 
533 //=======================================================================
534 Standard_Boolean GEOMAlgo_Gluer2::HasImage(const TopoDS_Shape& aC)
535 {
536   Standard_Boolean bRet;
537   TopAbs_ShapeEnum aType;
538   TopoDS_Iterator aItC;
539   //
540   bRet=Standard_False;
541   aItC.Initialize(aC);
542   for (; aItC.More(); aItC.Next()) {
543     const TopoDS_Shape& aCx=aItC.Value();
544     aType=aCx.ShapeType();
545     //
546     if (aType==TopAbs_COMPOUND) {
547       bRet=HasImage(aCx);
548       if (bRet) {
549         return bRet;
550       }
551     }
552     else {
553       bRet=myOrigins.IsBound(aCx);
554       if (bRet) {
555         return bRet;
556       }
557     }
558   }
559   //
560   bRet=myOrigins.IsBound(aC);
561   //
562   return bRet;
563 }
564 //=======================================================================
565 //function : BuildResult
566 //purpose  : 
567 //=======================================================================
568 void GEOMAlgo_Gluer2::BuildResult()
569 {
570   Standard_Boolean bHasImage;
571   TopoDS_Shape aCnew, aCXnew;
572   TopoDS_Iterator aItC;
573   BRep_Builder aBB;
574   //
575   myErrorStatus=0;
576   myWarningStatus=0;
577   //
578   aItC.Initialize(myArgument);
579   for (; aItC.More(); aItC.Next()) {
580     const TopoDS_Shape& aCx=aItC.Value();
581     bHasImage=HasImage(aCx);
582     if (bHasImage) {
583       break;
584     }
585   }
586   //
587   if (!bHasImage) {
588     myShape=myArgument;
589     return;
590   }
591   //
592   GEOMAlgo_Tools3D::MakeContainer(TopAbs_COMPOUND, aCnew);
593   //
594   aItC.Initialize(myArgument);
595   for (; aItC.More(); aItC.Next()) {
596     const TopoDS_Shape& aCX=aItC.Value();
597     if (myOrigins.IsBound(aCX)) {
598       aCXnew=myOrigins.Find(aCX);
599       aCXnew.Orientation(aCX.Orientation());
600       aBB.Add(aCnew, aCXnew);
601     }
602     else {
603       aBB.Add(aCnew, aCX);
604     }
605   }
606   //
607   if (!myKeepNonSolids) {
608     Standard_Integer i, aNb;
609     TopoDS_Shape aCnew1;
610     TopTools_IndexedMapOfShape aM;
611     //
612     GEOMAlgo_Tools3D::MakeContainer(TopAbs_COMPOUND, aCnew1);
613     //
614     TopExp::MapShapes(aCnew, TopAbs_SOLID, aM);
615     
616     aNb=aM.Extent();
617     for (i=1; i<=aNb; ++i) {
618       const TopoDS_Shape& aS=aM(i);
619       aBB.Add(aCnew1, aS);
620     }
621     aCnew=aCnew1;
622   }
623   //
624   myShape=aCnew;
625 }
626 //--------------------------------------------------------
627 //
628 // ErrorStatus
629 // 11   - GEOMAlgo_GlueDetector failed
630 // 13   - PerformImagesToWork failed
631 // 14   - PerformImagesToWork failed
632 //
633 // WarningStatus
634 // 1    - no shapes to glue