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: BlockFix_UnionEdges.cxx
21 // Created: 07.12.04 15:27:30
22 // Author: Sergey KUUL
25 #include <BlockFix_UnionEdges.ixx>
27 #include <Approx_Curve3d.hxx>
28 #include <BRepAdaptor_HCompCurve.hxx>
29 #include <BRep_Builder.hxx>
30 #include <BRep_Tool.hxx>
31 #include <GC_MakeCircle.hxx>
32 #include <Geom_BSplineCurve.hxx>
33 #include <Geom_Circle.hxx>
34 #include <Geom_Curve.hxx>
35 #include <Geom_Line.hxx>
36 #include <Geom_TrimmedCurve.hxx>
37 #include <ShapeAnalysis_Edge.hxx>
38 #include <ShapeFix_Edge.hxx>
39 #include <ShapeFix_Face.hxx>
40 #include <ShapeFix_Shell.hxx>
41 #include <TColgp_SequenceOfPnt.hxx>
42 #include <TColStd_MapOfInteger.hxx>
44 #include <TopExp_Explorer.hxx>
45 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47 #include <TopTools_ListOfShape.hxx>
48 #include <TopTools_MapOfShape.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_SequenceOfShape.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Face.hxx>
54 #include <TopoDS_Shell.hxx>
55 #include <TopoDS_Solid.hxx>
56 #include <TopoDS_Vertex.hxx>
57 #include <TopoDS_Iterator.hxx>
59 #include "utilities.h"
61 //=======================================================================
62 //function : BlockFix_UnionEdges()
63 //purpose : Constructor
64 //=======================================================================
66 BlockFix_UnionEdges::BlockFix_UnionEdges ( )
71 //=======================================================================
72 //function : MergeEdges
74 //=======================================================================
75 static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
76 const TopoDS_Face& aFace,
77 const Standard_Real Tol,
80 // make chain for union
82 ShapeAnalysis_Edge sae;
83 TopoDS_Edge FirstE = TopoDS::Edge(SeqEdges.Value(1));
84 TopoDS_Edge LastE = FirstE;
85 TopoDS_Vertex VF = sae.FirstVertex(FirstE);
86 TopoDS_Vertex VL = sae.LastVertex(LastE);
87 TopTools_SequenceOfShape aChain;
88 aChain.Append(FirstE);
89 TColStd_MapOfInteger IndUsedEdges;
92 for(j=2; j<=SeqEdges.Length(); j++) {
93 for(Standard_Integer k=2; k<=SeqEdges.Length(); k++) {
94 if(IndUsedEdges.Contains(k)) continue;
95 TopoDS_Edge edge = TopoDS::Edge(SeqEdges.Value(k));
96 TopoDS_Vertex VF2 = sae.FirstVertex(edge);
97 TopoDS_Vertex VL2 = sae.LastVertex(edge);
98 if(sae.FirstVertex(edge).IsSame(VL)) {
101 VL = sae.LastVertex(LastE);
104 else if(sae.LastVertex(edge).IsSame(VF)) {
105 aChain.Prepend(edge);
107 VF = sae.FirstVertex(FirstE);
112 if(aChain.Length()<SeqEdges.Length()) {
113 MESSAGE ("can not create correct chain...");
114 return Standard_False;
116 // union edges in chain
117 // first step: union lines and circles
119 Standard_Real fp1,lp1,fp2,lp2;
120 for(j=1; j<aChain.Length(); j++) {
121 TopoDS_Edge edge1 = TopoDS::Edge(aChain.Value(j));
122 Handle(Geom_Curve) c3d1 = BRep_Tool::Curve(edge1,Loc,fp1,lp1);
123 if(c3d1.IsNull()) break;
124 while(c3d1->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
125 Handle(Geom_TrimmedCurve) tc =
126 Handle(Geom_TrimmedCurve)::DownCast(c3d1);
127 c3d1 = tc->BasisCurve();
129 TopoDS_Edge edge2 = TopoDS::Edge(aChain.Value(j+1));
130 Handle(Geom_Curve) c3d2 = BRep_Tool::Curve(edge2,Loc,fp2,lp2);
131 if(c3d2.IsNull()) break;
132 while(c3d2->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
133 Handle(Geom_TrimmedCurve) tc =
134 Handle(Geom_TrimmedCurve)::DownCast(c3d2);
135 c3d2 = tc->BasisCurve();
137 if( c3d1->IsKind(STANDARD_TYPE(Geom_Line)) && c3d2->IsKind(STANDARD_TYPE(Geom_Line)) ) {
139 Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(c3d1);
140 Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(c3d2);
141 gp_Dir Dir1 = L1->Position().Direction();
142 gp_Dir Dir2 = L2->Position().Direction();
143 if(!Dir1.IsEqual(Dir2,Precision::Angular())) continue;
144 // can union lines => create new edge
145 TopoDS_Vertex V1 = sae.FirstVertex(edge1);
146 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
147 TopoDS_Vertex V2 = sae.LastVertex(edge2);
148 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
150 Handle(Geom_Line) L = new Geom_Line(gp_Ax1(PV1,Vec));
151 Standard_Real dist = PV1.Distance(PV2);
152 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(L,0.0,dist);
154 B.MakeEdge (E,tc,Precision::Confusion());
155 B.Add (E,V1); B.Add (E,V2);
156 B.UpdateVertex(V1, 0., E, 0.);
157 B.UpdateVertex(V2, dist, E, 0.);
159 sfe.FixAddPCurve(E,aFace,Standard_False);
160 sfe.FixSameParameter(E);
162 aChain.SetValue(j,E);
165 if( c3d1->IsKind(STANDARD_TYPE(Geom_Circle)) && c3d2->IsKind(STANDARD_TYPE(Geom_Circle)) ) {
167 Handle(Geom_Circle) C1 = Handle(Geom_Circle)::DownCast(c3d1);
168 Handle(Geom_Circle) C2 = Handle(Geom_Circle)::DownCast(c3d2);
169 gp_Pnt P01 = C1->Location();
170 gp_Pnt P02 = C2->Location();
171 if(P01.Distance(P02)>Precision::Confusion()) continue;
172 // can union circles => create new edge
173 TopoDS_Vertex V1 = sae.FirstVertex(edge1);
174 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
175 TopoDS_Vertex V2 = sae.LastVertex(edge2);
176 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
177 TopoDS_Vertex VM = sae.LastVertex(edge1);
178 gp_Pnt PVM = BRep_Tool::Pnt(VM);
179 GC_MakeCircle MC(PV1,PVM,PV2);
180 Handle(Geom_Circle) C = MC.Value();
181 gp_Pnt P0 = C->Location();
182 gp_Dir D1(gp_Vec(P0,PV1));
183 gp_Dir D2(gp_Vec(P0,PV2));
184 Standard_Real fpar = C->XAxis().Direction().Angle(D1);
185 Standard_Real lpar = C->XAxis().Direction().Angle(D2);
186 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
188 B.MakeEdge (E,tc,Precision::Confusion());
189 B.Add (E,V1); B.Add (E,V2);
190 B.UpdateVertex(V1, fpar, E, 0.);
191 B.UpdateVertex(V2, lpar, E, 0.);
193 sfe.FixAddPCurve(E,aFace,Standard_False);
194 sfe.FixSameParameter(E);
196 aChain.SetValue(j,E);
200 if(j<aChain.Length()) {
201 MESSAGE ("null curve3d in edge...");
202 return Standard_False;
204 if(aChain.Length()>1) {
205 // second step: union edges with various curves
206 // skl for bug 0020052 from Mantis: perform such unions
207 // only if curves are bspline or bezier
208 bool NeedUnion = true;
209 for(j=1; j<=aChain.Length(); j++) {
210 TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
211 Handle(Geom_Curve) c3d = BRep_Tool::Curve(edge,Loc,fp1,lp1);
212 if(c3d.IsNull()) continue;
213 while(c3d->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
214 Handle(Geom_TrimmedCurve) tc =
215 Handle(Geom_TrimmedCurve)::DownCast(c3d);
216 c3d = tc->BasisCurve();
218 if( ( c3d->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ||
219 c3d->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ) ) continue;
224 MESSAGE ("can not make analitical union => make approximation");
227 for(j=1; j<=aChain.Length(); j++) {
228 TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
231 Handle(BRepAdaptor_HCompCurve) Adapt = new BRepAdaptor_HCompCurve(W);
232 Approx_Curve3d Conv(Adapt,Tol,GeomAbs_C1,9,1000);
233 Handle(Geom_BSplineCurve) bc = Conv.Curve();
235 B.MakeEdge (E,bc,Precision::Confusion());
238 aChain.SetValue(1,E);
241 MESSAGE ("can not make approximation for such types of curves");
242 return Standard_False;
246 anEdge = TopoDS::Edge(aChain.Value(1));
247 return Standard_True;
251 //=======================================================================
254 //=======================================================================
256 TopoDS_Shape BlockFix_UnionEdges::Perform(const TopoDS_Shape& Shape,
257 const Standard_Real Tol)
259 myContext = new ShapeBuild_ReShape;
261 TopoDS_Shape aResult = myContext->Apply(Shape);
263 // processing each solid
264 TopExp_Explorer exps;
265 for(exps.Init(Shape, TopAbs_SOLID); exps.More(); exps.Next()) {
266 TopoDS_Solid aSolid = TopoDS::Solid(exps.Current());
268 TopTools_IndexedMapOfShape ChangedFaces;
270 // creating map of edge faces
271 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
272 TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
274 Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
275 TopoDS_Shape aRes = aSolid;
276 aRes = aContext->Apply(aSolid);
278 // processing each face
280 for(exp.Init(aRes, TopAbs_FACE); exp.More(); exp.Next()) {
282 TopoDS::Face(aContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
283 TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
285 for(TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
286 TopoDS_Edge edge = TopoDS::Edge(expe.Current());
287 if(!aMapEdgeFaces.Contains(edge)) continue;
288 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
289 TopTools_ListIteratorOfListOfShape anIter(aList);
290 for( ; anIter.More(); anIter.Next()) {
291 TopoDS_Face face = TopoDS::Face(anIter.Value());
292 TopoDS_Face face1 = TopoDS::Face(aContext->Apply(anIter.Value()));
293 if(face1.IsSame(aFace)) continue;
294 if(aMapFacesEdges.Contains(face)) {
295 aMapFacesEdges.ChangeFromKey(face).Append(edge);
298 TopTools_ListOfShape ListEdges;
299 ListEdges.Append(edge);
300 aMapFacesEdges.Add(face,ListEdges);
305 for(Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++) {
306 const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
307 TopTools_SequenceOfShape SeqEdges;
308 TopTools_ListIteratorOfListOfShape anIter(ListEdges);
309 for( ; anIter.More(); anIter.Next()) {
310 SeqEdges.Append(anIter.Value());
312 if(SeqEdges.Length()==1) continue;
314 if( MergeEdges(SeqEdges,aFace,Tol,E) ) {
315 // now we have only one edge - aChain.Value(1)
316 // we have to replace old ListEdges with this new edge
317 aContext->Replace(SeqEdges(1),E);
318 for(Standard_Integer j=2; j<=SeqEdges.Length(); j++) {
319 aContext->Remove(SeqEdges(j));
321 TopoDS_Face tmpF = TopoDS::Face(exp.Current());
322 if( !ChangedFaces.Contains(tmpF) )
323 ChangedFaces.Add(tmpF);
324 tmpF = TopoDS::Face(aMapFacesEdges.FindKey(i));
325 if( !ChangedFaces.Contains(tmpF) )
326 ChangedFaces.Add(tmpF);
330 } // end processing each face
332 // fix changed faces and replace them in the local context
333 for(Standard_Integer i=1; i<=ChangedFaces.Extent(); i++) {
334 TopoDS_Face aFace = TopoDS::Face(aContext->Apply(ChangedFaces.FindKey(i)));
335 Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFace);
336 sff->SetContext(myContext);
337 sff->SetPrecision(myTolerance);
338 sff->SetMinTolerance(myTolerance);
339 sff->SetMaxTolerance(Max(1.,myTolerance*1000.));
341 aContext->Replace(aFace,sff->Face());
344 if(ChangedFaces.Extent()>0) {
345 // fix changed shell and replace it in the local context
346 TopoDS_Shape aRes1 = aContext->Apply(aRes);
347 TopExp_Explorer expsh;
348 for(expsh.Init(aRes1, TopAbs_SHELL); expsh.More(); expsh.Next()) {
349 TopoDS_Shell aShell = TopoDS::Shell(expsh.Current());
350 Handle(ShapeFix_Shell) sfsh = new ShapeFix_Shell;
351 sfsh->FixFaceOrientation(aShell);
352 aContext->Replace(aShell,sfsh->Shell());
354 TopoDS_Shape aRes2 = aContext->Apply(aRes1);
355 // put new solid into global context
356 myContext->Replace(aSolid,aRes2);
359 } // end processing each solid
361 aResult = myContext->Apply(Shape);