Salome HOME
Correct precision of double values displaying (bug 16123).
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Builder_1.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File:        GEOMAlgo_Builder_1.cxx
21 // Created:     
22 // Author:      Peter KURNEV 
23
24 #include <GEOMAlgo_Builder.hxx>
25 //
26 #include <TColStd_ListOfInteger.hxx>
27
28 #include <TopoDS.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopoDS_Wire.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <TopoDS_Shell.hxx>
33 #include <TopoDS_Iterator.hxx>
34 //
35 #include <TopTools_MapOfShape.hxx>
36 #include <TopTools_ListOfShape.hxx>
37 #include <TopTools_ListIteratorOfListOfShape.hxx>
38 #include <TopTools_MapIteratorOfMapOfShape.hxx>
39 //
40 #include <BRep_Tool.hxx>
41 #include <BRep_Builder.hxx>
42 //
43 #include <IntTools_Context.hxx>
44 #include <BOPTools_ListIteratorOfListOfPaveBlock.hxx>
45 #include <BOPTools_ListOfPaveBlock.hxx>
46 #include <BOPTools_PaveBlock.hxx>
47 //
48 #include <NMTDS_ShapesDataStructure.hxx>
49 //
50 #include <NMTTools_PaveFiller.hxx>
51 #include <NMTTools_CommonBlockPool.hxx>
52 #include <NMTTools_ListIteratorOfListOfCommonBlock.hxx>
53 #include <NMTTools_CommonBlock.hxx>
54 #include <NMTTools_CommonBlockPool.hxx>
55 #include <NMTTools_ListOfCommonBlock.hxx>
56 #include <NMTTools_CommonBlockAPI.hxx>
57 //
58 #include <GEOMAlgo_Tools3D.hxx>
59 #include <TColStd_ListIteratorOfListOfInteger.hxx>
60
61
62
63 static
64   void FillImagesCompounds(const TopTools_MapOfShape& ,
65                            BRepAlgo_Image& );
66
67 static
68   void FillImagesCompound(const TopoDS_Shape& ,
69                         BRepAlgo_Image& ,
70                         TopTools_MapOfShape& );
71
72 //=======================================================================
73 //function : FillImagesVertices
74 //purpose  : 
75 //=======================================================================
76   void GEOMAlgo_Builder::FillImagesVertices()
77 {
78   myErrorStatus=0;
79   //
80   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
81   NMTTools_PaveFiller* pPF=myPaveFiller;
82   //
83   Standard_Integer i, aNb, iV;
84   //
85   aNb=aDS.NumberOfShapesOfTheObject();
86   for (i=1; i<=aNb; ++i) {
87     const TopoDS_Shape& aV=aDS.Shape(i);
88     if (aV.ShapeType()==TopAbs_VERTEX) {
89       iV=pPF->FindSDVertex(i);
90       if (iV) {
91         const TopoDS_Shape& aVSD=aDS.Shape(iV);
92         if (!myImages.HasImage(aV)) {
93           myImages.Bind(aV, aVSD);
94           //
95           mySameDomainShapes.Add(aV, aVSD);
96         }
97       }
98     }
99   }
100 }
101 //=======================================================================
102 // function: FillImagesEdges
103 // purpose: 
104 //=======================================================================
105   void GEOMAlgo_Builder::FillImagesEdges()
106 {
107   myErrorStatus=0;
108   //
109   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
110   NMTTools_PaveFiller* pPF=myPaveFiller;
111   const BOPTools_SplitShapesPool& aSSP=pPF->SplitShapesPool();
112   NMTTools_CommonBlockPool& aCBP=pPF->ChangeCommonBlockPool();
113   IntTools_Context& aCtx=pPF->ChangeContext();
114   //
115   Standard_Boolean bToReverse;
116   Standard_Integer i, aNb, aNbSp, nSp, nSpR, nSpx;
117   TColStd_ListIteratorOfListOfInteger aItLB;
118   TColStd_ListOfInteger aLB;
119   TopoDS_Edge aEE, aESpR;
120   TopTools_MapOfShape aMFence;
121   TopTools_ListOfShape aLSp;
122   TopTools_ListIteratorOfListOfShape aIt1;
123   BOPTools_ListIteratorOfListOfPaveBlock aIt;
124   //
125   aNb=aDS.NumberOfShapesOfTheObject();
126   for (i=1; i<=aNb; ++i) {
127     const TopoDS_Shape& aE=aDS.Shape(i);
128     if (aE.ShapeType()!=TopAbs_EDGE) {
129       continue;
130     }
131     //
132     if (!aMFence.Add(aE)) {
133       continue;
134     }
135     //
136     const BOPTools_ListOfPaveBlock& aLPB=aSSP(aDS.RefEdge(i));
137     aNbSp=aLPB.Extent();
138     if (!aNbSp) {
139       continue;
140     }
141     //
142     aEE=TopoDS::Edge(aE);
143     aLSp.Clear();
144     //
145     if (aNbSp==1) {
146       const BOPTools_PaveBlock& aPB=aLPB.First();
147       nSp=aPB.Edge();
148       const TopoDS_Shape& aSp=aDS.Shape(nSp);
149       //
150       const BOPTools_PaveBlock& aPBR=pPF->RealPaveBlock(aPB, aLB);
151       nSpR=aPBR.Edge();
152       const TopoDS_Shape& aSpR=aDS.Shape(nSpR);
153       if (aSpR.IsSame(aSp) && aSpR.IsSame(aE)) {
154         continue;
155       }
156       //
157       aESpR=TopoDS::Edge(aSpR);
158       bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aESpR, aEE, aCtx);
159       if (bToReverse) {
160         aESpR.Reverse();
161       }
162       aLSp.Append(aESpR);
163       //
164       aItLB.Initialize(aLB);
165       for (; aItLB.More(); aItLB.Next()) {
166         nSpx=aItLB.Value();
167         const TopoDS_Shape& aSpx=aDS.Shape(nSpx);
168         mySameDomainShapes.Add(aSpx ,aSpR);
169       }
170       //
171       //
172     }// if (aNbSp==1) {
173     else {
174       aIt.Initialize(aLPB);
175       for (; aIt.More(); aIt.Next()) {
176         const BOPTools_PaveBlock& aPB=aIt.Value();
177         const BOPTools_PaveBlock& aPBR=pPF->RealPaveBlock(aPB, aLB);
178         nSpR=aPBR.Edge();
179         const TopoDS_Shape& aSpR=aDS.Shape(nSpR);
180         //
181         aESpR=TopoDS::Edge(aSpR);
182         bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aESpR, aEE, aCtx);
183         if (bToReverse) {
184           aESpR.Reverse();
185         }
186         aLSp.Append(aESpR);
187         //
188         aItLB.Initialize(aLB);
189         for (; aItLB.More(); aItLB.Next()) {
190           nSpx=aItLB.Value();
191           const TopoDS_Shape& aSpx=aDS.Shape(nSpx);
192           mySameDomainShapes.Add(aSpx ,aSpR);
193         }
194         //
195       }
196     }
197     //
198     myImages.Bind(aE, aLSp);
199     
200   }//for (i=1; i<=aNb; ++i)
201 }
202 //=======================================================================
203 // function: FillImagesContainers
204 // purpose: 
205 //=======================================================================
206   void GEOMAlgo_Builder::FillImagesContainers(const TopAbs_ShapeEnum theType)
207 {
208   myErrorStatus=0;
209   //
210   Standard_Boolean bInterferred, bToReverse;
211   Standard_Integer i, aNbS;
212   TopAbs_ShapeEnum aType;
213   BRep_Builder aBB;
214   TopoDS_Iterator aIt;
215   TopTools_ListIteratorOfListOfShape aItIm; 
216   TopTools_MapOfShape aMS;
217   TopTools_MapIteratorOfMapOfShape aItS;
218   //
219   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
220   NMTTools_PaveFiller* pPF=myPaveFiller;
221   IntTools_Context& aCtx= pPF->ChangeContext();
222   //
223   aNbS=aDS.NumberOfShapesOfTheObject();
224   for (i=1; i<=aNbS; ++i) {
225     const TopoDS_Shape& aC=aDS.Shape(i);
226     aType=aC.ShapeType();
227     if (aType==theType) {
228       aMS.Add(aC);
229     }
230   }
231   //
232   if (theType==TopAbs_COMPOUND) {
233     FillImagesCompounds(aMS, myImages);
234     return;
235   }
236   //
237   aItS.Initialize(aMS);
238   for (; aItS.More(); aItS.Next()) {
239     const TopoDS_Shape& aC=aItS.Key();
240     // whether the shape has image
241     bInterferred=Standard_False;
242     aIt.Initialize(aC);
243     for (; aIt.More(); aIt.Next()) {
244       const TopoDS_Shape& aF=aIt.Value();
245       if (myImages.HasImage(aF)) {
246         bInterferred=!bInterferred;
247         break;
248       }
249     }
250     if (!bInterferred){
251       continue;
252     }
253     //
254     TopoDS_Shape aCIm;
255     GEOMAlgo_Tools3D::MakeContainer(theType, aCIm);
256     //
257     aIt.Initialize(aC);
258     for (; aIt.More(); aIt.Next()) {
259       const TopoDS_Shape& aF=aIt.Value();
260       if (myImages.HasImage(aF)) {
261         const TopTools_ListOfShape& aLFIm=myImages.Image(aF);
262         aItIm.Initialize(aLFIm);
263         for (; aItIm.More(); aItIm.Next()) {
264           TopoDS_Shape aFIm=aItIm.Value();
265           //
266           bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aFIm, aF, aCtx);
267           if (bToReverse) {
268             aFIm.Reverse();
269           }
270           aBB.Add(aCIm, aFIm);
271         }
272       }
273       else {
274         aBB.Add(aCIm, aF);
275       }
276     }
277     myImages.Bind(aC, aCIm); 
278   }// for (; aItS.More(); aItS.Next()) {
279 }
280 //=======================================================================
281 // function: FillImagesCompounds
282 // purpose: 
283 //=======================================================================
284 void FillImagesCompounds(const TopTools_MapOfShape& theMS,
285                          BRepAlgo_Image& theImages)
286 {
287   TopTools_MapOfShape aMFP;
288   TopTools_MapIteratorOfMapOfShape aItS;
289   //
290   aItS.Initialize(theMS);
291   for (; aItS.More(); aItS.Next()) {
292     const TopoDS_Shape& aC=aItS.Key();
293     FillImagesCompound(aC, theImages, aMFP);
294   }
295 }
296 //=======================================================================
297 //function : FillImagesCompound
298 //purpose  : 
299 //=======================================================================
300 void FillImagesCompound(const TopoDS_Shape& theS,
301                         BRepAlgo_Image& theImages,
302                         TopTools_MapOfShape& theMFP)
303
304   Standard_Boolean bInterferred;
305   TopAbs_ShapeEnum aTypeX;
306   TopAbs_Orientation aOrX;
307   TopoDS_Iterator aIt;
308   BRep_Builder aBB;
309   TopTools_ListIteratorOfListOfShape aItIm; 
310   //
311   if (!theMFP.Add(theS)) {
312     return;
313   }
314   //
315   bInterferred=Standard_False;
316   aIt.Initialize(theS);
317   for (; aIt.More(); aIt.Next()) {
318     const TopoDS_Shape& aSX=aIt.Value();
319     aTypeX=aSX.ShapeType();
320     if (aTypeX==TopAbs_COMPOUND) {
321       FillImagesCompound(aSX, theImages, theMFP);
322     }
323     if (theImages.HasImage(aSX)) {
324       bInterferred=Standard_True;
325     }
326   }
327   if (!bInterferred){
328     return;
329   }
330   //
331   TopoDS_Shape aCIm;
332   GEOMAlgo_Tools3D::MakeContainer(TopAbs_COMPOUND, aCIm);
333   //
334   aIt.Initialize(theS);
335   for (; aIt.More(); aIt.Next()) {
336     const TopoDS_Shape& aSX=aIt.Value();
337     aOrX=aSX.Orientation();
338     if (theImages.HasImage(aSX)) {
339       const TopTools_ListOfShape& aLFIm=theImages.Image(aSX);
340       aItIm.Initialize(aLFIm);
341       for (; aItIm.More(); aItIm.Next()) {
342         TopoDS_Shape aSXIm=aItIm.Value();
343         aSXIm.Orientation(aOrX);
344         aBB.Add(aCIm, aSXIm);
345       }
346     }
347     else {
348       aBB.Add(aCIm, aSX);
349     }
350   }
351   theImages.Bind(theS, aCIm); 
352 }