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