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>
80 #include <TopTools_MapOfShape.hxx>
83 //=======================================================================
84 //function : AddOrdinaryEdges
86 //=======================================================================
87 // adds edges from the shape to the sequence
88 // seams and equal edges are dropped
89 // Returns true if one of original edges dropped
90 static Standard_Boolean AddOrdinaryEdges(TopTools_SequenceOfShape& edges,
91 const TopoDS_Shape aShape,
92 Standard_Integer& anIndex)
95 TopTools_IndexedMapOfShape aNewEdges;
96 //add edges without seams
97 for(TopExp_Explorer exp(aShape,TopAbs_EDGE); exp.More(); exp.Next()) {
98 TopoDS_Shape edge = exp.Current();
99 if(aNewEdges.Contains(edge))
101 //aNewEdges.Remove(edge);
102 TopoDS_Shape LastEdge = aNewEdges(aNewEdges.Extent());
103 aNewEdges.RemoveLast();
104 if (aNewEdges.FindIndex(edge) != 0)
105 aNewEdges.Substitute(aNewEdges.FindIndex(edge), LastEdge);
106 /////////////////////////
112 Standard_Boolean isDropped = Standard_False;
113 //merge edges and drop seams
115 for (i = 1; i <= edges.Length(); i++) {
116 TopoDS_Shape current = edges(i);
117 if(aNewEdges.Contains(current)) {
119 //aNewEdges.Remove(current);
120 TopoDS_Shape LastEdge = aNewEdges(aNewEdges.Extent());
121 aNewEdges.RemoveLast();
122 if (aNewEdges.FindIndex(current) != 0)
123 aNewEdges.Substitute(aNewEdges.FindIndex(current), LastEdge);
124 /////////////////////////
129 isDropped = Standard_True;
135 //add edges to the sequemce
136 for (i = 1; i <= aNewEdges.Extent(); i++)
137 edges.Append(aNewEdges(i));
142 //=======================================================================
143 //function : getCylinder
145 //=======================================================================
146 static Standard_Boolean getCylinder(Handle(Geom_Surface)& theInSurface,
147 gp_Cylinder& theOutCylinder)
149 Standard_Boolean isCylinder = Standard_False;
151 if (theInSurface->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
152 Handle(Geom_CylindricalSurface) aGC = Handle(Geom_CylindricalSurface)::DownCast(theInSurface);
154 theOutCylinder = aGC->Cylinder();
155 isCylinder = Standard_True;
157 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution))) {
158 Handle(Geom_SurfaceOfRevolution) aRS =
159 Handle(Geom_SurfaceOfRevolution)::DownCast(theInSurface);
160 Handle(Geom_Curve) aBasis = aRS->BasisCurve();
161 if (aBasis->IsKind(STANDARD_TYPE(Geom_Line))) {
162 Handle(Geom_Line) aBasisLine = Handle(Geom_Line)::DownCast(aBasis);
163 gp_Dir aDir = aRS->Direction();
164 gp_Dir aBasisDir = aBasisLine->Position().Direction();
165 if (aBasisDir.IsParallel(aDir, Precision::Angular())) {
166 // basis line is parallel to the revolution axis: it is a cylinder
167 gp_Pnt aLoc = aRS->Location();
168 Standard_Real aR = aBasisLine->Lin().Distance(aLoc);
169 gp_Ax3 aCylAx (aLoc, aDir);
171 theOutCylinder = gp_Cylinder(aCylAx, aR);
172 isCylinder = Standard_True;
176 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion))) {
177 Handle(Geom_SurfaceOfLinearExtrusion) aLES =
178 Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(theInSurface);
179 Handle(Geom_Curve) aBasis = aLES->BasisCurve();
180 if (aBasis->IsKind(STANDARD_TYPE(Geom_Circle))) {
181 Handle(Geom_Circle) aBasisCircle = Handle(Geom_Circle)::DownCast(aBasis);
182 gp_Dir aDir = aLES->Direction();
183 gp_Dir aBasisDir = aBasisCircle->Position().Direction();
184 if (aBasisDir.IsParallel(aDir, Precision::Angular())) {
185 // basis circle is normal to the extrusion axis: it is a cylinder
186 gp_Pnt aLoc = aBasisCircle->Location();
187 Standard_Real aR = aBasisCircle->Radius();
188 gp_Ax3 aCylAx (aLoc, aDir);
190 theOutCylinder = gp_Cylinder(aCylAx, aR);
191 isCylinder = Standard_True;
201 //=======================================================================
202 //function : ClearRts
204 //=======================================================================
205 static Handle(Geom_Surface) ClearRts(const Handle(Geom_Surface)& aSurface)
207 if(aSurface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
208 Handle(Geom_RectangularTrimmedSurface) rts =
209 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
210 return rts->BasisSurface();
215 //=======================================================================
216 //function : IsSameDomain
218 //=======================================================================
219 static Standard_Boolean IsSameDomain(const TopoDS_Face& aFace,
220 const TopoDS_Face& aCheckedFace)
222 //checking the same handles
223 TopLoc_Location L1, L2;
224 Handle(Geom_Surface) S1, S2;
226 S1 = BRep_Tool::Surface(aFace,L1);
227 S2 = BRep_Tool::Surface(aCheckedFace,L2);
229 if (S1 == S2 && L1 == L2)
230 return Standard_True;
232 // planar and cylindrical cases (IMP 20052)
233 Standard_Real aPrec = Precision::Confusion();
235 S1 = BRep_Tool::Surface(aFace);
236 S2 = BRep_Tool::Surface(aCheckedFace);
241 //Handle(Geom_OffsetSurface) aGOFS1, aGOFS2;
242 //aGOFS1 = Handle(Geom_OffsetSurface)::DownCast(S1);
243 //aGOFS2 = Handle(Geom_OffsetSurface)::DownCast(S2);
244 //if (!aGOFS1.IsNull()) S1 = aGOFS1->BasisSurface();
245 //if (!aGOFS2.IsNull()) S2 = aGOFS2->BasisSurface();
247 // case of two elementary surfaces: use OCCT tool
248 // elementary surfaces: ConicalSurface, CylindricalSurface,
249 // Plane, SphericalSurface and ToroidalSurface
250 if (S1->IsKind(STANDARD_TYPE(Geom_ElementarySurface)) &&
251 S2->IsKind(STANDARD_TYPE(Geom_ElementarySurface)))
253 Handle(GeomAdaptor_HSurface) aGA1 = new GeomAdaptor_HSurface(S1);
254 Handle(GeomAdaptor_HSurface) aGA2 = new GeomAdaptor_HSurface(S2);
256 Handle(BRepTopAdaptor_TopolTool) aTT1 = new BRepTopAdaptor_TopolTool();
257 Handle(BRepTopAdaptor_TopolTool) aTT2 = new BRepTopAdaptor_TopolTool();
260 IntPatch_ImpImpIntersection anIIInt (aGA1, aTT1, aGA2, aTT2, aPrec, aPrec);
261 if (!anIIInt.IsDone() || anIIInt.IsEmpty())
262 return Standard_False;
264 return anIIInt.TangentFaces();
266 catch (Standard_Failure) {
267 return Standard_False;
271 // case of two planar surfaces:
272 // all kinds of surfaces checked, including b-spline and bezier
273 GeomLib_IsPlanarSurface aPlanarityChecker1 (S1, aPrec);
274 if (aPlanarityChecker1.IsPlanar()) {
275 GeomLib_IsPlanarSurface aPlanarityChecker2 (S2, aPrec);
276 if (aPlanarityChecker2.IsPlanar()) {
277 gp_Pln aPln1 = aPlanarityChecker1.Plan();
278 gp_Pln aPln2 = aPlanarityChecker2.Plan();
280 if (aPln1.Position().Direction().IsParallel(aPln2.Position().Direction(),Precision::Angular()) &&
281 aPln1.Distance(aPln2) < aPrec) {
282 return Standard_True;
287 // case of two cylindrical surfaces, at least one of which is a swept surface
288 // swept surfaces: SurfaceOfLinearExtrusion, SurfaceOfRevolution
289 if ((S1->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
290 S1->IsKind(STANDARD_TYPE(Geom_SweptSurface))) &&
291 (S2->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
292 S2->IsKind(STANDARD_TYPE(Geom_SweptSurface))))
294 gp_Cylinder aCyl1, aCyl2;
295 if (getCylinder(S1, aCyl1) && getCylinder(S2, aCyl2)) {
296 if (fabs(aCyl1.Radius() - aCyl2.Radius()) < aPrec) {
297 gp_Dir aDir1 = aCyl1.Position().Direction();
298 gp_Dir aDir2 = aCyl2.Position().Direction();
299 if (aDir1.IsParallel(aDir2, Precision::Angular())) {
300 gp_Pnt aLoc1 = aCyl1.Location();
301 gp_Pnt aLoc2 = aCyl2.Location();
302 gp_Vec aVec12 (aLoc1, aLoc2);
303 if (aVec12.SquareMagnitude() < aPrec*aPrec ||
304 aVec12.IsParallel(aDir1, Precision::Angular())) {
305 return Standard_True;
312 return Standard_False;
315 //=======================================================================
316 //function : MovePCurves
318 //=======================================================================
319 static void MovePCurves(TopoDS_Face& aTarget,
320 const TopoDS_Face& aSource)
323 for(TopExp_Explorer wexp(aSource,TopAbs_WIRE);wexp.More();wexp.Next()) {
324 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(TopoDS::Wire(wexp.Current()),
325 aTarget, Precision::Confusion());
327 Standard_Boolean isReoredFailed = sfw->StatusReorder ( ShapeExtend_FAIL );
328 sfw->FixEdgeCurves();
333 sfw->FixDegenerated();
335 // remove degenerated edges from not degenerated points
336 ShapeAnalysis_Edge sae;
337 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
338 for(Standard_Integer i = 1; i<=sewd->NbEdges();i++) {
339 TopoDS_Edge E = sewd->Edge(i);
340 if(BRep_Tool::Degenerated(E)&&!sae.HasPCurve(E,aTarget)) {
346 TopoDS_Wire ResWire = sfw->Wire();
347 B.Add(aTarget,ResWire);
351 //=======================================================================
352 //function : GlueEdgesWithPCurves
353 //purpose : Glues the pcurves of the sequence of edges
354 // and glues their 3d curves
355 //=======================================================================
356 static TopoDS_Edge GlueEdgesWithPCurves(const TopTools_SequenceOfShape& aChain,
357 const TopoDS_Vertex& FirstVertex,
358 const TopoDS_Vertex& LastVertex)
360 Standard_Integer i, j;
362 TopoDS_Edge FirstEdge = TopoDS::Edge(aChain(1));
363 //TColGeom2d_SequenceOfCurve PCurveSeq;
364 TColGeom_SequenceOfSurface SurfSeq;
365 //TopTools_SequenceOfShape LocSeq;
367 BRep_ListIteratorOfListOfCurveRepresentation itr( (Handle(BRep_TEdge)::DownCast(FirstEdge.TShape()))->Curves() );
368 for (; itr.More(); itr.Next())
370 Handle(BRep_CurveRepresentation) CurveRep = itr.Value();
371 if (CurveRep->IsCurveOnSurface())
373 //PCurveSeq.Append(CurveRep->PCurve());
374 SurfSeq.Append(CurveRep->Surface());
376 TopoDS_Shape aLocShape;
377 aLocShape.Location(CurveRep->Location());
378 LocSeq.Append(aLocShape);
383 Standard_Real fpar, lpar;
384 BRep_Tool::Range(FirstEdge, fpar, lpar);
385 TopoDS_Edge PrevEdge = FirstEdge;
387 Standard_Real MaxTol = 0.;
392 Standard_Integer nb_curve = aChain.Length(); //number of curves
393 TColGeom_Array1OfBSplineCurve tab_c3d(0,nb_curve-1); //array of the curves
394 TColStd_Array1OfReal tabtolvertex(0,nb_curve-1); //(0,nb_curve-2); //array of the tolerances
396 TopoDS_Vertex PrevVertex = FirstVertex;
397 for (i = 1; i <= nb_curve; i++)
399 TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
400 TopoDS_Vertex VF, VL;
401 TopExp::Vertices(anEdge, VF, VL);
402 Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
404 Standard_Real Tol1 = BRep_Tool::Tolerance(VF);
405 Standard_Real Tol2 = BRep_Tool::Tolerance(VL);
413 TopExp::CommonVertex(PrevEdge, anEdge, CV);
414 Standard_Real Tol = BRep_Tool::Tolerance(CV);
415 tabtolvertex(i-2) = Tol;
418 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, fpar, lpar);
419 Handle(Geom_TrimmedCurve) aTrCurve = new Geom_TrimmedCurve(aCurve, fpar, lpar);
420 tab_c3d(i-1) = GeomConvert::CurveToBSplineCurve(aTrCurve);
421 GeomConvert::C0BSplineToC1BSplineCurve(tab_c3d(i-1), Precision::Confusion());
423 tab_c3d(i-1)->Reverse();
424 PrevVertex = (ToReverse)? VF : VL;
427 Handle(TColGeom_HArray1OfBSplineCurve) concatcurve; //array of the concatenated curves
428 Handle(TColStd_HArray1OfInteger) ArrayOfIndices; //array of the remining Vertex
429 GeomConvert::ConcatC1(tab_c3d,
434 Precision::Confusion()); //C1 concatenation
436 if (concatcurve->Length() > 1)
438 GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
440 for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
441 Concat.Add( concatcurve->Value(i), MaxTol, Standard_True );
443 concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
445 Handle(Geom_BSplineCurve) ResCurve = concatcurve->Value(concatcurve->Lower());
447 TColGeom2d_SequenceOfBoundedCurve ResPCurves;
448 TopLoc_Location aLoc;
449 for (j = 1; j <= SurfSeq.Length(); j++)
451 TColGeom2d_Array1OfBSplineCurve tab_c2d(0,nb_curve-1); //array of the pcurves
453 PrevVertex = FirstVertex;
454 PrevEdge = FirstEdge;
455 //TopLoc_Location theLoc = LocSeq(j).Location();
456 for (i = 1; i <= nb_curve; i++)
458 TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
459 TopoDS_Vertex VF, VL;
460 TopExp::Vertices(anEdge, VF, VL);
461 Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
464 Handle(Geom2d_Curve) aPCurve =
465 BRep_Tool::CurveOnSurface(anEdge, SurfSeq(j), anEdge.Location()*theLoc, fpar, lpar);
467 Handle(Geom2d_Curve) aPCurve =
468 BRep_Tool::CurveOnSurface(anEdge, SurfSeq(j), aLoc, fpar, lpar);
469 Handle(Geom2d_TrimmedCurve) aTrPCurve = new Geom2d_TrimmedCurve(aPCurve, fpar, lpar);
470 tab_c2d(i-1) = Geom2dConvert::CurveToBSplineCurve(aTrPCurve);
471 Geom2dConvert::C0BSplineToC1BSplineCurve(tab_c2d(i-1), Precision::Confusion());
473 tab_c2d(i-1)->Reverse();
474 PrevVertex = (ToReverse)? VF : VL;
477 Handle(TColGeom2d_HArray1OfBSplineCurve) concatc2d; //array of the concatenated curves
478 Handle(TColStd_HArray1OfInteger) ArrayOfInd2d; //array of the remining Vertex
479 Geom2dConvert::ConcatC1(tab_c2d,
484 Precision::Confusion()); //C1 concatenation
486 if (concatc2d->Length() > 1)
488 Geom2dConvert_CompCurveToBSplineCurve Concat2d(concatc2d->Value(concatc2d->Lower()));
490 for (i = concatc2d->Lower()+1; i <= concatc2d->Upper(); i++)
491 Concat2d.Add( concatc2d->Value(i), MaxTol, Standard_True );
493 concatc2d->SetValue(concatc2d->Lower(), Concat2d.BSplineCurve());
495 Handle(Geom2d_BSplineCurve) aResPCurve = concatc2d->Value(concatc2d->Lower());
496 ResPCurves.Append(aResPCurve);
499 ResEdge = BRepLib_MakeEdge(ResCurve,
500 FirstVertex, LastVertex,
501 ResCurve->FirstParameter(), ResCurve->LastParameter());
502 BB.SameRange(ResEdge, Standard_False);
503 BB.SameParameter(ResEdge, Standard_False);
504 for (j = 1; j <= ResPCurves.Length(); j++)
506 BB.UpdateEdge(ResEdge, ResPCurves(j), SurfSeq(j), aLoc, MaxTol);
507 BB.Range(ResEdge, SurfSeq(j), aLoc, ResPCurves(j)->FirstParameter(), ResPCurves(j)->LastParameter());
510 BRepLib::SameParameter(ResEdge, MaxTol, Standard_True);
515 //=======================================================================
516 //function : MergeEdges
518 //=======================================================================
519 static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
520 const TopoDS_Face& /*aFace*/,
521 const Standard_Real Tol,
522 const Standard_Boolean ConcatBSplines,
525 // make chain for union
527 ShapeAnalysis_Edge sae;
528 TopoDS_Edge FirstE = TopoDS::Edge(SeqEdges.Value(1));
529 TopoDS_Edge LastE = FirstE;
530 TopoDS_Vertex VF = sae.FirstVertex(FirstE);
531 TopoDS_Vertex VL = sae.LastVertex(LastE);
532 TopTools_SequenceOfShape aChain;
533 aChain.Append(FirstE);
534 TColStd_MapOfInteger IndUsedEdges;
537 for (j = 1; j <= SeqEdges.Length(); j++)
539 TopoDS_Edge anEdge = TopoDS::Edge(SeqEdges(j));
540 if (BRep_Tool::Degenerated(anEdge))
541 return Standard_False;
544 for(j=2; j<=SeqEdges.Length(); j++) {
545 for(Standard_Integer k=2; k<=SeqEdges.Length(); k++) {
546 if(IndUsedEdges.Contains(k)) continue;
547 TopoDS_Edge edge = TopoDS::Edge(SeqEdges.Value(k));
548 TopoDS_Vertex VF2 = sae.FirstVertex(edge);
549 TopoDS_Vertex VL2 = sae.LastVertex(edge);
550 if(sae.FirstVertex(edge).IsSame(VL)) {
553 VL = sae.LastVertex(LastE);
556 else if(sae.LastVertex(edge).IsSame(VF)) {
557 aChain.Prepend(edge);
559 VF = sae.FirstVertex(FirstE);
564 if(aChain.Length()<SeqEdges.Length()) {
566 cout<<"can not create correct chain..."<<endl;
568 return Standard_False;
570 // union edges in chain
571 // 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,fp1,lp1);
577 if(c3d1.IsNull()) break;
578 while(c3d1->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
579 Handle(Geom_TrimmedCurve) tc =
580 Handle(Geom_TrimmedCurve)::DownCast(c3d1);
581 c3d1 = tc->BasisCurve();
583 TopoDS_Edge edge2 = TopoDS::Edge(aChain.Value(j+1));
584 Handle(Geom_Curve) c3d2 = BRep_Tool::Curve(edge2,fp2,lp2);
585 if(c3d2.IsNull()) break;
586 while(c3d2->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
587 Handle(Geom_TrimmedCurve) tc =
588 Handle(Geom_TrimmedCurve)::DownCast(c3d2);
589 c3d2 = tc->BasisCurve();
591 if( c3d1->IsKind(STANDARD_TYPE(Geom_Line)) && c3d2->IsKind(STANDARD_TYPE(Geom_Line)) ) {
593 Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(c3d1);
594 Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(c3d2);
595 gp_Dir Dir1 = L1->Position().Direction();
596 gp_Dir Dir2 = L2->Position().Direction();
597 //if(!Dir1.IsEqual(Dir2,Precision::Angular())) {
598 //if(!Dir1.IsParallel(Dir2,Precision::Angular())) {
599 if(!Dir1.IsParallel(Dir2,Tol)) {
600 return Standard_False;
602 // can union lines => create new edge
603 TopoDS_Vertex V1 = sae.FirstVertex(edge1);
604 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
605 TopoDS_Vertex V2 = sae.LastVertex(edge2);
606 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
608 Handle(Geom_Line) L = new Geom_Line(gp_Ax1(PV1,Vec));
609 Standard_Real dist = PV1.Distance(PV2);
610 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(L,0.0,dist);
612 B.MakeEdge (E,tc,Precision::Confusion());
613 B.Add (E,V1); B.Add (E,V2);
614 B.UpdateVertex(V1, 0., E, 0.);
615 B.UpdateVertex(V2, dist, E, 0.);
617 //sfe.FixAddPCurve(E,aFace,Standard_False);
618 //sfe.FixSameParameter(E);
620 aChain.SetValue(j,E);
623 if( c3d1->IsKind(STANDARD_TYPE(Geom_Circle)) && c3d2->IsKind(STANDARD_TYPE(Geom_Circle)) ) {
625 Handle(Geom_Circle) C1 = Handle(Geom_Circle)::DownCast(c3d1);
626 Handle(Geom_Circle) C2 = Handle(Geom_Circle)::DownCast(c3d2);
627 gp_Pnt P01 = C1->Location();
628 gp_Pnt P02 = C2->Location();
629 if (P01.Distance(P02) > Precision::Confusion())
630 return Standard_False;
631 // can union circles => create new edge
632 TopoDS_Vertex V1 = sae.FirstVertex(edge1);
633 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
634 TopoDS_Vertex V2 = sae.LastVertex(edge2);
635 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
636 TopoDS_Vertex VM = sae.LastVertex(edge1);
637 gp_Pnt PVM = BRep_Tool::Pnt(VM);
638 GC_MakeCircle MC (PV1,PVM,PV2);
640 if (!MC.IsDone() || MC.Value().IsNull()) {
641 // jfa for Mantis issue 0020228
642 if (PV1.Distance(PV2) > Precision::Confusion())
643 return Standard_False;
645 B.MakeEdge (E,C1,Precision::Confusion());
650 Handle(Geom_Circle) C = MC.Value();
651 gp_Pnt P0 = C->Location();
652 gp_Dir D1(gp_Vec(P0,PV1));
653 gp_Dir D2(gp_Vec(P0,PV2));
654 Standard_Real fpar = C->XAxis().Direction().Angle(D1);
655 if(fabs(fpar)>Precision::Confusion()) {
657 gp_Dir ND = C->XAxis().Direction().Crossed(D1);
658 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
662 Standard_Real lpar = C->XAxis().Direction().Angle(D2);
663 if(fabs(lpar)>Precision::Confusion()) {
665 gp_Dir ND = C->XAxis().Direction().Crossed(D2);
666 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
670 if (lpar < fpar) lpar += 2*M_PI;
671 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
672 B.MakeEdge (E,tc,Precision::Confusion());
675 B.UpdateVertex(V1, fpar, E, 0.);
676 B.UpdateVertex(V2, lpar, E, 0.);
679 aChain.SetValue(j,E);
683 if (j < aChain.Length()) {
685 cout<<"null curve3d in edge..."<<endl;
687 return Standard_False;
689 if (aChain.Length() > 1 && ConcatBSplines) {
690 // second step: union edges with various curves
691 // skl for bug 0020052 from Mantis: perform such unions
692 // only if curves are bspline or bezier
693 bool NeedUnion = true;
694 for(j=1; j<=aChain.Length(); j++) {
695 TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
697 Handle(Geom_Curve) c3d = BRep_Tool::Curve(edge,Loc,fp1,lp1);
698 if(c3d.IsNull()) continue;
699 while(c3d->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
700 Handle(Geom_TrimmedCurve) tc =
701 Handle(Geom_TrimmedCurve)::DownCast(c3d);
702 c3d = tc->BasisCurve();
704 if( ( c3d->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ||
705 c3d->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ) ) continue;
711 cout<<"can not make analitical union => make approximation"<<endl;
713 TopoDS_Edge E = GlueEdgesWithPCurves(aChain, VF, VL);
714 aChain.SetValue(1,E);
718 cout<<"can not make approximation for such types of curves"<<endl;
720 return Standard_False;
724 anEdge = TopoDS::Edge(aChain.Value(1));
725 return Standard_True;
728 //=======================================================================
729 //function : ShapeUpgrade_UnifySameDomain
730 //purpose : Constructor
731 //=======================================================================
733 ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain()
735 myUnifyEdges = Standard_True;
736 myUnifyFaces = Standard_True;
737 myConcatBSplines = Standard_False;
739 myContext = new ShapeBuild_ReShape;
742 //=======================================================================
743 //function : ShapeUpgrade_UnifySameDomain
744 //purpose : Constructor
745 //=======================================================================
747 ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain(const TopoDS_Shape& aShape,
748 const Standard_Boolean UnifyEdges,
749 const Standard_Boolean UnifyFaces,
750 const Standard_Boolean ConcatBSplines)
752 myInitShape = aShape;
754 myUnifyEdges = UnifyEdges;
755 myUnifyFaces = UnifyFaces;
756 myConcatBSplines = ConcatBSplines;
758 myContext = new ShapeBuild_ReShape;
761 //=======================================================================
762 //function : Initialize
764 //=======================================================================
766 void ShapeUpgrade_UnifySameDomain::Initialize(const TopoDS_Shape& aShape,
767 const Standard_Boolean UnifyEdges,
768 const Standard_Boolean UnifyFaces,
769 const Standard_Boolean ConcatBSplines)
771 myInitShape = aShape;
773 myUnifyEdges = UnifyEdges;
774 myUnifyFaces = UnifyFaces;
775 myConcatBSplines = ConcatBSplines;
778 //myOldNewMap.Clear();
779 //myGenerated.Clear();
782 //=======================================================================
783 //function : UnifyFaces
785 //=======================================================================
787 void ShapeUpgrade_UnifySameDomain::UnifyFaces()
789 //Handle(ShapeBuild_ReShape) myContext = new ShapeBuild_ReShape;
790 TopoDS_Shape aResShape = myContext->Apply(myShape);
792 // processing each shell
793 TopExp_Explorer exps;
794 for (exps.Init(myShape, TopAbs_SHELL); exps.More(); exps.Next()) {
795 TopoDS_Shell aShell = TopoDS::Shell(exps.Current());
797 // creating map of edge faces
798 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
799 TopExp::MapShapesAndAncestors(aShell, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
801 // map of processed shapes
802 TopTools_MapOfShape aProcessed;
804 //Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
806 Standard_Integer NbModif = 0;
807 Standard_Boolean hasFailed = Standard_False;
808 Standard_Real tol = Precision::Confusion();
811 Standard_Integer nbf = 0;
813 TopTools_MapOfShape mapF;
814 for (exp.Init(aShell, TopAbs_FACE); exp.More(); exp.Next()) {
815 if (mapF.Add(exp.Current()))
819 // processing each face
821 for (exp.Init(aShell, TopAbs_FACE); exp.More(); exp.Next()) {
822 TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
824 if (aProcessed.Contains(aFace))
827 Standard_Integer dummy;
828 TopTools_SequenceOfShape edges;
829 AddOrdinaryEdges(edges,aFace,dummy);
831 TopTools_SequenceOfShape faces;
834 //surface and location to construct result
835 TopLoc_Location aBaseLocation;
836 Handle(Geom_Surface) aBaseSurface = BRep_Tool::Surface(aFace,aBaseLocation);
837 aBaseSurface = ClearRts(aBaseSurface);
839 // find adjacent faces to union
841 for (i = 1; i <= edges.Length(); i++) {
842 TopoDS_Edge edge = TopoDS::Edge(edges(i));
843 if (BRep_Tool::Degenerated(edge))
846 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
847 TopTools_ListIteratorOfListOfShape anIter(aList);
848 for (; anIter.More(); anIter.Next()) {
849 TopoDS_Face anCheckedFace = TopoDS::Face(anIter.Value().Oriented(TopAbs_FORWARD));
850 if (anCheckedFace.IsSame(aFace))
853 if (aProcessed.Contains(anCheckedFace))
856 if (IsSameDomain(aFace,anCheckedFace)) {
858 if (aList.Extent() != 2) {
859 // non mainfold case is not processed
864 TopoDS_Face aMockUpFace;
866 B.MakeFace(aMockUpFace,aBaseSurface,aBaseLocation,0.);
867 MovePCurves(aMockUpFace,anCheckedFace);
869 if (AddOrdinaryEdges(edges,aMockUpFace,dummy)) {
870 // sequence edges is modified
874 faces.Append(anCheckedFace);
875 aProcessed.Add(anCheckedFace);
881 // all faces collected in the sequence. Perform union of faces
882 if (faces.Length() > 1) {
886 B.MakeFace(aResult,aBaseSurface,aBaseLocation,0);
887 Standard_Integer nbWires = 0;
890 while (edges.Length()>0) {
892 Standard_Boolean isEdge3d = Standard_False;
894 TopTools_MapOfShape aVertices;
898 TopoDS_Edge anEdge = TopoDS::Edge(edges(1));
901 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
904 TopExp::Vertices(anEdge,V1,V2);
908 Standard_Boolean isNewFound = Standard_False;
910 isNewFound = Standard_False;
911 for(Standard_Integer j = 1; j <= edges.Length(); j++) {
912 anEdge = TopoDS::Edge(edges(j));
913 TopExp::Vertices(anEdge,V1,V2);
914 if(aVertices.Contains(V1) || aVertices.Contains(V2)) {
915 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
921 isNewFound = Standard_True;
924 } while (isNewFound);
926 // sorting any type of edges
927 aWire.Closed (BRep_Tool::IsClosed (aWire));
928 aWire = TopoDS::Wire(myContext->Apply(aWire));
930 TopoDS_Face tmpF = TopoDS::Face(myContext->Apply(faces(1).Oriented(TopAbs_FORWARD)));
931 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(aWire,tmpF,Precision::Confusion());
933 Standard_Boolean isDegRemoved = Standard_False;
934 if(!sfw->StatusReorder ( ShapeExtend_FAIL )) {
935 // clear degenerated edges if at least one with 3d curve exist
937 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
938 for(Standard_Integer j = 1; j<=sewd->NbEdges();j++) {
939 TopoDS_Edge E = sewd->Edge(j);
940 if(BRep_Tool::Degenerated(E)) {
942 isDegRemoved = Standard_True;
949 sfw->FixDegenerated();
951 TopoDS_Wire aWireFixed = sfw->Wire();
952 //aContext->Replace(aWire,aWireFixed);
953 myContext->Replace(aWire,aWireFixed);
956 if (!myOldNewMap.IsBound(aWire))
958 TopTools_ListOfShape EmptyList;
959 myOldNewMap.Bind(aWire, EmptyList);
961 myOldNewMap(aWire).Clear();
962 myOldNewMap(aWire).Append(aWireFixed);
966 // add resulting wire
968 B.Add(aResult,aWireFixed);
972 Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
973 Standard_Integer nbEdges = sbwd->NbEdges();
974 // sort degenerated edges and create one edge instead of several ones
975 ShapeAnalysis_WireOrder sawo(Standard_False, 0);
976 ShapeAnalysis_Edge sae;
977 Standard_Integer aLastEdge = nbEdges;
978 for(Standard_Integer j = 1; j <= nbEdges; j++) {
980 //smh protection on NULL pcurve
981 Handle(Geom2d_Curve) c2d;
982 if(!sae.PCurve(sbwd->Edge(j),tmpF,c2d,f,l)) {
986 sawo.Add(c2d->Value(f).XY(),c2d->Value(l).XY());
990 // constructind one degenerative edge
991 gp_XY aStart, anEnd, tmp;
992 Standard_Integer nbFirst = sawo.Ordered(1);
993 TopoDS_Edge anOrigE = TopoDS::Edge(sbwd->Edge(nbFirst).Oriented(TopAbs_FORWARD));
995 TopoDS_Vertex aDummyV;
996 TopoDS_Edge E = sbe.CopyReplaceVertices(anOrigE,aDummyV,aDummyV);
997 sawo.XY(nbFirst,aStart,tmp);
998 sawo.XY(sawo.Ordered(aLastEdge),tmp,anEnd);
1000 gp_XY aVec = anEnd-aStart;
1001 Handle(Geom2d_Line) aLine = new Geom2d_Line(aStart,gp_Dir2d(anEnd-aStart));
1003 B.UpdateEdge(E,aLine,tmpF,0.);
1004 B.Range(E,tmpF,0.,aVec.Modulus());
1005 Handle(Geom_Curve) C3d;
1006 B.UpdateEdge(E,C3d,0.);
1007 B.Degenerated(E,Standard_True);
1011 aW.Closed (Standard_True);
1016 // perform substitution of face
1017 //aContext->Replace(aContext->Apply(aFace),aResult);
1018 myContext->Replace(myContext->Apply(aFace),aResult);
1021 if (!myOldNewMap.IsBound(aFace))
1023 TopTools_ListOfShape EmptyList;
1024 myOldNewMap.Bind(aFace, EmptyList);
1026 myOldNewMap(aFace).Clear();
1027 myOldNewMap(aFace).Append(aResult);
1031 ShapeFix_Face sff (aResult);
1032 //Intializing by tolerances
1033 sff.SetPrecision(Precision::Confusion());
1034 sff.SetMinTolerance(tol);
1035 sff.SetMaxTolerance(1.);
1037 sff.FixOrientationMode() = 0;
1038 //sff.FixWireMode() = 0;
1039 //sff.SetContext(aContext);
1040 sff.SetContext(myContext);
1041 // Applying the fixes
1043 if(sff.Status(ShapeExtend_FAIL))
1044 hasFailed = Standard_True;
1046 // breaking down to several faces
1047 //TopoDS_Shape theResult = aContext->Apply(aResult);
1048 TopoDS_Shape theResult = myContext->Apply(aResult);
1049 for (TopExp_Explorer aFaceExp (theResult,TopAbs_FACE); aFaceExp.More(); aFaceExp.Next()) {
1050 TopoDS_Face aCurrent = TopoDS::Face(aFaceExp.Current().Oriented(TopAbs_FORWARD));
1051 Handle(TColGeom_HArray2OfSurface) grid = new TColGeom_HArray2OfSurface ( 1, 1, 1, 1 );
1052 grid->SetValue ( 1, 1, aBaseSurface );
1053 Handle(ShapeExtend_CompositeSurface) G = new ShapeExtend_CompositeSurface ( grid );
1054 ShapeFix_ComposeShell CompShell;
1055 CompShell.Init ( G, aBaseLocation, aCurrent, ::Precision::Confusion() );//myPrecision
1056 //CompShell.SetContext( aContext );
1057 CompShell.SetContext( myContext );
1059 TopTools_SequenceOfShape parts;
1060 ShapeFix_SequenceOfWireSegment wires;
1061 for(TopExp_Explorer W_Exp(aCurrent,TopAbs_WIRE);W_Exp.More();W_Exp.Next()) {
1062 Handle(ShapeExtend_WireData) sbwd =
1063 new ShapeExtend_WireData ( TopoDS::Wire(W_Exp.Current() ));
1064 ShapeFix_WireSegment seg ( sbwd, TopAbs_REVERSED );
1068 CompShell.DispatchWires ( parts,wires );
1069 for (Standard_Integer j=1; j <= parts.Length(); j++ ) {
1070 ShapeFix_Face aFixOrient(TopoDS::Face(parts(j)));
1071 //aFixOrient.SetContext(aContext);
1072 aFixOrient.SetContext(myContext);
1073 aFixOrient.FixOrientation();
1076 TopoDS_Shape CompRes;
1077 if ( faces.Length() !=1 ) {
1080 for ( i=1; i <= parts.Length(); i++ )
1081 B.Add ( S, parts(i) );
1082 S.Closed (BRep_Tool::IsClosed (S));
1085 else CompRes = parts(1);
1087 //aContext->Replace(aCurrent,CompRes);
1088 myContext->Replace(aCurrent,CompRes);
1091 if (!myOldNewMap.IsBound(aCurrent))
1093 TopTools_ListOfShape EmptyList;
1094 myOldNewMap.Bind(aCurrent, EmptyList);
1096 myOldNewMap(aCurrent).Clear();
1097 myOldNewMap(aCurrent).Append(CompRes);
1102 // remove the remaining faces
1103 for(i = 2; i <= faces.Length(); i++)
1104 //aContext->Remove(faces(i));
1105 myContext->Remove(faces(i));
1107 } // end processing each face
1109 //TopoDS_Shape aResult = Shape;
1110 if (NbModif > 0 && !hasFailed) {
1111 //TopoDS_Shape aResult = aContext->Apply(aShell);
1112 TopoDS_Shape aResult = myContext->Apply(aShell);
1115 for (exp.Init(aResult,TopAbs_EDGE); exp.More(); exp.Next()) {
1116 TopoDS_Edge E = TopoDS::Edge(exp.Current());
1117 sfe.FixVertexTolerance (E);
1118 // ptv add fix same parameter
1119 sfe.FixSameParameter(E, Precision::Confusion());
1122 myContext->Replace(aShell, aResult);
1125 if (!myOldNewMap.IsBound(aShell))
1127 TopTools_ListOfShape EmptyList;
1128 myOldNewMap.Bind(aShell, EmptyList);
1130 myOldNewMap(aShell).Clear();
1131 myOldNewMap(aShell).Append(aResult);
1137 for (exp.Init(aShell, TopAbs_FACE); exp.More(); exp.Next()) {
1138 TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
1139 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
1140 sfw->SetContext(myContext);
1141 sfw->SetPrecision(Precision::Confusion());
1142 sfw->SetMinTolerance(Precision::Confusion());
1143 sfw->SetMaxTolerance(1.);
1144 sfw->SetFace(aFace);
1145 for (TopoDS_Iterator iter (aFace,Standard_False); iter.More(); iter.Next()) {
1146 TopoDS_Wire wire = TopoDS::Wire(iter.Value());
1153 } // end processing each shell
1155 myShape = myContext->Apply(myShape);
1158 //=======================================================================
1159 //function : UnifyEdges
1161 //=======================================================================
1163 void ShapeUpgrade_UnifySameDomain::UnifyEdges()
1165 Standard_Real Tol = Precision::Confusion();
1167 //Handle(ShapeBuild_ReShape) myContext = new ShapeBuild_ReShape;
1168 Standard_Real myTolerance = Precision::Confusion();
1169 TopoDS_Shape aResult = myContext->Apply(myShape);
1171 // processing each solid
1172 TopAbs_ShapeEnum aType = TopAbs_SOLID;
1173 TopExp_Explorer exps (myShape, aType);
1175 aType = TopAbs_SHELL;
1176 exps.Init(myShape, aType);
1178 for (; exps.More(); exps.Next()) {
1179 //TopoDS_Solid aSolid = TopoDS::Solid(exps.Current());
1180 TopoDS_Shape aSolid = exps.Current();
1182 TopTools_IndexedMapOfShape ChangedFaces;
1184 // creating map of edge faces
1185 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
1186 TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
1188 // creating map of vertex edges
1189 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgesVertex;
1190 TopExp::MapShapesAndAncestors(aSolid, TopAbs_VERTEX, TopAbs_EDGE, aMapEdgesVertex);
1192 //Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
1193 TopoDS_Shape aRes = aSolid;
1194 //aRes = aContext->Apply(aSolid);
1195 aRes = myContext->Apply(aSolid);
1197 // processing each face
1198 TopExp_Explorer exp;
1199 for (exp.Init(aRes, TopAbs_FACE); exp.More(); exp.Next()) {
1200 //TopoDS_Face aFace = TopoDS::Face(aContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
1201 TopoDS_Face aFace = TopoDS::Face(myContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
1202 TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
1204 for (TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
1205 TopoDS_Edge edge = TopoDS::Edge(expe.Current());
1206 if (!aMapEdgeFaces.Contains(edge)) continue;
1207 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
1208 TopTools_ListIteratorOfListOfShape anIter(aList);
1209 for ( ; anIter.More(); anIter.Next()) {
1210 TopoDS_Face face = TopoDS::Face(anIter.Value());
1211 //TopoDS_Face face1 = TopoDS::Face(aContext->Apply(anIter.Value()));
1212 TopoDS_Face face1 = TopoDS::Face(myContext->Apply(anIter.Value()));
1213 if (face1.IsSame(aFace)) continue;
1214 if (aMapFacesEdges.Contains(face)) {
1215 aMapFacesEdges.ChangeFromKey(face).Append(edge);
1218 TopTools_ListOfShape ListEdges;
1219 ListEdges.Append(edge);
1220 aMapFacesEdges.Add(face,ListEdges);
1225 for (Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++) {
1226 const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
1227 TopTools_SequenceOfShape SeqEdges;
1228 ShapeAnalysis_Edge sae;
1229 TopTools_ListIteratorOfListOfShape anIter(ListEdges);
1230 Standard_Boolean IsSharedVertexPresent = Standard_False;
1231 for ( ; anIter.More(); anIter.Next())
1232 SeqEdges.Append(anIter.Value());
1233 if (SeqEdges.Length()==1)
1235 TopTools_SequenceOfShape SeqVertexes;
1236 TopTools_MapOfShape MapVertexes;
1237 for (int k = 1; k <= SeqEdges.Length(); k++ )
1239 TopoDS_Vertex aV1 = sae.FirstVertex(TopoDS::Edge(SeqEdges(k)));
1240 TopoDS_Vertex aV2 = sae.LastVertex(TopoDS::Edge(SeqEdges(k)));
1241 if (!MapVertexes.Add(aV1))
1242 SeqVertexes.Append(aV1);
1243 if (!MapVertexes.Add(aV2))
1244 SeqVertexes.Append(aV2);
1247 for (Standard_Integer k = 1; k <= SeqVertexes.Length() && !IsSharedVertexPresent; k++ )
1249 const TopTools_ListOfShape& ListEdgesV1 = aMapEdgesVertex.FindFromKey(SeqVertexes(k));
1250 TopTools_MapOfShape aMapOfEdges;
1251 TopTools_ListIteratorOfListOfShape iter(ListEdgesV1);
1252 for (; iter.More(); iter.Next())
1253 aMapOfEdges.Add(iter.Value());
1254 if (aMapOfEdges.Extent() > 2)
1255 IsSharedVertexPresent = Standard_True;
1258 if (IsSharedVertexPresent)
1261 if ( MergeEdges(SeqEdges,aFace,Tol,myConcatBSplines,E) ) {
1262 // now we have only one edge - aChain.Value(1)
1263 // we have to replace old ListEdges with this new edge
1264 //aContext->Replace(SeqEdges(1),E);
1265 myContext->Replace(SeqEdges(1),E);
1267 for (j = 2; j <= SeqEdges.Length(); j++) {
1268 //aContext->Remove(SeqEdges(j));
1269 myContext->Remove(SeqEdges(j));
1270 //myOldNewMap.Bind(SeqEdges(j), E);
1274 for (j = 1; j <= SeqEdges.Length(); j++)
1276 if (!myOldNewMap.IsBound(SeqEdges(j)))
1278 TopTools_ListOfShape EmptyList;
1279 myOldNewMap.Bind(SeqEdges(j), EmptyList);
1281 myOldNewMap(SeqEdges(j)).Clear();
1282 myOldNewMap(SeqEdges(j)).Append(E);
1287 TopoDS_Face tmpF = TopoDS::Face(exp.Current());
1288 if ( !ChangedFaces.Contains(tmpF) )
1289 ChangedFaces.Add(tmpF);
1290 tmpF = TopoDS::Face(aMapFacesEdges.FindKey(i));
1291 if ( !ChangedFaces.Contains(tmpF) )
1292 ChangedFaces.Add(tmpF);
1296 } // end processing each face
1298 // fix changed faces and replace them in the local context
1299 for (Standard_Integer i=1; i<=ChangedFaces.Extent(); i++) {
1300 //TopoDS_Face aFace = TopoDS::Face(aContext->Apply(ChangedFaces.FindKey(i)));
1301 TopoDS_Face aFace = TopoDS::Face(myContext->Apply(ChangedFaces.FindKey(i)));
1302 Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFace);
1303 sff->SetContext(myContext);
1304 sff->SetPrecision(myTolerance);
1305 sff->SetMinTolerance(myTolerance);
1306 sff->SetMaxTolerance(Max(1.,myTolerance*1000.));
1308 TopoDS_Shape aNewFace = sff->Face();
1309 //aContext->Replace(aFace,aNewFace);
1310 myContext->Replace(aFace,aNewFace);
1313 if (!myOldNewMap.IsBound(aFace))
1315 TopTools_ListOfShape EmptyList;
1316 myOldNewMap.Bind(aFace, EmptyList);
1318 myOldNewMap(aFace).Clear();
1319 myOldNewMap(aFace).Append(aNewFace);
1324 if (ChangedFaces.Extent() > 0) {
1325 // fix changed shell and replace it in the local context
1326 //TopoDS_Shape aRes1 = aContext->Apply(aRes);
1327 TopoDS_Shape aRes1 = myContext->Apply(aRes);
1328 TopExp_Explorer expsh;
1329 for (expsh.Init(aRes1, TopAbs_SHELL); expsh.More(); expsh.Next()) {
1330 TopoDS_Shell aShell = TopoDS::Shell(expsh.Current());
1331 Handle(ShapeFix_Shell) sfsh = new ShapeFix_Shell;
1332 sfsh->FixFaceOrientation(aShell);
1333 TopoDS_Shape aNewShell = sfsh->Shell();
1334 //aContext->Replace(aShell,aNewShell);
1335 myContext->Replace(aShell,aNewShell);
1338 if (!myOldNewMap.IsBound(aShell))
1340 TopTools_ListOfShape EmptyList;
1341 myOldNewMap.Bind(aShell, EmptyList);
1343 myOldNewMap(aShell).Clear();
1344 myOldNewMap(aShell).Append(aNewShell);
1348 //TopoDS_Shape aRes2 = aContext->Apply(aRes1);
1349 TopoDS_Shape aRes2 = myContext->Apply(aRes1);
1350 // put new solid into global context
1351 myContext->Replace(aSolid,aRes2);
1354 if (!myOldNewMap.IsBound(aSolid))
1356 TopTools_ListOfShape EmptyList;
1357 myOldNewMap.Bind(aSolid, EmptyList);
1359 myOldNewMap(aSolid).Clear();
1360 myOldNewMap(aSolid).Append(aRes2);
1365 } // end processing each solid
1367 myShape = myContext->Apply(myShape);
1370 //=======================================================================
1371 //function : UnifyFacesAndEdges
1373 //=======================================================================
1375 void ShapeUpgrade_UnifySameDomain::UnifyFacesAndEdges()
1380 ShapeUpgrade_RemoveLocations RemLoc;
1381 RemLoc.Remove(myShape);
1382 myShape = RemLoc.GetResult();
1388 //=======================================================================
1390 //purpose : builds the resulting shape
1391 //======================================================================
1392 void ShapeUpgrade_UnifySameDomain::Build()
1394 if (myUnifyFaces && myUnifyEdges)
1395 UnifyFacesAndEdges();
1397 else if (myUnifyEdges)
1399 else if (myUnifyFaces)
1405 //=======================================================================
1407 //purpose : give the resulting shape
1408 //=======================================================================
1409 const TopoDS_Shape& ShapeUpgrade_UnifySameDomain::Shape() const
1414 //=======================================================================
1415 //function : Generated
1416 //purpose : returns the new shape from the old one
1417 //=======================================================================
1418 TopoDS_Shape ShapeUpgrade_UnifySameDomain::Generated(const TopoDS_Shape& aShape) const
1420 TopoDS_Shape aNewShape = myContext->Apply(aShape);
1422 if (aNewShape.IsNull())
1423 aNewShape = myOldNewMap(aShape);