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