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.
16 #include <ShapeUpgrade_UnifySameDomain.ixx>
17 #include <ShapeUpgrade_UnifySameDomain.hxx>
18 #include <TopTools_SequenceOfShape.hxx>
19 #include <TopTools_IndexedMapOfShape.hxx>
20 #include <TopExp_Explorer.hxx>
21 #include <Geom_Surface.hxx>
22 #include <Geom_Line.hxx>
24 #include <Geom_CylindricalSurface.hxx>
25 #include <gp_Cylinder.hxx>
26 #include <Geom_SurfaceOfRevolution.hxx>
27 #include <Geom_SurfaceOfLinearExtrusion.hxx>
29 #include <Geom_Circle.hxx>
30 #include <Geom_RectangularTrimmedSurface.hxx>
31 #include <TopoDS_Face.hxx>
32 #include <BRep_Tool.hxx>
33 #include <GeomAdaptor_HSurface.hxx>
34 #include <BRepTopAdaptor_TopolTool.hxx>
35 #include <IntPatch_ImpImpIntersection.hxx>
36 #include <GeomLib_IsPlanarSurface.hxx>
37 #include <BRep_Builder.hxx>
38 #include <ShapeFix_Wire.hxx>
40 #include <ShapeAnalysis_Edge.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <TColGeom_SequenceOfSurface.hxx>
43 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
44 #include <BRep_TEdge.hxx>
45 #include <BRep_CurveRepresentation.hxx>
46 #include <TColGeom_Array1OfBSplineCurve.hxx>
47 #include <TColGeom_HArray1OfBSplineCurve.hxx>
48 #include <TColGeom2d_Array1OfBSplineCurve.hxx>
49 #include <TColGeom2d_HArray1OfBSplineCurve.hxx>
50 #include <TColStd_Array1OfReal.hxx>
52 #include <Geom_TrimmedCurve.hxx>
53 #include <Geom2d_TrimmedCurve.hxx>
54 #include <GeomConvert.hxx>
55 #include <Geom2dConvert.hxx>
56 #include <GeomConvert_CompCurveToBSplineCurve.hxx>
57 #include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
58 #include <TColGeom2d_SequenceOfBoundedCurve.hxx>
59 #include <BRepLib_MakeEdge.hxx>
60 #include <TColStd_MapOfInteger.hxx>
61 #include <BRepLib.hxx>
62 #include <GC_MakeCircle.hxx>
63 #include <Geom_BezierCurve.hxx>
64 #include <ShapeBuild_ReShape.hxx>
65 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
66 #include <TopTools_MapOfShape.hxx>
67 #include <TopTools_ListIteratorOfListOfShape.hxx>
68 #include <ShapeAnalysis_WireOrder.hxx>
69 #include <ShapeBuild_Edge.hxx>
70 #include <Geom2d_Line.hxx>
71 #include <ShapeFix_Face.hxx>
72 #include <TColGeom_HArray2OfSurface.hxx>
73 #include <ShapeExtend_CompositeSurface.hxx>
74 #include <ShapeFix_ComposeShell.hxx>
75 #include <ShapeFix_SequenceOfWireSegment.hxx>
76 #include <ShapeFix_WireSegment.hxx>
77 #include <ShapeFix_Edge.hxx>
78 #include <ShapeFix_Shell.hxx>
79 #include <ShapeUpgrade_RemoveLocations.hxx>
82 //=======================================================================
83 //function : AddOrdinaryEdges
85 //=======================================================================
86 // adds edges from the shape to the sequence
87 // seams and equal edges are dropped
88 // Returns true if one of original edges dropped
89 static Standard_Boolean AddOrdinaryEdges(TopTools_SequenceOfShape& edges,
90 const TopoDS_Shape aShape,
91 Standard_Integer& anIndex)
94 TopTools_IndexedMapOfShape aNewEdges;
95 //add edges without seams
96 for(TopExp_Explorer exp(aShape,TopAbs_EDGE); exp.More(); exp.Next()) {
97 TopoDS_Shape edge = exp.Current();
98 if(aNewEdges.Contains(edge))
100 //aNewEdges.Remove(edge);
101 TopoDS_Shape LastEdge = aNewEdges(aNewEdges.Extent());
102 aNewEdges.RemoveLast();
103 if (aNewEdges.FindIndex(edge) != 0)
104 aNewEdges.Substitute(aNewEdges.FindIndex(edge), LastEdge);
105 /////////////////////////
111 Standard_Boolean isDropped = Standard_False;
112 //merge edges and drop seams
114 for (i = 1; i <= edges.Length(); i++) {
115 TopoDS_Shape current = edges(i);
116 if(aNewEdges.Contains(current)) {
118 //aNewEdges.Remove(current);
119 TopoDS_Shape LastEdge = aNewEdges(aNewEdges.Extent());
120 aNewEdges.RemoveLast();
121 if (aNewEdges.FindIndex(current) != 0)
122 aNewEdges.Substitute(aNewEdges.FindIndex(current), LastEdge);
123 /////////////////////////
128 isDropped = Standard_True;
134 //add edges to the sequemce
135 for (i = 1; i <= aNewEdges.Extent(); i++)
136 edges.Append(aNewEdges(i));
141 //=======================================================================
142 //function : getCylinder
144 //=======================================================================
145 static Standard_Boolean getCylinder(Handle(Geom_Surface)& theInSurface,
146 gp_Cylinder& theOutCylinder)
148 Standard_Boolean isCylinder = Standard_False;
150 if (theInSurface->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
151 Handle(Geom_CylindricalSurface) aGC = Handle(Geom_CylindricalSurface)::DownCast(theInSurface);
153 theOutCylinder = aGC->Cylinder();
154 isCylinder = Standard_True;
156 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution))) {
157 Handle(Geom_SurfaceOfRevolution) aRS =
158 Handle(Geom_SurfaceOfRevolution)::DownCast(theInSurface);
159 Handle(Geom_Curve) aBasis = aRS->BasisCurve();
160 if (aBasis->IsKind(STANDARD_TYPE(Geom_Line))) {
161 Handle(Geom_Line) aBasisLine = Handle(Geom_Line)::DownCast(aBasis);
162 gp_Dir aDir = aRS->Direction();
163 gp_Dir aBasisDir = aBasisLine->Position().Direction();
164 if (aBasisDir.IsParallel(aDir, Precision::Angular())) {
165 // basis line is parallel to the revolution axis: it is a cylinder
166 gp_Pnt aLoc = aRS->Location();
167 Standard_Real aR = aBasisLine->Lin().Distance(aLoc);
168 gp_Ax3 aCylAx (aLoc, aDir);
170 theOutCylinder = gp_Cylinder(aCylAx, aR);
171 isCylinder = Standard_True;
175 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion))) {
176 Handle(Geom_SurfaceOfLinearExtrusion) aLES =
177 Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(theInSurface);
178 Handle(Geom_Curve) aBasis = aLES->BasisCurve();
179 if (aBasis->IsKind(STANDARD_TYPE(Geom_Circle))) {
180 Handle(Geom_Circle) aBasisCircle = Handle(Geom_Circle)::DownCast(aBasis);
181 gp_Dir aDir = aLES->Direction();
182 gp_Dir aBasisDir = aBasisCircle->Position().Direction();
183 if (aBasisDir.IsParallel(aDir, Precision::Angular())) {
184 // basis circle is normal to the extrusion axis: it is a cylinder
185 gp_Pnt aLoc = aBasisCircle->Location();
186 Standard_Real aR = aBasisCircle->Radius();
187 gp_Ax3 aCylAx (aLoc, aDir);
189 theOutCylinder = gp_Cylinder(aCylAx, aR);
190 isCylinder = Standard_True;
200 //=======================================================================
201 //function : ClearRts
203 //=======================================================================
204 static Handle(Geom_Surface) ClearRts(const Handle(Geom_Surface)& aSurface)
206 if(aSurface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
207 Handle(Geom_RectangularTrimmedSurface) rts =
208 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
209 return rts->BasisSurface();
214 //=======================================================================
215 //function : IsSameDomain
217 //=======================================================================
218 static Standard_Boolean IsSameDomain(const TopoDS_Face& aFace,
219 const TopoDS_Face& aCheckedFace)
221 //checking the same handles
222 TopLoc_Location L1, L2;
223 Handle(Geom_Surface) S1, S2;
225 S1 = BRep_Tool::Surface(aFace,L1);
226 S2 = BRep_Tool::Surface(aCheckedFace,L2);
228 if (S1 == S2 && L1 == L2)
229 return Standard_True;
231 // planar and cylindrical cases (IMP 20052)
232 Standard_Real aPrec = Precision::Confusion();
234 S1 = BRep_Tool::Surface(aFace);
235 S2 = BRep_Tool::Surface(aCheckedFace);
240 //Handle(Geom_OffsetSurface) aGOFS1, aGOFS2;
241 //aGOFS1 = Handle(Geom_OffsetSurface)::DownCast(S1);
242 //aGOFS2 = Handle(Geom_OffsetSurface)::DownCast(S2);
243 //if (!aGOFS1.IsNull()) S1 = aGOFS1->BasisSurface();
244 //if (!aGOFS2.IsNull()) S2 = aGOFS2->BasisSurface();
246 // case of two elementary surfaces: use OCCT tool
247 // elementary surfaces: ConicalSurface, CylindricalSurface,
248 // Plane, SphericalSurface and ToroidalSurface
249 if (S1->IsKind(STANDARD_TYPE(Geom_ElementarySurface)) &&
250 S2->IsKind(STANDARD_TYPE(Geom_ElementarySurface)))
252 Handle(GeomAdaptor_HSurface) aGA1 = new GeomAdaptor_HSurface(S1);
253 Handle(GeomAdaptor_HSurface) aGA2 = new GeomAdaptor_HSurface(S2);
255 Handle(BRepTopAdaptor_TopolTool) aTT1 = new BRepTopAdaptor_TopolTool();
256 Handle(BRepTopAdaptor_TopolTool) aTT2 = new BRepTopAdaptor_TopolTool();
259 IntPatch_ImpImpIntersection anIIInt (aGA1, aTT1, aGA2, aTT2, aPrec, aPrec);
260 if (!anIIInt.IsDone() || anIIInt.IsEmpty())
261 return Standard_False;
263 return anIIInt.TangentFaces();
265 catch (Standard_Failure) {
266 return Standard_False;
270 // case of two planar surfaces:
271 // all kinds of surfaces checked, including b-spline and bezier
272 GeomLib_IsPlanarSurface aPlanarityChecker1 (S1, aPrec);
273 if (aPlanarityChecker1.IsPlanar()) {
274 GeomLib_IsPlanarSurface aPlanarityChecker2 (S2, aPrec);
275 if (aPlanarityChecker2.IsPlanar()) {
276 gp_Pln aPln1 = aPlanarityChecker1.Plan();
277 gp_Pln aPln2 = aPlanarityChecker2.Plan();
279 if (aPln1.Position().Direction().IsParallel(aPln2.Position().Direction(),Precision::Angular()) &&
280 aPln1.Distance(aPln2) < aPrec) {
281 return Standard_True;
286 // case of two cylindrical surfaces, at least one of which is a swept surface
287 // swept surfaces: SurfaceOfLinearExtrusion, SurfaceOfRevolution
288 if ((S1->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
289 S1->IsKind(STANDARD_TYPE(Geom_SweptSurface))) &&
290 (S2->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
291 S2->IsKind(STANDARD_TYPE(Geom_SweptSurface))))
293 gp_Cylinder aCyl1, aCyl2;
294 if (getCylinder(S1, aCyl1) && getCylinder(S2, aCyl2)) {
295 if (fabs(aCyl1.Radius() - aCyl2.Radius()) < aPrec) {
296 gp_Dir aDir1 = aCyl1.Position().Direction();
297 gp_Dir aDir2 = aCyl2.Position().Direction();
298 if (aDir1.IsParallel(aDir2, Precision::Angular())) {
299 gp_Pnt aLoc1 = aCyl1.Location();
300 gp_Pnt aLoc2 = aCyl2.Location();
301 gp_Vec aVec12 (aLoc1, aLoc2);
302 if (aVec12.SquareMagnitude() < aPrec*aPrec ||
303 aVec12.IsParallel(aDir1, Precision::Angular())) {
304 return Standard_True;
311 return Standard_False;
314 //=======================================================================
315 //function : MovePCurves
317 //=======================================================================
318 static void MovePCurves(TopoDS_Face& aTarget,
319 const TopoDS_Face& aSource)
322 for(TopExp_Explorer wexp(aSource,TopAbs_WIRE);wexp.More();wexp.Next()) {
323 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(TopoDS::Wire(wexp.Current()),
324 aTarget, Precision::Confusion());
326 Standard_Boolean isReoredFailed = sfw->StatusReorder ( ShapeExtend_FAIL );
327 sfw->FixEdgeCurves();
332 sfw->FixDegenerated();
334 // remove degenerated edges from not degenerated points
335 ShapeAnalysis_Edge sae;
336 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
337 for(Standard_Integer i = 1; i<=sewd->NbEdges();i++) {
338 TopoDS_Edge E = sewd->Edge(i);
339 if(BRep_Tool::Degenerated(E)&&!sae.HasPCurve(E,aTarget)) {
345 TopoDS_Wire ResWire = sfw->Wire();
346 B.Add(aTarget,ResWire);
350 //=======================================================================
351 //function : GlueEdgesWithPCurves
352 //purpose : Glues the pcurves of the sequence of edges
353 // and glues their 3d curves
354 //=======================================================================
355 static TopoDS_Edge GlueEdgesWithPCurves(const TopTools_SequenceOfShape& aChain,
356 const TopoDS_Vertex& FirstVertex,
357 const TopoDS_Vertex& LastVertex)
359 Standard_Integer i, j;
361 TopoDS_Edge FirstEdge = TopoDS::Edge(aChain(1));
362 //TColGeom2d_SequenceOfCurve PCurveSeq;
363 TColGeom_SequenceOfSurface SurfSeq;
364 //TopTools_SequenceOfShape LocSeq;
366 BRep_ListIteratorOfListOfCurveRepresentation itr( (Handle(BRep_TEdge)::DownCast(FirstEdge.TShape()))->Curves() );
367 for (; itr.More(); itr.Next())
369 Handle(BRep_CurveRepresentation) CurveRep = itr.Value();
370 if (CurveRep->IsCurveOnSurface())
372 //PCurveSeq.Append(CurveRep->PCurve());
373 SurfSeq.Append(CurveRep->Surface());
375 TopoDS_Shape aLocShape;
376 aLocShape.Location(CurveRep->Location());
377 LocSeq.Append(aLocShape);
382 Standard_Real fpar, lpar;
383 BRep_Tool::Range(FirstEdge, fpar, lpar);
384 TopoDS_Edge PrevEdge = FirstEdge;
386 Standard_Real MaxTol = 0.;
391 Standard_Integer nb_curve = aChain.Length(); //number of curves
392 TColGeom_Array1OfBSplineCurve tab_c3d(0,nb_curve-1); //array of the curves
393 TColStd_Array1OfReal tabtolvertex(0,nb_curve-1); //(0,nb_curve-2); //array of the tolerances
395 TopoDS_Vertex PrevVertex = FirstVertex;
396 for (i = 1; i <= nb_curve; i++)
398 TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
399 TopoDS_Vertex VF, VL;
400 TopExp::Vertices(anEdge, VF, VL);
401 Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
403 Standard_Real Tol1 = BRep_Tool::Tolerance(VF);
404 Standard_Real Tol2 = BRep_Tool::Tolerance(VL);
412 TopExp::CommonVertex(PrevEdge, anEdge, CV);
413 Standard_Real Tol = BRep_Tool::Tolerance(CV);
414 tabtolvertex(i-2) = Tol;
417 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, fpar, lpar);
418 Handle(Geom_TrimmedCurve) aTrCurve = new Geom_TrimmedCurve(aCurve, fpar, lpar);
419 tab_c3d(i-1) = GeomConvert::CurveToBSplineCurve(aTrCurve);
420 GeomConvert::C0BSplineToC1BSplineCurve(tab_c3d(i-1), Precision::Confusion());
422 tab_c3d(i-1)->Reverse();
423 PrevVertex = (ToReverse)? VF : VL;
426 Handle(TColGeom_HArray1OfBSplineCurve) concatcurve; //array of the concatenated curves
427 Handle(TColStd_HArray1OfInteger) ArrayOfIndices; //array of the remining Vertex
428 GeomConvert::ConcatC1(tab_c3d,
433 Precision::Confusion()); //C1 concatenation
435 if (concatcurve->Length() > 1)
437 GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
439 for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
440 Concat.Add( concatcurve->Value(i), MaxTol, Standard_True );
442 concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
444 Handle(Geom_BSplineCurve) ResCurve = concatcurve->Value(concatcurve->Lower());
446 TColGeom2d_SequenceOfBoundedCurve ResPCurves;
447 TopLoc_Location aLoc;
448 for (j = 1; j <= SurfSeq.Length(); j++)
450 TColGeom2d_Array1OfBSplineCurve tab_c2d(0,nb_curve-1); //array of the pcurves
452 PrevVertex = FirstVertex;
453 PrevEdge = FirstEdge;
454 //TopLoc_Location theLoc = LocSeq(j).Location();
455 for (i = 1; i <= nb_curve; i++)
457 TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
458 TopoDS_Vertex VF, VL;
459 TopExp::Vertices(anEdge, VF, VL);
460 Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
463 Handle(Geom2d_Curve) aPCurve =
464 BRep_Tool::CurveOnSurface(anEdge, SurfSeq(j), anEdge.Location()*theLoc, fpar, lpar);
466 Handle(Geom2d_Curve) aPCurve =
467 BRep_Tool::CurveOnSurface(anEdge, SurfSeq(j), aLoc, fpar, lpar);
468 Handle(Geom2d_TrimmedCurve) aTrPCurve = new Geom2d_TrimmedCurve(aPCurve, fpar, lpar);
469 tab_c2d(i-1) = Geom2dConvert::CurveToBSplineCurve(aTrPCurve);
470 Geom2dConvert::C0BSplineToC1BSplineCurve(tab_c2d(i-1), Precision::Confusion());
472 tab_c2d(i-1)->Reverse();
473 PrevVertex = (ToReverse)? VF : VL;
476 Handle(TColGeom2d_HArray1OfBSplineCurve) concatc2d; //array of the concatenated curves
477 Handle(TColStd_HArray1OfInteger) ArrayOfInd2d; //array of the remining Vertex
478 Geom2dConvert::ConcatC1(tab_c2d,
483 Precision::Confusion()); //C1 concatenation
485 if (concatc2d->Length() > 1)
487 Geom2dConvert_CompCurveToBSplineCurve Concat2d(concatc2d->Value(concatc2d->Lower()));
489 for (i = concatc2d->Lower()+1; i <= concatc2d->Upper(); i++)
490 Concat2d.Add( concatc2d->Value(i), MaxTol, Standard_True );
492 concatc2d->SetValue(concatc2d->Lower(), Concat2d.BSplineCurve());
494 Handle(Geom2d_BSplineCurve) aResPCurve = concatc2d->Value(concatc2d->Lower());
495 ResPCurves.Append(aResPCurve);
498 ResEdge = BRepLib_MakeEdge(ResCurve,
499 FirstVertex, LastVertex,
500 ResCurve->FirstParameter(), ResCurve->LastParameter());
501 BB.SameRange(ResEdge, Standard_False);
502 BB.SameParameter(ResEdge, Standard_False);
503 for (j = 1; j <= ResPCurves.Length(); j++)
505 BB.UpdateEdge(ResEdge, ResPCurves(j), SurfSeq(j), aLoc, MaxTol);
506 BB.Range(ResEdge, SurfSeq(j), aLoc, ResPCurves(j)->FirstParameter(), ResPCurves(j)->LastParameter());
509 BRepLib::SameParameter(ResEdge, MaxTol, Standard_True);
514 //=======================================================================
515 //function : MergeEdges
517 //=======================================================================
518 static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
519 const TopoDS_Face& /*aFace*/,
520 const Standard_Real Tol,
521 const Standard_Boolean ConcatBSplines,
524 // make chain for union
526 ShapeAnalysis_Edge sae;
527 TopoDS_Edge FirstE = TopoDS::Edge(SeqEdges.Value(1));
528 TopoDS_Edge LastE = FirstE;
529 TopoDS_Vertex VF = sae.FirstVertex(FirstE);
530 TopoDS_Vertex VL = sae.LastVertex(LastE);
531 TopTools_SequenceOfShape aChain;
532 aChain.Append(FirstE);
533 TColStd_MapOfInteger IndUsedEdges;
536 for (j = 1; j <= SeqEdges.Length(); j++)
538 TopoDS_Edge anEdge = TopoDS::Edge(SeqEdges(j));
539 if (BRep_Tool::Degenerated(anEdge))
540 return Standard_False;
543 for(j=2; j<=SeqEdges.Length(); j++) {
544 for(Standard_Integer k=2; k<=SeqEdges.Length(); k++) {
545 if(IndUsedEdges.Contains(k)) continue;
546 TopoDS_Edge edge = TopoDS::Edge(SeqEdges.Value(k));
547 TopoDS_Vertex VF2 = sae.FirstVertex(edge);
548 TopoDS_Vertex VL2 = sae.LastVertex(edge);
549 if(sae.FirstVertex(edge).IsSame(VL)) {
552 VL = sae.LastVertex(LastE);
555 else if(sae.LastVertex(edge).IsSame(VF)) {
556 aChain.Prepend(edge);
558 VF = sae.FirstVertex(FirstE);
563 if(aChain.Length()<SeqEdges.Length()) {
564 #ifdef SHAPEUPGRADE_DEB
565 cout<<"can not create correct chain..."<<endl;
567 return Standard_False;
569 // union edges in chain
570 // first step: union lines and circles
572 Standard_Real fp1,lp1,fp2,lp2;
573 for(j=1; j<aChain.Length(); j++) {
574 TopoDS_Edge edge1 = TopoDS::Edge(aChain.Value(j));
575 Handle(Geom_Curve) c3d1 = BRep_Tool::Curve(edge1,Loc,fp1,lp1);
576 if(c3d1.IsNull()) break;
577 while(c3d1->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
578 Handle(Geom_TrimmedCurve) tc =
579 Handle(Geom_TrimmedCurve)::DownCast(c3d1);
580 c3d1 = tc->BasisCurve();
582 TopoDS_Edge edge2 = TopoDS::Edge(aChain.Value(j+1));
583 Handle(Geom_Curve) c3d2 = BRep_Tool::Curve(edge2,Loc,fp2,lp2);
584 if(c3d2.IsNull()) break;
585 while(c3d2->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
586 Handle(Geom_TrimmedCurve) tc =
587 Handle(Geom_TrimmedCurve)::DownCast(c3d2);
588 c3d2 = tc->BasisCurve();
590 if( c3d1->IsKind(STANDARD_TYPE(Geom_Line)) && c3d2->IsKind(STANDARD_TYPE(Geom_Line)) ) {
592 Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(c3d1);
593 Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(c3d2);
594 gp_Dir Dir1 = L1->Position().Direction();
595 gp_Dir Dir2 = L2->Position().Direction();
596 //if(!Dir1.IsEqual(Dir2,Precision::Angular())) {
597 //if(!Dir1.IsParallel(Dir2,Precision::Angular())) {
598 if(!Dir1.IsParallel(Dir2,Tol)) {
601 // can union lines => create new edge
602 TopoDS_Vertex V1 = sae.FirstVertex(edge1);
603 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
604 TopoDS_Vertex V2 = sae.LastVertex(edge2);
605 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
607 Handle(Geom_Line) L = new Geom_Line(gp_Ax1(PV1,Vec));
608 Standard_Real dist = PV1.Distance(PV2);
609 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(L,0.0,dist);
611 B.MakeEdge (E,tc,Precision::Confusion());
612 B.Add (E,V1); B.Add (E,V2);
613 B.UpdateVertex(V1, 0., E, 0.);
614 B.UpdateVertex(V2, dist, E, 0.);
616 //sfe.FixAddPCurve(E,aFace,Standard_False);
617 //sfe.FixSameParameter(E);
619 aChain.SetValue(j,E);
622 if( c3d1->IsKind(STANDARD_TYPE(Geom_Circle)) && c3d2->IsKind(STANDARD_TYPE(Geom_Circle)) ) {
624 Handle(Geom_Circle) C1 = Handle(Geom_Circle)::DownCast(c3d1);
625 Handle(Geom_Circle) C2 = Handle(Geom_Circle)::DownCast(c3d2);
626 gp_Pnt P01 = C1->Location();
627 gp_Pnt P02 = C2->Location();
628 if (P01.Distance(P02) > Precision::Confusion()) continue;
629 // can union circles => create new edge
630 TopoDS_Vertex V1 = sae.FirstVertex(edge1);
631 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
632 TopoDS_Vertex V2 = sae.LastVertex(edge2);
633 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
634 TopoDS_Vertex VM = sae.LastVertex(edge1);
635 gp_Pnt PVM = BRep_Tool::Pnt(VM);
636 GC_MakeCircle MC (PV1,PVM,PV2);
637 Handle(Geom_Circle) C = MC.Value();
639 if (!MC.IsDone() || C.IsNull()) {
640 // jfa for Mantis issue 0020228
641 if (PV1.Distance(PV2) > Precision::Confusion()) continue;
644 B.MakeEdge (E,C,Precision::Confusion());
649 gp_Pnt P0 = C->Location();
650 gp_Dir D1(gp_Vec(P0,PV1));
651 gp_Dir D2(gp_Vec(P0,PV2));
652 Standard_Real fpar = C->XAxis().Direction().Angle(D1);
653 if(fabs(fpar)>Precision::Confusion()) {
655 gp_Dir ND = C->XAxis().Direction().Crossed(D1);
656 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
660 Standard_Real lpar = C->XAxis().Direction().Angle(D2);
661 if(fabs(lpar)>Precision::Confusion()) {
663 gp_Dir ND = C->XAxis().Direction().Crossed(D2);
664 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
668 if (lpar < fpar) lpar += 2*M_PI;
669 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
670 B.MakeEdge (E,tc,Precision::Confusion());
673 B.UpdateVertex(V1, fpar, E, 0.);
674 B.UpdateVertex(V2, lpar, E, 0.);
677 aChain.SetValue(j,E);
681 if (j < aChain.Length()) {
682 #ifdef SHAPEUPGRADE_DEB
683 cout<<"null curve3d in edge..."<<endl;
685 return Standard_False;
687 if (aChain.Length() > 1 && ConcatBSplines) {
688 // second step: union edges with various curves
689 // skl for bug 0020052 from Mantis: perform such unions
690 // only if curves are bspline or bezier
691 bool NeedUnion = true;
692 for(j=1; j<=aChain.Length(); j++) {
693 TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
694 Handle(Geom_Curve) c3d = BRep_Tool::Curve(edge,Loc,fp1,lp1);
695 if(c3d.IsNull()) continue;
696 while(c3d->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
697 Handle(Geom_TrimmedCurve) tc =
698 Handle(Geom_TrimmedCurve)::DownCast(c3d);
699 c3d = tc->BasisCurve();
701 if( ( c3d->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ||
702 c3d->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ) ) continue;
707 #ifdef SHAPEUPGRADE_DEB
708 cout<<"can not make analitical union => make approximation"<<endl;
710 TopoDS_Edge E = GlueEdgesWithPCurves(aChain, VF, VL);
711 aChain.SetValue(1,E);
714 #ifdef SHAPEUPGRADE_DEB
715 cout<<"can not make approximation for such types of curves"<<endl;
717 return Standard_False;
721 anEdge = TopoDS::Edge(aChain.Value(1));
722 return Standard_True;
725 //=======================================================================
726 //function : ShapeUpgrade_UnifySameDomain
727 //purpose : Constructor
728 //=======================================================================
730 ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain()
732 myUnifyEdges = Standard_True;
733 myUnifyFaces = Standard_True;
734 myConcatBSplines = Standard_False;
736 myContext = new ShapeBuild_ReShape;
739 //=======================================================================
740 //function : ShapeUpgrade_UnifySameDomain
741 //purpose : Constructor
742 //=======================================================================
744 ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain(const TopoDS_Shape& aShape,
745 const Standard_Boolean UnifyEdges,
746 const Standard_Boolean UnifyFaces,
747 const Standard_Boolean ConcatBSplines)
749 myInitShape = aShape;
751 myUnifyEdges = UnifyEdges;
752 myUnifyFaces = UnifyFaces;
753 myConcatBSplines = ConcatBSplines;
755 myContext = new ShapeBuild_ReShape;
758 //=======================================================================
759 //function : Initialize
761 //=======================================================================
763 void ShapeUpgrade_UnifySameDomain::Initialize(const TopoDS_Shape& aShape,
764 const Standard_Boolean UnifyEdges,
765 const Standard_Boolean UnifyFaces,
766 const Standard_Boolean ConcatBSplines)
768 myInitShape = aShape;
770 myUnifyEdges = UnifyEdges;
771 myUnifyFaces = UnifyFaces;
772 myConcatBSplines = ConcatBSplines;
775 //myOldNewMap.Clear();
776 //myGenerated.Clear();
779 //=======================================================================
780 //function : UnifyFaces
782 //=======================================================================
784 void ShapeUpgrade_UnifySameDomain::UnifyFaces()
786 //Handle(ShapeBuild_ReShape) myContext = new ShapeBuild_ReShape;
787 TopoDS_Shape aResShape = myContext->Apply(myShape);
789 // processing each shell
790 TopExp_Explorer exps;
791 for (exps.Init(myShape, TopAbs_SHELL); exps.More(); exps.Next()) {
792 TopoDS_Shell aShell = TopoDS::Shell(exps.Current());
794 // creating map of edge faces
795 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
796 TopExp::MapShapesAndAncestors(aShell, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
798 // map of processed shapes
799 TopTools_MapOfShape aProcessed;
801 //Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
803 Standard_Integer NbModif = 0;
804 Standard_Boolean hasFailed = Standard_False;
805 Standard_Real tol = Precision::Confusion();
808 Standard_Integer nbf = 0;
810 TopTools_MapOfShape mapF;
811 for (exp.Init(aShell, TopAbs_FACE); exp.More(); exp.Next()) {
812 if (mapF.Add(exp.Current()))
816 // processing each face
818 for (exp.Init(aShell, TopAbs_FACE); exp.More(); exp.Next()) {
819 TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
821 if (aProcessed.Contains(aFace))
824 Standard_Integer dummy;
825 TopTools_SequenceOfShape edges;
826 AddOrdinaryEdges(edges,aFace,dummy);
828 TopTools_SequenceOfShape faces;
831 //surface and location to construct result
832 TopLoc_Location aBaseLocation;
833 Handle(Geom_Surface) aBaseSurface = BRep_Tool::Surface(aFace,aBaseLocation);
834 aBaseSurface = ClearRts(aBaseSurface);
836 // find adjacent faces to union
838 for (i = 1; i <= edges.Length(); i++) {
839 TopoDS_Edge edge = TopoDS::Edge(edges(i));
840 if (BRep_Tool::Degenerated(edge))
843 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
844 TopTools_ListIteratorOfListOfShape anIter(aList);
845 for (; anIter.More(); anIter.Next()) {
846 TopoDS_Face anCheckedFace = TopoDS::Face(anIter.Value().Oriented(TopAbs_FORWARD));
847 if (anCheckedFace.IsSame(aFace))
850 if (aProcessed.Contains(anCheckedFace))
853 if (IsSameDomain(aFace,anCheckedFace)) {
855 if (aList.Extent() != 2) {
856 // non mainfold case is not processed
861 TopoDS_Face aMockUpFace;
863 B.MakeFace(aMockUpFace,aBaseSurface,aBaseLocation,0.);
864 MovePCurves(aMockUpFace,anCheckedFace);
866 if (AddOrdinaryEdges(edges,aMockUpFace,dummy)) {
867 // sequence edges is modified
871 faces.Append(anCheckedFace);
872 aProcessed.Add(anCheckedFace);
878 // all faces collected in the sequence. Perform union of faces
879 if (faces.Length() > 1) {
883 B.MakeFace(aResult,aBaseSurface,aBaseLocation,0);
884 Standard_Integer nbWires = 0;
887 while (edges.Length()>0) {
889 Standard_Boolean isEdge3d = Standard_False;
891 TopTools_MapOfShape aVertices;
895 TopoDS_Edge anEdge = TopoDS::Edge(edges(1));
898 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
901 TopExp::Vertices(anEdge,V1,V2);
905 Standard_Boolean isNewFound = Standard_False;
907 isNewFound = Standard_False;
908 for(Standard_Integer j = 1; j <= edges.Length(); j++) {
909 anEdge = TopoDS::Edge(edges(j));
910 TopExp::Vertices(anEdge,V1,V2);
911 if(aVertices.Contains(V1) || aVertices.Contains(V2)) {
912 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
918 isNewFound = Standard_True;
921 } while (isNewFound);
923 // sorting any type of edges
924 aWire.Closed (BRep_Tool::IsClosed (aWire));
925 aWire = TopoDS::Wire(myContext->Apply(aWire));
927 TopoDS_Face tmpF = TopoDS::Face(myContext->Apply(faces(1).Oriented(TopAbs_FORWARD)));
928 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(aWire,tmpF,Precision::Confusion());
930 Standard_Boolean isDegRemoved = Standard_False;
931 if(!sfw->StatusReorder ( ShapeExtend_FAIL )) {
932 // clear degenerated edges if at least one with 3d curve exist
934 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
935 for(Standard_Integer j = 1; j<=sewd->NbEdges();j++) {
936 TopoDS_Edge E = sewd->Edge(j);
937 if(BRep_Tool::Degenerated(E)) {
939 isDegRemoved = Standard_True;
946 sfw->FixDegenerated();
948 TopoDS_Wire aWireFixed = sfw->Wire();
949 //aContext->Replace(aWire,aWireFixed);
950 myContext->Replace(aWire,aWireFixed);
953 if (!myOldNewMap.IsBound(aWire))
955 TopTools_ListOfShape EmptyList;
956 myOldNewMap.Bind(aWire, EmptyList);
958 myOldNewMap(aWire).Clear();
959 myOldNewMap(aWire).Append(aWireFixed);
963 // add resulting wire
965 B.Add(aResult,aWireFixed);
969 Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
970 Standard_Integer nbEdges = sbwd->NbEdges();
971 // sort degenerated edges and create one edge instead of several ones
972 ShapeAnalysis_WireOrder sawo(Standard_False, 0);
973 ShapeAnalysis_Edge sae;
974 Standard_Integer aLastEdge = nbEdges;
975 for(Standard_Integer j = 1; j <= nbEdges; j++) {
977 //smh protection on NULL pcurve
978 Handle(Geom2d_Curve) c2d;
979 if(!sae.PCurve(sbwd->Edge(j),tmpF,c2d,f,l)) {
983 sawo.Add(c2d->Value(f).XY(),c2d->Value(l).XY());
987 // constructind one degenerative edge
988 gp_XY aStart, anEnd, tmp;
989 Standard_Integer nbFirst = sawo.Ordered(1);
990 TopoDS_Edge anOrigE = TopoDS::Edge(sbwd->Edge(nbFirst).Oriented(TopAbs_FORWARD));
992 TopoDS_Vertex aDummyV;
993 TopoDS_Edge E = sbe.CopyReplaceVertices(anOrigE,aDummyV,aDummyV);
994 sawo.XY(nbFirst,aStart,tmp);
995 sawo.XY(sawo.Ordered(aLastEdge),tmp,anEnd);
997 gp_XY aVec = anEnd-aStart;
998 Handle(Geom2d_Line) aLine = new Geom2d_Line(aStart,gp_Dir2d(anEnd-aStart));
1000 B.UpdateEdge(E,aLine,tmpF,0.);
1001 B.Range(E,tmpF,0.,aVec.Modulus());
1002 Handle(Geom_Curve) C3d;
1003 B.UpdateEdge(E,C3d,0.);
1004 B.Degenerated(E,Standard_True);
1008 aW.Closed (Standard_True);
1013 // perform substitution of face
1014 //aContext->Replace(aContext->Apply(aFace),aResult);
1015 myContext->Replace(myContext->Apply(aFace),aResult);
1018 if (!myOldNewMap.IsBound(aFace))
1020 TopTools_ListOfShape EmptyList;
1021 myOldNewMap.Bind(aFace, EmptyList);
1023 myOldNewMap(aFace).Clear();
1024 myOldNewMap(aFace).Append(aResult);
1028 ShapeFix_Face sff (aResult);
1029 //Intializing by tolerances
1030 sff.SetPrecision(Precision::Confusion());
1031 sff.SetMinTolerance(tol);
1032 sff.SetMaxTolerance(1.);
1034 sff.FixOrientationMode() = 0;
1035 //sff.FixWireMode() = 0;
1036 //sff.SetContext(aContext);
1037 sff.SetContext(myContext);
1038 // Applying the fixes
1040 if(sff.Status(ShapeExtend_FAIL))
1041 hasFailed = Standard_True;
1043 // breaking down to several faces
1044 //TopoDS_Shape theResult = aContext->Apply(aResult);
1045 TopoDS_Shape theResult = myContext->Apply(aResult);
1046 for (TopExp_Explorer aFaceExp (theResult,TopAbs_FACE); aFaceExp.More(); aFaceExp.Next()) {
1047 TopoDS_Face aCurrent = TopoDS::Face(aFaceExp.Current().Oriented(TopAbs_FORWARD));
1048 Handle(TColGeom_HArray2OfSurface) grid = new TColGeom_HArray2OfSurface ( 1, 1, 1, 1 );
1049 grid->SetValue ( 1, 1, aBaseSurface );
1050 Handle(ShapeExtend_CompositeSurface) G = new ShapeExtend_CompositeSurface ( grid );
1051 ShapeFix_ComposeShell CompShell;
1052 CompShell.Init ( G, aBaseLocation, aCurrent, ::Precision::Confusion() );//myPrecision
1053 //CompShell.SetContext( aContext );
1054 CompShell.SetContext( myContext );
1056 TopTools_SequenceOfShape parts;
1057 ShapeFix_SequenceOfWireSegment wires;
1058 for(TopExp_Explorer W_Exp(aCurrent,TopAbs_WIRE);W_Exp.More();W_Exp.Next()) {
1059 Handle(ShapeExtend_WireData) sbwd =
1060 new ShapeExtend_WireData ( TopoDS::Wire(W_Exp.Current() ));
1061 ShapeFix_WireSegment seg ( sbwd, TopAbs_REVERSED );
1065 CompShell.DispatchWires ( parts,wires );
1066 for (Standard_Integer j=1; j <= parts.Length(); j++ ) {
1067 ShapeFix_Face aFixOrient(TopoDS::Face(parts(j)));
1068 //aFixOrient.SetContext(aContext);
1069 aFixOrient.SetContext(myContext);
1070 aFixOrient.FixOrientation();
1073 TopoDS_Shape CompRes;
1074 if ( faces.Length() !=1 ) {
1077 for ( i=1; i <= parts.Length(); i++ )
1078 B.Add ( S, parts(i) );
1079 S.Closed (BRep_Tool::IsClosed (S));
1082 else CompRes = parts(1);
1084 //aContext->Replace(aCurrent,CompRes);
1085 myContext->Replace(aCurrent,CompRes);
1088 if (!myOldNewMap.IsBound(aCurrent))
1090 TopTools_ListOfShape EmptyList;
1091 myOldNewMap.Bind(aCurrent, EmptyList);
1093 myOldNewMap(aCurrent).Clear();
1094 myOldNewMap(aCurrent).Append(CompRes);
1099 // remove the remaining faces
1100 for(i = 2; i <= faces.Length(); i++)
1101 //aContext->Remove(faces(i));
1102 myContext->Remove(faces(i));
1104 } // end processing each face
1106 //TopoDS_Shape aResult = Shape;
1107 if (NbModif > 0 && !hasFailed) {
1108 //TopoDS_Shape aResult = aContext->Apply(aShell);
1109 TopoDS_Shape aResult = myContext->Apply(aShell);
1112 for (exp.Init(aResult,TopAbs_EDGE); exp.More(); exp.Next()) {
1113 TopoDS_Edge E = TopoDS::Edge(exp.Current());
1114 sfe.FixVertexTolerance (E);
1115 // ptv add fix same parameter
1116 sfe.FixSameParameter(E, Precision::Confusion());
1119 myContext->Replace(aShell, aResult);
1122 if (!myOldNewMap.IsBound(aShell))
1124 TopTools_ListOfShape EmptyList;
1125 myOldNewMap.Bind(aShell, EmptyList);
1127 myOldNewMap(aShell).Clear();
1128 myOldNewMap(aShell).Append(aResult);
1134 for (exp.Init(aShell, TopAbs_FACE); exp.More(); exp.Next()) {
1135 TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
1136 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
1137 sfw->SetContext(myContext);
1138 sfw->SetPrecision(Precision::Confusion());
1139 sfw->SetMinTolerance(Precision::Confusion());
1140 sfw->SetMaxTolerance(1.);
1141 sfw->SetFace(aFace);
1142 for (TopoDS_Iterator iter (aFace,Standard_False); iter.More(); iter.Next()) {
1143 TopoDS_Wire wire = TopoDS::Wire(iter.Value());
1150 } // end processing each shell
1152 myShape = myContext->Apply(myShape);
1155 //=======================================================================
1156 //function : UnifyEdges
1158 //=======================================================================
1160 void ShapeUpgrade_UnifySameDomain::UnifyEdges()
1162 Standard_Real Tol = Precision::Confusion();
1164 //Handle(ShapeBuild_ReShape) myContext = new ShapeBuild_ReShape;
1165 Standard_Real myTolerance = Precision::Confusion();
1166 TopoDS_Shape aResult = myContext->Apply(myShape);
1168 // processing each solid
1169 TopAbs_ShapeEnum aType = TopAbs_SOLID;
1170 TopExp_Explorer exps (myShape, aType);
1172 aType = TopAbs_SHELL;
1173 exps.Init(myShape, aType);
1175 for (; exps.More(); exps.Next()) {
1176 //TopoDS_Solid aSolid = TopoDS::Solid(exps.Current());
1177 TopoDS_Shape aSolid = exps.Current();
1179 TopTools_IndexedMapOfShape ChangedFaces;
1181 // creating map of edge faces
1182 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
1183 TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
1185 //Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
1186 TopoDS_Shape aRes = aSolid;
1187 //aRes = aContext->Apply(aSolid);
1188 aRes = myContext->Apply(aSolid);
1190 // processing each face
1191 TopExp_Explorer exp;
1192 for (exp.Init(aRes, TopAbs_FACE); exp.More(); exp.Next()) {
1193 //TopoDS_Face aFace = TopoDS::Face(aContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
1194 TopoDS_Face aFace = TopoDS::Face(myContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
1195 TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
1197 for (TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
1198 TopoDS_Edge edge = TopoDS::Edge(expe.Current());
1199 if (!aMapEdgeFaces.Contains(edge)) continue;
1200 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
1201 TopTools_ListIteratorOfListOfShape anIter(aList);
1202 for ( ; anIter.More(); anIter.Next()) {
1203 TopoDS_Face face = TopoDS::Face(anIter.Value());
1204 //TopoDS_Face face1 = TopoDS::Face(aContext->Apply(anIter.Value()));
1205 TopoDS_Face face1 = TopoDS::Face(myContext->Apply(anIter.Value()));
1206 if (face1.IsSame(aFace)) continue;
1207 if (aMapFacesEdges.Contains(face)) {
1208 aMapFacesEdges.ChangeFromKey(face).Append(edge);
1211 TopTools_ListOfShape ListEdges;
1212 ListEdges.Append(edge);
1213 aMapFacesEdges.Add(face,ListEdges);
1218 for (Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++) {
1219 const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
1220 TopTools_SequenceOfShape SeqEdges;
1221 TopTools_ListIteratorOfListOfShape anIter(ListEdges);
1222 for ( ; anIter.More(); anIter.Next()) {
1223 SeqEdges.Append(anIter.Value());
1225 if (SeqEdges.Length()==1) continue;
1227 if ( MergeEdges(SeqEdges,aFace,Tol,myConcatBSplines,E) ) {
1228 // now we have only one edge - aChain.Value(1)
1229 // we have to replace old ListEdges with this new edge
1230 //aContext->Replace(SeqEdges(1),E);
1231 myContext->Replace(SeqEdges(1),E);
1233 for (j = 2; j <= SeqEdges.Length(); j++) {
1234 //aContext->Remove(SeqEdges(j));
1235 myContext->Remove(SeqEdges(j));
1236 //myOldNewMap.Bind(SeqEdges(j), E);
1240 for (j = 1; j <= SeqEdges.Length(); j++)
1242 if (!myOldNewMap.IsBound(SeqEdges(j)))
1244 TopTools_ListOfShape EmptyList;
1245 myOldNewMap.Bind(SeqEdges(j), EmptyList);
1247 myOldNewMap(SeqEdges(j)).Clear();
1248 myOldNewMap(SeqEdges(j)).Append(E);
1253 TopoDS_Face tmpF = TopoDS::Face(exp.Current());
1254 if ( !ChangedFaces.Contains(tmpF) )
1255 ChangedFaces.Add(tmpF);
1256 tmpF = TopoDS::Face(aMapFacesEdges.FindKey(i));
1257 if ( !ChangedFaces.Contains(tmpF) )
1258 ChangedFaces.Add(tmpF);
1262 } // end processing each face
1264 // fix changed faces and replace them in the local context
1265 for (Standard_Integer i=1; i<=ChangedFaces.Extent(); i++) {
1266 //TopoDS_Face aFace = TopoDS::Face(aContext->Apply(ChangedFaces.FindKey(i)));
1267 TopoDS_Face aFace = TopoDS::Face(myContext->Apply(ChangedFaces.FindKey(i)));
1268 Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFace);
1269 sff->SetContext(myContext);
1270 sff->SetPrecision(myTolerance);
1271 sff->SetMinTolerance(myTolerance);
1272 sff->SetMaxTolerance(Max(1.,myTolerance*1000.));
1274 TopoDS_Shape aNewFace = sff->Face();
1275 //aContext->Replace(aFace,aNewFace);
1276 myContext->Replace(aFace,aNewFace);
1279 if (!myOldNewMap.IsBound(aFace))
1281 TopTools_ListOfShape EmptyList;
1282 myOldNewMap.Bind(aFace, EmptyList);
1284 myOldNewMap(aFace).Clear();
1285 myOldNewMap(aFace).Append(aNewFace);
1290 if (ChangedFaces.Extent() > 0) {
1291 // fix changed shell and replace it in the local context
1292 //TopoDS_Shape aRes1 = aContext->Apply(aRes);
1293 TopoDS_Shape aRes1 = myContext->Apply(aRes);
1294 TopExp_Explorer expsh;
1295 for (expsh.Init(aRes1, TopAbs_SHELL); expsh.More(); expsh.Next()) {
1296 TopoDS_Shell aShell = TopoDS::Shell(expsh.Current());
1297 Handle(ShapeFix_Shell) sfsh = new ShapeFix_Shell;
1298 sfsh->FixFaceOrientation(aShell);
1299 TopoDS_Shape aNewShell = sfsh->Shell();
1300 //aContext->Replace(aShell,aNewShell);
1301 myContext->Replace(aShell,aNewShell);
1304 if (!myOldNewMap.IsBound(aShell))
1306 TopTools_ListOfShape EmptyList;
1307 myOldNewMap.Bind(aShell, EmptyList);
1309 myOldNewMap(aShell).Clear();
1310 myOldNewMap(aShell).Append(aNewShell);
1314 //TopoDS_Shape aRes2 = aContext->Apply(aRes1);
1315 TopoDS_Shape aRes2 = myContext->Apply(aRes1);
1316 // put new solid into global context
1317 myContext->Replace(aSolid,aRes2);
1320 if (!myOldNewMap.IsBound(aSolid))
1322 TopTools_ListOfShape EmptyList;
1323 myOldNewMap.Bind(aSolid, EmptyList);
1325 myOldNewMap(aSolid).Clear();
1326 myOldNewMap(aSolid).Append(aRes2);
1331 } // end processing each solid
1333 myShape = myContext->Apply(myShape);
1336 //=======================================================================
1337 //function : UnifyFacesAndEdges
1339 //=======================================================================
1341 void ShapeUpgrade_UnifySameDomain::UnifyFacesAndEdges()
1346 ShapeUpgrade_RemoveLocations RemLoc;
1347 RemLoc.Remove(myShape);
1348 myShape = RemLoc.GetResult();
1354 //=======================================================================
1356 //purpose : builds the resulting shape
1357 //======================================================================
1358 void ShapeUpgrade_UnifySameDomain::Build()
1360 if (myUnifyFaces && myUnifyEdges)
1361 UnifyFacesAndEdges();
1363 else if (myUnifyEdges)
1365 else if (myUnifyFaces)
1371 //=======================================================================
1373 //purpose : give the resulting shape
1374 //=======================================================================
1375 const TopoDS_Shape& ShapeUpgrade_UnifySameDomain::Shape() const
1380 //=======================================================================
1381 //function : Generated
1382 //purpose : returns the new shape from the old one
1383 //=======================================================================
1384 TopoDS_Shape ShapeUpgrade_UnifySameDomain::Generated(const TopoDS_Shape& aShape) const
1386 TopoDS_Shape aNewShape = myContext->Apply(aShape);
1388 if (aNewShape.IsNull())
1389 aNewShape = myOldNewMap(aShape);