ac992b3aa8c273ba3cafd61eae6be95f476f3acf
[occt.git] / src / BRepAlgoAPI / BRepAlgoAPI_Section.cxx
1 // Created on: 1994-02-18
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 // modified by Michael KLOKOV  Wed Mar  6 15:01:25 2002
18 // modified by  Eugeny MALTCHIKOV Wed Jul 04 11:13:01 2012 
19
20 #include <BOPAlgo_BOP.hxx>
21 #include <BOPAlgo_PaveFiller.hxx>
22 #include <BOPAlgo_Section.hxx>
23 #include <BOPDS_DS.hxx>
24 #include <BRep_Tool.hxx>
25 #include <BRepAlgoAPI_Section.hxx>
26 #include <BRepBuilderAPI_MakeFace.hxx>
27 #include <BRepBuilderAPI_MakeShell.hxx>
28 #include <Geom2d_TrimmedCurve.hxx>
29 #include <Geom_Plane.hxx>
30 #include <Geom_Surface.hxx>
31 #include <gp_Pln.hxx>
32 #include <TopExp.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopTools_IndexedMapOfShape.hxx>
37 #include <TopTools_MapOfShape.hxx>
38
39 //
40 static 
41   TopoDS_Shape MakeShape(const Handle(Geom_Surface)& );
42
43 static 
44   Standard_Boolean HasAncestorFaces(const BOPAlgo_PPaveFiller&, 
45                                     const TopoDS_Shape&,
46                                     TopoDS_Shape&,
47                                     TopoDS_Shape&);
48 static
49   Standard_Boolean HasAncestorFace (const BOPAlgo_PPaveFiller& ,
50                                     Standard_Integer ,
51                                     const TopoDS_Shape& ,
52                                     TopoDS_Shape& ); 
53 //
54 //=======================================================================
55 //function : BRepAlgoAPI_Section
56 //purpose  : 
57 //=======================================================================
58 BRepAlgoAPI_Section::BRepAlgoAPI_Section()
59 :
60   BRepAlgoAPI_BooleanOperation()
61 {
62   Init(Standard_False);
63 }
64 //=======================================================================
65 //function : BRepAlgoAPI_Section
66 //purpose  : 
67 //=======================================================================
68 BRepAlgoAPI_Section::BRepAlgoAPI_Section(const BOPAlgo_PaveFiller& aPF)
69 :
70   BRepAlgoAPI_BooleanOperation(aPF)
71 {
72   Init(Standard_False);
73 }
74 //=======================================================================
75 //function : Constructor
76 //purpose  : 
77 //=======================================================================
78 BRepAlgoAPI_Section::BRepAlgoAPI_Section(const TopoDS_Shape& Sh1,
79                                          const TopoDS_Shape& Sh2,
80                                          const Standard_Boolean PerformNow)
81 :
82   BRepAlgoAPI_BooleanOperation(Sh1, 
83                                Sh2, 
84                                BOPAlgo_SECTION)
85 {
86   Init(PerformNow); 
87 }
88 //=======================================================================
89 //function : BRepAlgoAPI_Section
90 //purpose  : 
91 //=======================================================================
92 BRepAlgoAPI_Section::BRepAlgoAPI_Section
93   (const TopoDS_Shape&      aS1, 
94    const TopoDS_Shape&      aS2,
95    const BOPAlgo_PaveFiller& aDSF,
96    const Standard_Boolean   PerformNow)
97
98   BRepAlgoAPI_BooleanOperation(aS1, 
99                                aS2, 
100                                aDSF,
101                                BOPAlgo_SECTION)
102 {
103   Init(PerformNow); 
104 }
105 //=======================================================================
106 //function : Constructor
107 //purpose  : 
108 //=======================================================================
109 BRepAlgoAPI_Section::BRepAlgoAPI_Section
110   (const TopoDS_Shape&    Sh,
111    const gp_Pln&          Pl,
112    const Standard_Boolean PerformNow)
113
114   BRepAlgoAPI_BooleanOperation(Sh, 
115                                MakeShape(new Geom_Plane(Pl)), 
116                                BOPAlgo_SECTION)
117 {
118   Init(PerformNow); 
119 }
120 //=======================================================================
121 //function : Constructor
122 //purpose  : 
123 //=======================================================================
124 BRepAlgoAPI_Section::BRepAlgoAPI_Section
125   (const TopoDS_Shape&         Sh,
126    const Handle(Geom_Surface)& Sf,
127    const Standard_Boolean      PerformNow)
128
129   BRepAlgoAPI_BooleanOperation(Sh, 
130                                MakeShape(Sf), 
131                                BOPAlgo_SECTION)
132 {
133   Init(PerformNow); 
134 }
135 //=======================================================================
136 //function : Constructor
137 //purpose  : 
138 //=======================================================================
139 BRepAlgoAPI_Section::BRepAlgoAPI_Section
140   (const Handle(Geom_Surface)& Sf,
141    const TopoDS_Shape&         Sh,
142    const Standard_Boolean      PerformNow)
143
144   BRepAlgoAPI_BooleanOperation(MakeShape(Sf), 
145                                Sh, 
146                                BOPAlgo_SECTION)
147 {
148   Init(PerformNow); 
149 }
150 //=======================================================================
151 //function : Constructor
152 //purpose  : 
153 //=======================================================================
154 BRepAlgoAPI_Section::BRepAlgoAPI_Section
155   (const Handle(Geom_Surface)& Sf1,
156    const Handle(Geom_Surface)& Sf2,
157    const Standard_Boolean PerformNow)
158
159   BRepAlgoAPI_BooleanOperation(MakeShape(Sf1), 
160                                MakeShape(Sf2), 
161                                BOPAlgo_SECTION)
162 {
163   Init(PerformNow); 
164 }
165 //=======================================================================
166 //function : ~BRepAlgoAPI_Section
167 //purpose  : 
168 //=======================================================================
169 BRepAlgoAPI_Section::~BRepAlgoAPI_Section()
170 {
171 }
172 //=======================================================================
173 //function : Init
174 //purpose  : 
175 //=======================================================================
176 void BRepAlgoAPI_Section::Init(const Standard_Boolean bFlag) 
177 {
178   myOperation=BOPAlgo_SECTION;
179   myParametersChanged = Standard_False;
180   myApprox = Standard_False;
181   myComputePCurve1 = Standard_False;
182   myComputePCurve2 = Standard_False;
183   myParametersChanged = Standard_True;
184   // 
185   if (bFlag) {
186     Build();
187   }
188 }
189 //=======================================================================
190 //function : Init1
191 //purpose  : 
192 //=======================================================================
193 void BRepAlgoAPI_Section::Init1(const TopoDS_Shape& S1) 
194 {
195   myArguments.Clear();
196   myArguments.Append(S1);
197   myParametersChanged = Standard_True;
198 }
199 //=======================================================================
200 //function : Init1
201 //purpose  : 
202 //=======================================================================
203 void BRepAlgoAPI_Section::Init1(const gp_Pln& Pl) 
204 {
205   Init1(MakeShape(new Geom_Plane(Pl)));
206 }
207 //=======================================================================
208 //function : Init1
209 //purpose  : 
210 //=======================================================================
211 void BRepAlgoAPI_Section::Init1(const Handle(Geom_Surface)& Sf) 
212 {
213   Init1(MakeShape(Sf));
214 }
215 //=======================================================================
216 //function : Init2
217 //purpose  : 
218 //=======================================================================
219 void BRepAlgoAPI_Section::Init2(const TopoDS_Shape& S2) 
220 {
221   myTools.Clear();
222   myTools.Append(S2);
223   myParametersChanged = Standard_True;
224 }
225 //=======================================================================
226 //function : Init2
227 //purpose  : 
228 //=======================================================================
229 void BRepAlgoAPI_Section::Init2(const gp_Pln& Pl) 
230 {
231   Init2(MakeShape(new Geom_Plane(Pl)));
232 }
233 //=======================================================================
234 //function : Init2
235 //purpose  : 
236 //=======================================================================
237 void BRepAlgoAPI_Section::Init2(const Handle(Geom_Surface)& Sf) 
238 {
239   Init2(MakeShape(Sf));
240 }
241 //=======================================================================
242 //function : Approximation
243 //purpose  : 
244 //=======================================================================
245 void BRepAlgoAPI_Section::Approximation(const Standard_Boolean B) 
246 {
247   if(myApprox != B) {
248     myApprox = B;
249     myParametersChanged = Standard_True;
250   }
251 }
252 //=======================================================================
253 //function : ComputePCurveOn1
254 //purpose  : 
255 //=======================================================================
256 void BRepAlgoAPI_Section::ComputePCurveOn1(const Standard_Boolean B) 
257 {
258   if(myComputePCurve1 != B) {
259     myComputePCurve1 = B;
260     myParametersChanged = Standard_True;
261   }
262 }
263 //=======================================================================
264 //function : ComputePCurveOn2
265 //purpose  : 
266 //=======================================================================
267 void BRepAlgoAPI_Section::ComputePCurveOn2(const Standard_Boolean B) 
268 {
269   if(myComputePCurve2 != B) {
270     myComputePCurve2 = B;
271     myParametersChanged = Standard_True;
272   }
273 }
274 //=======================================================================
275 //function : SetAttributes
276 //purpose  : 
277 //=======================================================================
278 void BRepAlgoAPI_Section::SetAttributes()
279 {
280   BOPAlgo_SectionAttribute theSecAttr(myApprox, 
281           myComputePCurve1, 
282           myComputePCurve2);
283   myDSFiller->SetSectionAttribute(theSecAttr);
284 }
285 //=======================================================================
286 //function : Build
287 //purpose  : 
288 //=======================================================================
289 void BRepAlgoAPI_Section::Build() 
290 {
291   BRepAlgoAPI_BooleanOperation::Build();
292 }
293 //=======================================================================
294 //function : HasAncestorFaceOn1
295 //purpose  : 
296 //=======================================================================
297 Standard_Boolean BRepAlgoAPI_Section::HasAncestorFaceOn1
298   (const TopoDS_Shape& aE, 
299    TopoDS_Shape& aF) const
300 {
301   Standard_Boolean bRes;
302   //
303   bRes = HasAncestorFace(myDSFiller,1 , aE, aF);
304   return bRes;
305 }
306 //=======================================================================
307 //function : HasAncestorFaceOn2
308 //purpose  : 
309 //=======================================================================
310 Standard_Boolean BRepAlgoAPI_Section::HasAncestorFaceOn2
311   (const TopoDS_Shape& aE,
312    TopoDS_Shape& aF) const
313 {
314   Standard_Boolean bRes;
315   //
316   bRes = HasAncestorFace(myDSFiller, 2, aE, aF);
317   return bRes;
318 }
319 //=======================================================================
320 //function : HasAncestorFace
321 //purpose  : 
322 //=======================================================================
323 Standard_Boolean HasAncestorFace (const BOPAlgo_PPaveFiller& pPF,
324                                   Standard_Integer aIndex,
325                                   const TopoDS_Shape& aE,
326                                   TopoDS_Shape& aF) 
327 {
328   Standard_Boolean bRes;
329   //
330   bRes = Standard_False;
331   if(aE.IsNull()) {
332     return bRes;
333   }
334   if(aE.ShapeType() != TopAbs_EDGE) {
335     return bRes;
336   }
337   //
338   TopoDS_Shape aF1, aF2;
339   //
340   bRes=HasAncestorFaces(pPF, aE, aF1, aF2);
341   if (!bRes) {
342     return bRes;
343   }
344   //
345   aF=(aIndex==1) ? aF1 : aF2;
346   return bRes;
347 }
348
349 //=======================================================================
350 //function : HasAncestorFaces
351 //purpose  : 
352 //=======================================================================
353 Standard_Boolean HasAncestorFaces (const BOPAlgo_PPaveFiller& pPF, 
354                                    const TopoDS_Shape& aEx,
355                                    TopoDS_Shape& aF1,
356                                    TopoDS_Shape& aF2) 
357 {
358   
359   Standard_Integer aNbFF, i, j, nE, nF1, nF2, aNbVC;
360   BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
361   //
362   const BOPDS_PDS& pDS = pPF->PDS();
363   BOPDS_VectorOfInterfFF& aFFs=pDS->InterfFF();
364   //
365   //section edges
366   aNbFF=aFFs.Length();
367   for (i = 0; i<aNbFF; ++i) {
368     BOPDS_InterfFF& aFFi=aFFs(i);
369     aFFi.Indices(nF1, nF2);
370     //
371     const BOPDS_VectorOfCurve& aVC=aFFi.Curves();
372     aNbVC=aVC.Length();
373     for (j=0; j<aNbVC; j++) {
374       const BOPDS_Curve& aBC=aVC(j);
375       //
376       const BOPDS_ListOfPaveBlock& aLPB = aBC.PaveBlocks();
377       //
378       aItLPB.Initialize(aLPB);
379       for(; aItLPB.More(); aItLPB.Next()) {
380         const Handle(BOPDS_PaveBlock)& aPB = aItLPB.Value();
381         nE = aPB->Edge();
382         if(nE < 0)  {
383           continue;
384         }
385         //
386         const TopoDS_Shape aE=pDS->Shape(nE);
387         if(aEx.IsSame(aE)) {
388           aF1 = pDS->Shape(nF1);
389           aF2 = pDS->Shape(nF2);
390           return Standard_True;
391         }
392       }
393     }
394   }
395   return Standard_False;
396 }
397 //=======================================================================
398 //function : MakeShape
399 //purpose  : 
400 //=======================================================================
401 TopoDS_Shape MakeShape(const Handle(Geom_Surface)& S)
402 {
403   GeomAbs_Shape c = S->Continuity();
404   if (c >= GeomAbs_C2) {
405     return BRepBuilderAPI_MakeFace(S, Precision::Confusion());
406   }
407   return BRepBuilderAPI_MakeShell(S);
408 }