Salome HOME
65f6929a0c0d1d8553b663ceb35b154a35e8e729
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Builder_1.cxx
1 // Copyright (C) 2007-2013  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   const Handle(IntTools_Context)& aCtx=pPF->Context();
116   //
117   Standard_Boolean bToReverse;
118   Standard_Integer i, aNb, aNbSp, nSp, nSpR, nSpx, aIsCB, aNbLB;
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       const BOPTools_PaveBlock& aPBR=pPF->RealPaveBlock(aPB, aLB, aIsCB);
153       //modified by NIZNHY-PKV Wed Oct 27 11:19:30 2010f
154       aNbLB=aLB.Extent();
155       if (aIsCB && aNbLB<2) {
156         aIsCB=0;
157       }
158       //modified by NIZNHY-PKV Wed Oct 27 11:19:34 2010t
159       //
160       nSpR=aPBR.Edge();
161       const TopoDS_Shape& aSpR=aDS.Shape(nSpR);
162       if (aSpR.IsSame(aSp) && aSpR.IsSame(aE) && !aIsCB) {
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         const BOPTools_PaveBlock& aPBR=pPF->RealPaveBlock(aPB, aLB, aIsCB);
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     myImages.Bind(aE, aLSp);
207
208   }//for (i=1; i<=aNb; ++i)
209 }
210 //=======================================================================
211 // function: FillImagesContainers
212 // purpose:
213 //=======================================================================
214   void GEOMAlgo_Builder::FillImagesContainers(const TopAbs_ShapeEnum theType)
215 {
216   myErrorStatus=0;
217   //
218   Standard_Boolean bInterferred, bToReverse;
219   Standard_Integer i, aNbS;
220   TopAbs_ShapeEnum aType;
221   BRep_Builder aBB;
222   TopoDS_Iterator aIt;
223   TopTools_ListIteratorOfListOfShape aItIm;
224   TopTools_MapOfShape aMS;
225   TopTools_MapIteratorOfMapOfShape aItS;
226   //
227   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
228   NMTTools_PaveFiller* pPF=myPaveFiller;
229   const Handle(IntTools_Context)& aCtx= pPF->Context();
230   //
231   aNbS=aDS.NumberOfShapesOfTheObject();
232   for (i=1; i<=aNbS; ++i) {
233     const TopoDS_Shape& aC=aDS.Shape(i);
234     aType=aC.ShapeType();
235     if (aType==theType) {
236       aMS.Add(aC);
237     }
238   }
239   //
240   if (theType==TopAbs_COMPOUND) {
241     FillImagesCompounds(aMS, myImages);
242     return;
243   }
244   //
245   aItS.Initialize(aMS);
246   for (; aItS.More(); aItS.Next()) {
247     const TopoDS_Shape& aC=aItS.Key();
248     // whether the shape has image
249     bInterferred=Standard_False;
250     aIt.Initialize(aC);
251     for (; aIt.More(); aIt.Next()) {
252       const TopoDS_Shape& aF=aIt.Value();
253       if (myImages.HasImage(aF)) {
254         bInterferred=!bInterferred;
255         break;
256       }
257     }
258     if (!bInterferred){
259       continue;
260     }
261     //
262     TopoDS_Shape aCIm;
263     GEOMAlgo_Tools3D::MakeContainer(theType, aCIm);
264     //
265     aIt.Initialize(aC);
266     for (; aIt.More(); aIt.Next()) {
267       const TopoDS_Shape& aF=aIt.Value();
268       if (myImages.HasImage(aF)) {
269         const TopTools_ListOfShape& aLFIm=myImages.Image(aF);
270         aItIm.Initialize(aLFIm);
271         for (; aItIm.More(); aItIm.Next()) {
272           TopoDS_Shape aFIm=aItIm.Value();
273           //
274           bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aFIm, aF, aCtx);
275           if (bToReverse) {
276             aFIm.Reverse();
277           }
278           aBB.Add(aCIm, aFIm);
279         }
280       }
281       else {
282         aBB.Add(aCIm, aF);
283       }
284     }
285     myImages.Bind(aC, aCIm);
286   }// for (; aItS.More(); aItS.Next()) {
287 }
288 //=======================================================================
289 // function: FillImagesCompounds
290 // purpose:
291 //=======================================================================
292 void FillImagesCompounds(const TopTools_MapOfShape& theMS,
293                          BRepAlgo_Image& theImages)
294 {
295   TopTools_MapOfShape aMFP;
296   TopTools_MapIteratorOfMapOfShape aItS;
297   //
298   aItS.Initialize(theMS);
299   for (; aItS.More(); aItS.Next()) {
300     const TopoDS_Shape& aC=aItS.Key();
301     FillImagesCompound(aC, theImages, aMFP);
302   }
303 }
304 //=======================================================================
305 //function : FillImagesCompound
306 //purpose  :
307 //=======================================================================
308 void FillImagesCompound(const TopoDS_Shape& theS,
309                         BRepAlgo_Image& theImages,
310                         TopTools_MapOfShape& theMFP)
311 {
312   Standard_Boolean bInterferred;
313   TopAbs_ShapeEnum aTypeX;
314   TopAbs_Orientation aOrX;
315   TopoDS_Iterator aIt;
316   BRep_Builder aBB;
317   TopTools_ListIteratorOfListOfShape aItIm;
318   //
319   if (!theMFP.Add(theS)) {
320     return;
321   }
322   //
323   bInterferred=Standard_False;
324   aIt.Initialize(theS);
325   for (; aIt.More(); aIt.Next()) {
326     const TopoDS_Shape& aSX=aIt.Value();
327     aTypeX=aSX.ShapeType();
328     if (aTypeX==TopAbs_COMPOUND) {
329       FillImagesCompound(aSX, theImages, theMFP);
330     }
331     if (theImages.HasImage(aSX)) {
332       bInterferred=Standard_True;
333     }
334   }
335   if (!bInterferred){
336     return;
337   }
338   //
339   TopoDS_Shape aCIm;
340   GEOMAlgo_Tools3D::MakeContainer(TopAbs_COMPOUND, aCIm);
341   //
342   aIt.Initialize(theS);
343   for (; aIt.More(); aIt.Next()) {
344     const TopoDS_Shape& aSX=aIt.Value();
345     aOrX=aSX.Orientation();
346     if (theImages.HasImage(aSX)) {
347       const TopTools_ListOfShape& aLFIm=theImages.Image(aSX);
348       aItIm.Initialize(aLFIm);
349       for (; aItIm.More(); aItIm.Next()) {
350         TopoDS_Shape aSXIm=aItIm.Value();
351         aSXIm.Orientation(aOrX);
352         aBB.Add(aCIm, aSXIm);
353       }
354     }
355     else {
356       aBB.Add(aCIm, aSX);
357     }
358   }
359   theImages.Bind(theS, aCIm);
360 }