Salome HOME
86f26a96ef24ff7adf98386cb5fefd7f3c966661
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_ShellSolid.cxx
1 // Copyright (C) 2007-2023  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 #include <Basics_OCCTVersion.hxx>
60
61 /////////////////////////////////////////////////////////////////////////
62 //=======================================================================
63 //class : GEOMAlgo_ShellSolidBuilder
64 //purpose  : 
65 //=======================================================================
66 class GEOMAlgo_ShellSolidBuilder : public BOPAlgo_Builder {
67  public:
68   Standard_EXPORT
69     GEOMAlgo_ShellSolidBuilder();
70
71   Standard_EXPORT
72     virtual ~GEOMAlgo_ShellSolidBuilder();
73
74  protected:
75   Standard_EXPORT
76 #if OCC_VERSION_LARGE < 0x07070000
77     virtual void PerformInternal(const BOPAlgo_PaveFiller& theFiller);
78 #else
79     virtual void PerformInternal(const BOPAlgo_PaveFiller& theFiller,
80                                  const Message_ProgressRange& theRange);
81 #endif
82 };
83
84 //=======================================================================
85 //function : GEOMAlgo_ShellSolidBuilder
86 //purpose  : 
87 //=======================================================================
88 GEOMAlgo_ShellSolidBuilder::GEOMAlgo_ShellSolidBuilder()
89 :
90   BOPAlgo_Builder()
91 {
92 }
93 //=======================================================================
94 //function : ~GEOMAlgo_ShellSolidBuilder
95 //purpose  : 
96 //=======================================================================
97 GEOMAlgo_ShellSolidBuilder::~GEOMAlgo_ShellSolidBuilder()
98 {
99 }
100 //=======================================================================
101 //function : PerformInternal
102 //purpose  : 
103 //=======================================================================
104 #if OCC_VERSION_LARGE < 0x07070000
105 void GEOMAlgo_ShellSolidBuilder::PerformInternal(const BOPAlgo_PaveFiller& theFiller)
106 #else
107 void GEOMAlgo_ShellSolidBuilder::PerformInternal(const BOPAlgo_PaveFiller& theFiller,
108                                                  const Message_ProgressRange& theRange)
109 #endif
110 {
111   myPaveFiller=(BOPAlgo_PaveFiller*)&theFiller;
112   myDS=myPaveFiller->PDS();
113   myContext=myPaveFiller->Context();
114   //
115   // 1. CheckData
116   CheckData();
117   if (HasErrors()) {
118     return;
119   }
120   //
121   // 2. Prepare
122   Prepare();
123   if (HasErrors()) {
124     return;
125   }
126
127 #if OCC_VERSION_LARGE >= 0x07070000
128   Message_ProgressScope aPS(theRange, "Building the result of Boolean operation", 100);
129   //
130   BOPAlgo_PISteps aSteps (PIOperation_Last);
131   analyzeProgress (100, aSteps);
132 #endif
133
134   // 3. Fill Images
135   // 3.1 Vertice
136 #if OCC_VERSION_LARGE < 0x07070000
137   FillImagesVertices();
138 #else
139   FillImagesVertices(aPS.Next(aSteps.GetStep(PIOperation_TreatVertices)));
140 #endif
141   if (HasErrors()) {
142     return;
143   }
144   //
145   BuildResult(TopAbs_VERTEX);
146   if (HasErrors()) {
147     return;
148   }
149   // 3.2 Edges
150 #if OCC_VERSION_LARGE < 0x07070000
151   FillImagesEdges();
152 #else
153   FillImagesEdges(aPS.Next(aSteps.GetStep(PIOperation_TreatEdges)));
154 #endif
155   if (HasErrors()) {
156     return;
157   }
158   //
159   BuildResult(TopAbs_EDGE);
160   if (HasErrors()) {
161     return;
162   } 
163   //
164   // 3.3 Wires
165 #if OCC_VERSION_LARGE < 0x07070000
166   FillImagesContainers(TopAbs_WIRE);
167 #else
168   FillImagesContainers(TopAbs_WIRE, aPS.Next(aSteps.GetStep(PIOperation_TreatWires)));
169 #endif
170   if (HasErrors()) {
171     return;
172   }
173   //
174   BuildResult(TopAbs_WIRE);
175   if (HasErrors()) {
176     return;
177   }
178   
179   // 3.4 Faces
180 #if OCC_VERSION_LARGE < 0x07070000
181   FillImagesFaces();
182 #else
183   FillImagesFaces(aPS.Next(aSteps.GetStep(PIOperation_TreatFaces)));
184 #endif
185   if (HasErrors()) {
186     return;
187   }
188   //
189   BuildResult(TopAbs_FACE);
190   if (HasErrors()) {
191     return;
192   }
193 }
194 /////////////////////////////////////////////////////////////////////////
195 //=======================================================================
196 //function : GEOMAlgo_ShellSolid
197 //purpose  :
198 //=======================================================================
199 GEOMAlgo_ShellSolid::GEOMAlgo_ShellSolid()
200 :
201   GEOMAlgo_ShapeSolid()
202 {
203 }
204 //=======================================================================
205 //function : ~
206 //purpose  :
207 //=======================================================================
208 GEOMAlgo_ShellSolid::~GEOMAlgo_ShellSolid()
209 {
210 }
211 //=======================================================================
212 // function:
213 // purpose:
214 //=======================================================================
215 void GEOMAlgo_ShellSolid::Perform()
216 {
217   //
218   try {
219     Standard_Integer aNbArgs, iRank, iErr, iBeg, iEnd, i, aNbSp;
220     Standard_Real aTol;
221     TopAbs_ShapeEnum aType;
222     TopAbs_State aState;
223     gp_Pnt aP;
224     gp_Pnt2d aP2D;
225     TopoDS_Face aF;
226     //
227     myLSIN.Clear();
228     myLSOUT.Clear();
229     myLSON.Clear();
230     //
231     aTol=1.e-7;
232     //
233     if (myDSFiller==NULL) {
234       myErrorStatus=10;
235       return;
236     }
237     if(myDSFiller->HasErrors()) {
238       myErrorStatus=11;
239       return;
240     }
241     //
242     const BOPDS_DS& aDS=myDSFiller->DS();
243     BOPDS_DS* pDS=(BOPDS_DS*)&aDS;
244     const TopTools_ListOfShape& aLS=pDS->Arguments();
245     //
246     aNbArgs=aLS.Extent();
247     if (aNbArgs!=2) {
248       myErrorStatus=13;
249       return;
250     }
251     //
252     iRank=-1;
253     const TopoDS_Shape& aObj=aLS.First();
254     if (aObj.ShapeType()==TopAbs_SHELL) {
255       iRank=0;
256     }
257     const TopoDS_Shape& aTool=aLS.Last();
258     if (aTool.ShapeType()==TopAbs_SHELL) {
259       iRank=1;
260     }
261     //
262     if (iRank==-1) {
263       myErrorStatus=14;
264       return;
265     }
266     //
267     Handle(IntTools_Context) aCtx=myDSFiller->Context();
268     const BOPDS_IndexRange& aRange=pDS->Range(iRank);
269     aRange.Indices(iBeg, iEnd);
270     const TopoDS_Solid& aSolid=(!iRank) ? *((TopoDS_Solid*)&aTool) : *((TopoDS_Solid*)&aObj);
271     //BRepClass3d_SolidClassifier& aSC=aCtx->SolidClassifier(aSolid);
272     //
273     //------------------------------ShellSolidBuilder
274     GEOMAlgo_ShellSolidBuilder aSSB;
275     //
276     aSSB.PerformWithFiller(*myDSFiller);
277     iErr=aSSB.HasErrors();
278     if (iErr) {
279       myErrorStatus=15;
280       return;
281     }
282     //
283     const TopTools_DataMapOfShapeListOfShape& aImages=aSSB.Images();
284     //
285     //-------------------------------
286     for (i=iBeg; i<=iEnd; ++i) {
287       const TopoDS_Shape& aS=pDS->Shape(i);
288       aType=aS.ShapeType();
289       if (aType!=TopAbs_FACE) {
290         continue;
291       }
292       //
293       aState=TopAbs_UNKNOWN;
294       aF=*((TopoDS_Face*)&aS);
295       //
296       if (!aImages.IsBound(aS)) {
297         iErr=GEOMAlgo_AlgoTools::PntInFace(aF, aP, aP2D);
298         if (iErr) {
299           myErrorStatus=16;
300           return;
301         }
302         //
303         aState=BOPTools_AlgoTools::ComputeState(aP, aSolid, aTol, aCtx);
304       }
305       else {
306         const TopTools_ListOfShape& aLSp=aImages.Find(aS);
307         aNbSp=aLSp.Extent();
308         if (aNbSp>0) {
309           continue;
310         }
311         //
312         if (aNbSp==1) {
313           aF=*((TopoDS_Face*)&aLSp.First());
314         }
315         //
316         iErr=GEOMAlgo_AlgoTools::PntInFace(aF, aP, aP2D);
317         if (iErr) {
318           myErrorStatus=16;
319           return;
320         }
321         //
322         aState=BOPTools_AlgoTools::ComputeState(aP, aSolid, aTol, aCtx);
323       }
324       //----------
325       if (aState==TopAbs_ON) {
326         myLSON.Append(aF);
327       }
328       else if (aState==TopAbs_OUT) {
329         myLSOUT.Append(aF);
330       }
331       else if (aState==TopAbs_IN) {
332         myLSIN.Append(aF);
333       } 
334       //----------
335     }//for (i=iBeg; i<=iEnd; ++i) {
336     
337   }// try
338   catch (Standard_Failure&) {
339     myErrorStatus=12;
340   }
341 }