Salome HOME
Merge from BR_Dev_For_4_0 branch (from tag mergeto_BR_QT4_Dev_17Jan08)
[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, aIsCB;
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       //modified by NIZNHY-PKV Fri Nov 30 10:40:36 2007 f
151       //const BOPTools_PaveBlock& aPBR=pPF->RealPaveBlock(aPB, aLB);
152       const BOPTools_PaveBlock& aPBR=pPF->RealPaveBlock(aPB, aLB, aIsCB);
153       //modified by NIZNHY-PKV Fri Nov 30 10:40:48 2007t
154       nSpR=aPBR.Edge();
155       const TopoDS_Shape& aSpR=aDS.Shape(nSpR);
156       //modified by NIZNHY-PKV Fri Nov 30 10:41:39 2007f
157       //if (aSpR.IsSame(aSp) && aSpR.IsSame(aE)) {
158       if (aSpR.IsSame(aSp) && aSpR.IsSame(aE) && !aIsCB) {
159         //modified by NIZNHY-PKV Fri Nov 30 10:41:46 2007t
160         continue;
161       }
162       //
163       aESpR=TopoDS::Edge(aSpR);
164       bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aESpR, aEE, aCtx);
165       if (bToReverse) {
166         aESpR.Reverse();
167       }
168       aLSp.Append(aESpR);
169       //
170       aItLB.Initialize(aLB);
171       for (; aItLB.More(); aItLB.Next()) {
172         nSpx=aItLB.Value();
173         const TopoDS_Shape& aSpx=aDS.Shape(nSpx);
174         mySameDomainShapes.Add(aSpx ,aSpR);
175       }
176       //
177       //
178     }// if (aNbSp==1) {
179     else {
180       aIt.Initialize(aLPB);
181       for (; aIt.More(); aIt.Next()) {
182         const BOPTools_PaveBlock& aPB=aIt.Value();
183         //modified by NIZNHY-PKV Fri Nov 30 10:42:15 2007f
184         //const BOPTools_PaveBlock& aPBR=pPF->RealPaveBlock(aPB, aLB);
185         const BOPTools_PaveBlock& aPBR=pPF->RealPaveBlock(aPB, aLB, aIsCB);
186         //modified by NIZNHY-PKV Fri Nov 30 10:42:20 2007t
187         nSpR=aPBR.Edge();
188         const TopoDS_Shape& aSpR=aDS.Shape(nSpR);
189         //
190         aESpR=TopoDS::Edge(aSpR);
191         bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aESpR, aEE, aCtx);
192         if (bToReverse) {
193           aESpR.Reverse();
194         }
195         aLSp.Append(aESpR);
196         //
197         aItLB.Initialize(aLB);
198         for (; aItLB.More(); aItLB.Next()) {
199           nSpx=aItLB.Value();
200           const TopoDS_Shape& aSpx=aDS.Shape(nSpx);
201           mySameDomainShapes.Add(aSpx ,aSpR);
202         }
203         //
204       }
205     }
206     //
207     myImages.Bind(aE, aLSp);
208     
209   }//for (i=1; i<=aNb; ++i)
210 }
211 //=======================================================================
212 // function: FillImagesContainers
213 // purpose: 
214 //=======================================================================
215   void GEOMAlgo_Builder::FillImagesContainers(const TopAbs_ShapeEnum theType)
216 {
217   myErrorStatus=0;
218   //
219   Standard_Boolean bInterferred, bToReverse;
220   Standard_Integer i, aNbS;
221   TopAbs_ShapeEnum aType;
222   BRep_Builder aBB;
223   TopoDS_Iterator aIt;
224   TopTools_ListIteratorOfListOfShape aItIm; 
225   TopTools_MapOfShape aMS;
226   TopTools_MapIteratorOfMapOfShape aItS;
227   //
228   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
229   NMTTools_PaveFiller* pPF=myPaveFiller;
230   IntTools_Context& aCtx= pPF->ChangeContext();
231   //
232   aNbS=aDS.NumberOfShapesOfTheObject();
233   for (i=1; i<=aNbS; ++i) {
234     const TopoDS_Shape& aC=aDS.Shape(i);
235     aType=aC.ShapeType();
236     if (aType==theType) {
237       aMS.Add(aC);
238     }
239   }
240   //
241   if (theType==TopAbs_COMPOUND) {
242     FillImagesCompounds(aMS, myImages);
243     return;
244   }
245   //
246   aItS.Initialize(aMS);
247   for (; aItS.More(); aItS.Next()) {
248     const TopoDS_Shape& aC=aItS.Key();
249     // whether the shape has image
250     bInterferred=Standard_False;
251     aIt.Initialize(aC);
252     for (; aIt.More(); aIt.Next()) {
253       const TopoDS_Shape& aF=aIt.Value();
254       if (myImages.HasImage(aF)) {
255         bInterferred=!bInterferred;
256         break;
257       }
258     }
259     if (!bInterferred){
260       continue;
261     }
262     //
263     TopoDS_Shape aCIm;
264     GEOMAlgo_Tools3D::MakeContainer(theType, aCIm);
265     //
266     aIt.Initialize(aC);
267     for (; aIt.More(); aIt.Next()) {
268       const TopoDS_Shape& aF=aIt.Value();
269       if (myImages.HasImage(aF)) {
270         const TopTools_ListOfShape& aLFIm=myImages.Image(aF);
271         aItIm.Initialize(aLFIm);
272         for (; aItIm.More(); aItIm.Next()) {
273           TopoDS_Shape aFIm=aItIm.Value();
274           //
275           bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aFIm, aF, aCtx);
276           if (bToReverse) {
277             aFIm.Reverse();
278           }
279           aBB.Add(aCIm, aFIm);
280         }
281       }
282       else {
283         aBB.Add(aCIm, aF);
284       }
285     }
286     myImages.Bind(aC, aCIm); 
287   }// for (; aItS.More(); aItS.Next()) {
288 }
289 //=======================================================================
290 // function: FillImagesCompounds
291 // purpose: 
292 //=======================================================================
293 void FillImagesCompounds(const TopTools_MapOfShape& theMS,
294                          BRepAlgo_Image& theImages)
295 {
296   TopTools_MapOfShape aMFP;
297   TopTools_MapIteratorOfMapOfShape aItS;
298   //
299   aItS.Initialize(theMS);
300   for (; aItS.More(); aItS.Next()) {
301     const TopoDS_Shape& aC=aItS.Key();
302     FillImagesCompound(aC, theImages, aMFP);
303   }
304 }
305 //=======================================================================
306 //function : FillImagesCompound
307 //purpose  : 
308 //=======================================================================
309 void FillImagesCompound(const TopoDS_Shape& theS,
310                         BRepAlgo_Image& theImages,
311                         TopTools_MapOfShape& theMFP)
312
313   Standard_Boolean bInterferred;
314   TopAbs_ShapeEnum aTypeX;
315   TopAbs_Orientation aOrX;
316   TopoDS_Iterator aIt;
317   BRep_Builder aBB;
318   TopTools_ListIteratorOfListOfShape aItIm; 
319   //
320   if (!theMFP.Add(theS)) {
321     return;
322   }
323   //
324   bInterferred=Standard_False;
325   aIt.Initialize(theS);
326   for (; aIt.More(); aIt.Next()) {
327     const TopoDS_Shape& aSX=aIt.Value();
328     aTypeX=aSX.ShapeType();
329     if (aTypeX==TopAbs_COMPOUND) {
330       FillImagesCompound(aSX, theImages, theMFP);
331     }
332     if (theImages.HasImage(aSX)) {
333       bInterferred=Standard_True;
334     }
335   }
336   if (!bInterferred){
337     return;
338   }
339   //
340   TopoDS_Shape aCIm;
341   GEOMAlgo_Tools3D::MakeContainer(TopAbs_COMPOUND, aCIm);
342   //
343   aIt.Initialize(theS);
344   for (; aIt.More(); aIt.Next()) {
345     const TopoDS_Shape& aSX=aIt.Value();
346     aOrX=aSX.Orientation();
347     if (theImages.HasImage(aSX)) {
348       const TopTools_ListOfShape& aLFIm=theImages.Image(aSX);
349       aItIm.Initialize(aLFIm);
350       for (; aItIm.More(); aItIm.Next()) {
351         TopoDS_Shape aSXIm=aItIm.Value();
352         aSXIm.Orientation(aOrX);
353         aBB.Add(aCIm, aSXIm);
354       }
355     }
356     else {
357       aBB.Add(aCIm, aSX);
358     }
359   }
360   theImages.Bind(theS, aCIm); 
361 }