1 // Created on: 2000-03-16
2 // Created by: Peter KURNEV
3 // Copyright (c) 2000-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
22 #include <BOPTest.ixx>
24 #include <TCollection_AsciiString.hxx>
25 #include <TColStd_IndexedMapOfTransient.hxx>
26 #include <TopoDS_Shape.hxx>
27 #include <TopoDS_Vertex.hxx>
29 #include <gp_Pnt2d.hxx>
31 #include <Geom_Curve.hxx>
32 #include <Geom2d_Curve.hxx>
33 #include <Geom_Surface.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Face.hxx>
40 #include <TopAbs_Orientation.hxx>
42 #include <TopTools_ListIteratorOfListOfShape.hxx>
43 #include <TopTools_ListOfShape.hxx>
44 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
45 #include <TopTools_MapOfShape.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
48 #include <BRep_TVertex.hxx>
49 #include <BRep_TEdge.hxx>
50 #include <BRep_ListOfCurveRepresentation.hxx>
51 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
52 #include <BRep_CurveRepresentation.hxx>
53 #include <BRep_Tool.hxx>
54 #include <BRep_Builder.hxx>
55 #include <BRep_TFace.hxx>
57 #include <TopLoc_Location.hxx>
59 #include <BRepLib.hxx>
62 #include <TopExp_Explorer.hxx>
65 Standard_Integer breducetolerance (Draw_Interpretor&, Standard_Integer, const char** );
67 Standard_Integer btolx (Draw_Interpretor&, Standard_Integer, const char** );
69 Standard_Integer bopaddpcs (Draw_Interpretor&, Standard_Integer, const char** );
72 void ProcessVertex(const TopoDS_Vertex&,
73 const TopTools_ListOfShape&,
74 const TopTools_ListOfShape&);
76 void ProcessEdge(const TopoDS_Edge&, const Standard_Real);
79 void ReduceVertexTolerance (const TopoDS_Shape&);
82 void ReduceFaceTolerance (const TopoDS_Shape&);
85 void ReduceEdgeTolerance (const TopoDS_Shape&, const Standard_Real);
88 //=======================================================================
89 //function : TolerCommands
91 //=======================================================================
92 void BOPTest::TolerCommands(Draw_Interpretor& theCommands)
94 static Standard_Boolean done = Standard_False;
100 const char* g = "CCR commands";
102 theCommands.Add("breducetolerance" , "Use >breducetolerance Shape [maxTol=0.01]", __FILE__, breducetolerance, g);
103 theCommands.Add("btolx" , "Use >btolx Shape [minTol=1.e-7]", __FILE__, btolx, g);
104 theCommands.Add("bopaddpcs" , "Use >bopaddpcs Shape" , __FILE__, bopaddpcs, g);
107 //=======================================================================
110 //=======================================================================
111 Standard_Integer btolx(Draw_Interpretor& di, Standard_Integer n, const char** a)
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;
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, const Standard_Real aTolTreshold)
150 Standard_Integer i, aNbE;
151 TopTools_IndexedMapOfShape aEMap;
153 TopExp::MapShapes(aS, TopAbs_EDGE, aEMap);
156 for (i=1; i<=aNbE; i++) {
157 const TopoDS_Edge& aE= TopoDS::Edge(aEMap(i));
159 ProcessEdge(aE, aTolTreshold);
162 //=======================================================================
163 //function : ReduceFaceTolerance
165 //=======================================================================
166 void ReduceFaceTolerance (const TopoDS_Shape& aS)
168 Standard_Integer i, j, aNbF, aNbE;
169 Standard_Real aTolE, aTolx, aTolEMin;
170 TopTools_IndexedMapOfShape aFMap, aEMap;
174 TopExp::MapShapes(aS, TopAbs_FACE, aFMap);
176 for (i=1; i<=aNbF; i++) {
178 const TopoDS_Face& aF= TopoDS::Face(aFMap(i));
179 Handle(BRep_TFace)& aTF = *((Handle(BRep_TFace)*)&aF.TShape());
181 TopExp::MapShapes(aF, TopAbs_EDGE, aEMap);
183 for (j=1; j<=aNbE; ++j) {
184 const TopoDS_Edge& aE= TopoDS::Edge(aEMap(j));
185 aTolE =BRep_Tool::Tolerance(aE);
190 aTolE=(aTolx>aTolEMin) ? aTolx : aTolEMin;
191 aTF->Tolerance(aTolE);
194 //=======================================================================
195 //function : ReduceVertexTolerance
197 //=======================================================================
198 void ReduceVertexTolerance (const TopoDS_Shape& aS)
200 Standard_Integer i, aNbV;
201 TopTools_IndexedDataMapOfShapeListOfShape aVEMap, aVFMap;
203 TopExp::MapShapesAndAncestors(aS, TopAbs_VERTEX, TopAbs_EDGE, aVEMap);
204 TopExp::MapShapesAndAncestors(aS, TopAbs_VERTEX, TopAbs_FACE, aVFMap);
206 aNbV=aVEMap.Extent();
207 for (i=1; i<=aNbV; i++) {
208 const TopoDS_Vertex& aV= TopoDS::Vertex(aVEMap.FindKey(i));
209 const TopTools_ListOfShape& aLE=aVEMap(i);
210 const TopTools_ListOfShape& aLF=aVFMap.FindFromKey(aV);
212 ProcessVertex(aV, aLE, aLF);
215 //=======================================================================
216 //function : ProcessEdge
218 //=======================================================================
219 void ProcessEdge(const TopoDS_Edge& aE, const Standard_Real aTolTreshold)
221 Standard_Integer i, aNb=23;
222 Standard_Real aTolE, aD2, aTolMax2, aT1, aT2, aT, dT;
226 //TopTools_ListIteratorOfListOfShape anIt;// Wng in Gcc 3.0
227 BRep_ListIteratorOfListOfCurveRepresentation itcr;
229 Handle(Geom_Curve) aC3D=BRep_Tool::Curve(aE, aT1, aT2);
236 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&aE.TShape());
237 const TopLoc_Location& Eloc = aE.Location();
240 const BRep_ListOfCurveRepresentation& aLCR=TE->Curves();
242 itcr.Initialize(aLCR);
243 for (; itcr.More(); itcr.Next()) {
244 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
245 const TopLoc_Location& loc = cr->Location();
246 TopLoc_Location L = (Eloc * loc);//.Predivided(aV.Location());
249 if (cr->IsCurve3D()) {
254 else if (cr->IsCurveOnSurface()) {
255 const Handle(Geom2d_Curve)& aC2D = cr->PCurve();
260 const Handle(Geom_Surface)& aS=cr->Surface();
262 // 2D-point treatment
263 for (i=0; i<=aNb; ++i) {
268 aPC3D=aC3D->Value(aT);
269 aPC2D=aC2D->Value(aT);
270 aS->D0(aPC2D.X(), aPC2D.Y(), aP3D);
271 aP3D.Transform(L.Transformation());
272 aD2=aPC3D.SquareDistance(aP3D);
273 if (aD2 > aTolMax2) {
277 } //if (cr->IsCurveOnSurface())
278 }//for (; itcr.More(); itcr.Next())
280 //#########################################################
286 aTolE =BRep_Tool::Tolerance(aE);
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, aTol2, 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);
323 anIt.Initialize(aLE);
324 for (; anIt.More(); anIt.Next()) {
325 const TopoDS_Edge& aE=TopoDS::Edge(anIt.Value());
327 if (aProcessedEdges.Contains(aE)) {
330 aProcessedEdges.Add(aE);
332 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&aE.TShape());
333 const TopLoc_Location& Eloc = aE.Location();
335 aVExp.Init(aE, TopAbs_VERTEX);
336 for (; aVExp.More(); aVExp.Next()) {
337 const TopoDS_Vertex& aVx=TopoDS::Vertex(aVExp.Current());
339 if (!aVx.IsSame(aV)) {
343 anOrV=aVx.Orientation();
344 if (!(anOrV==TopAbs_FORWARD || anOrV==TopAbs_REVERSED)) {
348 const BRep_ListOfCurveRepresentation& aLCR=TE->Curves();
349 itcr.Initialize(aLCR);
350 for (; itcr.More(); itcr.Next()) {
351 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
352 const TopLoc_Location& loc = cr->Location();
353 TopLoc_Location L = (Eloc * loc).Predivided(aV.Location());
356 if (cr->IsCurve3D()) {
357 const Handle(Geom_Curve)& aC3D = cr->Curve3D();
362 // 3D-point treatment
363 aParam=BRep_Tool::Parameter(aVx, aE);
364 aPC3D= aC3D->Value(aParam);
365 aPC3D.Transform(L.Transformation());
366 aD2=aPV3D.SquareDistance(aPC3D);
367 if (aD2 > aTolMax2) {
371 }//if (cr->IsCurve3D())
374 else if (cr->IsCurveOnSurface()) {
375 const Handle(Geom2d_Curve)& aC2D = cr->PCurve();
380 const Handle(Geom_Surface)& aS=cr->Surface();
382 // 2D-point treatment
383 aParam=BRep_Tool::Parameter(aVx, aE, aS, L);
384 aPC2D=aC2D->Value(aParam);
385 aS->D0(aPC2D.X(), aPC2D.Y(), aPC3D);
386 aPC3D.Transform(L.Transformation());
387 aD2=aPV3D.SquareDistance(aPC3D);
388 if (aD2 > aTolMax2) {
391 } //if (cr->IsCurveOnSurface())
393 }//for (; itcr.More(); itcr.Next())
394 }//for (; aVExp.More(); aVExp.Next())
395 }//for (; anIt.More(); anIt.Next())
396 //#########################################################
403 aTolMax2=sqrt(aTolMax2);
408 aProcessedEdges.Clear();
409 anIt.Initialize(aLE);
410 for (; anIt.More(); anIt.Next()) {
411 const TopoDS_Edge& aE=TopoDS::Edge(anIt.Value());
413 if (aProcessedEdges.Contains(aE)) {
416 aProcessedEdges.Add(aE);
418 aTolE =BRep_Tool::Tolerance(aE);
419 if (aTolMax2 < aTolE) {
424 aProcessedEdges.Clear();
425 anIt.Initialize(aLF);
426 for (; anIt.More(); anIt.Next()) {
427 const TopoDS_Face& aF=TopoDS::Face(anIt.Value());
429 if (aProcessedEdges.Contains(aF)) {
432 aProcessedEdges.Add(aF);
434 aTolE =BRep_Tool::Tolerance(aF);
435 if (aTolMax2 < aTolE) {
445 TV->Tolerance(aTolMax2);
447 //=======================================================================
448 //function : breducetolerance
450 //=======================================================================
451 Standard_Integer breducetolerance(Draw_Interpretor& di,
456 di << " Use >bupdatetolerance Shape\n";
460 TopoDS_Shape aS = DBRep::Get(a[1]);
463 di << " Null shape is not allowed \n";
466 ReduceVertexTolerance(aS);
467 DBRep::Set (a[1], aS);
474 void PreparePCurves(const TopoDS_Shape& aShape, Draw_Interpretor& di);
476 //=======================================================================
477 //function : bopaddpcs
478 //purpose : Some Edgesdo not contain P-Curveson Faces to which they belong to.
479 // These faces usually based on Geom_Plane surface.
480 // To prevent sophisticated treatment the Command "bopaddpcs:
481 // adds P-Curves for the edges .
482 //=======================================================================
483 Standard_Integer bopaddpcs(Draw_Interpretor& di, Standard_Integer n, const char** a)
486 di << " Use >bopaddpcs Shape\n";
490 TopoDS_Shape aS = DBRep::Get(a[1]);
493 di << " Null shape is not allowed \n";
497 PreparePCurves(aS, di);
499 DBRep::Set (a[1], aS);
503 //=======================================================================
504 //function : PreparePCurves
506 //=======================================================================
507 void PreparePCurves(const TopoDS_Shape& aShape, Draw_Interpretor& di)
509 Standard_Integer i, aNbE;
510 Standard_Real aTolE, aT1, aT2;
511 TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
512 TopLoc_Location aLoc;
513 Handle(Geom_Curve) aC3D;
514 Handle(Geom2d_Curve) aC2D;
517 TopExp::MapShapesAndAncestors (aShape, TopAbs_EDGE, TopAbs_FACE, aEFMap);
519 aNbE=aEFMap.Extent();
520 for (i=1; i<=aNbE; ++i) {
521 const TopoDS_Edge& aE=TopoDS::Edge(aEFMap.FindKey(i));
523 if (BRep_Tool::Degenerated(aE)) {
527 aC3D=BRep_Tool::Curve(aE, aT1, aT2);
531 aTolE=BRep_Tool::Tolerance(aE);
533 const TopTools_ListOfShape& aLF=aEFMap(i);
534 TopTools_ListIteratorOfListOfShape aFIt(aLF);
535 for (; aFIt.More(); aFIt.Next()) {
536 const TopoDS_Face& aF=TopoDS::Face(aFIt.Value());
538 // Map of surfaces on which the edge lays .
539 TColStd_IndexedMapOfTransient aSCRMap;
540 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&aE.TShape());
541 const BRep_ListOfCurveRepresentation& aLCR=TE->Curves();
542 BRep_ListIteratorOfListOfCurveRepresentation itcr;
543 itcr.Initialize(aLCR);
544 for (; itcr.More(); itcr.Next()) {
545 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
547 if (cr->IsCurveOnSurface()) {
548 const Handle(Geom_Surface)& aSCR=cr->Surface();
554 const Handle(Geom_Surface)& aS=BRep_Tool::Surface(aF, aLoc);
555 if (!aSCRMap.Contains(aS)) {
556 // try to obtain 2D curve
557 aC2D=BRep_Tool::CurveOnSurface(aE, aS, aLoc, aT1, aT2);
559 di << " Warning: Can not obtain P-Curve\n";
563 aBB.UpdateEdge(aE, aC2D, aF, aTolE);