0026071: BRepOffsetAPI_MakePipeShell produces rough result
[occt.git] / src / BRepTools / BRepTools.cxx
CommitLineData
b311480e 1// Created on: 1993-01-21
2// Created by: Remi LEQUETTE
3// Copyright (c) 1993-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
7fd59977 17
42cf5bc1 18#include <Bnd_Box2d.hxx>
7fd59977 19#include <BndLib_Add2dCurve.hxx>
42cf5bc1 20#include <BRep_Builder.hxx>
7fd59977 21#include <BRep_CurveRepresentation.hxx>
e50ebf1f 22#include <BRep_GCurve.hxx>
23#include <BRep_ListOfCurveRepresentation.hxx>
7fd59977 24#include <BRep_TEdge.hxx>
42cf5bc1 25#include <BRep_Tool.hxx>
26#include <BRepTools.hxx>
27#include <BRepTools_MapOfVertexPnt2d.hxx>
28#include <BRepTools_ShapeSet.hxx>
11af6cdd 29#include <BRepAdaptor_Surface.hxx>
7fd59977 30#include <ElCLib.hxx>
42cf5bc1 31#include <Geom2d_Curve.hxx>
32#include <Geom2dAdaptor_Curve.hxx>
33#include <Geom_BSplineSurface.hxx>
34#include <Geom_Curve.hxx>
35#include <Geom_RectangularTrimmedSurface.hxx>
36#include <Geom_Surface.hxx>
37#include <gp_Lin2d.hxx>
7fd59977 38#include <gp_Vec2d.hxx>
42cf5bc1 39#include <Message_ProgressIndicator.hxx>
40#include <OSD_OpenFile.hxx>
41#include <Poly_PolygonOnTriangulation.hxx>
42#include <Poly_Triangulation.hxx>
43#include <Precision.hxx>
7fd59977 44#include <Standard_ErrorHandler.hxx>
45#include <Standard_Failure.hxx>
42cf5bc1 46#include <Standard_Stream.hxx>
47#include <TColGeom2d_SequenceOfCurve.hxx>
48#include <TColgp_SequenceOfPnt2d.hxx>
49#include <TColStd_Array1OfReal.hxx>
50#include <TColStd_HArray1OfInteger.hxx>
51#include <TColStd_MapOfTransient.hxx>
52#include <TColStd_SequenceOfReal.hxx>
53#include <TopExp.hxx>
54#include <TopExp_Explorer.hxx>
55#include <TopoDS.hxx>
56#include <TopoDS_Compound.hxx>
57#include <TopoDS_CompSolid.hxx>
58#include <TopoDS_Edge.hxx>
59#include <TopoDS_Face.hxx>
42cf5bc1 60#include <TopoDS_Shape.hxx>
61#include <TopoDS_Shell.hxx>
62#include <TopoDS_Solid.hxx>
63#include <TopoDS_Vertex.hxx>
64#include <TopoDS_Wire.hxx>
65#include <TopTools_SequenceOfShape.hxx>
2651bfde 66#include <GeomLib_CheckCurveOnSurface.hxx>
7fd59977 67#include <errno.h>
11af6cdd 68
69
70//=======================================================================
7fd59977 71//function : UVBounds
72//purpose :
73//=======================================================================
7fd59977 74void BRepTools::UVBounds(const TopoDS_Face& F,
75 Standard_Real& UMin, Standard_Real& UMax,
76 Standard_Real& VMin, Standard_Real& VMax)
77{
78 Bnd_Box2d B;
79 AddUVBounds(F,B);
413b1c1a 80 if (!B.IsVoid())
81 {
82 B.Get(UMin,VMin,UMax,VMax);
83 }
84 else
85 {
86 UMin = UMax = VMin = VMax = 0.0;
87 }
7fd59977 88}
89
90//=======================================================================
91//function : UVBounds
92//purpose :
93//=======================================================================
94
95void BRepTools::UVBounds(const TopoDS_Face& F,
96 const TopoDS_Wire& W,
97 Standard_Real& UMin, Standard_Real& UMax,
98 Standard_Real& VMin, Standard_Real& VMax)
99{
100 Bnd_Box2d B;
101 AddUVBounds(F,W,B);
413b1c1a 102 if (!B.IsVoid())
103 {
104 B.Get(UMin,VMin,UMax,VMax);
105 }
106 else
107 {
108 UMin = UMax = VMin = VMax = 0.0;
109 }
7fd59977 110}
111
112
113//=======================================================================
114//function : UVBounds
115//purpose :
116//=======================================================================
117
118void BRepTools::UVBounds(const TopoDS_Face& F,
119 const TopoDS_Edge& E,
120 Standard_Real& UMin, Standard_Real& UMax,
121 Standard_Real& VMin, Standard_Real& VMax)
122{
123 Bnd_Box2d B;
124 AddUVBounds(F,E,B);
413b1c1a 125 if (!B.IsVoid())
126 {
127 B.Get(UMin,VMin,UMax,VMax);
128 }
129 else
130 {
131 UMin = UMax = VMin = VMax = 0.0;
132 }
7fd59977 133}
134
135//=======================================================================
136//function : AddUVBounds
137//purpose :
138//=======================================================================
139
140void BRepTools::AddUVBounds(const TopoDS_Face& FF, Bnd_Box2d& B)
141{
142 TopoDS_Face F = FF;
143 F.Orientation(TopAbs_FORWARD);
144 TopExp_Explorer ex(F,TopAbs_EDGE);
145
146 // fill box for the given face
147 Bnd_Box2d aBox;
148 for (;ex.More();ex.Next()) {
149 BRepTools::AddUVBounds(F,TopoDS::Edge(ex.Current()),aBox);
150 }
151
152 // if the box is empty (face without edges or without pcurves),
153 // get natural bounds
154 if (aBox.IsVoid()) {
155 Standard_Real UMin,UMax,VMin,VMax;
156 TopLoc_Location L;
413b1c1a 157 const Handle(Geom_Surface)& aSurf = BRep_Tool::Surface(F, L);
158 if (aSurf.IsNull())
159 {
160 return;
161 }
162
163 aSurf->Bounds(UMin,UMax,VMin,VMax);
7fd59977 164 aBox.Update(UMin,VMin,UMax,VMax);
165 }
166
167 // add face box to result
168 B.Add ( aBox );
169}
170
171
172//=======================================================================
173//function : AddUVBounds
174//purpose :
175//=======================================================================
7fd59977 176void BRepTools::AddUVBounds(const TopoDS_Face& F,
177 const TopoDS_Wire& W,
178 Bnd_Box2d& B)
179{
180 TopExp_Explorer ex;
181 for (ex.Init(W,TopAbs_EDGE);ex.More();ex.Next()) {
182 BRepTools::AddUVBounds(F,TopoDS::Edge(ex.Current()),B);
183 }
184}
185
186
187//=======================================================================
188//function : AddUVBounds
189//purpose :
190//=======================================================================
59495dbe 191void BRepTools::AddUVBounds(const TopoDS_Face& aF,
6219c44c 192 const TopoDS_Edge& aE,
193 Bnd_Box2d& aB)
7fd59977 194{
413b1c1a 195 Standard_Real aT1, aT2, aXmin = 0.0, aYmin = 0.0, aXmax = 0.0, aYmax = 0.0;
59495dbe 196 Standard_Real aUmin, aUmax, aVmin, aVmax;
197 Bnd_Box2d aBoxC, aBoxS;
198 TopLoc_Location aLoc;
199 //
200 const Handle(Geom2d_Curve) aC2D = BRep_Tool::CurveOnSurface(aE, aF, aT1, aT2);
201 if (aC2D.IsNull()) {
202 return;
203 }
204 //
205 BndLib_Add2dCurve::Add(aC2D, aT1, aT2, 0., aBoxC);
413b1c1a 206 if (!aBoxC.IsVoid())
207 {
208 aBoxC.Get(aXmin, aYmin, aXmax, aYmax);
209 }
59495dbe 210 //
211 Handle(Geom_Surface) aS = BRep_Tool::Surface(aF, aLoc);
212 aS->Bounds(aUmin, aUmax, aVmin, aVmax);
213
214 if(aS->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface))
215 {
216 const Handle(Geom_RectangularTrimmedSurface) aSt =
217 Handle(Geom_RectangularTrimmedSurface)::DownCast(aS);
218 aS = aSt->BasisSurface();
7fd59977 219 }
7fd59977 220
59495dbe 221 //
222 if(!aS->IsUPeriodic())
223 {
6219c44c 224 Standard_Boolean isUPeriodic = Standard_False;
225
226 // Additional verification for U-periodicity for B-spline surfaces
227 // 1. Verify that the surface is U-closed (if such flag is false). Verification uses 2 points
228 // 2. Verify periodicity of surface inside UV-bounds of the edge. Verification uses 3 or 6 points.
229 if (aS->DynamicType() == STANDARD_TYPE(Geom_BSplineSurface) &&
230 (aXmin < aUmin || aXmax > aUmax))
9c06009a 231 {
6219c44c 232 Standard_Real aTol2 = 100 * Precision::Confusion() * Precision::Confusion();
233 isUPeriodic = Standard_True;
234 gp_Pnt P1, P2;
235 // 1. Verify that the surface is U-closed
236 if (!aS->IsUClosed())
237 {
238 Standard_Real aVStep = aVmax - aVmin;
239 for (Standard_Real aV = aVmin; aV <= aVmax; aV += aVStep)
240 {
241 P1 = aS->Value(aUmin, aV);
242 P2 = aS->Value(aUmax, aV);
243 if (P1.SquareDistance(P2) > aTol2)
244 {
245 isUPeriodic = Standard_False;
246 break;
247 }
248 }
249 }
250 // 2. Verify periodicity of surface inside UV-bounds of the edge
251 if (isUPeriodic) // the flag still not changed
252 {
253 Standard_Real aV = (aVmin + aVmax) * 0.5;
254 Standard_Real aU[6]; // values of U lying out of surface boundaries
255 Standard_Real aUpp[6]; // corresponding U-values plus/minus period
256 Standard_Integer aNbPnt = 0;
257 if (aXmin < aUmin)
258 {
259 aU[0] = aXmin;
260 aU[1] = (aXmin + aUmin) * 0.5;
261 aU[2] = aUmin;
262 aUpp[0] = aU[0] + aUmax - aUmin;
263 aUpp[1] = aU[1] + aUmax - aUmin;
264 aUpp[2] = aU[2] + aUmax - aUmin;
265 aNbPnt += 3;
266 }
267 if (aXmax > aUmax)
268 {
269 aU[aNbPnt] = aUmax;
270 aU[aNbPnt + 1] = (aXmax + aUmax) * 0.5;
271 aU[aNbPnt + 2] = aXmax;
272 aUpp[aNbPnt] = aU[aNbPnt] - aUmax + aUmin;
273 aUpp[aNbPnt + 1] = aU[aNbPnt + 1] - aUmax + aUmin;
274 aUpp[aNbPnt + 2] = aU[aNbPnt + 2] - aUmax + aUmin;
275 aNbPnt += 3;
276 }
277 for (Standard_Integer anInd = 0; anInd < aNbPnt; anInd++)
278 {
279 P1 = aS->Value(aU[anInd], aV);
280 P2 = aS->Value(aUpp[anInd], aV);
281 if (P1.SquareDistance(P2) > aTol2)
282 {
283 isUPeriodic = Standard_False;
284 break;
285 }
286 }
287 }
9c06009a 288 }
6219c44c 289
290 if (!isUPeriodic)
59495dbe 291 {
6219c44c 292 if((aXmin<aUmin) && (aUmin < aXmax))
293 {
294 aXmin=aUmin;
295 }
296 if((aXmin < aUmax) && (aUmax < aXmax))
297 {
298 aXmax=aUmax;
299 }
7fd59977 300 }
59495dbe 301 }
7fd59977 302
59495dbe 303 if(!aS->IsVPeriodic())
304 {
6219c44c 305 Standard_Boolean isVPeriodic = Standard_False;
306
307 // Additional verification for V-periodicity for B-spline surfaces
308 // 1. Verify that the surface is V-closed (if such flag is false). Verification uses 2 points
309 // 2. Verify periodicity of surface inside UV-bounds of the edge. Verification uses 3 or 6 points.
310 if (aS->DynamicType() == STANDARD_TYPE(Geom_BSplineSurface) &&
311 (aYmin < aVmin || aYmax > aVmax))
59495dbe 312 {
6219c44c 313 Standard_Real aTol2 = 100 * Precision::Confusion() * Precision::Confusion();
314 isVPeriodic = Standard_True;
315 gp_Pnt P1, P2;
316 // 1. Verify that the surface is V-closed
317 if (!aS->IsVClosed())
318 {
319 Standard_Real aUStep = aUmax - aUmin;
320 for (Standard_Real aU = aUmin; aU <= aUmax; aU += aUStep)
321 {
322 P1 = aS->Value(aU, aVmin);
323 P2 = aS->Value(aU, aVmax);
324 if (P1.SquareDistance(P2) > aTol2)
325 {
326 isVPeriodic = Standard_False;
327 break;
328 }
329 }
330 }
331 // 2. Verify periodicity of surface inside UV-bounds of the edge
332 if (isVPeriodic) // the flag still not changed
333 {
334 Standard_Real aU = (aUmin + aUmax) * 0.5;
335 Standard_Real aV[6]; // values of V lying out of surface boundaries
336 Standard_Real aVpp[6]; // corresponding V-values plus/minus period
337 Standard_Integer aNbPnt = 0;
338 if (aYmin < aVmin)
339 {
340 aV[0] = aYmin;
341 aV[1] = (aYmin + aVmin) * 0.5;
342 aV[2] = aVmin;
343 aVpp[0] = aV[0] + aVmax - aVmin;
344 aVpp[1] = aV[1] + aVmax - aVmin;
345 aVpp[2] = aV[2] + aVmax - aVmin;
346 aNbPnt += 3;
347 }
348 if (aYmax > aVmax)
349 {
350 aV[aNbPnt] = aVmax;
351 aV[aNbPnt + 1] = (aYmax + aVmax) * 0.5;
352 aV[aNbPnt + 2] = aYmax;
353 aVpp[aNbPnt] = aV[aNbPnt] - aVmax + aVmin;
354 aVpp[aNbPnt + 1] = aV[aNbPnt + 1] - aVmax + aVmin;
355 aVpp[aNbPnt + 2] = aV[aNbPnt + 2] - aVmax + aVmin;
356 aNbPnt += 3;
357 }
358 for (Standard_Integer anInd = 0; anInd < aNbPnt; anInd++)
359 {
360 P1 = aS->Value(aU, aV[anInd]);
361 P2 = aS->Value(aU, aVpp[anInd]);
362 if (P1.SquareDistance(P2) > aTol2)
363 {
364 isVPeriodic = Standard_False;
365 break;
366 }
367 }
368 }
7fd59977 369 }
6219c44c 370
371 if (!isVPeriodic)
59495dbe 372 {
6219c44c 373 if((aYmin<aVmin) && (aVmin < aYmax))
374 {
375 aYmin=aVmin;
376 }
377 if((aYmin < aVmax) && (aVmax < aYmax))
378 {
379 aYmax=aVmax;
380 }
59495dbe 381 }
7fd59977 382 }
59495dbe 383
384 aBoxS.Update(aXmin, aYmin, aXmax, aYmax);
385
386 aB.Add(aBoxS);
7fd59977 387}
388
389//=======================================================================
390//function : Update
391//purpose :
392//=======================================================================
393
394void BRepTools::Update(const TopoDS_Vertex&)
395{
396}
397
398//=======================================================================
399//function : Update
400//purpose :
401//=======================================================================
402
403void BRepTools::Update(const TopoDS_Edge&)
404{
405}
406
407//=======================================================================
408//function : Update
409//purpose :
410//=======================================================================
411
412void BRepTools::Update(const TopoDS_Wire&)
413{
414}
415
416//=======================================================================
417//function : Update
418//purpose :
419//=======================================================================
420
421void BRepTools::Update(const TopoDS_Face& F)
422{
423 if (!F.Checked()) {
424 UpdateFaceUVPoints(F);
425 F.TShape()->Checked(Standard_True);
426 }
427}
428
429//=======================================================================
430//function : Update
431//purpose :
432//=======================================================================
433
434void BRepTools::Update(const TopoDS_Shell& S)
435{
436 TopExp_Explorer ex(S,TopAbs_FACE);
437 while (ex.More()) {
438 Update(TopoDS::Face(ex.Current()));
439 ex.Next();
440 }
441}
442
443//=======================================================================
444//function : Update
445//purpose :
446//=======================================================================
447
448void BRepTools::Update(const TopoDS_Solid& S)
449{
450 TopExp_Explorer ex(S,TopAbs_FACE);
451 while (ex.More()) {
452 Update(TopoDS::Face(ex.Current()));
453 ex.Next();
454 }
455}
456
457//=======================================================================
458//function : Update
459//purpose :
460//=======================================================================
461
462void BRepTools::Update(const TopoDS_CompSolid& CS)
463{
464 TopExp_Explorer ex(CS,TopAbs_FACE);
465 while (ex.More()) {
466 Update(TopoDS::Face(ex.Current()));
467 ex.Next();
468 }
469}
470
471//=======================================================================
472//function : Update
473//purpose :
474//=======================================================================
475
476void BRepTools::Update(const TopoDS_Compound& C)
477{
478 TopExp_Explorer ex(C,TopAbs_FACE);
479 while (ex.More()) {
480 Update(TopoDS::Face(ex.Current()));
481 ex.Next();
482 }
483}
484
485//=======================================================================
486//function : Update
487//purpose :
488//=======================================================================
489
490void BRepTools::Update(const TopoDS_Shape& S)
491{
492 switch (S.ShapeType()) {
493
494 case TopAbs_VERTEX :
495 Update(TopoDS::Vertex(S));
496 break;
497
498 case TopAbs_EDGE :
499 Update(TopoDS::Edge(S));
500 break;
501
502 case TopAbs_WIRE :
503 Update(TopoDS::Wire(S));
504 break;
505
506 case TopAbs_FACE :
507 Update(TopoDS::Face(S));
508 break;
509
510 case TopAbs_SHELL :
511 Update(TopoDS::Shell(S));
512 break;
513
514 case TopAbs_SOLID :
515 Update(TopoDS::Solid(S));
516 break;
517
518 case TopAbs_COMPSOLID :
519 Update(TopoDS::CompSolid(S));
520 break;
521
522 case TopAbs_COMPOUND :
523 Update(TopoDS::Compound(S));
524 break;
525
526 default:
527 break;
528
529 }
530}
531
7fd59977 532//=======================================================================
533//function : UpdateFaceUVPoints
e50ebf1f 534//purpose : Reset the UV points of edges on the Face
7fd59977 535//=======================================================================
e50ebf1f 536void BRepTools::UpdateFaceUVPoints(const TopoDS_Face& theF)
7fd59977 537{
e50ebf1f 538 // For each edge of the face <F> reset the UV points to the bounding
539 // points of the parametric curve of the edge on the face.
7fd59977 540
e50ebf1f 541 // Get surface of the face
542 TopLoc_Location aLoc;
543 const Handle(Geom_Surface)& aSurf = BRep_Tool::Surface(theF, aLoc);
544 // Iterate on edges and reset UV points
545 TopExp_Explorer anExpE(theF, TopAbs_EDGE);
546 for (; anExpE.More(); anExpE.Next())
7fd59977 547 {
e50ebf1f 548 const TopoDS_Edge& aE = TopoDS::Edge(anExpE.Current());
549
550 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&aE.TShape());
551 if (TE->Locked())
552 return;
553
554 const TopLoc_Location aELoc = aLoc.Predivided(aE.Location());
555 // Edge representations
556 BRep_ListOfCurveRepresentation& aLCR = TE->ChangeCurves();
557 BRep_ListIteratorOfListOfCurveRepresentation itLCR(aLCR);
558 for (; itLCR.More(); itLCR.Next())
559 {
560 Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itLCR.Value());
561 if (!GC.IsNull() && GC->IsCurveOnSurface(aSurf, aELoc))
562 {
563 // Update UV points
564 GC->Update();
565 break;
7fd59977 566 }
567 }
7fd59977 568 }
569}
570
7fd59977 571//=======================================================================
572//function : Compare
573//purpose :
574//=======================================================================
575
576Standard_Boolean BRepTools::Compare(const TopoDS_Vertex& V1,
577 const TopoDS_Vertex& V2)
578{
579 if (V1.IsSame(V2)) return Standard_True;
580 gp_Pnt p1 = BRep_Tool::Pnt(V1);
581 gp_Pnt p2 = BRep_Tool::Pnt(V2);
582 Standard_Real l = p1.Distance(p2);
583 if (l <= BRep_Tool::Tolerance(V1)) return Standard_True;
584 if (l <= BRep_Tool::Tolerance(V2)) return Standard_True;
585 return Standard_False;
586}
587
588//=======================================================================
589//function : Compare
590//purpose :
591//=======================================================================
592
593Standard_Boolean BRepTools::Compare(const TopoDS_Edge& E1,
594 const TopoDS_Edge& E2)
595{
596 if (E1.IsSame(E2)) return Standard_True;
597 return Standard_False;
598}
599
600//=======================================================================
601//function : OuterWire
602//purpose :
603//=======================================================================
604
605TopoDS_Wire BRepTools::OuterWire(const TopoDS_Face& F)
606{
607 TopoDS_Wire Wres;
608 TopExp_Explorer expw (F,TopAbs_WIRE);
609
610 if (expw.More()) {
611 Wres = TopoDS::Wire(expw.Current());
612 expw.Next();
613 if (expw.More()) {
614 Standard_Real UMin, UMax, VMin, VMax;
615 Standard_Real umin, umax, vmin, vmax;
616 BRepTools::UVBounds(F,Wres,UMin,UMax,VMin,VMax);
617 while (expw.More()) {
618 const TopoDS_Wire& W = TopoDS::Wire(expw.Current());
619 BRepTools::UVBounds(F,W,umin, umax, vmin, vmax);
620 if ((umin <= UMin) &&
621 (umax >= UMax) &&
622 (vmin <= VMin) &&
623 (vmax >= VMax)) {
624 Wres = W;
625 UMin = umin;
626 UMax = umax;
627 VMin = vmin;
628 VMax = vmax;
629 }
630 expw.Next();
631 }
632 }
633 }
634 return Wres;
635}
636
7fd59977 637//=======================================================================
638//function : Map3DEdges
639//purpose :
640//=======================================================================
641
642void BRepTools::Map3DEdges(const TopoDS_Shape& S,
643 TopTools_IndexedMapOfShape& M)
644{
645 TopExp_Explorer Ex;
646 for (Ex.Init(S,TopAbs_EDGE); Ex.More(); Ex.Next()) {
647 if (!BRep_Tool::Degenerated(TopoDS::Edge(Ex.Current())))
648 M.Add(Ex.Current());
649 }
650}
651
652//=======================================================================
653//function : Dump
654//purpose :
655//=======================================================================
656
657void BRepTools::Dump(const TopoDS_Shape& Sh, Standard_OStream& S)
658{
659 BRepTools_ShapeSet SS;
660 SS.Add(Sh);
661 SS.Dump(Sh,S);
662 SS.Dump(S);
663}
664
7fd59977 665//=======================================================================
666//function : Write
667//purpose :
668//=======================================================================
669
670void BRepTools::Write(const TopoDS_Shape& Sh, Standard_OStream& S,
671 const Handle(Message_ProgressIndicator)& PR)
672{
673 BRepTools_ShapeSet SS;
674 SS.SetProgress(PR);
675 SS.Add(Sh);
676 SS.Write(S);
677 SS.Write(Sh,S);
678}
679
680
681//=======================================================================
682//function : Read
683//purpose :
684//=======================================================================
685
686void BRepTools::Read(TopoDS_Shape& Sh,
04232180 687 std::istream& S,
7fd59977 688 const BRep_Builder& B,
689 const Handle(Message_ProgressIndicator)& PR)
690{
691 BRepTools_ShapeSet SS(B);
692 SS.SetProgress(PR);
693 SS.Read(S);
694 SS.Read(Sh,S);
695}
696
697//=======================================================================
698//function : Write
699//purpose :
700//=======================================================================
701
702Standard_Boolean BRepTools::Write(const TopoDS_Shape& Sh,
703 const Standard_CString File,
704 const Handle(Message_ProgressIndicator)& PR)
705{
04232180 706 std::ofstream os;
707 OSD_OpenStream(os, File, std::ios::out);
fc8918ad 708 if (!os.is_open() || !os.good())
709 return Standard_False;
7fd59977 710
711 Standard_Boolean isGood = (os.good() && !os.eof());
712 if(!isGood)
713 return isGood;
714
715 BRepTools_ShapeSet SS;
716 SS.SetProgress(PR);
717 SS.Add(Sh);
718
719 os << "DBRep_DrawableShape\n"; // for easy Draw read
720 SS.Write(os);
721 isGood = os.good();
722 if(isGood )
723 SS.Write(Sh,os);
724 os.flush();
725 isGood = os.good();
726
727 errno = 0;
728 os.close();
7fd59977 729 isGood = os.good() && isGood && !errno;
730
731 return isGood;
732}
733
734//=======================================================================
735//function : Read
736//purpose :
737//=======================================================================
738
739Standard_Boolean BRepTools::Read(TopoDS_Shape& Sh,
740 const Standard_CString File,
741 const BRep_Builder& B,
742 const Handle(Message_ProgressIndicator)& PR)
743{
04232180 744 std::filebuf fic;
745 std::istream in(&fic);
746 OSD_OpenStream (fic, File, std::ios::in);
94708556 747 if(!fic.is_open()) return Standard_False;
748
7fd59977 749 BRepTools_ShapeSet SS(B);
750 SS.SetProgress(PR);
751 SS.Read(in);
752 if(!SS.NbShapes()) return Standard_False;
753 SS.Read(Sh,in);
754 return Standard_True;
755}
756
757
758//=======================================================================
759//function : Clean
760//purpose :
761//=======================================================================
762
d0a994c7 763void BRepTools::Clean(const TopoDS_Shape& theShape)
7fd59977 764{
d0a994c7 765 BRep_Builder aBuilder;
766 Handle(Poly_Triangulation) aNullTriangulation;
767 Handle(Poly_PolygonOnTriangulation) aNullPoly;
7fd59977 768
d0a994c7 769 if (theShape.IsNull())
770 return;
771
772 TopExp_Explorer aFaceIt(theShape, TopAbs_FACE);
773 for (; aFaceIt.More(); aFaceIt.Next())
774 {
775 const TopoDS_Face& aFace = TopoDS::Face(aFaceIt.Current());
776
777 TopLoc_Location aLoc;
778 const Handle(Poly_Triangulation)& aTriangulation =
779 BRep_Tool::Triangulation(aFace, aLoc);
780
781 if (aTriangulation.IsNull())
782 continue;
783
784 // Nullify edges
785 TopExp_Explorer aEdgeIt(aFace, TopAbs_EDGE);
786 for (; aEdgeIt.More(); aEdgeIt.Next())
787 {
788 const TopoDS_Edge& aEdge = TopoDS::Edge(aEdgeIt.Current());
789 aBuilder.UpdateEdge(aEdge, aNullPoly, aTriangulation, aLoc);
7fd59977 790 }
d0a994c7 791
792 aBuilder.UpdateFace(aFace, aNullTriangulation);
7fd59977 793 }
a4ab454c 794
795 // Iterate over all edges seeking for 3d polygons
796 Handle (Poly_Polygon3D) aNullPoly3d;
797 TopExp_Explorer aEdgeIt (theShape, TopAbs_EDGE);
798 for (; aEdgeIt.More (); aEdgeIt.Next ())
799 {
800 const TopoDS_Edge& aEdge = TopoDS::Edge (aEdgeIt.Current ());
801
802 TopLoc_Location aLoc;
803 Handle (Poly_Polygon3D) aPoly3d = BRep_Tool::Polygon3D (aEdge, aLoc);
804 if (aPoly3d.IsNull ())
805 continue;
806
807 aBuilder.UpdateEdge (aEdge, aNullPoly3d);
808 }
7fd59977 809}
4b114473 810//=======================================================================
811//function : CleanGeometry
812//purpose :
813//=======================================================================
814
815void BRepTools::CleanGeometry(const TopoDS_Shape& theShape)
816{
817 if (theShape.IsNull())
818 return;
819
820 BRep_Builder aBuilder;
821
822 for (TopExp_Explorer aFaceIt(theShape, TopAbs_FACE); aFaceIt.More(); aFaceIt.Next())
823 {
824 TopLoc_Location aLocation;
825 const TopoDS_Face& aFace = TopoDS::Face(aFaceIt.Current());
826 const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(aFace, aLocation);
827
828 for (TopExp_Explorer aEdgeIt(aFace, TopAbs_EDGE); aEdgeIt.More(); aEdgeIt.Next())
829 {
830 const TopoDS_Edge& anEdge = TopoDS::Edge(aEdgeIt.Current());
831 aBuilder.UpdateEdge(anEdge, Handle(Geom2d_Curve)(), aSurface,
832 aLocation, BRep_Tool::Tolerance(anEdge));
833 }
834
835 aBuilder.UpdateFace(aFace, Handle(Geom_Surface)(), aFace.Location(), BRep_Tool::Tolerance(aFace));
836 }
837
838 for (TopExp_Explorer aEdgeIt2(theShape, TopAbs_EDGE); aEdgeIt2.More(); aEdgeIt2.Next())
839 {
840 const TopoDS_Edge& anEdge = TopoDS::Edge(aEdgeIt2.Current());
841
842 aBuilder.UpdateEdge(anEdge, Handle(Geom_Curve)(),
843 TopLoc_Location(), BRep_Tool::Tolerance(anEdge));
844 }
845}
846
7fd59977 847
01697018
J
848//=======================================================================
849//function : RemoveUnusedPCurves
850//purpose :
851//=======================================================================
852
853void BRepTools::RemoveUnusedPCurves(const TopoDS_Shape& S)
854{
855 TColStd_MapOfTransient UsedSurfaces;
856
857 TopExp_Explorer Explo(S, TopAbs_FACE);
858 for (; Explo.More(); Explo.Next())
859 {
860 TopoDS_Face aFace = TopoDS::Face(Explo.Current());
861 TopLoc_Location aLoc;
862 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
863 UsedSurfaces.Add(aSurf);
864 }
865
866 TopTools_IndexedMapOfShape Emap;
867 TopExp::MapShapes(S, TopAbs_EDGE, Emap);
7fd59977 868
01697018
J
869 Standard_Integer i;
870 for (i = 1; i <= Emap.Extent(); i++)
871 {
872 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*) &Emap(i).TShape());
873 BRep_ListOfCurveRepresentation& lcr = TE -> ChangeCurves();
874 BRep_ListIteratorOfListOfCurveRepresentation itrep(lcr );
875 while (itrep.More())
876 {
877 Standard_Boolean ToRemove = Standard_False;
878
879 Handle(BRep_CurveRepresentation) CurveRep = itrep.Value();
880 if (CurveRep->IsCurveOnSurface())
881 {
882 Handle(Geom_Surface) aSurface = CurveRep->Surface();
883 if (!UsedSurfaces.Contains(aSurface))
884 ToRemove = Standard_True;
885 }
886 else if (CurveRep->IsRegularity())
887 {
888 Handle(Geom_Surface) Surf1 = CurveRep->Surface();
889 Handle(Geom_Surface) Surf2 = CurveRep->Surface2();
890 ToRemove = (!UsedSurfaces.Contains(Surf1) || !UsedSurfaces.Contains(Surf2));
891 }
892
893 if (ToRemove)
894 lcr.Remove(itrep);
895 else
896 itrep.Next();
897 }
898 }
899}
7fd59977 900
901//=======================================================================
902//function : Triangulation
903//purpose :
904//=======================================================================
905
906Standard_Boolean BRepTools::Triangulation(const TopoDS_Shape& S,
907 const Standard_Real deflec)
908{
909 TopExp_Explorer exf, exe;
910 TopLoc_Location l;
911 Handle(Poly_Triangulation) T;
912 Handle(Poly_PolygonOnTriangulation) Poly;
913
914 for (exf.Init(S, TopAbs_FACE); exf.More(); exf.Next()) {
915 const TopoDS_Face& F = TopoDS::Face(exf.Current());
916 T = BRep_Tool::Triangulation(F, l);
917 if (T.IsNull() || (T->Deflection() > deflec))
918 return Standard_False;
919 for (exe.Init(F, TopAbs_EDGE); exe.More(); exe.Next()) {
920 const TopoDS_Edge& E = TopoDS::Edge(exe.Current());
921 Poly = BRep_Tool::PolygonOnTriangulation(E, T, l);
922 if (Poly.IsNull()) return Standard_False;
923 }
924 }
925 return Standard_True;
926}
927
928
929//=======================================================================
930//function : IsReallyClosed
931//purpose :
932//=======================================================================
933
934Standard_Boolean BRepTools::IsReallyClosed(const TopoDS_Edge& E,
935 const TopoDS_Face& F)
936{
937 if (!BRep_Tool::IsClosed(E,F)) {
938 return Standard_False;
939 }
940 Standard_Integer nbocc = 0;
941 TopExp_Explorer exp;
942 for (exp.Init(F,TopAbs_EDGE);exp.More();exp.Next()) {
943 if (exp.Current().IsSame(E)) {
944 nbocc++;
945 }
946 }
947 return nbocc == 2;
948}
949
2651bfde 950//=======================================================================
11af6cdd 951//function : DetectClosedness
952//purpose :
953//=======================================================================
954
955void BRepTools::DetectClosedness(const TopoDS_Face& theFace,
956 Standard_Boolean& theUclosed,
957 Standard_Boolean& theVclosed)
958{
959 theUclosed = theVclosed = Standard_False;
960
11af6cdd 961 TopExp_Explorer Explo(theFace, TopAbs_EDGE);
962 for (; Explo.More(); Explo.Next())
963 {
964 const TopoDS_Edge& anEdge = TopoDS::Edge(Explo.Current());
85c103d2 965 if (BRep_Tool::IsClosed(anEdge, theFace) &&
966 BRepTools::IsReallyClosed(anEdge, theFace))
11af6cdd 967 {
968 Standard_Real fpar, lpar;
85c103d2 969 Handle(Geom2d_Curve) PCurve1 = BRep_Tool::CurveOnSurface(anEdge, theFace, fpar, lpar);
970 Handle(Geom2d_Curve) PCurve2 = BRep_Tool::CurveOnSurface(TopoDS::Edge(anEdge.Reversed()),
971 theFace, fpar, lpar);
972 gp_Pnt2d Point1 = PCurve1->Value(fpar);
973 gp_Pnt2d Point2 = PCurve2->Value(fpar);
974 Standard_Boolean IsUiso = (Abs(Point1.X() - Point2.X()) > Abs(Point1.Y() - Point2.Y()));
975 if (IsUiso)
11af6cdd 976 theUclosed = Standard_True;
85c103d2 977 else
11af6cdd 978 theVclosed = Standard_True;
11af6cdd 979 }
980 }
981}
982
983//=======================================================================
2651bfde 984//function : EvalAndUpdateTol
985//purpose :
986//=======================================================================
987
988Standard_Real BRepTools::EvalAndUpdateTol(const TopoDS_Edge& theE,
989 const Handle(Geom_Curve)& C3d,
990 const Handle(Geom2d_Curve) C2d,
991 const Handle(Geom_Surface)& S,
992 const Standard_Real f,
993 const Standard_Real l)
994{
995 Standard_Real newtol = 0.;
996 Standard_Real first = f, last = l;
997 //Set first, last to avoid ErrosStatus = 2 because of
998 //too strong checking of limits in class CheckCurveOnSurface
999 //
1000 if(!C3d->IsPeriodic())
1001 {
1002 first = Max(first, C3d->FirstParameter());
1003 last = Min(last, C3d->LastParameter());
1004 }
1005 if(!C2d->IsPeriodic())
1006 {
1007 first = Max(first, C2d->FirstParameter());
1008 last = Min(last, C2d->LastParameter());
1009 }
1010
1011 GeomLib_CheckCurveOnSurface CT(C3d, S, first, last);
1012 CT.Perform(C2d);
1013 if(CT.IsDone())
1014 {
1015 newtol = CT.MaxDistance();
1016 }
1017 else
1018 {
1019 if(CT.ErrorStatus() == 3 || (CT.ErrorStatus() == 2 &&
1020 (C3d->IsPeriodic() || C2d->IsPeriodic())))
1021 {
1022 //Try to estimate by sample points
1023 Standard_Integer nbint = 22;
1024 Standard_Real dt = (last - first) / nbint;
1025 dt = Max(dt, Precision::Confusion());
1026 Standard_Real d, dmax = 0.;
1027 gp_Pnt2d aP2d;
1028 gp_Pnt aPC, aPS;
1029 Standard_Integer cnt = 0;
1030 Standard_Real t = first;
1031 for(; t <= last; t += dt)
1032 {
1033 cnt++;
1034 C2d->D0(t, aP2d);
1035 C3d->D0(t, aPC);
1036 S->D0(aP2d.X(), aP2d.Y(), aPS);
1037 d = aPS.SquareDistance(aPC);
1038 if(d > dmax)
1039 {
1040 dmax = d;
1041 }
1042 }
1043 if(cnt < nbint + 1)
1044 {
1045 t = last;
1046 C2d->D0(t, aP2d);
1047 C3d->D0(t, aPC);
1048 S->D0(aP2d.X(), aP2d.Y(), aPS);
1049 d = aPS.SquareDistance(aPC);
1050 if(d > dmax)
1051 {
1052 dmax = d;
1053 }
1054 }
1055
1056 newtol = 1.2 * Sqrt(dmax);
1057 }
1058 }
1059 Standard_Real Tol = BRep_Tool::Tolerance(theE);
1060 if(newtol > Tol)
1061 {
1062 Tol = newtol;
1063 BRep_Builder B;
1064 B.UpdateEdge(theE, Tol);
1065 }
1066
1067 return Tol;
1068
1069}
7fd59977 1070
1071