Salome HOME
Update copyright: 2016
[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   myErrorStatus=0;
105    //
106   myPaveFiller=(BOPAlgo_PaveFiller*)&theFiller;
107   myDS=myPaveFiller->PDS();
108   myContext=myPaveFiller->Context();
109   //
110   // 1. CheckData
111   CheckData();
112   if (myErrorStatus) {
113     return;
114   }
115   //
116   // 2. Prepare
117   Prepare();
118   if (myErrorStatus) {
119     return;
120   }
121   //
122   // 3. Fill Images
123   // 3.1 Vertice
124   FillImagesVertices();
125   if (myErrorStatus) {
126     return;
127   }
128   //
129   BuildResult(TopAbs_VERTEX);
130   if (myErrorStatus) {
131     return;
132   }
133   // 3.2 Edges
134   FillImagesEdges();
135   if (myErrorStatus) {
136     return;
137   }
138   //
139   BuildResult(TopAbs_EDGE);
140   if (myErrorStatus) {
141     return;
142   } 
143   //
144   // 3.3 Wires
145   FillImagesContainers(TopAbs_WIRE);
146   if (myErrorStatus) {
147     return;
148   }
149   //
150   BuildResult(TopAbs_WIRE);
151   if (myErrorStatus) {
152     return;
153   }
154   
155   // 3.4 Faces
156   FillImagesFaces();
157   if (myErrorStatus) {
158     return;
159   }
160   //
161   BuildResult(TopAbs_FACE);
162   if (myErrorStatus) {
163     return;
164   }
165 }
166 /////////////////////////////////////////////////////////////////////////
167 //=======================================================================
168 //function : GEOMAlgo_ShellSolid
169 //purpose  :
170 //=======================================================================
171 GEOMAlgo_ShellSolid::GEOMAlgo_ShellSolid()
172 :
173   GEOMAlgo_ShapeSolid()
174 {
175 }
176 //=======================================================================
177 //function : ~
178 //purpose  :
179 //=======================================================================
180 GEOMAlgo_ShellSolid::~GEOMAlgo_ShellSolid()
181 {
182 }
183 //=======================================================================
184 // function:
185 // purpose:
186 //=======================================================================
187 void GEOMAlgo_ShellSolid::Perform()
188 {
189   myErrorStatus=0;
190   //
191   try {
192     Standard_Integer aNbArgs, iRank, iErr, iBeg, iEnd, i, aNbSp;
193     Standard_Real aTol;
194     TopAbs_ShapeEnum aType;
195     TopAbs_State aState;
196     gp_Pnt aP;
197     gp_Pnt2d aP2D;
198     TopoDS_Face aF;
199     //
200     myLSIN.Clear();
201     myLSOUT.Clear();
202     myLSON.Clear();
203     //
204     aTol=1.e-7;
205     //
206     if (myDSFiller==NULL) {
207       myErrorStatus=10;
208       return;
209     }
210     if(myDSFiller->ErrorStatus()) {
211       myErrorStatus=11;
212       return;
213     }
214     //
215     const BOPDS_DS& aDS=myDSFiller->DS();
216     BOPDS_DS* pDS=(BOPDS_DS*)&aDS;
217     const BOPCol_ListOfShape& aLS=pDS->Arguments();
218     //
219     aNbArgs=aLS.Extent();
220     if (aNbArgs!=2) {
221       myErrorStatus=13;
222       return;
223     }
224     //
225     iRank=-1;
226     const TopoDS_Shape& aObj=aLS.First();
227     if (aObj.ShapeType()==TopAbs_SHELL) {
228       iRank=0;
229     }
230     const TopoDS_Shape& aTool=aLS.Last();
231     if (aTool.ShapeType()==TopAbs_SHELL) {
232       iRank=1;
233     }
234     //
235     if (iRank==-1) {
236       myErrorStatus=14;
237       return;
238     }
239     //
240 #if OCC_VERSION_LARGE > 0x06070100
241     Handle(IntTools_Context) aCtx=myDSFiller->Context();
242 #else
243     Handle(BOPInt_Context) aCtx=myDSFiller->Context();
244 #endif
245     const BOPDS_IndexRange& aRange=pDS->Range(iRank);
246     aRange.Indices(iBeg, iEnd);
247     const TopoDS_Solid& aSolid=(!iRank) ? *((TopoDS_Solid*)&aTool) : *((TopoDS_Solid*)&aObj);
248     //BRepClass3d_SolidClassifier& aSC=aCtx->SolidClassifier(aSolid);
249     //
250     //------------------------------ShellSolidBuilder
251     GEOMAlgo_ShellSolidBuilder aSSB;
252     //
253     aSSB.PerformWithFiller(*myDSFiller);
254     iErr=aSSB.ErrorStatus();
255     if (iErr) {
256       myErrorStatus=15;
257       return;
258     }
259     //
260     const BOPCol_DataMapOfShapeListOfShape& aImages=aSSB.Images();
261     //
262     //-------------------------------
263     for (i=iBeg; i<=iEnd; ++i) {
264       const TopoDS_Shape& aS=pDS->Shape(i);
265       aType=aS.ShapeType();
266       if (aType!=TopAbs_FACE) {
267         continue;
268       }
269       //
270       aState=TopAbs_UNKNOWN;
271       aF=*((TopoDS_Face*)&aS);
272       //
273       if (!aImages.IsBound(aS)) {
274         iErr=GEOMAlgo_AlgoTools::PntInFace(aF, aP, aP2D);
275         if (iErr) {
276           myErrorStatus=16;
277           return;
278         }
279         //
280         aState=BOPTools_AlgoTools::ComputeState(aP, aSolid, aTol, aCtx);
281       }
282       else {
283         const BOPCol_ListOfShape& aLSp=aImages.Find(aS);
284         aNbSp=aLSp.Extent();
285         if (aNbSp>0) {
286           continue;
287         }
288         //
289         if (aNbSp==1) {
290           aF=*((TopoDS_Face*)&aLSp.First());
291         }
292         //
293         iErr=GEOMAlgo_AlgoTools::PntInFace(aF, aP, aP2D);
294         if (iErr) {
295           myErrorStatus=16;
296           return;
297         }
298         //
299         aState=BOPTools_AlgoTools::ComputeState(aP, aSolid, aTol, aCtx);
300       }
301       //----------
302       if (aState==TopAbs_ON) {
303         myLSON.Append(aF);
304       }
305       else if (aState==TopAbs_OUT) {
306         myLSOUT.Append(aF);
307       }
308       else if (aState==TopAbs_IN) {
309         myLSIN.Append(aF);
310       } 
311       //----------
312     }//for (i=iBeg; i<=iEnd; ++i) {
313     
314   }// try
315   catch (Standard_Failure) {
316     myErrorStatus=12;
317   }
318 }