Salome HOME
Merge from V5_1_main 14/05/2010
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Builder_1.cxx
1 //  Copyright (C) 2007-2010  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_Builder_1.cxx
24 // Created:     
25 // Author:      Peter KURNEV 
26 //
27 #include <GEOMAlgo_Builder.hxx>
28 //
29 #include <TColStd_ListOfInteger.hxx>
30
31 #include <TopoDS.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <TopoDS_Wire.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Shell.hxx>
36 #include <TopoDS_Iterator.hxx>
37 //
38 #include <TopTools_MapOfShape.hxx>
39 #include <TopTools_ListOfShape.hxx>
40 #include <TopTools_ListIteratorOfListOfShape.hxx>
41 #include <TopTools_MapIteratorOfMapOfShape.hxx>
42 //
43 #include <BRep_Tool.hxx>
44 #include <BRep_Builder.hxx>
45 //
46 #include <IntTools_Context.hxx>
47 #include <BOPTools_ListIteratorOfListOfPaveBlock.hxx>
48 #include <BOPTools_ListOfPaveBlock.hxx>
49 #include <BOPTools_PaveBlock.hxx>
50 //
51 #include <NMTDS_ShapesDataStructure.hxx>
52 //
53 #include <NMTTools_PaveFiller.hxx>
54 #include <NMTTools_CommonBlockPool.hxx>
55 #include <NMTTools_ListIteratorOfListOfCommonBlock.hxx>
56 #include <NMTTools_CommonBlock.hxx>
57 #include <NMTTools_CommonBlockPool.hxx>
58 #include <NMTTools_ListOfCommonBlock.hxx>
59 #include <NMTTools_CommonBlockAPI.hxx>
60 //
61 #include <GEOMAlgo_Tools3D.hxx>
62 #include <TColStd_ListIteratorOfListOfInteger.hxx>
63
64
65
66 static
67   void FillImagesCompounds(const TopTools_MapOfShape& ,
68                            BRepAlgo_Image& );
69
70 static
71   void FillImagesCompound(const TopoDS_Shape& ,
72                         BRepAlgo_Image& ,
73                         TopTools_MapOfShape& );
74
75 //=======================================================================
76 //function : FillImagesVertices
77 //purpose  : 
78 //=======================================================================
79   void GEOMAlgo_Builder::FillImagesVertices()
80 {
81   myErrorStatus=0;
82   //
83   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
84   NMTTools_PaveFiller* pPF=myPaveFiller;
85   //
86   Standard_Integer i, aNb, iV;
87   //
88   aNb=aDS.NumberOfShapesOfTheObject();
89   for (i=1; i<=aNb; ++i) {
90     const TopoDS_Shape& aV=aDS.Shape(i);
91     if (aV.ShapeType()==TopAbs_VERTEX) {
92       iV=pPF->FindSDVertex(i);
93       if (iV) {
94         const TopoDS_Shape& aVSD=aDS.Shape(iV);
95         if (!myImages.HasImage(aV)) {
96           myImages.Bind(aV, aVSD);
97           //
98           mySameDomainShapes.Add(aV, aVSD);
99         }
100       }
101     }
102   }
103 }
104 //=======================================================================
105 // function: FillImagesEdges
106 // purpose: 
107 //=======================================================================
108   void GEOMAlgo_Builder::FillImagesEdges()
109 {
110   myErrorStatus=0;
111   //
112   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
113   NMTTools_PaveFiller* pPF=myPaveFiller;
114   const BOPTools_SplitShapesPool& aSSP=pPF->SplitShapesPool();
115   NMTTools_CommonBlockPool& aCBP=pPF->ChangeCommonBlockPool();
116   IntTools_Context& aCtx=pPF->ChangeContext();
117   //
118   Standard_Boolean bToReverse;
119   Standard_Integer i, aNb, aNbSp, nSp, nSpR, nSpx, aIsCB;
120   TColStd_ListIteratorOfListOfInteger aItLB;
121   TColStd_ListOfInteger aLB;
122   TopoDS_Edge aEE, aESpR;
123   TopTools_MapOfShape aMFence;
124   TopTools_ListOfShape aLSp;
125   TopTools_ListIteratorOfListOfShape aIt1;
126   BOPTools_ListIteratorOfListOfPaveBlock aIt;
127   //
128   aNb=aDS.NumberOfShapesOfTheObject();
129   for (i=1; i<=aNb; ++i) {
130     const TopoDS_Shape& aE=aDS.Shape(i);
131     if (aE.ShapeType()!=TopAbs_EDGE) {
132       continue;
133     }
134     //
135     if (!aMFence.Add(aE)) {
136       continue;
137     }
138     //
139     const BOPTools_ListOfPaveBlock& aLPB=aSSP(aDS.RefEdge(i));
140     aNbSp=aLPB.Extent();
141     if (!aNbSp) {
142       continue;
143     }
144     //
145     aEE=TopoDS::Edge(aE);
146     aLSp.Clear();
147     //
148     if (aNbSp==1) {
149       const BOPTools_PaveBlock& aPB=aLPB.First();
150       nSp=aPB.Edge();
151       const TopoDS_Shape& aSp=aDS.Shape(nSp);
152       //
153       //modified by NIZNHY-PKV Fri Nov 30 10:40:36 2007 f
154       //const BOPTools_PaveBlock& aPBR=pPF->RealPaveBlock(aPB, aLB);
155       const BOPTools_PaveBlock& aPBR=pPF->RealPaveBlock(aPB, aLB, aIsCB);
156       //modified by NIZNHY-PKV Fri Nov 30 10:40:48 2007t
157       nSpR=aPBR.Edge();
158       const TopoDS_Shape& aSpR=aDS.Shape(nSpR);
159       //modified by NIZNHY-PKV Fri Nov 30 10:41:39 2007f
160       //if (aSpR.IsSame(aSp) && aSpR.IsSame(aE)) {
161       if (aSpR.IsSame(aSp) && aSpR.IsSame(aE) && !aIsCB) {
162         //modified by NIZNHY-PKV Fri Nov 30 10:41:46 2007t
163         continue;
164       }
165       //
166       aESpR=TopoDS::Edge(aSpR);
167       bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aESpR, aEE, aCtx);
168       if (bToReverse) {
169         aESpR.Reverse();
170       }
171       aLSp.Append(aESpR);
172       //
173       aItLB.Initialize(aLB);
174       for (; aItLB.More(); aItLB.Next()) {
175         nSpx=aItLB.Value();
176         const TopoDS_Shape& aSpx=aDS.Shape(nSpx);
177         mySameDomainShapes.Add(aSpx ,aSpR);
178       }
179       //
180       //
181     }// if (aNbSp==1) {
182     else {
183       aIt.Initialize(aLPB);
184       for (; aIt.More(); aIt.Next()) {
185         const BOPTools_PaveBlock& aPB=aIt.Value();
186         //modified by NIZNHY-PKV Fri Nov 30 10:42:15 2007f
187         //const BOPTools_PaveBlock& aPBR=pPF->RealPaveBlock(aPB, aLB);
188         const BOPTools_PaveBlock& aPBR=pPF->RealPaveBlock(aPB, aLB, aIsCB);
189         //modified by NIZNHY-PKV Fri Nov 30 10:42:20 2007t
190         nSpR=aPBR.Edge();
191         const TopoDS_Shape& aSpR=aDS.Shape(nSpR);
192         //
193         aESpR=TopoDS::Edge(aSpR);
194         bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aESpR, aEE, aCtx);
195         if (bToReverse) {
196           aESpR.Reverse();
197         }
198         aLSp.Append(aESpR);
199         //
200         aItLB.Initialize(aLB);
201         for (; aItLB.More(); aItLB.Next()) {
202           nSpx=aItLB.Value();
203           const TopoDS_Shape& aSpx=aDS.Shape(nSpx);
204           mySameDomainShapes.Add(aSpx ,aSpR);
205         }
206         //
207       }
208     }
209     //
210     myImages.Bind(aE, aLSp);
211     
212   }//for (i=1; i<=aNb; ++i)
213 }
214 //=======================================================================
215 // function: FillImagesContainers
216 // purpose: 
217 //=======================================================================
218   void GEOMAlgo_Builder::FillImagesContainers(const TopAbs_ShapeEnum theType)
219 {
220   myErrorStatus=0;
221   //
222   Standard_Boolean bInterferred, bToReverse;
223   Standard_Integer i, aNbS;
224   TopAbs_ShapeEnum aType;
225   BRep_Builder aBB;
226   TopoDS_Iterator aIt;
227   TopTools_ListIteratorOfListOfShape aItIm; 
228   TopTools_MapOfShape aMS;
229   TopTools_MapIteratorOfMapOfShape aItS;
230   //
231   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
232   NMTTools_PaveFiller* pPF=myPaveFiller;
233   IntTools_Context& aCtx= pPF->ChangeContext();
234   //
235   aNbS=aDS.NumberOfShapesOfTheObject();
236   for (i=1; i<=aNbS; ++i) {
237     const TopoDS_Shape& aC=aDS.Shape(i);
238     aType=aC.ShapeType();
239     if (aType==theType) {
240       aMS.Add(aC);
241     }
242   }
243   //
244   if (theType==TopAbs_COMPOUND) {
245     FillImagesCompounds(aMS, myImages);
246     return;
247   }
248   //
249   aItS.Initialize(aMS);
250   for (; aItS.More(); aItS.Next()) {
251     const TopoDS_Shape& aC=aItS.Key();
252     // whether the shape has image
253     bInterferred=Standard_False;
254     aIt.Initialize(aC);
255     for (; aIt.More(); aIt.Next()) {
256       const TopoDS_Shape& aF=aIt.Value();
257       if (myImages.HasImage(aF)) {
258         bInterferred=!bInterferred;
259         break;
260       }
261     }
262     if (!bInterferred){
263       continue;
264     }
265     //
266     TopoDS_Shape aCIm;
267     GEOMAlgo_Tools3D::MakeContainer(theType, aCIm);
268     //
269     aIt.Initialize(aC);
270     for (; aIt.More(); aIt.Next()) {
271       const TopoDS_Shape& aF=aIt.Value();
272       if (myImages.HasImage(aF)) {
273         const TopTools_ListOfShape& aLFIm=myImages.Image(aF);
274         aItIm.Initialize(aLFIm);
275         for (; aItIm.More(); aItIm.Next()) {
276           TopoDS_Shape aFIm=aItIm.Value();
277           //
278           bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aFIm, aF, aCtx);
279           if (bToReverse) {
280             aFIm.Reverse();
281           }
282           aBB.Add(aCIm, aFIm);
283         }
284       }
285       else {
286         aBB.Add(aCIm, aF);
287       }
288     }
289     myImages.Bind(aC, aCIm); 
290   }// for (; aItS.More(); aItS.Next()) {
291 }
292 //=======================================================================
293 // function: FillImagesCompounds
294 // purpose: 
295 //=======================================================================
296 void FillImagesCompounds(const TopTools_MapOfShape& theMS,
297                          BRepAlgo_Image& theImages)
298 {
299   TopTools_MapOfShape aMFP;
300   TopTools_MapIteratorOfMapOfShape aItS;
301   //
302   aItS.Initialize(theMS);
303   for (; aItS.More(); aItS.Next()) {
304     const TopoDS_Shape& aC=aItS.Key();
305     FillImagesCompound(aC, theImages, aMFP);
306   }
307 }
308 //=======================================================================
309 //function : FillImagesCompound
310 //purpose  : 
311 //=======================================================================
312 void FillImagesCompound(const TopoDS_Shape& theS,
313                         BRepAlgo_Image& theImages,
314                         TopTools_MapOfShape& theMFP)
315
316   Standard_Boolean bInterferred;
317   TopAbs_ShapeEnum aTypeX;
318   TopAbs_Orientation aOrX;
319   TopoDS_Iterator aIt;
320   BRep_Builder aBB;
321   TopTools_ListIteratorOfListOfShape aItIm; 
322   //
323   if (!theMFP.Add(theS)) {
324     return;
325   }
326   //
327   bInterferred=Standard_False;
328   aIt.Initialize(theS);
329   for (; aIt.More(); aIt.Next()) {
330     const TopoDS_Shape& aSX=aIt.Value();
331     aTypeX=aSX.ShapeType();
332     if (aTypeX==TopAbs_COMPOUND) {
333       FillImagesCompound(aSX, theImages, theMFP);
334     }
335     if (theImages.HasImage(aSX)) {
336       bInterferred=Standard_True;
337     }
338   }
339   if (!bInterferred){
340     return;
341   }
342   //
343   TopoDS_Shape aCIm;
344   GEOMAlgo_Tools3D::MakeContainer(TopAbs_COMPOUND, aCIm);
345   //
346   aIt.Initialize(theS);
347   for (; aIt.More(); aIt.Next()) {
348     const TopoDS_Shape& aSX=aIt.Value();
349     aOrX=aSX.Orientation();
350     if (theImages.HasImage(aSX)) {
351       const TopTools_ListOfShape& aLFIm=theImages.Image(aSX);
352       aItIm.Initialize(aLFIm);
353       for (; aItIm.More(); aItIm.Next()) {
354         TopoDS_Shape aSXIm=aItIm.Value();
355         aSXIm.Orientation(aOrX);
356         aBB.Add(aCIm, aSXIm);
357       }
358     }
359     else {
360       aBB.Add(aCIm, aSX);
361     }
362   }
363   theImages.Bind(theS, aCIm); 
364 }