0029234: BRepOffsetAPI_NormalProjection produces INTERNAL edges and vertices
[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_Tool.hxx>
19#include <BRepLib.hxx>
20#include <BRepLib_MakeEdge.hxx>
21#include <BRepTopAdaptor_TopolTool.hxx>
22#include <GC_MakeCircle.hxx>
23#include <Geom2d_Line.hxx>
2277323d 24#include <Geom2d_TrimmedCurve.hxx>
2277323d 25#include <Geom2dConvert.hxx>
2277323d 26#include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
2277323d 27#include <Geom_BezierCurve.hxx>
42cf5bc1 28#include <Geom_BSplineCurve.hxx>
29#include <Geom_Circle.hxx>
30#include <Geom_CylindricalSurface.hxx>
31#include <Geom_ElementarySurface.hxx>
32#include <Geom_Line.hxx>
33#include <Geom_OffsetSurface.hxx>
f16a6cc5 34#include <Geom_Plane.hxx>
42cf5bc1 35#include <Geom_RectangularTrimmedSurface.hxx>
36#include <Geom_Surface.hxx>
37#include <Geom_SurfaceOfLinearExtrusion.hxx>
38#include <Geom_SurfaceOfRevolution.hxx>
39#include <Geom_SweptSurface.hxx>
40#include <Geom_TrimmedCurve.hxx>
41#include <GeomAdaptor_HSurface.hxx>
42#include <GeomConvert.hxx>
43#include <GeomConvert_CompCurveToBSplineCurve.hxx>
44#include <GeomLib_IsPlanarSurface.hxx>
45#include <gp_Cylinder.hxx>
46#include <gp_Dir.hxx>
47#include <gp_Lin.hxx>
48#include <IntPatch_ImpImpIntersection.hxx>
49#include <ShapeAnalysis_Edge.hxx>
2277323d 50#include <ShapeAnalysis_WireOrder.hxx>
51#include <ShapeBuild_Edge.hxx>
42cf5bc1 52#include <ShapeBuild_ReShape.hxx>
2277323d 53#include <ShapeFix_Edge.hxx>
42cf5bc1 54#include <ShapeFix_Face.hxx>
2277323d 55#include <ShapeFix_Shell.hxx>
42cf5bc1 56#include <ShapeFix_Wire.hxx>
42cf5bc1 57#include <ShapeUpgrade_UnifySameDomain.hxx>
58#include <Standard_Type.hxx>
59#include <TColGeom2d_Array1OfBSplineCurve.hxx>
60#include <TColGeom2d_HArray1OfBSplineCurve.hxx>
61#include <TColGeom2d_SequenceOfBoundedCurve.hxx>
62#include <TColGeom_Array1OfBSplineCurve.hxx>
63#include <TColGeom_HArray1OfBSplineCurve.hxx>
42cf5bc1 64#include <TColGeom_SequenceOfSurface.hxx>
65#include <TColStd_Array1OfReal.hxx>
66#include <TColStd_MapOfInteger.hxx>
67#include <TopExp.hxx>
68#include <TopExp_Explorer.hxx>
69#include <TopoDS.hxx>
70#include <TopoDS_Edge.hxx>
71#include <TopoDS_Face.hxx>
72#include <TopoDS_Shape.hxx>
73#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
74#include <TopTools_IndexedMapOfShape.hxx>
75#include <TopTools_ListIteratorOfListOfShape.hxx>
b4d4dbe8 76#include <TopTools_MapOfShape.hxx>
42cf5bc1 77#include <TopTools_SequenceOfShape.hxx>
f7d70540 78#include <gp_Circ.hxx>
79#include <BRepAdaptor_Curve.hxx>
f0144633 80#include <BRepAdaptor_Curve2d.hxx>
81#include <gp_Vec2d.hxx>
f7d70540 82
25e59720 83IMPLEMENT_STANDARD_RTTIEXT(ShapeUpgrade_UnifySameDomain,Standard_Transient)
92efcf78 84
f7d70540 85struct SubSequenceOfEdges
86{
87 TopTools_SequenceOfShape SeqsEdges;
88 TopoDS_Edge UnionEdges;
89};
90
f0144633 91static Standard_Boolean IsLikeSeam(const TopoDS_Edge& anEdge,
92 const TopoDS_Face& aFace,
93 const Handle(Geom_Surface)& aBaseSurface)
94{
95 if (!aBaseSurface->IsUPeriodic() && !aBaseSurface->IsVPeriodic())
96 return Standard_False;
97
98 BRepAdaptor_Curve2d BAcurve2d(anEdge, aFace);
99 gp_Pnt2d FirstPoint, LastPoint;
100 gp_Vec2d FirstDir, LastDir;
101 BAcurve2d.D1(BAcurve2d.FirstParameter(), FirstPoint, FirstDir);
102 BAcurve2d.D1(BAcurve2d.LastParameter(), LastPoint, LastDir);
103 Standard_Real Length = FirstDir.Magnitude();
104 if (Length <= gp::Resolution())
105 return Standard_False;
106 else
107 FirstDir /= Length;
108 Length = LastDir.Magnitude();
109 if (Length <= gp::Resolution())
110 return Standard_False;
111 else
112 LastDir /= Length;
113
114 Standard_Real Tol = 1.e-7;
115 if (aBaseSurface->IsUPeriodic() &&
116 (Abs(FirstDir.X()) < Tol) &&
117 (Abs(LastDir.X()) < Tol))
118 return Standard_True;
119
120 if (aBaseSurface->IsVPeriodic() &&
121 (Abs(FirstDir.Y()) < Tol) &&
122 (Abs(LastDir.Y()) < Tol))
123 return Standard_True;
124
125 return Standard_False;
126}
2277323d 127
128//=======================================================================
129//function : AddOrdinaryEdges
130//purpose : auxilary
131//=======================================================================
132// adds edges from the shape to the sequence
133// seams and equal edges are dropped
134// Returns true if one of original edges dropped
135static Standard_Boolean AddOrdinaryEdges(TopTools_SequenceOfShape& edges,
136 const TopoDS_Shape aShape,
137 Standard_Integer& anIndex)
138{
139 //map of edges
140 TopTools_IndexedMapOfShape aNewEdges;
141 //add edges without seams
142 for(TopExp_Explorer exp(aShape,TopAbs_EDGE); exp.More(); exp.Next()) {
143 TopoDS_Shape edge = exp.Current();
144 if(aNewEdges.Contains(edge))
3f5aa017 145 aNewEdges.RemoveKey(edge);
2277323d 146 else
147 aNewEdges.Add(edge);
148 }
149
150 Standard_Boolean isDropped = Standard_False;
151 //merge edges and drop seams
152 Standard_Integer i;
153 for (i = 1; i <= edges.Length(); i++) {
154 TopoDS_Shape current = edges(i);
155 if(aNewEdges.Contains(current)) {
156
3f5aa017 157 aNewEdges.RemoveKey(current);
2277323d 158 edges.Remove(i);
159 i--;
160
161 if(!isDropped) {
162 isDropped = Standard_True;
163 anIndex = i;
164 }
165 }
166 }
167
9ed6494b 168 //add edges to the sequence
2277323d 169 for (i = 1; i <= aNewEdges.Extent(); i++)
170 edges.Append(aNewEdges(i));
171
172 return isDropped;
173}
174
175//=======================================================================
176//function : getCylinder
177//purpose : auxilary
178//=======================================================================
179static Standard_Boolean getCylinder(Handle(Geom_Surface)& theInSurface,
180 gp_Cylinder& theOutCylinder)
181{
182 Standard_Boolean isCylinder = Standard_False;
183
184 if (theInSurface->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
185 Handle(Geom_CylindricalSurface) aGC = Handle(Geom_CylindricalSurface)::DownCast(theInSurface);
186
187 theOutCylinder = aGC->Cylinder();
188 isCylinder = Standard_True;
189 }
190 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution))) {
191 Handle(Geom_SurfaceOfRevolution) aRS =
192 Handle(Geom_SurfaceOfRevolution)::DownCast(theInSurface);
193 Handle(Geom_Curve) aBasis = aRS->BasisCurve();
194 if (aBasis->IsKind(STANDARD_TYPE(Geom_Line))) {
195 Handle(Geom_Line) aBasisLine = Handle(Geom_Line)::DownCast(aBasis);
196 gp_Dir aDir = aRS->Direction();
197 gp_Dir aBasisDir = aBasisLine->Position().Direction();
198 if (aBasisDir.IsParallel(aDir, Precision::Angular())) {
199 // basis line is parallel to the revolution axis: it is a cylinder
200 gp_Pnt aLoc = aRS->Location();
201 Standard_Real aR = aBasisLine->Lin().Distance(aLoc);
202 gp_Ax3 aCylAx (aLoc, aDir);
203
204 theOutCylinder = gp_Cylinder(aCylAx, aR);
205 isCylinder = Standard_True;
206 }
207 }
208 }
209 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion))) {
210 Handle(Geom_SurfaceOfLinearExtrusion) aLES =
211 Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(theInSurface);
212 Handle(Geom_Curve) aBasis = aLES->BasisCurve();
213 if (aBasis->IsKind(STANDARD_TYPE(Geom_Circle))) {
214 Handle(Geom_Circle) aBasisCircle = Handle(Geom_Circle)::DownCast(aBasis);
215 gp_Dir aDir = aLES->Direction();
216 gp_Dir aBasisDir = aBasisCircle->Position().Direction();
217 if (aBasisDir.IsParallel(aDir, Precision::Angular())) {
218 // basis circle is normal to the extrusion axis: it is a cylinder
219 gp_Pnt aLoc = aBasisCircle->Location();
220 Standard_Real aR = aBasisCircle->Radius();
221 gp_Ax3 aCylAx (aLoc, aDir);
222
223 theOutCylinder = gp_Cylinder(aCylAx, aR);
224 isCylinder = Standard_True;
225 }
226 }
227 }
228 else {
229 }
230
231 return isCylinder;
232}
233
234//=======================================================================
235//function : ClearRts
236//purpose : auxilary
237//=======================================================================
238static Handle(Geom_Surface) ClearRts(const Handle(Geom_Surface)& aSurface)
239{
240 if(aSurface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
241 Handle(Geom_RectangularTrimmedSurface) rts =
242 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
243 return rts->BasisSurface();
244 }
245 return aSurface;
246}
247
10ce3246 248//=======================================================================
249//function : GetNormalToSurface
250//purpose : Gets the normal to surface by the given parameter on edge.
251// Returns True if normal was computed.
252//=======================================================================
253static Standard_Boolean GetNormalToSurface(const TopoDS_Face& theFace,
254 const TopoDS_Edge& theEdge,
255 const Standard_Real theP,
256 gp_Dir& theNormal)
257{
258 Standard_Real f, l;
259 // get 2d curve to get point in 2d
260 const Handle(Geom2d_Curve)& aC2d = BRep_Tool::CurveOnSurface(theEdge, theFace, f, l);
261 if (aC2d.IsNull()) {
262 return Standard_False;
263 }
264 //
265 // 2d point
266 gp_Pnt2d aP2d;
267 aC2d->D0(theP, aP2d);
268 //
269 // get D1
270 gp_Vec aDU, aDV;
271 gp_Pnt aP3d;
272 TopLoc_Location aLoc;
273 const Handle(Geom_Surface)& aS = BRep_Tool::Surface(theFace, aLoc);
274 aS->D1(aP2d.X(), aP2d.Y(), aP3d, aDU, aDV);
275 //
276 // compute normal
277 gp_Vec aVNormal = aDU.Crossed(aDV);
278 if (aVNormal.Magnitude() < Precision::Confusion()) {
279 return Standard_False;
280 }
281 //
282 if (theFace.Orientation() == TopAbs_REVERSED) {
283 aVNormal.Reverse();
284 }
285 //
286 aVNormal.Transform(aLoc.Transformation());
287 theNormal = gp_Dir(aVNormal);
288 return Standard_True;
289}
290
2277323d 291//=======================================================================
292//function : IsSameDomain
293//purpose :
294//=======================================================================
295static Standard_Boolean IsSameDomain(const TopoDS_Face& aFace,
2ba9eb30 296 const TopoDS_Face& aCheckedFace,
297 const Standard_Real theLinTol,
298 const Standard_Real theAngTol)
2277323d 299{
300 //checking the same handles
301 TopLoc_Location L1, L2;
302 Handle(Geom_Surface) S1, S2;
303
304 S1 = BRep_Tool::Surface(aFace,L1);
305 S2 = BRep_Tool::Surface(aCheckedFace,L2);
306
307 if (S1 == S2 && L1 == L2)
308 return Standard_True;
309
2277323d 310 S1 = BRep_Tool::Surface(aFace);
311 S2 = BRep_Tool::Surface(aCheckedFace);
312
313 S1 = ClearRts(S1);
314 S2 = ClearRts(S2);
315
316 //Handle(Geom_OffsetSurface) aGOFS1, aGOFS2;
317 //aGOFS1 = Handle(Geom_OffsetSurface)::DownCast(S1);
318 //aGOFS2 = Handle(Geom_OffsetSurface)::DownCast(S2);
319 //if (!aGOFS1.IsNull()) S1 = aGOFS1->BasisSurface();
320 //if (!aGOFS2.IsNull()) S2 = aGOFS2->BasisSurface();
321
2ba9eb30 322 // case of two planar surfaces:
323 // all kinds of surfaces checked, including b-spline and bezier
324 GeomLib_IsPlanarSurface aPlanarityChecker1(S1, theLinTol);
325 if (aPlanarityChecker1.IsPlanar()) {
326 GeomLib_IsPlanarSurface aPlanarityChecker2(S2, theLinTol);
327 if (aPlanarityChecker2.IsPlanar()) {
328 gp_Pln aPln1 = aPlanarityChecker1.Plan();
329 gp_Pln aPln2 = aPlanarityChecker2.Plan();
330
331 if (aPln1.Position().Direction().IsParallel(aPln2.Position().Direction(), theAngTol) &&
332 aPln1.Distance(aPln2) < theLinTol) {
333 return Standard_True;
334 }
335 }
336 }
337
2277323d 338 // case of two elementary surfaces: use OCCT tool
339 // elementary surfaces: ConicalSurface, CylindricalSurface,
340 // Plane, SphericalSurface and ToroidalSurface
341 if (S1->IsKind(STANDARD_TYPE(Geom_ElementarySurface)) &&
342 S2->IsKind(STANDARD_TYPE(Geom_ElementarySurface)))
343 {
344 Handle(GeomAdaptor_HSurface) aGA1 = new GeomAdaptor_HSurface(S1);
345 Handle(GeomAdaptor_HSurface) aGA2 = new GeomAdaptor_HSurface(S2);
346
347 Handle(BRepTopAdaptor_TopolTool) aTT1 = new BRepTopAdaptor_TopolTool();
348 Handle(BRepTopAdaptor_TopolTool) aTT2 = new BRepTopAdaptor_TopolTool();
349
350 try {
2ba9eb30 351 IntPatch_ImpImpIntersection anIIInt(aGA1, aTT1, aGA2, aTT2, theLinTol, theLinTol);
2277323d 352 if (!anIIInt.IsDone() || anIIInt.IsEmpty())
353 return Standard_False;
354
355 return anIIInt.TangentFaces();
356 }
357 catch (Standard_Failure) {
358 return Standard_False;
359 }
360 }
361
2277323d 362 // case of two cylindrical surfaces, at least one of which is a swept surface
363 // swept surfaces: SurfaceOfLinearExtrusion, SurfaceOfRevolution
364 if ((S1->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
365 S1->IsKind(STANDARD_TYPE(Geom_SweptSurface))) &&
366 (S2->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
367 S2->IsKind(STANDARD_TYPE(Geom_SweptSurface))))
368 {
369 gp_Cylinder aCyl1, aCyl2;
370 if (getCylinder(S1, aCyl1) && getCylinder(S2, aCyl2)) {
2ba9eb30 371 if (fabs(aCyl1.Radius() - aCyl2.Radius()) < theLinTol) {
2277323d 372 gp_Dir aDir1 = aCyl1.Position().Direction();
373 gp_Dir aDir2 = aCyl2.Position().Direction();
374 if (aDir1.IsParallel(aDir2, Precision::Angular())) {
375 gp_Pnt aLoc1 = aCyl1.Location();
376 gp_Pnt aLoc2 = aCyl2.Location();
377 gp_Vec aVec12 (aLoc1, aLoc2);
2ba9eb30 378 if (aVec12.SquareMagnitude() < theLinTol*theLinTol ||
2277323d 379 aVec12.IsParallel(aDir1, Precision::Angular())) {
380 return Standard_True;
381 }
382 }
383 }
384 }
385 }
386
387 return Standard_False;
388}
389
632175c3 390//=======================================================================
391//function : UpdateMapOfShapes
392//purpose :
393//=======================================================================
394static void UpdateMapOfShapes(TopTools_MapOfShape& theMapOfShapes,
395 Handle(ShapeBuild_ReShape)& theContext)
396{
397 for (TopTools_MapIteratorOfMapOfShape it(theMapOfShapes); it.More(); it.Next()) {
398 const TopoDS_Shape& aShape = it.Value();
399 TopoDS_Shape aContextShape = theContext->Apply(aShape);
400 if (!aContextShape.IsSame(aShape))
401 theMapOfShapes.Add(aContextShape);
402 }
403}
404
2277323d 405//=======================================================================
406//function : GlueEdgesWithPCurves
407//purpose : Glues the pcurves of the sequence of edges
408// and glues their 3d curves
409//=======================================================================
410static TopoDS_Edge GlueEdgesWithPCurves(const TopTools_SequenceOfShape& aChain,
411 const TopoDS_Vertex& FirstVertex,
412 const TopoDS_Vertex& LastVertex)
413{
414 Standard_Integer i, j;
415
416 TopoDS_Edge FirstEdge = TopoDS::Edge(aChain(1));
2277323d 417 TColGeom_SequenceOfSurface SurfSeq;
fbfdf61a 418 NCollection_Sequence<TopLoc_Location> LocSeq;
2277323d 419
fbfdf61a 420 for (int aCurveIndex = 0;; aCurveIndex++)
2277323d 421 {
fbfdf61a 422 Handle(Geom2d_Curve) aCurve;
423 Handle(Geom_Surface) aSurface;
424 TopLoc_Location aLocation;
425 Standard_Real aFirst, aLast;
426 BRep_Tool::CurveOnSurface (FirstEdge, aCurve, aSurface, aLocation, aFirst, aLast, aCurveIndex);
427 if (aCurve.IsNull())
428 break;
429
430 SurfSeq.Append(aSurface);
431 LocSeq.Append(aLocation);
2277323d 432 }
433
434 Standard_Real fpar, lpar;
435 BRep_Tool::Range(FirstEdge, fpar, lpar);
436 TopoDS_Edge PrevEdge = FirstEdge;
437 TopoDS_Vertex CV;
438 Standard_Real MaxTol = 0.;
439
440 TopoDS_Edge ResEdge;
441 BRep_Builder BB;
442
443 Standard_Integer nb_curve = aChain.Length(); //number of curves
444 TColGeom_Array1OfBSplineCurve tab_c3d(0,nb_curve-1); //array of the curves
445 TColStd_Array1OfReal tabtolvertex(0,nb_curve-1); //(0,nb_curve-2); //array of the tolerances
446
447 TopoDS_Vertex PrevVertex = FirstVertex;
448 for (i = 1; i <= nb_curve; i++)
449 {
450 TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
451 TopoDS_Vertex VF, VL;
452 TopExp::Vertices(anEdge, VF, VL);
453 Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
454
455 Standard_Real Tol1 = BRep_Tool::Tolerance(VF);
456 Standard_Real Tol2 = BRep_Tool::Tolerance(VL);
457 if (Tol1 > MaxTol)
458 MaxTol = Tol1;
459 if (Tol2 > MaxTol)
460 MaxTol = Tol2;
461
462 if (i > 1)
463 {
464 TopExp::CommonVertex(PrevEdge, anEdge, CV);
465 Standard_Real Tol = BRep_Tool::Tolerance(CV);
466 tabtolvertex(i-2) = Tol;
467 }
468
469 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, fpar, lpar);
470 Handle(Geom_TrimmedCurve) aTrCurve = new Geom_TrimmedCurve(aCurve, fpar, lpar);
471 tab_c3d(i-1) = GeomConvert::CurveToBSplineCurve(aTrCurve);
472 GeomConvert::C0BSplineToC1BSplineCurve(tab_c3d(i-1), Precision::Confusion());
473 if (ToReverse)
474 tab_c3d(i-1)->Reverse();
475 PrevVertex = (ToReverse)? VF : VL;
476 PrevEdge = anEdge;
477 }
478 Handle(TColGeom_HArray1OfBSplineCurve) concatcurve; //array of the concatenated curves
479 Handle(TColStd_HArray1OfInteger) ArrayOfIndices; //array of the remining Vertex
480 GeomConvert::ConcatC1(tab_c3d,
481 tabtolvertex,
482 ArrayOfIndices,
483 concatcurve,
484 Standard_False,
485 Precision::Confusion()); //C1 concatenation
486
487 if (concatcurve->Length() > 1)
488 {
489 GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
490
491 for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
492 Concat.Add( concatcurve->Value(i), MaxTol, Standard_True );
493
494 concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
495 }
496 Handle(Geom_BSplineCurve) ResCurve = concatcurve->Value(concatcurve->Lower());
497
498 TColGeom2d_SequenceOfBoundedCurve ResPCurves;
2277323d 499 for (j = 1; j <= SurfSeq.Length(); j++)
500 {
501 TColGeom2d_Array1OfBSplineCurve tab_c2d(0,nb_curve-1); //array of the pcurves
502
503 PrevVertex = FirstVertex;
504 PrevEdge = FirstEdge;
2277323d 505 for (i = 1; i <= nb_curve; i++)
506 {
507 TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
508 TopoDS_Vertex VF, VL;
509 TopExp::Vertices(anEdge, VF, VL);
510 Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
511
2277323d 512 Handle(Geom2d_Curve) aPCurve =
fbfdf61a 513 BRep_Tool::CurveOnSurface(anEdge, SurfSeq(j), LocSeq(j), fpar, lpar);
514 if (aPCurve.IsNull())
515 continue;
2277323d 516 Handle(Geom2d_TrimmedCurve) aTrPCurve = new Geom2d_TrimmedCurve(aPCurve, fpar, lpar);
517 tab_c2d(i-1) = Geom2dConvert::CurveToBSplineCurve(aTrPCurve);
518 Geom2dConvert::C0BSplineToC1BSplineCurve(tab_c2d(i-1), Precision::Confusion());
519 if (ToReverse)
520 tab_c2d(i-1)->Reverse();
521 PrevVertex = (ToReverse)? VF : VL;
522 PrevEdge = anEdge;
523 }
524 Handle(TColGeom2d_HArray1OfBSplineCurve) concatc2d; //array of the concatenated curves
525 Handle(TColStd_HArray1OfInteger) ArrayOfInd2d; //array of the remining Vertex
526 Geom2dConvert::ConcatC1(tab_c2d,
527 tabtolvertex,
528 ArrayOfInd2d,
529 concatc2d,
530 Standard_False,
531 Precision::Confusion()); //C1 concatenation
532
533 if (concatc2d->Length() > 1)
534 {
535 Geom2dConvert_CompCurveToBSplineCurve Concat2d(concatc2d->Value(concatc2d->Lower()));
536
537 for (i = concatc2d->Lower()+1; i <= concatc2d->Upper(); i++)
538 Concat2d.Add( concatc2d->Value(i), MaxTol, Standard_True );
539
540 concatc2d->SetValue(concatc2d->Lower(), Concat2d.BSplineCurve());
541 }
542 Handle(Geom2d_BSplineCurve) aResPCurve = concatc2d->Value(concatc2d->Lower());
543 ResPCurves.Append(aResPCurve);
544 }
545
546 ResEdge = BRepLib_MakeEdge(ResCurve,
547 FirstVertex, LastVertex,
548 ResCurve->FirstParameter(), ResCurve->LastParameter());
549 BB.SameRange(ResEdge, Standard_False);
550 BB.SameParameter(ResEdge, Standard_False);
551 for (j = 1; j <= ResPCurves.Length(); j++)
552 {
fbfdf61a 553 BB.UpdateEdge(ResEdge, ResPCurves(j), SurfSeq(j), LocSeq(j), MaxTol);
554 BB.Range(ResEdge, SurfSeq(j), LocSeq(j), ResPCurves(j)->FirstParameter(), ResPCurves(j)->LastParameter());
2277323d 555 }
556
557 BRepLib::SameParameter(ResEdge, MaxTol, Standard_True);
558
559 return ResEdge;
560}
561
cef6867c 562//=======================================================================
563//function : MergeSubSeq
564//purpose : Merges a sequence of edges into one edge if possible
565//=======================================================================
566
632175c3 567static Standard_Boolean MergeSubSeq(const TopTools_SequenceOfShape& aChain,
568 TopoDS_Edge& OutEdge,
569 double theAngTol,
570 Standard_Boolean ConcatBSplines,
571 Standard_Boolean isSafeInputMode,
572 Handle(ShapeBuild_ReShape)& theContext)
2277323d 573{
2277323d 574 ShapeAnalysis_Edge sae;
f7d70540 575 BRep_Builder B;
2277323d 576 // union edges in chain
f7d70540 577 int j;
2277323d 578 Standard_Real fp1,lp1,fp2,lp2;
f7d70540 579 Standard_Boolean IsUnionOfLinesPossible = Standard_True;
580 Standard_Boolean IsUnionOfCirclesPossible = Standard_True;
581 Handle(Geom_Curve) c3d1, c3d2;
582 for(j=1; j<aChain.Length(); j++)
583 {
2277323d 584 TopoDS_Edge edge1 = TopoDS::Edge(aChain.Value(j));
f7d70540 585 c3d1 = BRep_Tool::Curve(edge1,fp1,lp1);
586
587 TopoDS_Edge edge2 = TopoDS::Edge(aChain.Value(j+1));
588 c3d2 = BRep_Tool::Curve(edge2,fp2,lp2);
589
590 if(c3d1.IsNull() || c3d2.IsNull())
591 return Standard_False;
b4d4dbe8 592
2277323d 593 while(c3d1->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
594 Handle(Geom_TrimmedCurve) tc =
595 Handle(Geom_TrimmedCurve)::DownCast(c3d1);
596 c3d1 = tc->BasisCurve();
597 }
2277323d 598 while(c3d2->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
599 Handle(Geom_TrimmedCurve) tc =
600 Handle(Geom_TrimmedCurve)::DownCast(c3d2);
601 c3d2 = tc->BasisCurve();
602 }
603 if( c3d1->IsKind(STANDARD_TYPE(Geom_Line)) && c3d2->IsKind(STANDARD_TYPE(Geom_Line)) ) {
2277323d 604 Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(c3d1);
605 Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(c3d2);
606 gp_Dir Dir1 = L1->Position().Direction();
607 gp_Dir Dir2 = L2->Position().Direction();
632175c3 608 if(!Dir1.IsParallel(Dir2,theAngTol))
f7d70540 609 IsUnionOfLinesPossible = Standard_False;
2277323d 610 }
f7d70540 611 else
612 IsUnionOfLinesPossible = Standard_False;
2277323d 613 if( c3d1->IsKind(STANDARD_TYPE(Geom_Circle)) && c3d2->IsKind(STANDARD_TYPE(Geom_Circle)) ) {
2277323d 614 Handle(Geom_Circle) C1 = Handle(Geom_Circle)::DownCast(c3d1);
615 Handle(Geom_Circle) C2 = Handle(Geom_Circle)::DownCast(c3d2);
616 gp_Pnt P01 = C1->Location();
617 gp_Pnt P02 = C2->Location();
b4d4dbe8 618 if (P01.Distance(P02) > Precision::Confusion())
f7d70540 619 IsUnionOfCirclesPossible = Standard_False;
620 }
621 else
622 IsUnionOfCirclesPossible = Standard_False;
623 }
624 if (IsUnionOfLinesPossible && IsUnionOfCirclesPossible)
625 return Standard_False;
626
627 //union of lines is possible
628 if (IsUnionOfLinesPossible)
629 {
632175c3 630 TopoDS_Vertex V[2];
631 V[0] = sae.FirstVertex(TopoDS::Edge(aChain.First()));
632 gp_Pnt PV1 = BRep_Tool::Pnt(V[0]);
633 V[1] = sae.LastVertex(TopoDS::Edge(aChain.Last()));
634 gp_Pnt PV2 = BRep_Tool::Pnt(V[1]);
f7d70540 635 gp_Vec Vec(PV1, PV2);
632175c3 636 if (isSafeInputMode) {
637 for (int k = 0; k < 2; k++) {
638 if (!theContext->IsRecorded(V[k])) {
639 TopoDS_Vertex Vcopy = TopoDS::Vertex(V[k].EmptyCopied());
640 theContext->Replace(V[k], Vcopy);
641 V[k] = Vcopy;
642 }
643 else
644 V[k] = TopoDS::Vertex(theContext->Apply(V[k]));
645 }
646 }
f7d70540 647 Handle(Geom_Line) L = new Geom_Line(gp_Ax1(PV1,Vec));
648 Standard_Real dist = PV1.Distance(PV2);
649 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(L,0.0,dist);
650 TopoDS_Edge E;
651 B.MakeEdge (E, tc ,Precision::Confusion());
632175c3 652 B.Add (E,V[0]); B.Add (E,V[1]);
653 B.UpdateVertex(V[0], 0., E, 0.);
654 B.UpdateVertex(V[1], dist, E, 0.);
f7d70540 655 OutEdge = E;
656 return Standard_True;
657 }
658
659 if (IsUnionOfCirclesPossible)
660 {
661 double f,l;
662 TopoDS_Edge FE = TopoDS::Edge(aChain.First());
663 Handle(Geom_Curve) c3d = BRep_Tool::Curve(FE,f,l);
664
665 while(c3d->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
666 Handle(Geom_TrimmedCurve) tc =
667 Handle(Geom_TrimmedCurve)::DownCast(c3d);
668 c3d = tc->BasisCurve();
669 }
670 Handle(Geom_Circle) Cir = Handle(Geom_Circle)::DownCast(c3d);
671
632175c3 672 TopoDS_Vertex V[2];
673 V[0] = sae.FirstVertex(FE);
674 V[1] = sae.LastVertex(TopoDS::Edge(aChain.Last()));
f7d70540 675 TopoDS_Edge E;
632175c3 676 if (V[0].IsSame(V[1])) {
f7d70540 677 // closed chain
dd2f1b75 678 BRepAdaptor_Curve adef(FE);
679 Handle(Geom_Circle) Cir1;
680 double FP, LP;
681 if ( FE.Orientation() == TopAbs_FORWARD)
682 {
683 FP = adef.FirstParameter();
684 LP = adef.LastParameter();
685 }
686 else
687 {
688 FP = adef.LastParameter();
689 LP = adef.FirstParameter();
690 }
691 if (Abs(FP) < Precision::PConfusion())
692 {
f0144633 693 B.MakeEdge (E,Cir, Precision::Confusion());
632175c3 694 B.Add(E,V[0]);
695 B.Add(E,V[1]);
f0144633 696 E.Orientation(FE.Orientation());
697 }
dd2f1b75 698 else
699 {
700 GC_MakeCircle MC1 (adef.Value(FP), adef.Value((FP + LP) * 0.5), adef.Value(LP));
701 if (MC1.IsDone())
702 Cir1 = MC1.Value();
703 else
704 return Standard_False;
705 B.MakeEdge (E, Cir1, Precision::Confusion());
632175c3 706 B.Add(E,V[0]);
707 B.Add(E,V[1]);
dd2f1b75 708 }
f7d70540 709 }
710 else {
632175c3 711 if (isSafeInputMode) {
712 for (int k = 0; k < 2; k++) {
713 if (!theContext->IsRecorded(V[k])) {
714 TopoDS_Vertex Vcopy = TopoDS::Vertex(V[k].EmptyCopied());
715 theContext->Replace(V[k], Vcopy);
716 V[k] = Vcopy;
717 }
718 else
719 V[k] = TopoDS::Vertex(theContext->Apply(V[k]));
720 }
721 }
722 gp_Pnt PV1 = BRep_Tool::Pnt(V[0]);
723 gp_Pnt PV2 = BRep_Tool::Pnt(V[1]);
dd2f1b75 724 TopoDS_Vertex VM = sae.LastVertex(FE);
725 gp_Pnt PVM = BRep_Tool::Pnt(VM);
726 GC_MakeCircle MC (PV1,PVM,PV2);
f7d70540 727 Handle(Geom_Circle) C = MC.Value();
728 gp_Pnt P0 = C->Location();
729 gp_Dir D1(gp_Vec(P0,PV1));
730 gp_Dir D2(gp_Vec(P0,PV2));
731 Standard_Real fpar = C->XAxis().Direction().Angle(D1);
732 if(fabs(fpar)>Precision::Confusion()) {
733 // check orientation
734 gp_Dir ND = C->XAxis().Direction().Crossed(D1);
735 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
736 fpar = -fpar;
2277323d 737 }
f7d70540 738 }
739 Standard_Real lpar = C->XAxis().Direction().Angle(D2);
740 if(fabs(lpar)>Precision::Confusion()) {
741 // check orientation
742 gp_Dir ND = C->XAxis().Direction().Crossed(D2);
743 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
744 lpar = -lpar;
2277323d 745 }
2277323d 746 }
f7d70540 747 if (lpar < fpar) lpar += 2*M_PI;
748 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
749 B.MakeEdge (E,tc,Precision::Confusion());
632175c3 750 B.Add(E,V[0]);
751 B.Add(E,V[1]);
752 B.UpdateVertex(V[0], fpar, E, 0.);
753 B.UpdateVertex(V[1], lpar, E, 0.);
2277323d 754 }
f7d70540 755 OutEdge = E;
756 return Standard_True;
2277323d 757 }
758 if (aChain.Length() > 1 && ConcatBSplines) {
759 // second step: union edges with various curves
760 // skl for bug 0020052 from Mantis: perform such unions
761 // only if curves are bspline or bezier
f7d70540 762
763 TopoDS_Vertex VF = sae.FirstVertex(TopoDS::Edge(aChain.First()));
764 TopoDS_Vertex VL = sae.LastVertex(TopoDS::Edge(aChain.Last()));
765 Standard_Boolean NeedUnion = Standard_True;
2277323d 766 for(j=1; j<=aChain.Length(); j++) {
767 TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
b4d4dbe8 768 TopLoc_Location Loc;
2277323d 769 Handle(Geom_Curve) c3d = BRep_Tool::Curve(edge,Loc,fp1,lp1);
770 if(c3d.IsNull()) continue;
771 while(c3d->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
772 Handle(Geom_TrimmedCurve) tc =
773 Handle(Geom_TrimmedCurve)::DownCast(c3d);
774 c3d = tc->BasisCurve();
775 }
776 if( ( c3d->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ||
777 c3d->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ) ) continue;
f7d70540 778 NeedUnion = Standard_False;
2277323d 779 break;
780 }
781 if(NeedUnion) {
0797d9d3 782#ifdef OCCT_DEBUG
2277323d 783 cout<<"can not make analitical union => make approximation"<<endl;
784#endif
785 TopoDS_Edge E = GlueEdgesWithPCurves(aChain, VF, VL);
f7d70540 786 OutEdge = E;
787 return Standard_True;
2277323d 788 }
789 else {
0797d9d3 790#ifdef OCCT_DEBUG
2277323d 791 cout<<"can not make approximation for such types of curves"<<endl;
792#endif
793 return Standard_False;
794 }
795 }
f7d70540 796 return Standard_False;
797}
798
cef6867c 799//=======================================================================
800//function : IsMergingPossible
801//purpose : Checks if merging of two edges is possible
802//=======================================================================
803
f7d70540 804static Standard_Boolean IsMergingPossible(const TopoDS_Edge& edge1, const TopoDS_Edge& edge2,
632175c3 805 double theAngTol, const TopTools_MapOfShape& AvoidEdgeVrt)
f7d70540 806{
807 TopoDS_Vertex CV = TopExp::LastVertex(edge1, Standard_True);
9ed6494b 808 if (CV.IsNull() || AvoidEdgeVrt.Contains(CV))
f7d70540 809 return Standard_False;
810
811 BRepAdaptor_Curve ade1(edge1);
812 BRepAdaptor_Curve ade2(edge2);
813
814 GeomAbs_CurveType t1 = ade1.GetType();
815 GeomAbs_CurveType t2 = ade2.GetType();
816
817 if( t1 == GeomAbs_Circle && t2 == GeomAbs_Circle)
818 {
819 if (ade1.Circle().Location().Distance(ade2.Circle().Location()) > Precision::Confusion())
820 return Standard_False;
821 }
822
823 if( ( (t1 != GeomAbs_BezierCurve && t1 != GeomAbs_BSplineCurve) ||
824 (t2 != GeomAbs_BezierCurve && t2 != GeomAbs_BSplineCurve)) && t1 != t2)
825 return Standard_False;
826
827 gp_Vec Diff1, Diff2;
828 gp_Pnt P1, P2;
829 if (edge1.Orientation() == TopAbs_FORWARD)
830 ade1.D1(ade1.LastParameter(), P1, Diff1);
831 else
832 {
833 ade1.D1(ade1.FirstParameter(), P1, Diff1);
834 Diff1 = -Diff1;
835 }
836
837 if (edge2.Orientation() == TopAbs_FORWARD)
838 ade2.D1(ade2.FirstParameter(), P2, Diff2);
839 else
840 {
841 ade2.D1(ade2.LastParameter(), P2, Diff2);
842 Diff2 = -Diff2;
843 }
844
632175c3 845 if (Diff1.Angle(Diff2) > theAngTol)
f7d70540 846 return Standard_False;
847
848 return Standard_True;
849}
850
cef6867c 851//=======================================================================
852//function : GenerateSubSeq
853//purpose : Generates sub-sequences of edges from sequence of edges
854//Edges from each subsequences can be merged into the one edge
855//=======================================================================
856
f7d70540 857static void GenerateSubSeq (const TopTools_SequenceOfShape& anInpEdgeSeq,
858 NCollection_Sequence<SubSequenceOfEdges>& SeqOfSubSeqOfEdges,
632175c3 859 Standard_Boolean IsClosed, double theAngTol, const TopTools_MapOfShape& AvoidEdgeVrt)
f7d70540 860{
861 Standard_Boolean isOk = Standard_False;
862 TopoDS_Edge edge1, edge2;
863
864 SubSequenceOfEdges SubSeq;
865 SubSeq.SeqsEdges.Append(TopoDS::Edge(anInpEdgeSeq(1)));
866 SeqOfSubSeqOfEdges.Append(SubSeq);
867
868 for (int i = 1; i < anInpEdgeSeq.Length(); i++)
869 {
870 edge1 = TopoDS::Edge(anInpEdgeSeq(i));
871 edge2 = TopoDS::Edge(anInpEdgeSeq(i+1));
632175c3 872 isOk = IsMergingPossible(edge1, edge2, theAngTol, AvoidEdgeVrt);
f7d70540 873 if (!isOk)
874 {
51740958 875 SubSequenceOfEdges aSubSeq;
876 aSubSeq.SeqsEdges.Append(edge2);
877 SeqOfSubSeqOfEdges.Append(aSubSeq);
f7d70540 878 }
879 else
880 SeqOfSubSeqOfEdges.ChangeLast().SeqsEdges.Append(edge2);
881 }
882 /// check first and last chain segments
883 if (IsClosed && SeqOfSubSeqOfEdges.Length() > 1)
884 {
885 edge1 = TopoDS::Edge(anInpEdgeSeq.Last());
886 edge2 = TopoDS::Edge(anInpEdgeSeq.First());
632175c3 887 if (IsMergingPossible(edge1, edge2, theAngTol, AvoidEdgeVrt))
f7d70540 888 {
889 SeqOfSubSeqOfEdges.ChangeLast().SeqsEdges.Append(SeqOfSubSeqOfEdges.ChangeFirst().SeqsEdges);
890 SeqOfSubSeqOfEdges.Remove(1);
891 }
892 }
893}
894
f7d70540 895//=======================================================================
896//function : MergeEdges
897//purpose : auxilary
898//=======================================================================
f98965d2 899static Standard_Boolean MergeEdges(TopTools_SequenceOfShape& SeqEdges,
632175c3 900 const Standard_Real theAngTol,
f7d70540 901 const Standard_Boolean ConcatBSplines,
632175c3 902 const Standard_Boolean isSafeInputMode,
903 Handle(ShapeBuild_ReShape)& theContext,
f7d70540 904 NCollection_Sequence<SubSequenceOfEdges>& SeqOfSubSeqOfEdges,
9ed6494b 905 const TopTools_MapOfShape& NonMergVrt)
f7d70540 906{
f98965d2 907 // skip degenerated edges, and forbid merging through them
908 TopTools_IndexedDataMapOfShapeListOfShape aMapVE;
f7d70540 909 Standard_Integer j;
910 TopTools_MapOfShape VerticesToAvoid;
f7d70540 911 for (j = 1; j <= SeqEdges.Length(); j++)
912 {
913 TopoDS_Edge anEdge = TopoDS::Edge(SeqEdges(j));
914 if (BRep_Tool::Degenerated(anEdge))
915 {
916 TopoDS_Vertex V1, V2;
917 TopExp::Vertices(anEdge, V1, V2);
918 VerticesToAvoid.Add(V1);
919 VerticesToAvoid.Add(V2);
f98965d2 920 SeqEdges.Remove(j--);
921 }
922 else
923 {
924 // fill in the map V-E
925 for (TopoDS_Iterator it(anEdge.Oriented(TopAbs_FORWARD)); it.More(); it.Next())
926 {
927 TopoDS_Shape aV = it.Value();
928 if (aV.Orientation() == TopAbs_FORWARD || aV.Orientation() == TopAbs_REVERSED)
929 {
930 if (!aMapVE.Contains(aV))
931 aMapVE.Add(aV, TopTools_ListOfShape());
932 aMapVE.ChangeFromKey(aV).Append(anEdge);
38c2acd4 933 }
f98965d2 934 }
f7d70540 935 }
f7d70540 936 }
f98965d2 937 VerticesToAvoid.Unite(NonMergVrt);
f7d70540 938
f98965d2 939 // do loop while there are unused edges
940 TopTools_MapOfShape aUsedEdges;
941 for (;;)
942 {
943 TopoDS_Edge edge;
944 for(j=1; j <= SeqEdges.Length(); j++)
945 {
946 edge = TopoDS::Edge(SeqEdges.Value(j));
947 if (!aUsedEdges.Contains(edge))
948 break;
949 }
950 if (j > SeqEdges.Length())
951 break; // all edges have been used
952
953 // make chain for unite
954 TopTools_SequenceOfShape aChain;
38c2acd4 955 aChain.Append(edge);
f98965d2 956 aUsedEdges.Add(edge);
957 TopoDS_Vertex V[2];
958 TopExp::Vertices(edge, V[0], V[1], Standard_True);
959
960 // connect more edges to the chain in both directions
961 for (j = 0; j < 2; j++)
962 {
963 Standard_Boolean isAdded = Standard_True;
964 while (isAdded)
965 {
966 isAdded = Standard_False;
967 if (V[j].IsNull())
968 break;
969 const TopTools_ListOfShape& aLE = aMapVE.FindFromKey(V[j]);
970 for (TopTools_ListIteratorOfListOfShape itL(aLE); itL.More(); itL.Next())
971 {
972 edge = TopoDS::Edge(itL.Value());
973 if (!aUsedEdges.Contains(edge))
974 {
38c2acd4 975 TopoDS_Vertex V2[2];
976 TopExp::Vertices(edge, V2[0], V2[1], Standard_True);
977 // the neighboring edge must have V[j] reversed and located on the opposite end
978 if (V2[1 - j].IsEqual(V[j].Reversed()))
979 {
980 if (j == 0)
981 aChain.Prepend(edge);
982 else
983 aChain.Append(edge);
984 aUsedEdges.Add(edge);
985 V[j] = V2[j];
986 isAdded = Standard_True;
987 break;
988 }
989 }
990 }
f7d70540 991 }
992 }
f7d70540 993
f98965d2 994 if (aChain.Length() < 2)
995 continue;
2277323d 996
38c2acd4 997 Standard_Boolean IsClosed = Standard_False;
f98965d2 998 if (V[0].IsSame ( V[1] ))
38c2acd4 999 IsClosed = Standard_True;
f98965d2 1000
1001 // split chain by vertices at which merging is not possible
1002 NCollection_Sequence<SubSequenceOfEdges> aOneSeq;
632175c3 1003 GenerateSubSeq(aChain, aOneSeq, IsClosed, theAngTol, VerticesToAvoid);
f98965d2 1004
1005 // put sub-chains in the result
1006 SeqOfSubSeqOfEdges.Append(aOneSeq);
1007 }
f7d70540 1008
1009 for (int i = 1; i <= SeqOfSubSeqOfEdges.Length(); i++)
1010 {
1011 TopoDS_Edge UE;
1012 if (SeqOfSubSeqOfEdges(i).SeqsEdges.Length() < 2)
1013 continue;
632175c3 1014 if (MergeSubSeq(SeqOfSubSeqOfEdges(i).SeqsEdges, UE, theAngTol,
1015 ConcatBSplines, isSafeInputMode, theContext))
f7d70540 1016 SeqOfSubSeqOfEdges(i).UnionEdges = UE;
1017 }
2277323d 1018 return Standard_True;
1019}
1020
cef6867c 1021//=======================================================================
1022//function : MergeSeq
f523acf0 1023//purpose : Tries to unify the sequence of edges with the set of
1024// another edges which lies on the same geometry
cef6867c 1025//=======================================================================
f98965d2 1026static Standard_Boolean MergeSeq (TopTools_SequenceOfShape& SeqEdges,
632175c3 1027 const Standard_Real theAngTol,
f7d70540 1028 const Standard_Boolean ConcatBSplines,
632175c3 1029 const Standard_Boolean isSafeInputMode,
f7d70540 1030 Handle(ShapeBuild_ReShape)& theContext,
f523acf0 1031 const TopTools_MapOfShape& nonMergVert)
1032{
f7d70540 1033 NCollection_Sequence<SubSequenceOfEdges> SeqOfSubsSeqOfEdges;
632175c3 1034 if (MergeEdges(SeqEdges, theAngTol, ConcatBSplines, isSafeInputMode,
1035 theContext, SeqOfSubsSeqOfEdges, nonMergVert))
f7d70540 1036 {
1037 for (Standard_Integer i = 1; i <= SeqOfSubsSeqOfEdges.Length(); i++ )
1038 {
1039 if (SeqOfSubsSeqOfEdges(i).UnionEdges.IsNull())
1040 continue;
654c48b2 1041
1042 theContext->Merge(SeqOfSubsSeqOfEdges(i).SeqsEdges,
1043 SeqOfSubsSeqOfEdges(i).UnionEdges);
f7d70540 1044 }
1045 return Standard_True;
1046 }
f523acf0 1047 return Standard_False;
f7d70540 1048}
1049
cef6867c 1050//=======================================================================
1051//function : CheckSharedVertices
1052//purpose : Checks the sequence of edges on the presence of shared vertex
1053//=======================================================================
1054
f7d70540 1055static void CheckSharedVertices(const TopTools_SequenceOfShape& theSeqEdges,
1056 const TopTools_IndexedDataMapOfShapeListOfShape& theMapEdgesVertex,
9ed6494b 1057 const TopTools_MapOfShape& theMapKeepShape,
f7d70540 1058 TopTools_MapOfShape& theShareVertMap)
1059{
1060 ShapeAnalysis_Edge sae;
1061 TopTools_SequenceOfShape SeqVertexes;
1062 TopTools_MapOfShape MapVertexes;
1063 for (Standard_Integer k = 1; k <= theSeqEdges.Length(); k++ )
1064 {
1065 TopoDS_Vertex aV1 = sae.FirstVertex(TopoDS::Edge(theSeqEdges(k)));
1066 TopoDS_Vertex aV2 = sae.LastVertex(TopoDS::Edge(theSeqEdges(k)));
1067 if (!MapVertexes.Add(aV1))
1068 SeqVertexes.Append(aV1);
1069 if (!MapVertexes.Add(aV2))
1070 SeqVertexes.Append(aV2);
1071 }
1072
1073 for (Standard_Integer k = 1; k <= SeqVertexes.Length()/* && !IsSharedVertexPresent*/; k++ )
1074 {
1075 const TopTools_ListOfShape& ListEdgesV1 = theMapEdgesVertex.FindFromKey(SeqVertexes(k));
f1191d30 1076 if (ListEdgesV1.Extent() > 2 || theMapKeepShape.Contains(SeqVertexes(k)))
f7d70540 1077 theShareVertMap.Add(SeqVertexes(k));
1078 }
1079 //return theShareVertMap.IsEmpty() ? false : true;
1080}
1081
2277323d 1082//=======================================================================
1083//function : ShapeUpgrade_UnifySameDomain
1084//purpose : Constructor
1085//=======================================================================
1086
1087ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain()
2ba9eb30 1088 : myLinTol(Precision::Confusion()),
1089 myAngTol(Precision::Angular()),
1090 myUnifyFaces(Standard_True),
fe1a6e4e 1091 myUnifyEdges (Standard_True),
1092 myConcatBSplines (Standard_False),
632175c3 1093 myAllowInternal (Standard_False),
654c48b2 1094 mySafeInputMode(Standard_True),
1095 myHistory(new BRepTools_History)
2277323d 1096{
2277323d 1097 myContext = new ShapeBuild_ReShape;
1098}
1099
1100//=======================================================================
1101//function : ShapeUpgrade_UnifySameDomain
1102//purpose : Constructor
1103//=======================================================================
1104
1105ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain(const TopoDS_Shape& aShape,
1106 const Standard_Boolean UnifyEdges,
1107 const Standard_Boolean UnifyFaces,
1108 const Standard_Boolean ConcatBSplines)
fe1a6e4e 1109 : myInitShape (aShape),
2ba9eb30 1110 myLinTol(Precision::Confusion()),
1111 myAngTol(Precision::Angular()),
1112 myUnifyFaces(UnifyFaces),
fe1a6e4e 1113 myUnifyEdges (UnifyEdges),
1114 myConcatBSplines (ConcatBSplines),
1115 myAllowInternal (Standard_False),
632175c3 1116 mySafeInputMode (Standard_True),
654c48b2 1117 myShape (aShape),
1118 myHistory(new BRepTools_History)
2277323d 1119{
2277323d 1120 myContext = new ShapeBuild_ReShape;
1121}
1122
1123//=======================================================================
1124//function : Initialize
1125//purpose :
1126//=======================================================================
1127
1128void ShapeUpgrade_UnifySameDomain::Initialize(const TopoDS_Shape& aShape,
1129 const Standard_Boolean UnifyEdges,
1130 const Standard_Boolean UnifyFaces,
1131 const Standard_Boolean ConcatBSplines)
1132{
1133 myInitShape = aShape;
1134 myShape = aShape;
1135 myUnifyEdges = UnifyEdges;
1136 myUnifyFaces = UnifyFaces;
1137 myConcatBSplines = ConcatBSplines;
1138
1139 myContext->Clear();
9ed6494b 1140 myKeepShapes.Clear();
f523acf0 1141 myHistory->Clear();
fe1a6e4e 1142}
1143
1144//=======================================================================
1145//function : AllowInternalEdges
1146//purpose :
1147//=======================================================================
1148
1149void ShapeUpgrade_UnifySameDomain::AllowInternalEdges (const Standard_Boolean theValue)
1150{
1151 myAllowInternal = theValue;
2277323d 1152}
1153
632175c3 1154//=======================================================================
1155//function : SetSafeInputMode
1156//purpose :
1157//=======================================================================
1158
1159void ShapeUpgrade_UnifySameDomain::SetSafeInputMode(Standard_Boolean theValue)
1160{
1161 mySafeInputMode = theValue;
1162}
1163
9ed6494b 1164//=======================================================================
1165//function : KeepShape
1166//purpose :
1167//=======================================================================
1168
1169void ShapeUpgrade_UnifySameDomain::KeepShape(const TopoDS_Shape& theShape)
1170{
1171 if (theShape.ShapeType() == TopAbs_EDGE || theShape.ShapeType() == TopAbs_VERTEX)
1172 myKeepShapes.Add(theShape);
1173}
1174
1175//=======================================================================
1176//function : KeepShapes
1177//purpose :
1178//=======================================================================
1179
1180void ShapeUpgrade_UnifySameDomain::KeepShapes(const TopTools_MapOfShape& theShapes)
1181{
1182 for (TopTools_MapIteratorOfMapOfShape it(theShapes); it.More(); it.Next()) {
1183 if (it.Value().ShapeType() == TopAbs_EDGE || it.Value().ShapeType() == TopAbs_VERTEX)
1184 myKeepShapes.Add(it.Value());
1185 }
1186}
1187
2277323d 1188//=======================================================================
1189//function : UnifyFaces
1190//purpose :
1191//=======================================================================
1192
1193void ShapeUpgrade_UnifySameDomain::UnifyFaces()
1194{
fe1a6e4e 1195 // creating map of edge faces for the whole shape
1196 TopTools_IndexedDataMapOfShapeListOfShape aGMapEdgeFaces;
1197 TopExp::MapShapesAndAncestors(myShape, TopAbs_EDGE, TopAbs_FACE, aGMapEdgeFaces);
56091b56 1198
1199 // unify faces in each shell separately
2277323d 1200 TopExp_Explorer exps;
56091b56 1201 for (exps.Init(myShape, TopAbs_SHELL); exps.More(); exps.Next())
977ad983 1202 IntUnifyFaces(exps.Current(), aGMapEdgeFaces);
56091b56 1203
1204 // gather all faces out of shells in one compound and unify them at once
1205 BRep_Builder aBB;
1206 TopoDS_Compound aCmp;
1207 aBB.MakeCompound(aCmp);
1208 Standard_Integer nbf = 0;
1209 for (exps.Init(myShape, TopAbs_FACE, TopAbs_SHELL); exps.More(); exps.Next(), nbf++)
1210 aBB.Add(aCmp, exps.Current());
1211
1212 if (nbf > 0)
977ad983 1213 IntUnifyFaces(aCmp, aGMapEdgeFaces);
56091b56 1214
1215 myShape = myContext->Apply(myShape);
1216}
2277323d 1217
f16a6cc5 1218//=======================================================================
1219//function : SetFixWireModes
1220//purpose :
1221//=======================================================================
1222
1223static void SetFixWireModes(ShapeFix_Face& theSff)
1224{
1225 Handle(ShapeFix_Wire) aFixWire = theSff.FixWireTool();
1226 aFixWire->FixSelfIntersectionMode() = 0;
1227 aFixWire->FixNonAdjacentIntersectingEdgesMode() = 0;
1228 aFixWire->FixLackingMode() = 0;
1229 aFixWire->FixNotchedEdgesMode() = 0;
1230 aFixWire->ModifyTopologyMode() = Standard_False;
1231 aFixWire->ModifyRemoveLoopMode() = 0;
1232 aFixWire->FixGapsByRangesMode() = Standard_False;
1233 aFixWire->FixSmallMode() = 0;
1234}
1235
632175c3 1236//=======================================================================
1237//function : IntUnifyFaces
1238//purpose :
1239//=======================================================================
2277323d 1240
56091b56 1241void ShapeUpgrade_UnifySameDomain::IntUnifyFaces(const TopoDS_Shape& theInpShape,
977ad983 1242 TopTools_IndexedDataMapOfShapeListOfShape& theGMapEdgeFaces)
56091b56 1243{
1244 // creating map of edge faces for the shape
1245 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
1246 TopExp::MapShapesAndAncestors(theInpShape, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
1247
1248 // map of processed shapes
1249 TopTools_MapOfShape aProcessed;
1250
56091b56 1251 // processing each face
f523acf0 1252 TopExp_Explorer exp;
56091b56 1253 for (exp.Init(theInpShape, TopAbs_FACE); exp.More(); exp.Next()) {
10ce3246 1254 const TopoDS_Face& aFaceOriginal = TopoDS::Face(exp.Current());
1255 TopoDS_Face aFace = TopoDS::Face(aFaceOriginal.Oriented(TopAbs_FORWARD));
2277323d 1256
56091b56 1257 if (aProcessed.Contains(aFace))
1258 continue;
2277323d 1259
f523acf0 1260 // Boundary edges for the new face
56091b56 1261 TopTools_SequenceOfShape edges;
2277323d 1262
f523acf0 1263 Standard_Integer dummy;
1264 AddOrdinaryEdges(edges, aFace, dummy);
1265
1266 // Faces to get unified with the current faces
56091b56 1267 TopTools_SequenceOfShape faces;
f523acf0 1268
1269 // Add the current face for unification
56091b56 1270 faces.Append(aFace);
1271
f523acf0 1272 // surface and location to construct result
56091b56 1273 TopLoc_Location aBaseLocation;
1274 Handle(Geom_Surface) aBaseSurface = BRep_Tool::Surface(aFace,aBaseLocation);
1275 aBaseSurface = ClearRts(aBaseSurface);
1276
1277 // find adjacent faces to union
1278 Standard_Integer i;
1279 for (i = 1; i <= edges.Length(); i++) {
1280 TopoDS_Edge edge = TopoDS::Edge(edges(i));
1281 if (BRep_Tool::Degenerated(edge))
1282 continue;
2277323d 1283
56091b56 1284 // get connectivity of the edge in the global shape
1285 const TopTools_ListOfShape& aGList = theGMapEdgeFaces.FindFromKey(edge);
9ed6494b 1286 if (!myAllowInternal && (aGList.Extent() != 2 || myKeepShapes.Contains(edge))) {
f523acf0 1287 // non manifold case is not processed unless myAllowInternal
56091b56 1288 continue;
1289 }
10ce3246 1290 //
f523acf0 1291 // Get the faces connected through the edge in the current shape
1292 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
1293 if (aList.Extent() < 2) {
1294 continue;
1295 }
1296
f16a6cc5 1297 // for a planar face create and store pcurve of edge on face
1298 // to speed up all operations
1299 if (!mySafeInputMode && aBaseSurface->IsKind(STANDARD_TYPE(Geom_Plane)))
1300 BRepLib::BuildPCurveForEdgeOnPlane(edge, aFace);
1301
10ce3246 1302 // get normal of the face to compare it with normals of other faces
1303 gp_Dir aDN1;
1304 //
1305 // take intermediate point on edge to compute the normal
1306 Standard_Real f, l;
1307 BRep_Tool::Range(edge, f, l);
1308 Standard_Real aTMid = (f + l) * .5;
1309 //
1310 Standard_Boolean bCheckNormals = GetNormalToSurface(aFaceOriginal, edge, aTMid, aDN1);
1311 //
f523acf0 1312 // Process the faces
56091b56 1313 TopTools_ListIteratorOfListOfShape anIter(aList);
1314 for (; anIter.More(); anIter.Next()) {
10ce3246 1315 const TopoDS_Face& aCheckedFaceOriginal = TopoDS::Face(anIter.Value());
1316 TopoDS_Face anCheckedFace = TopoDS::Face(aCheckedFaceOriginal.Oriented(TopAbs_FORWARD));
56091b56 1317 if (anCheckedFace.IsSame(aFace))
fe1a6e4e 1318 continue;
2277323d 1319
56091b56 1320 if (aProcessed.Contains(anCheckedFace))
1321 continue;
2277323d 1322
10ce3246 1323 if (bCheckNormals) {
1324 // get normal of checked face using the same parameter on edge
1325 gp_Dir aDN2;
1326 if (GetNormalToSurface(aCheckedFaceOriginal, edge, aTMid, aDN2)) {
1327 // and check if the adjacent faces are having approximately same normals
1328 Standard_Real anAngle = aDN1.Angle(aDN2);
1329 if (anAngle > myAngTol) {
1330 continue;
1331 }
1332 }
1333 }
1334 //
2ba9eb30 1335 if (IsSameDomain(aFace,anCheckedFace, myLinTol, myAngTol)) {
f0144633 1336
56091b56 1337 // hotfix for 27271: prevent merging along periodic direction.
d3dadd23 1338 if (IsLikeSeam(edge, anCheckedFace, aBaseSurface))
56091b56 1339 continue;
2277323d 1340
f523acf0 1341 if (AddOrdinaryEdges(edges,anCheckedFace,dummy)) {
56091b56 1342 // sequence edges is modified
1343 i = dummy;
2277323d 1344 }
56091b56 1345
1346 faces.Append(anCheckedFace);
1347 aProcessed.Add(anCheckedFace);
1348 break;
2277323d 1349 }
1350 }
56091b56 1351 }
2277323d 1352
56091b56 1353 if (faces.Length() > 1) {
1354 // fill in the connectivity map for selected faces
1355 TopTools_IndexedDataMapOfShapeListOfShape aMapEF;
1356 for (i = 1; i <= faces.Length(); i++) {
1357 TopExp::MapShapesAndAncestors(faces(i), TopAbs_EDGE, TopAbs_FACE, aMapEF);
1358 }
f523acf0 1359 // Collect keep edges and multi-connected edges, i.e. edges that are internal to
56091b56 1360 // the set of selected faces and have connections to other faces.
9ed6494b 1361 TopTools_ListOfShape aKeepEdges;
56091b56 1362 for (i = 1; i <= aMapEF.Extent(); i++) {
1363 const TopTools_ListOfShape& aLF = aMapEF(i);
1364 if (aLF.Extent() == 2) {
1365 const TopoDS_Shape& aE = aMapEF.FindKey(i);
1366 const TopTools_ListOfShape& aGLF = theGMapEdgeFaces.FindFromKey(aE);
9ed6494b 1367 if (aGLF.Extent() > 2 || myKeepShapes.Contains(aE)) {
1368 aKeepEdges.Append(aE);
fe1a6e4e 1369 }
1370 }
9ed6494b 1371 }
1372 if (!aKeepEdges.IsEmpty()) {
1373 if (!myAllowInternal) {
1374 // Remove from the selection the faces which have no other connect edges
f523acf0 1375 // and contain multi-connected edges and/or keep edges.
56091b56 1376 TopTools_MapOfShape anAvoidFaces;
9ed6494b 1377 TopTools_ListIteratorOfListOfShape it(aKeepEdges);
56091b56 1378 for (; it.More(); it.Next()) {
1379 const TopoDS_Shape& aE = it.Value();
1380 const TopTools_ListOfShape& aLF = aMapEF.FindFromKey(aE);
1381 anAvoidFaces.Add(aLF.First());
1382 anAvoidFaces.Add(aLF.Last());
1383 }
9ed6494b 1384 for (i = 1; i <= faces.Length(); i++) {
56091b56 1385 if (anAvoidFaces.Contains(faces(i))) {
1386 // update the boundaries of merged area, for that
1387 // remove from 'edges' the edges of this face and add to 'edges'
1388 // the edges of this face that were not present in 'edges' before
9ed6494b 1389 Standard_Boolean hasConnectAnotherFaces = Standard_False;
56091b56 1390 TopExp_Explorer ex(faces(i), TopAbs_EDGE);
9ed6494b 1391 for (; ex.More() && !hasConnectAnotherFaces; ex.Next()) {
56091b56 1392 TopoDS_Shape aE = ex.Current();
9ed6494b 1393 const TopTools_ListOfShape& aLF = aMapEF.FindFromKey(aE);
1394 if (aLF.Extent() > 1) {
1395 for (it.Init(aLF); it.More() && !hasConnectAnotherFaces; it.Next()) {
1396 if (!anAvoidFaces.Contains(it.Value()))
1397 hasConnectAnotherFaces = Standard_True;
1398 }
1399 }
1400 }
1401 if (!hasConnectAnotherFaces) {
1402 AddOrdinaryEdges(edges, faces(i), dummy);
1403 faces.Remove(i);
1404 i--;
1405 }
1406 }
1407 }
1408 // check if the faces with keep edges contained in
1409 // already updated the boundaries of merged area
1410 if (!faces.IsEmpty()) {
1411 TopTools_MapOfShape aMapFaces;
1412 for (i = 1; i <= faces.Length(); i++) {
1413 aMapFaces.Add(faces(i));
1414 }
1415 for (it.Init(aKeepEdges); it.More(); it.Next()) {
1416 const TopoDS_Shape& aE = it.Value();
1417 const TopTools_ListOfShape& aLF = aMapEF.FindFromKey(aE);
1418 if (aLF.Extent() < 2)
1419 continue;
1420 if (aMapFaces.Contains(aLF.First()) &&
1421 aMapFaces.Contains(aLF.Last())) {
1422 for (i = 1; i <= faces.Length(); i++) {
1423 if (faces(i).IsEqual(aLF.First()) ||
1424 faces(i).IsEqual(aLF.Last())) {
1425 AddOrdinaryEdges(edges, faces(i), dummy);
1426 faces.Remove(i);
1427 i--;
1428 }
cb120537 1429 }
cb120537 1430 }
fe1a6e4e 1431 }
1432 }
56091b56 1433 }
1434 else {
f523acf0 1435 // add multi-connected and keep edges as internal in new face
9ed6494b 1436 TopTools_ListIteratorOfListOfShape it(aKeepEdges);
56091b56 1437 for (; it.More(); it.Next()) {
1438 const TopoDS_Shape& aE = it.Value();
1439 edges.Append(aE.Oriented(TopAbs_INTERNAL));
fe1a6e4e 1440 }
1441 }
1442 }
56091b56 1443 }
fe1a6e4e 1444
56091b56 1445 // all faces collected in the sequence. Perform union of faces
1446 if (faces.Length() > 1) {
56091b56 1447 TopoDS_Face aResult;
1448 BRep_Builder B;
1449 B.MakeFace(aResult,aBaseSurface,aBaseLocation,0);
1450 Standard_Integer nbWires = 0;
1451
f16a6cc5 1452 TopoDS_Face tmpF = TopoDS::Face(faces(1).Oriented(TopAbs_FORWARD));
20aa0d3f 1453
56091b56 1454 // connecting wires
1455 while (edges.Length()>0) {
1456
1457 Standard_Boolean isEdge3d = Standard_False;
1458 nbWires++;
1459 TopTools_MapOfShape aVertices;
1460 TopoDS_Wire aWire;
1461 B.MakeWire(aWire);
1462
1463 TopoDS_Edge anEdge = TopoDS::Edge(edges(1));
1464 edges.Remove(1);
1465 // collect internal edges in separate wires
1466 Standard_Boolean isInternal = (anEdge.Orientation() == TopAbs_INTERNAL);
1467
1468 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
1469 B.Add(aWire,anEdge);
1470 TopoDS_Vertex V1,V2;
1471 TopExp::Vertices(anEdge,V1,V2);
1472 aVertices.Add(V1);
1473 aVertices.Add(V2);
1474
1475 Standard_Boolean isNewFound = Standard_False;
1476 do {
1477 isNewFound = Standard_False;
1478 for(Standard_Integer j = 1; j <= edges.Length(); j++) {
1479 anEdge = TopoDS::Edge(edges(j));
1480 // check if the current edge orientation corresponds to the first one
1481 Standard_Boolean isCurrInternal = (anEdge.Orientation() == TopAbs_INTERNAL);
1482 if (isCurrInternal != isInternal)
1483 continue;
1484 TopExp::Vertices(anEdge,V1,V2);
1485 if(aVertices.Contains(V1) || aVertices.Contains(V2)) {
1486 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
1487 aVertices.Add(V1);
1488 aVertices.Add(V2);
1489 B.Add(aWire,anEdge);
1490 edges.Remove(j);
1491 j--;
1492 isNewFound = Standard_True;
2277323d 1493 }
2277323d 1494 }
56091b56 1495 } while (isNewFound);
1496
1497 // sorting any type of edges
1498 aWire.Closed (BRep_Tool::IsClosed (aWire));
56091b56 1499
1500 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(aWire,tmpF,Precision::Confusion());
632175c3 1501 if (mySafeInputMode)
1502 sfw->SetContext(myContext);
56091b56 1503 sfw->FixReorder();
1504 Standard_Boolean isDegRemoved = Standard_False;
1505 if(!sfw->StatusReorder ( ShapeExtend_FAIL )) {
1506 // clear degenerated edges if at least one with 3d curve exist
2277323d 1507 if(isEdge3d) {
56091b56 1508 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
1509 for(Standard_Integer j = 1; j<=sewd->NbEdges();j++) {
1510 TopoDS_Edge E = sewd->Edge(j);
1511 if(BRep_Tool::Degenerated(E)) {
1512 sewd->Remove(j);
1513 isDegRemoved = Standard_True;
1514 j--;
2277323d 1515 }
2277323d 1516 }
2277323d 1517 }
56091b56 1518 sfw->FixShifted();
1519 if(isDegRemoved)
1520 sfw->FixDegenerated();
2277323d 1521 }
f16a6cc5 1522 aWire = sfw->Wire();
2277323d 1523
56091b56 1524 // add resulting wire
1525 if(isEdge3d) {
f16a6cc5 1526 B.Add(aResult,aWire);
56091b56 1527 }
1528 else {
1529 // sorting edges
1530 Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
1531 Standard_Integer nbEdges = sbwd->NbEdges();
1532 // sort degenerated edges and create one edge instead of several ones
1533 ShapeAnalysis_WireOrder sawo(Standard_False, 0);
1534 ShapeAnalysis_Edge sae;
1535 Standard_Integer aLastEdge = nbEdges;
1536 for(Standard_Integer j = 1; j <= nbEdges; j++) {
1537 Standard_Real f,l;
1538 //smh protection on NULL pcurve
1539 Handle(Geom2d_Curve) c2d;
1540 if(!sae.PCurve(sbwd->Edge(j),tmpF,c2d,f,l)) {
1541 aLastEdge--;
1542 continue;
38c2acd4 1543 }
56091b56 1544 sawo.Add(c2d->Value(f).XY(),c2d->Value(l).XY());
2277323d 1545 }
d3dadd23 1546 if (sawo.NbEdges() == 0)
1547 continue;
56091b56 1548 sawo.Perform();
1549
1550 // constructind one degenerative edge
1551 gp_XY aStart, anEnd, tmp;
1552 Standard_Integer nbFirst = sawo.Ordered(1);
1553 TopoDS_Edge anOrigE = TopoDS::Edge(sbwd->Edge(nbFirst).Oriented(TopAbs_FORWARD));
1554 ShapeBuild_Edge sbe;
1555 TopoDS_Vertex aDummyV;
1556 TopoDS_Edge E = sbe.CopyReplaceVertices(anOrigE,aDummyV,aDummyV);
1557 sawo.XY(nbFirst,aStart,tmp);
1558 sawo.XY(sawo.Ordered(aLastEdge),tmp,anEnd);
1559
1560 gp_XY aVec = anEnd-aStart;
1561 Handle(Geom2d_Line) aLine = new Geom2d_Line(aStart,gp_Dir2d(anEnd-aStart));
1562
1563 B.UpdateEdge(E,aLine,tmpF,0.);
1564 B.Range(E,tmpF,0.,aVec.Modulus());
1565 Handle(Geom_Curve) C3d;
1566 B.UpdateEdge(E,C3d,0.);
1567 B.Degenerated(E,Standard_True);
1568 TopoDS_Wire aW;
1569 B.MakeWire(aW);
1570 B.Add(aW,E);
1571 aW.Closed (Standard_True);
1572 B.Add(aResult,aW);
1573 }
1574 }
2277323d 1575
56091b56 1576 ShapeFix_Face sff (aResult);
f523acf0 1577 //Initializing by tolerances
56091b56 1578 sff.SetPrecision(Precision::Confusion());
f523acf0 1579 sff.SetMinTolerance(Precision::Confusion());
56091b56 1580 sff.SetMaxTolerance(1.);
1581 //Setting modes
f16a6cc5 1582 SetFixWireModes(sff);
1583 if (mySafeInputMode)
1584 sff.SetContext(myContext);
56091b56 1585 // Applying the fixes
1586 sff.Perform();
f16a6cc5 1587 if(!sff.Status(ShapeExtend_FAIL))
1588 {
1589 // perform substitution of faces
1590 aResult = sff.Face();
1591 myContext->Merge(faces, aResult);
2277323d 1592 }
56091b56 1593 }
1594 } // end processing each face
2277323d 1595}
1596
1597//=======================================================================
1598//function : UnifyEdges
1599//purpose :
1600//=======================================================================
2277323d 1601void ShapeUpgrade_UnifySameDomain::UnifyEdges()
1602{
632175c3 1603 TopoDS_Shape aRes = myContext->Apply(myShape);
2277323d 1604
ce41efde 1605 TopTools_IndexedMapOfShape ChangedFaces;
2277323d 1606
ce41efde 1607 // creating map of edge faces
1608 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
632175c3 1609 TopExp::MapShapesAndAncestors(aRes, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
ce41efde 1610
1611 // creating map of vertex edges
1612 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgesVertex;
632175c3 1613 TopExp::MapShapesAndUniqueAncestors(aRes, TopAbs_VERTEX, TopAbs_EDGE, aMapEdgesVertex);
f7d70540 1614
1615 TopTools_MapOfShape SharedVert;
1616
f7d70540 1617 TopTools_IndexedMapOfShape anOldEdges;
1618 TopExp::MapShapes(myInitShape, TopAbs_EDGE, anOldEdges);
1619
1620 TopTools_DataMapOfShapeShape NewEdges2OldEdges;
1621 for (int i = 1; i <= anOldEdges.Extent(); i++)
1622 {
632175c3 1623 const TopoDS_Shape& anOldEdge = anOldEdges(i);
1624 TopoDS_Shape aNewEdge = myContext->Apply(anOldEdge);
1625 if (!aNewEdge.IsNull() && !aNewEdge.IsSame(anOldEdge))
1626 NewEdges2OldEdges.Bind(aNewEdge, anOldEdge);
f7d70540 1627 }
ce41efde 1628
632175c3 1629 if (mySafeInputMode)
1630 UpdateMapOfShapes(myKeepShapes, myContext);
1631
ce41efde 1632 TopExp_Explorer exp;
f7d70540 1633 // processing separate wires
1634 for (exp.Init(aRes, TopAbs_WIRE, TopAbs_FACE); exp.More(); exp.Next())
1635 {
1636 TopTools_SequenceOfShape SeqEdges;
1637 TopExp_Explorer expE(exp.Current(), TopAbs_EDGE);
1638 for (; expE.More(); expE.Next())
1639 SeqEdges.Append(expE.Current());
1640 SharedVert.Clear();
9ed6494b 1641 CheckSharedVertices(SeqEdges, aMapEdgesVertex, myKeepShapes, SharedVert);
654c48b2 1642 MergeSeq(SeqEdges, myAngTol, myConcatBSplines, mySafeInputMode, myContext,
f523acf0 1643 SharedVert);
f7d70540 1644 }
1645
1646 // processing each face
ce41efde 1647 for (exp.Init(aRes, TopAbs_FACE); exp.More(); exp.Next()) {
632175c3 1648 TopoDS_Shape aFace = exp.Current().Oriented(TopAbs_FORWARD);
ce41efde 1649 TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
f7d70540 1650 TopTools_SequenceOfShape aNonSharedEdges;
ce41efde 1651 for (TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
1652 TopoDS_Edge edge = TopoDS::Edge(expe.Current());
ce41efde 1653 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
1654 TopTools_ListIteratorOfListOfShape anIter(aList);
f7d70540 1655 Standard_Integer NbFacesPerEdge = aList.Extent();
ce41efde 1656 for ( ; anIter.More(); anIter.Next()) {
632175c3 1657 const TopoDS_Shape& aFace1 = anIter.Value();
1658 if (aFace1.IsSame(aFace) && NbFacesPerEdge != 1)
f7d70540 1659 continue;
1660 if (NbFacesPerEdge == 1)
1661 //store non-shared edges separately
1662 aNonSharedEdges.Append(edge);
1663 else
1664 {
632175c3 1665 if (aMapFacesEdges.Contains(aFace1))
1666 aMapFacesEdges.ChangeFromKey(aFace1).Append(edge);
f7d70540 1667 else
1668 {
1669 TopTools_ListOfShape ListEdges;
1670 ListEdges.Append(edge);
632175c3 1671 aMapFacesEdges.Add(aFace1, ListEdges);
f7d70540 1672 }
2277323d 1673 }
1674 }
ce41efde 1675 }
f7d70540 1676
1677 for (Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++)
1678 {
ce41efde 1679 const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
1680 TopTools_SequenceOfShape SeqEdges;
ce41efde 1681 TopTools_ListIteratorOfListOfShape anIter(ListEdges);
ce41efde 1682 for ( ; anIter.More(); anIter.Next())
1683 SeqEdges.Append(anIter.Value());
1684 if (SeqEdges.Length()==1)
1685 continue;
2277323d 1686
f7d70540 1687 SharedVert.Clear();
9ed6494b 1688 CheckSharedVertices(SeqEdges, aMapEdgesVertex, myKeepShapes, SharedVert);
632175c3 1689 if (MergeSeq(SeqEdges, myAngTol, myConcatBSplines, mySafeInputMode,
f523acf0 1690 myContext, SharedVert))
2277323d 1691 {
ce41efde 1692 TopoDS_Face tmpF = TopoDS::Face(exp.Current());
1693 if ( !ChangedFaces.Contains(tmpF) )
1694 ChangedFaces.Add(tmpF);
1695 tmpF = TopoDS::Face(aMapFacesEdges.FindKey(i));
1696 if ( !ChangedFaces.Contains(tmpF) )
1697 ChangedFaces.Add(tmpF);
2277323d 1698 }
ce41efde 1699 }
f7d70540 1700
1701 if ( aNonSharedEdges.Length() > 1 )
1702 {
1703 SharedVert.Clear();
9ed6494b 1704 CheckSharedVertices(aNonSharedEdges, aMapEdgesVertex, myKeepShapes, SharedVert);
632175c3 1705 if (MergeSeq(aNonSharedEdges, myAngTol, myConcatBSplines, mySafeInputMode,
f523acf0 1706 myContext, SharedVert))
f7d70540 1707 {
1708 TopoDS_Face tmpF = TopoDS::Face(exp.Current());
1709 if ( !ChangedFaces.Contains(tmpF) )
1710 ChangedFaces.Add(tmpF);
1711 }
1712 }
ce41efde 1713
1714 } // end processing each face
1715
1716 // fix changed faces and replace them in the local context
632175c3 1717 Standard_Real aPrec = Precision::Confusion();
1718 for (Standard_Integer i = 1; i <= ChangedFaces.Extent(); i++) {
ce41efde 1719 TopoDS_Face aFace = TopoDS::Face(myContext->Apply(ChangedFaces.FindKey(i)));
f7d70540 1720 if (aFace.IsNull())
1721 continue;
f16a6cc5 1722
1723 // for a planar face create and store pcurve of edge on face
1724 // to speed up all operations; but this is allowed only when non-safe mode in force
1725 if (!mySafeInputMode)
1726 {
1727 TopLoc_Location aLoc;
1728 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace, aLoc);
1729 aSurface = ClearRts(aSurface);
1730 if (aSurface->IsKind(STANDARD_TYPE(Geom_Plane)))
1731 {
1732 TopTools_ListOfShape aLE;
1733 for (TopExp_Explorer anEx(aFace, TopAbs_EDGE); anEx.More(); anEx.Next())
1734 aLE.Append(anEx.Current());
1735 BRepLib::BuildPCurveForEdgesOnPlane(aLE, aFace);
1736 }
1737 }
1738
1739 ShapeFix_Face sff(aFace);
1740 if (mySafeInputMode)
1741 sff.SetContext(myContext);
1742 sff.SetPrecision(aPrec);
1743 sff.SetMinTolerance(aPrec);
1744 sff.SetMaxTolerance(Max(1., aPrec*1000.));
1745 sff.FixOrientationMode() = 0;
1746 sff.FixAddNaturalBoundMode() = 0;
1747 sff.FixIntersectingWiresMode() = 0;
1748 sff.FixLoopWiresMode() = 0;
1749 sff.FixSplitFaceMode() = 0;
1750 sff.FixPeriodicDegeneratedMode() = 0;
1751 SetFixWireModes(sff);
1752 sff.Perform();
1753 TopoDS_Shape aNewFace = sff.Face();
ce41efde 1754 myContext->Replace(aFace,aNewFace);
ce41efde 1755 }
1756
1757 if (ChangedFaces.Extent() > 0) {
1758 // fix changed shell and replace it in the local context
ce41efde 1759 TopoDS_Shape aRes1 = myContext->Apply(aRes);
632175c3 1760 Standard_Boolean isChanged = Standard_False;
ce41efde 1761 TopExp_Explorer expsh;
1762 for (expsh.Init(aRes1, TopAbs_SHELL); expsh.More(); expsh.Next()) {
1763 TopoDS_Shell aShell = TopoDS::Shell(expsh.Current());
1764 Handle(ShapeFix_Shell) sfsh = new ShapeFix_Shell;
1765 sfsh->FixFaceOrientation(aShell);
1766 TopoDS_Shape aNewShell = sfsh->Shell();
632175c3 1767 if (!aNewShell.IsSame(aShell)) {
1768 myContext->Replace(aShell, aNewShell);
1769 isChanged = Standard_True;
2277323d 1770 }
ce41efde 1771 }
632175c3 1772 if (isChanged)
1773 aRes1 = myContext->Apply(aRes1);
1774 myContext->Replace(myShape, aRes1);
ce41efde 1775 }
2277323d 1776
1777 myShape = myContext->Apply(myShape);
1778}
1779
2277323d 1780//=======================================================================
1781//function : Build
1782//purpose : builds the resulting shape
f523acf0 1783//=======================================================================
2277323d 1784void ShapeUpgrade_UnifySameDomain::Build()
1785{
f16a6cc5 1786 if (myUnifyFaces)
2277323d 1787 UnifyFaces();
f16a6cc5 1788 if (myUnifyEdges)
1789 UnifyEdges();
2277323d 1790
f523acf0 1791 // Fill the history of modifications during the operation
1792 FillHistory();
1793}
1794
1795//=======================================================================
1796//function : FillHistory
1797//purpose : Fill the history of modifications during the operation
1798//=======================================================================
1799void ShapeUpgrade_UnifySameDomain::FillHistory()
1800{
1801 if (myHistory.IsNull())
1802 // History is not requested
1803 return;
1804
1805 // Only Vertices, Edges and Faces can be modified during unification.
1806 // Thus, only these kind of shapes should be checked.
1807
1808 // Get history from the context.
1809 // It contains all modifications of the operation. Some of these
1810 // modifications become not relevant and should be filtered.
1811 Handle(BRepTools_History) aCtxHistory = myContext->History();
1812
1813 // Explore the history of the context and fill
1814 // the history of UnifySameDomain algorithm
1815 Handle(BRepTools_History) aUSDHistory = new BRepTools_History();
1816
1817 // Map all Vertices, Edges and Faces in the input shape
1818 TopTools_IndexedMapOfShape aMapInputShape;
1819 TopExp::MapShapes(myInitShape, TopAbs_VERTEX, aMapInputShape);
1820 TopExp::MapShapes(myInitShape, TopAbs_EDGE , aMapInputShape);
1821 TopExp::MapShapes(myInitShape, TopAbs_FACE , aMapInputShape);
1822
1823 // Map all Vertices, Edges and Faces in the result shape
1824 TopTools_IndexedMapOfShape aMapResultShapes;
1825 TopExp::MapShapes(myShape, TopAbs_VERTEX, aMapResultShapes);
1826 TopExp::MapShapes(myShape, TopAbs_EDGE , aMapResultShapes);
1827 TopExp::MapShapes(myShape, TopAbs_FACE , aMapResultShapes);
1828
1829 // Iterate on all input shapes and get their modifications
1830 Standard_Integer i, aNb = aMapInputShape.Extent();
1831 for (i = 1; i <= aNb; ++i)
654c48b2 1832 {
f523acf0 1833 const TopoDS_Shape& aS = aMapInputShape(i);
1834
1835 // Check the shape itself to be present in the result
1836 if (aMapResultShapes.Contains(aS))
1837 {
1838 // The shape is present in the result as is, thus has not been modified
1839 continue;
1840 }
1841
1842 // Check if the shape has been modified during the operation
1843 const TopTools_ListOfShape& aLSImages = aCtxHistory->Modified(aS);
1844 if (aLSImages.IsEmpty())
1845 {
1846 // The shape has not been modified and not present in the result,
1847 // thus it has been removed
1848 aUSDHistory->Remove(aS);
1849 continue;
1850 }
1851
1852 // Check the images of the shape to be present in the result
1853 Standard_Boolean bRemoved = Standard_True;
1854 TopTools_ListIteratorOfListOfShape aItLSIm(aLSImages);
1855 for (; aItLSIm.More(); aItLSIm.Next())
1856 {
1857 if (aMapResultShapes.Contains(aItLSIm.Value()))
1858 {
1859 // Image is found in the result, thus the shape has been modified
1860 aUSDHistory->AddModified(aS, aItLSIm.Value());
1861 bRemoved = Standard_False;
1862 }
1863 }
1864
1865 if (bRemoved)
654c48b2 1866 {
f523acf0 1867 // No images are found in the result, thus the shape has been removed
1868 aUSDHistory->Remove(aS);
654c48b2 1869 }
20aa0d3f 1870 }
f7d70540 1871
f523acf0 1872 // Merge the history of the operation into global history
1873 myHistory->Merge(aUSDHistory);
2277323d 1874}