0032768: Coding - get rid of unused headers [BopAlgo to BRepBuilderAPI]
[occt.git] / src / BOPTest / BOPTest_TolerCommands.cxx
CommitLineData
b311480e 1// Created on: 2000-03-16
973c2be1 2// Copyright (c) 2000-2014 OPEN CASCADE SAS
b311480e 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
b311480e 5//
d5f74e42 6// This library is free software; you can redistribute it and/or modify it under
7// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 8// by the Free Software Foundation, with special exception defined in the file
9// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10// distribution for complete text of the license and disclaimer of any warranty.
b311480e 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
7fd59977 14
7fd59977 15
42cf5bc1 16#include <BOPTest.hxx>
17#include <BRep_Builder.hxx>
42cf5bc1 18#include <BRep_TEdge.hxx>
19#include <BRep_TFace.hxx>
20#include <BRep_Tool.hxx>
21#include <BRep_TVertex.hxx>
22#include <BRepLib.hxx>
23#include <DBRep.hxx>
24#include <Draw.hxx>
7fd59977 25#include <Geom2d_Curve.hxx>
42cf5bc1 26#include <Geom_Curve.hxx>
7fd59977 27#include <Geom_Surface.hxx>
42cf5bc1 28#include <gp_Pnt2d.hxx>
42cf5bc1 29#include <TColStd_IndexedMapOfTransient.hxx>
30#include <TopAbs_Orientation.hxx>
31#include <TopExp.hxx>
32#include <TopExp_Explorer.hxx>
33#include <TopLoc_Location.hxx>
7fd59977 34#include <TopoDS.hxx>
7fd59977 35#include <TopoDS_Edge.hxx>
36#include <TopoDS_Face.hxx>
42cf5bc1 37#include <TopoDS_Shape.hxx>
38#include <TopoDS_Vertex.hxx>
39#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
40#include <TopTools_IndexedMapOfShape.hxx>
7fd59977 41#include <TopTools_ListOfShape.hxx>
7fd59977 42#include <TopTools_MapOfShape.hxx>
43cb0011 43
42cf5bc1 44#include <stdio.h>
7fd59977 45//
46static
47 void ProcessVertex(const TopoDS_Vertex&,
4e57c75e 48 const TopTools_ListOfShape&,
49 const TopTools_ListOfShape&);
7fd59977 50static
51 void ProcessEdge(const TopoDS_Edge&, const Standard_Real);
52
53static
54 void ReduceVertexTolerance (const TopoDS_Shape&);
55
56static
57 void ReduceFaceTolerance (const TopoDS_Shape&);
58
59static
43cb0011 60 void ReduceEdgeTolerance (const TopoDS_Shape&,
61 const Standard_Real);
7fd59977 62
43cb0011 63static
64 void PreparePCurves(const TopoDS_Shape& ,
65 Draw_Interpretor& di);
66//
67static Standard_Integer breducetolerance (Draw_Interpretor&, Standard_Integer, const char** );
68static Standard_Integer btolx (Draw_Interpretor&, Standard_Integer, const char** );
69static Standard_Integer bopaddpcs (Draw_Interpretor&, Standard_Integer, const char** );
7fd59977 70//=======================================================================
71//function : TolerCommands
72//purpose :
73//=======================================================================
74 void BOPTest::TolerCommands(Draw_Interpretor& theCommands)
75{
76 static Standard_Boolean done = Standard_False;
77 if (done)
78 return;
79
80 done = Standard_True;
81 // Chapter's name
43cb0011 82 const char* g = "BOPTest commands";
83 //
84 theCommands.Add("breducetolerance" , "use breducetolerance Shape",
85 __FILE__, breducetolerance, g);
86 theCommands.Add("btolx" , "use btolx Shape [minTol=1.e-7]",
87 __FILE__, btolx, g);
88 theCommands.Add("bopaddpcs" , "Use >bopaddpcs Shape",
89 __FILE__, bopaddpcs, g);
7fd59977 90}
7fd59977 91//=======================================================================
92//function : btolx
93//purpose :
94//=======================================================================
43cb0011 95Standard_Integer btolx(Draw_Interpretor& di,
96 Standard_Integer n,
97 const char** a)
7fd59977 98{
99 if (n<2) {
43cb0011 100 di << " use btolx Shape [minTol=1.e-7]\n";
7fd59977 101 return 1;
102 }
103
104 TopoDS_Shape aS = DBRep::Get(a[1]);
105
106 if (aS.IsNull()) {
43cb0011 107 di << " Null shape is not allowed\n";
7fd59977 108 return 1;
109 }
110 //
111 Standard_Real aTolEMin=1.e-7;
112 if (n==3) {
91322f44 113 aTolEMin=Draw::Atof(a[2]);
7fd59977 114 }
115 //
116 // Edge Tolerances
117 ReduceEdgeTolerance(aS, aTolEMin);
118 //
119 // Face Tolerances
120 ReduceFaceTolerance(aS);
121 //
122 // Vertex Tolerances
123 ReduceVertexTolerance(aS);
124 //
125 BRepLib::SameParameter(aS, 1.e-7, Standard_True);
126 //
127 DBRep::Set (a[1], aS);
128 return 0;
129}
130//=======================================================================
131//function : ReduceEdgeTolerance
132//purpose :
133//=======================================================================
43cb0011 134void ReduceEdgeTolerance (const TopoDS_Shape& aS,
135 const Standard_Real aTolTreshold)
7fd59977 136{
137 Standard_Integer i, aNbE;
138 TopTools_IndexedMapOfShape aEMap;
139 //
140 TopExp::MapShapes(aS, TopAbs_EDGE, aEMap);
141 //
142 aNbE=aEMap.Extent();
143 for (i=1; i<=aNbE; i++) {
144 const TopoDS_Edge& aE= TopoDS::Edge(aEMap(i));
145
146 ProcessEdge(aE, aTolTreshold);
147 }
148}
149//=======================================================================
150//function : ReduceFaceTolerance
151//purpose :
152//=======================================================================
153void ReduceFaceTolerance (const TopoDS_Shape& aS)
154{
155 Standard_Integer i, j, aNbF, aNbE;
156 Standard_Real aTolE, aTolx, aTolEMin;
157 TopTools_IndexedMapOfShape aFMap, aEMap;
158 //
159 aTolEMin=1.e-7;
160 //
161 TopExp::MapShapes(aS, TopAbs_FACE, aFMap);
162 aNbF=aFMap.Extent();
163 for (i=1; i<=aNbF; i++) {
164 aTolx=1.e6;
165 const TopoDS_Face& aF= TopoDS::Face(aFMap(i));
166 Handle(BRep_TFace)& aTF = *((Handle(BRep_TFace)*)&aF.TShape());
167 //
168 TopExp::MapShapes(aF, TopAbs_EDGE, aEMap);
169 aNbE=aEMap.Extent();
170 for (j=1; j<=aNbE; ++j) {
171 const TopoDS_Edge& aE= TopoDS::Edge(aEMap(j));
172 aTolE =BRep_Tool::Tolerance(aE);
173 if (aTolE<aTolx) {
43cb0011 174 aTolx=aTolE;
7fd59977 175 }
176 }
177 aTolE=(aTolx>aTolEMin) ? aTolx : aTolEMin;
178 aTF->Tolerance(aTolE);
179 }
180}
181//=======================================================================
182//function : ReduceVertexTolerance
183//purpose :
184//=======================================================================
185void ReduceVertexTolerance (const TopoDS_Shape& aS)
186{
187 Standard_Integer i, aNbV;
188 TopTools_IndexedDataMapOfShapeListOfShape aVEMap, aVFMap;
43cb0011 189
f1191d30 190 TopExp::MapShapesAndUniqueAncestors(aS, TopAbs_VERTEX, TopAbs_EDGE, aVEMap);
191 TopExp::MapShapesAndUniqueAncestors(aS, TopAbs_VERTEX, TopAbs_FACE, aVFMap);
7fd59977 192
193 aNbV=aVEMap.Extent();
194 for (i=1; i<=aNbV; i++) {
195 const TopoDS_Vertex& aV= TopoDS::Vertex(aVEMap.FindKey(i));
196 const TopTools_ListOfShape& aLE=aVEMap(i);
197 const TopTools_ListOfShape& aLF=aVFMap.FindFromKey(aV);
198
199 ProcessVertex(aV, aLE, aLF);
200 }
201}
202//=======================================================================
203//function : ProcessEdge
204//purpose :
205//=======================================================================
206void ProcessEdge(const TopoDS_Edge& aE, const Standard_Real aTolTreshold)
207{
208 Standard_Integer i, aNb=23;
96a95605 209 Standard_Real aD2, aTolMax2, aT1, aT2, aT, dT;
7fd59977 210 gp_Pnt aPC3D, aP3D;
211 gp_Pnt2d aPC2D;
212
213 //TopTools_ListIteratorOfListOfShape anIt;// Wng in Gcc 3.0
214 BRep_ListIteratorOfListOfCurveRepresentation itcr;
215 //
216 Handle(Geom_Curve) aC3D=BRep_Tool::Curve(aE, aT1, aT2);
217 if (aC3D.IsNull()) {
218 return;
219 }
220 //
221 dT=(aT2-aT1)/aNb;
222 //
223 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&aE.TShape());
224 const TopLoc_Location& Eloc = aE.Location();
225 //
226 aTolMax2=-1.e6;
227 const BRep_ListOfCurveRepresentation& aLCR=TE->Curves();
228 //
229 itcr.Initialize(aLCR);
230 for (; itcr.More(); itcr.Next()) {
231 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
232 const TopLoc_Location& loc = cr->Location();
233 TopLoc_Location L = (Eloc * loc);//.Predivided(aV.Location());
234 //
235 // 3D-Curve
236 if (cr->IsCurve3D()) {
237 continue;
238 }
239 //
240 // 2D-Curve
241 else if (cr->IsCurveOnSurface()) {
242 const Handle(Geom2d_Curve)& aC2D = cr->PCurve();
243 if (aC2D.IsNull()) {
43cb0011 244 continue;
7fd59977 245 }
246 // Surface
247 const Handle(Geom_Surface)& aS=cr->Surface();
248 //
249 // 2D-point treatment
250 for (i=0; i<=aNb; ++i) {
43cb0011 251 aT=aT1+i*dT;
252 if (i==aNb) {
253 aT=aT2;
254 }
255 aPC3D=aC3D->Value(aT);
256 aPC2D=aC2D->Value(aT);
257 aS->D0(aPC2D.X(), aPC2D.Y(), aP3D);
258 aP3D.Transform(L.Transformation());
259 aD2=aPC3D.SquareDistance(aP3D);
260 if (aD2 > aTolMax2) {
261 aTolMax2=aD2;
262 }
7fd59977 263 }
264 } //if (cr->IsCurveOnSurface())
265 }//for (; itcr.More(); itcr.Next())
266
267 //#########################################################
268 //
269 if (aTolMax2<0.){
270 return;
271 }
272 //
7fd59977 273 //
274 aTolMax2=sqrt(aTolMax2);
275
276 //printf(" aTolMax=%15.10lf, aTolWas=%15.10lf\n", aTolMax2, aTolE);
277
278 Standard_Real aTolSet;
279 aTolSet=(aTolMax2>aTolTreshold) ? aTolMax2 : aTolTreshold;
280
281 TE->Tolerance(aTolSet);
282}
283//=======================================================================
284//function : ProcessVertex
285//purpose :
286//=======================================================================
287void ProcessVertex(const TopoDS_Vertex& aV,
43cb0011 288 const TopTools_ListOfShape& aLE,
289 const TopTools_ListOfShape& aLF)
7fd59977 290{
96a95605 291 Standard_Real aTol, aD2, aTolMax2, aTolE, aParam;
7fd59977 292 gp_Pnt aPC3D;
293 gp_Pnt2d aPC2D;
294 TopAbs_Orientation anOrV;
295
296 TopTools_ListIteratorOfListOfShape anIt;
7fd59977 297 TopExp_Explorer aVExp;
298
299 BRep_ListIteratorOfListOfCurveRepresentation itcr;
300 //
301 aTolMax2=-1.e6;
302 //
303 Handle(BRep_TVertex)& TV = *((Handle(BRep_TVertex)*) &aV.TShape());
304 const gp_Pnt& aPV3D = TV->Pnt();
305 aTol =BRep_Tool::Tolerance(aV);
7fd59977 306 //
307 anIt.Initialize(aLE);
308 for (; anIt.More(); anIt.Next()) {
309 const TopoDS_Edge& aE=TopoDS::Edge(anIt.Value());
310 //
7fd59977 311 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&aE.TShape());
312 const TopLoc_Location& Eloc = aE.Location();
313 //
314 aVExp.Init(aE, TopAbs_VERTEX);
315 for (; aVExp.More(); aVExp.Next()) {
316 const TopoDS_Vertex& aVx=TopoDS::Vertex(aVExp.Current());
317 //
318 if (!aVx.IsSame(aV)) {
43cb0011 319 continue;
7fd59977 320 }
321 //
322 anOrV=aVx.Orientation();
323 if (!(anOrV==TopAbs_FORWARD || anOrV==TopAbs_REVERSED)) {
43cb0011 324 continue;
7fd59977 325 }
326 //
327 const BRep_ListOfCurveRepresentation& aLCR=TE->Curves();
328 itcr.Initialize(aLCR);
329 for (; itcr.More(); itcr.Next()) {
43cb0011 330 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
331 const TopLoc_Location& loc = cr->Location();
332 TopLoc_Location L = (Eloc * loc).Predivided(aV.Location());
333 //
334 // 3D-Curve
335 if (cr->IsCurve3D()) {
336 const Handle(Geom_Curve)& aC3D = cr->Curve3D();
337 //
338 if (aC3D.IsNull()) {
339 continue;
340 }
341 // 3D-point treatment
342 aParam=BRep_Tool::Parameter(aVx, aE);
343 aPC3D= aC3D->Value(aParam);
344 aPC3D.Transform(L.Transformation());
345 aD2=aPV3D.SquareDistance(aPC3D);
346 if (aD2 > aTolMax2) {
347 aTolMax2=aD2;
348 }
349 //
350 }//if (cr->IsCurve3D())
351 //
352 // 2D-Curve
353 else if (cr->IsCurveOnSurface()) {
354 const Handle(Geom2d_Curve)& aC2D = cr->PCurve();
355 if (aC2D.IsNull()) {
356 continue;
357 }
358 // Surface
359 const Handle(Geom_Surface)& aS=cr->Surface();
360 //
361 // 2D-point treatment
362 aParam=BRep_Tool::Parameter(aVx, aE, aS, L);
363 aPC2D=aC2D->Value(aParam);
364 aS->D0(aPC2D.X(), aPC2D.Y(), aPC3D);
365 aPC3D.Transform(L.Transformation());
366 aD2=aPV3D.SquareDistance(aPC3D);
367 if (aD2 > aTolMax2) {
368 aTolMax2=aD2;
369 }
370 } //if (cr->IsCurveOnSurface())
371
7fd59977 372 }//for (; itcr.More(); itcr.Next())
373 }//for (; aVExp.More(); aVExp.Next())
374 }//for (; anIt.More(); anIt.Next())
375 //#########################################################
376 //
377 // Reducing
378 if (aTolMax2<0.){
379 return;
380 }
381 //
382 aTolMax2=sqrt(aTolMax2);
383 if (aTolMax2>aTol) {
384 return;
385 }
386 //
7fd59977 387 anIt.Initialize(aLE);
388 for (; anIt.More(); anIt.Next()) {
389 const TopoDS_Edge& aE=TopoDS::Edge(anIt.Value());
390
7fd59977 391 aTolE =BRep_Tool::Tolerance(aE);
392 if (aTolMax2 < aTolE) {
393 aTolMax2=aTolE;
394 }
395 }
396 //
7fd59977 397 anIt.Initialize(aLF);
398 for (; anIt.More(); anIt.Next()) {
399 const TopoDS_Face& aF=TopoDS::Face(anIt.Value());
400
7fd59977 401 aTolE =BRep_Tool::Tolerance(aF);
402 if (aTolMax2 < aTolE) {
403 aTolMax2=aTolE;
404 }
405 }
406 //
407 if (aTolMax2>aTol) {
408 return;
409 }
410 //
411 // Update Tolerance
412 TV->Tolerance(aTolMax2);
413}
414//=======================================================================
415//function : breducetolerance
416//purpose :
417//=======================================================================
418Standard_Integer breducetolerance(Draw_Interpretor& di,
43cb0011 419 Standard_Integer n,
420 const char** a)
7fd59977 421{
422 if (n<2) {
43cb0011 423 di << " use bupdatetolerance Shape\n";
7fd59977 424 return 1;
425 }
426
427 TopoDS_Shape aS = DBRep::Get(a[1]);
428
429 if (aS.IsNull()) {
430 di << " Null shape is not allowed \n";
431 return 1;
432 }
433 ReduceVertexTolerance(aS);
434 DBRep::Set (a[1], aS);
435
436 return 0;
437}
438//
7fd59977 439//=======================================================================
440//function : bopaddpcs
43cb0011 441//purpose : Some Edges do not contain P-Curves on Faces to which
442// they belong to.
7fd59977 443// These faces usually based on Geom_Plane surface.
444// To prevent sophisticated treatment the Command "bopaddpcs:
445// adds P-Curves for the edges .
446//=======================================================================
43cb0011 447Standard_Integer bopaddpcs(Draw_Interpretor& di,
448 Standard_Integer n,
449 const char** a)
7fd59977 450{
451 if (n<2) {
452 di << " Use >bopaddpcs Shape\n";
453 return 1;
454 }
455
456 TopoDS_Shape aS = DBRep::Get(a[1]);
457
458 if (aS.IsNull()) {
459 di << " Null shape is not allowed \n";
460 return 1;
461 }
462 //
463 PreparePCurves(aS, di);
464 //
465 DBRep::Set (a[1], aS);
466 return 0;
467}
7fd59977 468//=======================================================================
469//function : PreparePCurves
470//purpose :
471//=======================================================================
472void PreparePCurves(const TopoDS_Shape& aShape, Draw_Interpretor& di)
473{
474 Standard_Integer i, aNbE;
475 Standard_Real aTolE, aT1, aT2;
476 TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
477 TopLoc_Location aLoc;
478 Handle(Geom_Curve) aC3D;
479 Handle(Geom2d_Curve) aC2D;
480 BRep_Builder aBB;
481 //
482 TopExp::MapShapesAndAncestors (aShape, TopAbs_EDGE, TopAbs_FACE, aEFMap);
483 //
484 aNbE=aEFMap.Extent();
485 for (i=1; i<=aNbE; ++i) {
486 const TopoDS_Edge& aE=TopoDS::Edge(aEFMap.FindKey(i));
487 //
488 if (BRep_Tool::Degenerated(aE)) {
489 continue;
490 }
491 //
492 aC3D=BRep_Tool::Curve(aE, aT1, aT2);
493 if (aC3D.IsNull()) {
494 continue;
495 }
496 aTolE=BRep_Tool::Tolerance(aE);
497 //
498 const TopTools_ListOfShape& aLF=aEFMap(i);
499 TopTools_ListIteratorOfListOfShape aFIt(aLF);
500 for (; aFIt.More(); aFIt.Next()) {
501 const TopoDS_Face& aF=TopoDS::Face(aFIt.Value());
502 //
503 // Map of surfaces on which the edge lays .
504 TColStd_IndexedMapOfTransient aSCRMap;
505 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&aE.TShape());
506 const BRep_ListOfCurveRepresentation& aLCR=TE->Curves();
507 BRep_ListIteratorOfListOfCurveRepresentation itcr;
508 itcr.Initialize(aLCR);
509 for (; itcr.More(); itcr.Next()) {
43cb0011 510 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
511 //
512 if (cr->IsCurveOnSurface()) {
513 const Handle(Geom_Surface)& aSCR=cr->Surface();
514 aSCRMap.Add(aSCR);
515 }
516 //
7fd59977 517 }
518 //
519 const Handle(Geom_Surface)& aS=BRep_Tool::Surface(aF, aLoc);
520 if (!aSCRMap.Contains(aS)) {
43cb0011 521 // try to obtain 2D curve
522 aC2D=BRep_Tool::CurveOnSurface(aE, aS, aLoc, aT1, aT2);
523 if (aC2D.IsNull()) {
524 di << " Warning: Can not obtain P-Curve\n";
525 continue;
526 }
527 else {
528 aBB.UpdateEdge(aE, aC2D, aF, aTolE);
529 }
7fd59977 530 }
531 }
532 }
533}