1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: ShHealOper_CloseContour.cxx
24 // Created: 20.04.04 11:36:01
25 // Author: Galina KULIKOVA
27 #include <ShHealOper_CloseContour.hxx>
28 #include <ShapeExtend_WireData.hxx>
29 #include <ShapeFix_Wire.hxx>
30 #include <TopoDS_Face.hxx>
31 #include <ShapeFix_Edge.hxx>
32 #include <TopoDS_Iterator.hxx>
33 #include <ShapeAnalysis_Wire.hxx>
34 #include <TColStd_SequenceOfInteger.hxx>
35 #include <TopTools_IndexedMapOfShape.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopTools_ListOfShape.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
39 #include <TColStd_MapOfInteger.hxx>
42 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
43 #include <ShapeAnalysis_Edge.hxx>
44 #include <BRep_Builder.hxx>
45 #include <TopoDS_Edge.hxx>
46 #include <TopoDS_Vertex.hxx>
47 #include <BRepBuilderAPI_MakeVertex.hxx>
48 #include <ShapeAnalysis_Edge.hxx>
50 #include <gp_Vec2d.hxx>
52 #include <Geom_Line.hxx>
53 #include <Geom2d_Line.hxx>
54 #include <TopoDS_Compound.hxx>
55 #include <BRep_Tool.hxx>
56 #include <ShapeBuild_Edge.hxx>
57 #include <TopExp_Explorer.hxx>
59 //=======================================================================
60 //function : ShHealOper_CloseContour()
61 //purpose : Constructor
62 //=======================================================================
63 ShHealOper_CloseContour::ShHealOper_CloseContour (const TopoDS_Shape& theShape )
68 //=======================================================================
71 //=======================================================================
72 void ShHealOper_CloseContour::Init(const TopoDS_Shape& theShape)
74 ShHealOper_Tool::Init(theShape);
75 myTolerance = Precision::Confusion();
77 TopExp::MapShapesAndAncestors(theShape,TopAbs_EDGE,TopAbs_FACE,myMapEdgesFace);
80 //=======================================================================
83 //=======================================================================
84 Standard_Boolean ShHealOper_CloseContour::Perform(const TopTools_SequenceOfShape& theSeqEdges,
85 const Standard_Boolean theModeVertex,
86 const Standard_Boolean theModeFixGapsCurves)
89 myDone = Standard_False;
90 myErrorStatus =ShHealOper_NotError;
91 if(myInitShape.IsNull()) {
92 myErrorStatus = ShHealOper_InvalidParameters;
95 Handle(ShapeExtend_WireData) asewd = new ShapeExtend_WireData;
96 Standard_Integer i =1;
97 for ( ; i <= theSeqEdges.Length(); i++)
98 asewd->Add(theSeqEdges.Value(i));
100 myModeVertex = theModeVertex;
101 myModeFixGapsCurves = theModeFixGapsCurves;
108 //=======================================================================
111 //=======================================================================
112 Standard_Boolean ShHealOper_CloseContour::Perform(const TopoDS_Wire& theWire,
113 const Standard_Boolean theModeVertex,
114 const Standard_Boolean theModeFixGapsCurves)
117 myDone = Standard_False;
118 if(myInitShape.IsNull()) {
119 myErrorStatus = ShHealOper_InvalidParameters;
122 Handle(ShapeExtend_WireData) asewd = new ShapeExtend_WireData(theWire);
123 myModeVertex = theModeVertex;
124 myModeFixGapsCurves = theModeFixGapsCurves;
129 //=======================================================================
132 //=======================================================================
133 void ShHealOper_CloseContour::build(Handle(ShapeExtend_WireData)& theSewd)
135 TopTools_SequenceOfShape aCommonFaces;
136 //checks that all specified edges belong the one face or not.
137 Standard_Boolean isOneFace = checkOneFace(theSewd,aCommonFaces);
139 //add edge or increase max tolerance in dependance on specified VertexMode.
140 //if all edges belong the one face that gap will be closed in the 2D by line
141 //else gap will be closed in the 3D by line.
142 myDone = fixGaps(theSewd,aCommonFaces);
144 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
145 sfw->SetContext(myContext);
148 sfw->ClosedWireMode() = Standard_True;
149 sfw->SetPrecision(myTolerance);
150 sfw->SetMaxTolerance(myMaxTolerance);
153 Standard_Integer ii =1;
154 for( ; ii <= aCommonFaces.Length(); ii++) {
155 TopoDS_Face aFace = TopoDS::Face(aCommonFaces.Value(ii));
157 myDone = (sfw->Perform() || myDone);
158 //if Mode for fix gaps is equal to true that
159 //curve 3D and curve 2D will be pull to each other.
160 if(myModeFixGapsCurves) {
161 Standard_Boolean isFixgaps = Standard_False;
164 isFixgaps = sfw->StatusGaps3d(ShapeExtend_DONE);
165 if(!isFixgaps && sfw->StatusGaps3d(ShapeExtend_FAIL))
166 myErrorStatus = ShHealOper_ErrorExecution;
169 sfw->FixSelfIntersection();
170 else if(sfw->StatusGaps2d(ShapeExtend_FAIL))
171 myErrorStatus = ShHealOper_ErrorExecution;
173 isFixgaps = (isFixgaps || sfw->StatusGaps2d(ShapeExtend_DONE));
174 myDone = (myDone || isFixgaps);
176 Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
177 Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
178 for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
179 TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
180 sfe->FixVertexTolerance(aEdge,aFace);
181 sfe->FixSameParameter(aEdge);
188 myDone = (sfw->Perform() || myDone);
189 if(myModeFixGapsCurves) {
190 //if Mode for fix gaps is equal to true that
191 //curve 3D will be pull to each other.
192 if(sfw->FixGaps3d()) {
193 Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
194 Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
195 for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
196 TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
197 sfe->FixVertexTolerance(aEdge);
198 sfe->FixSameParameter(aEdge);
201 else if(sfw->StatusGaps3d(ShapeExtend_FAIL))
202 myErrorStatus = ShHealOper_ErrorExecution;
203 myDone = (sfw->StatusGaps3d(ShapeExtend_DONE) || myDone);
208 TopoDS_Shape aoldShape = myInitShape;
209 //if free edges were added they will be added to the result shape
210 //and type of result shape should be change if initial shape is not COMPOUND.
211 if( myFreeEdges.Length()) {
213 TopoDS_Compound aComp;
214 aB.MakeCompound(aComp);
215 if(aoldShape.ShapeType() == TopAbs_COMPOUND) {
216 TopoDS_Iterator aIt(aoldShape);
217 for( ;aIt.More();aIt.Next() )
218 aB.Add(aComp,aIt.Value());
221 aB.Add(aComp,aoldShape);
223 Standard_Integer i=1;
224 for ( ; i <= myFreeEdges.Length(); i++ )
225 aB.Add(aComp,myFreeEdges.Value(i));
229 myResultShape = myContext->Apply(aoldShape);
234 //=======================================================================
235 //function : checkGaps
237 //=======================================================================
238 Standard_Boolean ShHealOper_CloseContour::fixGaps(const Handle(ShapeExtend_WireData)& theWire,
239 const TopTools_SequenceOfShape& theCommonFaces)
241 Handle(ShapeAnalysis_Wire) asaw = new ShapeAnalysis_Wire;
244 Standard_Integer i =1;
245 Standard_Boolean hasGaps = Standard_False;
246 for ( ; i <= asaw->NbEdges(); i++) {
247 if(asaw->CheckGap3d(i)) {
248 Standard_Real dist = asaw->MinDistance3d();
249 if(dist > myMaxTolerance) {
250 hasGaps = Standard_True;
251 Standard_Integer ind2 = (i ==0 ? theWire->NbEdges() :i);
252 Standard_Integer ind1 = (ind2 >1 ? ind2 -1 : theWire->NbEdges());
253 TopoDS_Edge aE1= theWire->Edge(ind1);
254 TopoDS_Edge aE2= theWire->Edge(ind2);
256 buildEdge(aE1,aE2,theCommonFaces);
258 myMaxTolerance = RealLast();
259 if(ind2 == ind1) break;
267 //=======================================================================
268 //function : checkOneFace
270 //=======================================================================
271 Standard_Boolean ShHealOper_CloseContour::checkOneFace(const Handle(ShapeExtend_WireData)& theSewd,
272 TopTools_SequenceOfShape& theCommonFaces) const
274 TopTools_IndexedMapOfShape amapfaces;
275 TopoDS_Edge aEdge1 = theSewd->Edge(1);
276 Standard_Boolean isOneFace = myMapEdgesFace.Contains(aEdge1 );
280 //check that all specified edges belong to one face.
281 const TopTools_ListOfShape& alfaces = myMapEdgesFace.FindFromKey(aEdge1);
282 isOneFace = isOneFace && (!alfaces.IsEmpty());
284 return Standard_False;
286 TopTools_ListIteratorOfListOfShape litr(alfaces);
287 for( ; litr.More();litr.Next() )
288 amapfaces.Add(litr.Value());
290 TColStd_MapOfInteger amapIndex;
291 Standard_Integer ind =0;
292 Standard_Integer i =2;
293 for( ; i <= theSewd->NbEdges() && isOneFace; i++) {
294 isOneFace = myMapEdgesFace.Contains(theSewd->Edge(i));
296 const TopTools_ListOfShape& alfaces1 = myMapEdgesFace.FindFromKey(theSewd->Edge(i));
298 TColStd_MapOfInteger aTmpInd;
299 for(litr.Initialize(alfaces1) ; litr.More() ;litr.Next() ) {
300 if(amapfaces.Contains(litr.Value())) {
301 ind = amapfaces.FindIndex(litr.Value());
309 isOneFace = (amapIndex.Extent());
313 TColStd_MapIteratorOfMapOfInteger aMi(amapIndex);
314 for( ; aMi.More();aMi.Next()) {
315 if(!aTmpInd.Contains(aMi.Key()))
316 amapIndex.Remove(aMi.Key());
318 isOneFace = (amapIndex.Extent());
323 if(theSewd->NbEdges() >1) {
324 Standard_Integer j =1;
325 for( ; j <= amapfaces.Extent(); j++) {
326 if(!amapIndex.Contains(j)) continue;
327 TopoDS_Shape aF = amapfaces.FindKey(j);
328 theCommonFaces.Append(aF);
334 //=======================================================================
335 //function : buildEdge
337 //=======================================================================
338 void ShHealOper_CloseContour::buildEdge(const TopoDS_Edge& aE1,
339 const TopoDS_Edge& aE2,
340 const TopTools_SequenceOfShape& theCommonFaces)
342 ShapeAnalysis_Edge asae;
343 TopoDS_Vertex aV1 = asae.LastVertex(aE1);
344 TopoDS_Vertex aV2 = asae.FirstVertex(aE2);
345 gp_Pnt p1 = BRep_Tool::Pnt(aV1);
346 gp_Pnt p2 = BRep_Tool::Pnt(aV2);
347 BRepBuilderAPI_MakeVertex mkver1( p1 );
348 TopoDS_Vertex newV1 = mkver1.Vertex();
350 BRepBuilderAPI_MakeVertex mkver2( p2 );
351 TopoDS_Vertex newV2 = mkver2.Vertex();
353 B.UpdateVertex ( newV1,Precision::Confusion());
354 B.UpdateVertex ( newV2, Precision::Confusion());
355 Standard_Boolean isBuild = Standard_False;
359 //if all edges belong to one face that gap will be closed in the 2D by line
360 //than 3D curve will be built by 2D curve
361 if(theCommonFaces.Length()) {
363 TopoDS_Face aF1 = TopoDS::Face(theCommonFaces.Value(1));
364 Handle(Geom2d_Curve) c2d1,c2d2;
366 Standard_Real a1, b1,a2, b2;
367 if ( asae.PCurve ( aE1, aF1, c2d1, a1, b1, Standard_True ) &&
368 asae.PCurve ( aE2, aF1, c2d2, a2, b2, Standard_True )) {
370 c2d1->D0 ( b1, p2d1);
371 c2d2->D0 ( a2, p2d2);
372 gp_Vec2d v12 ( p2d1, p2d2 );
374 Handle(Geom2d_Line) aLine2d = new Geom2d_Line ( p2d1, gp_Dir2d ( v12 ) );
375 B.UpdateEdge ( edge, aLine2d, aF1, ::Precision::Confusion() );
376 B.Range ( edge, aF1, 0.0, v12.Magnitude() );
378 isBuild = sbe.BuildCurve3d ( edge );
381 //else gap will be closed in the 3D by line.
383 gp_Vec v1 ( p1, p2 );
384 Handle(Geom_Line) aLine = new Geom_Line ( p1, gp_Dir ( v1 ) );
385 B.UpdateEdge ( edge, aLine, ::Precision::Confusion() );
386 B.Range ( edge,0.0, v1.Magnitude());
388 B.Add ( edge, newV1.Oriented ( TopAbs_FORWARD ) );
389 B.Add ( edge, newV2.Oriented ( TopAbs_REVERSED ) );
390 myContext->Replace(aV1, newV1.Oriented (aV1.Orientation()));
391 myContext->Replace(aV2, newV2.Oriented (aV2.Orientation()));
393 newV1.Orientation(aV1.Orientation());
394 TopoDS_Edge newEdge = sbe.CopyReplaceVertices ( aE1,TopoDS_Vertex(), newV1);
399 TopoDS_Shape anE1 = myContext->Apply(aE1);
400 myContext->Replace(anE1,aw);
403 myFreeEdges.Append(edge);
407 //=======================================================================
408 //function : UpdateWire
410 //=======================================================================
411 void ShHealOper_CloseContour::updateWire (Handle(ShapeExtend_WireData)& sbwd)
413 Standard_Integer i=1;
414 for ( ; i <= sbwd->NbEdges(); i++ ) {
415 TopoDS_Edge E = sbwd->Edge(i);
416 TopoDS_Shape S = myContext->Apply ( E );
417 if ( S == E ) continue;
418 for ( TopExp_Explorer exp(S,TopAbs_EDGE); exp.More(); exp.Next() )
419 sbwd->Add ( exp.Current(), i++ );
420 sbwd->Remove ( i-- );
422 for ( i =1; i <= myFreeEdges.Length(); i++ ) {
423 sbwd->Add(TopoDS::Edge(myFreeEdges.Value(i)));