a088e394d4dc010eb91885c40638d62a048ac999
[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-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21 // modified by Michael KLOKOV  Wed Mar  6 15:01:25 2002
22 // modified by  Eugeny MALTCHIKOV Wed Jul 04 11:13:01 2012 
23
24 #include <BRepAlgoAPI_Section.ixx>
25
26
27 #include <BRepBuilderAPI_MakeFace.hxx>
28 #include <BRepBuilderAPI_MakeShell.hxx>
29 #include <Geom_Plane.hxx>
30 #include <Geom2d_TrimmedCurve.hxx>
31
32 #include <TopExp.hxx>
33 #include <TopTools_IndexedMapOfShape.hxx>
34 #include <TopTools_MapOfShape.hxx>
35 #include <TopoDS.hxx>
36 #include <TopoDS_Face.hxx>
37 #include <BRep_Tool.hxx>
38
39 #include <BOPAlgo_PaveFiller.hxx>
40 #include <BOPAlgo_BOP.hxx>
41 #include <BOPDS_DS.hxx>
42
43 static TopoDS_Shape MakeShape(const Handle(Geom_Surface)& S)
44 {
45   GeomAbs_Shape c = S->Continuity();
46   if (c >= GeomAbs_C2) return BRepBuilderAPI_MakeFace(S, Precision::Confusion());
47   else return BRepBuilderAPI_MakeShell(S);
48 }
49
50 static Standard_Boolean HasAncestorFaces(const BOPAlgo_PPaveFiller& theDSFiller, 
51                                          const TopoDS_Shape&      E,
52                                          TopoDS_Shape&            F1,
53                                          TopoDS_Shape&            F2);
54
55 //=======================================================================
56 //function : Constructor
57 //purpose  : 
58 //=======================================================================
59 BRepAlgoAPI_Section::BRepAlgoAPI_Section(const TopoDS_Shape& Sh1,
60                                          const TopoDS_Shape& Sh2,
61                                          const Standard_Boolean PerformNow)
62 : BRepAlgoAPI_BooleanOperation(Sh1, Sh2, BOPAlgo_SECTION)
63 {
64   InitParameters();
65   myparameterschanged = Standard_True;
66
67   if(myS1.IsNull() || myS2.IsNull()) {
68     //     StdFail_NotDone::Raise("BRepAlgoAPI_Section : Shape NULL");
69     myshapeisnull = Standard_True;
70   }
71   if (PerformNow) {
72     Build();
73   }
74 }
75
76 BRepAlgoAPI_Section::BRepAlgoAPI_Section(const TopoDS_Shape&      aS1, 
77                                          const TopoDS_Shape&      aS2,
78                                          const BOPAlgo_PaveFiller& aDSF,
79                                          const Standard_Boolean   PerformNow)
80 : BRepAlgoAPI_BooleanOperation(aS1, aS2, aDSF, BOPAlgo_SECTION)
81 {
82   InitParameters();
83   myparameterschanged = Standard_True;
84
85   if(myS1.IsNull() || myS2.IsNull()) {
86     //     StdFail_NotDone::Raise("BRepAlgoAPI_Section : Shape NULL");
87     myshapeisnull = Standard_True;
88   }
89
90   if (PerformNow) {
91     Build();
92   }
93 }
94
95
96 //=======================================================================
97 //function : Constructor
98 //purpose  : 
99 //=======================================================================
100 BRepAlgoAPI_Section::BRepAlgoAPI_Section(const TopoDS_Shape&    Sh,
101                                          const gp_Pln&          Pl,
102                                          const Standard_Boolean PerformNow)
103 : BRepAlgoAPI_BooleanOperation(Sh, MakeShape(new Geom_Plane(Pl)), BOPAlgo_SECTION)
104 {
105   InitParameters();
106   myparameterschanged = Standard_True;
107
108   if(Sh.IsNull() || myS2.IsNull()) {
109     //     StdFail_NotDone::Raise("BRepAlgoAPI_Section : Shape NULL");
110     myshapeisnull = Standard_True;
111   }
112   if (PerformNow) {
113     Build();
114   }
115 }
116
117 //=======================================================================
118 //function : Constructor
119 //purpose  : 
120 //=======================================================================
121 BRepAlgoAPI_Section::BRepAlgoAPI_Section(const TopoDS_Shape&         Sh,
122                                                const Handle(Geom_Surface)& Sf,
123                                                const Standard_Boolean      PerformNow)
124 : BRepAlgoAPI_BooleanOperation(Sh, MakeShape(Sf), BOPAlgo_SECTION)
125 {
126   InitParameters();
127   myparameterschanged = Standard_True;
128
129   if(Sh.IsNull() || myS2.IsNull()) {
130 //     StdFail_NotDone::Raise("BRepAlgoAPI_Section : Shape NULL");
131     myshapeisnull = Standard_True;
132   }
133   if (PerformNow) {
134     Build();
135   }
136 }
137
138 //=======================================================================
139 //function : Constructor
140 //purpose  : 
141 //=======================================================================
142 BRepAlgoAPI_Section::BRepAlgoAPI_Section(const Handle(Geom_Surface)& Sf,
143                                                const TopoDS_Shape&         Sh,
144                                                const Standard_Boolean      PerformNow)
145 : BRepAlgoAPI_BooleanOperation(MakeShape(Sf), Sh, BOPAlgo_SECTION)
146 {
147   InitParameters();
148   myparameterschanged = Standard_True;
149
150   if(myS1.IsNull() || Sh.IsNull()) {
151 //     StdFail_NotDone::Raise("BRepAlgoAPI_Section : Shape NULL");
152     myshapeisnull = Standard_True;
153   }
154   if (PerformNow) {
155     Build();
156   }
157 }
158
159 //=======================================================================
160 //function : Constructor
161 //purpose  : 
162 //=======================================================================
163 BRepAlgoAPI_Section::BRepAlgoAPI_Section(const Handle(Geom_Surface)& Sf1,
164                                                const Handle(Geom_Surface)& Sf2,
165                                                const Standard_Boolean      PerformNow)
166 : BRepAlgoAPI_BooleanOperation(MakeShape(Sf1), MakeShape(Sf2), BOPAlgo_SECTION)
167 {
168   InitParameters();
169   myparameterschanged = Standard_True;
170
171   if(myS1.IsNull() || myS2.IsNull()) {
172 //     StdFail_NotDone::Raise("BRepAlgoAPI_Section : Shape NULL");
173     myshapeisnull = Standard_True;
174   }
175
176   if (PerformNow) {
177     Build();
178   }
179 }
180
181 //=======================================================================
182 //function : Init1
183 //purpose  : 
184 //=======================================================================
185 void BRepAlgoAPI_Section::Init1(const TopoDS_Shape& S1) 
186 {
187   if(!S1.IsNull()) {
188     if (!S1.IsEqual(myS1)) {
189       myS1 = S1;
190
191       if(!myS2.IsNull()) {
192         myshapeisnull = Standard_False;
193       }
194       myparameterschanged = Standard_True;
195     }
196   }
197   else {
198     if(!myS1.IsNull()) {
199       myS1 = S1;
200       myshapeisnull = Standard_True;
201       myparameterschanged = Standard_True;
202     }
203   }
204
205   if(myparameterschanged)
206     NotDone();
207 }
208
209 //=======================================================================
210 //function : Init1
211 //purpose  : 
212 //=======================================================================
213 void BRepAlgoAPI_Section::Init1(const gp_Pln& Pl) 
214 {
215   Init1(MakeShape(new Geom_Plane(Pl)));
216 }
217
218 //=======================================================================
219 //function : Init1
220 //purpose  : 
221 //=======================================================================
222 void BRepAlgoAPI_Section::Init1(const Handle(Geom_Surface)& Sf) 
223 {
224   Init1(MakeShape(Sf));
225 }
226
227 //=======================================================================
228 //function : Init2
229 //purpose  : 
230 //=======================================================================
231 void BRepAlgoAPI_Section::Init2(const TopoDS_Shape& S2) 
232 {
233   if(!S2.IsNull()) {
234     if (!S2.IsEqual(myS2)) {
235       myS2 = S2;
236
237       if(!myS1.IsNull()) {
238         myshapeisnull = Standard_False;
239       }
240       myparameterschanged = Standard_True;
241     }
242   }
243   else {
244     if(!myS2.IsNull()) {
245       myS2 = S2;
246       myshapeisnull = Standard_True;
247       myparameterschanged = Standard_True;
248     }
249   }
250
251   if(myparameterschanged) {
252     NotDone();
253   }
254 }
255
256 //=======================================================================
257 //function : Init2
258 //purpose  : 
259 //=======================================================================
260 void BRepAlgoAPI_Section::Init2(const gp_Pln& Pl) 
261 {
262   Init2(MakeShape(new Geom_Plane(Pl)));
263 }
264
265 //=======================================================================
266 //function : Init2
267 //purpose  : 
268 //=======================================================================
269 void BRepAlgoAPI_Section::Init2(const Handle(Geom_Surface)& Sf) 
270 {
271   Init2(MakeShape(Sf));
272 }
273
274 //=======================================================================
275 //function : Approximation
276 //purpose  : 
277 //=======================================================================
278 void BRepAlgoAPI_Section::Approximation(const Standard_Boolean B) 
279 {
280   if(myApprox != B) {
281     myApprox = B;
282     myparameterschanged = Standard_True;
283   }
284 }
285
286 //=======================================================================
287 //function : ComputePCurveOn1
288 //purpose  : 
289 //=======================================================================
290 void BRepAlgoAPI_Section::ComputePCurveOn1(const Standard_Boolean B) 
291 {
292   if(myComputePCurve1 != B) {
293     myComputePCurve1 = B;
294     myparameterschanged = Standard_True;
295   }
296 }
297
298 //=======================================================================
299 //function : ComputePCurveOn2
300 //purpose  : 
301 //=======================================================================
302 void BRepAlgoAPI_Section::ComputePCurveOn2(const Standard_Boolean B) 
303 {
304   if(myComputePCurve2 != B) {
305     myComputePCurve2 = B;
306     myparameterschanged = Standard_True;
307   }
308 }
309
310 //=======================================================================
311 //function : Build
312 //purpose  : 
313 //=======================================================================
314 void BRepAlgoAPI_Section::Build() 
315 {
316   if(myshapeisnull) {
317     myErrorStatus = 2;
318     NotDone();
319     return;
320   }
321
322   if(myparameterschanged) {
323     Standard_Boolean bIsNewFiller = PrepareFiller();
324     //
325     if (myErrorStatus!=1) {
326       NotDone();
327       // there were errors during the preparation 
328       return;
329     }
330     //
331     if (bIsNewFiller) {
332       BOPAlgo_SectionAttribute theSecAttr(myApprox, myComputePCurve1, myComputePCurve2);
333       myDSFiller->SetSectionAttribute(theSecAttr);
334       myDSFiller->Perform();
335     }
336     //
337     BRepAlgoAPI_BooleanOperation::Build();
338     //
339     myparameterschanged = Standard_False;
340   }
341 }
342
343 //=======================================================================
344 //function : HasAncestorFaceOn1
345 //purpose  : 
346 //=======================================================================
347 Standard_Boolean BRepAlgoAPI_Section::HasAncestorFaceOn1(const TopoDS_Shape& E, TopoDS_Shape& F) const
348 {
349   Standard_Boolean aResult = Standard_False;
350   if(E.IsNull()) {
351     return aResult;
352   }
353
354   if(E.ShapeType() != TopAbs_EDGE) {
355     return aResult;
356   }
357   TopoDS_Shape F1, F2;
358   aResult = HasAncestorFaces(myDSFiller, E, F1, F2);
359
360   if(F1.IsNull()) {
361     return Standard_False;
362   }
363   F = F1;
364   return aResult;
365 }
366
367 //=======================================================================
368 //function : HasAncestorFaceOn2
369 //purpose  : 
370 //=======================================================================
371 Standard_Boolean BRepAlgoAPI_Section::HasAncestorFaceOn2(const TopoDS_Shape& E,TopoDS_Shape& F) const
372 {
373   Standard_Boolean aResult = Standard_False;
374   if(E.IsNull()) {
375     return aResult;
376   }
377
378   if(E.ShapeType() != TopAbs_EDGE) {
379     return aResult;
380   }
381   TopoDS_Shape F1, F2;
382   aResult = HasAncestorFaces(myDSFiller, E, F1, F2);
383
384   if(F2.IsNull()) {
385     return Standard_False;
386   }
387   F = F2;
388   return aResult;
389 }
390
391 //=======================================================================
392 //function : PCurveOn1
393 //purpose  : 
394 //=======================================================================
395 Handle(Geom2d_Curve) BRepAlgoAPI_Section::PCurveOn1(const TopoDS_Shape& E) const
396 {
397   Handle(Geom2d_Curve) aResult;
398   
399   if(myComputePCurve1) {
400     TopoDS_Shape aShape;
401
402     if(HasAncestorFaceOn1(E, aShape)) {
403       const TopoDS_Edge& anEdge = TopoDS::Edge(E);
404       const TopoDS_Face& aFace  = TopoDS::Face(aShape);
405       Standard_Real f, l;
406       aResult = BRep_Tool::CurveOnSurface(anEdge, aFace, f, l);
407
408       if(!aResult->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
409         aResult = new Geom2d_TrimmedCurve(aResult, f, l);
410       }
411     }
412   }
413   return aResult;
414 }
415
416 //=======================================================================
417 //function : PCurveOn2
418 //purpose  : 
419 //=======================================================================
420 Handle(Geom2d_Curve) BRepAlgoAPI_Section::PCurveOn2(const TopoDS_Shape& E) const
421 {
422   Handle(Geom2d_Curve) aResult;
423   
424   if(myComputePCurve2) {
425     TopoDS_Shape aShape;
426
427     if(HasAncestorFaceOn2(E, aShape)) {
428       const TopoDS_Edge& anEdge = TopoDS::Edge(E);
429       const TopoDS_Face& aFace  = TopoDS::Face(aShape);
430       Standard_Real f, l;
431       aResult = BRep_Tool::CurveOnSurface(anEdge, aFace, f, l);
432       
433       if(!aResult->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
434         aResult = new Geom2d_TrimmedCurve(aResult, f, l);
435       }
436     }
437   }
438   return aResult;
439 }
440
441 //=======================================================================
442 //function : InitParameters
443 //purpose  : 
444 //=======================================================================
445 void BRepAlgoAPI_Section::InitParameters() 
446 {
447   myparameterschanged = Standard_False;
448   myshapeisnull = Standard_False;
449   myApprox = Standard_False;
450   myComputePCurve1 = Standard_False;
451   myComputePCurve2 = Standard_False;
452 }
453
454 // ------------------------------------------------------------------------
455 // static function : HasAncestorFaces
456 // purpose  : 
457 // ------------------------------------------------------------------------
458 static Standard_Boolean HasAncestorFaces(const BOPAlgo_PPaveFiller& theDSFiller, 
459                                          const TopoDS_Shape&      E,
460                                          TopoDS_Shape&            F1,
461                                          TopoDS_Shape&            F2) {
462
463   Standard_Integer aNb, i, j, nE, nF1, nF2, aNbCurves;;
464   //
465   const BOPDS_PDS& pDS = theDSFiller->PDS();
466   BOPDS_VectorOfInterfFF& aFFs=pDS->InterfFF();
467   //
468   aNb=aFFs.Extent();
469
470   for (i = 0; i < aNb; i++) {
471     BOPDS_InterfFF& aFFi=aFFs(i);
472     aFFi.Indices(nF1, nF2);
473     //
474     //section edges
475     const BOPDS_VectorOfCurve& aSeqOfCurve=aFFi.Curves();
476     aNbCurves=aSeqOfCurve.Extent();
477     for (j=0; j<aNbCurves; j++) {
478       const BOPDS_Curve& aCurve=aSeqOfCurve(j);
479
480       const BOPDS_ListOfPaveBlock& aSectEdges = aCurve.PaveBlocks();
481
482       BOPDS_ListIteratorOfListOfPaveBlock anIt;
483       anIt.Initialize(aSectEdges);
484
485       for(; anIt.More(); anIt.Next()) {
486         const Handle(BOPDS_PaveBlock)& aPB = anIt.Value();
487         nE = aPB->Edge();
488         if(nE < 0)  continue;
489         //
490         if(E.IsSame(pDS->Shape(nE))) {
491           F1 = pDS->Shape(nF1);
492           F2 = pDS->Shape(nF2);
493           return Standard_True;
494         }
495       }
496     }
497
498     //existing pave blocks
499     BOPCol_ListOfInteger aLSE;
500     BOPCol_ListIteratorOfListOfInteger aItLSE;
501     //
502     pDS->SharedEdges(nF1, nF2, aLSE, theDSFiller->Allocator());
503     aItLSE.Initialize(aLSE);
504     for (; aItLSE.More(); aItLSE.Next()) {
505       nE = aItLSE.Value();
506       //
507       if(E.IsSame(pDS->Shape(nE))) {
508         F1 = pDS->Shape(nF1);
509         F2 = pDS->Shape(nF2);
510         return Standard_True;
511       }
512     }
513   }
514   return Standard_False;
515 }