1 // Created on: 2012-06-09
2 // Created by: jgv@ROLEX
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <BRep_Builder.hxx>
18 #include <BRep_CurveRepresentation.hxx>
19 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
20 #include <BRep_TEdge.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRepLib.hxx>
23 #include <BRepLib_MakeEdge.hxx>
24 #include <BRepTopAdaptor_TopolTool.hxx>
25 #include <GC_MakeCircle.hxx>
26 #include <Geom2d_Line.hxx>
27 #include <Geom2d_TrimmedCurve.hxx>
28 #include <Geom2dConvert.hxx>
29 #include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
30 #include <Geom_BezierCurve.hxx>
31 #include <Geom_BSplineCurve.hxx>
32 #include <Geom_Circle.hxx>
33 #include <Geom_CylindricalSurface.hxx>
34 #include <Geom_ElementarySurface.hxx>
35 #include <Geom_Line.hxx>
36 #include <Geom_OffsetSurface.hxx>
37 #include <Geom_RectangularTrimmedSurface.hxx>
38 #include <Geom_Surface.hxx>
39 #include <Geom_SurfaceOfLinearExtrusion.hxx>
40 #include <Geom_SurfaceOfRevolution.hxx>
41 #include <Geom_SweptSurface.hxx>
42 #include <Geom_TrimmedCurve.hxx>
43 #include <GeomAdaptor_HSurface.hxx>
44 #include <GeomConvert.hxx>
45 #include <GeomConvert_CompCurveToBSplineCurve.hxx>
46 #include <GeomLib_IsPlanarSurface.hxx>
47 #include <gp_Cylinder.hxx>
50 #include <IntPatch_ImpImpIntersection.hxx>
51 #include <ShapeAnalysis_Edge.hxx>
52 #include <ShapeAnalysis_WireOrder.hxx>
53 #include <ShapeBuild_Edge.hxx>
54 #include <ShapeBuild_ReShape.hxx>
55 #include <ShapeExtend_CompositeSurface.hxx>
56 #include <ShapeFix_ComposeShell.hxx>
57 #include <ShapeFix_Edge.hxx>
58 #include <ShapeFix_Face.hxx>
59 #include <ShapeFix_SequenceOfWireSegment.hxx>
60 #include <ShapeFix_Shell.hxx>
61 #include <ShapeFix_Wire.hxx>
62 #include <ShapeFix_WireSegment.hxx>
63 #include <ShapeUpgrade_RemoveLocations.hxx>
64 #include <ShapeUpgrade_UnifySameDomain.hxx>
65 #include <Standard_Type.hxx>
66 #include <TColGeom2d_Array1OfBSplineCurve.hxx>
67 #include <TColGeom2d_HArray1OfBSplineCurve.hxx>
68 #include <TColGeom2d_SequenceOfBoundedCurve.hxx>
69 #include <TColGeom_Array1OfBSplineCurve.hxx>
70 #include <TColGeom_HArray1OfBSplineCurve.hxx>
71 #include <TColGeom_HArray2OfSurface.hxx>
72 #include <TColGeom_SequenceOfSurface.hxx>
73 #include <TColStd_Array1OfReal.hxx>
74 #include <TColStd_MapOfInteger.hxx>
76 #include <TopExp_Explorer.hxx>
78 #include <TopoDS_Edge.hxx>
79 #include <TopoDS_Face.hxx>
80 #include <TopoDS_Shape.hxx>
81 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
82 #include <TopTools_IndexedMapOfShape.hxx>
83 #include <TopTools_ListIteratorOfListOfShape.hxx>
84 #include <TopTools_MapOfShape.hxx>
85 #include <TopTools_SequenceOfShape.hxx>
86 #include <gp_Circ.hxx>
87 #include <BRepAdaptor_Curve.hxx>
88 #include <BRepClass_FaceClassifier.hxx>
90 struct SubSequenceOfEdges
92 TopTools_SequenceOfShape SeqsEdges;
93 TopoDS_Edge UnionEdges;
97 //=======================================================================
98 //function : AddOrdinaryEdges
100 //=======================================================================
101 // adds edges from the shape to the sequence
102 // seams and equal edges are dropped
103 // Returns true if one of original edges dropped
104 static Standard_Boolean AddOrdinaryEdges(TopTools_SequenceOfShape& edges,
105 const TopoDS_Shape aShape,
106 Standard_Integer& anIndex)
109 TopTools_IndexedMapOfShape aNewEdges;
110 //add edges without seams
111 for(TopExp_Explorer exp(aShape,TopAbs_EDGE); exp.More(); exp.Next()) {
112 TopoDS_Shape edge = exp.Current();
113 if(aNewEdges.Contains(edge))
115 //aNewEdges.Remove(edge);
116 TopoDS_Shape LastEdge = aNewEdges(aNewEdges.Extent());
117 aNewEdges.RemoveLast();
118 if (aNewEdges.FindIndex(edge) != 0)
119 aNewEdges.Substitute(aNewEdges.FindIndex(edge), LastEdge);
120 /////////////////////////
126 Standard_Boolean isDropped = Standard_False;
127 //merge edges and drop seams
129 for (i = 1; i <= edges.Length(); i++) {
130 TopoDS_Shape current = edges(i);
131 if(aNewEdges.Contains(current)) {
133 //aNewEdges.Remove(current);
134 TopoDS_Shape LastEdge = aNewEdges(aNewEdges.Extent());
135 aNewEdges.RemoveLast();
136 if (aNewEdges.FindIndex(current) != 0)
137 aNewEdges.Substitute(aNewEdges.FindIndex(current), LastEdge);
138 /////////////////////////
143 isDropped = Standard_True;
149 //add edges to the sequemce
150 for (i = 1; i <= aNewEdges.Extent(); i++)
151 edges.Append(aNewEdges(i));
156 //=======================================================================
157 //function : getCylinder
159 //=======================================================================
160 static Standard_Boolean getCylinder(Handle(Geom_Surface)& theInSurface,
161 gp_Cylinder& theOutCylinder)
163 Standard_Boolean isCylinder = Standard_False;
165 if (theInSurface->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
166 Handle(Geom_CylindricalSurface) aGC = Handle(Geom_CylindricalSurface)::DownCast(theInSurface);
168 theOutCylinder = aGC->Cylinder();
169 isCylinder = Standard_True;
171 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution))) {
172 Handle(Geom_SurfaceOfRevolution) aRS =
173 Handle(Geom_SurfaceOfRevolution)::DownCast(theInSurface);
174 Handle(Geom_Curve) aBasis = aRS->BasisCurve();
175 if (aBasis->IsKind(STANDARD_TYPE(Geom_Line))) {
176 Handle(Geom_Line) aBasisLine = Handle(Geom_Line)::DownCast(aBasis);
177 gp_Dir aDir = aRS->Direction();
178 gp_Dir aBasisDir = aBasisLine->Position().Direction();
179 if (aBasisDir.IsParallel(aDir, Precision::Angular())) {
180 // basis line is parallel to the revolution axis: it is a cylinder
181 gp_Pnt aLoc = aRS->Location();
182 Standard_Real aR = aBasisLine->Lin().Distance(aLoc);
183 gp_Ax3 aCylAx (aLoc, aDir);
185 theOutCylinder = gp_Cylinder(aCylAx, aR);
186 isCylinder = Standard_True;
190 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion))) {
191 Handle(Geom_SurfaceOfLinearExtrusion) aLES =
192 Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(theInSurface);
193 Handle(Geom_Curve) aBasis = aLES->BasisCurve();
194 if (aBasis->IsKind(STANDARD_TYPE(Geom_Circle))) {
195 Handle(Geom_Circle) aBasisCircle = Handle(Geom_Circle)::DownCast(aBasis);
196 gp_Dir aDir = aLES->Direction();
197 gp_Dir aBasisDir = aBasisCircle->Position().Direction();
198 if (aBasisDir.IsParallel(aDir, Precision::Angular())) {
199 // basis circle is normal to the extrusion axis: it is a cylinder
200 gp_Pnt aLoc = aBasisCircle->Location();
201 Standard_Real aR = aBasisCircle->Radius();
202 gp_Ax3 aCylAx (aLoc, aDir);
204 theOutCylinder = gp_Cylinder(aCylAx, aR);
205 isCylinder = Standard_True;
215 //=======================================================================
216 //function : ClearRts
218 //=======================================================================
219 static Handle(Geom_Surface) ClearRts(const Handle(Geom_Surface)& aSurface)
221 if(aSurface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
222 Handle(Geom_RectangularTrimmedSurface) rts =
223 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
224 return rts->BasisSurface();
229 //=======================================================================
230 //function : IsSameDomain
232 //=======================================================================
233 static Standard_Boolean IsSameDomain(const TopoDS_Face& aFace,
234 const TopoDS_Face& aCheckedFace)
236 //checking the same handles
237 TopLoc_Location L1, L2;
238 Handle(Geom_Surface) S1, S2;
240 S1 = BRep_Tool::Surface(aFace,L1);
241 S2 = BRep_Tool::Surface(aCheckedFace,L2);
243 if (S1 == S2 && L1 == L2)
244 return Standard_True;
246 // planar and cylindrical cases (IMP 20052)
247 Standard_Real aPrec = Precision::Confusion();
249 S1 = BRep_Tool::Surface(aFace);
250 S2 = BRep_Tool::Surface(aCheckedFace);
255 //Handle(Geom_OffsetSurface) aGOFS1, aGOFS2;
256 //aGOFS1 = Handle(Geom_OffsetSurface)::DownCast(S1);
257 //aGOFS2 = Handle(Geom_OffsetSurface)::DownCast(S2);
258 //if (!aGOFS1.IsNull()) S1 = aGOFS1->BasisSurface();
259 //if (!aGOFS2.IsNull()) S2 = aGOFS2->BasisSurface();
261 // case of two elementary surfaces: use OCCT tool
262 // elementary surfaces: ConicalSurface, CylindricalSurface,
263 // Plane, SphericalSurface and ToroidalSurface
264 if (S1->IsKind(STANDARD_TYPE(Geom_ElementarySurface)) &&
265 S2->IsKind(STANDARD_TYPE(Geom_ElementarySurface)))
267 Handle(GeomAdaptor_HSurface) aGA1 = new GeomAdaptor_HSurface(S1);
268 Handle(GeomAdaptor_HSurface) aGA2 = new GeomAdaptor_HSurface(S2);
270 Handle(BRepTopAdaptor_TopolTool) aTT1 = new BRepTopAdaptor_TopolTool();
271 Handle(BRepTopAdaptor_TopolTool) aTT2 = new BRepTopAdaptor_TopolTool();
274 IntPatch_ImpImpIntersection anIIInt (aGA1, aTT1, aGA2, aTT2, aPrec, aPrec);
275 if (!anIIInt.IsDone() || anIIInt.IsEmpty())
276 return Standard_False;
278 return anIIInt.TangentFaces();
280 catch (Standard_Failure) {
281 return Standard_False;
285 // case of two planar surfaces:
286 // all kinds of surfaces checked, including b-spline and bezier
287 GeomLib_IsPlanarSurface aPlanarityChecker1 (S1, aPrec);
288 if (aPlanarityChecker1.IsPlanar()) {
289 GeomLib_IsPlanarSurface aPlanarityChecker2 (S2, aPrec);
290 if (aPlanarityChecker2.IsPlanar()) {
291 gp_Pln aPln1 = aPlanarityChecker1.Plan();
292 gp_Pln aPln2 = aPlanarityChecker2.Plan();
294 if (aPln1.Position().Direction().IsParallel(aPln2.Position().Direction(),Precision::Angular()) &&
295 aPln1.Distance(aPln2) < aPrec) {
296 return Standard_True;
301 // case of two cylindrical surfaces, at least one of which is a swept surface
302 // swept surfaces: SurfaceOfLinearExtrusion, SurfaceOfRevolution
303 if ((S1->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
304 S1->IsKind(STANDARD_TYPE(Geom_SweptSurface))) &&
305 (S2->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
306 S2->IsKind(STANDARD_TYPE(Geom_SweptSurface))))
308 gp_Cylinder aCyl1, aCyl2;
309 if (getCylinder(S1, aCyl1) && getCylinder(S2, aCyl2)) {
310 if (fabs(aCyl1.Radius() - aCyl2.Radius()) < aPrec) {
311 gp_Dir aDir1 = aCyl1.Position().Direction();
312 gp_Dir aDir2 = aCyl2.Position().Direction();
313 if (aDir1.IsParallel(aDir2, Precision::Angular())) {
314 gp_Pnt aLoc1 = aCyl1.Location();
315 gp_Pnt aLoc2 = aCyl2.Location();
316 gp_Vec aVec12 (aLoc1, aLoc2);
317 if (aVec12.SquareMagnitude() < aPrec*aPrec ||
318 aVec12.IsParallel(aDir1, Precision::Angular())) {
319 return Standard_True;
326 return Standard_False;
329 //=======================================================================
330 //function : MovePCurves
332 //=======================================================================
333 static void MovePCurves(TopoDS_Face& aTarget,
334 const TopoDS_Face& aSource)
337 for(TopExp_Explorer wexp(aSource,TopAbs_WIRE);wexp.More();wexp.Next()) {
338 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(TopoDS::Wire(wexp.Current()),
339 aTarget, Precision::Confusion());
341 Standard_Boolean isReoredFailed = sfw->StatusReorder ( ShapeExtend_FAIL );
342 sfw->FixEdgeCurves();
347 sfw->FixDegenerated();
349 // remove degenerated edges from not degenerated points
350 ShapeAnalysis_Edge sae;
351 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
352 for(Standard_Integer i = 1; i<=sewd->NbEdges();i++) {
353 TopoDS_Edge E = sewd->Edge(i);
354 if(BRep_Tool::Degenerated(E)&&!sae.HasPCurve(E,aTarget)) {
360 TopoDS_Wire ResWire = sfw->Wire();
361 B.Add(aTarget,ResWire);
365 //=======================================================================
366 //function : GlueEdgesWithPCurves
367 //purpose : Glues the pcurves of the sequence of edges
368 // and glues their 3d curves
369 //=======================================================================
370 static TopoDS_Edge GlueEdgesWithPCurves(const TopTools_SequenceOfShape& aChain,
371 const TopoDS_Vertex& FirstVertex,
372 const TopoDS_Vertex& LastVertex)
374 Standard_Integer i, j;
376 TopoDS_Edge FirstEdge = TopoDS::Edge(aChain(1));
377 //TColGeom2d_SequenceOfCurve PCurveSeq;
378 TColGeom_SequenceOfSurface SurfSeq;
379 //TopTools_SequenceOfShape LocSeq;
381 BRep_ListIteratorOfListOfCurveRepresentation itr( (Handle(BRep_TEdge)::DownCast(FirstEdge.TShape()))->Curves() );
382 for (; itr.More(); itr.Next())
384 Handle(BRep_CurveRepresentation) CurveRep = itr.Value();
385 if (CurveRep->IsCurveOnSurface())
387 //PCurveSeq.Append(CurveRep->PCurve());
388 SurfSeq.Append(CurveRep->Surface());
390 TopoDS_Shape aLocShape;
391 aLocShape.Location(CurveRep->Location());
392 LocSeq.Append(aLocShape);
397 Standard_Real fpar, lpar;
398 BRep_Tool::Range(FirstEdge, fpar, lpar);
399 TopoDS_Edge PrevEdge = FirstEdge;
401 Standard_Real MaxTol = 0.;
406 Standard_Integer nb_curve = aChain.Length(); //number of curves
407 TColGeom_Array1OfBSplineCurve tab_c3d(0,nb_curve-1); //array of the curves
408 TColStd_Array1OfReal tabtolvertex(0,nb_curve-1); //(0,nb_curve-2); //array of the tolerances
410 TopoDS_Vertex PrevVertex = FirstVertex;
411 for (i = 1; i <= nb_curve; i++)
413 TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
414 TopoDS_Vertex VF, VL;
415 TopExp::Vertices(anEdge, VF, VL);
416 Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
418 Standard_Real Tol1 = BRep_Tool::Tolerance(VF);
419 Standard_Real Tol2 = BRep_Tool::Tolerance(VL);
427 TopExp::CommonVertex(PrevEdge, anEdge, CV);
428 Standard_Real Tol = BRep_Tool::Tolerance(CV);
429 tabtolvertex(i-2) = Tol;
432 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, fpar, lpar);
433 Handle(Geom_TrimmedCurve) aTrCurve = new Geom_TrimmedCurve(aCurve, fpar, lpar);
434 tab_c3d(i-1) = GeomConvert::CurveToBSplineCurve(aTrCurve);
435 GeomConvert::C0BSplineToC1BSplineCurve(tab_c3d(i-1), Precision::Confusion());
437 tab_c3d(i-1)->Reverse();
438 PrevVertex = (ToReverse)? VF : VL;
441 Handle(TColGeom_HArray1OfBSplineCurve) concatcurve; //array of the concatenated curves
442 Handle(TColStd_HArray1OfInteger) ArrayOfIndices; //array of the remining Vertex
443 GeomConvert::ConcatC1(tab_c3d,
448 Precision::Confusion()); //C1 concatenation
450 if (concatcurve->Length() > 1)
452 GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
454 for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
455 Concat.Add( concatcurve->Value(i), MaxTol, Standard_True );
457 concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
459 Handle(Geom_BSplineCurve) ResCurve = concatcurve->Value(concatcurve->Lower());
461 TColGeom2d_SequenceOfBoundedCurve ResPCurves;
462 TopLoc_Location aLoc;
463 for (j = 1; j <= SurfSeq.Length(); j++)
465 TColGeom2d_Array1OfBSplineCurve tab_c2d(0,nb_curve-1); //array of the pcurves
467 PrevVertex = FirstVertex;
468 PrevEdge = FirstEdge;
469 //TopLoc_Location theLoc = LocSeq(j).Location();
470 for (i = 1; i <= nb_curve; i++)
472 TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
473 TopoDS_Vertex VF, VL;
474 TopExp::Vertices(anEdge, VF, VL);
475 Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
478 Handle(Geom2d_Curve) aPCurve =
479 BRep_Tool::CurveOnSurface(anEdge, SurfSeq(j), anEdge.Location()*theLoc, fpar, lpar);
481 Handle(Geom2d_Curve) aPCurve =
482 BRep_Tool::CurveOnSurface(anEdge, SurfSeq(j), aLoc, fpar, lpar);
483 Handle(Geom2d_TrimmedCurve) aTrPCurve = new Geom2d_TrimmedCurve(aPCurve, fpar, lpar);
484 tab_c2d(i-1) = Geom2dConvert::CurveToBSplineCurve(aTrPCurve);
485 Geom2dConvert::C0BSplineToC1BSplineCurve(tab_c2d(i-1), Precision::Confusion());
487 tab_c2d(i-1)->Reverse();
488 PrevVertex = (ToReverse)? VF : VL;
491 Handle(TColGeom2d_HArray1OfBSplineCurve) concatc2d; //array of the concatenated curves
492 Handle(TColStd_HArray1OfInteger) ArrayOfInd2d; //array of the remining Vertex
493 Geom2dConvert::ConcatC1(tab_c2d,
498 Precision::Confusion()); //C1 concatenation
500 if (concatc2d->Length() > 1)
502 Geom2dConvert_CompCurveToBSplineCurve Concat2d(concatc2d->Value(concatc2d->Lower()));
504 for (i = concatc2d->Lower()+1; i <= concatc2d->Upper(); i++)
505 Concat2d.Add( concatc2d->Value(i), MaxTol, Standard_True );
507 concatc2d->SetValue(concatc2d->Lower(), Concat2d.BSplineCurve());
509 Handle(Geom2d_BSplineCurve) aResPCurve = concatc2d->Value(concatc2d->Lower());
510 ResPCurves.Append(aResPCurve);
513 ResEdge = BRepLib_MakeEdge(ResCurve,
514 FirstVertex, LastVertex,
515 ResCurve->FirstParameter(), ResCurve->LastParameter());
516 BB.SameRange(ResEdge, Standard_False);
517 BB.SameParameter(ResEdge, Standard_False);
518 for (j = 1; j <= ResPCurves.Length(); j++)
520 BB.UpdateEdge(ResEdge, ResPCurves(j), SurfSeq(j), aLoc, MaxTol);
521 BB.Range(ResEdge, SurfSeq(j), aLoc, ResPCurves(j)->FirstParameter(), ResPCurves(j)->LastParameter());
524 BRepLib::SameParameter(ResEdge, MaxTol, Standard_True);
529 //=======================================================================
530 //function : MergeSubSeq
531 //purpose : Merges a sequence of edges into one edge if possible
532 //=======================================================================
534 static Standard_Boolean MergeSubSeq(const TopTools_SequenceOfShape& aChain, TopoDS_Edge& OutEdge, double Tol, Standard_Boolean ConcatBSplines)
536 ShapeAnalysis_Edge sae;
538 // union edges in chain
540 Standard_Real fp1,lp1,fp2,lp2;
541 Standard_Boolean IsUnionOfLinesPossible = Standard_True;
542 Standard_Boolean IsUnionOfCirclesPossible = Standard_True;
543 Handle(Geom_Curve) c3d1, c3d2;
544 for(j=1; j<aChain.Length(); j++)
546 TopoDS_Edge edge1 = TopoDS::Edge(aChain.Value(j));
547 c3d1 = BRep_Tool::Curve(edge1,fp1,lp1);
549 TopoDS_Edge edge2 = TopoDS::Edge(aChain.Value(j+1));
550 c3d2 = BRep_Tool::Curve(edge2,fp2,lp2);
552 if(c3d1.IsNull() || c3d2.IsNull())
553 return Standard_False;
555 while(c3d1->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
556 Handle(Geom_TrimmedCurve) tc =
557 Handle(Geom_TrimmedCurve)::DownCast(c3d1);
558 c3d1 = tc->BasisCurve();
560 while(c3d2->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
561 Handle(Geom_TrimmedCurve) tc =
562 Handle(Geom_TrimmedCurve)::DownCast(c3d2);
563 c3d2 = tc->BasisCurve();
565 if( c3d1->IsKind(STANDARD_TYPE(Geom_Line)) && c3d2->IsKind(STANDARD_TYPE(Geom_Line)) ) {
566 Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(c3d1);
567 Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(c3d2);
568 gp_Dir Dir1 = L1->Position().Direction();
569 gp_Dir Dir2 = L2->Position().Direction();
570 if(!Dir1.IsParallel(Dir2,Tol))
571 IsUnionOfLinesPossible = Standard_False;
574 IsUnionOfLinesPossible = Standard_False;
575 if( c3d1->IsKind(STANDARD_TYPE(Geom_Circle)) && c3d2->IsKind(STANDARD_TYPE(Geom_Circle)) ) {
576 Handle(Geom_Circle) C1 = Handle(Geom_Circle)::DownCast(c3d1);
577 Handle(Geom_Circle) C2 = Handle(Geom_Circle)::DownCast(c3d2);
578 gp_Pnt P01 = C1->Location();
579 gp_Pnt P02 = C2->Location();
580 if (P01.Distance(P02) > Precision::Confusion())
581 IsUnionOfCirclesPossible = Standard_False;
584 IsUnionOfCirclesPossible = Standard_False;
586 if (IsUnionOfLinesPossible && IsUnionOfCirclesPossible)
587 return Standard_False;
589 //union of lines is possible
590 if (IsUnionOfLinesPossible)
592 TopoDS_Vertex V1 = sae.FirstVertex(TopoDS::Edge(aChain.First()));
593 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
594 TopoDS_Vertex V2 = sae.LastVertex(TopoDS::Edge(aChain.Last()));
595 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
596 gp_Vec Vec(PV1, PV2);
597 Handle(Geom_Line) L = new Geom_Line(gp_Ax1(PV1,Vec));
598 Standard_Real dist = PV1.Distance(PV2);
599 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(L,0.0,dist);
601 B.MakeEdge (E, tc ,Precision::Confusion());
602 B.Add (E,V1); B.Add (E,V2);
603 B.UpdateVertex(V1, 0., E, 0.);
604 B.UpdateVertex(V2, dist, E, 0.);
606 return Standard_True;
609 if (IsUnionOfCirclesPossible)
612 TopoDS_Edge FE = TopoDS::Edge(aChain.First());
613 Handle(Geom_Curve) c3d = BRep_Tool::Curve(FE,f,l);
615 while(c3d->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
616 Handle(Geom_TrimmedCurve) tc =
617 Handle(Geom_TrimmedCurve)::DownCast(c3d);
618 c3d = tc->BasisCurve();
620 Handle(Geom_Circle) Cir = Handle(Geom_Circle)::DownCast(c3d);
622 TopoDS_Vertex V1 = sae.FirstVertex(FE);
623 TopoDS_Vertex V2 = sae.LastVertex(TopoDS::Edge(aChain.Last()));
627 BRepAdaptor_Curve adef(FE);
628 Handle(Geom_Circle) Cir1;
630 if ( FE.Orientation() == TopAbs_FORWARD)
632 FP = adef.FirstParameter();
633 LP = adef.LastParameter();
637 FP = adef.LastParameter();
638 LP = adef.FirstParameter();
640 if (Abs(FP) < Precision::PConfusion())
642 B.MakeEdge (E,Cir, Precision::Confusion());
645 E.Orientation(FE.Orientation());
649 GC_MakeCircle MC1 (adef.Value(FP), adef.Value((FP + LP) * 0.5), adef.Value(LP));
653 return Standard_False;
654 B.MakeEdge (E, Cir1, Precision::Confusion());
660 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
661 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
662 TopoDS_Vertex VM = sae.LastVertex(FE);
663 gp_Pnt PVM = BRep_Tool::Pnt(VM);
664 GC_MakeCircle MC (PV1,PVM,PV2);
665 Handle(Geom_Circle) C = MC.Value();
666 gp_Pnt P0 = C->Location();
667 gp_Dir D1(gp_Vec(P0,PV1));
668 gp_Dir D2(gp_Vec(P0,PV2));
669 Standard_Real fpar = C->XAxis().Direction().Angle(D1);
670 if(fabs(fpar)>Precision::Confusion()) {
672 gp_Dir ND = C->XAxis().Direction().Crossed(D1);
673 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
677 Standard_Real lpar = C->XAxis().Direction().Angle(D2);
678 if(fabs(lpar)>Precision::Confusion()) {
680 gp_Dir ND = C->XAxis().Direction().Crossed(D2);
681 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
685 if (lpar < fpar) lpar += 2*M_PI;
686 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
687 B.MakeEdge (E,tc,Precision::Confusion());
690 B.UpdateVertex(V1, fpar, E, 0.);
691 B.UpdateVertex(V2, lpar, E, 0.);
694 return Standard_True;
696 if (aChain.Length() > 1 && ConcatBSplines) {
697 // second step: union edges with various curves
698 // skl for bug 0020052 from Mantis: perform such unions
699 // only if curves are bspline or bezier
701 TopoDS_Vertex VF = sae.FirstVertex(TopoDS::Edge(aChain.First()));
702 TopoDS_Vertex VL = sae.LastVertex(TopoDS::Edge(aChain.Last()));
703 Standard_Boolean NeedUnion = Standard_True;
704 for(j=1; j<=aChain.Length(); j++) {
705 TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
707 Handle(Geom_Curve) c3d = BRep_Tool::Curve(edge,Loc,fp1,lp1);
708 if(c3d.IsNull()) continue;
709 while(c3d->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
710 Handle(Geom_TrimmedCurve) tc =
711 Handle(Geom_TrimmedCurve)::DownCast(c3d);
712 c3d = tc->BasisCurve();
714 if( ( c3d->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ||
715 c3d->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ) ) continue;
716 NeedUnion = Standard_False;
721 cout<<"can not make analitical union => make approximation"<<endl;
723 TopoDS_Edge E = GlueEdgesWithPCurves(aChain, VF, VL);
725 return Standard_True;
729 cout<<"can not make approximation for such types of curves"<<endl;
731 return Standard_False;
734 return Standard_False;
737 //=======================================================================
738 //function : IsMergingPossible
739 //purpose : Checks if merging of two edges is possible
740 //=======================================================================
742 static Standard_Boolean IsMergingPossible(const TopoDS_Edge& edge1, const TopoDS_Edge& edge2,
743 double Tol, const TopTools_MapOfShape& DegEdgeVrt)
745 TopoDS_Vertex CV = TopExp::LastVertex(edge1, Standard_True);
746 if (CV.IsNull() || DegEdgeVrt.Contains(CV))
747 return Standard_False;
749 BRepAdaptor_Curve ade1(edge1);
750 BRepAdaptor_Curve ade2(edge2);
752 GeomAbs_CurveType t1 = ade1.GetType();
753 GeomAbs_CurveType t2 = ade2.GetType();
755 if( t1 == GeomAbs_Circle && t2 == GeomAbs_Circle)
757 if (ade1.Circle().Location().Distance(ade2.Circle().Location()) > Precision::Confusion())
758 return Standard_False;
761 if( ( (t1 != GeomAbs_BezierCurve && t1 != GeomAbs_BSplineCurve) ||
762 (t2 != GeomAbs_BezierCurve && t2 != GeomAbs_BSplineCurve)) && t1 != t2)
763 return Standard_False;
767 if (edge1.Orientation() == TopAbs_FORWARD)
768 ade1.D1(ade1.LastParameter(), P1, Diff1);
771 ade1.D1(ade1.FirstParameter(), P1, Diff1);
775 if (edge2.Orientation() == TopAbs_FORWARD)
776 ade2.D1(ade2.FirstParameter(), P2, Diff2);
779 ade2.D1(ade2.LastParameter(), P2, Diff2);
783 if (Diff1.Angle(Diff2) > Tol)
784 return Standard_False;
786 return Standard_True;
789 //=======================================================================
790 //function : GenerateSubSeq
791 //purpose : Generates sub-sequences of edges from sequence of edges
792 //Edges from each subsequences can be merged into the one edge
793 //=======================================================================
795 static void GenerateSubSeq (const TopTools_SequenceOfShape& anInpEdgeSeq,
796 NCollection_Sequence<SubSequenceOfEdges>& SeqOfSubSeqOfEdges,
797 Standard_Boolean IsClosed, double Tol, const TopTools_MapOfShape& DegEdgeVrt )
799 Standard_Boolean isOk = Standard_False;
800 TopoDS_Edge edge1, edge2;
802 SubSequenceOfEdges SubSeq;
803 SubSeq.SeqsEdges.Append(TopoDS::Edge(anInpEdgeSeq(1)));
804 SeqOfSubSeqOfEdges.Append(SubSeq);
806 for (int i = 1; i < anInpEdgeSeq.Length(); i++)
808 edge1 = TopoDS::Edge(anInpEdgeSeq(i));
809 edge2 = TopoDS::Edge(anInpEdgeSeq(i+1));
810 isOk = IsMergingPossible(edge1, edge2, Tol, DegEdgeVrt);
813 SubSequenceOfEdges aSubSeq;
814 aSubSeq.SeqsEdges.Append(edge2);
815 SeqOfSubSeqOfEdges.Append(aSubSeq);
818 SeqOfSubSeqOfEdges.ChangeLast().SeqsEdges.Append(edge2);
820 /// check first and last chain segments
821 if (IsClosed && SeqOfSubSeqOfEdges.Length() > 1)
823 edge1 = TopoDS::Edge(anInpEdgeSeq.Last());
824 edge2 = TopoDS::Edge(anInpEdgeSeq.First());
825 if (IsMergingPossible(edge1, edge2, Tol, DegEdgeVrt))
827 SeqOfSubSeqOfEdges.ChangeLast().SeqsEdges.Append(SeqOfSubSeqOfEdges.ChangeFirst().SeqsEdges);
828 SeqOfSubSeqOfEdges.Remove(1);
834 //=======================================================================
835 //function : MergeEdges
837 //=======================================================================
838 static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
839 const Standard_Real Tol,
840 const Standard_Boolean ConcatBSplines,
841 NCollection_Sequence<SubSequenceOfEdges>& SeqOfSubSeqOfEdges,
842 const TopTools_MapOfShape& NonMergVrt )
844 // make chain for union
846 ShapeAnalysis_Edge sae;
847 TopoDS_Edge FirstE = TopoDS::Edge(SeqEdges.Value(1));
848 TopoDS_Edge LastE = FirstE;
849 TopoDS_Vertex VF = sae.FirstVertex(FirstE);
850 TopoDS_Vertex VL = sae.LastVertex(LastE);
851 TopTools_SequenceOfShape aChain;
852 aChain.Append(FirstE);
853 TColStd_MapOfInteger IndUsedEdges;
856 TopTools_MapOfShape VerticesToAvoid;
857 TopTools_SequenceOfShape SeqEdges1;
858 for (j = 1; j <= SeqEdges.Length(); j++)
860 TopoDS_Edge anEdge = TopoDS::Edge(SeqEdges(j));
861 if (BRep_Tool::Degenerated(anEdge))
863 TopoDS_Vertex V1, V2;
864 TopExp::Vertices(anEdge, V1, V2);
865 VerticesToAvoid.Add(V1);
866 VerticesToAvoid.Add(V2);
869 SeqEdges1.Append(anEdge);
872 for(j=2; j<=SeqEdges1.Length(); j++) {
873 for(Standard_Integer k=2; k<=SeqEdges1.Length(); k++) {
874 if(IndUsedEdges.Contains(k)) continue;
875 TopoDS_Edge edge = TopoDS::Edge(SeqEdges1.Value(k));
876 TopoDS_Vertex VF2 = sae.FirstVertex(edge);
877 TopoDS_Vertex VL2 = sae.LastVertex(edge);
881 VL = sae.LastVertex(LastE);
884 else if(VL2.IsSame(VF)) {
885 aChain.Prepend(edge);
887 VF = sae.FirstVertex(FirstE);
893 Standard_Boolean IsClosed = Standard_False;
894 if (VF.IsSame ( VL ))
895 IsClosed = Standard_True;
897 VerticesToAvoid.Unite(NonMergVrt);
898 GenerateSubSeq(aChain, SeqOfSubSeqOfEdges, IsClosed, Tol, VerticesToAvoid);
900 for (int i = 1; i <= SeqOfSubSeqOfEdges.Length(); i++)
903 if (SeqOfSubSeqOfEdges(i).SeqsEdges.Length() < 2)
905 if (MergeSubSeq(SeqOfSubSeqOfEdges(i).SeqsEdges, UE, Tol, ConcatBSplines))
906 SeqOfSubSeqOfEdges(i).UnionEdges = UE;
908 return Standard_True;
911 //=======================================================================
912 //function : MergeSeq
913 //purpose : Tries to unify the sequence of edges with the set of another edges
914 //which lies on the same geometry
915 //=======================================================================
917 static Standard_Boolean MergeSeq (const TopTools_SequenceOfShape& SeqEdges,
918 const Standard_Real Tol,
919 const Standard_Boolean ConcatBSplines,
920 Handle(ShapeBuild_ReShape)& theContext,
921 TopTools_DataMapOfShapeShape& theOldShapes,
922 const TopTools_MapOfShape& nonMergVert,
923 const TopTools_DataMapOfShapeShape& NewEdges2OldEdges)
925 NCollection_Sequence<SubSequenceOfEdges> SeqOfSubsSeqOfEdges;
927 if ( MergeEdges(SeqEdges, Tol, ConcatBSplines, SeqOfSubsSeqOfEdges, nonMergVert) )
929 for (Standard_Integer i = 1; i <= SeqOfSubsSeqOfEdges.Length(); i++ )
931 if (SeqOfSubsSeqOfEdges(i).UnionEdges.IsNull())
933 theContext->Replace(SeqOfSubsSeqOfEdges(i).SeqsEdges(1), SeqOfSubsSeqOfEdges(i).UnionEdges);
935 for (Standard_Integer j = 2; j <= SeqOfSubsSeqOfEdges(i).SeqsEdges.Length(); j++)
937 TopoDS_Shape OldEdge = NewEdges2OldEdges(SeqOfSubsSeqOfEdges(i).SeqsEdges(j));
938 theOldShapes.Bind(OldEdge, SeqOfSubsSeqOfEdges(i).UnionEdges);
939 theContext->Remove(SeqOfSubsSeqOfEdges(i).SeqsEdges(j));
942 return Standard_True;
945 return Standard_False;
948 //=======================================================================
949 //function : CheckSharedVertices
950 //purpose : Checks the sequence of edges on the presence of shared vertex
951 //=======================================================================
953 static void CheckSharedVertices(const TopTools_SequenceOfShape& theSeqEdges,
954 const TopTools_IndexedDataMapOfShapeListOfShape& theMapEdgesVertex,
955 TopTools_MapOfShape& theShareVertMap)
957 ShapeAnalysis_Edge sae;
958 TopTools_SequenceOfShape SeqVertexes;
959 TopTools_MapOfShape MapVertexes;
960 for (Standard_Integer k = 1; k <= theSeqEdges.Length(); k++ )
962 TopoDS_Vertex aV1 = sae.FirstVertex(TopoDS::Edge(theSeqEdges(k)));
963 TopoDS_Vertex aV2 = sae.LastVertex(TopoDS::Edge(theSeqEdges(k)));
964 if (!MapVertexes.Add(aV1))
965 SeqVertexes.Append(aV1);
966 if (!MapVertexes.Add(aV2))
967 SeqVertexes.Append(aV2);
970 for (Standard_Integer k = 1; k <= SeqVertexes.Length()/* && !IsSharedVertexPresent*/; k++ )
972 const TopTools_ListOfShape& ListEdgesV1 = theMapEdgesVertex.FindFromKey(SeqVertexes(k));
973 TopTools_MapOfShape aMapOfEdges;
974 TopTools_ListIteratorOfListOfShape iter(ListEdgesV1);
975 for (; iter.More(); iter.Next())
976 aMapOfEdges.Add(iter.Value());
977 if (aMapOfEdges.Extent() > 2)
978 theShareVertMap.Add(SeqVertexes(k));
980 //return theShareVertMap.IsEmpty() ? false : true;
983 //=======================================================================
984 //function : ShapeUpgrade_UnifySameDomain
985 //purpose : Constructor
986 //=======================================================================
988 ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain()
990 myUnifyEdges = Standard_True;
991 myUnifyFaces = Standard_True;
992 myConcatBSplines = Standard_False;
994 myContext = new ShapeBuild_ReShape;
997 //=======================================================================
998 //function : ShapeUpgrade_UnifySameDomain
999 //purpose : Constructor
1000 //=======================================================================
1002 ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain(const TopoDS_Shape& aShape,
1003 const Standard_Boolean UnifyEdges,
1004 const Standard_Boolean UnifyFaces,
1005 const Standard_Boolean ConcatBSplines)
1007 myInitShape = aShape;
1009 myUnifyEdges = UnifyEdges;
1010 myUnifyFaces = UnifyFaces;
1011 myConcatBSplines = ConcatBSplines;
1013 myContext = new ShapeBuild_ReShape;
1016 //=======================================================================
1017 //function : Initialize
1019 //=======================================================================
1021 void ShapeUpgrade_UnifySameDomain::Initialize(const TopoDS_Shape& aShape,
1022 const Standard_Boolean UnifyEdges,
1023 const Standard_Boolean UnifyFaces,
1024 const Standard_Boolean ConcatBSplines)
1026 myInitShape = aShape;
1028 myUnifyEdges = UnifyEdges;
1029 myUnifyFaces = UnifyFaces;
1030 myConcatBSplines = ConcatBSplines;
1033 myOldShapes.Clear();
1034 //myGenerated.Clear();
1037 //=======================================================================
1038 //function : putIntWires
1039 //purpose : Add internal wires that are classified inside the face as a subshape,
1040 // and remove them from the sequence
1041 //=======================================================================
1042 static void putIntWires(TopoDS_Shape& theFace, TopTools_SequenceOfShape& theWires)
1044 TopoDS_Face& aFace = TopoDS::Face(theFace);
1045 for (Standard_Integer i=1; i <= theWires.Length(); i++)
1047 TopoDS_Shape aWire = theWires(i);
1049 Standard_Boolean isP2d = Standard_False;
1050 for (TopoDS_Iterator it(aWire); it.More() && !isP2d; it.Next())
1052 const TopoDS_Edge& anEdge = TopoDS::Edge(it.Value());
1053 Standard_Real aFirst, aLast;
1054 Handle(Geom2d_Curve) aC2d = BRep_Tool::CurveOnSurface(anEdge, aFace, aFirst, aLast);
1055 aC2d->D0((aFirst + aLast) * 0.5, aP2d);
1056 isP2d = Standard_True;
1058 BRepClass_FaceClassifier aClass(aFace, aP2d, Precision::PConfusion());
1059 if (aClass.State() == TopAbs_IN)
1061 BRep_Builder().Add(aFace, aWire);
1068 //=======================================================================
1069 //function : UnifyFaces
1071 //=======================================================================
1073 void ShapeUpgrade_UnifySameDomain::UnifyFaces()
1075 // creating map of edge faces
1076 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
1077 TopExp::MapShapesAndAncestors(myShape, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
1078 // processing each shell
1079 TopExp_Explorer exps;
1080 for (exps.Init(myShape, TopAbs_SHELL); exps.More(); exps.Next()) {
1081 TopoDS_Shell aShell = TopoDS::Shell(exps.Current());
1083 // map of processed shapes
1084 TopTools_MapOfShape aProcessed;
1086 Standard_Integer NbModif = 0;
1087 Standard_Boolean hasFailed = Standard_False;
1088 Standard_Real tol = Precision::Confusion();
1091 Standard_Integer nbf = 0;
1092 TopExp_Explorer exp;
1093 TopTools_MapOfShape mapF;
1094 for (exp.Init(aShell, TopAbs_FACE); exp.More(); exp.Next()) {
1095 if (mapF.Add(exp.Current()))
1099 // processing each face
1101 for (exp.Init(aShell, TopAbs_FACE); exp.More(); exp.Next()) {
1102 TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
1104 if (aProcessed.Contains(aFace))
1107 Standard_Integer dummy;
1108 TopTools_SequenceOfShape edges;
1109 AddOrdinaryEdges(edges,aFace,dummy);
1111 TopTools_SequenceOfShape faces;
1112 faces.Append(aFace);
1114 //surface and location to construct result
1115 TopLoc_Location aBaseLocation;
1116 Handle(Geom_Surface) aBaseSurface = BRep_Tool::Surface(aFace,aBaseLocation);
1117 aBaseSurface = ClearRts(aBaseSurface);
1119 // find adjacent faces to union
1121 for (i = 1; i <= edges.Length(); i++) {
1122 TopoDS_Edge edge = TopoDS::Edge(edges(i));
1123 if (BRep_Tool::Degenerated(edge))
1126 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
1127 TopTools_ListIteratorOfListOfShape anIter(aList);
1128 for (; anIter.More(); anIter.Next()) {
1129 TopoDS_Face anCheckedFace = TopoDS::Face(anIter.Value().Oriented(TopAbs_FORWARD));
1130 if (anCheckedFace.IsSame(aFace))
1133 if (aProcessed.Contains(anCheckedFace))
1136 if (IsSameDomain(aFace,anCheckedFace)) {
1138 if (aList.Extent() != 2) {
1139 // non mainfold case is not processed
1143 // replacing pcurves
1144 TopoDS_Face aMockUpFace;
1146 B.MakeFace(aMockUpFace,aBaseSurface,aBaseLocation,0.);
1147 MovePCurves(aMockUpFace,anCheckedFace);
1149 if (AddOrdinaryEdges(edges,aMockUpFace,dummy)) {
1150 // sequence edges is modified
1154 faces.Append(anCheckedFace);
1155 aProcessed.Add(anCheckedFace);
1161 // all faces collected in the sequence. Perform union of faces
1162 if (faces.Length() > 1) {
1164 TopoDS_Face aResult;
1166 B.MakeFace(aResult,aBaseSurface,aBaseLocation,0);
1167 Standard_Integer nbWires = 0;
1170 while (edges.Length()>0) {
1172 Standard_Boolean isEdge3d = Standard_False;
1174 TopTools_MapOfShape aVertices;
1178 TopoDS_Edge anEdge = TopoDS::Edge(edges(1));
1180 // collect internal edges in separate wires
1181 Standard_Boolean isInternal = (anEdge.Orientation() == TopAbs_INTERNAL);
1183 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
1184 B.Add(aWire,anEdge);
1185 TopoDS_Vertex V1,V2;
1186 TopExp::Vertices(anEdge,V1,V2);
1190 Standard_Boolean isNewFound = Standard_False;
1192 isNewFound = Standard_False;
1193 for(Standard_Integer j = 1; j <= edges.Length(); j++) {
1194 anEdge = TopoDS::Edge(edges(j));
1195 // check if the current edge orientation corresponds to the first one
1196 Standard_Boolean isCurrInternal = (anEdge.Orientation() == TopAbs_INTERNAL);
1197 if (isCurrInternal != isInternal)
1199 TopExp::Vertices(anEdge,V1,V2);
1200 if(aVertices.Contains(V1) || aVertices.Contains(V2)) {
1201 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
1204 B.Add(aWire,anEdge);
1207 isNewFound = Standard_True;
1210 } while (isNewFound);
1212 // sorting any type of edges
1213 aWire.Closed (BRep_Tool::IsClosed (aWire));
1214 aWire = TopoDS::Wire(myContext->Apply(aWire));
1216 TopoDS_Face tmpF = TopoDS::Face(myContext->Apply(faces(1).Oriented(TopAbs_FORWARD)));
1217 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(aWire,tmpF,Precision::Confusion());
1219 Standard_Boolean isDegRemoved = Standard_False;
1220 if(!sfw->StatusReorder ( ShapeExtend_FAIL )) {
1221 // clear degenerated edges if at least one with 3d curve exist
1223 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
1224 for(Standard_Integer j = 1; j<=sewd->NbEdges();j++) {
1225 TopoDS_Edge E = sewd->Edge(j);
1226 if(BRep_Tool::Degenerated(E)) {
1228 isDegRemoved = Standard_True;
1235 sfw->FixDegenerated();
1237 TopoDS_Wire aWireFixed = sfw->Wire();
1238 //aContext->Replace(aWire,aWireFixed);
1239 myContext->Replace(aWire,aWireFixed);
1242 if (!myOldNewMap.IsBound(aWire))
1244 TopTools_ListOfShape EmptyList;
1245 myOldNewMap.Bind(aWire, EmptyList);
1247 myOldNewMap(aWire).Clear();
1248 myOldNewMap(aWire).Append(aWireFixed);
1252 // add resulting wire
1254 B.Add(aResult,aWireFixed);
1258 Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
1259 Standard_Integer nbEdges = sbwd->NbEdges();
1260 // sort degenerated edges and create one edge instead of several ones
1261 ShapeAnalysis_WireOrder sawo(Standard_False, 0);
1262 ShapeAnalysis_Edge sae;
1263 Standard_Integer aLastEdge = nbEdges;
1264 for(Standard_Integer j = 1; j <= nbEdges; j++) {
1266 //smh protection on NULL pcurve
1267 Handle(Geom2d_Curve) c2d;
1268 if(!sae.PCurve(sbwd->Edge(j),tmpF,c2d,f,l)) {
1272 sawo.Add(c2d->Value(f).XY(),c2d->Value(l).XY());
1276 // constructind one degenerative edge
1277 gp_XY aStart, anEnd, tmp;
1278 Standard_Integer nbFirst = sawo.Ordered(1);
1279 TopoDS_Edge anOrigE = TopoDS::Edge(sbwd->Edge(nbFirst).Oriented(TopAbs_FORWARD));
1280 ShapeBuild_Edge sbe;
1281 TopoDS_Vertex aDummyV;
1282 TopoDS_Edge E = sbe.CopyReplaceVertices(anOrigE,aDummyV,aDummyV);
1283 sawo.XY(nbFirst,aStart,tmp);
1284 sawo.XY(sawo.Ordered(aLastEdge),tmp,anEnd);
1286 gp_XY aVec = anEnd-aStart;
1287 Handle(Geom2d_Line) aLine = new Geom2d_Line(aStart,gp_Dir2d(anEnd-aStart));
1289 B.UpdateEdge(E,aLine,tmpF,0.);
1290 B.Range(E,tmpF,0.,aVec.Modulus());
1291 Handle(Geom_Curve) C3d;
1292 B.UpdateEdge(E,C3d,0.);
1293 B.Degenerated(E,Standard_True);
1297 aW.Closed (Standard_True);
1302 // perform substitution of face
1303 //aContext->Replace(aContext->Apply(aFace),aResult);
1304 myContext->Replace(myContext->Apply(aFace),aResult);
1307 if (!myOldNewMap.IsBound(aFace))
1309 TopTools_ListOfShape EmptyList;
1310 myOldNewMap.Bind(aFace, EmptyList);
1312 myOldNewMap(aFace).Clear();
1313 myOldNewMap(aFace).Append(aResult);
1317 ShapeFix_Face sff (aResult);
1318 //Intializing by tolerances
1319 sff.SetPrecision(Precision::Confusion());
1320 sff.SetMinTolerance(tol);
1321 sff.SetMaxTolerance(1.);
1323 sff.FixOrientationMode() = 0;
1324 //sff.FixWireMode() = 0;
1325 //sff.SetContext(aContext);
1326 sff.SetContext(myContext);
1327 // Applying the fixes
1329 if(sff.Status(ShapeExtend_FAIL))
1330 hasFailed = Standard_True;
1332 // breaking down to several faces
1333 //TopoDS_Shape theResult = aContext->Apply(aResult);
1334 TopoDS_Shape theResult = myContext->Apply(aResult);
1335 for (TopExp_Explorer aFaceExp (theResult,TopAbs_FACE); aFaceExp.More(); aFaceExp.Next()) {
1336 TopoDS_Face aCurrent = TopoDS::Face(aFaceExp.Current().Oriented(TopAbs_FORWARD));
1337 Handle(TColGeom_HArray2OfSurface) grid = new TColGeom_HArray2OfSurface ( 1, 1, 1, 1 );
1338 grid->SetValue ( 1, 1, aBaseSurface );
1339 Handle(ShapeExtend_CompositeSurface) G = new ShapeExtend_CompositeSurface ( grid );
1340 ShapeFix_ComposeShell CompShell;
1341 CompShell.Init ( G, aBaseLocation, aCurrent, ::Precision::Confusion() );//myPrecision
1342 //CompShell.SetContext( aContext );
1343 CompShell.SetContext( myContext );
1345 TopTools_SequenceOfShape parts, anIntWires;
1346 ShapeFix_SequenceOfWireSegment wires;
1347 for(TopExp_Explorer W_Exp(aCurrent,TopAbs_WIRE);W_Exp.More();W_Exp.Next()) {
1348 const TopoDS_Wire& aWire = TopoDS::Wire(W_Exp.Current());
1349 // check if the wire is ordinary (contains non-internal edges)
1350 Standard_Boolean isInternal = Standard_True;
1351 for (TopoDS_Iterator it(aWire); it.More() && isInternal; it.Next())
1352 isInternal = (it.Value().Orientation() == TopAbs_INTERNAL);
1355 // place internal wire separately
1356 anIntWires.Append(aWire);
1360 Handle(ShapeExtend_WireData) sbwd =
1361 new ShapeExtend_WireData (aWire);
1362 ShapeFix_WireSegment seg ( sbwd, TopAbs_REVERSED );
1367 CompShell.DispatchWires ( parts,wires );
1368 for (Standard_Integer j=1; j <= parts.Length(); j++ ) {
1369 ShapeFix_Face aFixOrient(TopoDS::Face(parts(j)));
1370 //aFixOrient.SetContext(aContext);
1371 aFixOrient.SetContext(myContext);
1372 aFixOrient.FixOrientation();
1373 // put internal wires to faces
1374 putIntWires(parts(j), anIntWires);
1377 TopoDS_Shape CompRes;
1378 if ( parts.Length() !=1 ) {
1381 for ( i=1; i <= parts.Length(); i++ )
1382 B.Add ( S, parts(i) );
1383 S.Closed (BRep_Tool::IsClosed (S));
1386 else CompRes = parts(1);
1388 //aContext->Replace(aCurrent,CompRes);
1389 myContext->Replace(aCurrent,CompRes);
1392 if (!myOldNewMap.IsBound(aCurrent))
1394 TopTools_ListOfShape EmptyList;
1395 myOldNewMap.Bind(aCurrent, EmptyList);
1397 myOldNewMap(aCurrent).Clear();
1398 myOldNewMap(aCurrent).Append(CompRes);
1403 // remove the remaining faces
1404 for(i = 2; i <= faces.Length(); i++)
1406 myOldShapes.Bind(faces(i), theResult);
1407 myContext->Remove(faces(i));
1410 } // end processing each face
1412 //TopoDS_Shape aResult = Shape;
1413 if (NbModif > 0 && !hasFailed) {
1414 //TopoDS_Shape aResult = aContext->Apply(aShell);
1415 TopoDS_Shape aResult = myContext->Apply(aShell);
1418 if (!myContext.IsNull()) sfe.SetContext(myContext);
1419 for (exp.Init(aResult,TopAbs_EDGE); exp.More(); exp.Next()) {
1420 TopoDS_Edge E = TopoDS::Edge(exp.Current());
1421 sfe.FixVertexTolerance (E);
1422 // ptv add fix same parameter
1423 sfe.FixSameParameter(E, Precision::Confusion());
1426 myContext->Replace(aShell, aResult);
1429 if (!myOldNewMap.IsBound(aShell))
1431 TopTools_ListOfShape EmptyList;
1432 myOldNewMap.Bind(aShell, EmptyList);
1434 myOldNewMap(aShell).Clear();
1435 myOldNewMap(aShell).Append(aResult);
1441 for (exp.Init(aShell, TopAbs_FACE); exp.More(); exp.Next()) {
1442 TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
1443 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
1444 sfw->SetContext(myContext);
1445 sfw->SetPrecision(Precision::Confusion());
1446 sfw->SetMinTolerance(Precision::Confusion());
1447 sfw->SetMaxTolerance(1.);
1448 sfw->SetFace(aFace);
1449 for (TopoDS_Iterator iter (aFace,Standard_False); iter.More(); iter.Next()) {
1450 TopoDS_Wire wire = TopoDS::Wire(iter.Value());
1457 } // end processing each shell
1459 myShape = myContext->Apply(myShape);
1462 //=======================================================================
1463 //function : UnifyEdges
1465 //=======================================================================
1466 void ShapeUpgrade_UnifySameDomain::UnifyEdges()
1468 Standard_Real Tol = Precision::Confusion();
1470 //Handle(ShapeBuild_ReShape) myContext = new ShapeBuild_ReShape;
1471 Standard_Real myTolerance = Precision::Confusion();
1472 TopoDS_Shape aResult = myContext->Apply(myShape);
1474 TopTools_IndexedMapOfShape ChangedFaces;
1476 // creating map of edge faces
1477 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
1478 TopExp::MapShapesAndAncestors(myShape, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
1480 // creating map of vertex edges
1481 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgesVertex;
1482 TopExp::MapShapesAndAncestors(myShape, TopAbs_VERTEX, TopAbs_EDGE, aMapEdgesVertex);
1484 //Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
1485 TopoDS_Shape aRes = myShape;
1486 //aRes = aContext->Apply(aSolid);
1487 aRes = myContext->Apply(myShape);
1489 TopTools_MapOfShape SharedVert;
1492 TopTools_IndexedMapOfShape anOldEdges;
1493 TopExp::MapShapes(myInitShape, TopAbs_EDGE, anOldEdges);
1495 TopTools_DataMapOfShapeShape NewEdges2OldEdges;
1496 for (int i = 1; i <= anOldEdges.Extent(); i++)
1498 TopoDS_Shape NewEdge = myContext->Apply(anOldEdges(i));
1499 if (!NewEdge.IsNull())
1500 NewEdges2OldEdges.Bind(NewEdge, anOldEdges(i));
1503 TopExp_Explorer exp;
1504 // processing separate wires
1505 for (exp.Init(aRes, TopAbs_WIRE, TopAbs_FACE); exp.More(); exp.Next())
1507 TopTools_SequenceOfShape SeqEdges;
1508 TopExp_Explorer expE(exp.Current(), TopAbs_EDGE);
1509 for (; expE.More(); expE.Next())
1510 SeqEdges.Append(expE.Current());
1512 CheckSharedVertices(SeqEdges, aMapEdgesVertex, SharedVert);
1513 MergeSeq(SeqEdges, Tol, myConcatBSplines, myContext, myOldShapes, SharedVert, NewEdges2OldEdges);
1516 // processing each face
1517 for (exp.Init(aRes, TopAbs_FACE); exp.More(); exp.Next()) {
1518 //TopoDS_Face aFace = TopoDS::Face(aContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
1519 TopoDS_Face aFace = TopoDS::Face(myContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
1520 TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
1521 TopTools_SequenceOfShape aNonSharedEdges;
1522 for (TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
1523 TopoDS_Edge edge = TopoDS::Edge(expe.Current());
1524 if (!aMapEdgeFaces.Contains(edge)) continue;
1525 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
1526 TopTools_ListIteratorOfListOfShape anIter(aList);
1527 Standard_Integer NbFacesPerEdge = aList.Extent();
1528 for ( ; anIter.More(); anIter.Next()) {
1529 TopoDS_Face face = TopoDS::Face(anIter.Value());
1530 TopoDS_Face face1 = TopoDS::Face(myContext->Apply(anIter.Value()));
1531 if (face1.IsSame(aFace) && NbFacesPerEdge != 1)
1533 if (NbFacesPerEdge == 1)
1534 //store non-shared edges separately
1535 aNonSharedEdges.Append(edge);
1538 if (aMapFacesEdges.Contains(face))
1539 aMapFacesEdges.ChangeFromKey(face).Append(edge);
1542 TopTools_ListOfShape ListEdges;
1543 ListEdges.Append(edge);
1544 aMapFacesEdges.Add(face,ListEdges);
1550 for (Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++)
1552 const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
1553 TopTools_SequenceOfShape SeqEdges;
1554 TopTools_ListIteratorOfListOfShape anIter(ListEdges);
1555 for ( ; anIter.More(); anIter.Next())
1556 SeqEdges.Append(anIter.Value());
1557 if (SeqEdges.Length()==1)
1561 CheckSharedVertices(SeqEdges, aMapEdgesVertex, SharedVert);
1562 //if (!SharedVert.IsEmpty())
1564 if ( MergeSeq(SeqEdges,Tol,myConcatBSplines, myContext, myOldShapes, SharedVert, NewEdges2OldEdges) )
1568 for (j = 1; j <= SeqEdges.Length(); j++)
1570 if (!myOldNewMap.IsBound(SeqEdges(j)))
1572 TopTools_ListOfShape EmptyList;
1573 myOldNewMap.Bind(SeqEdges(j), EmptyList);
1575 myOldNewMap(SeqEdges(j)).Clear();
1576 myOldNewMap(SeqEdges(j)).Append(E);
1581 TopoDS_Face tmpF = TopoDS::Face(exp.Current());
1582 if ( !ChangedFaces.Contains(tmpF) )
1583 ChangedFaces.Add(tmpF);
1584 tmpF = TopoDS::Face(aMapFacesEdges.FindKey(i));
1585 if ( !ChangedFaces.Contains(tmpF) )
1586 ChangedFaces.Add(tmpF);
1590 if ( aNonSharedEdges.Length() > 1 )
1593 CheckSharedVertices(aNonSharedEdges, aMapEdgesVertex, SharedVert);
1594 if ( MergeSeq(aNonSharedEdges, Tol, myConcatBSplines, myContext, myOldShapes, SharedVert, NewEdges2OldEdges) )
1596 TopoDS_Face tmpF = TopoDS::Face(exp.Current());
1597 if ( !ChangedFaces.Contains(tmpF) )
1598 ChangedFaces.Add(tmpF);
1602 } // end processing each face
1604 // fix changed faces and replace them in the local context
1605 for (Standard_Integer i=1; i<=ChangedFaces.Extent(); i++) {
1606 //TopoDS_Face aFace = TopoDS::Face(aContext->Apply(ChangedFaces.FindKey(i)));
1607 TopoDS_Face aFace = TopoDS::Face(myContext->Apply(ChangedFaces.FindKey(i)));
1610 Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFace);
1611 sff->SetContext(myContext);
1612 sff->SetPrecision(myTolerance);
1613 sff->SetMinTolerance(myTolerance);
1614 sff->SetMaxTolerance(Max(1.,myTolerance*1000.));
1616 TopoDS_Shape aNewFace = sff->Face();
1617 //aContext->Replace(aFace,aNewFace);
1618 myContext->Replace(aFace,aNewFace);
1621 if (!myOldNewMap.IsBound(aFace))
1623 TopTools_ListOfShape EmptyList;
1624 myOldNewMap.Bind(aFace, EmptyList);
1626 myOldNewMap(aFace).Clear();
1627 myOldNewMap(aFace).Append(aNewFace);
1632 if (ChangedFaces.Extent() > 0) {
1633 // fix changed shell and replace it in the local context
1634 //TopoDS_Shape aRes1 = aContext->Apply(aRes);
1635 TopoDS_Shape aRes1 = myContext->Apply(aRes);
1636 TopExp_Explorer expsh;
1637 for (expsh.Init(aRes1, TopAbs_SHELL); expsh.More(); expsh.Next()) {
1638 TopoDS_Shell aShell = TopoDS::Shell(expsh.Current());
1639 Handle(ShapeFix_Shell) sfsh = new ShapeFix_Shell;
1640 sfsh->FixFaceOrientation(aShell);
1641 TopoDS_Shape aNewShell = sfsh->Shell();
1642 //aContext->Replace(aShell,aNewShell);
1643 myContext->Replace(aShell,aNewShell);
1646 if (!myOldNewMap.IsBound(aShell))
1648 TopTools_ListOfShape EmptyList;
1649 myOldNewMap.Bind(aShell, EmptyList);
1651 myOldNewMap(aShell).Clear();
1652 myOldNewMap(aShell).Append(aNewShell);
1656 //TopoDS_Shape aRes2 = aContext->Apply(aRes1);
1657 TopoDS_Shape aRes2 = myContext->Apply(aRes1);
1658 myContext->Replace(myShape,aRes2);
1661 if (!myOldNewMap.IsBound(aSolid))
1663 TopTools_ListOfShape EmptyList;
1664 myOldNewMap.Bind(aSolid, EmptyList);
1666 myOldNewMap(aSolid).Clear();
1667 myOldNewMap(aSolid).Append(aRes2);
1672 myShape = myContext->Apply(myShape);
1675 //=======================================================================
1676 //function : UnifyFacesAndEdges
1678 //=======================================================================
1680 void ShapeUpgrade_UnifySameDomain::UnifyFacesAndEdges()
1685 ShapeUpgrade_RemoveLocations RemLoc;
1686 RemLoc.Remove(myShape);
1687 myShape = RemLoc.GetResult();
1693 //=======================================================================
1695 //purpose : builds the resulting shape
1696 //======================================================================
1697 void ShapeUpgrade_UnifySameDomain::Build()
1699 if (myUnifyFaces && myUnifyEdges)
1700 UnifyFacesAndEdges();
1702 else if (myUnifyEdges)
1704 else if (myUnifyFaces)
1710 //=======================================================================
1712 //purpose : give the resulting shape
1713 //=======================================================================
1714 const TopoDS_Shape& ShapeUpgrade_UnifySameDomain::Shape() const
1719 //=======================================================================
1720 //function : Generated
1721 //purpose : returns the new shape from the old one
1722 //=======================================================================
1723 TopoDS_Shape ShapeUpgrade_UnifySameDomain::Generated(const TopoDS_Shape& aShape) const
1725 TopoDS_Shape aNewShape = myContext->Apply(aShape);
1727 if (aNewShape.IsNull())
1728 aNewShape = myContext->Apply(myOldShapes(aShape));