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>
89 #include <BRepAdaptor_Curve2d.hxx>
90 #include <gp_Vec2d.hxx>
92 IMPLEMENT_STANDARD_RTTIEXT(ShapeUpgrade_UnifySameDomain,MMgt_TShared)
94 struct SubSequenceOfEdges
96 TopTools_SequenceOfShape SeqsEdges;
97 TopoDS_Edge UnionEdges;
100 static Standard_Boolean IsLikeSeam(const TopoDS_Edge& anEdge,
101 const TopoDS_Face& aFace,
102 const Handle(Geom_Surface)& aBaseSurface)
104 if (!aBaseSurface->IsUPeriodic() && !aBaseSurface->IsVPeriodic())
105 return Standard_False;
107 BRepAdaptor_Curve2d BAcurve2d(anEdge, aFace);
108 gp_Pnt2d FirstPoint, LastPoint;
109 gp_Vec2d FirstDir, LastDir;
110 BAcurve2d.D1(BAcurve2d.FirstParameter(), FirstPoint, FirstDir);
111 BAcurve2d.D1(BAcurve2d.LastParameter(), LastPoint, LastDir);
112 Standard_Real Length = FirstDir.Magnitude();
113 if (Length <= gp::Resolution())
114 return Standard_False;
117 Length = LastDir.Magnitude();
118 if (Length <= gp::Resolution())
119 return Standard_False;
123 Standard_Real Tol = 1.e-7;
124 if (aBaseSurface->IsUPeriodic() &&
125 (Abs(FirstDir.X()) < Tol) &&
126 (Abs(LastDir.X()) < Tol))
127 return Standard_True;
129 if (aBaseSurface->IsVPeriodic() &&
130 (Abs(FirstDir.Y()) < Tol) &&
131 (Abs(LastDir.Y()) < Tol))
132 return Standard_True;
134 return Standard_False;
137 static Standard_Boolean CheckSharedEdgeOri(const TopoDS_Face& theF1,
138 const TopoDS_Face& theF2,
139 const TopoDS_Edge& theE)
141 TopAbs_Orientation anEOri = theE.Orientation();
142 if (anEOri == TopAbs_EXTERNAL || anEOri == TopAbs_INTERNAL)
143 return Standard_False;
145 TopExp_Explorer Exp(theF1, TopAbs_EDGE);
146 for (;Exp.More();Exp.Next())
148 const TopoDS_Shape& aCE = Exp.Current();
149 if (aCE.IsSame(theE))
151 anEOri = aCE.Orientation();
156 for (Exp.Init(theF2, TopAbs_EDGE);Exp.More();Exp.Next())
158 const TopoDS_Shape& aCE = Exp.Current();
159 if (aCE.IsSame(theE))
161 if (aCE.Orientation() == TopAbs::Reverse(anEOri))
162 return Standard_True;
164 return Standard_False;
168 return Standard_False;
172 //=======================================================================
173 //function : AddOrdinaryEdges
175 //=======================================================================
176 // adds edges from the shape to the sequence
177 // seams and equal edges are dropped
178 // Returns true if one of original edges dropped
179 static Standard_Boolean AddOrdinaryEdges(TopTools_SequenceOfShape& edges,
180 const TopoDS_Shape aShape,
181 Standard_Integer& anIndex)
184 TopTools_IndexedMapOfShape aNewEdges;
185 //add edges without seams
186 for(TopExp_Explorer exp(aShape,TopAbs_EDGE); exp.More(); exp.Next()) {
187 TopoDS_Shape edge = exp.Current();
188 if(aNewEdges.Contains(edge))
190 //aNewEdges.Remove(edge);
191 TopoDS_Shape LastEdge = aNewEdges(aNewEdges.Extent());
192 aNewEdges.RemoveLast();
193 if (aNewEdges.FindIndex(edge) != 0)
194 aNewEdges.Substitute(aNewEdges.FindIndex(edge), LastEdge);
195 /////////////////////////
201 Standard_Boolean isDropped = Standard_False;
202 //merge edges and drop seams
204 for (i = 1; i <= edges.Length(); i++) {
205 TopoDS_Shape current = edges(i);
206 if(aNewEdges.Contains(current)) {
208 //aNewEdges.Remove(current);
209 TopoDS_Shape LastEdge = aNewEdges(aNewEdges.Extent());
210 aNewEdges.RemoveLast();
211 if (aNewEdges.FindIndex(current) != 0)
212 aNewEdges.Substitute(aNewEdges.FindIndex(current), LastEdge);
213 /////////////////////////
218 isDropped = Standard_True;
224 //add edges to the sequence
225 for (i = 1; i <= aNewEdges.Extent(); i++)
226 edges.Append(aNewEdges(i));
231 //=======================================================================
232 //function : getCylinder
234 //=======================================================================
235 static Standard_Boolean getCylinder(Handle(Geom_Surface)& theInSurface,
236 gp_Cylinder& theOutCylinder)
238 Standard_Boolean isCylinder = Standard_False;
240 if (theInSurface->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
241 Handle(Geom_CylindricalSurface) aGC = Handle(Geom_CylindricalSurface)::DownCast(theInSurface);
243 theOutCylinder = aGC->Cylinder();
244 isCylinder = Standard_True;
246 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution))) {
247 Handle(Geom_SurfaceOfRevolution) aRS =
248 Handle(Geom_SurfaceOfRevolution)::DownCast(theInSurface);
249 Handle(Geom_Curve) aBasis = aRS->BasisCurve();
250 if (aBasis->IsKind(STANDARD_TYPE(Geom_Line))) {
251 Handle(Geom_Line) aBasisLine = Handle(Geom_Line)::DownCast(aBasis);
252 gp_Dir aDir = aRS->Direction();
253 gp_Dir aBasisDir = aBasisLine->Position().Direction();
254 if (aBasisDir.IsParallel(aDir, Precision::Angular())) {
255 // basis line is parallel to the revolution axis: it is a cylinder
256 gp_Pnt aLoc = aRS->Location();
257 Standard_Real aR = aBasisLine->Lin().Distance(aLoc);
258 gp_Ax3 aCylAx (aLoc, aDir);
260 theOutCylinder = gp_Cylinder(aCylAx, aR);
261 isCylinder = Standard_True;
265 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion))) {
266 Handle(Geom_SurfaceOfLinearExtrusion) aLES =
267 Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(theInSurface);
268 Handle(Geom_Curve) aBasis = aLES->BasisCurve();
269 if (aBasis->IsKind(STANDARD_TYPE(Geom_Circle))) {
270 Handle(Geom_Circle) aBasisCircle = Handle(Geom_Circle)::DownCast(aBasis);
271 gp_Dir aDir = aLES->Direction();
272 gp_Dir aBasisDir = aBasisCircle->Position().Direction();
273 if (aBasisDir.IsParallel(aDir, Precision::Angular())) {
274 // basis circle is normal to the extrusion axis: it is a cylinder
275 gp_Pnt aLoc = aBasisCircle->Location();
276 Standard_Real aR = aBasisCircle->Radius();
277 gp_Ax3 aCylAx (aLoc, aDir);
279 theOutCylinder = gp_Cylinder(aCylAx, aR);
280 isCylinder = Standard_True;
290 //=======================================================================
291 //function : ClearRts
293 //=======================================================================
294 static Handle(Geom_Surface) ClearRts(const Handle(Geom_Surface)& aSurface)
296 if(aSurface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
297 Handle(Geom_RectangularTrimmedSurface) rts =
298 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
299 return rts->BasisSurface();
304 //=======================================================================
305 //function : GetNormalToSurface
306 //purpose : Gets the normal to surface by the given parameter on edge.
307 // Returns True if normal was computed.
308 //=======================================================================
309 static Standard_Boolean GetNormalToSurface(const TopoDS_Face& theFace,
310 const TopoDS_Edge& theEdge,
311 const Standard_Real theP,
315 // get 2d curve to get point in 2d
316 const Handle(Geom2d_Curve)& aC2d = BRep_Tool::CurveOnSurface(theEdge, theFace, f, l);
318 return Standard_False;
323 aC2d->D0(theP, aP2d);
328 TopLoc_Location aLoc;
329 const Handle(Geom_Surface)& aS = BRep_Tool::Surface(theFace, aLoc);
330 aS->D1(aP2d.X(), aP2d.Y(), aP3d, aDU, aDV);
333 gp_Vec aVNormal = aDU.Crossed(aDV);
334 if (aVNormal.Magnitude() < Precision::Confusion()) {
335 return Standard_False;
338 if (theFace.Orientation() == TopAbs_REVERSED) {
342 aVNormal.Transform(aLoc.Transformation());
343 theNormal = gp_Dir(aVNormal);
344 return Standard_True;
347 //=======================================================================
348 //function : IsSameDomain
350 //=======================================================================
351 static Standard_Boolean IsSameDomain(const TopoDS_Face& aFace,
352 const TopoDS_Face& aCheckedFace,
353 const Standard_Real theLinTol,
354 const Standard_Real theAngTol)
356 //checking the same handles
357 TopLoc_Location L1, L2;
358 Handle(Geom_Surface) S1, S2;
360 S1 = BRep_Tool::Surface(aFace,L1);
361 S2 = BRep_Tool::Surface(aCheckedFace,L2);
363 if (S1 == S2 && L1 == L2)
364 return Standard_True;
366 S1 = BRep_Tool::Surface(aFace);
367 S2 = BRep_Tool::Surface(aCheckedFace);
372 //Handle(Geom_OffsetSurface) aGOFS1, aGOFS2;
373 //aGOFS1 = Handle(Geom_OffsetSurface)::DownCast(S1);
374 //aGOFS2 = Handle(Geom_OffsetSurface)::DownCast(S2);
375 //if (!aGOFS1.IsNull()) S1 = aGOFS1->BasisSurface();
376 //if (!aGOFS2.IsNull()) S2 = aGOFS2->BasisSurface();
378 // case of two planar surfaces:
379 // all kinds of surfaces checked, including b-spline and bezier
380 GeomLib_IsPlanarSurface aPlanarityChecker1(S1, theLinTol);
381 if (aPlanarityChecker1.IsPlanar()) {
382 GeomLib_IsPlanarSurface aPlanarityChecker2(S2, theLinTol);
383 if (aPlanarityChecker2.IsPlanar()) {
384 gp_Pln aPln1 = aPlanarityChecker1.Plan();
385 gp_Pln aPln2 = aPlanarityChecker2.Plan();
387 if (aPln1.Position().Direction().IsParallel(aPln2.Position().Direction(), theAngTol) &&
388 aPln1.Distance(aPln2) < theLinTol) {
389 return Standard_True;
394 // case of two elementary surfaces: use OCCT tool
395 // elementary surfaces: ConicalSurface, CylindricalSurface,
396 // Plane, SphericalSurface and ToroidalSurface
397 if (S1->IsKind(STANDARD_TYPE(Geom_ElementarySurface)) &&
398 S2->IsKind(STANDARD_TYPE(Geom_ElementarySurface)))
400 Handle(GeomAdaptor_HSurface) aGA1 = new GeomAdaptor_HSurface(S1);
401 Handle(GeomAdaptor_HSurface) aGA2 = new GeomAdaptor_HSurface(S2);
403 Handle(BRepTopAdaptor_TopolTool) aTT1 = new BRepTopAdaptor_TopolTool();
404 Handle(BRepTopAdaptor_TopolTool) aTT2 = new BRepTopAdaptor_TopolTool();
407 IntPatch_ImpImpIntersection anIIInt(aGA1, aTT1, aGA2, aTT2, theLinTol, theLinTol);
408 if (!anIIInt.IsDone() || anIIInt.IsEmpty())
409 return Standard_False;
411 return anIIInt.TangentFaces();
413 catch (Standard_Failure) {
414 return Standard_False;
418 // case of two cylindrical surfaces, at least one of which is a swept surface
419 // swept surfaces: SurfaceOfLinearExtrusion, SurfaceOfRevolution
420 if ((S1->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
421 S1->IsKind(STANDARD_TYPE(Geom_SweptSurface))) &&
422 (S2->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
423 S2->IsKind(STANDARD_TYPE(Geom_SweptSurface))))
425 gp_Cylinder aCyl1, aCyl2;
426 if (getCylinder(S1, aCyl1) && getCylinder(S2, aCyl2)) {
427 if (fabs(aCyl1.Radius() - aCyl2.Radius()) < theLinTol) {
428 gp_Dir aDir1 = aCyl1.Position().Direction();
429 gp_Dir aDir2 = aCyl2.Position().Direction();
430 if (aDir1.IsParallel(aDir2, Precision::Angular())) {
431 gp_Pnt aLoc1 = aCyl1.Location();
432 gp_Pnt aLoc2 = aCyl2.Location();
433 gp_Vec aVec12 (aLoc1, aLoc2);
434 if (aVec12.SquareMagnitude() < theLinTol*theLinTol ||
435 aVec12.IsParallel(aDir1, Precision::Angular())) {
436 return Standard_True;
443 return Standard_False;
446 //=======================================================================
447 //function : MovePCurves
449 //=======================================================================
450 static void MovePCurves(TopoDS_Face& aTarget,
451 const TopoDS_Face& aSource)
454 for(TopExp_Explorer wexp(aSource,TopAbs_WIRE);wexp.More();wexp.Next()) {
455 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(TopoDS::Wire(wexp.Current()),
456 aTarget, Precision::Confusion());
458 Standard_Boolean isReoredFailed = sfw->StatusReorder ( ShapeExtend_FAIL );
459 sfw->FixEdgeCurves();
464 sfw->FixDegenerated();
466 // remove degenerated edges from not degenerated points
467 ShapeAnalysis_Edge sae;
468 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
469 for(Standard_Integer i = 1; i<=sewd->NbEdges();i++) {
470 TopoDS_Edge E = sewd->Edge(i);
471 if(BRep_Tool::Degenerated(E)&&!sae.HasPCurve(E,aTarget)) {
477 TopoDS_Wire ResWire = sfw->Wire();
478 B.Add(aTarget,ResWire);
482 //=======================================================================
483 //function : GlueEdgesWithPCurves
484 //purpose : Glues the pcurves of the sequence of edges
485 // and glues their 3d curves
486 //=======================================================================
487 static TopoDS_Edge GlueEdgesWithPCurves(const TopTools_SequenceOfShape& aChain,
488 const TopoDS_Vertex& FirstVertex,
489 const TopoDS_Vertex& LastVertex)
491 Standard_Integer i, j;
493 TopoDS_Edge FirstEdge = TopoDS::Edge(aChain(1));
494 //TColGeom2d_SequenceOfCurve PCurveSeq;
495 TColGeom_SequenceOfSurface SurfSeq;
496 //TopTools_SequenceOfShape LocSeq;
498 BRep_ListIteratorOfListOfCurveRepresentation itr( (Handle(BRep_TEdge)::DownCast(FirstEdge.TShape()))->Curves() );
499 for (; itr.More(); itr.Next())
501 Handle(BRep_CurveRepresentation) CurveRep = itr.Value();
502 if (CurveRep->IsCurveOnSurface())
504 //PCurveSeq.Append(CurveRep->PCurve());
505 SurfSeq.Append(CurveRep->Surface());
507 TopoDS_Shape aLocShape;
508 aLocShape.Location(CurveRep->Location());
509 LocSeq.Append(aLocShape);
514 Standard_Real fpar, lpar;
515 BRep_Tool::Range(FirstEdge, fpar, lpar);
516 TopoDS_Edge PrevEdge = FirstEdge;
518 Standard_Real MaxTol = 0.;
523 Standard_Integer nb_curve = aChain.Length(); //number of curves
524 TColGeom_Array1OfBSplineCurve tab_c3d(0,nb_curve-1); //array of the curves
525 TColStd_Array1OfReal tabtolvertex(0,nb_curve-1); //(0,nb_curve-2); //array of the tolerances
527 TopoDS_Vertex PrevVertex = FirstVertex;
528 for (i = 1; i <= nb_curve; i++)
530 TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
531 TopoDS_Vertex VF, VL;
532 TopExp::Vertices(anEdge, VF, VL);
533 Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
535 Standard_Real Tol1 = BRep_Tool::Tolerance(VF);
536 Standard_Real Tol2 = BRep_Tool::Tolerance(VL);
544 TopExp::CommonVertex(PrevEdge, anEdge, CV);
545 Standard_Real Tol = BRep_Tool::Tolerance(CV);
546 tabtolvertex(i-2) = Tol;
549 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, fpar, lpar);
550 Handle(Geom_TrimmedCurve) aTrCurve = new Geom_TrimmedCurve(aCurve, fpar, lpar);
551 tab_c3d(i-1) = GeomConvert::CurveToBSplineCurve(aTrCurve);
552 GeomConvert::C0BSplineToC1BSplineCurve(tab_c3d(i-1), Precision::Confusion());
554 tab_c3d(i-1)->Reverse();
555 PrevVertex = (ToReverse)? VF : VL;
558 Handle(TColGeom_HArray1OfBSplineCurve) concatcurve; //array of the concatenated curves
559 Handle(TColStd_HArray1OfInteger) ArrayOfIndices; //array of the remining Vertex
560 GeomConvert::ConcatC1(tab_c3d,
565 Precision::Confusion()); //C1 concatenation
567 if (concatcurve->Length() > 1)
569 GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
571 for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
572 Concat.Add( concatcurve->Value(i), MaxTol, Standard_True );
574 concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
576 Handle(Geom_BSplineCurve) ResCurve = concatcurve->Value(concatcurve->Lower());
578 TColGeom2d_SequenceOfBoundedCurve ResPCurves;
579 TopLoc_Location aLoc;
580 for (j = 1; j <= SurfSeq.Length(); j++)
582 TColGeom2d_Array1OfBSplineCurve tab_c2d(0,nb_curve-1); //array of the pcurves
584 PrevVertex = FirstVertex;
585 PrevEdge = FirstEdge;
586 //TopLoc_Location theLoc = LocSeq(j).Location();
587 for (i = 1; i <= nb_curve; i++)
589 TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
590 TopoDS_Vertex VF, VL;
591 TopExp::Vertices(anEdge, VF, VL);
592 Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
595 Handle(Geom2d_Curve) aPCurve =
596 BRep_Tool::CurveOnSurface(anEdge, SurfSeq(j), anEdge.Location()*theLoc, fpar, lpar);
598 Handle(Geom2d_Curve) aPCurve =
599 BRep_Tool::CurveOnSurface(anEdge, SurfSeq(j), aLoc, fpar, lpar);
600 Handle(Geom2d_TrimmedCurve) aTrPCurve = new Geom2d_TrimmedCurve(aPCurve, fpar, lpar);
601 tab_c2d(i-1) = Geom2dConvert::CurveToBSplineCurve(aTrPCurve);
602 Geom2dConvert::C0BSplineToC1BSplineCurve(tab_c2d(i-1), Precision::Confusion());
604 tab_c2d(i-1)->Reverse();
605 PrevVertex = (ToReverse)? VF : VL;
608 Handle(TColGeom2d_HArray1OfBSplineCurve) concatc2d; //array of the concatenated curves
609 Handle(TColStd_HArray1OfInteger) ArrayOfInd2d; //array of the remining Vertex
610 Geom2dConvert::ConcatC1(tab_c2d,
615 Precision::Confusion()); //C1 concatenation
617 if (concatc2d->Length() > 1)
619 Geom2dConvert_CompCurveToBSplineCurve Concat2d(concatc2d->Value(concatc2d->Lower()));
621 for (i = concatc2d->Lower()+1; i <= concatc2d->Upper(); i++)
622 Concat2d.Add( concatc2d->Value(i), MaxTol, Standard_True );
624 concatc2d->SetValue(concatc2d->Lower(), Concat2d.BSplineCurve());
626 Handle(Geom2d_BSplineCurve) aResPCurve = concatc2d->Value(concatc2d->Lower());
627 ResPCurves.Append(aResPCurve);
630 ResEdge = BRepLib_MakeEdge(ResCurve,
631 FirstVertex, LastVertex,
632 ResCurve->FirstParameter(), ResCurve->LastParameter());
633 BB.SameRange(ResEdge, Standard_False);
634 BB.SameParameter(ResEdge, Standard_False);
635 for (j = 1; j <= ResPCurves.Length(); j++)
637 BB.UpdateEdge(ResEdge, ResPCurves(j), SurfSeq(j), aLoc, MaxTol);
638 BB.Range(ResEdge, SurfSeq(j), aLoc, ResPCurves(j)->FirstParameter(), ResPCurves(j)->LastParameter());
641 BRepLib::SameParameter(ResEdge, MaxTol, Standard_True);
646 //=======================================================================
647 //function : MergeSubSeq
648 //purpose : Merges a sequence of edges into one edge if possible
649 //=======================================================================
651 static Standard_Boolean MergeSubSeq(const TopTools_SequenceOfShape& aChain, TopoDS_Edge& OutEdge, double Tol, Standard_Boolean ConcatBSplines)
653 ShapeAnalysis_Edge sae;
655 // union edges in chain
657 Standard_Real fp1,lp1,fp2,lp2;
658 Standard_Boolean IsUnionOfLinesPossible = Standard_True;
659 Standard_Boolean IsUnionOfCirclesPossible = Standard_True;
660 Handle(Geom_Curve) c3d1, c3d2;
661 for(j=1; j<aChain.Length(); j++)
663 TopoDS_Edge edge1 = TopoDS::Edge(aChain.Value(j));
664 c3d1 = BRep_Tool::Curve(edge1,fp1,lp1);
666 TopoDS_Edge edge2 = TopoDS::Edge(aChain.Value(j+1));
667 c3d2 = BRep_Tool::Curve(edge2,fp2,lp2);
669 if(c3d1.IsNull() || c3d2.IsNull())
670 return Standard_False;
672 while(c3d1->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
673 Handle(Geom_TrimmedCurve) tc =
674 Handle(Geom_TrimmedCurve)::DownCast(c3d1);
675 c3d1 = tc->BasisCurve();
677 while(c3d2->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
678 Handle(Geom_TrimmedCurve) tc =
679 Handle(Geom_TrimmedCurve)::DownCast(c3d2);
680 c3d2 = tc->BasisCurve();
682 if( c3d1->IsKind(STANDARD_TYPE(Geom_Line)) && c3d2->IsKind(STANDARD_TYPE(Geom_Line)) ) {
683 Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(c3d1);
684 Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(c3d2);
685 gp_Dir Dir1 = L1->Position().Direction();
686 gp_Dir Dir2 = L2->Position().Direction();
687 if(!Dir1.IsParallel(Dir2,Tol))
688 IsUnionOfLinesPossible = Standard_False;
691 IsUnionOfLinesPossible = Standard_False;
692 if( c3d1->IsKind(STANDARD_TYPE(Geom_Circle)) && c3d2->IsKind(STANDARD_TYPE(Geom_Circle)) ) {
693 Handle(Geom_Circle) C1 = Handle(Geom_Circle)::DownCast(c3d1);
694 Handle(Geom_Circle) C2 = Handle(Geom_Circle)::DownCast(c3d2);
695 gp_Pnt P01 = C1->Location();
696 gp_Pnt P02 = C2->Location();
697 if (P01.Distance(P02) > Precision::Confusion())
698 IsUnionOfCirclesPossible = Standard_False;
701 IsUnionOfCirclesPossible = Standard_False;
703 if (IsUnionOfLinesPossible && IsUnionOfCirclesPossible)
704 return Standard_False;
706 //union of lines is possible
707 if (IsUnionOfLinesPossible)
709 TopoDS_Vertex V1 = sae.FirstVertex(TopoDS::Edge(aChain.First()));
710 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
711 TopoDS_Vertex V2 = sae.LastVertex(TopoDS::Edge(aChain.Last()));
712 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
713 gp_Vec Vec(PV1, PV2);
714 Handle(Geom_Line) L = new Geom_Line(gp_Ax1(PV1,Vec));
715 Standard_Real dist = PV1.Distance(PV2);
716 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(L,0.0,dist);
718 B.MakeEdge (E, tc ,Precision::Confusion());
719 B.Add (E,V1); B.Add (E,V2);
720 B.UpdateVertex(V1, 0., E, 0.);
721 B.UpdateVertex(V2, dist, E, 0.);
723 return Standard_True;
726 if (IsUnionOfCirclesPossible)
729 TopoDS_Edge FE = TopoDS::Edge(aChain.First());
730 Handle(Geom_Curve) c3d = BRep_Tool::Curve(FE,f,l);
732 while(c3d->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
733 Handle(Geom_TrimmedCurve) tc =
734 Handle(Geom_TrimmedCurve)::DownCast(c3d);
735 c3d = tc->BasisCurve();
737 Handle(Geom_Circle) Cir = Handle(Geom_Circle)::DownCast(c3d);
739 TopoDS_Vertex V1 = sae.FirstVertex(FE);
740 TopoDS_Vertex V2 = sae.LastVertex(TopoDS::Edge(aChain.Last()));
744 BRepAdaptor_Curve adef(FE);
745 Handle(Geom_Circle) Cir1;
747 if ( FE.Orientation() == TopAbs_FORWARD)
749 FP = adef.FirstParameter();
750 LP = adef.LastParameter();
754 FP = adef.LastParameter();
755 LP = adef.FirstParameter();
757 if (Abs(FP) < Precision::PConfusion())
759 B.MakeEdge (E,Cir, Precision::Confusion());
762 E.Orientation(FE.Orientation());
766 GC_MakeCircle MC1 (adef.Value(FP), adef.Value((FP + LP) * 0.5), adef.Value(LP));
770 return Standard_False;
771 B.MakeEdge (E, Cir1, Precision::Confusion());
777 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
778 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
779 TopoDS_Vertex VM = sae.LastVertex(FE);
780 gp_Pnt PVM = BRep_Tool::Pnt(VM);
781 GC_MakeCircle MC (PV1,PVM,PV2);
782 Handle(Geom_Circle) C = MC.Value();
783 gp_Pnt P0 = C->Location();
784 gp_Dir D1(gp_Vec(P0,PV1));
785 gp_Dir D2(gp_Vec(P0,PV2));
786 Standard_Real fpar = C->XAxis().Direction().Angle(D1);
787 if(fabs(fpar)>Precision::Confusion()) {
789 gp_Dir ND = C->XAxis().Direction().Crossed(D1);
790 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
794 Standard_Real lpar = C->XAxis().Direction().Angle(D2);
795 if(fabs(lpar)>Precision::Confusion()) {
797 gp_Dir ND = C->XAxis().Direction().Crossed(D2);
798 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
802 if (lpar < fpar) lpar += 2*M_PI;
803 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
804 B.MakeEdge (E,tc,Precision::Confusion());
807 B.UpdateVertex(V1, fpar, E, 0.);
808 B.UpdateVertex(V2, lpar, E, 0.);
811 return Standard_True;
813 if (aChain.Length() > 1 && ConcatBSplines) {
814 // second step: union edges with various curves
815 // skl for bug 0020052 from Mantis: perform such unions
816 // only if curves are bspline or bezier
818 TopoDS_Vertex VF = sae.FirstVertex(TopoDS::Edge(aChain.First()));
819 TopoDS_Vertex VL = sae.LastVertex(TopoDS::Edge(aChain.Last()));
820 Standard_Boolean NeedUnion = Standard_True;
821 for(j=1; j<=aChain.Length(); j++) {
822 TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
824 Handle(Geom_Curve) c3d = BRep_Tool::Curve(edge,Loc,fp1,lp1);
825 if(c3d.IsNull()) continue;
826 while(c3d->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
827 Handle(Geom_TrimmedCurve) tc =
828 Handle(Geom_TrimmedCurve)::DownCast(c3d);
829 c3d = tc->BasisCurve();
831 if( ( c3d->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ||
832 c3d->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ) ) continue;
833 NeedUnion = Standard_False;
838 cout<<"can not make analitical union => make approximation"<<endl;
840 TopoDS_Edge E = GlueEdgesWithPCurves(aChain, VF, VL);
842 return Standard_True;
846 cout<<"can not make approximation for such types of curves"<<endl;
848 return Standard_False;
851 return Standard_False;
854 //=======================================================================
855 //function : IsMergingPossible
856 //purpose : Checks if merging of two edges is possible
857 //=======================================================================
859 static Standard_Boolean IsMergingPossible(const TopoDS_Edge& edge1, const TopoDS_Edge& edge2,
860 double Tol, const TopTools_MapOfShape& AvoidEdgeVrt)
862 TopoDS_Vertex CV = TopExp::LastVertex(edge1, Standard_True);
863 if (CV.IsNull() || AvoidEdgeVrt.Contains(CV))
864 return Standard_False;
866 BRepAdaptor_Curve ade1(edge1);
867 BRepAdaptor_Curve ade2(edge2);
869 GeomAbs_CurveType t1 = ade1.GetType();
870 GeomAbs_CurveType t2 = ade2.GetType();
872 if( t1 == GeomAbs_Circle && t2 == GeomAbs_Circle)
874 if (ade1.Circle().Location().Distance(ade2.Circle().Location()) > Precision::Confusion())
875 return Standard_False;
878 if( ( (t1 != GeomAbs_BezierCurve && t1 != GeomAbs_BSplineCurve) ||
879 (t2 != GeomAbs_BezierCurve && t2 != GeomAbs_BSplineCurve)) && t1 != t2)
880 return Standard_False;
884 if (edge1.Orientation() == TopAbs_FORWARD)
885 ade1.D1(ade1.LastParameter(), P1, Diff1);
888 ade1.D1(ade1.FirstParameter(), P1, Diff1);
892 if (edge2.Orientation() == TopAbs_FORWARD)
893 ade2.D1(ade2.FirstParameter(), P2, Diff2);
896 ade2.D1(ade2.LastParameter(), P2, Diff2);
900 if (Diff1.Angle(Diff2) > Tol)
901 return Standard_False;
903 return Standard_True;
906 //=======================================================================
907 //function : GenerateSubSeq
908 //purpose : Generates sub-sequences of edges from sequence of edges
909 //Edges from each subsequences can be merged into the one edge
910 //=======================================================================
912 static void GenerateSubSeq (const TopTools_SequenceOfShape& anInpEdgeSeq,
913 NCollection_Sequence<SubSequenceOfEdges>& SeqOfSubSeqOfEdges,
914 Standard_Boolean IsClosed, double Tol, const TopTools_MapOfShape& AvoidEdgeVrt)
916 Standard_Boolean isOk = Standard_False;
917 TopoDS_Edge edge1, edge2;
919 SubSequenceOfEdges SubSeq;
920 SubSeq.SeqsEdges.Append(TopoDS::Edge(anInpEdgeSeq(1)));
921 SeqOfSubSeqOfEdges.Append(SubSeq);
923 for (int i = 1; i < anInpEdgeSeq.Length(); i++)
925 edge1 = TopoDS::Edge(anInpEdgeSeq(i));
926 edge2 = TopoDS::Edge(anInpEdgeSeq(i+1));
927 isOk = IsMergingPossible(edge1, edge2, Tol, AvoidEdgeVrt);
930 SubSequenceOfEdges aSubSeq;
931 aSubSeq.SeqsEdges.Append(edge2);
932 SeqOfSubSeqOfEdges.Append(aSubSeq);
935 SeqOfSubSeqOfEdges.ChangeLast().SeqsEdges.Append(edge2);
937 /// check first and last chain segments
938 if (IsClosed && SeqOfSubSeqOfEdges.Length() > 1)
940 edge1 = TopoDS::Edge(anInpEdgeSeq.Last());
941 edge2 = TopoDS::Edge(anInpEdgeSeq.First());
942 if (IsMergingPossible(edge1, edge2, Tol, AvoidEdgeVrt))
944 SeqOfSubSeqOfEdges.ChangeLast().SeqsEdges.Append(SeqOfSubSeqOfEdges.ChangeFirst().SeqsEdges);
945 SeqOfSubSeqOfEdges.Remove(1);
950 //=======================================================================
951 //function : MergeEdges
953 //=======================================================================
954 static Standard_Boolean MergeEdges(TopTools_SequenceOfShape& SeqEdges,
955 const Standard_Real Tol,
956 const Standard_Boolean ConcatBSplines,
957 NCollection_Sequence<SubSequenceOfEdges>& SeqOfSubSeqOfEdges,
958 const TopTools_MapOfShape& NonMergVrt)
960 // skip degenerated edges, and forbid merging through them
961 TopTools_IndexedDataMapOfShapeListOfShape aMapVE;
963 TopTools_MapOfShape VerticesToAvoid;
964 for (j = 1; j <= SeqEdges.Length(); j++)
966 TopoDS_Edge anEdge = TopoDS::Edge(SeqEdges(j));
967 if (BRep_Tool::Degenerated(anEdge))
969 TopoDS_Vertex V1, V2;
970 TopExp::Vertices(anEdge, V1, V2);
971 VerticesToAvoid.Add(V1);
972 VerticesToAvoid.Add(V2);
973 SeqEdges.Remove(j--);
977 // fill in the map V-E
978 for (TopoDS_Iterator it(anEdge.Oriented(TopAbs_FORWARD)); it.More(); it.Next())
980 TopoDS_Shape aV = it.Value();
981 if (aV.Orientation() == TopAbs_FORWARD || aV.Orientation() == TopAbs_REVERSED)
983 if (!aMapVE.Contains(aV))
984 aMapVE.Add(aV, TopTools_ListOfShape());
985 aMapVE.ChangeFromKey(aV).Append(anEdge);
990 VerticesToAvoid.Unite(NonMergVrt);
992 // do loop while there are unused edges
993 TopTools_MapOfShape aUsedEdges;
997 for(j=1; j <= SeqEdges.Length(); j++)
999 edge = TopoDS::Edge(SeqEdges.Value(j));
1000 if (!aUsedEdges.Contains(edge))
1003 if (j > SeqEdges.Length())
1004 break; // all edges have been used
1006 // make chain for unite
1007 TopTools_SequenceOfShape aChain;
1008 aChain.Append(edge);
1009 aUsedEdges.Add(edge);
1011 TopExp::Vertices(edge, V[0], V[1], Standard_True);
1013 // connect more edges to the chain in both directions
1014 for (j = 0; j < 2; j++)
1016 Standard_Boolean isAdded = Standard_True;
1019 isAdded = Standard_False;
1022 const TopTools_ListOfShape& aLE = aMapVE.FindFromKey(V[j]);
1023 for (TopTools_ListIteratorOfListOfShape itL(aLE); itL.More(); itL.Next())
1025 edge = TopoDS::Edge(itL.Value());
1026 if (!aUsedEdges.Contains(edge))
1028 TopoDS_Vertex V2[2];
1029 TopExp::Vertices(edge, V2[0], V2[1], Standard_True);
1030 // the neighboring edge must have V[j] reversed and located on the opposite end
1031 if (V2[1 - j].IsEqual(V[j].Reversed()))
1034 aChain.Prepend(edge);
1036 aChain.Append(edge);
1037 aUsedEdges.Add(edge);
1039 isAdded = Standard_True;
1047 if (aChain.Length() < 2)
1050 Standard_Boolean IsClosed = Standard_False;
1051 if (V[0].IsSame ( V[1] ))
1052 IsClosed = Standard_True;
1054 // split chain by vertices at which merging is not possible
1055 NCollection_Sequence<SubSequenceOfEdges> aOneSeq;
1056 GenerateSubSeq(aChain, aOneSeq, IsClosed, Tol, VerticesToAvoid);
1058 // put sub-chains in the result
1059 SeqOfSubSeqOfEdges.Append(aOneSeq);
1062 for (int i = 1; i <= SeqOfSubSeqOfEdges.Length(); i++)
1065 if (SeqOfSubSeqOfEdges(i).SeqsEdges.Length() < 2)
1067 if (MergeSubSeq(SeqOfSubSeqOfEdges(i).SeqsEdges, UE, Tol, ConcatBSplines))
1068 SeqOfSubSeqOfEdges(i).UnionEdges = UE;
1070 return Standard_True;
1073 //=======================================================================
1074 //function : MergeSeq
1075 //purpose : Tries to unify the sequence of edges with the set of another edges
1076 //which lies on the same geometry
1077 //=======================================================================
1079 static Standard_Boolean MergeSeq (TopTools_SequenceOfShape& SeqEdges,
1080 const Standard_Real Tol,
1081 const Standard_Boolean ConcatBSplines,
1082 Handle(ShapeBuild_ReShape)& theContext,
1083 TopTools_DataMapOfShapeShape& theOldToGeneratedShapes,
1084 const TopTools_MapOfShape& nonMergVert,
1085 TopTools_MapOfShape& RemovedShapes,
1086 const TopTools_DataMapOfShapeShape& NewEdges2OldEdges)
1088 NCollection_Sequence<SubSequenceOfEdges> SeqOfSubsSeqOfEdges;
1089 if ( MergeEdges(SeqEdges, Tol, ConcatBSplines, SeqOfSubsSeqOfEdges, nonMergVert) )
1091 for (Standard_Integer i = 1; i <= SeqOfSubsSeqOfEdges.Length(); i++ )
1093 if (SeqOfSubsSeqOfEdges(i).UnionEdges.IsNull())
1095 ShapeAnalysis_Edge sae;
1096 TopoDS_Vertex VF = sae.FirstVertex(SeqOfSubsSeqOfEdges(i).UnionEdges);
1097 TopoDS_Vertex VL = sae.LastVertex(SeqOfSubsSeqOfEdges(i).UnionEdges);
1098 for (Standard_Integer j = 1; j <= SeqOfSubsSeqOfEdges(i).SeqsEdges.Length(); j++)
1100 const TopoDS_Shape& anOldEdge = SeqOfSubsSeqOfEdges(i).SeqsEdges(j);
1101 const TopoDS_Shape* pOrigEdge = NewEdges2OldEdges.Seek(anOldEdge);
1103 pOrigEdge = &anOldEdge;
1104 theOldToGeneratedShapes.Bind(*pOrigEdge, SeqOfSubsSeqOfEdges(i).UnionEdges);
1106 theContext->Replace(anOldEdge, SeqOfSubsSeqOfEdges(i).UnionEdges);
1108 theContext->Remove(anOldEdge);
1110 TopExp::Vertices(TopoDS::Edge(anOldEdge), V[0], V[1]);
1111 for (int k = 0; k < 2; k++)
1113 if (!V[k].IsEqual(VF) && !V[k].IsEqual(VL))
1114 RemovedShapes.Add(V[k]);
1118 return Standard_True;
1121 return Standard_False;
1124 //=======================================================================
1125 //function : CheckSharedVertices
1126 //purpose : Checks the sequence of edges on the presence of shared vertex
1127 //=======================================================================
1129 static void CheckSharedVertices(const TopTools_SequenceOfShape& theSeqEdges,
1130 const TopTools_IndexedDataMapOfShapeListOfShape& theMapEdgesVertex,
1131 const TopTools_MapOfShape& theMapKeepShape,
1132 TopTools_MapOfShape& theShareVertMap)
1134 ShapeAnalysis_Edge sae;
1135 TopTools_SequenceOfShape SeqVertexes;
1136 TopTools_MapOfShape MapVertexes;
1137 for (Standard_Integer k = 1; k <= theSeqEdges.Length(); k++ )
1139 TopoDS_Vertex aV1 = sae.FirstVertex(TopoDS::Edge(theSeqEdges(k)));
1140 TopoDS_Vertex aV2 = sae.LastVertex(TopoDS::Edge(theSeqEdges(k)));
1141 if (!MapVertexes.Add(aV1))
1142 SeqVertexes.Append(aV1);
1143 if (!MapVertexes.Add(aV2))
1144 SeqVertexes.Append(aV2);
1147 for (Standard_Integer k = 1; k <= SeqVertexes.Length()/* && !IsSharedVertexPresent*/; k++ )
1149 const TopTools_ListOfShape& ListEdgesV1 = theMapEdgesVertex.FindFromKey(SeqVertexes(k));
1150 if (ListEdgesV1.Extent() > 2 || theMapKeepShape.Contains(SeqVertexes(k)))
1151 theShareVertMap.Add(SeqVertexes(k));
1153 //return theShareVertMap.IsEmpty() ? false : true;
1156 //=======================================================================
1157 //function : ShapeUpgrade_UnifySameDomain
1158 //purpose : Constructor
1159 //=======================================================================
1161 ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain()
1162 : myLinTol(Precision::Confusion()),
1163 myAngTol(Precision::Angular()),
1164 myUnifyFaces(Standard_True),
1165 myUnifyEdges (Standard_True),
1166 myConcatBSplines (Standard_False),
1167 myAllowInternal (Standard_False)
1169 myContext = new ShapeBuild_ReShape;
1172 //=======================================================================
1173 //function : ShapeUpgrade_UnifySameDomain
1174 //purpose : Constructor
1175 //=======================================================================
1177 ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain(const TopoDS_Shape& aShape,
1178 const Standard_Boolean UnifyEdges,
1179 const Standard_Boolean UnifyFaces,
1180 const Standard_Boolean ConcatBSplines)
1181 : myInitShape (aShape),
1182 myLinTol(Precision::Confusion()),
1183 myAngTol(Precision::Angular()),
1184 myUnifyFaces(UnifyFaces),
1185 myUnifyEdges (UnifyEdges),
1186 myConcatBSplines (ConcatBSplines),
1187 myAllowInternal (Standard_False),
1190 myContext = new ShapeBuild_ReShape;
1193 //=======================================================================
1194 //function : Initialize
1196 //=======================================================================
1198 void ShapeUpgrade_UnifySameDomain::Initialize(const TopoDS_Shape& aShape,
1199 const Standard_Boolean UnifyEdges,
1200 const Standard_Boolean UnifyFaces,
1201 const Standard_Boolean ConcatBSplines)
1203 myInitShape = aShape;
1205 myUnifyEdges = UnifyEdges;
1206 myUnifyFaces = UnifyFaces;
1207 myConcatBSplines = ConcatBSplines;
1210 myOldToGeneratedShapes.Clear();
1211 myKeepShapes.Clear();
1212 myRemovedShapes.Clear();
1215 //=======================================================================
1216 //function : AllowInternalEdges
1218 //=======================================================================
1220 void ShapeUpgrade_UnifySameDomain::AllowInternalEdges (const Standard_Boolean theValue)
1222 myAllowInternal = theValue;
1225 //=======================================================================
1226 //function : KeepShape
1228 //=======================================================================
1230 void ShapeUpgrade_UnifySameDomain::KeepShape(const TopoDS_Shape& theShape)
1232 if (theShape.ShapeType() == TopAbs_EDGE || theShape.ShapeType() == TopAbs_VERTEX)
1233 myKeepShapes.Add(theShape);
1236 //=======================================================================
1237 //function : KeepShapes
1239 //=======================================================================
1241 void ShapeUpgrade_UnifySameDomain::KeepShapes(const TopTools_MapOfShape& theShapes)
1243 for (TopTools_MapIteratorOfMapOfShape it(theShapes); it.More(); it.Next()) {
1244 if (it.Value().ShapeType() == TopAbs_EDGE || it.Value().ShapeType() == TopAbs_VERTEX)
1245 myKeepShapes.Add(it.Value());
1249 //=======================================================================
1250 //function : putIntWires
1251 //purpose : Add internal wires that are classified inside the face as a subshape,
1252 // and remove them from the sequence
1253 //=======================================================================
1254 static void putIntWires(TopoDS_Shape& theFace, TopTools_SequenceOfShape& theWires)
1256 TopoDS_Face& aFace = TopoDS::Face(theFace);
1257 for (Standard_Integer i=1; i <= theWires.Length(); i++)
1259 TopoDS_Shape aWire = theWires(i);
1261 Standard_Boolean isP2d = Standard_False;
1262 for (TopoDS_Iterator it(aWire); it.More() && !isP2d; it.Next())
1264 const TopoDS_Edge& anEdge = TopoDS::Edge(it.Value());
1265 Standard_Real aFirst, aLast;
1266 Handle(Geom2d_Curve) aC2d = BRep_Tool::CurveOnSurface(anEdge, aFace, aFirst, aLast);
1267 aC2d->D0((aFirst + aLast) * 0.5, aP2d);
1268 isP2d = Standard_True;
1270 BRepClass_FaceClassifier aClass(aFace, aP2d, Precision::PConfusion());
1271 if (aClass.State() == TopAbs_IN)
1273 BRep_Builder().Add(aFace, aWire);
1280 //=======================================================================
1281 //function : UnifyFaces
1283 //=======================================================================
1285 void ShapeUpgrade_UnifySameDomain::UnifyFaces()
1287 // creating map of edge faces for the whole shape
1288 TopTools_IndexedDataMapOfShapeListOfShape aGMapEdgeFaces;
1289 TopExp::MapShapesAndAncestors(myShape, TopAbs_EDGE, TopAbs_FACE, aGMapEdgeFaces);
1291 // unify faces in each shell separately
1292 TopExp_Explorer exps;
1293 for (exps.Init(myShape, TopAbs_SHELL); exps.More(); exps.Next())
1294 IntUnifyFaces(exps.Current(), aGMapEdgeFaces, Standard_False);
1296 // gather all faces out of shells in one compound and unify them at once
1298 TopoDS_Compound aCmp;
1299 aBB.MakeCompound(aCmp);
1300 Standard_Integer nbf = 0;
1301 for (exps.Init(myShape, TopAbs_FACE, TopAbs_SHELL); exps.More(); exps.Next(), nbf++)
1302 aBB.Add(aCmp, exps.Current());
1305 IntUnifyFaces(aCmp, aGMapEdgeFaces, Standard_True);
1307 myShape = myContext->Apply(myShape);
1311 void ShapeUpgrade_UnifySameDomain::IntUnifyFaces(const TopoDS_Shape& theInpShape,
1312 const TopTools_IndexedDataMapOfShapeListOfShape& theGMapEdgeFaces,
1313 Standard_Boolean IsCheckSharedEdgeOri)
1315 // creating map of edge faces for the shape
1316 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
1317 TopExp::MapShapesAndAncestors(theInpShape, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
1319 // map of processed shapes
1320 TopTools_MapOfShape aProcessed;
1322 Standard_Integer NbModif = 0;
1323 Standard_Boolean hasFailed = Standard_False;
1324 Standard_Real tol = Precision::Confusion();
1327 Standard_Integer nbf = 0;
1328 TopExp_Explorer exp;
1329 TopTools_MapOfShape mapF;
1330 for (exp.Init(theInpShape, TopAbs_FACE); exp.More(); exp.Next()) {
1331 if (mapF.Add(exp.Current()))
1335 // processing each face
1337 for (exp.Init(theInpShape, TopAbs_FACE); exp.More(); exp.Next()) {
1338 const TopoDS_Face& aFaceOriginal = TopoDS::Face(exp.Current());
1339 TopoDS_Face aFace = TopoDS::Face(aFaceOriginal.Oriented(TopAbs_FORWARD));
1341 if (aProcessed.Contains(aFace))
1344 Standard_Integer dummy;
1345 TopTools_SequenceOfShape edges;
1346 AddOrdinaryEdges(edges,aFace,dummy);
1348 TopTools_SequenceOfShape faces;
1349 faces.Append(aFace);
1351 //surface and location to construct result
1352 TopLoc_Location aBaseLocation;
1353 Handle(Geom_Surface) aBaseSurface = BRep_Tool::Surface(aFace,aBaseLocation);
1354 aBaseSurface = ClearRts(aBaseSurface);
1356 // find adjacent faces to union
1358 for (i = 1; i <= edges.Length(); i++) {
1359 TopoDS_Edge edge = TopoDS::Edge(edges(i));
1360 if (BRep_Tool::Degenerated(edge))
1363 // get connectivity of the edge in the global shape
1364 const TopTools_ListOfShape& aGList = theGMapEdgeFaces.FindFromKey(edge);
1365 if (!myAllowInternal && (aGList.Extent() != 2 || myKeepShapes.Contains(edge))) {
1366 // non mainfold case is not processed unless myAllowInternal
1370 // get normal of the face to compare it with normals of other faces
1373 // take intermediate point on edge to compute the normal
1375 BRep_Tool::Range(edge, f, l);
1376 Standard_Real aTMid = (f + l) * .5;
1378 Standard_Boolean bCheckNormals = GetNormalToSurface(aFaceOriginal, edge, aTMid, aDN1);
1380 // process faces connected through the edge in the current shape
1381 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
1382 TopTools_ListIteratorOfListOfShape anIter(aList);
1383 for (; anIter.More(); anIter.Next()) {
1384 const TopoDS_Face& aCheckedFaceOriginal = TopoDS::Face(anIter.Value());
1385 TopoDS_Face anCheckedFace = TopoDS::Face(aCheckedFaceOriginal.Oriented(TopAbs_FORWARD));
1386 if (anCheckedFace.IsSame(aFace))
1389 if (aProcessed.Contains(anCheckedFace))
1392 if (IsCheckSharedEdgeOri && !CheckSharedEdgeOri(aFace, anCheckedFace, edge) )
1395 if (bCheckNormals) {
1396 // get normal of checked face using the same parameter on edge
1398 if (GetNormalToSurface(aCheckedFaceOriginal, edge, aTMid, aDN2)) {
1399 // and check if the adjacent faces are having approximately same normals
1400 Standard_Real anAngle = aDN1.Angle(aDN2);
1401 if (anAngle > myAngTol) {
1407 if (IsSameDomain(aFace,anCheckedFace, myLinTol, myAngTol)) {
1409 // hotfix for 27271: prevent merging along periodic direction.
1410 if (IsLikeSeam(edge, anCheckedFace, aBaseSurface))
1413 // replacing pcurves
1414 TopoDS_Face aMockUpFace;
1416 B.MakeFace(aMockUpFace,aBaseSurface,aBaseLocation,0.);
1417 MovePCurves(aMockUpFace,anCheckedFace);
1419 if (AddOrdinaryEdges(edges,aMockUpFace,dummy)) {
1420 // sequence edges is modified
1424 faces.Append(anCheckedFace);
1425 aProcessed.Add(anCheckedFace);
1431 if (faces.Length() > 1) {
1432 // fill in the connectivity map for selected faces
1433 TopTools_IndexedDataMapOfShapeListOfShape aMapEF;
1434 for (i = 1; i <= faces.Length(); i++) {
1435 TopExp::MapShapesAndAncestors(faces(i), TopAbs_EDGE, TopAbs_FACE, aMapEF);
1438 // Collect keep edges and multiconnected edges, i.e. edges that are internal to
1439 // the set of selected faces and have connections to other faces.
1440 TopTools_ListOfShape aKeepEdges;
1441 for (i = 1; i <= aMapEF.Extent(); i++) {
1442 const TopTools_ListOfShape& aLF = aMapEF(i);
1443 if (aLF.Extent() == 2) {
1444 const TopoDS_Shape& aE = aMapEF.FindKey(i);
1445 const TopTools_ListOfShape& aGLF = theGMapEdgeFaces.FindFromKey(aE);
1446 if (aGLF.Extent() > 2 || myKeepShapes.Contains(aE)) {
1447 aKeepEdges.Append(aE);
1451 if (!aKeepEdges.IsEmpty()) {
1452 if (!myAllowInternal) {
1453 // Remove from the selection the faces which have no other connect edges
1454 // and contain multiconnected edges and/or keep edges.
1455 TopTools_MapOfShape anAvoidFaces;
1456 TopTools_ListIteratorOfListOfShape it(aKeepEdges);
1457 for (; it.More(); it.Next()) {
1458 const TopoDS_Shape& aE = it.Value();
1459 const TopTools_ListOfShape& aLF = aMapEF.FindFromKey(aE);
1460 anAvoidFaces.Add(aLF.First());
1461 anAvoidFaces.Add(aLF.Last());
1463 for (i = 1; i <= faces.Length(); i++) {
1464 if (anAvoidFaces.Contains(faces(i))) {
1465 // update the boundaries of merged area, for that
1466 // remove from 'edges' the edges of this face and add to 'edges'
1467 // the edges of this face that were not present in 'edges' before
1468 Standard_Boolean hasConnectAnotherFaces = Standard_False;
1469 TopExp_Explorer ex(faces(i), TopAbs_EDGE);
1470 for (; ex.More() && !hasConnectAnotherFaces; ex.Next()) {
1471 TopoDS_Shape aE = ex.Current();
1472 const TopTools_ListOfShape& aLF = aMapEF.FindFromKey(aE);
1473 if (aLF.Extent() > 1) {
1474 for (it.Init(aLF); it.More() && !hasConnectAnotherFaces; it.Next()) {
1475 if (!anAvoidFaces.Contains(it.Value()))
1476 hasConnectAnotherFaces = Standard_True;
1480 if (!hasConnectAnotherFaces) {
1481 AddOrdinaryEdges(edges, faces(i), dummy);
1487 // check if the faces with keep edges contained in
1488 // already updated the boundaries of merged area
1489 if (!faces.IsEmpty()) {
1490 TopTools_MapOfShape aMapFaces;
1491 for (i = 1; i <= faces.Length(); i++) {
1492 aMapFaces.Add(faces(i));
1494 for (it.Init(aKeepEdges); it.More(); it.Next()) {
1495 const TopoDS_Shape& aE = it.Value();
1496 const TopTools_ListOfShape& aLF = aMapEF.FindFromKey(aE);
1497 if (aLF.Extent() < 2)
1499 if (aMapFaces.Contains(aLF.First()) &&
1500 aMapFaces.Contains(aLF.Last())) {
1501 for (i = 1; i <= faces.Length(); i++) {
1502 if (faces(i).IsEqual(aLF.First()) ||
1503 faces(i).IsEqual(aLF.Last())) {
1504 AddOrdinaryEdges(edges, faces(i), dummy);
1514 // add multiconnected and keep edges as internal in new face
1515 TopTools_ListIteratorOfListOfShape it(aKeepEdges);
1516 for (; it.More(); it.Next()) {
1517 const TopoDS_Shape& aE = it.Value();
1518 edges.Append(aE.Oriented(TopAbs_INTERNAL));
1524 // all faces collected in the sequence. Perform union of faces
1525 if (faces.Length() > 1) {
1527 TopoDS_Face aResult;
1529 B.MakeFace(aResult,aBaseSurface,aBaseLocation,0);
1530 Standard_Integer nbWires = 0;
1532 TopoDS_Face tmpF = TopoDS::Face(myContext->Apply(faces(1).Oriented(TopAbs_FORWARD)));
1534 TopTools_IndexedMapOfShape anOldEdges;
1535 for (int j = 1; j <= faces.Length(); j++) {
1536 TopExp::MapShapes(faces(j), TopAbs_EDGE, anOldEdges);
1538 TopTools_IndexedMapOfShape aMapEdgesAndVertexes;
1539 for (int j = 1; j <= edges.Length(); j++) {
1540 TopExp::MapShapes(edges(j), aMapEdgesAndVertexes);
1542 for (int j = 1; j <= anOldEdges.Extent(); j++) {
1543 const TopoDS_Edge& anEdge = TopoDS::Edge(anOldEdges(j));
1544 if (!aMapEdgesAndVertexes.Contains(anEdge)) {
1545 myRemovedShapes.Add(anEdge);
1547 TopExp::Vertices(anEdge, V[0], V[1]);
1548 for (int k = 0; k < 2; k++) {
1549 if (!aMapEdgesAndVertexes.Contains(V[k]))
1550 myRemovedShapes.Add(V[k]);
1556 while (edges.Length()>0) {
1558 Standard_Boolean isEdge3d = Standard_False;
1560 TopTools_MapOfShape aVertices;
1564 TopoDS_Edge anEdge = TopoDS::Edge(edges(1));
1566 // collect internal edges in separate wires
1567 Standard_Boolean isInternal = (anEdge.Orientation() == TopAbs_INTERNAL);
1569 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
1570 B.Add(aWire,anEdge);
1571 TopoDS_Vertex V1,V2;
1572 TopExp::Vertices(anEdge,V1,V2);
1576 Standard_Boolean isNewFound = Standard_False;
1578 isNewFound = Standard_False;
1579 for(Standard_Integer j = 1; j <= edges.Length(); j++) {
1580 anEdge = TopoDS::Edge(edges(j));
1581 // check if the current edge orientation corresponds to the first one
1582 Standard_Boolean isCurrInternal = (anEdge.Orientation() == TopAbs_INTERNAL);
1583 if (isCurrInternal != isInternal)
1585 TopExp::Vertices(anEdge,V1,V2);
1586 if(aVertices.Contains(V1) || aVertices.Contains(V2)) {
1587 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
1590 B.Add(aWire,anEdge);
1593 isNewFound = Standard_True;
1596 } while (isNewFound);
1598 // sorting any type of edges
1599 aWire.Closed (BRep_Tool::IsClosed (aWire));
1600 aWire = TopoDS::Wire(myContext->Apply(aWire));
1602 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(aWire,tmpF,Precision::Confusion());
1604 Standard_Boolean isDegRemoved = Standard_False;
1605 if(!sfw->StatusReorder ( ShapeExtend_FAIL )) {
1606 // clear degenerated edges if at least one with 3d curve exist
1608 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
1609 for(Standard_Integer j = 1; j<=sewd->NbEdges();j++) {
1610 TopoDS_Edge E = sewd->Edge(j);
1611 if(BRep_Tool::Degenerated(E)) {
1613 isDegRemoved = Standard_True;
1614 myRemovedShapes.Add(E);
1621 sfw->FixDegenerated();
1623 TopoDS_Wire aWireFixed = sfw->Wire();
1624 //aContext->Replace(aWire,aWireFixed);
1625 myContext->Replace(aWire,aWireFixed);
1628 if (!myOldNewMap.IsBound(aWire))
1630 TopTools_ListOfShape EmptyList;
1631 myOldNewMap.Bind(aWire, EmptyList);
1633 myOldNewMap(aWire).Clear();
1634 myOldNewMap(aWire).Append(aWireFixed);
1638 // add resulting wire
1640 B.Add(aResult,aWireFixed);
1644 Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
1645 Standard_Integer nbEdges = sbwd->NbEdges();
1646 // sort degenerated edges and create one edge instead of several ones
1647 ShapeAnalysis_WireOrder sawo(Standard_False, 0);
1648 ShapeAnalysis_Edge sae;
1649 Standard_Integer aLastEdge = nbEdges;
1650 for(Standard_Integer j = 1; j <= nbEdges; j++) {
1652 //smh protection on NULL pcurve
1653 Handle(Geom2d_Curve) c2d;
1654 if(!sae.PCurve(sbwd->Edge(j),tmpF,c2d,f,l)) {
1658 sawo.Add(c2d->Value(f).XY(),c2d->Value(l).XY());
1660 if (sawo.NbEdges() == 0)
1664 // constructind one degenerative edge
1665 gp_XY aStart, anEnd, tmp;
1666 Standard_Integer nbFirst = sawo.Ordered(1);
1667 TopoDS_Edge anOrigE = TopoDS::Edge(sbwd->Edge(nbFirst).Oriented(TopAbs_FORWARD));
1668 ShapeBuild_Edge sbe;
1669 TopoDS_Vertex aDummyV;
1670 TopoDS_Edge E = sbe.CopyReplaceVertices(anOrigE,aDummyV,aDummyV);
1671 sawo.XY(nbFirst,aStart,tmp);
1672 sawo.XY(sawo.Ordered(aLastEdge),tmp,anEnd);
1674 gp_XY aVec = anEnd-aStart;
1675 Handle(Geom2d_Line) aLine = new Geom2d_Line(aStart,gp_Dir2d(anEnd-aStart));
1677 B.UpdateEdge(E,aLine,tmpF,0.);
1678 B.Range(E,tmpF,0.,aVec.Modulus());
1679 Handle(Geom_Curve) C3d;
1680 B.UpdateEdge(E,C3d,0.);
1681 B.Degenerated(E,Standard_True);
1685 aW.Closed (Standard_True);
1690 // perform substitution of face
1691 //aContext->Replace(aContext->Apply(aFace),aResult);
1692 myContext->Replace(myContext->Apply(aFace),aResult);
1695 if (!myOldNewMap.IsBound(aFace))
1697 TopTools_ListOfShape EmptyList;
1698 myOldNewMap.Bind(aFace, EmptyList);
1700 myOldNewMap(aFace).Clear();
1701 myOldNewMap(aFace).Append(aResult);
1705 ShapeFix_Face sff (aResult);
1706 //Intializing by tolerances
1707 sff.SetPrecision(Precision::Confusion());
1708 sff.SetMinTolerance(tol);
1709 sff.SetMaxTolerance(1.);
1711 sff.FixOrientationMode() = 0;
1712 //sff.FixWireMode() = 0;
1713 //sff.SetContext(aContext);
1714 sff.SetContext(myContext);
1715 // Applying the fixes
1717 if(sff.Status(ShapeExtend_FAIL))
1718 hasFailed = Standard_True;
1720 // breaking down to several faces
1721 //TopoDS_Shape theResult = aContext->Apply(aResult);
1722 TopoDS_Shape theResult = myContext->Apply(aResult);
1723 for (TopExp_Explorer aFaceExp (theResult,TopAbs_FACE); aFaceExp.More(); aFaceExp.Next()) {
1724 TopoDS_Face aCurrent = TopoDS::Face(aFaceExp.Current().Oriented(TopAbs_FORWARD));
1725 Handle(TColGeom_HArray2OfSurface) grid = new TColGeom_HArray2OfSurface ( 1, 1, 1, 1 );
1726 grid->SetValue ( 1, 1, aBaseSurface );
1727 Handle(ShapeExtend_CompositeSurface) G = new ShapeExtend_CompositeSurface ( grid );
1728 ShapeFix_ComposeShell CompShell;
1729 CompShell.Init ( G, aBaseLocation, aCurrent, ::Precision::Confusion() );//myPrecision
1730 //CompShell.SetContext( aContext );
1731 CompShell.SetContext( myContext );
1733 TopTools_SequenceOfShape parts, anIntWires;
1734 ShapeFix_SequenceOfWireSegment wires;
1735 for(TopExp_Explorer W_Exp(aCurrent,TopAbs_WIRE);W_Exp.More();W_Exp.Next()) {
1736 const TopoDS_Wire& aWire = TopoDS::Wire(W_Exp.Current());
1737 // check if the wire is ordinary (contains non-internal edges)
1738 Standard_Boolean isInternal = Standard_True;
1739 for (TopoDS_Iterator it(aWire); it.More() && isInternal; it.Next())
1740 isInternal = (it.Value().Orientation() == TopAbs_INTERNAL);
1743 // place internal wire separately
1744 anIntWires.Append(aWire);
1748 Handle(ShapeExtend_WireData) sbwd =
1749 new ShapeExtend_WireData (aWire);
1750 ShapeFix_WireSegment seg ( sbwd, TopAbs_REVERSED );
1755 CompShell.DispatchWires ( parts,wires );
1756 for (Standard_Integer j=1; j <= parts.Length(); j++ ) {
1757 ShapeFix_Face aFixOrient(TopoDS::Face(parts(j)));
1758 //aFixOrient.SetContext(aContext);
1759 aFixOrient.SetContext(myContext);
1760 aFixOrient.FixOrientation();
1761 // put internal wires to faces
1762 putIntWires(parts(j), anIntWires);
1765 TopoDS_Shape CompRes;
1766 if ( parts.Length() !=1 ) {
1769 for ( i=1; i <= parts.Length(); i++ )
1770 B.Add ( S, parts(i) );
1771 S.Closed (BRep_Tool::IsClosed (S));
1774 else CompRes = parts(1);
1776 //aContext->Replace(aCurrent,CompRes);
1777 myContext->Replace(aCurrent,CompRes);
1780 if (!myOldNewMap.IsBound(aCurrent))
1782 TopTools_ListOfShape EmptyList;
1783 myOldNewMap.Bind(aCurrent, EmptyList);
1785 myOldNewMap(aCurrent).Clear();
1786 myOldNewMap(aCurrent).Append(CompRes);
1791 // remove the remaining faces
1792 for(i = 1; i <= faces.Length(); i++)
1794 myOldToGeneratedShapes.Bind(faces(i), theResult);
1796 myContext->Remove(faces(i));
1799 } // end processing each face
1801 //TopoDS_Shape aResult = Shape;
1802 if (NbModif > 0 && !hasFailed) {
1803 //TopoDS_Shape aResult = aContext->Apply(aShell);
1804 TopoDS_Shape aResult = myContext->Apply(theInpShape);
1807 if (!myContext.IsNull()) sfe.SetContext(myContext);
1808 for (exp.Init(aResult,TopAbs_EDGE); exp.More(); exp.Next()) {
1809 TopoDS_Edge E = TopoDS::Edge(exp.Current());
1810 sfe.FixVertexTolerance (E);
1811 // ptv add fix same parameter
1812 sfe.FixSameParameter(E, Precision::Confusion());
1815 myContext->Replace(theInpShape, aResult);
1818 if (!myOldNewMap.IsBound(aShell))
1820 TopTools_ListOfShape EmptyList;
1821 myOldNewMap.Bind(aShell, EmptyList);
1823 myOldNewMap(aShell).Clear();
1824 myOldNewMap(aShell).Append(aResult);
1830 for (exp.Init(theInpShape, TopAbs_FACE); exp.More(); exp.Next()) {
1831 TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
1832 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
1833 sfw->SetContext(myContext);
1834 sfw->SetPrecision(Precision::Confusion());
1835 sfw->SetMinTolerance(Precision::Confusion());
1836 sfw->SetMaxTolerance(1.);
1837 sfw->SetFace(aFace);
1838 for (TopoDS_Iterator iter (aFace,Standard_False); iter.More(); iter.Next()) {
1839 TopoDS_Wire wire = TopoDS::Wire(iter.Value());
1849 //=======================================================================
1850 //function : UnifyEdges
1852 //=======================================================================
1853 void ShapeUpgrade_UnifySameDomain::UnifyEdges()
1855 Standard_Real Tol = Precision::Confusion();
1857 //Handle(ShapeBuild_ReShape) myContext = new ShapeBuild_ReShape;
1858 Standard_Real myTolerance = Precision::Confusion();
1859 TopoDS_Shape aResult = myContext->Apply(myShape);
1861 TopTools_IndexedMapOfShape ChangedFaces;
1863 // creating map of edge faces
1864 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
1865 TopExp::MapShapesAndAncestors(myShape, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
1867 // creating map of vertex edges
1868 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgesVertex;
1869 TopExp::MapShapesAndUniqueAncestors(myShape, TopAbs_VERTEX, TopAbs_EDGE, aMapEdgesVertex);
1871 //Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
1872 TopoDS_Shape aRes = myShape;
1873 //aRes = aContext->Apply(aSolid);
1874 aRes = myContext->Apply(myShape);
1876 TopTools_MapOfShape SharedVert;
1878 TopTools_IndexedMapOfShape anOldEdges;
1879 TopExp::MapShapes(myInitShape, TopAbs_EDGE, anOldEdges);
1881 TopTools_DataMapOfShapeShape NewEdges2OldEdges;
1882 for (int i = 1; i <= anOldEdges.Extent(); i++)
1884 TopoDS_Shape NewEdge = myContext->Apply(anOldEdges(i));
1885 if (!NewEdge.IsNull())
1886 NewEdges2OldEdges.Bind(NewEdge, anOldEdges(i));
1889 TopExp_Explorer exp;
1890 // processing separate wires
1891 for (exp.Init(aRes, TopAbs_WIRE, TopAbs_FACE); exp.More(); exp.Next())
1893 TopTools_SequenceOfShape SeqEdges;
1894 TopExp_Explorer expE(exp.Current(), TopAbs_EDGE);
1895 for (; expE.More(); expE.Next())
1896 SeqEdges.Append(expE.Current());
1898 CheckSharedVertices(SeqEdges, aMapEdgesVertex, myKeepShapes, SharedVert);
1899 MergeSeq(SeqEdges, Tol, myConcatBSplines, myContext,
1900 myOldToGeneratedShapes, SharedVert,
1901 myRemovedShapes, NewEdges2OldEdges);
1904 TopTools_DataMapOfShapeShape oldFaces2NewFaces;
1905 for (exp.Init(myShape, TopAbs_FACE); exp.More(); exp.Next())
1907 const TopoDS_Face& f = TopoDS::Face(exp.Current());
1908 TopoDS_Face NewF = TopoDS::Face(myContext->Apply(f));
1910 oldFaces2NewFaces.Bind(f, NewF);
1913 // processing each face
1914 for (exp.Init(aRes, TopAbs_FACE); exp.More(); exp.Next()) {
1915 //TopoDS_Face aFace = TopoDS::Face(aContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
1916 TopoDS_Face aFace = TopoDS::Face(myContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
1917 TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
1918 TopTools_SequenceOfShape aNonSharedEdges;
1919 for (TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
1920 TopoDS_Edge edge = TopoDS::Edge(expe.Current());
1921 if (!aMapEdgeFaces.Contains(edge)) continue;
1922 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
1923 TopTools_ListIteratorOfListOfShape anIter(aList);
1924 Standard_Integer NbFacesPerEdge = aList.Extent();
1925 for ( ; anIter.More(); anIter.Next()) {
1926 TopoDS_Face face = TopoDS::Face(anIter.Value());
1927 TopoDS_Face face1 = TopoDS::Face(oldFaces2NewFaces(anIter.Value()));
1928 if (face1.IsSame(aFace) && NbFacesPerEdge != 1)
1930 if (NbFacesPerEdge == 1)
1931 //store non-shared edges separately
1932 aNonSharedEdges.Append(edge);
1935 if (aMapFacesEdges.Contains(face))
1936 aMapFacesEdges.ChangeFromKey(face).Append(edge);
1939 TopTools_ListOfShape ListEdges;
1940 ListEdges.Append(edge);
1941 aMapFacesEdges.Add(face,ListEdges);
1947 for (Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++)
1949 const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
1950 TopTools_SequenceOfShape SeqEdges;
1951 TopTools_ListIteratorOfListOfShape anIter(ListEdges);
1952 for ( ; anIter.More(); anIter.Next())
1953 SeqEdges.Append(anIter.Value());
1954 if (SeqEdges.Length()==1)
1958 CheckSharedVertices(SeqEdges, aMapEdgesVertex, myKeepShapes, SharedVert);
1959 //if (!SharedVert.IsEmpty())
1961 if ( MergeSeq(SeqEdges, Tol, myConcatBSplines, myContext,
1962 myOldToGeneratedShapes, SharedVert,
1963 myRemovedShapes, NewEdges2OldEdges))
1967 for (j = 1; j <= SeqEdges.Length(); j++)
1969 if (!myOldNewMap.IsBound(SeqEdges(j)))
1971 TopTools_ListOfShape EmptyList;
1972 myOldNewMap.Bind(SeqEdges(j), EmptyList);
1974 myOldNewMap(SeqEdges(j)).Clear();
1975 myOldNewMap(SeqEdges(j)).Append(E);
1980 TopoDS_Face tmpF = TopoDS::Face(exp.Current());
1981 if ( !ChangedFaces.Contains(tmpF) )
1982 ChangedFaces.Add(tmpF);
1983 tmpF = TopoDS::Face(aMapFacesEdges.FindKey(i));
1984 if ( !ChangedFaces.Contains(tmpF) )
1985 ChangedFaces.Add(tmpF);
1989 if ( aNonSharedEdges.Length() > 1 )
1992 CheckSharedVertices(aNonSharedEdges, aMapEdgesVertex, myKeepShapes, SharedVert);
1993 if ( MergeSeq(aNonSharedEdges, Tol, myConcatBSplines, myContext,
1994 myOldToGeneratedShapes, SharedVert,
1995 myRemovedShapes, NewEdges2OldEdges))
1997 TopoDS_Face tmpF = TopoDS::Face(exp.Current());
1998 if ( !ChangedFaces.Contains(tmpF) )
1999 ChangedFaces.Add(tmpF);
2003 } // end processing each face
2005 // fix changed faces and replace them in the local context
2006 for (Standard_Integer i=1; i<=ChangedFaces.Extent(); i++) {
2007 //TopoDS_Face aFace = TopoDS::Face(aContext->Apply(ChangedFaces.FindKey(i)));
2008 TopoDS_Face aFace = TopoDS::Face(myContext->Apply(ChangedFaces.FindKey(i)));
2011 Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFace);
2012 sff->SetContext(myContext);
2013 sff->SetPrecision(myTolerance);
2014 sff->SetMinTolerance(myTolerance);
2015 sff->SetMaxTolerance(Max(1.,myTolerance*1000.));
2017 TopoDS_Shape aNewFace = sff->Face();
2018 //aContext->Replace(aFace,aNewFace);
2019 myContext->Replace(aFace,aNewFace);
2022 if (!myOldNewMap.IsBound(aFace))
2024 TopTools_ListOfShape EmptyList;
2025 myOldNewMap.Bind(aFace, EmptyList);
2027 myOldNewMap(aFace).Clear();
2028 myOldNewMap(aFace).Append(aNewFace);
2033 if (ChangedFaces.Extent() > 0) {
2034 // fix changed shell and replace it in the local context
2035 //TopoDS_Shape aRes1 = aContext->Apply(aRes);
2036 TopoDS_Shape aRes1 = myContext->Apply(aRes);
2037 TopExp_Explorer expsh;
2038 for (expsh.Init(aRes1, TopAbs_SHELL); expsh.More(); expsh.Next()) {
2039 TopoDS_Shell aShell = TopoDS::Shell(expsh.Current());
2040 Handle(ShapeFix_Shell) sfsh = new ShapeFix_Shell;
2041 sfsh->FixFaceOrientation(aShell);
2042 TopoDS_Shape aNewShell = sfsh->Shell();
2043 //aContext->Replace(aShell,aNewShell);
2044 myContext->Replace(aShell,aNewShell);
2047 if (!myOldNewMap.IsBound(aShell))
2049 TopTools_ListOfShape EmptyList;
2050 myOldNewMap.Bind(aShell, EmptyList);
2052 myOldNewMap(aShell).Clear();
2053 myOldNewMap(aShell).Append(aNewShell);
2057 //TopoDS_Shape aRes2 = aContext->Apply(aRes1);
2058 TopoDS_Shape aRes2 = myContext->Apply(aRes1);
2059 myContext->Replace(myShape,aRes2);
2062 if (!myOldNewMap.IsBound(aSolid))
2064 TopTools_ListOfShape EmptyList;
2065 myOldNewMap.Bind(aSolid, EmptyList);
2067 myOldNewMap(aSolid).Clear();
2068 myOldNewMap(aSolid).Append(aRes2);
2073 myShape = myContext->Apply(myShape);
2076 //=======================================================================
2077 //function : UnifyFacesAndEdges
2079 //=======================================================================
2081 void ShapeUpgrade_UnifySameDomain::UnifyFacesAndEdges()
2086 ShapeUpgrade_RemoveLocations RemLoc;
2087 RemLoc.Remove(myShape);
2088 myShape = RemLoc.GetResult();
2094 //=======================================================================
2096 //purpose : builds the resulting shape
2097 //======================================================================
2098 void ShapeUpgrade_UnifySameDomain::Build()
2100 if (myUnifyFaces && myUnifyEdges)
2101 UnifyFacesAndEdges();
2103 else if (myUnifyEdges)
2105 else if (myUnifyFaces)
2111 //=======================================================================
2113 //purpose : give the resulting shape
2114 //=======================================================================
2115 const TopoDS_Shape& ShapeUpgrade_UnifySameDomain::Shape() const
2120 //=======================================================================
2121 //function : Generated
2122 //purpose : returns the new shape from the old one
2123 //=======================================================================
2124 const TopTools_ListOfShape& ShapeUpgrade_UnifySameDomain::Generated(const TopoDS_Shape& aShape)
2126 const TopoDS_Shape* aNewShape;
2127 myHistShapes.Clear();
2128 aNewShape = myOldToGeneratedShapes.Seek(aShape);
2130 if (myContext->IsRecorded(*aNewShape))
2131 myHistShapes.Append(myContext->Apply(*aNewShape));
2133 myHistShapes.Append(*aNewShape);
2135 return myHistShapes;
2138 //=======================================================================
2139 //function : Modified
2140 //purpose : returns the new modified shape from the old one shape
2141 //=======================================================================
2142 const TopTools_ListOfShape& ShapeUpgrade_UnifySameDomain::Modified(const TopoDS_Shape& aShape)
2144 TopoDS_Shape aNewShape;
2145 Standard_Integer aModifiedStatus;
2146 myHistShapes.Clear();
2147 if (!myOldToGeneratedShapes.Seek(aShape) &&
2148 !myRemovedShapes.Contains(aShape) &&
2149 myContext->IsRecorded(aShape)){
2150 aModifiedStatus = myContext->Status(aShape, aNewShape, Standard_True);
2151 if (aModifiedStatus > 0)
2152 myHistShapes.Append(aNewShape);
2154 return myHistShapes;
2157 //=======================================================================
2158 //function : IsDeleted
2159 //purpose : returns true if the shape has been deleted.
2160 //=======================================================================
2161 Standard_Boolean ShapeUpgrade_UnifySameDomain::IsDeleted(const TopoDS_Shape& aShape)
2163 return myRemovedShapes.Contains(aShape);