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