1 // Created on: 2000-03-16
2 // Copyright (c) 2000-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <BOPTest.ixx>
17 #include <TCollection_AsciiString.hxx>
18 #include <TColStd_IndexedMapOfTransient.hxx>
19 #include <TopoDS_Shape.hxx>
20 #include <TopoDS_Vertex.hxx>
23 #include <gp_Pnt2d.hxx>
25 #include <Geom_Curve.hxx>
26 #include <Geom2d_Curve.hxx>
27 #include <Geom_Surface.hxx>
30 #include <TopoDS_Vertex.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <TopoDS_Face.hxx>
34 #include <TopAbs_Orientation.hxx>
36 #include <TopTools_ListIteratorOfListOfShape.hxx>
37 #include <TopTools_ListOfShape.hxx>
38 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
39 #include <TopTools_MapOfShape.hxx>
40 #include <TopTools_IndexedMapOfShape.hxx>
42 #include <BRep_TVertex.hxx>
43 #include <BRep_TEdge.hxx>
44 #include <BRep_ListOfCurveRepresentation.hxx>
45 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
46 #include <BRep_CurveRepresentation.hxx>
47 #include <BRep_Tool.hxx>
48 #include <BRep_Builder.hxx>
49 #include <BRep_TFace.hxx>
51 #include <TopLoc_Location.hxx>
53 #include <BRepLib.hxx>
56 #include <TopExp_Explorer.hxx>
61 void ProcessVertex(const TopoDS_Vertex&,
62 const TopTools_ListOfShape&,
63 const TopTools_ListOfShape&);
65 void ProcessEdge(const TopoDS_Edge&, const Standard_Real);
68 void ReduceVertexTolerance (const TopoDS_Shape&);
71 void ReduceFaceTolerance (const TopoDS_Shape&);
74 void ReduceEdgeTolerance (const TopoDS_Shape&,
78 void PreparePCurves(const TopoDS_Shape& ,
79 Draw_Interpretor& di);
81 static Standard_Integer breducetolerance (Draw_Interpretor&, Standard_Integer, const char** );
82 static Standard_Integer btolx (Draw_Interpretor&, Standard_Integer, const char** );
83 static Standard_Integer bopaddpcs (Draw_Interpretor&, Standard_Integer, const char** );
84 //=======================================================================
85 //function : TolerCommands
87 //=======================================================================
88 void BOPTest::TolerCommands(Draw_Interpretor& theCommands)
90 static Standard_Boolean done = Standard_False;
96 const char* g = "BOPTest commands";
98 theCommands.Add("breducetolerance" , "use breducetolerance Shape",
99 __FILE__, breducetolerance, g);
100 theCommands.Add("btolx" , "use btolx Shape [minTol=1.e-7]",
102 theCommands.Add("bopaddpcs" , "Use >bopaddpcs Shape",
103 __FILE__, bopaddpcs, g);
105 //=======================================================================
108 //=======================================================================
109 Standard_Integer btolx(Draw_Interpretor& di,
114 di << " use btolx Shape [minTol=1.e-7]\n";
118 TopoDS_Shape aS = DBRep::Get(a[1]);
121 di << " Null shape is not allowed\n";
125 Standard_Real aTolEMin=1.e-7;
127 aTolEMin=Draw::Atof(a[2]);
131 ReduceEdgeTolerance(aS, aTolEMin);
134 ReduceFaceTolerance(aS);
137 ReduceVertexTolerance(aS);
139 BRepLib::SameParameter(aS, 1.e-7, Standard_True);
141 DBRep::Set (a[1], aS);
144 //=======================================================================
145 //function : ReduceEdgeTolerance
147 //=======================================================================
148 void ReduceEdgeTolerance (const TopoDS_Shape& aS,
149 const Standard_Real aTolTreshold)
151 Standard_Integer i, aNbE;
152 TopTools_IndexedMapOfShape aEMap;
154 TopExp::MapShapes(aS, TopAbs_EDGE, aEMap);
157 for (i=1; i<=aNbE; i++) {
158 const TopoDS_Edge& aE= TopoDS::Edge(aEMap(i));
160 ProcessEdge(aE, aTolTreshold);
163 //=======================================================================
164 //function : ReduceFaceTolerance
166 //=======================================================================
167 void ReduceFaceTolerance (const TopoDS_Shape& aS)
169 Standard_Integer i, j, aNbF, aNbE;
170 Standard_Real aTolE, aTolx, aTolEMin;
171 TopTools_IndexedMapOfShape aFMap, aEMap;
175 TopExp::MapShapes(aS, TopAbs_FACE, aFMap);
177 for (i=1; i<=aNbF; i++) {
179 const TopoDS_Face& aF= TopoDS::Face(aFMap(i));
180 Handle(BRep_TFace)& aTF = *((Handle(BRep_TFace)*)&aF.TShape());
182 TopExp::MapShapes(aF, TopAbs_EDGE, aEMap);
184 for (j=1; j<=aNbE; ++j) {
185 const TopoDS_Edge& aE= TopoDS::Edge(aEMap(j));
186 aTolE =BRep_Tool::Tolerance(aE);
191 aTolE=(aTolx>aTolEMin) ? aTolx : aTolEMin;
192 aTF->Tolerance(aTolE);
195 //=======================================================================
196 //function : ReduceVertexTolerance
198 //=======================================================================
199 void ReduceVertexTolerance (const TopoDS_Shape& aS)
201 Standard_Integer i, aNbV;
202 TopTools_IndexedDataMapOfShapeListOfShape aVEMap, aVFMap;
204 TopExp::MapShapesAndAncestors(aS, TopAbs_VERTEX, TopAbs_EDGE, aVEMap);
205 TopExp::MapShapesAndAncestors(aS, TopAbs_VERTEX, TopAbs_FACE, aVFMap);
207 aNbV=aVEMap.Extent();
208 for (i=1; i<=aNbV; i++) {
209 const TopoDS_Vertex& aV= TopoDS::Vertex(aVEMap.FindKey(i));
210 const TopTools_ListOfShape& aLE=aVEMap(i);
211 const TopTools_ListOfShape& aLF=aVFMap.FindFromKey(aV);
213 ProcessVertex(aV, aLE, aLF);
216 //=======================================================================
217 //function : ProcessEdge
219 //=======================================================================
220 void ProcessEdge(const TopoDS_Edge& aE, const Standard_Real aTolTreshold)
222 Standard_Integer i, aNb=23;
223 Standard_Real aD2, aTolMax2, aT1, aT2, aT, dT;
227 //TopTools_ListIteratorOfListOfShape anIt;// Wng in Gcc 3.0
228 BRep_ListIteratorOfListOfCurveRepresentation itcr;
230 Handle(Geom_Curve) aC3D=BRep_Tool::Curve(aE, aT1, aT2);
237 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&aE.TShape());
238 const TopLoc_Location& Eloc = aE.Location();
241 const BRep_ListOfCurveRepresentation& aLCR=TE->Curves();
243 itcr.Initialize(aLCR);
244 for (; itcr.More(); itcr.Next()) {
245 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
246 const TopLoc_Location& loc = cr->Location();
247 TopLoc_Location L = (Eloc * loc);//.Predivided(aV.Location());
250 if (cr->IsCurve3D()) {
255 else if (cr->IsCurveOnSurface()) {
256 const Handle(Geom2d_Curve)& aC2D = cr->PCurve();
261 const Handle(Geom_Surface)& aS=cr->Surface();
263 // 2D-point treatment
264 for (i=0; i<=aNb; ++i) {
269 aPC3D=aC3D->Value(aT);
270 aPC2D=aC2D->Value(aT);
271 aS->D0(aPC2D.X(), aPC2D.Y(), aP3D);
272 aP3D.Transform(L.Transformation());
273 aD2=aPC3D.SquareDistance(aP3D);
274 if (aD2 > aTolMax2) {
278 } //if (cr->IsCurveOnSurface())
279 }//for (; itcr.More(); itcr.Next())
281 //#########################################################
288 aTolMax2=sqrt(aTolMax2);
290 //printf(" aTolMax=%15.10lf, aTolWas=%15.10lf\n", aTolMax2, aTolE);
292 Standard_Real aTolSet;
293 aTolSet=(aTolMax2>aTolTreshold) ? aTolMax2 : aTolTreshold;
295 TE->Tolerance(aTolSet);
297 //=======================================================================
298 //function : ProcessVertex
300 //=======================================================================
301 void ProcessVertex(const TopoDS_Vertex& aV,
302 const TopTools_ListOfShape& aLE,
303 const TopTools_ListOfShape& aLF)
305 Standard_Real aTol, aD2, aTolMax2, aTolE, aParam;
308 TopAbs_Orientation anOrV;
310 TopTools_ListIteratorOfListOfShape anIt;
311 TopTools_MapOfShape aProcessedEdges;
312 TopExp_Explorer aVExp;
314 BRep_ListIteratorOfListOfCurveRepresentation itcr;
318 Handle(BRep_TVertex)& TV = *((Handle(BRep_TVertex)*) &aV.TShape());
319 const gp_Pnt& aPV3D = TV->Pnt();
320 aTol =BRep_Tool::Tolerance(aV);
322 anIt.Initialize(aLE);
323 for (; anIt.More(); anIt.Next()) {
324 const TopoDS_Edge& aE=TopoDS::Edge(anIt.Value());
326 if (aProcessedEdges.Contains(aE)) {
329 aProcessedEdges.Add(aE);
331 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&aE.TShape());
332 const TopLoc_Location& Eloc = aE.Location();
334 aVExp.Init(aE, TopAbs_VERTEX);
335 for (; aVExp.More(); aVExp.Next()) {
336 const TopoDS_Vertex& aVx=TopoDS::Vertex(aVExp.Current());
338 if (!aVx.IsSame(aV)) {
342 anOrV=aVx.Orientation();
343 if (!(anOrV==TopAbs_FORWARD || anOrV==TopAbs_REVERSED)) {
347 const BRep_ListOfCurveRepresentation& aLCR=TE->Curves();
348 itcr.Initialize(aLCR);
349 for (; itcr.More(); itcr.Next()) {
350 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
351 const TopLoc_Location& loc = cr->Location();
352 TopLoc_Location L = (Eloc * loc).Predivided(aV.Location());
355 if (cr->IsCurve3D()) {
356 const Handle(Geom_Curve)& aC3D = cr->Curve3D();
361 // 3D-point treatment
362 aParam=BRep_Tool::Parameter(aVx, aE);
363 aPC3D= aC3D->Value(aParam);
364 aPC3D.Transform(L.Transformation());
365 aD2=aPV3D.SquareDistance(aPC3D);
366 if (aD2 > aTolMax2) {
370 }//if (cr->IsCurve3D())
373 else if (cr->IsCurveOnSurface()) {
374 const Handle(Geom2d_Curve)& aC2D = cr->PCurve();
379 const Handle(Geom_Surface)& aS=cr->Surface();
381 // 2D-point treatment
382 aParam=BRep_Tool::Parameter(aVx, aE, aS, L);
383 aPC2D=aC2D->Value(aParam);
384 aS->D0(aPC2D.X(), aPC2D.Y(), aPC3D);
385 aPC3D.Transform(L.Transformation());
386 aD2=aPV3D.SquareDistance(aPC3D);
387 if (aD2 > aTolMax2) {
390 } //if (cr->IsCurveOnSurface())
392 }//for (; itcr.More(); itcr.Next())
393 }//for (; aVExp.More(); aVExp.Next())
394 }//for (; anIt.More(); anIt.Next())
395 //#########################################################
402 aTolMax2=sqrt(aTolMax2);
407 aProcessedEdges.Clear();
408 anIt.Initialize(aLE);
409 for (; anIt.More(); anIt.Next()) {
410 const TopoDS_Edge& aE=TopoDS::Edge(anIt.Value());
412 if (aProcessedEdges.Contains(aE)) {
415 aProcessedEdges.Add(aE);
417 aTolE =BRep_Tool::Tolerance(aE);
418 if (aTolMax2 < aTolE) {
423 aProcessedEdges.Clear();
424 anIt.Initialize(aLF);
425 for (; anIt.More(); anIt.Next()) {
426 const TopoDS_Face& aF=TopoDS::Face(anIt.Value());
428 if (aProcessedEdges.Contains(aF)) {
431 aProcessedEdges.Add(aF);
433 aTolE =BRep_Tool::Tolerance(aF);
434 if (aTolMax2 < aTolE) {
444 TV->Tolerance(aTolMax2);
446 //=======================================================================
447 //function : breducetolerance
449 //=======================================================================
450 Standard_Integer breducetolerance(Draw_Interpretor& di,
455 di << " use bupdatetolerance Shape\n";
459 TopoDS_Shape aS = DBRep::Get(a[1]);
462 di << " Null shape is not allowed \n";
465 ReduceVertexTolerance(aS);
466 DBRep::Set (a[1], aS);
471 //=======================================================================
472 //function : bopaddpcs
473 //purpose : Some Edges do not contain P-Curves on Faces to which
475 // These faces usually based on Geom_Plane surface.
476 // To prevent sophisticated treatment the Command "bopaddpcs:
477 // adds P-Curves for the edges .
478 //=======================================================================
479 Standard_Integer bopaddpcs(Draw_Interpretor& di,
484 di << " Use >bopaddpcs Shape\n";
488 TopoDS_Shape aS = DBRep::Get(a[1]);
491 di << " Null shape is not allowed \n";
495 PreparePCurves(aS, di);
497 DBRep::Set (a[1], aS);
500 //=======================================================================
501 //function : PreparePCurves
503 //=======================================================================
504 void PreparePCurves(const TopoDS_Shape& aShape, Draw_Interpretor& di)
506 Standard_Integer i, aNbE;
507 Standard_Real aTolE, aT1, aT2;
508 TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
509 TopLoc_Location aLoc;
510 Handle(Geom_Curve) aC3D;
511 Handle(Geom2d_Curve) aC2D;
514 TopExp::MapShapesAndAncestors (aShape, TopAbs_EDGE, TopAbs_FACE, aEFMap);
516 aNbE=aEFMap.Extent();
517 for (i=1; i<=aNbE; ++i) {
518 const TopoDS_Edge& aE=TopoDS::Edge(aEFMap.FindKey(i));
520 if (BRep_Tool::Degenerated(aE)) {
524 aC3D=BRep_Tool::Curve(aE, aT1, aT2);
528 aTolE=BRep_Tool::Tolerance(aE);
530 const TopTools_ListOfShape& aLF=aEFMap(i);
531 TopTools_ListIteratorOfListOfShape aFIt(aLF);
532 for (; aFIt.More(); aFIt.Next()) {
533 const TopoDS_Face& aF=TopoDS::Face(aFIt.Value());
535 // Map of surfaces on which the edge lays .
536 TColStd_IndexedMapOfTransient aSCRMap;
537 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&aE.TShape());
538 const BRep_ListOfCurveRepresentation& aLCR=TE->Curves();
539 BRep_ListIteratorOfListOfCurveRepresentation itcr;
540 itcr.Initialize(aLCR);
541 for (; itcr.More(); itcr.Next()) {
542 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
544 if (cr->IsCurveOnSurface()) {
545 const Handle(Geom_Surface)& aSCR=cr->Surface();
551 const Handle(Geom_Surface)& aS=BRep_Tool::Surface(aF, aLoc);
552 if (!aSCRMap.Contains(aS)) {
553 // try to obtain 2D curve
554 aC2D=BRep_Tool::CurveOnSurface(aE, aS, aLoc, aT1, aT2);
556 di << " Warning: Can not obtain P-Curve\n";
560 aBB.UpdateEdge(aE, aC2D, aF, aTolE);