0026466: UnifySameDomain creates invalid result shape from valid input shape
[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>
2277323d 86
87//=======================================================================
88//function : AddOrdinaryEdges
89//purpose : auxilary
90//=======================================================================
91// adds edges from the shape to the sequence
92// seams and equal edges are dropped
93// Returns true if one of original edges dropped
94static Standard_Boolean AddOrdinaryEdges(TopTools_SequenceOfShape& edges,
95 const TopoDS_Shape aShape,
96 Standard_Integer& anIndex)
97{
98 //map of edges
99 TopTools_IndexedMapOfShape aNewEdges;
100 //add edges without seams
101 for(TopExp_Explorer exp(aShape,TopAbs_EDGE); exp.More(); exp.Next()) {
102 TopoDS_Shape edge = exp.Current();
103 if(aNewEdges.Contains(edge))
104 {
105 //aNewEdges.Remove(edge);
106 TopoDS_Shape LastEdge = aNewEdges(aNewEdges.Extent());
107 aNewEdges.RemoveLast();
108 if (aNewEdges.FindIndex(edge) != 0)
109 aNewEdges.Substitute(aNewEdges.FindIndex(edge), LastEdge);
110 /////////////////////////
111 }
112 else
113 aNewEdges.Add(edge);
114 }
115
116 Standard_Boolean isDropped = Standard_False;
117 //merge edges and drop seams
118 Standard_Integer i;
119 for (i = 1; i <= edges.Length(); i++) {
120 TopoDS_Shape current = edges(i);
121 if(aNewEdges.Contains(current)) {
122
123 //aNewEdges.Remove(current);
124 TopoDS_Shape LastEdge = aNewEdges(aNewEdges.Extent());
125 aNewEdges.RemoveLast();
126 if (aNewEdges.FindIndex(current) != 0)
127 aNewEdges.Substitute(aNewEdges.FindIndex(current), LastEdge);
128 /////////////////////////
129 edges.Remove(i);
130 i--;
131
132 if(!isDropped) {
133 isDropped = Standard_True;
134 anIndex = i;
135 }
136 }
137 }
138
139 //add edges to the sequemce
140 for (i = 1; i <= aNewEdges.Extent(); i++)
141 edges.Append(aNewEdges(i));
142
143 return isDropped;
144}
145
146//=======================================================================
147//function : getCylinder
148//purpose : auxilary
149//=======================================================================
150static Standard_Boolean getCylinder(Handle(Geom_Surface)& theInSurface,
151 gp_Cylinder& theOutCylinder)
152{
153 Standard_Boolean isCylinder = Standard_False;
154
155 if (theInSurface->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
156 Handle(Geom_CylindricalSurface) aGC = Handle(Geom_CylindricalSurface)::DownCast(theInSurface);
157
158 theOutCylinder = aGC->Cylinder();
159 isCylinder = Standard_True;
160 }
161 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution))) {
162 Handle(Geom_SurfaceOfRevolution) aRS =
163 Handle(Geom_SurfaceOfRevolution)::DownCast(theInSurface);
164 Handle(Geom_Curve) aBasis = aRS->BasisCurve();
165 if (aBasis->IsKind(STANDARD_TYPE(Geom_Line))) {
166 Handle(Geom_Line) aBasisLine = Handle(Geom_Line)::DownCast(aBasis);
167 gp_Dir aDir = aRS->Direction();
168 gp_Dir aBasisDir = aBasisLine->Position().Direction();
169 if (aBasisDir.IsParallel(aDir, Precision::Angular())) {
170 // basis line is parallel to the revolution axis: it is a cylinder
171 gp_Pnt aLoc = aRS->Location();
172 Standard_Real aR = aBasisLine->Lin().Distance(aLoc);
173 gp_Ax3 aCylAx (aLoc, aDir);
174
175 theOutCylinder = gp_Cylinder(aCylAx, aR);
176 isCylinder = Standard_True;
177 }
178 }
179 }
180 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion))) {
181 Handle(Geom_SurfaceOfLinearExtrusion) aLES =
182 Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(theInSurface);
183 Handle(Geom_Curve) aBasis = aLES->BasisCurve();
184 if (aBasis->IsKind(STANDARD_TYPE(Geom_Circle))) {
185 Handle(Geom_Circle) aBasisCircle = Handle(Geom_Circle)::DownCast(aBasis);
186 gp_Dir aDir = aLES->Direction();
187 gp_Dir aBasisDir = aBasisCircle->Position().Direction();
188 if (aBasisDir.IsParallel(aDir, Precision::Angular())) {
189 // basis circle is normal to the extrusion axis: it is a cylinder
190 gp_Pnt aLoc = aBasisCircle->Location();
191 Standard_Real aR = aBasisCircle->Radius();
192 gp_Ax3 aCylAx (aLoc, aDir);
193
194 theOutCylinder = gp_Cylinder(aCylAx, aR);
195 isCylinder = Standard_True;
196 }
197 }
198 }
199 else {
200 }
201
202 return isCylinder;
203}
204
205//=======================================================================
206//function : ClearRts
207//purpose : auxilary
208//=======================================================================
209static Handle(Geom_Surface) ClearRts(const Handle(Geom_Surface)& aSurface)
210{
211 if(aSurface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
212 Handle(Geom_RectangularTrimmedSurface) rts =
213 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
214 return rts->BasisSurface();
215 }
216 return aSurface;
217}
218
219//=======================================================================
220//function : IsSameDomain
221//purpose :
222//=======================================================================
223static Standard_Boolean IsSameDomain(const TopoDS_Face& aFace,
224 const TopoDS_Face& aCheckedFace)
225{
226 //checking the same handles
227 TopLoc_Location L1, L2;
228 Handle(Geom_Surface) S1, S2;
229
230 S1 = BRep_Tool::Surface(aFace,L1);
231 S2 = BRep_Tool::Surface(aCheckedFace,L2);
232
233 if (S1 == S2 && L1 == L2)
234 return Standard_True;
235
236 // planar and cylindrical cases (IMP 20052)
237 Standard_Real aPrec = Precision::Confusion();
238
239 S1 = BRep_Tool::Surface(aFace);
240 S2 = BRep_Tool::Surface(aCheckedFace);
241
242 S1 = ClearRts(S1);
243 S2 = ClearRts(S2);
244
245 //Handle(Geom_OffsetSurface) aGOFS1, aGOFS2;
246 //aGOFS1 = Handle(Geom_OffsetSurface)::DownCast(S1);
247 //aGOFS2 = Handle(Geom_OffsetSurface)::DownCast(S2);
248 //if (!aGOFS1.IsNull()) S1 = aGOFS1->BasisSurface();
249 //if (!aGOFS2.IsNull()) S2 = aGOFS2->BasisSurface();
250
251 // case of two elementary surfaces: use OCCT tool
252 // elementary surfaces: ConicalSurface, CylindricalSurface,
253 // Plane, SphericalSurface and ToroidalSurface
254 if (S1->IsKind(STANDARD_TYPE(Geom_ElementarySurface)) &&
255 S2->IsKind(STANDARD_TYPE(Geom_ElementarySurface)))
256 {
257 Handle(GeomAdaptor_HSurface) aGA1 = new GeomAdaptor_HSurface(S1);
258 Handle(GeomAdaptor_HSurface) aGA2 = new GeomAdaptor_HSurface(S2);
259
260 Handle(BRepTopAdaptor_TopolTool) aTT1 = new BRepTopAdaptor_TopolTool();
261 Handle(BRepTopAdaptor_TopolTool) aTT2 = new BRepTopAdaptor_TopolTool();
262
263 try {
264 IntPatch_ImpImpIntersection anIIInt (aGA1, aTT1, aGA2, aTT2, aPrec, aPrec);
265 if (!anIIInt.IsDone() || anIIInt.IsEmpty())
266 return Standard_False;
267
268 return anIIInt.TangentFaces();
269 }
270 catch (Standard_Failure) {
271 return Standard_False;
272 }
273 }
274
275 // case of two planar surfaces:
276 // all kinds of surfaces checked, including b-spline and bezier
277 GeomLib_IsPlanarSurface aPlanarityChecker1 (S1, aPrec);
278 if (aPlanarityChecker1.IsPlanar()) {
279 GeomLib_IsPlanarSurface aPlanarityChecker2 (S2, aPrec);
280 if (aPlanarityChecker2.IsPlanar()) {
281 gp_Pln aPln1 = aPlanarityChecker1.Plan();
282 gp_Pln aPln2 = aPlanarityChecker2.Plan();
283
284 if (aPln1.Position().Direction().IsParallel(aPln2.Position().Direction(),Precision::Angular()) &&
285 aPln1.Distance(aPln2) < aPrec) {
286 return Standard_True;
287 }
288 }
289 }
290
291 // case of two cylindrical surfaces, at least one of which is a swept surface
292 // swept surfaces: SurfaceOfLinearExtrusion, SurfaceOfRevolution
293 if ((S1->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
294 S1->IsKind(STANDARD_TYPE(Geom_SweptSurface))) &&
295 (S2->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
296 S2->IsKind(STANDARD_TYPE(Geom_SweptSurface))))
297 {
298 gp_Cylinder aCyl1, aCyl2;
299 if (getCylinder(S1, aCyl1) && getCylinder(S2, aCyl2)) {
300 if (fabs(aCyl1.Radius() - aCyl2.Radius()) < aPrec) {
301 gp_Dir aDir1 = aCyl1.Position().Direction();
302 gp_Dir aDir2 = aCyl2.Position().Direction();
303 if (aDir1.IsParallel(aDir2, Precision::Angular())) {
304 gp_Pnt aLoc1 = aCyl1.Location();
305 gp_Pnt aLoc2 = aCyl2.Location();
306 gp_Vec aVec12 (aLoc1, aLoc2);
307 if (aVec12.SquareMagnitude() < aPrec*aPrec ||
308 aVec12.IsParallel(aDir1, Precision::Angular())) {
309 return Standard_True;
310 }
311 }
312 }
313 }
314 }
315
316 return Standard_False;
317}
318
319//=======================================================================
320//function : MovePCurves
321//purpose :
322//=======================================================================
323static void MovePCurves(TopoDS_Face& aTarget,
324 const TopoDS_Face& aSource)
325{
326 BRep_Builder B;
327 for(TopExp_Explorer wexp(aSource,TopAbs_WIRE);wexp.More();wexp.Next()) {
328 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(TopoDS::Wire(wexp.Current()),
329 aTarget, Precision::Confusion());
330 sfw->FixReorder();
331 Standard_Boolean isReoredFailed = sfw->StatusReorder ( ShapeExtend_FAIL );
332 sfw->FixEdgeCurves();
333 if(isReoredFailed)
334 continue;
335
336 sfw->FixShifted();
337 sfw->FixDegenerated();
338
339 // remove degenerated edges from not degenerated points
340 ShapeAnalysis_Edge sae;
341 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
342 for(Standard_Integer i = 1; i<=sewd->NbEdges();i++) {
343 TopoDS_Edge E = sewd->Edge(i);
344 if(BRep_Tool::Degenerated(E)&&!sae.HasPCurve(E,aTarget)) {
345 sewd->Remove(i);
346 i--;
347 }
348 }
349
350 TopoDS_Wire ResWire = sfw->Wire();
351 B.Add(aTarget,ResWire);
352 }
353}
354
355//=======================================================================
356//function : GlueEdgesWithPCurves
357//purpose : Glues the pcurves of the sequence of edges
358// and glues their 3d curves
359//=======================================================================
360static TopoDS_Edge GlueEdgesWithPCurves(const TopTools_SequenceOfShape& aChain,
361 const TopoDS_Vertex& FirstVertex,
362 const TopoDS_Vertex& LastVertex)
363{
364 Standard_Integer i, j;
365
366 TopoDS_Edge FirstEdge = TopoDS::Edge(aChain(1));
367 //TColGeom2d_SequenceOfCurve PCurveSeq;
368 TColGeom_SequenceOfSurface SurfSeq;
369 //TopTools_SequenceOfShape LocSeq;
370
371 BRep_ListIteratorOfListOfCurveRepresentation itr( (Handle(BRep_TEdge)::DownCast(FirstEdge.TShape()))->Curves() );
372 for (; itr.More(); itr.Next())
373 {
374 Handle(BRep_CurveRepresentation) CurveRep = itr.Value();
375 if (CurveRep->IsCurveOnSurface())
376 {
377 //PCurveSeq.Append(CurveRep->PCurve());
378 SurfSeq.Append(CurveRep->Surface());
379 /*
380 TopoDS_Shape aLocShape;
381 aLocShape.Location(CurveRep->Location());
382 LocSeq.Append(aLocShape);
383 */
384 }
385 }
386
387 Standard_Real fpar, lpar;
388 BRep_Tool::Range(FirstEdge, fpar, lpar);
389 TopoDS_Edge PrevEdge = FirstEdge;
390 TopoDS_Vertex CV;
391 Standard_Real MaxTol = 0.;
392
393 TopoDS_Edge ResEdge;
394 BRep_Builder BB;
395
396 Standard_Integer nb_curve = aChain.Length(); //number of curves
397 TColGeom_Array1OfBSplineCurve tab_c3d(0,nb_curve-1); //array of the curves
398 TColStd_Array1OfReal tabtolvertex(0,nb_curve-1); //(0,nb_curve-2); //array of the tolerances
399
400 TopoDS_Vertex PrevVertex = FirstVertex;
401 for (i = 1; i <= nb_curve; i++)
402 {
403 TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
404 TopoDS_Vertex VF, VL;
405 TopExp::Vertices(anEdge, VF, VL);
406 Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
407
408 Standard_Real Tol1 = BRep_Tool::Tolerance(VF);
409 Standard_Real Tol2 = BRep_Tool::Tolerance(VL);
410 if (Tol1 > MaxTol)
411 MaxTol = Tol1;
412 if (Tol2 > MaxTol)
413 MaxTol = Tol2;
414
415 if (i > 1)
416 {
417 TopExp::CommonVertex(PrevEdge, anEdge, CV);
418 Standard_Real Tol = BRep_Tool::Tolerance(CV);
419 tabtolvertex(i-2) = Tol;
420 }
421
422 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, fpar, lpar);
423 Handle(Geom_TrimmedCurve) aTrCurve = new Geom_TrimmedCurve(aCurve, fpar, lpar);
424 tab_c3d(i-1) = GeomConvert::CurveToBSplineCurve(aTrCurve);
425 GeomConvert::C0BSplineToC1BSplineCurve(tab_c3d(i-1), Precision::Confusion());
426 if (ToReverse)
427 tab_c3d(i-1)->Reverse();
428 PrevVertex = (ToReverse)? VF : VL;
429 PrevEdge = anEdge;
430 }
431 Handle(TColGeom_HArray1OfBSplineCurve) concatcurve; //array of the concatenated curves
432 Handle(TColStd_HArray1OfInteger) ArrayOfIndices; //array of the remining Vertex
433 GeomConvert::ConcatC1(tab_c3d,
434 tabtolvertex,
435 ArrayOfIndices,
436 concatcurve,
437 Standard_False,
438 Precision::Confusion()); //C1 concatenation
439
440 if (concatcurve->Length() > 1)
441 {
442 GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
443
444 for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
445 Concat.Add( concatcurve->Value(i), MaxTol, Standard_True );
446
447 concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
448 }
449 Handle(Geom_BSplineCurve) ResCurve = concatcurve->Value(concatcurve->Lower());
450
451 TColGeom2d_SequenceOfBoundedCurve ResPCurves;
452 TopLoc_Location aLoc;
453 for (j = 1; j <= SurfSeq.Length(); j++)
454 {
455 TColGeom2d_Array1OfBSplineCurve tab_c2d(0,nb_curve-1); //array of the pcurves
456
457 PrevVertex = FirstVertex;
458 PrevEdge = FirstEdge;
459 //TopLoc_Location theLoc = LocSeq(j).Location();
460 for (i = 1; i <= nb_curve; i++)
461 {
462 TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
463 TopoDS_Vertex VF, VL;
464 TopExp::Vertices(anEdge, VF, VL);
465 Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
466
467 /*
468 Handle(Geom2d_Curve) aPCurve =
469 BRep_Tool::CurveOnSurface(anEdge, SurfSeq(j), anEdge.Location()*theLoc, fpar, lpar);
470 */
471 Handle(Geom2d_Curve) aPCurve =
472 BRep_Tool::CurveOnSurface(anEdge, SurfSeq(j), aLoc, fpar, lpar);
473 Handle(Geom2d_TrimmedCurve) aTrPCurve = new Geom2d_TrimmedCurve(aPCurve, fpar, lpar);
474 tab_c2d(i-1) = Geom2dConvert::CurveToBSplineCurve(aTrPCurve);
475 Geom2dConvert::C0BSplineToC1BSplineCurve(tab_c2d(i-1), Precision::Confusion());
476 if (ToReverse)
477 tab_c2d(i-1)->Reverse();
478 PrevVertex = (ToReverse)? VF : VL;
479 PrevEdge = anEdge;
480 }
481 Handle(TColGeom2d_HArray1OfBSplineCurve) concatc2d; //array of the concatenated curves
482 Handle(TColStd_HArray1OfInteger) ArrayOfInd2d; //array of the remining Vertex
483 Geom2dConvert::ConcatC1(tab_c2d,
484 tabtolvertex,
485 ArrayOfInd2d,
486 concatc2d,
487 Standard_False,
488 Precision::Confusion()); //C1 concatenation
489
490 if (concatc2d->Length() > 1)
491 {
492 Geom2dConvert_CompCurveToBSplineCurve Concat2d(concatc2d->Value(concatc2d->Lower()));
493
494 for (i = concatc2d->Lower()+1; i <= concatc2d->Upper(); i++)
495 Concat2d.Add( concatc2d->Value(i), MaxTol, Standard_True );
496
497 concatc2d->SetValue(concatc2d->Lower(), Concat2d.BSplineCurve());
498 }
499 Handle(Geom2d_BSplineCurve) aResPCurve = concatc2d->Value(concatc2d->Lower());
500 ResPCurves.Append(aResPCurve);
501 }
502
503 ResEdge = BRepLib_MakeEdge(ResCurve,
504 FirstVertex, LastVertex,
505 ResCurve->FirstParameter(), ResCurve->LastParameter());
506 BB.SameRange(ResEdge, Standard_False);
507 BB.SameParameter(ResEdge, Standard_False);
508 for (j = 1; j <= ResPCurves.Length(); j++)
509 {
510 BB.UpdateEdge(ResEdge, ResPCurves(j), SurfSeq(j), aLoc, MaxTol);
511 BB.Range(ResEdge, SurfSeq(j), aLoc, ResPCurves(j)->FirstParameter(), ResPCurves(j)->LastParameter());
512 }
513
514 BRepLib::SameParameter(ResEdge, MaxTol, Standard_True);
515
516 return ResEdge;
517}
518
519//=======================================================================
520//function : MergeEdges
521//purpose : auxilary
522//=======================================================================
523static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
35e08fe8 524 const TopoDS_Face& /*aFace*/,
2277323d 525 const Standard_Real Tol,
526 const Standard_Boolean ConcatBSplines,
527 TopoDS_Edge& anEdge)
528{
529 // make chain for union
530 BRep_Builder B;
531 ShapeAnalysis_Edge sae;
532 TopoDS_Edge FirstE = TopoDS::Edge(SeqEdges.Value(1));
533 TopoDS_Edge LastE = FirstE;
534 TopoDS_Vertex VF = sae.FirstVertex(FirstE);
535 TopoDS_Vertex VL = sae.LastVertex(LastE);
536 TopTools_SequenceOfShape aChain;
537 aChain.Append(FirstE);
538 TColStd_MapOfInteger IndUsedEdges;
539 IndUsedEdges.Add(1);
540 Standard_Integer j;
541 for (j = 1; j <= SeqEdges.Length(); j++)
542 {
543 TopoDS_Edge anEdge = TopoDS::Edge(SeqEdges(j));
544 if (BRep_Tool::Degenerated(anEdge))
545 return Standard_False;
546 }
547
548 for(j=2; j<=SeqEdges.Length(); j++) {
549 for(Standard_Integer k=2; k<=SeqEdges.Length(); k++) {
550 if(IndUsedEdges.Contains(k)) continue;
551 TopoDS_Edge edge = TopoDS::Edge(SeqEdges.Value(k));
552 TopoDS_Vertex VF2 = sae.FirstVertex(edge);
553 TopoDS_Vertex VL2 = sae.LastVertex(edge);
554 if(sae.FirstVertex(edge).IsSame(VL)) {
555 aChain.Append(edge);
556 LastE = edge;
557 VL = sae.LastVertex(LastE);
558 IndUsedEdges.Add(k);
559 }
560 else if(sae.LastVertex(edge).IsSame(VF)) {
561 aChain.Prepend(edge);
562 FirstE = edge;
563 VF = sae.FirstVertex(FirstE);
564 IndUsedEdges.Add(k);
565 }
566 }
567 }
568 if(aChain.Length()<SeqEdges.Length()) {
0797d9d3 569#ifdef OCCT_DEBUG
2277323d 570 cout<<"can not create correct chain..."<<endl;
571#endif
572 return Standard_False;
573 }
574 // union edges in chain
575 // first step: union lines and circles
2277323d 576 Standard_Real fp1,lp1,fp2,lp2;
577 for(j=1; j<aChain.Length(); j++) {
578 TopoDS_Edge edge1 = TopoDS::Edge(aChain.Value(j));
b4d4dbe8 579 Handle(Geom_Curve) c3d1 = BRep_Tool::Curve(edge1,fp1,lp1);
580
2277323d 581 if(c3d1.IsNull()) break;
582 while(c3d1->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
583 Handle(Geom_TrimmedCurve) tc =
584 Handle(Geom_TrimmedCurve)::DownCast(c3d1);
585 c3d1 = tc->BasisCurve();
586 }
587 TopoDS_Edge edge2 = TopoDS::Edge(aChain.Value(j+1));
b4d4dbe8 588 Handle(Geom_Curve) c3d2 = BRep_Tool::Curve(edge2,fp2,lp2);
2277323d 589 if(c3d2.IsNull()) break;
590 while(c3d2->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
591 Handle(Geom_TrimmedCurve) tc =
592 Handle(Geom_TrimmedCurve)::DownCast(c3d2);
593 c3d2 = tc->BasisCurve();
594 }
595 if( c3d1->IsKind(STANDARD_TYPE(Geom_Line)) && c3d2->IsKind(STANDARD_TYPE(Geom_Line)) ) {
596 // union lines
597 Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(c3d1);
598 Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(c3d2);
599 gp_Dir Dir1 = L1->Position().Direction();
600 gp_Dir Dir2 = L2->Position().Direction();
601 //if(!Dir1.IsEqual(Dir2,Precision::Angular())) {
602 //if(!Dir1.IsParallel(Dir2,Precision::Angular())) {
603 if(!Dir1.IsParallel(Dir2,Tol)) {
b4d4dbe8 604 return Standard_False;
2277323d 605 }
606 // can union lines => create new edge
607 TopoDS_Vertex V1 = sae.FirstVertex(edge1);
608 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
609 TopoDS_Vertex V2 = sae.LastVertex(edge2);
610 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
611 gp_Vec Vec(PV1,PV2);
612 Handle(Geom_Line) L = new Geom_Line(gp_Ax1(PV1,Vec));
613 Standard_Real dist = PV1.Distance(PV2);
614 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(L,0.0,dist);
615 TopoDS_Edge E;
616 B.MakeEdge (E,tc,Precision::Confusion());
617 B.Add (E,V1); B.Add (E,V2);
618 B.UpdateVertex(V1, 0., E, 0.);
619 B.UpdateVertex(V2, dist, E, 0.);
620 //ShapeFix_Edge sfe;
621 //sfe.FixAddPCurve(E,aFace,Standard_False);
622 //sfe.FixSameParameter(E);
623 aChain.Remove(j);
624 aChain.SetValue(j,E);
625 j--;
626 }
627 if( c3d1->IsKind(STANDARD_TYPE(Geom_Circle)) && c3d2->IsKind(STANDARD_TYPE(Geom_Circle)) ) {
628 // union circles
629 Handle(Geom_Circle) C1 = Handle(Geom_Circle)::DownCast(c3d1);
630 Handle(Geom_Circle) C2 = Handle(Geom_Circle)::DownCast(c3d2);
631 gp_Pnt P01 = C1->Location();
632 gp_Pnt P02 = C2->Location();
b4d4dbe8 633 if (P01.Distance(P02) > Precision::Confusion())
634 return Standard_False;
2277323d 635 // can union circles => create new edge
636 TopoDS_Vertex V1 = sae.FirstVertex(edge1);
637 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
638 TopoDS_Vertex V2 = sae.LastVertex(edge2);
639 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
640 TopoDS_Vertex VM = sae.LastVertex(edge1);
641 gp_Pnt PVM = BRep_Tool::Pnt(VM);
642 GC_MakeCircle MC (PV1,PVM,PV2);
2277323d 643 TopoDS_Edge E;
b4d4dbe8 644 if (!MC.IsDone() || MC.Value().IsNull()) {
2277323d 645 // jfa for Mantis issue 0020228
b4d4dbe8 646 if (PV1.Distance(PV2) > Precision::Confusion())
647 return Standard_False;
2277323d 648 // closed chain
b4d4dbe8 649 B.MakeEdge (E,C1,Precision::Confusion());
2277323d 650 B.Add(E,V1);
651 B.Add(E,V2);
652 }
653 else {
b4d4dbe8 654 Handle(Geom_Circle) C = MC.Value();
2277323d 655 gp_Pnt P0 = C->Location();
656 gp_Dir D1(gp_Vec(P0,PV1));
657 gp_Dir D2(gp_Vec(P0,PV2));
658 Standard_Real fpar = C->XAxis().Direction().Angle(D1);
659 if(fabs(fpar)>Precision::Confusion()) {
660 // check orientation
661 gp_Dir ND = C->XAxis().Direction().Crossed(D1);
662 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
663 fpar = -fpar;
664 }
665 }
666 Standard_Real lpar = C->XAxis().Direction().Angle(D2);
667 if(fabs(lpar)>Precision::Confusion()) {
668 // check orientation
669 gp_Dir ND = C->XAxis().Direction().Crossed(D2);
670 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
671 lpar = -lpar;
672 }
673 }
674 if (lpar < fpar) lpar += 2*M_PI;
675 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
676 B.MakeEdge (E,tc,Precision::Confusion());
677 B.Add(E,V1);
678 B.Add(E,V2);
679 B.UpdateVertex(V1, fpar, E, 0.);
680 B.UpdateVertex(V2, lpar, E, 0.);
681 }
682 aChain.Remove(j);
683 aChain.SetValue(j,E);
684 j--;
685 }
686 }
687 if (j < aChain.Length()) {
0797d9d3 688#ifdef OCCT_DEBUG
2277323d 689 cout<<"null curve3d in edge..."<<endl;
690#endif
691 return Standard_False;
692 }
693 if (aChain.Length() > 1 && ConcatBSplines) {
694 // second step: union edges with various curves
695 // skl for bug 0020052 from Mantis: perform such unions
696 // only if curves are bspline or bezier
697 bool NeedUnion = true;
698 for(j=1; j<=aChain.Length(); j++) {
699 TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
b4d4dbe8 700 TopLoc_Location Loc;
2277323d 701 Handle(Geom_Curve) c3d = BRep_Tool::Curve(edge,Loc,fp1,lp1);
702 if(c3d.IsNull()) continue;
703 while(c3d->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
704 Handle(Geom_TrimmedCurve) tc =
705 Handle(Geom_TrimmedCurve)::DownCast(c3d);
706 c3d = tc->BasisCurve();
707 }
708 if( ( c3d->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ||
709 c3d->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ) ) continue;
710 NeedUnion = false;
711 break;
712 }
713 if(NeedUnion) {
0797d9d3 714#ifdef OCCT_DEBUG
2277323d 715 cout<<"can not make analitical union => make approximation"<<endl;
716#endif
717 TopoDS_Edge E = GlueEdgesWithPCurves(aChain, VF, VL);
2277323d 718 aChain.SetValue(1,E);
719 }
720 else {
0797d9d3 721#ifdef OCCT_DEBUG
2277323d 722 cout<<"can not make approximation for such types of curves"<<endl;
723#endif
724 return Standard_False;
725 }
726 }
727
728 anEdge = TopoDS::Edge(aChain.Value(1));
729 return Standard_True;
730}
731
732//=======================================================================
733//function : ShapeUpgrade_UnifySameDomain
734//purpose : Constructor
735//=======================================================================
736
737ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain()
738{
739 myUnifyEdges = Standard_True;
740 myUnifyFaces = Standard_True;
741 myConcatBSplines = Standard_False;
742
743 myContext = new ShapeBuild_ReShape;
744}
745
746//=======================================================================
747//function : ShapeUpgrade_UnifySameDomain
748//purpose : Constructor
749//=======================================================================
750
751ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain(const TopoDS_Shape& aShape,
752 const Standard_Boolean UnifyEdges,
753 const Standard_Boolean UnifyFaces,
754 const Standard_Boolean ConcatBSplines)
755{
756 myInitShape = aShape;
757 myShape = aShape;
758 myUnifyEdges = UnifyEdges;
759 myUnifyFaces = UnifyFaces;
760 myConcatBSplines = ConcatBSplines;
761
762 myContext = new ShapeBuild_ReShape;
763}
764
765//=======================================================================
766//function : Initialize
767//purpose :
768//=======================================================================
769
770void ShapeUpgrade_UnifySameDomain::Initialize(const TopoDS_Shape& aShape,
771 const Standard_Boolean UnifyEdges,
772 const Standard_Boolean UnifyFaces,
773 const Standard_Boolean ConcatBSplines)
774{
775 myInitShape = aShape;
776 myShape = aShape;
777 myUnifyEdges = UnifyEdges;
778 myUnifyFaces = UnifyFaces;
779 myConcatBSplines = ConcatBSplines;
780
781 myContext->Clear();
782 //myOldNewMap.Clear();
783 //myGenerated.Clear();
784}
785
786//=======================================================================
787//function : UnifyFaces
788//purpose :
789//=======================================================================
790
791void ShapeUpgrade_UnifySameDomain::UnifyFaces()
792{
793 //Handle(ShapeBuild_ReShape) myContext = new ShapeBuild_ReShape;
794 TopoDS_Shape aResShape = myContext->Apply(myShape);
795
796 // processing each shell
797 TopExp_Explorer exps;
798 for (exps.Init(myShape, TopAbs_SHELL); exps.More(); exps.Next()) {
799 TopoDS_Shell aShell = TopoDS::Shell(exps.Current());
800
801 // creating map of edge faces
802 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
803 TopExp::MapShapesAndAncestors(aShell, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
804
805 // map of processed shapes
806 TopTools_MapOfShape aProcessed;
807
808 //Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
809
810 Standard_Integer NbModif = 0;
811 Standard_Boolean hasFailed = Standard_False;
812 Standard_Real tol = Precision::Confusion();
813
814 // count faces
815 Standard_Integer nbf = 0;
816 TopExp_Explorer exp;
817 TopTools_MapOfShape mapF;
818 for (exp.Init(aShell, TopAbs_FACE); exp.More(); exp.Next()) {
819 if (mapF.Add(exp.Current()))
820 nbf++;
821 }
822
823 // processing each face
824 mapF.Clear();
825 for (exp.Init(aShell, TopAbs_FACE); exp.More(); exp.Next()) {
826 TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
827
828 if (aProcessed.Contains(aFace))
829 continue;
830
831 Standard_Integer dummy;
832 TopTools_SequenceOfShape edges;
833 AddOrdinaryEdges(edges,aFace,dummy);
834
835 TopTools_SequenceOfShape faces;
836 faces.Append(aFace);
837
838 //surface and location to construct result
839 TopLoc_Location aBaseLocation;
840 Handle(Geom_Surface) aBaseSurface = BRep_Tool::Surface(aFace,aBaseLocation);
841 aBaseSurface = ClearRts(aBaseSurface);
842
843 // find adjacent faces to union
844 Standard_Integer i;
845 for (i = 1; i <= edges.Length(); i++) {
846 TopoDS_Edge edge = TopoDS::Edge(edges(i));
847 if (BRep_Tool::Degenerated(edge))
848 continue;
849
850 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
851 TopTools_ListIteratorOfListOfShape anIter(aList);
852 for (; anIter.More(); anIter.Next()) {
853 TopoDS_Face anCheckedFace = TopoDS::Face(anIter.Value().Oriented(TopAbs_FORWARD));
854 if (anCheckedFace.IsSame(aFace))
855 continue;
856
857 if (aProcessed.Contains(anCheckedFace))
858 continue;
859
860 if (IsSameDomain(aFace,anCheckedFace)) {
861
862 if (aList.Extent() != 2) {
863 // non mainfold case is not processed
864 continue;
865 }
866
867 // replacing pcurves
868 TopoDS_Face aMockUpFace;
869 BRep_Builder B;
870 B.MakeFace(aMockUpFace,aBaseSurface,aBaseLocation,0.);
871 MovePCurves(aMockUpFace,anCheckedFace);
872
873 if (AddOrdinaryEdges(edges,aMockUpFace,dummy)) {
874 // sequence edges is modified
875 i = dummy;
876 }
877
878 faces.Append(anCheckedFace);
879 aProcessed.Add(anCheckedFace);
880 break;
881 }
882 }
883 }
884
885 // all faces collected in the sequence. Perform union of faces
886 if (faces.Length() > 1) {
887 NbModif++;
888 TopoDS_Face aResult;
889 BRep_Builder B;
890 B.MakeFace(aResult,aBaseSurface,aBaseLocation,0);
891 Standard_Integer nbWires = 0;
892
893 // connecting wires
894 while (edges.Length()>0) {
895
896 Standard_Boolean isEdge3d = Standard_False;
897 nbWires++;
898 TopTools_MapOfShape aVertices;
899 TopoDS_Wire aWire;
900 B.MakeWire(aWire);
901
902 TopoDS_Edge anEdge = TopoDS::Edge(edges(1));
903 edges.Remove(1);
904
905 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
906 B.Add(aWire,anEdge);
907 TopoDS_Vertex V1,V2;
908 TopExp::Vertices(anEdge,V1,V2);
909 aVertices.Add(V1);
910 aVertices.Add(V2);
911
912 Standard_Boolean isNewFound = Standard_False;
913 do {
914 isNewFound = Standard_False;
915 for(Standard_Integer j = 1; j <= edges.Length(); j++) {
916 anEdge = TopoDS::Edge(edges(j));
917 TopExp::Vertices(anEdge,V1,V2);
918 if(aVertices.Contains(V1) || aVertices.Contains(V2)) {
919 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
920 aVertices.Add(V1);
921 aVertices.Add(V2);
922 B.Add(aWire,anEdge);
923 edges.Remove(j);
924 j--;
925 isNewFound = Standard_True;
926 }
927 }
928 } while (isNewFound);
929
930 // sorting any type of edges
ab860031 931 aWire.Closed (BRep_Tool::IsClosed (aWire));
2277323d 932 aWire = TopoDS::Wire(myContext->Apply(aWire));
933
2277323d 934 TopoDS_Face tmpF = TopoDS::Face(myContext->Apply(faces(1).Oriented(TopAbs_FORWARD)));
935 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(aWire,tmpF,Precision::Confusion());
936 sfw->FixReorder();
937 Standard_Boolean isDegRemoved = Standard_False;
938 if(!sfw->StatusReorder ( ShapeExtend_FAIL )) {
939 // clear degenerated edges if at least one with 3d curve exist
940 if(isEdge3d) {
941 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
942 for(Standard_Integer j = 1; j<=sewd->NbEdges();j++) {
943 TopoDS_Edge E = sewd->Edge(j);
944 if(BRep_Tool::Degenerated(E)) {
945 sewd->Remove(j);
946 isDegRemoved = Standard_True;
947 j--;
948 }
949 }
950 }
951 sfw->FixShifted();
952 if(isDegRemoved)
953 sfw->FixDegenerated();
954 }
955 TopoDS_Wire aWireFixed = sfw->Wire();
956 //aContext->Replace(aWire,aWireFixed);
957 myContext->Replace(aWire,aWireFixed);
958 //for history
959 /*
960 if (!myOldNewMap.IsBound(aWire))
961 {
962 TopTools_ListOfShape EmptyList;
963 myOldNewMap.Bind(aWire, EmptyList);
964 }
965 myOldNewMap(aWire).Clear();
966 myOldNewMap(aWire).Append(aWireFixed);
967 */
968 /////////////
969
970 // add resulting wire
971 if(isEdge3d) {
972 B.Add(aResult,aWireFixed);
973 }
974 else {
975 // sorting edges
976 Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
977 Standard_Integer nbEdges = sbwd->NbEdges();
978 // sort degenerated edges and create one edge instead of several ones
979 ShapeAnalysis_WireOrder sawo(Standard_False, 0);
980 ShapeAnalysis_Edge sae;
981 Standard_Integer aLastEdge = nbEdges;
982 for(Standard_Integer j = 1; j <= nbEdges; j++) {
983 Standard_Real f,l;
984 //smh protection on NULL pcurve
985 Handle(Geom2d_Curve) c2d;
986 if(!sae.PCurve(sbwd->Edge(j),tmpF,c2d,f,l)) {
987 aLastEdge--;
988 continue;
989 }
990 sawo.Add(c2d->Value(f).XY(),c2d->Value(l).XY());
991 }
992 sawo.Perform();
993
994 // constructind one degenerative edge
995 gp_XY aStart, anEnd, tmp;
996 Standard_Integer nbFirst = sawo.Ordered(1);
997 TopoDS_Edge anOrigE = TopoDS::Edge(sbwd->Edge(nbFirst).Oriented(TopAbs_FORWARD));
998 ShapeBuild_Edge sbe;
999 TopoDS_Vertex aDummyV;
1000 TopoDS_Edge E = sbe.CopyReplaceVertices(anOrigE,aDummyV,aDummyV);
1001 sawo.XY(nbFirst,aStart,tmp);
1002 sawo.XY(sawo.Ordered(aLastEdge),tmp,anEnd);
1003
1004 gp_XY aVec = anEnd-aStart;
1005 Handle(Geom2d_Line) aLine = new Geom2d_Line(aStart,gp_Dir2d(anEnd-aStart));
1006
1007 B.UpdateEdge(E,aLine,tmpF,0.);
1008 B.Range(E,tmpF,0.,aVec.Modulus());
1009 Handle(Geom_Curve) C3d;
1010 B.UpdateEdge(E,C3d,0.);
1011 B.Degenerated(E,Standard_True);
1012 TopoDS_Wire aW;
1013 B.MakeWire(aW);
1014 B.Add(aW,E);
ab860031 1015 aW.Closed (Standard_True);
2277323d 1016 B.Add(aResult,aW);
1017 }
1018 }
1019
1020 // perform substitution of face
1021 //aContext->Replace(aContext->Apply(aFace),aResult);
1022 myContext->Replace(myContext->Apply(aFace),aResult);
1023 //for history
1024 /*
1025 if (!myOldNewMap.IsBound(aFace))
1026 {
1027 TopTools_ListOfShape EmptyList;
1028 myOldNewMap.Bind(aFace, EmptyList);
1029 }
1030 myOldNewMap(aFace).Clear();
1031 myOldNewMap(aFace).Append(aResult);
1032 */
1033 /////////////
1034
1035 ShapeFix_Face sff (aResult);
1036 //Intializing by tolerances
1037 sff.SetPrecision(Precision::Confusion());
1038 sff.SetMinTolerance(tol);
1039 sff.SetMaxTolerance(1.);
1040 //Setting modes
1041 sff.FixOrientationMode() = 0;
1042 //sff.FixWireMode() = 0;
1043 //sff.SetContext(aContext);
1044 sff.SetContext(myContext);
1045 // Applying the fixes
1046 sff.Perform();
1047 if(sff.Status(ShapeExtend_FAIL))
1048 hasFailed = Standard_True;
1049
1050 // breaking down to several faces
1051 //TopoDS_Shape theResult = aContext->Apply(aResult);
1052 TopoDS_Shape theResult = myContext->Apply(aResult);
1053 for (TopExp_Explorer aFaceExp (theResult,TopAbs_FACE); aFaceExp.More(); aFaceExp.Next()) {
1054 TopoDS_Face aCurrent = TopoDS::Face(aFaceExp.Current().Oriented(TopAbs_FORWARD));
1055 Handle(TColGeom_HArray2OfSurface) grid = new TColGeom_HArray2OfSurface ( 1, 1, 1, 1 );
1056 grid->SetValue ( 1, 1, aBaseSurface );
1057 Handle(ShapeExtend_CompositeSurface) G = new ShapeExtend_CompositeSurface ( grid );
1058 ShapeFix_ComposeShell CompShell;
1059 CompShell.Init ( G, aBaseLocation, aCurrent, ::Precision::Confusion() );//myPrecision
1060 //CompShell.SetContext( aContext );
1061 CompShell.SetContext( myContext );
1062
1063 TopTools_SequenceOfShape parts;
1064 ShapeFix_SequenceOfWireSegment wires;
1065 for(TopExp_Explorer W_Exp(aCurrent,TopAbs_WIRE);W_Exp.More();W_Exp.Next()) {
1066 Handle(ShapeExtend_WireData) sbwd =
1067 new ShapeExtend_WireData ( TopoDS::Wire(W_Exp.Current() ));
1068 ShapeFix_WireSegment seg ( sbwd, TopAbs_REVERSED );
1069 wires.Append(seg);
1070 }
1071
1072 CompShell.DispatchWires ( parts,wires );
1073 for (Standard_Integer j=1; j <= parts.Length(); j++ ) {
1074 ShapeFix_Face aFixOrient(TopoDS::Face(parts(j)));
1075 //aFixOrient.SetContext(aContext);
1076 aFixOrient.SetContext(myContext);
1077 aFixOrient.FixOrientation();
1078 }
1079
1080 TopoDS_Shape CompRes;
1081 if ( faces.Length() !=1 ) {
1082 TopoDS_Shell S;
1083 B.MakeShell ( S );
1084 for ( i=1; i <= parts.Length(); i++ )
1085 B.Add ( S, parts(i) );
ab860031 1086 S.Closed (BRep_Tool::IsClosed (S));
2277323d 1087 CompRes = S;
1088 }
1089 else CompRes = parts(1);
1090
1091 //aContext->Replace(aCurrent,CompRes);
1092 myContext->Replace(aCurrent,CompRes);
1093 //for history
1094 /*
1095 if (!myOldNewMap.IsBound(aCurrent))
1096 {
1097 TopTools_ListOfShape EmptyList;
1098 myOldNewMap.Bind(aCurrent, EmptyList);
1099 }
1100 myOldNewMap(aCurrent).Clear();
1101 myOldNewMap(aCurrent).Append(CompRes);
1102 */
1103 /////////////
1104 }
1105
1106 // remove the remaining faces
1107 for(i = 2; i <= faces.Length(); i++)
1108 //aContext->Remove(faces(i));
1109 myContext->Remove(faces(i));
1110 }
1111 } // end processing each face
1112
1113 //TopoDS_Shape aResult = Shape;
1114 if (NbModif > 0 && !hasFailed) {
1115 //TopoDS_Shape aResult = aContext->Apply(aShell);
1116 TopoDS_Shape aResult = myContext->Apply(aShell);
1117
1118 ShapeFix_Edge sfe;
1119 for (exp.Init(aResult,TopAbs_EDGE); exp.More(); exp.Next()) {
1120 TopoDS_Edge E = TopoDS::Edge(exp.Current());
1121 sfe.FixVertexTolerance (E);
1122 // ptv add fix same parameter
1123 sfe.FixSameParameter(E, Precision::Confusion());
1124 }
1125
1126 myContext->Replace(aShell, aResult);
1127 //for history
1128 /*
1129 if (!myOldNewMap.IsBound(aShell))
1130 {
1131 TopTools_ListOfShape EmptyList;
1132 myOldNewMap.Bind(aShell, EmptyList);
1133 }
1134 myOldNewMap(aShell).Clear();
1135 myOldNewMap(aShell).Append(aResult);
1136 */
1137 /////////////
1138 }
1139 //else
1140 {
1141 for (exp.Init(aShell, TopAbs_FACE); exp.More(); exp.Next()) {
1142 TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
1143 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
1144 sfw->SetContext(myContext);
1145 sfw->SetPrecision(Precision::Confusion());
1146 sfw->SetMinTolerance(Precision::Confusion());
1147 sfw->SetMaxTolerance(1.);
1148 sfw->SetFace(aFace);
1149 for (TopoDS_Iterator iter (aFace,Standard_False); iter.More(); iter.Next()) {
1150 TopoDS_Wire wire = TopoDS::Wire(iter.Value());
1151 sfw->Load(wire);
1152 sfw->FixReorder();
1153 sfw->FixShifted();
1154 }
1155 }
1156 }
1157 } // end processing each shell
1158
1159 myShape = myContext->Apply(myShape);
1160}
1161
1162//=======================================================================
1163//function : UnifyEdges
1164//purpose :
1165//=======================================================================
1166
1167void ShapeUpgrade_UnifySameDomain::UnifyEdges()
1168{
1169 Standard_Real Tol = Precision::Confusion();
1170
1171 //Handle(ShapeBuild_ReShape) myContext = new ShapeBuild_ReShape;
1172 Standard_Real myTolerance = Precision::Confusion();
1173 TopoDS_Shape aResult = myContext->Apply(myShape);
1174
ce41efde 1175 TopTools_IndexedMapOfShape ChangedFaces;
2277323d 1176
ce41efde 1177 // creating map of edge faces
1178 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
1179 TopExp::MapShapesAndAncestors(myShape, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
1180
1181 // creating map of vertex edges
1182 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgesVertex;
1183 TopExp::MapShapesAndAncestors(myShape, TopAbs_VERTEX, TopAbs_EDGE, aMapEdgesVertex);
1184
1185 //Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
1186 TopoDS_Shape aRes = myShape;
1187 //aRes = aContext->Apply(aSolid);
1188 aRes = myContext->Apply(myShape);
1189
1190 // processing each face
1191 TopExp_Explorer exp;
1192 for (exp.Init(aRes, TopAbs_FACE); exp.More(); exp.Next()) {
1193 //TopoDS_Face aFace = TopoDS::Face(aContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
1194 TopoDS_Face aFace = TopoDS::Face(myContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
1195 TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
1196
1197 for (TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
1198 TopoDS_Edge edge = TopoDS::Edge(expe.Current());
1199 if (!aMapEdgeFaces.Contains(edge)) continue;
1200 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
1201 TopTools_ListIteratorOfListOfShape anIter(aList);
1202 for ( ; anIter.More(); anIter.Next()) {
1203 TopoDS_Face face = TopoDS::Face(anIter.Value());
1204 //TopoDS_Face face1 = TopoDS::Face(aContext->Apply(anIter.Value()));
1205 TopoDS_Face face1 = TopoDS::Face(myContext->Apply(anIter.Value()));
1206 if (face1.IsSame(aFace)) continue;
1207 if (aMapFacesEdges.Contains(face)) {
1208 aMapFacesEdges.ChangeFromKey(face).Append(edge);
2277323d 1209 }
ce41efde 1210 else {
1211 TopTools_ListOfShape ListEdges;
1212 ListEdges.Append(edge);
1213 aMapFacesEdges.Add(face,ListEdges);
2277323d 1214 }
1215 }
ce41efde 1216 }
2277323d 1217
ce41efde 1218 for (Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++) {
1219 const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
1220 TopTools_SequenceOfShape SeqEdges;
1221 ShapeAnalysis_Edge sae;
1222 TopTools_ListIteratorOfListOfShape anIter(ListEdges);
1223 Standard_Boolean IsSharedVertexPresent = Standard_False;
1224 for ( ; anIter.More(); anIter.Next())
1225 SeqEdges.Append(anIter.Value());
1226 if (SeqEdges.Length()==1)
1227 continue;
1228 TopTools_SequenceOfShape SeqVertexes;
1229 TopTools_MapOfShape MapVertexes;
1230 for (int k = 1; k <= SeqEdges.Length(); k++ )
1231 {
1232 TopoDS_Vertex aV1 = sae.FirstVertex(TopoDS::Edge(SeqEdges(k)));
1233 TopoDS_Vertex aV2 = sae.LastVertex(TopoDS::Edge(SeqEdges(k)));
1234 if (!MapVertexes.Add(aV1))
1235 SeqVertexes.Append(aV1);
1236 if (!MapVertexes.Add(aV2))
1237 SeqVertexes.Append(aV2);
1238 }
2277323d 1239
ce41efde 1240 for (Standard_Integer k = 1; k <= SeqVertexes.Length() && !IsSharedVertexPresent; k++ )
2277323d 1241 {
ce41efde 1242 const TopTools_ListOfShape& ListEdgesV1 = aMapEdgesVertex.FindFromKey(SeqVertexes(k));
1243 TopTools_MapOfShape aMapOfEdges;
1244 TopTools_ListIteratorOfListOfShape iter(ListEdgesV1);
1245 for (; iter.More(); iter.Next())
1246 aMapOfEdges.Add(iter.Value());
1247 if (aMapOfEdges.Extent() > 2)
1248 IsSharedVertexPresent = Standard_True;
2277323d 1249 }
2277323d 1250
ce41efde 1251 if (IsSharedVertexPresent)
1252 continue;
1253 TopoDS_Edge E;
1254 if ( MergeEdges(SeqEdges,aFace,Tol,myConcatBSplines,E) ) {
1255 // now we have only one edge - aChain.Value(1)
1256 // we have to replace old ListEdges with this new edge
1257 //aContext->Replace(SeqEdges(1),E);
1258 myContext->Replace(SeqEdges(1),E);
1259 Standard_Integer j;
1260 for (j = 2; j <= SeqEdges.Length(); j++) {
1261 //aContext->Remove(SeqEdges(j));
1262 myContext->Remove(SeqEdges(j));
1263 //myOldNewMap.Bind(SeqEdges(j), E);
1264 }
2277323d 1265 //for history
1266 /*
ce41efde 1267 for (j = 1; j <= SeqEdges.Length(); j++)
2277323d 1268 {
ce41efde 1269 if (!myOldNewMap.IsBound(SeqEdges(j)))
1270 {
1271 TopTools_ListOfShape EmptyList;
1272 myOldNewMap.Bind(SeqEdges(j), EmptyList);
1273 }
1274 myOldNewMap(SeqEdges(j)).Clear();
1275 myOldNewMap(SeqEdges(j)).Append(E);
2277323d 1276 }
2277323d 1277 */
1278 /////////////
ce41efde 1279
1280 TopoDS_Face tmpF = TopoDS::Face(exp.Current());
1281 if ( !ChangedFaces.Contains(tmpF) )
1282 ChangedFaces.Add(tmpF);
1283 tmpF = TopoDS::Face(aMapFacesEdges.FindKey(i));
1284 if ( !ChangedFaces.Contains(tmpF) )
1285 ChangedFaces.Add(tmpF);
2277323d 1286 }
ce41efde 1287 }
1288
1289 } // end processing each face
1290
1291 // fix changed faces and replace them in the local context
1292 for (Standard_Integer i=1; i<=ChangedFaces.Extent(); i++) {
1293 //TopoDS_Face aFace = TopoDS::Face(aContext->Apply(ChangedFaces.FindKey(i)));
1294 TopoDS_Face aFace = TopoDS::Face(myContext->Apply(ChangedFaces.FindKey(i)));
1295 Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFace);
1296 sff->SetContext(myContext);
1297 sff->SetPrecision(myTolerance);
1298 sff->SetMinTolerance(myTolerance);
1299 sff->SetMaxTolerance(Max(1.,myTolerance*1000.));
1300 sff->Perform();
1301 TopoDS_Shape aNewFace = sff->Face();
1302 //aContext->Replace(aFace,aNewFace);
1303 myContext->Replace(aFace,aNewFace);
1304 //for history
1305 /*
1306 if (!myOldNewMap.IsBound(aFace))
1307 {
1308 TopTools_ListOfShape EmptyList;
1309 myOldNewMap.Bind(aFace, EmptyList);
1310 }
1311 myOldNewMap(aFace).Clear();
1312 myOldNewMap(aFace).Append(aNewFace);
1313 */
1314 /////////////
1315 }
1316
1317 if (ChangedFaces.Extent() > 0) {
1318 // fix changed shell and replace it in the local context
1319 //TopoDS_Shape aRes1 = aContext->Apply(aRes);
1320 TopoDS_Shape aRes1 = myContext->Apply(aRes);
1321 TopExp_Explorer expsh;
1322 for (expsh.Init(aRes1, TopAbs_SHELL); expsh.More(); expsh.Next()) {
1323 TopoDS_Shell aShell = TopoDS::Shell(expsh.Current());
1324 Handle(ShapeFix_Shell) sfsh = new ShapeFix_Shell;
1325 sfsh->FixFaceOrientation(aShell);
1326 TopoDS_Shape aNewShell = sfsh->Shell();
1327 //aContext->Replace(aShell,aNewShell);
1328 myContext->Replace(aShell,aNewShell);
2277323d 1329 //for history
1330 /*
ce41efde 1331 if (!myOldNewMap.IsBound(aShell))
2277323d 1332 {
1333 TopTools_ListOfShape EmptyList;
ce41efde 1334 myOldNewMap.Bind(aShell, EmptyList);
2277323d 1335 }
ce41efde 1336 myOldNewMap(aShell).Clear();
1337 myOldNewMap(aShell).Append(aNewShell);
2277323d 1338 */
1339 /////////////
1340 }
ce41efde 1341 //TopoDS_Shape aRes2 = aContext->Apply(aRes1);
1342 TopoDS_Shape aRes2 = myContext->Apply(aRes1);
1343 myContext->Replace(myShape,aRes2);
1344 //for history
1345 /*
1346 if (!myOldNewMap.IsBound(aSolid))
1347 {
1348 TopTools_ListOfShape EmptyList;
1349 myOldNewMap.Bind(aSolid, EmptyList);
1350 }
1351 myOldNewMap(aSolid).Clear();
1352 myOldNewMap(aSolid).Append(aRes2);
1353 */
1354 /////////////
1355 }
2277323d 1356
1357 myShape = myContext->Apply(myShape);
1358}
1359
1360//=======================================================================
1361//function : UnifyFacesAndEdges
1362//purpose :
1363//=======================================================================
1364
1365void ShapeUpgrade_UnifySameDomain::UnifyFacesAndEdges()
1366{
1367 UnifyFaces();
1368
1369 /*
1370 ShapeUpgrade_RemoveLocations RemLoc;
1371 RemLoc.Remove(myShape);
1372 myShape = RemLoc.GetResult();
1373 */
1374
1375 UnifyEdges();
1376}
1377
1378//=======================================================================
1379//function : Build
1380//purpose : builds the resulting shape
1381//======================================================================
1382void ShapeUpgrade_UnifySameDomain::Build()
1383{
1384 if (myUnifyFaces && myUnifyEdges)
1385 UnifyFacesAndEdges();
1386
1387 else if (myUnifyEdges)
1388 UnifyEdges();
1389 else if (myUnifyFaces)
1390 UnifyFaces();
1391
1392 //Done();
1393}
1394
1395//=======================================================================
1396//function : Shape
1397//purpose : give the resulting shape
1398//=======================================================================
1399const TopoDS_Shape& ShapeUpgrade_UnifySameDomain::Shape() const
1400{
1401 return myShape;
1402}
1403
1404//=======================================================================
1405//function : Generated
1406//purpose : returns the new shape from the old one
1407//=======================================================================
1408TopoDS_Shape ShapeUpgrade_UnifySameDomain::Generated(const TopoDS_Shape& aShape) const
1409{
1410 TopoDS_Shape aNewShape = myContext->Apply(aShape);
1411 /*
1412 if (aNewShape.IsNull())
1413 aNewShape = myOldNewMap(aShape);
1414 */
1415
1416 return aNewShape;
1417}