0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / BRepAlgo / BRepAlgo_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
18 #include <BRep_Builder.hxx>
19 #include <BRep_Tool.hxx>
20 #include <BRepAlgo_Section.hxx>
21 #include <BRepBuilderAPI_MakeFace.hxx>
22 #include <BRepBuilderAPI_MakeShell.hxx>
23 #include <Geom2d_TrimmedCurve.hxx>
24 #include <Geom_Plane.hxx>
25 #include <Geom_Surface.hxx>
26 #include <gp_Pln.hxx>
27 #include <TopoDS.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopOpeBRepBuild_HBuilder.hxx>
30 #include <TopOpeBRepBuild_Tools.hxx>
31 #include <TopOpeBRepDS_BuildTool.hxx>
32 #include <TopOpeBRepDS_HDataStructure.hxx>
33 #include <TopTools_ListIteratorOfListOfShape.hxx>
34
35 static TopoDS_Shape MakeShape(const Handle(Geom_Surface)& );
36
37 Standard_DISABLE_DEPRECATION_WARNINGS
38
39 //=======================================================================
40 //function : BRepAlgo_Section
41 //purpose  : 
42 //=======================================================================
43   BRepAlgo_Section::BRepAlgo_Section(const TopoDS_Shape& S1, 
44                                      const TopoDS_Shape& S2,
45                                      const Standard_Boolean PerformNow)
46 : BRepAlgo_BooleanOperation(S1, S2)
47 {
48   InitParameters();
49   myS1Changed = Standard_True;
50   myS2Changed = Standard_True;  
51   if(myS1.IsNull() || S2.IsNull()) {
52     myshapeisnull = Standard_True;
53   }
54   if (PerformNow)
55     Build();
56 }
57
58 //=======================================================================
59 //function : BRepAlgo_Section
60 //purpose  : 
61 //=======================================================================
62   BRepAlgo_Section::BRepAlgo_Section(const TopoDS_Shape& S1, 
63                                      const gp_Pln& Pl,
64                                      const Standard_Boolean PerformNow) 
65 : BRepAlgo_BooleanOperation(S1, MakeShape(new Geom_Plane(Pl)))
66
67   InitParameters();
68   myS1Changed = Standard_True;
69   myS2Changed = Standard_True;  
70   if(S1.IsNull() || myS2.IsNull()) {
71     myshapeisnull = Standard_True;
72   }
73   if (PerformNow)
74     Build();
75 }
76
77 //=======================================================================
78 //function : BRepAlgo_Section
79 //purpose  : 
80 //=======================================================================
81   BRepAlgo_Section::BRepAlgo_Section(const TopoDS_Shape& S1, 
82                                      const Handle(Geom_Surface)& Sf,
83                                      const Standard_Boolean PerformNow)
84 : BRepAlgo_BooleanOperation(S1, MakeShape(Sf))
85 {
86   InitParameters();
87   myS1Changed = Standard_True;
88   myS2Changed = Standard_True;  
89   if(S1.IsNull() || myS2.IsNull()) {
90     myshapeisnull = Standard_True;
91   }
92   if (PerformNow)
93     Build();
94 }
95
96 //=======================================================================
97 //function : BRepAlgo_Section
98 //purpose  : 
99 //=======================================================================
100   BRepAlgo_Section::BRepAlgo_Section(const Handle(Geom_Surface)& Sf, 
101                                      const TopoDS_Shape& S2,
102                                      const Standard_Boolean PerformNow)
103 : BRepAlgo_BooleanOperation(MakeShape(Sf), S2)
104 {
105   InitParameters();
106   myS1Changed = Standard_True;
107   myS2Changed = Standard_True;  
108   if(myS1.IsNull() || S2.IsNull()) {
109     myshapeisnull = Standard_True;
110   }
111   if (PerformNow)
112     Build();
113 }
114
115 //=======================================================================
116 //function : BRepAlgo_Section
117 //purpose  : 
118 //=======================================================================
119   BRepAlgo_Section::BRepAlgo_Section(const Handle(Geom_Surface)& Sf1,
120                                      const Handle(Geom_Surface)& Sf2,
121                                      const Standard_Boolean PerformNow)
122 : BRepAlgo_BooleanOperation(MakeShape(Sf1), MakeShape(Sf2))
123 {
124   InitParameters();
125   myS1Changed = Standard_True;
126   myS2Changed = Standard_True;  
127   if(myS1.IsNull() || myS2.IsNull()) {
128     myshapeisnull = Standard_True;
129   }
130   if (PerformNow)
131     Build();
132 }
133
134 //=======================================================================
135 //function : Init1
136 //purpose  : initialize the first Shape
137 //=======================================================================
138   void BRepAlgo_Section::Init1(const TopoDS_Shape& S1)
139 {
140   if(!S1.IsNull()) {
141     if (!S1.IsEqual(myS1)) {
142       myS1 = S1;
143       myS1Changed = Standard_True;
144     }
145   } else {
146     if(!myS1.IsNull()) {
147       myS1 = S1;
148       myS1Changed = Standard_True;
149     }      
150   }
151   if (myS1Changed || myS2Changed)
152     NotDone();
153 }
154
155 //=======================================================================
156 //function : Init1
157 //purpose  : initialize the first Shape
158 //=======================================================================
159   void BRepAlgo_Section::Init1(const gp_Pln& Pl)
160 {
161   Init1(MakeShape(new Geom_Plane(Pl)));
162 }
163
164 //=======================================================================
165 //function : Init1
166 //purpose  : initialize the first Shape
167 //=======================================================================
168   void BRepAlgo_Section::Init1(const Handle(Geom_Surface)& Sf)
169 {
170   Init1(MakeShape(Sf));
171 }
172
173 //=======================================================================
174 //function : Init2
175 //purpose  : initialize the second Shape
176 //=======================================================================
177   void BRepAlgo_Section::Init2(const TopoDS_Shape& S2)
178 {
179   if(!S2.IsNull()) {
180     if (!S2.IsEqual(myS2)) {
181       myS2 = S2;
182       myS2Changed = Standard_True;
183     }
184   } else {
185     if(!myS2.IsNull()) {
186       myS2 = S2;
187       myS2Changed = Standard_True;
188     }      
189   }
190   if (myS1Changed || myS2Changed)
191     NotDone();
192 }
193
194 //=======================================================================
195 //function : Init2
196 //purpose  : initialize the second Shape
197 //=======================================================================
198   void BRepAlgo_Section::Init2(const gp_Pln& Pl)
199 {
200   Init2(MakeShape(new Geom_Plane(Pl)));
201 }
202
203 //=======================================================================
204 //function : Init2
205 //purpose  : initialize the second Shape
206 //=======================================================================
207   void BRepAlgo_Section::Init2(const Handle(Geom_Surface)& Sf)
208 {
209   Init2(MakeShape(Sf));
210 }
211
212 //=======================================================================
213 //function : Approximation
214 //purpose  : To learn if an approximation of the geometry is calculated
215 //=======================================================================
216   void BRepAlgo_Section::Approximation(const Standard_Boolean Approx)
217 {
218   TopOpeBRepDS_BuildTool& BTofBuilder = myHBuilder->ChangeBuildTool();
219   TopOpeBRepTool_GeomTool& GTofBTofBuilder = BTofBuilder.ChangeGeomTool();
220
221   TopOpeBRepTool_OutCurveType OCT = 
222     (Approx) ? TopOpeBRepTool_APPROX :TopOpeBRepTool_BSPLINE1;  
223   if (GTofBTofBuilder.TypeC3D() != OCT) {
224     myApproxChanged = Standard_True;
225     GTofBTofBuilder.Define(OCT);
226   }  
227   
228   if (myApproxChanged)
229     NotDone();
230 }
231
232 //=======================================================================
233 //function : ComputePCurveOn1
234 //purpose  : To learn if PCurves are calculated on the faces of the 1st Shape
235 //=======================================================================
236   void BRepAlgo_Section::ComputePCurveOn1(const Standard_Boolean ComputePCurve1)
237 {
238   TopOpeBRepDS_BuildTool& BTofBuilder = myHBuilder->ChangeBuildTool();
239   TopOpeBRepTool_GeomTool& GTofBTofBuilder = BTofBuilder.ChangeGeomTool();
240
241   if( GTofBTofBuilder.CompPC1() != ComputePCurve1) {
242     GTofBTofBuilder.DefinePCurves1(ComputePCurve1);
243     myPCurve1Changed = Standard_True;
244   }
245
246   if(myPCurve1Changed)
247     NotDone();
248 }
249
250 //=======================================================================
251 //function : ComputePCurve2
252 //purpose  : To learn if PCurves are calculated on the faces of the 2nd Shape
253 //=======================================================================
254   void BRepAlgo_Section::ComputePCurveOn2(const Standard_Boolean ComputePCurve2)
255 {
256   TopOpeBRepDS_BuildTool& BTofBuilder = myHBuilder->ChangeBuildTool();
257   TopOpeBRepTool_GeomTool& GTofBTofBuilder = BTofBuilder.ChangeGeomTool();
258   
259   if( GTofBTofBuilder.CompPC2() != ComputePCurve2) {
260     GTofBTofBuilder.DefinePCurves2(ComputePCurve2);
261     myPCurve2Changed = Standard_True;
262   }
263   
264   if(myPCurve2Changed)
265     NotDone();
266 }
267
268 //=======================================================================
269 //function : Build
270 //purpose  : compute the section
271 //=======================================================================
272   void BRepAlgo_Section::Build()
273 {
274   if (myS1Changed || 
275       myS2Changed ||
276       myApproxChanged ||
277       myPCurve1Changed ||
278       myPCurve2Changed) {
279     PerformDS();
280     Standard_Boolean bcw = BuilderCanWork();
281     if ( ! bcw || myshapeisnull) return;
282     BRep_Builder BB;
283     BB.MakeCompound(TopoDS::Compound(myShape));
284     Handle(TopOpeBRepBuild_HBuilder) HB = Builder();
285     TopTools_ListIteratorOfListOfShape itloe = HB->Section();
286     for(; itloe.More(); itloe.Next()) BB.Add(myShape,itloe.Value());
287     
288     TopOpeBRepBuild_Tools::CorrectTolerances(myShape);
289     
290     
291     Done();
292   }
293
294
295 }
296
297 //=======================================================================
298 //function : HasAncestorFaceOn1
299 //purpose  : 
300 //=======================================================================
301   Standard_Boolean BRepAlgo_Section::HasAncestorFaceOn1(const TopoDS_Shape& E, 
302                                                            TopoDS_Shape& F)const
303 {
304   TopoDS_Shape F1,F2; Standard_Integer iC;
305   Standard_Boolean res = myHBuilder->EdgeCurveAncestors(E,F1,F2,iC);
306   if ( res ) F = F1;
307   return res;
308 }
309
310 //=======================================================================
311 //function : HasAncestorFaceOn2
312 //purpose  : 
313 //=======================================================================
314   Standard_Boolean BRepAlgo_Section::HasAncestorFaceOn2(const TopoDS_Shape& E, 
315                                                            TopoDS_Shape& F)const
316 {
317   TopoDS_Shape F1,F2; Standard_Integer iC;
318   Standard_Boolean res = myHBuilder->EdgeCurveAncestors(E,F1,F2,iC);
319   if ( res ) F = F2;
320   return res;
321 }
322
323 //=======================================================================
324 //function : InitParameters
325 //purpose  : initialize the fields of the class
326 //=======================================================================
327   void BRepAlgo_Section::InitParameters()
328 {
329   TopOpeBRepDS_BuildTool& BTofBuilder = myHBuilder->ChangeBuildTool();
330   TopOpeBRepTool_GeomTool& GTofBTofBuilder = BTofBuilder.ChangeGeomTool();
331   
332   GTofBTofBuilder.Define(TopOpeBRepTool_BSPLINE1);
333   GTofBTofBuilder.DefineCurves(Standard_True);
334   GTofBTofBuilder.DefinePCurves1(Standard_False);
335   GTofBTofBuilder.DefinePCurves2(Standard_False);
336   
337   myS1Changed = Standard_False;
338   myS2Changed = Standard_False;
339   // 
340   myApproxChanged = Standard_False;
341   // 
342   myPCurve1Changed = Standard_False;
343   //  
344   myPCurve2Changed = Standard_False;
345   myshapeisnull = Standard_False;
346 }
347 //=======================================================================
348 //function : MakeShape
349 //purpose  : 
350 //=======================================================================
351 TopoDS_Shape MakeShape(const Handle(Geom_Surface)& S)
352 {
353   GeomAbs_Shape c = S->Continuity();
354   if (c >= GeomAbs_C2) return BRepBuilderAPI_MakeFace(S, Precision::Confusion());
355   else return BRepBuilderAPI_MakeShell(S);
356 }
357
358 Standard_ENABLE_DEPRECATION_WARNINGS