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