0027894: Crash when calling ShapeUpgrade_UnifySameDomain
[occt.git] / src / ShapeUpgrade / ShapeUpgrade_UnifySameDomain.cxx
CommitLineData
973c2be1 1// Created on: 2012-06-09
2// Created by: jgv@ROLEX
3// Copyright (c) 2012-2014 OPEN CASCADE SAS
4//
5// This file is part of Open CASCADE Technology software library.
6//
d5f74e42 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
973c2be1 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.
12//
13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
2277323d 15
42cf5bc1 16
2277323d 17#include <BRep_Builder.hxx>
42cf5bc1 18#include <BRep_CurveRepresentation.hxx>
2277323d 19#include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
20#include <BRep_TEdge.hxx>
42cf5bc1 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>
2277323d 27#include <Geom2d_TrimmedCurve.hxx>
2277323d 28#include <Geom2dConvert.hxx>
2277323d 29#include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
2277323d 30#include <Geom_BezierCurve.hxx>
42cf5bc1 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>
48#include <gp_Dir.hxx>
49#include <gp_Lin.hxx>
50#include <IntPatch_ImpImpIntersection.hxx>
51#include <ShapeAnalysis_Edge.hxx>
2277323d 52#include <ShapeAnalysis_WireOrder.hxx>
53#include <ShapeBuild_Edge.hxx>
42cf5bc1 54#include <ShapeBuild_ReShape.hxx>
2277323d 55#include <ShapeExtend_CompositeSurface.hxx>
56#include <ShapeFix_ComposeShell.hxx>
2277323d 57#include <ShapeFix_Edge.hxx>
42cf5bc1 58#include <ShapeFix_Face.hxx>
59#include <ShapeFix_SequenceOfWireSegment.hxx>
2277323d 60#include <ShapeFix_Shell.hxx>
42cf5bc1 61#include <ShapeFix_Wire.hxx>
62#include <ShapeFix_WireSegment.hxx>
2277323d 63#include <ShapeUpgrade_RemoveLocations.hxx>
42cf5bc1 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>
75#include <TopExp.hxx>
76#include <TopExp_Explorer.hxx>
77#include <TopoDS.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>
b4d4dbe8 84#include <TopTools_MapOfShape.hxx>
42cf5bc1 85#include <TopTools_SequenceOfShape.hxx>
f7d70540 86#include <gp_Circ.hxx>
87#include <BRepAdaptor_Curve.hxx>
6a0c0b14 88#include <BRepClass_FaceClassifier.hxx>
f0144633 89#include <BRepAdaptor_Curve2d.hxx>
90#include <gp_Vec2d.hxx>
f7d70540 91
92efcf78 92IMPLEMENT_STANDARD_RTTIEXT(ShapeUpgrade_UnifySameDomain,MMgt_TShared)
93
f7d70540 94struct SubSequenceOfEdges
95{
96 TopTools_SequenceOfShape SeqsEdges;
97 TopoDS_Edge UnionEdges;
98};
99
f0144633 100static Standard_Boolean IsLikeSeam(const TopoDS_Edge& anEdge,
101 const TopoDS_Face& aFace,
102 const Handle(Geom_Surface)& aBaseSurface)
103{
104 if (!aBaseSurface->IsUPeriodic() && !aBaseSurface->IsVPeriodic())
105 return Standard_False;
106
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;
115 else
116 FirstDir /= Length;
117 Length = LastDir.Magnitude();
118 if (Length <= gp::Resolution())
119 return Standard_False;
120 else
121 LastDir /= Length;
122
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;
128
129 if (aBaseSurface->IsVPeriodic() &&
130 (Abs(FirstDir.Y()) < Tol) &&
131 (Abs(LastDir.Y()) < Tol))
132 return Standard_True;
133
134 return Standard_False;
135}
2277323d 136
56091b56 137static Standard_Boolean CheckSharedEdgeOri(const TopoDS_Face& theF1,
138 const TopoDS_Face& theF2,
139 const TopoDS_Edge& theE)
140{
141 TopAbs_Orientation anEOri = theE.Orientation();
142 if (anEOri == TopAbs_EXTERNAL || anEOri == TopAbs_INTERNAL)
143 return Standard_False;
144
145 TopExp_Explorer Exp(theF1, TopAbs_EDGE);
146 for (;Exp.More();Exp.Next())
147 {
148 const TopoDS_Shape& aCE = Exp.Current();
149 if (aCE.IsSame(theE))
150 {
151 anEOri = aCE.Orientation();
152 break;
153 }
154 }
155
156 for (Exp.Init(theF2, TopAbs_EDGE);Exp.More();Exp.Next())
157 {
158 const TopoDS_Shape& aCE = Exp.Current();
159 if (aCE.IsSame(theE))
160 {
161 if (aCE.Orientation() == TopAbs::Reverse(anEOri))
162 return Standard_True;
163 else
164 return Standard_False;
165 }
166 }
167
168 return Standard_False;
169
170}
171
2277323d 172//=======================================================================
173//function : AddOrdinaryEdges
174//purpose : auxilary
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
179static Standard_Boolean AddOrdinaryEdges(TopTools_SequenceOfShape& edges,
180 const TopoDS_Shape aShape,
181 Standard_Integer& anIndex)
182{
183 //map of edges
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))
189 {
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 /////////////////////////
196 }
197 else
198 aNewEdges.Add(edge);
199 }
200
201 Standard_Boolean isDropped = Standard_False;
202 //merge edges and drop seams
203 Standard_Integer i;
204 for (i = 1; i <= edges.Length(); i++) {
205 TopoDS_Shape current = edges(i);
206 if(aNewEdges.Contains(current)) {
207
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 /////////////////////////
214 edges.Remove(i);
215 i--;
216
217 if(!isDropped) {
218 isDropped = Standard_True;
219 anIndex = i;
220 }
221 }
222 }
223
224 //add edges to the sequemce
225 for (i = 1; i <= aNewEdges.Extent(); i++)
226 edges.Append(aNewEdges(i));
227
228 return isDropped;
229}
230
231//=======================================================================
232//function : getCylinder
233//purpose : auxilary
234//=======================================================================
235static Standard_Boolean getCylinder(Handle(Geom_Surface)& theInSurface,
236 gp_Cylinder& theOutCylinder)
237{
238 Standard_Boolean isCylinder = Standard_False;
239
240 if (theInSurface->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
241 Handle(Geom_CylindricalSurface) aGC = Handle(Geom_CylindricalSurface)::DownCast(theInSurface);
242
243 theOutCylinder = aGC->Cylinder();
244 isCylinder = Standard_True;
245 }
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);
259
260 theOutCylinder = gp_Cylinder(aCylAx, aR);
261 isCylinder = Standard_True;
262 }
263 }
264 }
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);
278
279 theOutCylinder = gp_Cylinder(aCylAx, aR);
280 isCylinder = Standard_True;
281 }
282 }
283 }
284 else {
285 }
286
287 return isCylinder;
288}
289
290//=======================================================================
291//function : ClearRts
292//purpose : auxilary
293//=======================================================================
294static Handle(Geom_Surface) ClearRts(const Handle(Geom_Surface)& aSurface)
295{
296 if(aSurface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
297 Handle(Geom_RectangularTrimmedSurface) rts =
298 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
299 return rts->BasisSurface();
300 }
301 return aSurface;
302}
303
10ce3246 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//=======================================================================
309static Standard_Boolean GetNormalToSurface(const TopoDS_Face& theFace,
310 const TopoDS_Edge& theEdge,
311 const Standard_Real theP,
312 gp_Dir& theNormal)
313{
314 Standard_Real f, l;
315 // get 2d curve to get point in 2d
316 const Handle(Geom2d_Curve)& aC2d = BRep_Tool::CurveOnSurface(theEdge, theFace, f, l);
317 if (aC2d.IsNull()) {
318 return Standard_False;
319 }
320 //
321 // 2d point
322 gp_Pnt2d aP2d;
323 aC2d->D0(theP, aP2d);
324 //
325 // get D1
326 gp_Vec aDU, aDV;
327 gp_Pnt aP3d;
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);
331 //
332 // compute normal
333 gp_Vec aVNormal = aDU.Crossed(aDV);
334 if (aVNormal.Magnitude() < Precision::Confusion()) {
335 return Standard_False;
336 }
337 //
338 if (theFace.Orientation() == TopAbs_REVERSED) {
339 aVNormal.Reverse();
340 }
341 //
342 aVNormal.Transform(aLoc.Transformation());
343 theNormal = gp_Dir(aVNormal);
344 return Standard_True;
345}
346
2277323d 347//=======================================================================
348//function : IsSameDomain
349//purpose :
350//=======================================================================
351static Standard_Boolean IsSameDomain(const TopoDS_Face& aFace,
2ba9eb30 352 const TopoDS_Face& aCheckedFace,
353 const Standard_Real theLinTol,
354 const Standard_Real theAngTol)
2277323d 355{
356 //checking the same handles
357 TopLoc_Location L1, L2;
358 Handle(Geom_Surface) S1, S2;
359
360 S1 = BRep_Tool::Surface(aFace,L1);
361 S2 = BRep_Tool::Surface(aCheckedFace,L2);
362
363 if (S1 == S2 && L1 == L2)
364 return Standard_True;
365
2277323d 366 S1 = BRep_Tool::Surface(aFace);
367 S2 = BRep_Tool::Surface(aCheckedFace);
368
369 S1 = ClearRts(S1);
370 S2 = ClearRts(S2);
371
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();
377
2ba9eb30 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();
386
387 if (aPln1.Position().Direction().IsParallel(aPln2.Position().Direction(), theAngTol) &&
388 aPln1.Distance(aPln2) < theLinTol) {
389 return Standard_True;
390 }
391 }
392 }
393
2277323d 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)))
399 {
400 Handle(GeomAdaptor_HSurface) aGA1 = new GeomAdaptor_HSurface(S1);
401 Handle(GeomAdaptor_HSurface) aGA2 = new GeomAdaptor_HSurface(S2);
402
403 Handle(BRepTopAdaptor_TopolTool) aTT1 = new BRepTopAdaptor_TopolTool();
404 Handle(BRepTopAdaptor_TopolTool) aTT2 = new BRepTopAdaptor_TopolTool();
405
406 try {
2ba9eb30 407 IntPatch_ImpImpIntersection anIIInt(aGA1, aTT1, aGA2, aTT2, theLinTol, theLinTol);
2277323d 408 if (!anIIInt.IsDone() || anIIInt.IsEmpty())
409 return Standard_False;
410
411 return anIIInt.TangentFaces();
412 }
413 catch (Standard_Failure) {
414 return Standard_False;
415 }
416 }
417
2277323d 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))))
424 {
425 gp_Cylinder aCyl1, aCyl2;
426 if (getCylinder(S1, aCyl1) && getCylinder(S2, aCyl2)) {
2ba9eb30 427 if (fabs(aCyl1.Radius() - aCyl2.Radius()) < theLinTol) {
2277323d 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);
2ba9eb30 434 if (aVec12.SquareMagnitude() < theLinTol*theLinTol ||
2277323d 435 aVec12.IsParallel(aDir1, Precision::Angular())) {
436 return Standard_True;
437 }
438 }
439 }
440 }
441 }
442
443 return Standard_False;
444}
445
446//=======================================================================
447//function : MovePCurves
448//purpose :
449//=======================================================================
450static void MovePCurves(TopoDS_Face& aTarget,
451 const TopoDS_Face& aSource)
452{
453 BRep_Builder B;
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());
457 sfw->FixReorder();
458 Standard_Boolean isReoredFailed = sfw->StatusReorder ( ShapeExtend_FAIL );
459 sfw->FixEdgeCurves();
460 if(isReoredFailed)
461 continue;
462
463 sfw->FixShifted();
464 sfw->FixDegenerated();
465
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)) {
472 sewd->Remove(i);
473 i--;
474 }
475 }
476
477 TopoDS_Wire ResWire = sfw->Wire();
478 B.Add(aTarget,ResWire);
479 }
480}
481
482//=======================================================================
483//function : GlueEdgesWithPCurves
484//purpose : Glues the pcurves of the sequence of edges
485// and glues their 3d curves
486//=======================================================================
487static TopoDS_Edge GlueEdgesWithPCurves(const TopTools_SequenceOfShape& aChain,
488 const TopoDS_Vertex& FirstVertex,
489 const TopoDS_Vertex& LastVertex)
490{
491 Standard_Integer i, j;
492
493 TopoDS_Edge FirstEdge = TopoDS::Edge(aChain(1));
494 //TColGeom2d_SequenceOfCurve PCurveSeq;
495 TColGeom_SequenceOfSurface SurfSeq;
496 //TopTools_SequenceOfShape LocSeq;
497
498 BRep_ListIteratorOfListOfCurveRepresentation itr( (Handle(BRep_TEdge)::DownCast(FirstEdge.TShape()))->Curves() );
499 for (; itr.More(); itr.Next())
500 {
501 Handle(BRep_CurveRepresentation) CurveRep = itr.Value();
502 if (CurveRep->IsCurveOnSurface())
503 {
504 //PCurveSeq.Append(CurveRep->PCurve());
505 SurfSeq.Append(CurveRep->Surface());
506 /*
507 TopoDS_Shape aLocShape;
508 aLocShape.Location(CurveRep->Location());
509 LocSeq.Append(aLocShape);
510 */
511 }
512 }
513
514 Standard_Real fpar, lpar;
515 BRep_Tool::Range(FirstEdge, fpar, lpar);
516 TopoDS_Edge PrevEdge = FirstEdge;
517 TopoDS_Vertex CV;
518 Standard_Real MaxTol = 0.;
519
520 TopoDS_Edge ResEdge;
521 BRep_Builder BB;
522
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
526
527 TopoDS_Vertex PrevVertex = FirstVertex;
528 for (i = 1; i <= nb_curve; i++)
529 {
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));
534
535 Standard_Real Tol1 = BRep_Tool::Tolerance(VF);
536 Standard_Real Tol2 = BRep_Tool::Tolerance(VL);
537 if (Tol1 > MaxTol)
538 MaxTol = Tol1;
539 if (Tol2 > MaxTol)
540 MaxTol = Tol2;
541
542 if (i > 1)
543 {
544 TopExp::CommonVertex(PrevEdge, anEdge, CV);
545 Standard_Real Tol = BRep_Tool::Tolerance(CV);
546 tabtolvertex(i-2) = Tol;
547 }
548
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());
553 if (ToReverse)
554 tab_c3d(i-1)->Reverse();
555 PrevVertex = (ToReverse)? VF : VL;
556 PrevEdge = anEdge;
557 }
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,
561 tabtolvertex,
562 ArrayOfIndices,
563 concatcurve,
564 Standard_False,
565 Precision::Confusion()); //C1 concatenation
566
567 if (concatcurve->Length() > 1)
568 {
569 GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
570
571 for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
572 Concat.Add( concatcurve->Value(i), MaxTol, Standard_True );
573
574 concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
575 }
576 Handle(Geom_BSplineCurve) ResCurve = concatcurve->Value(concatcurve->Lower());
577
578 TColGeom2d_SequenceOfBoundedCurve ResPCurves;
579 TopLoc_Location aLoc;
580 for (j = 1; j <= SurfSeq.Length(); j++)
581 {
582 TColGeom2d_Array1OfBSplineCurve tab_c2d(0,nb_curve-1); //array of the pcurves
583
584 PrevVertex = FirstVertex;
585 PrevEdge = FirstEdge;
586 //TopLoc_Location theLoc = LocSeq(j).Location();
587 for (i = 1; i <= nb_curve; i++)
588 {
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));
593
594 /*
595 Handle(Geom2d_Curve) aPCurve =
596 BRep_Tool::CurveOnSurface(anEdge, SurfSeq(j), anEdge.Location()*theLoc, fpar, lpar);
597 */
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());
603 if (ToReverse)
604 tab_c2d(i-1)->Reverse();
605 PrevVertex = (ToReverse)? VF : VL;
606 PrevEdge = anEdge;
607 }
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,
611 tabtolvertex,
612 ArrayOfInd2d,
613 concatc2d,
614 Standard_False,
615 Precision::Confusion()); //C1 concatenation
616
617 if (concatc2d->Length() > 1)
618 {
619 Geom2dConvert_CompCurveToBSplineCurve Concat2d(concatc2d->Value(concatc2d->Lower()));
620
621 for (i = concatc2d->Lower()+1; i <= concatc2d->Upper(); i++)
622 Concat2d.Add( concatc2d->Value(i), MaxTol, Standard_True );
623
624 concatc2d->SetValue(concatc2d->Lower(), Concat2d.BSplineCurve());
625 }
626 Handle(Geom2d_BSplineCurve) aResPCurve = concatc2d->Value(concatc2d->Lower());
627 ResPCurves.Append(aResPCurve);
628 }
629
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++)
636 {
637 BB.UpdateEdge(ResEdge, ResPCurves(j), SurfSeq(j), aLoc, MaxTol);
638 BB.Range(ResEdge, SurfSeq(j), aLoc, ResPCurves(j)->FirstParameter(), ResPCurves(j)->LastParameter());
639 }
640
641 BRepLib::SameParameter(ResEdge, MaxTol, Standard_True);
642
643 return ResEdge;
644}
645
cef6867c 646//=======================================================================
647//function : MergeSubSeq
648//purpose : Merges a sequence of edges into one edge if possible
649//=======================================================================
650
f7d70540 651static Standard_Boolean MergeSubSeq(const TopTools_SequenceOfShape& aChain, TopoDS_Edge& OutEdge, double Tol, Standard_Boolean ConcatBSplines)
2277323d 652{
2277323d 653 ShapeAnalysis_Edge sae;
f7d70540 654 BRep_Builder B;
2277323d 655 // union edges in chain
f7d70540 656 int j;
2277323d 657 Standard_Real fp1,lp1,fp2,lp2;
f7d70540 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++)
662 {
2277323d 663 TopoDS_Edge edge1 = TopoDS::Edge(aChain.Value(j));
f7d70540 664 c3d1 = BRep_Tool::Curve(edge1,fp1,lp1);
665
666 TopoDS_Edge edge2 = TopoDS::Edge(aChain.Value(j+1));
667 c3d2 = BRep_Tool::Curve(edge2,fp2,lp2);
668
669 if(c3d1.IsNull() || c3d2.IsNull())
670 return Standard_False;
b4d4dbe8 671
2277323d 672 while(c3d1->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
673 Handle(Geom_TrimmedCurve) tc =
674 Handle(Geom_TrimmedCurve)::DownCast(c3d1);
675 c3d1 = tc->BasisCurve();
676 }
2277323d 677 while(c3d2->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
678 Handle(Geom_TrimmedCurve) tc =
679 Handle(Geom_TrimmedCurve)::DownCast(c3d2);
680 c3d2 = tc->BasisCurve();
681 }
682 if( c3d1->IsKind(STANDARD_TYPE(Geom_Line)) && c3d2->IsKind(STANDARD_TYPE(Geom_Line)) ) {
2277323d 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();
f7d70540 687 if(!Dir1.IsParallel(Dir2,Tol))
688 IsUnionOfLinesPossible = Standard_False;
2277323d 689 }
f7d70540 690 else
691 IsUnionOfLinesPossible = Standard_False;
2277323d 692 if( c3d1->IsKind(STANDARD_TYPE(Geom_Circle)) && c3d2->IsKind(STANDARD_TYPE(Geom_Circle)) ) {
2277323d 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();
b4d4dbe8 697 if (P01.Distance(P02) > Precision::Confusion())
f7d70540 698 IsUnionOfCirclesPossible = Standard_False;
699 }
700 else
701 IsUnionOfCirclesPossible = Standard_False;
702 }
703 if (IsUnionOfLinesPossible && IsUnionOfCirclesPossible)
704 return Standard_False;
705
706 //union of lines is possible
707 if (IsUnionOfLinesPossible)
708 {
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);
717 TopoDS_Edge E;
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.);
722 OutEdge = E;
723 return Standard_True;
724 }
725
726 if (IsUnionOfCirclesPossible)
727 {
728 double f,l;
729 TopoDS_Edge FE = TopoDS::Edge(aChain.First());
730 Handle(Geom_Curve) c3d = BRep_Tool::Curve(FE,f,l);
731
732 while(c3d->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
733 Handle(Geom_TrimmedCurve) tc =
734 Handle(Geom_TrimmedCurve)::DownCast(c3d);
735 c3d = tc->BasisCurve();
736 }
737 Handle(Geom_Circle) Cir = Handle(Geom_Circle)::DownCast(c3d);
738
739 TopoDS_Vertex V1 = sae.FirstVertex(FE);
f7d70540 740 TopoDS_Vertex V2 = sae.LastVertex(TopoDS::Edge(aChain.Last()));
f7d70540 741 TopoDS_Edge E;
dd2f1b75 742 if (V1.IsSame(V2)) {
f7d70540 743 // closed chain
dd2f1b75 744 BRepAdaptor_Curve adef(FE);
745 Handle(Geom_Circle) Cir1;
746 double FP, LP;
747 if ( FE.Orientation() == TopAbs_FORWARD)
748 {
749 FP = adef.FirstParameter();
750 LP = adef.LastParameter();
751 }
752 else
753 {
754 FP = adef.LastParameter();
755 LP = adef.FirstParameter();
756 }
757 if (Abs(FP) < Precision::PConfusion())
758 {
f0144633 759 B.MakeEdge (E,Cir, Precision::Confusion());
760 B.Add(E,V1);
761 B.Add(E,V2);
762 E.Orientation(FE.Orientation());
763 }
dd2f1b75 764 else
765 {
766 GC_MakeCircle MC1 (adef.Value(FP), adef.Value((FP + LP) * 0.5), adef.Value(LP));
767 if (MC1.IsDone())
768 Cir1 = MC1.Value();
769 else
770 return Standard_False;
771 B.MakeEdge (E, Cir1, Precision::Confusion());
772 B.Add(E,V1);
773 B.Add(E,V2);
774 }
f7d70540 775 }
776 else {
dd2f1b75 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);
f7d70540 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()) {
788 // check orientation
789 gp_Dir ND = C->XAxis().Direction().Crossed(D1);
790 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
791 fpar = -fpar;
2277323d 792 }
f7d70540 793 }
794 Standard_Real lpar = C->XAxis().Direction().Angle(D2);
795 if(fabs(lpar)>Precision::Confusion()) {
796 // check orientation
797 gp_Dir ND = C->XAxis().Direction().Crossed(D2);
798 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
799 lpar = -lpar;
2277323d 800 }
2277323d 801 }
f7d70540 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());
805 B.Add(E,V1);
806 B.Add(E,V2);
807 B.UpdateVertex(V1, fpar, E, 0.);
808 B.UpdateVertex(V2, lpar, E, 0.);
2277323d 809 }
f7d70540 810 OutEdge = E;
811 return Standard_True;
2277323d 812 }
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
f7d70540 817
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;
2277323d 821 for(j=1; j<=aChain.Length(); j++) {
822 TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
b4d4dbe8 823 TopLoc_Location Loc;
2277323d 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();
830 }
831 if( ( c3d->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ||
832 c3d->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ) ) continue;
f7d70540 833 NeedUnion = Standard_False;
2277323d 834 break;
835 }
836 if(NeedUnion) {
0797d9d3 837#ifdef OCCT_DEBUG
2277323d 838 cout<<"can not make analitical union => make approximation"<<endl;
839#endif
840 TopoDS_Edge E = GlueEdgesWithPCurves(aChain, VF, VL);
f7d70540 841 OutEdge = E;
842 return Standard_True;
2277323d 843 }
844 else {
0797d9d3 845#ifdef OCCT_DEBUG
2277323d 846 cout<<"can not make approximation for such types of curves"<<endl;
847#endif
848 return Standard_False;
849 }
850 }
f7d70540 851 return Standard_False;
852}
853
cef6867c 854//=======================================================================
855//function : IsMergingPossible
856//purpose : Checks if merging of two edges is possible
857//=======================================================================
858
f7d70540 859static Standard_Boolean IsMergingPossible(const TopoDS_Edge& edge1, const TopoDS_Edge& edge2,
860 double Tol, const TopTools_MapOfShape& DegEdgeVrt)
861{
862 TopoDS_Vertex CV = TopExp::LastVertex(edge1, Standard_True);
863 if (CV.IsNull() || DegEdgeVrt.Contains(CV))
864 return Standard_False;
865
866 BRepAdaptor_Curve ade1(edge1);
867 BRepAdaptor_Curve ade2(edge2);
868
869 GeomAbs_CurveType t1 = ade1.GetType();
870 GeomAbs_CurveType t2 = ade2.GetType();
871
872 if( t1 == GeomAbs_Circle && t2 == GeomAbs_Circle)
873 {
874 if (ade1.Circle().Location().Distance(ade2.Circle().Location()) > Precision::Confusion())
875 return Standard_False;
876 }
877
878 if( ( (t1 != GeomAbs_BezierCurve && t1 != GeomAbs_BSplineCurve) ||
879 (t2 != GeomAbs_BezierCurve && t2 != GeomAbs_BSplineCurve)) && t1 != t2)
880 return Standard_False;
881
882 gp_Vec Diff1, Diff2;
883 gp_Pnt P1, P2;
884 if (edge1.Orientation() == TopAbs_FORWARD)
885 ade1.D1(ade1.LastParameter(), P1, Diff1);
886 else
887 {
888 ade1.D1(ade1.FirstParameter(), P1, Diff1);
889 Diff1 = -Diff1;
890 }
891
892 if (edge2.Orientation() == TopAbs_FORWARD)
893 ade2.D1(ade2.FirstParameter(), P2, Diff2);
894 else
895 {
896 ade2.D1(ade2.LastParameter(), P2, Diff2);
897 Diff2 = -Diff2;
898 }
899
900 if (Diff1.Angle(Diff2) > Tol)
901 return Standard_False;
902
903 return Standard_True;
904}
905
cef6867c 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//=======================================================================
911
f7d70540 912static void GenerateSubSeq (const TopTools_SequenceOfShape& anInpEdgeSeq,
913 NCollection_Sequence<SubSequenceOfEdges>& SeqOfSubSeqOfEdges,
914 Standard_Boolean IsClosed, double Tol, const TopTools_MapOfShape& DegEdgeVrt )
915{
916 Standard_Boolean isOk = Standard_False;
917 TopoDS_Edge edge1, edge2;
918
919 SubSequenceOfEdges SubSeq;
920 SubSeq.SeqsEdges.Append(TopoDS::Edge(anInpEdgeSeq(1)));
921 SeqOfSubSeqOfEdges.Append(SubSeq);
922
923 for (int i = 1; i < anInpEdgeSeq.Length(); i++)
924 {
925 edge1 = TopoDS::Edge(anInpEdgeSeq(i));
926 edge2 = TopoDS::Edge(anInpEdgeSeq(i+1));
927 isOk = IsMergingPossible(edge1, edge2, Tol, DegEdgeVrt);
928 if (!isOk)
929 {
51740958 930 SubSequenceOfEdges aSubSeq;
931 aSubSeq.SeqsEdges.Append(edge2);
932 SeqOfSubSeqOfEdges.Append(aSubSeq);
f7d70540 933 }
934 else
935 SeqOfSubSeqOfEdges.ChangeLast().SeqsEdges.Append(edge2);
936 }
937 /// check first and last chain segments
938 if (IsClosed && SeqOfSubSeqOfEdges.Length() > 1)
939 {
940 edge1 = TopoDS::Edge(anInpEdgeSeq.Last());
941 edge2 = TopoDS::Edge(anInpEdgeSeq.First());
942 if (IsMergingPossible(edge1, edge2, Tol, DegEdgeVrt))
943 {
944 SeqOfSubSeqOfEdges.ChangeLast().SeqsEdges.Append(SeqOfSubSeqOfEdges.ChangeFirst().SeqsEdges);
945 SeqOfSubSeqOfEdges.Remove(1);
946 }
947 }
948}
949
950
951//=======================================================================
952//function : MergeEdges
953//purpose : auxilary
954//=======================================================================
f98965d2 955static Standard_Boolean MergeEdges(TopTools_SequenceOfShape& SeqEdges,
f7d70540 956 const Standard_Real Tol,
957 const Standard_Boolean ConcatBSplines,
958 NCollection_Sequence<SubSequenceOfEdges>& SeqOfSubSeqOfEdges,
959 const TopTools_MapOfShape& NonMergVrt )
960{
f98965d2 961 // skip degenerated edges, and forbid merging through them
962 TopTools_IndexedDataMapOfShapeListOfShape aMapVE;
f7d70540 963 Standard_Integer j;
964 TopTools_MapOfShape VerticesToAvoid;
f7d70540 965 for (j = 1; j <= SeqEdges.Length(); j++)
966 {
967 TopoDS_Edge anEdge = TopoDS::Edge(SeqEdges(j));
968 if (BRep_Tool::Degenerated(anEdge))
969 {
970 TopoDS_Vertex V1, V2;
971 TopExp::Vertices(anEdge, V1, V2);
972 VerticesToAvoid.Add(V1);
973 VerticesToAvoid.Add(V2);
f98965d2 974 SeqEdges.Remove(j--);
975 }
976 else
977 {
978 // fill in the map V-E
979 for (TopoDS_Iterator it(anEdge.Oriented(TopAbs_FORWARD)); it.More(); it.Next())
980 {
981 TopoDS_Shape aV = it.Value();
982 if (aV.Orientation() == TopAbs_FORWARD || aV.Orientation() == TopAbs_REVERSED)
983 {
984 if (!aMapVE.Contains(aV))
985 aMapVE.Add(aV, TopTools_ListOfShape());
986 aMapVE.ChangeFromKey(aV).Append(anEdge);
38c2acd4 987 }
f98965d2 988 }
f7d70540 989 }
f7d70540 990 }
f98965d2 991 VerticesToAvoid.Unite(NonMergVrt);
f7d70540 992
f98965d2 993 // do loop while there are unused edges
994 TopTools_MapOfShape aUsedEdges;
995 for (;;)
996 {
997 TopoDS_Edge edge;
998 for(j=1; j <= SeqEdges.Length(); j++)
999 {
1000 edge = TopoDS::Edge(SeqEdges.Value(j));
1001 if (!aUsedEdges.Contains(edge))
1002 break;
1003 }
1004 if (j > SeqEdges.Length())
1005 break; // all edges have been used
1006
1007 // make chain for unite
1008 TopTools_SequenceOfShape aChain;
38c2acd4 1009 aChain.Append(edge);
f98965d2 1010 aUsedEdges.Add(edge);
1011 TopoDS_Vertex V[2];
1012 TopExp::Vertices(edge, V[0], V[1], Standard_True);
1013
1014 // connect more edges to the chain in both directions
1015 for (j = 0; j < 2; j++)
1016 {
1017 Standard_Boolean isAdded = Standard_True;
1018 while (isAdded)
1019 {
1020 isAdded = Standard_False;
1021 if (V[j].IsNull())
1022 break;
1023 const TopTools_ListOfShape& aLE = aMapVE.FindFromKey(V[j]);
1024 for (TopTools_ListIteratorOfListOfShape itL(aLE); itL.More(); itL.Next())
1025 {
1026 edge = TopoDS::Edge(itL.Value());
1027 if (!aUsedEdges.Contains(edge))
1028 {
38c2acd4 1029 TopoDS_Vertex V2[2];
1030 TopExp::Vertices(edge, V2[0], V2[1], Standard_True);
1031 // the neighboring edge must have V[j] reversed and located on the opposite end
1032 if (V2[1 - j].IsEqual(V[j].Reversed()))
1033 {
1034 if (j == 0)
1035 aChain.Prepend(edge);
1036 else
1037 aChain.Append(edge);
1038 aUsedEdges.Add(edge);
1039 V[j] = V2[j];
1040 isAdded = Standard_True;
1041 break;
1042 }
1043 }
1044 }
f7d70540 1045 }
1046 }
f7d70540 1047
f98965d2 1048 if (aChain.Length() < 2)
1049 continue;
2277323d 1050
38c2acd4 1051 Standard_Boolean IsClosed = Standard_False;
f98965d2 1052 if (V[0].IsSame ( V[1] ))
38c2acd4 1053 IsClosed = Standard_True;
f98965d2 1054
1055 // split chain by vertices at which merging is not possible
1056 NCollection_Sequence<SubSequenceOfEdges> aOneSeq;
1057 GenerateSubSeq(aChain, aOneSeq, IsClosed, Tol, VerticesToAvoid);
1058
1059 // put sub-chains in the result
1060 SeqOfSubSeqOfEdges.Append(aOneSeq);
1061 }
f7d70540 1062
1063 for (int i = 1; i <= SeqOfSubSeqOfEdges.Length(); i++)
1064 {
1065 TopoDS_Edge UE;
1066 if (SeqOfSubSeqOfEdges(i).SeqsEdges.Length() < 2)
1067 continue;
1068 if (MergeSubSeq(SeqOfSubSeqOfEdges(i).SeqsEdges, UE, Tol, ConcatBSplines))
1069 SeqOfSubSeqOfEdges(i).UnionEdges = UE;
1070 }
2277323d 1071 return Standard_True;
1072}
1073
cef6867c 1074//=======================================================================
1075//function : MergeSeq
1076//purpose : Tries to unify the sequence of edges with the set of another edges
1077//which lies on the same geometry
1078//=======================================================================
1079
f98965d2 1080static Standard_Boolean MergeSeq (TopTools_SequenceOfShape& SeqEdges,
f7d70540 1081 const Standard_Real Tol,
1082 const Standard_Boolean ConcatBSplines,
1083 Handle(ShapeBuild_ReShape)& theContext,
1084 TopTools_DataMapOfShapeShape& theOldShapes,
1085 const TopTools_MapOfShape& nonMergVert,
1086 const TopTools_DataMapOfShapeShape& NewEdges2OldEdges)
1087{
1088 NCollection_Sequence<SubSequenceOfEdges> SeqOfSubsSeqOfEdges;
1089 int k = 1;
1090 if ( MergeEdges(SeqEdges, Tol, ConcatBSplines, SeqOfSubsSeqOfEdges, nonMergVert) )
1091 {
1092 for (Standard_Integer i = 1; i <= SeqOfSubsSeqOfEdges.Length(); i++ )
1093 {
1094 if (SeqOfSubsSeqOfEdges(i).UnionEdges.IsNull())
1095 continue;
1096 theContext->Replace(SeqOfSubsSeqOfEdges(i).SeqsEdges(1), SeqOfSubsSeqOfEdges(i).UnionEdges);
1097 k++;
1098 for (Standard_Integer j = 2; j <= SeqOfSubsSeqOfEdges(i).SeqsEdges.Length(); j++)
1099 {
2ba9eb30 1100 const TopoDS_Shape& anOldEdge = SeqOfSubsSeqOfEdges(i).SeqsEdges(j);
1101 const TopoDS_Shape* pOrigEdge = NewEdges2OldEdges.Seek(anOldEdge);
1102 if (!pOrigEdge)
1103 pOrigEdge = &anOldEdge;
1104 theOldShapes.Bind(*pOrigEdge, SeqOfSubsSeqOfEdges(i).UnionEdges);
f7d70540 1105 theContext->Remove(SeqOfSubsSeqOfEdges(i).SeqsEdges(j));
1106 }
1107 }
1108 return Standard_True;
1109 }
1110 else
1111 return Standard_False;
1112}
1113
cef6867c 1114//=======================================================================
1115//function : CheckSharedVertices
1116//purpose : Checks the sequence of edges on the presence of shared vertex
1117//=======================================================================
1118
f7d70540 1119static void CheckSharedVertices(const TopTools_SequenceOfShape& theSeqEdges,
1120 const TopTools_IndexedDataMapOfShapeListOfShape& theMapEdgesVertex,
1121 TopTools_MapOfShape& theShareVertMap)
1122{
1123 ShapeAnalysis_Edge sae;
1124 TopTools_SequenceOfShape SeqVertexes;
1125 TopTools_MapOfShape MapVertexes;
1126 for (Standard_Integer k = 1; k <= theSeqEdges.Length(); k++ )
1127 {
1128 TopoDS_Vertex aV1 = sae.FirstVertex(TopoDS::Edge(theSeqEdges(k)));
1129 TopoDS_Vertex aV2 = sae.LastVertex(TopoDS::Edge(theSeqEdges(k)));
1130 if (!MapVertexes.Add(aV1))
1131 SeqVertexes.Append(aV1);
1132 if (!MapVertexes.Add(aV2))
1133 SeqVertexes.Append(aV2);
1134 }
1135
1136 for (Standard_Integer k = 1; k <= SeqVertexes.Length()/* && !IsSharedVertexPresent*/; k++ )
1137 {
1138 const TopTools_ListOfShape& ListEdgesV1 = theMapEdgesVertex.FindFromKey(SeqVertexes(k));
1139 TopTools_MapOfShape aMapOfEdges;
1140 TopTools_ListIteratorOfListOfShape iter(ListEdgesV1);
1141 for (; iter.More(); iter.Next())
1142 aMapOfEdges.Add(iter.Value());
1143 if (aMapOfEdges.Extent() > 2)
1144 theShareVertMap.Add(SeqVertexes(k));
1145 }
1146 //return theShareVertMap.IsEmpty() ? false : true;
1147}
1148
2277323d 1149//=======================================================================
1150//function : ShapeUpgrade_UnifySameDomain
1151//purpose : Constructor
1152//=======================================================================
1153
1154ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain()
2ba9eb30 1155 : myLinTol(Precision::Confusion()),
1156 myAngTol(Precision::Angular()),
1157 myUnifyFaces(Standard_True),
fe1a6e4e 1158 myUnifyEdges (Standard_True),
1159 myConcatBSplines (Standard_False),
1160 myAllowInternal (Standard_False)
2277323d 1161{
2277323d 1162 myContext = new ShapeBuild_ReShape;
1163}
1164
1165//=======================================================================
1166//function : ShapeUpgrade_UnifySameDomain
1167//purpose : Constructor
1168//=======================================================================
1169
1170ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain(const TopoDS_Shape& aShape,
1171 const Standard_Boolean UnifyEdges,
1172 const Standard_Boolean UnifyFaces,
1173 const Standard_Boolean ConcatBSplines)
fe1a6e4e 1174 : myInitShape (aShape),
2ba9eb30 1175 myLinTol(Precision::Confusion()),
1176 myAngTol(Precision::Angular()),
1177 myUnifyFaces(UnifyFaces),
fe1a6e4e 1178 myUnifyEdges (UnifyEdges),
1179 myConcatBSplines (ConcatBSplines),
1180 myAllowInternal (Standard_False),
1181 myShape (aShape)
2277323d 1182{
2277323d 1183 myContext = new ShapeBuild_ReShape;
1184}
1185
1186//=======================================================================
1187//function : Initialize
1188//purpose :
1189//=======================================================================
1190
1191void ShapeUpgrade_UnifySameDomain::Initialize(const TopoDS_Shape& aShape,
1192 const Standard_Boolean UnifyEdges,
1193 const Standard_Boolean UnifyFaces,
1194 const Standard_Boolean ConcatBSplines)
1195{
1196 myInitShape = aShape;
1197 myShape = aShape;
1198 myUnifyEdges = UnifyEdges;
1199 myUnifyFaces = UnifyFaces;
1200 myConcatBSplines = ConcatBSplines;
1201
1202 myContext->Clear();
f7d70540 1203 myOldShapes.Clear();
fe1a6e4e 1204}
1205
1206//=======================================================================
1207//function : AllowInternalEdges
1208//purpose :
1209//=======================================================================
1210
1211void ShapeUpgrade_UnifySameDomain::AllowInternalEdges (const Standard_Boolean theValue)
1212{
1213 myAllowInternal = theValue;
2277323d 1214}
1215
6a0c0b14 1216//=======================================================================
1217//function : putIntWires
1218//purpose : Add internal wires that are classified inside the face as a subshape,
1219// and remove them from the sequence
1220//=======================================================================
1221static void putIntWires(TopoDS_Shape& theFace, TopTools_SequenceOfShape& theWires)
1222{
1223 TopoDS_Face& aFace = TopoDS::Face(theFace);
1224 for (Standard_Integer i=1; i <= theWires.Length(); i++)
1225 {
1226 TopoDS_Shape aWire = theWires(i);
1227 gp_Pnt2d aP2d;
1228 Standard_Boolean isP2d = Standard_False;
1229 for (TopoDS_Iterator it(aWire); it.More() && !isP2d; it.Next())
1230 {
1231 const TopoDS_Edge& anEdge = TopoDS::Edge(it.Value());
1232 Standard_Real aFirst, aLast;
1233 Handle(Geom2d_Curve) aC2d = BRep_Tool::CurveOnSurface(anEdge, aFace, aFirst, aLast);
1234 aC2d->D0((aFirst + aLast) * 0.5, aP2d);
1235 isP2d = Standard_True;
1236 }
1237 BRepClass_FaceClassifier aClass(aFace, aP2d, Precision::PConfusion());
1238 if (aClass.State() == TopAbs_IN)
1239 {
1240 BRep_Builder().Add(aFace, aWire);
1241 theWires.Remove(i);
1242 i--;
1243 }
1244 }
1245}
1246
2277323d 1247//=======================================================================
1248//function : UnifyFaces
1249//purpose :
1250//=======================================================================
1251
1252void ShapeUpgrade_UnifySameDomain::UnifyFaces()
1253{
fe1a6e4e 1254 // creating map of edge faces for the whole shape
1255 TopTools_IndexedDataMapOfShapeListOfShape aGMapEdgeFaces;
1256 TopExp::MapShapesAndAncestors(myShape, TopAbs_EDGE, TopAbs_FACE, aGMapEdgeFaces);
56091b56 1257
1258 // unify faces in each shell separately
2277323d 1259 TopExp_Explorer exps;
56091b56 1260 for (exps.Init(myShape, TopAbs_SHELL); exps.More(); exps.Next())
1261 IntUnifyFaces(exps.Current(), aGMapEdgeFaces, Standard_False);
1262
1263 // gather all faces out of shells in one compound and unify them at once
1264 BRep_Builder aBB;
1265 TopoDS_Compound aCmp;
1266 aBB.MakeCompound(aCmp);
1267 Standard_Integer nbf = 0;
1268 for (exps.Init(myShape, TopAbs_FACE, TopAbs_SHELL); exps.More(); exps.Next(), nbf++)
1269 aBB.Add(aCmp, exps.Current());
1270
1271 if (nbf > 0)
1272 IntUnifyFaces(aCmp, aGMapEdgeFaces, Standard_True);
1273
1274 myShape = myContext->Apply(myShape);
1275}
2277323d 1276
2277323d 1277
56091b56 1278void ShapeUpgrade_UnifySameDomain::IntUnifyFaces(const TopoDS_Shape& theInpShape,
1279 const TopTools_IndexedDataMapOfShapeListOfShape& theGMapEdgeFaces,
1280 Standard_Boolean IsCheckSharedEdgeOri)
1281{
1282 // creating map of edge faces for the shape
1283 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
1284 TopExp::MapShapesAndAncestors(theInpShape, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
1285
1286 // map of processed shapes
1287 TopTools_MapOfShape aProcessed;
1288
1289 Standard_Integer NbModif = 0;
1290 Standard_Boolean hasFailed = Standard_False;
1291 Standard_Real tol = Precision::Confusion();
1292
1293 // count faces
1294 Standard_Integer nbf = 0;
1295 TopExp_Explorer exp;
1296 TopTools_MapOfShape mapF;
1297 for (exp.Init(theInpShape, TopAbs_FACE); exp.More(); exp.Next()) {
1298 if (mapF.Add(exp.Current()))
1299 nbf++;
1300 }
2277323d 1301
56091b56 1302 // processing each face
1303 mapF.Clear();
1304 for (exp.Init(theInpShape, TopAbs_FACE); exp.More(); exp.Next()) {
10ce3246 1305 const TopoDS_Face& aFaceOriginal = TopoDS::Face(exp.Current());
1306 TopoDS_Face aFace = TopoDS::Face(aFaceOriginal.Oriented(TopAbs_FORWARD));
2277323d 1307
56091b56 1308 if (aProcessed.Contains(aFace))
1309 continue;
2277323d 1310
56091b56 1311 Standard_Integer dummy;
1312 TopTools_SequenceOfShape edges;
1313 AddOrdinaryEdges(edges,aFace,dummy);
2277323d 1314
56091b56 1315 TopTools_SequenceOfShape faces;
1316 faces.Append(aFace);
1317
1318 //surface and location to construct result
1319 TopLoc_Location aBaseLocation;
1320 Handle(Geom_Surface) aBaseSurface = BRep_Tool::Surface(aFace,aBaseLocation);
1321 aBaseSurface = ClearRts(aBaseSurface);
1322
1323 // find adjacent faces to union
1324 Standard_Integer i;
1325 for (i = 1; i <= edges.Length(); i++) {
1326 TopoDS_Edge edge = TopoDS::Edge(edges(i));
1327 if (BRep_Tool::Degenerated(edge))
1328 continue;
2277323d 1329
56091b56 1330 // get connectivity of the edge in the global shape
1331 const TopTools_ListOfShape& aGList = theGMapEdgeFaces.FindFromKey(edge);
1332 if (!myAllowInternal && aGList.Extent() != 2) {
1333 // non mainfold case is not processed unless myAllowInternal
1334 continue;
1335 }
10ce3246 1336 //
1337 // get normal of the face to compare it with normals of other faces
1338 gp_Dir aDN1;
1339 //
1340 // take intermediate point on edge to compute the normal
1341 Standard_Real f, l;
1342 BRep_Tool::Range(edge, f, l);
1343 Standard_Real aTMid = (f + l) * .5;
1344 //
1345 Standard_Boolean bCheckNormals = GetNormalToSurface(aFaceOriginal, edge, aTMid, aDN1);
1346 //
56091b56 1347 // process faces connected through the edge in the current shape
1348 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
1349 TopTools_ListIteratorOfListOfShape anIter(aList);
1350 for (; anIter.More(); anIter.Next()) {
10ce3246 1351 const TopoDS_Face& aCheckedFaceOriginal = TopoDS::Face(anIter.Value());
1352 TopoDS_Face anCheckedFace = TopoDS::Face(aCheckedFaceOriginal.Oriented(TopAbs_FORWARD));
56091b56 1353 if (anCheckedFace.IsSame(aFace))
fe1a6e4e 1354 continue;
2277323d 1355
56091b56 1356 if (aProcessed.Contains(anCheckedFace))
1357 continue;
2277323d 1358
56091b56 1359 if (IsCheckSharedEdgeOri && !CheckSharedEdgeOri(aFace, anCheckedFace, edge) )
1360 continue;
2277323d 1361
10ce3246 1362 if (bCheckNormals) {
1363 // get normal of checked face using the same parameter on edge
1364 gp_Dir aDN2;
1365 if (GetNormalToSurface(aCheckedFaceOriginal, edge, aTMid, aDN2)) {
1366 // and check if the adjacent faces are having approximately same normals
1367 Standard_Real anAngle = aDN1.Angle(aDN2);
1368 if (anAngle > myAngTol) {
1369 continue;
1370 }
1371 }
1372 }
1373 //
2ba9eb30 1374 if (IsSameDomain(aFace,anCheckedFace, myLinTol, myAngTol)) {
f0144633 1375
56091b56 1376 // hotfix for 27271: prevent merging along periodic direction.
d3dadd23 1377 if (IsLikeSeam(edge, anCheckedFace, aBaseSurface))
56091b56 1378 continue;
2277323d 1379
56091b56 1380 // replacing pcurves
1381 TopoDS_Face aMockUpFace;
1382 BRep_Builder B;
1383 B.MakeFace(aMockUpFace,aBaseSurface,aBaseLocation,0.);
1384 MovePCurves(aMockUpFace,anCheckedFace);
2277323d 1385
56091b56 1386 if (AddOrdinaryEdges(edges,aMockUpFace,dummy)) {
1387 // sequence edges is modified
1388 i = dummy;
2277323d 1389 }
56091b56 1390
1391 faces.Append(anCheckedFace);
1392 aProcessed.Add(anCheckedFace);
1393 break;
2277323d 1394 }
1395 }
56091b56 1396 }
2277323d 1397
56091b56 1398 if (faces.Length() > 1) {
1399 // fill in the connectivity map for selected faces
1400 TopTools_IndexedDataMapOfShapeListOfShape aMapEF;
1401 for (i = 1; i <= faces.Length(); i++) {
1402 TopExp::MapShapesAndAncestors(faces(i), TopAbs_EDGE, TopAbs_FACE, aMapEF);
1403 }
fe1a6e4e 1404
56091b56 1405 // Collect multiconnected edges, i.e. edges that are internal to
1406 // the set of selected faces and have connections to other faces.
1407 TopTools_ListOfShape aMultEdges;
1408 for (i = 1; i <= aMapEF.Extent(); i++) {
1409 const TopTools_ListOfShape& aLF = aMapEF(i);
1410 if (aLF.Extent() == 2) {
1411 const TopoDS_Shape& aE = aMapEF.FindKey(i);
1412 const TopTools_ListOfShape& aGLF = theGMapEdgeFaces.FindFromKey(aE);
1413 if (aGLF.Extent() > 2) {
1414 aMultEdges.Append(aE);
fe1a6e4e 1415 }
1416 }
56091b56 1417 }
1418 if (!aMultEdges.IsEmpty()) {
1419 if (!myAllowInternal) {
1420 // Remove from the selection the faces containing multiconnected edges
1421 TopTools_MapOfShape anAvoidFaces;
1422 TopTools_ListIteratorOfListOfShape it(aMultEdges);
1423 for (; it.More(); it.Next()) {
1424 const TopoDS_Shape& aE = it.Value();
1425 const TopTools_ListOfShape& aLF = aMapEF.FindFromKey(aE);
1426 anAvoidFaces.Add(aLF.First());
1427 anAvoidFaces.Add(aLF.Last());
1428 }
1429 for (i = 1; i <= faces.Length(); ) {
1430 if (anAvoidFaces.Contains(faces(i))) {
1431 // update the boundaries of merged area, for that
1432 // remove from 'edges' the edges of this face and add to 'edges'
1433 // the edges of this face that were not present in 'edges' before
1434 TopExp_Explorer ex(faces(i), TopAbs_EDGE);
1435 for (; ex.More(); ex.Next()) {
1436 TopoDS_Shape aE = ex.Current();
1437 Standard_Integer j;
1438 for (j = 1; j <= edges.Length(); j++) {
1439 if (edges(j).IsSame(aE))
1440 break;
cb120537 1441 }
56091b56 1442 if (j <= edges.Length())
1443 edges.Remove(j);
1444 else
1445 edges.Append(aE);
cb120537 1446 }
56091b56 1447 faces.Remove(i);
fe1a6e4e 1448 }
56091b56 1449 else
1450 i++;
fe1a6e4e 1451 }
56091b56 1452 }
1453 else {
1454 // add multiconnected edges as internal in new face
1455 TopTools_ListIteratorOfListOfShape it(aMultEdges);
1456 for (; it.More(); it.Next()) {
1457 const TopoDS_Shape& aE = it.Value();
1458 edges.Append(aE.Oriented(TopAbs_INTERNAL));
fe1a6e4e 1459 }
1460 }
1461 }
56091b56 1462 }
fe1a6e4e 1463
56091b56 1464 // all faces collected in the sequence. Perform union of faces
1465 if (faces.Length() > 1) {
1466 NbModif++;
1467 TopoDS_Face aResult;
1468 BRep_Builder B;
1469 B.MakeFace(aResult,aBaseSurface,aBaseLocation,0);
1470 Standard_Integer nbWires = 0;
1471
1472 TopoDS_Face tmpF = TopoDS::Face(myContext->Apply(faces(1).Oriented(TopAbs_FORWARD)));
1473 // connecting wires
1474 while (edges.Length()>0) {
1475
1476 Standard_Boolean isEdge3d = Standard_False;
1477 nbWires++;
1478 TopTools_MapOfShape aVertices;
1479 TopoDS_Wire aWire;
1480 B.MakeWire(aWire);
1481
1482 TopoDS_Edge anEdge = TopoDS::Edge(edges(1));
1483 edges.Remove(1);
1484 // collect internal edges in separate wires
1485 Standard_Boolean isInternal = (anEdge.Orientation() == TopAbs_INTERNAL);
1486
1487 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
1488 B.Add(aWire,anEdge);
1489 TopoDS_Vertex V1,V2;
1490 TopExp::Vertices(anEdge,V1,V2);
1491 aVertices.Add(V1);
1492 aVertices.Add(V2);
1493
1494 Standard_Boolean isNewFound = Standard_False;
1495 do {
1496 isNewFound = Standard_False;
1497 for(Standard_Integer j = 1; j <= edges.Length(); j++) {
1498 anEdge = TopoDS::Edge(edges(j));
1499 // check if the current edge orientation corresponds to the first one
1500 Standard_Boolean isCurrInternal = (anEdge.Orientation() == TopAbs_INTERNAL);
1501 if (isCurrInternal != isInternal)
1502 continue;
1503 TopExp::Vertices(anEdge,V1,V2);
1504 if(aVertices.Contains(V1) || aVertices.Contains(V2)) {
1505 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
1506 aVertices.Add(V1);
1507 aVertices.Add(V2);
1508 B.Add(aWire,anEdge);
1509 edges.Remove(j);
1510 j--;
1511 isNewFound = Standard_True;
2277323d 1512 }
2277323d 1513 }
56091b56 1514 } while (isNewFound);
1515
1516 // sorting any type of edges
1517 aWire.Closed (BRep_Tool::IsClosed (aWire));
1518 aWire = TopoDS::Wire(myContext->Apply(aWire));
1519
1520 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(aWire,tmpF,Precision::Confusion());
1521 sfw->FixReorder();
1522 Standard_Boolean isDegRemoved = Standard_False;
1523 if(!sfw->StatusReorder ( ShapeExtend_FAIL )) {
1524 // clear degenerated edges if at least one with 3d curve exist
2277323d 1525 if(isEdge3d) {
56091b56 1526 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
1527 for(Standard_Integer j = 1; j<=sewd->NbEdges();j++) {
1528 TopoDS_Edge E = sewd->Edge(j);
1529 if(BRep_Tool::Degenerated(E)) {
1530 sewd->Remove(j);
1531 isDegRemoved = Standard_True;
1532 j--;
2277323d 1533 }
2277323d 1534 }
2277323d 1535 }
56091b56 1536 sfw->FixShifted();
1537 if(isDegRemoved)
1538 sfw->FixDegenerated();
2277323d 1539 }
56091b56 1540 TopoDS_Wire aWireFixed = sfw->Wire();
1541 //aContext->Replace(aWire,aWireFixed);
1542 myContext->Replace(aWire,aWireFixed);
2277323d 1543 //for history
1544 /*
56091b56 1545 if (!myOldNewMap.IsBound(aWire))
2277323d 1546 {
56091b56 1547 TopTools_ListOfShape EmptyList;
1548 myOldNewMap.Bind(aWire, EmptyList);
2277323d 1549 }
56091b56 1550 myOldNewMap(aWire).Clear();
1551 myOldNewMap(aWire).Append(aWireFixed);
2277323d 1552 */
1553 /////////////
1554
56091b56 1555 // add resulting wire
1556 if(isEdge3d) {
1557 B.Add(aResult,aWireFixed);
1558 }
1559 else {
1560 // sorting edges
1561 Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
1562 Standard_Integer nbEdges = sbwd->NbEdges();
1563 // sort degenerated edges and create one edge instead of several ones
1564 ShapeAnalysis_WireOrder sawo(Standard_False, 0);
1565 ShapeAnalysis_Edge sae;
1566 Standard_Integer aLastEdge = nbEdges;
1567 for(Standard_Integer j = 1; j <= nbEdges; j++) {
1568 Standard_Real f,l;
1569 //smh protection on NULL pcurve
1570 Handle(Geom2d_Curve) c2d;
1571 if(!sae.PCurve(sbwd->Edge(j),tmpF,c2d,f,l)) {
1572 aLastEdge--;
1573 continue;
38c2acd4 1574 }
56091b56 1575 sawo.Add(c2d->Value(f).XY(),c2d->Value(l).XY());
2277323d 1576 }
d3dadd23 1577 if (sawo.NbEdges() == 0)
1578 continue;
56091b56 1579 sawo.Perform();
1580
1581 // constructind one degenerative edge
1582 gp_XY aStart, anEnd, tmp;
1583 Standard_Integer nbFirst = sawo.Ordered(1);
1584 TopoDS_Edge anOrigE = TopoDS::Edge(sbwd->Edge(nbFirst).Oriented(TopAbs_FORWARD));
1585 ShapeBuild_Edge sbe;
1586 TopoDS_Vertex aDummyV;
1587 TopoDS_Edge E = sbe.CopyReplaceVertices(anOrigE,aDummyV,aDummyV);
1588 sawo.XY(nbFirst,aStart,tmp);
1589 sawo.XY(sawo.Ordered(aLastEdge),tmp,anEnd);
1590
1591 gp_XY aVec = anEnd-aStart;
1592 Handle(Geom2d_Line) aLine = new Geom2d_Line(aStart,gp_Dir2d(anEnd-aStart));
1593
1594 B.UpdateEdge(E,aLine,tmpF,0.);
1595 B.Range(E,tmpF,0.,aVec.Modulus());
1596 Handle(Geom_Curve) C3d;
1597 B.UpdateEdge(E,C3d,0.);
1598 B.Degenerated(E,Standard_True);
1599 TopoDS_Wire aW;
1600 B.MakeWire(aW);
1601 B.Add(aW,E);
1602 aW.Closed (Standard_True);
1603 B.Add(aResult,aW);
1604 }
1605 }
2277323d 1606
56091b56 1607 // perform substitution of face
1608 //aContext->Replace(aContext->Apply(aFace),aResult);
1609 myContext->Replace(myContext->Apply(aFace),aResult);
1610 //for history
1611 /*
1612 if (!myOldNewMap.IsBound(aFace))
1613 {
1614 TopTools_ListOfShape EmptyList;
1615 myOldNewMap.Bind(aFace, EmptyList);
1616 }
1617 myOldNewMap(aFace).Clear();
1618 myOldNewMap(aFace).Append(aResult);
1619 */
1620 /////////////
2277323d 1621
56091b56 1622 ShapeFix_Face sff (aResult);
1623 //Intializing by tolerances
1624 sff.SetPrecision(Precision::Confusion());
1625 sff.SetMinTolerance(tol);
1626 sff.SetMaxTolerance(1.);
1627 //Setting modes
1628 sff.FixOrientationMode() = 0;
1629 //sff.FixWireMode() = 0;
1630 //sff.SetContext(aContext);
1631 sff.SetContext(myContext);
1632 // Applying the fixes
1633 sff.Perform();
1634 if(sff.Status(ShapeExtend_FAIL))
1635 hasFailed = Standard_True;
2277323d 1636
56091b56 1637 // breaking down to several faces
1638 //TopoDS_Shape theResult = aContext->Apply(aResult);
1639 TopoDS_Shape theResult = myContext->Apply(aResult);
1640 for (TopExp_Explorer aFaceExp (theResult,TopAbs_FACE); aFaceExp.More(); aFaceExp.Next()) {
1641 TopoDS_Face aCurrent = TopoDS::Face(aFaceExp.Current().Oriented(TopAbs_FORWARD));
1642 Handle(TColGeom_HArray2OfSurface) grid = new TColGeom_HArray2OfSurface ( 1, 1, 1, 1 );
1643 grid->SetValue ( 1, 1, aBaseSurface );
1644 Handle(ShapeExtend_CompositeSurface) G = new ShapeExtend_CompositeSurface ( grid );
1645 ShapeFix_ComposeShell CompShell;
1646 CompShell.Init ( G, aBaseLocation, aCurrent, ::Precision::Confusion() );//myPrecision
1647 //CompShell.SetContext( aContext );
1648 CompShell.SetContext( myContext );
1649
1650 TopTools_SequenceOfShape parts, anIntWires;
1651 ShapeFix_SequenceOfWireSegment wires;
1652 for(TopExp_Explorer W_Exp(aCurrent,TopAbs_WIRE);W_Exp.More();W_Exp.Next()) {
1653 const TopoDS_Wire& aWire = TopoDS::Wire(W_Exp.Current());
1654 // check if the wire is ordinary (contains non-internal edges)
1655 Standard_Boolean isInternal = Standard_True;
1656 for (TopoDS_Iterator it(aWire); it.More() && isInternal; it.Next())
1657 isInternal = (it.Value().Orientation() == TopAbs_INTERNAL);
1658 if (isInternal)
1659 {
1660 // place internal wire separately
1661 anIntWires.Append(aWire);
1662 }
1663 else
2277323d 1664 {
56091b56 1665 Handle(ShapeExtend_WireData) sbwd =
1666 new ShapeExtend_WireData (aWire);
1667 ShapeFix_WireSegment seg ( sbwd, TopAbs_REVERSED );
1668 wires.Append(seg);
2277323d 1669 }
2277323d 1670 }
1671
56091b56 1672 CompShell.DispatchWires ( parts,wires );
1673 for (Standard_Integer j=1; j <= parts.Length(); j++ ) {
1674 ShapeFix_Face aFixOrient(TopoDS::Face(parts(j)));
1675 //aFixOrient.SetContext(aContext);
1676 aFixOrient.SetContext(myContext);
1677 aFixOrient.FixOrientation();
1678 // put internal wires to faces
1679 putIntWires(parts(j), anIntWires);
f7d70540 1680 }
2277323d 1681
56091b56 1682 TopoDS_Shape CompRes;
1683 if ( parts.Length() !=1 ) {
1684 TopoDS_Shell S;
1685 B.MakeShell ( S );
1686 for ( i=1; i <= parts.Length(); i++ )
1687 B.Add ( S, parts(i) );
1688 S.Closed (BRep_Tool::IsClosed (S));
1689 CompRes = S;
1690 }
1691 else CompRes = parts(1);
1692
1693 //aContext->Replace(aCurrent,CompRes);
1694 myContext->Replace(aCurrent,CompRes);
1695 //for history
1696 /*
1697 if (!myOldNewMap.IsBound(aCurrent))
1698 {
2277323d 1699 TopTools_ListOfShape EmptyList;
56091b56 1700 myOldNewMap.Bind(aCurrent, EmptyList);
1701 }
1702 myOldNewMap(aCurrent).Clear();
1703 myOldNewMap(aCurrent).Append(CompRes);
1704 */
1705 /////////////
2277323d 1706 }
56091b56 1707
1708 // remove the remaining faces
1709 for(i = 2; i <= faces.Length(); i++)
1710 {
1711 myOldShapes.Bind(faces(i), theResult);
1712 myContext->Remove(faces(i));
1713 }
1714 }
1715 } // end processing each face
1716
1717 //TopoDS_Shape aResult = Shape;
1718 if (NbModif > 0 && !hasFailed) {
1719 //TopoDS_Shape aResult = aContext->Apply(aShell);
1720 TopoDS_Shape aResult = myContext->Apply(theInpShape);
1721
1722 ShapeFix_Edge sfe;
1723 if (!myContext.IsNull()) sfe.SetContext(myContext);
1724 for (exp.Init(aResult,TopAbs_EDGE); exp.More(); exp.Next()) {
1725 TopoDS_Edge E = TopoDS::Edge(exp.Current());
1726 sfe.FixVertexTolerance (E);
1727 // ptv add fix same parameter
1728 sfe.FixSameParameter(E, Precision::Confusion());
2277323d 1729 }
56091b56 1730
1731 myContext->Replace(theInpShape, aResult);
1732 //for history
1733 /*
1734 if (!myOldNewMap.IsBound(aShell))
2277323d 1735 {
56091b56 1736 TopTools_ListOfShape EmptyList;
1737 myOldNewMap.Bind(aShell, EmptyList);
1738 }
1739 myOldNewMap(aShell).Clear();
1740 myOldNewMap(aShell).Append(aResult);
1741 */
1742 /////////////
1743 }
1744 //else
1745 {
1746 for (exp.Init(theInpShape, TopAbs_FACE); exp.More(); exp.Next()) {
1747 TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
1748 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
1749 sfw->SetContext(myContext);
1750 sfw->SetPrecision(Precision::Confusion());
1751 sfw->SetMinTolerance(Precision::Confusion());
1752 sfw->SetMaxTolerance(1.);
1753 sfw->SetFace(aFace);
1754 for (TopoDS_Iterator iter (aFace,Standard_False); iter.More(); iter.Next()) {
1755 TopoDS_Wire wire = TopoDS::Wire(iter.Value());
1756 sfw->Load(wire);
1757 sfw->FixReorder();
1758 sfw->FixShifted();
2277323d 1759 }
1760 }
56091b56 1761 }
2277323d 1762
2277323d 1763}
1764
1765//=======================================================================
1766//function : UnifyEdges
1767//purpose :
1768//=======================================================================
2277323d 1769void ShapeUpgrade_UnifySameDomain::UnifyEdges()
1770{
1771 Standard_Real Tol = Precision::Confusion();
1772
1773 //Handle(ShapeBuild_ReShape) myContext = new ShapeBuild_ReShape;
1774 Standard_Real myTolerance = Precision::Confusion();
1775 TopoDS_Shape aResult = myContext->Apply(myShape);
1776
ce41efde 1777 TopTools_IndexedMapOfShape ChangedFaces;
2277323d 1778
ce41efde 1779 // creating map of edge faces
1780 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
1781 TopExp::MapShapesAndAncestors(myShape, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
1782
1783 // creating map of vertex edges
1784 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgesVertex;
1785 TopExp::MapShapesAndAncestors(myShape, TopAbs_VERTEX, TopAbs_EDGE, aMapEdgesVertex);
1786
1787 //Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
1788 TopoDS_Shape aRes = myShape;
1789 //aRes = aContext->Apply(aSolid);
1790 aRes = myContext->Apply(myShape);
f7d70540 1791
1792 TopTools_MapOfShape SharedVert;
1793
1794
1795 TopTools_IndexedMapOfShape anOldEdges;
1796 TopExp::MapShapes(myInitShape, TopAbs_EDGE, anOldEdges);
1797
1798 TopTools_DataMapOfShapeShape NewEdges2OldEdges;
1799 for (int i = 1; i <= anOldEdges.Extent(); i++)
1800 {
1801 TopoDS_Shape NewEdge = myContext->Apply(anOldEdges(i));
1802 if (!NewEdge.IsNull())
1803 NewEdges2OldEdges.Bind(NewEdge, anOldEdges(i));
1804 }
ce41efde 1805
ce41efde 1806 TopExp_Explorer exp;
f7d70540 1807 // processing separate wires
1808 for (exp.Init(aRes, TopAbs_WIRE, TopAbs_FACE); exp.More(); exp.Next())
1809 {
1810 TopTools_SequenceOfShape SeqEdges;
1811 TopExp_Explorer expE(exp.Current(), TopAbs_EDGE);
1812 for (; expE.More(); expE.Next())
1813 SeqEdges.Append(expE.Current());
1814 SharedVert.Clear();
1815 CheckSharedVertices(SeqEdges, aMapEdgesVertex, SharedVert);
1816 MergeSeq(SeqEdges, Tol, myConcatBSplines, myContext, myOldShapes, SharedVert, NewEdges2OldEdges);
1817 }
1818
26ca3bd5 1819 TopTools_DataMapOfShapeShape oldFaces2NewFaces;
1820 for (exp.Init(myShape, TopAbs_FACE); exp.More(); exp.Next())
1821 {
1822 const TopoDS_Face& f = TopoDS::Face(exp.Current());
1823 TopoDS_Face NewF = TopoDS::Face(myContext->Apply(f));
1824 if (!NewF.IsNull())
1825 oldFaces2NewFaces.Bind(f, NewF);
1826 }
1827
f7d70540 1828 // processing each face
ce41efde 1829 for (exp.Init(aRes, TopAbs_FACE); exp.More(); exp.Next()) {
1830 //TopoDS_Face aFace = TopoDS::Face(aContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
1831 TopoDS_Face aFace = TopoDS::Face(myContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
1832 TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
f7d70540 1833 TopTools_SequenceOfShape aNonSharedEdges;
ce41efde 1834 for (TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
1835 TopoDS_Edge edge = TopoDS::Edge(expe.Current());
1836 if (!aMapEdgeFaces.Contains(edge)) continue;
1837 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
1838 TopTools_ListIteratorOfListOfShape anIter(aList);
f7d70540 1839 Standard_Integer NbFacesPerEdge = aList.Extent();
ce41efde 1840 for ( ; anIter.More(); anIter.Next()) {
1841 TopoDS_Face face = TopoDS::Face(anIter.Value());
26ca3bd5 1842 TopoDS_Face face1 = TopoDS::Face(oldFaces2NewFaces(anIter.Value()));
f7d70540 1843 if (face1.IsSame(aFace) && NbFacesPerEdge != 1)
1844 continue;
1845 if (NbFacesPerEdge == 1)
1846 //store non-shared edges separately
1847 aNonSharedEdges.Append(edge);
1848 else
1849 {
1850 if (aMapFacesEdges.Contains(face))
1851 aMapFacesEdges.ChangeFromKey(face).Append(edge);
1852 else
1853 {
1854 TopTools_ListOfShape ListEdges;
1855 ListEdges.Append(edge);
1856 aMapFacesEdges.Add(face,ListEdges);
1857 }
2277323d 1858 }
1859 }
ce41efde 1860 }
f7d70540 1861
1862 for (Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++)
1863 {
ce41efde 1864 const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
1865 TopTools_SequenceOfShape SeqEdges;
ce41efde 1866 TopTools_ListIteratorOfListOfShape anIter(ListEdges);
ce41efde 1867 for ( ; anIter.More(); anIter.Next())
1868 SeqEdges.Append(anIter.Value());
1869 if (SeqEdges.Length()==1)
1870 continue;
2277323d 1871
f7d70540 1872 SharedVert.Clear();
1873 CheckSharedVertices(SeqEdges, aMapEdgesVertex, SharedVert);
1874 //if (!SharedVert.IsEmpty())
1875 // continue;
1876 if ( MergeSeq(SeqEdges,Tol,myConcatBSplines, myContext, myOldShapes, SharedVert, NewEdges2OldEdges) )
2277323d 1877 {
2277323d 1878 //for history
1879 /*
ce41efde 1880 for (j = 1; j <= SeqEdges.Length(); j++)
2277323d 1881 {
ce41efde 1882 if (!myOldNewMap.IsBound(SeqEdges(j)))
1883 {
1884 TopTools_ListOfShape EmptyList;
1885 myOldNewMap.Bind(SeqEdges(j), EmptyList);
1886 }
1887 myOldNewMap(SeqEdges(j)).Clear();
1888 myOldNewMap(SeqEdges(j)).Append(E);
2277323d 1889 }
2277323d 1890 */
1891 /////////////
ce41efde 1892
1893 TopoDS_Face tmpF = TopoDS::Face(exp.Current());
1894 if ( !ChangedFaces.Contains(tmpF) )
1895 ChangedFaces.Add(tmpF);
1896 tmpF = TopoDS::Face(aMapFacesEdges.FindKey(i));
1897 if ( !ChangedFaces.Contains(tmpF) )
1898 ChangedFaces.Add(tmpF);
2277323d 1899 }
ce41efde 1900 }
f7d70540 1901
1902 if ( aNonSharedEdges.Length() > 1 )
1903 {
1904 SharedVert.Clear();
1905 CheckSharedVertices(aNonSharedEdges, aMapEdgesVertex, SharedVert);
1906 if ( MergeSeq(aNonSharedEdges, Tol, myConcatBSplines, myContext, myOldShapes, SharedVert, NewEdges2OldEdges) )
1907 {
1908 TopoDS_Face tmpF = TopoDS::Face(exp.Current());
1909 if ( !ChangedFaces.Contains(tmpF) )
1910 ChangedFaces.Add(tmpF);
1911 }
1912 }
ce41efde 1913
1914 } // end processing each face
1915
1916 // fix changed faces and replace them in the local context
1917 for (Standard_Integer i=1; i<=ChangedFaces.Extent(); i++) {
1918 //TopoDS_Face aFace = TopoDS::Face(aContext->Apply(ChangedFaces.FindKey(i)));
1919 TopoDS_Face aFace = TopoDS::Face(myContext->Apply(ChangedFaces.FindKey(i)));
f7d70540 1920 if (aFace.IsNull())
1921 continue;
ce41efde 1922 Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFace);
1923 sff->SetContext(myContext);
1924 sff->SetPrecision(myTolerance);
1925 sff->SetMinTolerance(myTolerance);
1926 sff->SetMaxTolerance(Max(1.,myTolerance*1000.));
1927 sff->Perform();
1928 TopoDS_Shape aNewFace = sff->Face();
1929 //aContext->Replace(aFace,aNewFace);
1930 myContext->Replace(aFace,aNewFace);
1931 //for history
1932 /*
1933 if (!myOldNewMap.IsBound(aFace))
1934 {
1935 TopTools_ListOfShape EmptyList;
1936 myOldNewMap.Bind(aFace, EmptyList);
1937 }
1938 myOldNewMap(aFace).Clear();
1939 myOldNewMap(aFace).Append(aNewFace);
1940 */
1941 /////////////
1942 }
1943
1944 if (ChangedFaces.Extent() > 0) {
1945 // fix changed shell and replace it in the local context
1946 //TopoDS_Shape aRes1 = aContext->Apply(aRes);
1947 TopoDS_Shape aRes1 = myContext->Apply(aRes);
1948 TopExp_Explorer expsh;
1949 for (expsh.Init(aRes1, TopAbs_SHELL); expsh.More(); expsh.Next()) {
1950 TopoDS_Shell aShell = TopoDS::Shell(expsh.Current());
1951 Handle(ShapeFix_Shell) sfsh = new ShapeFix_Shell;
1952 sfsh->FixFaceOrientation(aShell);
1953 TopoDS_Shape aNewShell = sfsh->Shell();
1954 //aContext->Replace(aShell,aNewShell);
1955 myContext->Replace(aShell,aNewShell);
2277323d 1956 //for history
1957 /*
ce41efde 1958 if (!myOldNewMap.IsBound(aShell))
2277323d 1959 {
1960 TopTools_ListOfShape EmptyList;
ce41efde 1961 myOldNewMap.Bind(aShell, EmptyList);
2277323d 1962 }
ce41efde 1963 myOldNewMap(aShell).Clear();
1964 myOldNewMap(aShell).Append(aNewShell);
2277323d 1965 */
1966 /////////////
1967 }
ce41efde 1968 //TopoDS_Shape aRes2 = aContext->Apply(aRes1);
1969 TopoDS_Shape aRes2 = myContext->Apply(aRes1);
1970 myContext->Replace(myShape,aRes2);
1971 //for history
1972 /*
1973 if (!myOldNewMap.IsBound(aSolid))
1974 {
1975 TopTools_ListOfShape EmptyList;
1976 myOldNewMap.Bind(aSolid, EmptyList);
1977 }
1978 myOldNewMap(aSolid).Clear();
1979 myOldNewMap(aSolid).Append(aRes2);
1980 */
1981 /////////////
1982 }
2277323d 1983
1984 myShape = myContext->Apply(myShape);
1985}
1986
1987//=======================================================================
1988//function : UnifyFacesAndEdges
1989//purpose :
1990//=======================================================================
1991
1992void ShapeUpgrade_UnifySameDomain::UnifyFacesAndEdges()
1993{
1994 UnifyFaces();
1995
1996 /*
1997 ShapeUpgrade_RemoveLocations RemLoc;
1998 RemLoc.Remove(myShape);
1999 myShape = RemLoc.GetResult();
2000 */
2001
2002 UnifyEdges();
2003}
2004
2005//=======================================================================
2006//function : Build
2007//purpose : builds the resulting shape
2008//======================================================================
2009void ShapeUpgrade_UnifySameDomain::Build()
2010{
2011 if (myUnifyFaces && myUnifyEdges)
2012 UnifyFacesAndEdges();
2013
2014 else if (myUnifyEdges)
2015 UnifyEdges();
2016 else if (myUnifyFaces)
2017 UnifyFaces();
2018
2019 //Done();
2020}
2021
2022//=======================================================================
2023//function : Shape
2024//purpose : give the resulting shape
2025//=======================================================================
2026const TopoDS_Shape& ShapeUpgrade_UnifySameDomain::Shape() const
2027{
2028 return myShape;
2029}
2030
2031//=======================================================================
2032//function : Generated
2033//purpose : returns the new shape from the old one
2034//=======================================================================
2035TopoDS_Shape ShapeUpgrade_UnifySameDomain::Generated(const TopoDS_Shape& aShape) const
2036{
2037 TopoDS_Shape aNewShape = myContext->Apply(aShape);
f7d70540 2038
2277323d 2039 if (aNewShape.IsNull())
f7d70540 2040 aNewShape = myContext->Apply(myOldShapes(aShape));
2277323d 2041
2042 return aNewShape;
2043}