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