1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: ShHealOper_CloseContour.cxx
21 // Created: 20.04.04 11:36:01
22 // Author: Galina KULIKOVA
25 #include <ShHealOper_CloseContour.hxx>
26 #include <ShapeExtend_WireData.hxx>
27 #include <ShapeFix_Wire.hxx>
28 #include <TopoDS_Face.hxx>
29 #include <ShapeFix_Edge.hxx>
30 #include <TopoDS_Iterator.hxx>
31 #include <ShapeAnalysis_Wire.hxx>
32 #include <TColStd_SequenceOfInteger.hxx>
33 #include <TopTools_IndexedMapOfShape.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopTools_ListOfShape.hxx>
36 #include <TopTools_ListIteratorOfListOfShape.hxx>
37 #include <TColStd_MapOfInteger.hxx>
40 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
41 #include <ShapeAnalysis_Edge.hxx>
42 #include <BRep_Builder.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <BRepBuilderAPI_MakeVertex.hxx>
46 #include <ShapeAnalysis_Edge.hxx>
48 #include <gp_Vec2d.hxx>
50 #include <Geom_Line.hxx>
51 #include <Geom2d_Line.hxx>
52 #include <TopoDS_Compound.hxx>
53 #include <BRep_Tool.hxx>
54 #include <ShapeBuild_Edge.hxx>
55 #include <TopExp_Explorer.hxx>
57 //=======================================================================
58 //function : ShHealOper_CloseContour()
59 //purpose : Constructor
60 //=======================================================================
61 ShHealOper_CloseContour::ShHealOper_CloseContour (const TopoDS_Shape& theShape )
66 //=======================================================================
69 //=======================================================================
70 void ShHealOper_CloseContour::Init(const TopoDS_Shape& theShape)
72 ShHealOper_Tool::Init(theShape);
73 myTolerance = Precision::Confusion();
75 TopExp::MapShapesAndAncestors(theShape,TopAbs_EDGE,TopAbs_FACE,myMapEdgesFace);
78 //=======================================================================
81 //=======================================================================
82 Standard_Boolean ShHealOper_CloseContour::Perform(const TopTools_SequenceOfShape& theSeqEdges,
83 const Standard_Boolean theModeVertex,
84 const Standard_Boolean theModeFixGapsCurves)
87 myDone = Standard_False;
88 myErrorStatus =ShHealOper_NotError;
89 if(myInitShape.IsNull()) {
90 myErrorStatus = ShHealOper_InvalidParameters;
93 Handle(ShapeExtend_WireData) asewd = new ShapeExtend_WireData;
94 Standard_Integer i =1;
95 for ( ; i <= theSeqEdges.Length(); i++)
96 asewd->Add(theSeqEdges.Value(i));
98 myModeVertex = theModeVertex;
99 myModeFixGapsCurves = theModeFixGapsCurves;
106 //=======================================================================
109 //=======================================================================
110 Standard_Boolean ShHealOper_CloseContour::Perform(const TopoDS_Wire& theWire,
111 const Standard_Boolean theModeVertex,
112 const Standard_Boolean theModeFixGapsCurves)
115 myDone = Standard_False;
116 if(myInitShape.IsNull()) {
117 myErrorStatus = ShHealOper_InvalidParameters;
120 Handle(ShapeExtend_WireData) asewd = new ShapeExtend_WireData(theWire);
121 myModeVertex = theModeVertex;
122 myModeFixGapsCurves = theModeFixGapsCurves;
127 //=======================================================================
130 //=======================================================================
131 void ShHealOper_CloseContour::build(Handle(ShapeExtend_WireData)& theSewd)
133 TopTools_SequenceOfShape aCommonFaces;
134 //checks that all specified edges belong the one face or not.
135 Standard_Boolean isOneFace = checkOneFace(theSewd,aCommonFaces);
137 //add edge or increase max tolerance in dependance on specified VertexMode.
138 //if all edges belong the one face that gap will be closed in the 2D by line
139 //else gap will be closed in the 3D by line.
140 myDone = fixGaps(theSewd,aCommonFaces);
142 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
143 sfw->SetContext(myContext);
146 sfw->ClosedWireMode() = Standard_True;
147 sfw->SetPrecision(myTolerance);
148 sfw->SetMaxTolerance(myMaxTolerance);
151 Standard_Integer ii =1;
152 for( ; ii <= aCommonFaces.Length(); ii++) {
153 TopoDS_Face aFace = TopoDS::Face(aCommonFaces.Value(ii));
155 myDone = (sfw->Perform() || myDone);
156 //if Mode for fix gaps is equal to true that
157 //curve 3D and curve 2D will be pull to each other.
158 if(myModeFixGapsCurves) {
159 Standard_Boolean isFixgaps = Standard_False;
162 isFixgaps = sfw->StatusGaps3d(ShapeExtend_DONE);
163 if(!isFixgaps && sfw->StatusGaps3d(ShapeExtend_FAIL))
164 myErrorStatus = ShHealOper_ErrorExecution;
167 sfw->FixSelfIntersection();
168 else if(sfw->StatusGaps2d(ShapeExtend_FAIL))
169 myErrorStatus = ShHealOper_ErrorExecution;
171 isFixgaps = (isFixgaps || sfw->StatusGaps2d(ShapeExtend_DONE));
172 myDone = (myDone || isFixgaps);
174 Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
175 Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
176 for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
177 TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
178 sfe->FixVertexTolerance(aEdge,aFace);
179 sfe->FixSameParameter(aEdge);
186 myDone = (sfw->Perform() || myDone);
187 if(myModeFixGapsCurves) {
188 //if Mode for fix gaps is equal to true that
189 //curve 3D will be pull to each other.
190 if(sfw->FixGaps3d()) {
191 Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
192 Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
193 for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
194 TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
195 sfe->FixVertexTolerance(aEdge);
196 sfe->FixSameParameter(aEdge);
199 else if(sfw->StatusGaps3d(ShapeExtend_FAIL))
200 myErrorStatus = ShHealOper_ErrorExecution;
201 myDone = (sfw->StatusGaps3d(ShapeExtend_DONE) || myDone);
206 TopoDS_Shape aoldShape = myInitShape;
207 //if free edges were added they will be added to the result shape
208 //and type of result shape should be change if initial shape is not COMPOUND.
209 if( myFreeEdges.Length()) {
211 TopoDS_Compound aComp;
212 aB.MakeCompound(aComp);
213 if(aoldShape.ShapeType() == TopAbs_COMPOUND) {
214 TopoDS_Iterator aIt(aoldShape);
215 for( ;aIt.More();aIt.Next() )
216 aB.Add(aComp,aIt.Value());
219 aB.Add(aComp,aoldShape);
221 Standard_Integer i=1;
222 for ( ; i <= myFreeEdges.Length(); i++ )
223 aB.Add(aComp,myFreeEdges.Value(i));
227 myResultShape = myContext->Apply(aoldShape);
232 //=======================================================================
233 //function : checkGaps
235 //=======================================================================
236 Standard_Boolean ShHealOper_CloseContour::fixGaps(const Handle(ShapeExtend_WireData)& theWire,
237 const TopTools_SequenceOfShape& theCommonFaces)
239 Handle(ShapeAnalysis_Wire) asaw = new ShapeAnalysis_Wire;
242 Standard_Integer i =1;
243 Standard_Boolean hasGaps = Standard_False;
244 for ( ; i <= asaw->NbEdges(); i++) {
245 if(asaw->CheckGap3d(i)) {
246 Standard_Real dist = asaw->MinDistance3d();
247 if(dist > myMaxTolerance) {
248 hasGaps = Standard_True;
249 Standard_Integer ind2 = (i ==0 ? theWire->NbEdges() :i);
250 Standard_Integer ind1 = (ind2 >1 ? ind2 -1 : theWire->NbEdges());
251 TopoDS_Edge aE1= theWire->Edge(ind1);
252 TopoDS_Edge aE2= theWire->Edge(ind2);
254 buildEdge(aE1,aE2,theCommonFaces);
256 myMaxTolerance = RealLast();
257 if(ind2 == ind1) break;
265 //=======================================================================
266 //function : checkOneFace
268 //=======================================================================
269 Standard_Boolean ShHealOper_CloseContour::checkOneFace(const Handle(ShapeExtend_WireData)& theSewd,
270 TopTools_SequenceOfShape& theCommonFaces) const
272 TopTools_IndexedMapOfShape amapfaces;
273 TopoDS_Edge aEdge1 = theSewd->Edge(1);
274 Standard_Boolean isOneFace = myMapEdgesFace.Contains(aEdge1 );
278 //check that all specified edges belong to one face.
279 const TopTools_ListOfShape& alfaces = myMapEdgesFace.FindFromKey(aEdge1);
280 isOneFace = isOneFace && (!alfaces.IsEmpty());
282 return Standard_False;
284 TopTools_ListIteratorOfListOfShape litr(alfaces);
285 for( ; litr.More();litr.Next() )
286 amapfaces.Add(litr.Value());
288 TColStd_MapOfInteger amapIndex;
289 Standard_Integer ind =0;
290 Standard_Integer i =2;
291 for( ; i <= theSewd->NbEdges() && isOneFace; i++) {
292 isOneFace = myMapEdgesFace.Contains(theSewd->Edge(i));
294 const TopTools_ListOfShape& alfaces1 = myMapEdgesFace.FindFromKey(theSewd->Edge(i));
296 TColStd_MapOfInteger aTmpInd;
297 for(litr.Initialize(alfaces1) ; litr.More() ;litr.Next() ) {
298 if(amapfaces.Contains(litr.Value())) {
299 ind = amapfaces.FindIndex(litr.Value());
307 isOneFace = (amapIndex.Extent());
311 TColStd_MapIteratorOfMapOfInteger aMi(amapIndex);
312 for( ; aMi.More();aMi.Next()) {
313 if(!aTmpInd.Contains(aMi.Key()))
314 amapIndex.Remove(aMi.Key());
316 isOneFace = (amapIndex.Extent());
321 if(theSewd->NbEdges() >1) {
322 Standard_Integer j =1;
323 for( ; j <= amapfaces.Extent(); j++) {
324 if(!amapIndex.Contains(j)) continue;
325 TopoDS_Shape aF = amapfaces.FindKey(j);
326 theCommonFaces.Append(aF);
332 //=======================================================================
333 //function : buildEdge
335 //=======================================================================
336 void ShHealOper_CloseContour::buildEdge(const TopoDS_Edge& aE1,
337 const TopoDS_Edge& aE2,
338 const TopTools_SequenceOfShape& theCommonFaces)
340 ShapeAnalysis_Edge asae;
341 TopoDS_Vertex aV1 = asae.LastVertex(aE1);
342 TopoDS_Vertex aV2 = asae.FirstVertex(aE2);
343 gp_Pnt p1 = BRep_Tool::Pnt(aV1);
344 gp_Pnt p2 = BRep_Tool::Pnt(aV2);
345 BRepBuilderAPI_MakeVertex mkver1( p1 );
346 TopoDS_Vertex newV1 = mkver1.Vertex();
348 BRepBuilderAPI_MakeVertex mkver2( p2 );
349 TopoDS_Vertex newV2 = mkver2.Vertex();
351 B.UpdateVertex ( newV1,Precision::Confusion());
352 B.UpdateVertex ( newV2, Precision::Confusion());
353 Standard_Boolean isBuild = Standard_False;
357 //if all edges belong to one face that gap will be closed in the 2D by line
358 //than 3D curve will be built by 2D curve
359 if(theCommonFaces.Length()) {
361 TopoDS_Face aF1 = TopoDS::Face(theCommonFaces.Value(1));
362 Handle(Geom2d_Curve) c2d1,c2d2;
364 Standard_Real a1, b1,a2, b2;
365 if ( asae.PCurve ( aE1, aF1, c2d1, a1, b1, Standard_True ) &&
366 asae.PCurve ( aE2, aF1, c2d2, a2, b2, Standard_True )) {
368 c2d1->D0 ( b1, p2d1);
369 c2d2->D0 ( a2, p2d2);
370 gp_Vec2d v12 ( p2d1, p2d2 );
372 Handle(Geom2d_Line) aLine2d = new Geom2d_Line ( p2d1, gp_Dir2d ( v12 ) );
373 B.UpdateEdge ( edge, aLine2d, aF1, ::Precision::Confusion() );
374 B.Range ( edge, aF1, 0.0, v12.Magnitude() );
376 isBuild = sbe.BuildCurve3d ( edge );
379 //else gap will be closed in the 3D by line.
381 gp_Vec v1 ( p1, p2 );
382 Handle(Geom_Line) aLine = new Geom_Line ( p1, gp_Dir ( v1 ) );
383 B.UpdateEdge ( edge, aLine, ::Precision::Confusion() );
384 B.Range ( edge,0.0, v1.Magnitude());
386 B.Add ( edge, newV1.Oriented ( TopAbs_FORWARD ) );
387 B.Add ( edge, newV2.Oriented ( TopAbs_REVERSED ) );
388 myContext->Replace(aV1, newV1.Oriented (aV1.Orientation()));
389 myContext->Replace(aV2, newV2.Oriented (aV2.Orientation()));
391 newV1.Orientation(aV1.Orientation());
392 TopoDS_Edge newEdge = sbe.CopyReplaceVertices ( aE1,TopoDS_Vertex(), newV1);
397 TopoDS_Shape anE1 = myContext->Apply(aE1);
398 myContext->Replace(anE1,aw);
401 myFreeEdges.Append(edge);
405 //=======================================================================
406 //function : UpdateWire
408 //=======================================================================
409 void ShHealOper_CloseContour::updateWire (Handle(ShapeExtend_WireData)& sbwd)
411 Standard_Integer i=1;
412 for ( ; i <= sbwd->NbEdges(); i++ ) {
413 TopoDS_Edge E = sbwd->Edge(i);
414 TopoDS_Shape S = myContext->Apply ( E );
415 if ( S == E ) continue;
416 for ( TopExp_Explorer exp(S,TopAbs_EDGE); exp.More(); exp.Next() )
417 sbwd->Add ( exp.Current(), i++ );
418 sbwd->Remove ( i-- );
420 for ( i =1; i <= myFreeEdges.Length(); i++ ) {
421 sbwd->Add(TopoDS::Edge(myFreeEdges.Value(i)));