Salome HOME
0023505: Sigsegv with fuse on cylinder and cone
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_ShellSolid.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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_ShellSolid.cxx
24 // Created:     Wed Jan 12 12:49:45 2005
25 // Author:      Peter KURNEV
26 //              <pkv@irinox>
27 //
28 #include <GEOMAlgo_ShellSolid.hxx>
29
30 #include <Basics_OCCTVersion.hxx>
31
32 #include <Standard_Failure.hxx>
33
34 #include <gp_Pnt2d.hxx>
35 #include <gp_Pnt.hxx>
36 #include <gp_Dir.hxx>
37
38 #include <TopoDS.hxx>
39 #include <TopoDS_Face.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Solid.hxx>
43
44 #include <BRep_Tool.hxx>
45 #include <BRepTools.hxx>
46
47 #include <TopTools_ListOfShape.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
49 #include <TopExp_Explorer.hxx>
50
51 #include <BOPTools_AlgoTools.hxx>
52
53 #include <BOPCol_DataMapOfShapeListOfShape.hxx>
54 #include <BOPCol_ListOfShape.hxx>
55 #if OCC_VERSION_LARGE > 0x06070100
56 #include <IntTools_Context.hxx>
57 #else
58 #include <BOPInt_Context.hxx>
59 #endif
60 #include <BOPDS_DS.hxx>
61 #include <BOPAlgo_Builder.hxx>
62
63 #include <GEOMAlgo_AlgoTools.hxx>
64 /////////////////////////////////////////////////////////////////////////
65 //=======================================================================
66 //class : GEOMAlgo_ShellSolidBuilder
67 //purpose  : 
68 //=======================================================================
69 class GEOMAlgo_ShellSolidBuilder : public BOPAlgo_Builder {
70  public:
71   Standard_EXPORT
72     GEOMAlgo_ShellSolidBuilder();
73
74   Standard_EXPORT
75     virtual ~GEOMAlgo_ShellSolidBuilder();
76
77  protected:
78   Standard_EXPORT
79     virtual void PerformInternal(const BOPAlgo_PaveFiller& theFiller);
80 };
81
82 //=======================================================================
83 //function : GEOMAlgo_ShellSolidBuilder
84 //purpose  : 
85 //=======================================================================
86 GEOMAlgo_ShellSolidBuilder::GEOMAlgo_ShellSolidBuilder()
87 :
88   BOPAlgo_Builder()
89 {
90 }
91 //=======================================================================
92 //function : ~GEOMAlgo_ShellSolidBuilder
93 //purpose  : 
94 //=======================================================================
95 GEOMAlgo_ShellSolidBuilder::~GEOMAlgo_ShellSolidBuilder()
96 {
97 }
98 //=======================================================================
99 //function : PerformInternal
100 //purpose  : 
101 //=======================================================================
102 void GEOMAlgo_ShellSolidBuilder::PerformInternal(const BOPAlgo_PaveFiller& theFiller)
103 {
104 #if OCC_VERSION_LARGE <= 0x07010000
105   myErrorStatus=0;
106 #endif 
107    //
108   myPaveFiller=(BOPAlgo_PaveFiller*)&theFiller;
109   myDS=myPaveFiller->PDS();
110   myContext=myPaveFiller->Context();
111   //
112   // 1. CheckData
113   CheckData();
114 #if OCC_VERSION_LARGE > 0x07010001
115   if (HasErrors()) {
116 #else
117   if (myErrorStatus) {
118 #endif
119     return;
120   }
121   //
122   // 2. Prepare
123   Prepare();
124 #if OCC_VERSION_LARGE > 0x07010001
125   if (HasErrors()) {
126 #else
127   if (myErrorStatus) {
128 #endif
129     return;
130   }
131   //
132   // 3. Fill Images
133   // 3.1 Vertice
134   FillImagesVertices();
135 #if OCC_VERSION_LARGE > 0x07010001
136   if (HasErrors()) {
137 #else
138   if (myErrorStatus) {
139 #endif
140     return;
141   }
142   //
143   BuildResult(TopAbs_VERTEX);
144 #if OCC_VERSION_LARGE > 0x07010001
145   if (HasErrors()) {
146 #else
147   if (myErrorStatus) {
148 #endif
149     return;
150   }
151   // 3.2 Edges
152   FillImagesEdges();
153 #if OCC_VERSION_LARGE > 0x07010001
154   if (HasErrors()) {
155 #else
156   if (myErrorStatus) {
157 #endif
158     return;
159   }
160   //
161   BuildResult(TopAbs_EDGE);
162 #if OCC_VERSION_LARGE > 0x07010001
163   if (HasErrors()) {
164 #else
165   if (myErrorStatus) {
166 #endif
167     return;
168   } 
169   //
170   // 3.3 Wires
171   FillImagesContainers(TopAbs_WIRE);
172 #if OCC_VERSION_LARGE > 0x07010001
173   if (HasErrors()) {
174 #else
175   if (myErrorStatus) {
176 #endif
177     return;
178   }
179   //
180   BuildResult(TopAbs_WIRE);
181 #if OCC_VERSION_LARGE > 0x07010001
182   if (HasErrors()) {
183 #else
184   if (myErrorStatus) {
185 #endif
186     return;
187   }
188   
189   // 3.4 Faces
190   FillImagesFaces();
191 #if OCC_VERSION_LARGE > 0x07010001
192   if (HasErrors()) {
193 #else
194   if (myErrorStatus) {
195 #endif
196     return;
197   }
198   //
199   BuildResult(TopAbs_FACE);
200 #if OCC_VERSION_LARGE > 0x07010001
201   if (HasErrors()) {
202 #else
203   if (myErrorStatus) {
204 #endif
205     return;
206   }
207 }
208 /////////////////////////////////////////////////////////////////////////
209 //=======================================================================
210 //function : GEOMAlgo_ShellSolid
211 //purpose  :
212 //=======================================================================
213 GEOMAlgo_ShellSolid::GEOMAlgo_ShellSolid()
214 :
215   GEOMAlgo_ShapeSolid()
216 {
217 }
218 //=======================================================================
219 //function : ~
220 //purpose  :
221 //=======================================================================
222 GEOMAlgo_ShellSolid::~GEOMAlgo_ShellSolid()
223 {
224 }
225 //=======================================================================
226 // function:
227 // purpose:
228 //=======================================================================
229 void GEOMAlgo_ShellSolid::Perform()
230 {
231 #if OCC_VERSION_LARGE <= 0x07010000
232   myErrorStatus=0;
233 #endif
234   //
235   try {
236     Standard_Integer aNbArgs, iRank, iErr, iBeg, iEnd, i, aNbSp;
237     Standard_Real aTol;
238     TopAbs_ShapeEnum aType;
239     TopAbs_State aState;
240     gp_Pnt aP;
241     gp_Pnt2d aP2D;
242     TopoDS_Face aF;
243     //
244     myLSIN.Clear();
245     myLSOUT.Clear();
246     myLSON.Clear();
247     //
248     aTol=1.e-7;
249     //
250     if (myDSFiller==NULL) {
251       myErrorStatus=10;
252       return;
253     }
254 #if OCC_VERSION_LARGE > 0x07010001
255     if(myDSFiller->HasErrors()) {
256 #else
257     if(myDSFiller->ErrorStatus()) {
258 #endif
259       myErrorStatus=11;
260       return;
261     }
262     //
263     const BOPDS_DS& aDS=myDSFiller->DS();
264     BOPDS_DS* pDS=(BOPDS_DS*)&aDS;
265     const BOPCol_ListOfShape& aLS=pDS->Arguments();
266     //
267     aNbArgs=aLS.Extent();
268     if (aNbArgs!=2) {
269       myErrorStatus=13;
270       return;
271     }
272     //
273     iRank=-1;
274     const TopoDS_Shape& aObj=aLS.First();
275     if (aObj.ShapeType()==TopAbs_SHELL) {
276       iRank=0;
277     }
278     const TopoDS_Shape& aTool=aLS.Last();
279     if (aTool.ShapeType()==TopAbs_SHELL) {
280       iRank=1;
281     }
282     //
283     if (iRank==-1) {
284       myErrorStatus=14;
285       return;
286     }
287     //
288 #if OCC_VERSION_LARGE > 0x06070100
289     Handle(IntTools_Context) aCtx=myDSFiller->Context();
290 #else
291     Handle(BOPInt_Context) aCtx=myDSFiller->Context();
292 #endif
293     const BOPDS_IndexRange& aRange=pDS->Range(iRank);
294     aRange.Indices(iBeg, iEnd);
295     const TopoDS_Solid& aSolid=(!iRank) ? *((TopoDS_Solid*)&aTool) : *((TopoDS_Solid*)&aObj);
296     //BRepClass3d_SolidClassifier& aSC=aCtx->SolidClassifier(aSolid);
297     //
298     //------------------------------ShellSolidBuilder
299     GEOMAlgo_ShellSolidBuilder aSSB;
300     //
301     aSSB.PerformWithFiller(*myDSFiller);
302 #if OCC_VERSION_LARGE > 0x07010001
303     iErr=aSSB.HasErrors();
304 #else
305     iErr=aSSB.ErrorStatus();
306 #endif
307     if (iErr) {
308       myErrorStatus=15;
309       return;
310     }
311     //
312     const BOPCol_DataMapOfShapeListOfShape& aImages=aSSB.Images();
313     //
314     //-------------------------------
315     for (i=iBeg; i<=iEnd; ++i) {
316       const TopoDS_Shape& aS=pDS->Shape(i);
317       aType=aS.ShapeType();
318       if (aType!=TopAbs_FACE) {
319         continue;
320       }
321       //
322       aState=TopAbs_UNKNOWN;
323       aF=*((TopoDS_Face*)&aS);
324       //
325       if (!aImages.IsBound(aS)) {
326         iErr=GEOMAlgo_AlgoTools::PntInFace(aF, aP, aP2D);
327         if (iErr) {
328           myErrorStatus=16;
329           return;
330         }
331         //
332         aState=BOPTools_AlgoTools::ComputeState(aP, aSolid, aTol, aCtx);
333       }
334       else {
335         const BOPCol_ListOfShape& aLSp=aImages.Find(aS);
336         aNbSp=aLSp.Extent();
337         if (aNbSp>0) {
338           continue;
339         }
340         //
341         if (aNbSp==1) {
342           aF=*((TopoDS_Face*)&aLSp.First());
343         }
344         //
345         iErr=GEOMAlgo_AlgoTools::PntInFace(aF, aP, aP2D);
346         if (iErr) {
347           myErrorStatus=16;
348           return;
349         }
350         //
351         aState=BOPTools_AlgoTools::ComputeState(aP, aSolid, aTol, aCtx);
352       }
353       //----------
354       if (aState==TopAbs_ON) {
355         myLSON.Append(aF);
356       }
357       else if (aState==TopAbs_OUT) {
358         myLSOUT.Append(aF);
359       }
360       else if (aState==TopAbs_IN) {
361         myLSIN.Append(aF);
362       } 
363       //----------
364     }//for (i=iBeg; i<=iEnd; ++i) {
365     
366   }// try
367   catch (Standard_Failure) {
368     myErrorStatus=12;
369   }
370 }