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>
60 //=======================================================================
61 //function : BlockFix_UnionEdges()
62 //purpose : Constructor
63 //=======================================================================
65 BlockFix_UnionEdges::BlockFix_UnionEdges ( )
70 //=======================================================================
71 //function : MergeEdges
73 //=======================================================================
74 static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
75 const TopoDS_Face& aFace,
76 const Standard_Real Tol,
79 // make chain for union
81 ShapeAnalysis_Edge sae;
82 TopoDS_Edge FirstE = TopoDS::Edge(SeqEdges.Value(1));
83 TopoDS_Edge LastE = FirstE;
84 TopoDS_Vertex VF = sae.FirstVertex(FirstE);
85 TopoDS_Vertex VL = sae.LastVertex(LastE);
86 TopTools_SequenceOfShape aChain;
87 aChain.Append(FirstE);
88 TColStd_MapOfInteger IndUsedEdges;
91 for(j=2; j<=SeqEdges.Length(); j++) {
92 for(Standard_Integer k=2; k<=SeqEdges.Length(); k++) {
93 if(IndUsedEdges.Contains(k)) continue;
94 TopoDS_Edge edge = TopoDS::Edge(SeqEdges.Value(k));
95 TopoDS_Vertex VF2 = sae.FirstVertex(edge);
96 TopoDS_Vertex VL2 = sae.LastVertex(edge);
97 if(sae.FirstVertex(edge).IsSame(VL)) {
100 VL = sae.LastVertex(LastE);
103 else if(sae.LastVertex(edge).IsSame(VF)) {
104 aChain.Prepend(edge);
106 VF = sae.FirstVertex(FirstE);
111 if(aChain.Length()<SeqEdges.Length()) {
112 cout<<"can not create correct chain..."<<endl;
113 return Standard_False;
115 // union edges in chain
116 // first step: union lines and circles
118 Standard_Real fp1,lp1,fp2,lp2;
119 for(j=1; j<aChain.Length(); j++) {
120 TopoDS_Edge edge1 = TopoDS::Edge(aChain.Value(j));
121 Handle(Geom_Curve) c3d1 = BRep_Tool::Curve(edge1,Loc,fp1,lp1);
122 if(c3d1.IsNull()) break;
123 while(c3d1->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
124 Handle(Geom_TrimmedCurve) tc =
125 Handle(Geom_TrimmedCurve)::DownCast(c3d1);
126 c3d1 = tc->BasisCurve();
128 TopoDS_Edge edge2 = TopoDS::Edge(aChain.Value(j+1));
129 Handle(Geom_Curve) c3d2 = BRep_Tool::Curve(edge2,Loc,fp2,lp2);
130 if(c3d2.IsNull()) break;
131 while(c3d2->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
132 Handle(Geom_TrimmedCurve) tc =
133 Handle(Geom_TrimmedCurve)::DownCast(c3d2);
134 c3d2 = tc->BasisCurve();
136 if( c3d1->IsKind(STANDARD_TYPE(Geom_Line)) && c3d2->IsKind(STANDARD_TYPE(Geom_Line)) ) {
138 Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(c3d1);
139 Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(c3d2);
140 gp_Dir Dir1 = L1->Position().Direction();
141 gp_Dir Dir2 = L2->Position().Direction();
142 if(!Dir1.IsEqual(Dir2,Precision::Angular())) continue;
143 // can union lines => create new edge
144 TopoDS_Vertex V1 = sae.FirstVertex(edge1);
145 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
146 TopoDS_Vertex V2 = sae.LastVertex(edge2);
147 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
149 Handle(Geom_Line) L = new Geom_Line(gp_Ax1(PV1,Vec));
150 Standard_Real dist = PV1.Distance(PV2);
151 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(L,0.0,dist);
153 B.MakeEdge (E,tc,Precision::Confusion());
154 B.Add (E,V1); B.Add (E,V2);
155 B.UpdateVertex(V1, 0., E, 0.);
156 B.UpdateVertex(V2, dist, E, 0.);
158 sfe.FixAddPCurve(E,aFace,Standard_False);
159 sfe.FixSameParameter(E);
161 aChain.SetValue(j,E);
164 if( c3d1->IsKind(STANDARD_TYPE(Geom_Circle)) && c3d2->IsKind(STANDARD_TYPE(Geom_Circle)) ) {
166 Handle(Geom_Circle) C1 = Handle(Geom_Circle)::DownCast(c3d1);
167 Handle(Geom_Circle) C2 = Handle(Geom_Circle)::DownCast(c3d2);
168 gp_Pnt P01 = C1->Location();
169 gp_Pnt P02 = C2->Location();
170 if(P01.Distance(P02)>Precision::Confusion()) continue;
171 // can union circles => create new edge
172 TopoDS_Vertex V1 = sae.FirstVertex(edge1);
173 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
174 TopoDS_Vertex V2 = sae.LastVertex(edge2);
175 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
176 TopoDS_Vertex VM = sae.LastVertex(edge1);
177 gp_Pnt PVM = BRep_Tool::Pnt(VM);
178 GC_MakeCircle MC(PV1,PVM,PV2);
179 Handle(Geom_Circle) C = MC.Value();
180 gp_Pnt P0 = C->Location();
181 gp_Dir D1(gp_Vec(P0,PV1));
182 gp_Dir D2(gp_Vec(P0,PV2));
183 Standard_Real fpar = C->XAxis().Direction().Angle(D1);
184 Standard_Real lpar = C->XAxis().Direction().Angle(D2);
185 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
187 B.MakeEdge (E,tc,Precision::Confusion());
188 B.Add (E,V1); B.Add (E,V2);
189 B.UpdateVertex(V1, fpar, E, 0.);
190 B.UpdateVertex(V2, lpar, E, 0.);
192 sfe.FixAddPCurve(E,aFace,Standard_False);
193 sfe.FixSameParameter(E);
195 aChain.SetValue(j,E);
199 if(j<aChain.Length()) {
200 cout<<"null curve3d in edge..."<<endl;
201 return Standard_False;
203 if(aChain.Length()>1) {
204 // second step: union edges with various curves
205 cout<<"can not make analitical union => make approximation"<<endl;
208 for(j=1; j<=aChain.Length(); j++) {
209 TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
212 Handle(BRepAdaptor_HCompCurve) Adapt = new BRepAdaptor_HCompCurve(W);
213 Approx_Curve3d Conv(Adapt,Tol,GeomAbs_C1,9,1000);
214 Handle(Geom_BSplineCurve) bc = Conv.Curve();
216 B.MakeEdge (E,bc,Precision::Confusion());
219 aChain.SetValue(1,E);
222 anEdge = TopoDS::Edge(aChain.Value(1));
223 return Standard_True;
227 //=======================================================================
230 //=======================================================================
232 TopoDS_Shape BlockFix_UnionEdges::Perform(const TopoDS_Shape& Shape,
233 const Standard_Real Tol)
235 myContext = new ShapeBuild_ReShape;
237 TopoDS_Shape aResult = myContext->Apply(Shape);
239 // processing each solid
240 TopExp_Explorer exps;
241 for(exps.Init(Shape, TopAbs_SOLID); exps.More(); exps.Next()) {
242 TopoDS_Solid aSolid = TopoDS::Solid(exps.Current());
244 TopTools_IndexedMapOfShape ChangedFaces;
246 // creating map of edge faces
247 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
248 TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
250 Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
251 TopoDS_Shape aRes = aSolid;
252 aRes = aContext->Apply(aSolid);
254 // processing each face
256 for(exp.Init(aRes, TopAbs_FACE); exp.More(); exp.Next()) {
258 TopoDS::Face(aContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
259 TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
261 for(TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
262 TopoDS_Edge edge = TopoDS::Edge(expe.Current());
263 if(!aMapEdgeFaces.Contains(edge)) continue;
264 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
265 TopTools_ListIteratorOfListOfShape anIter(aList);
266 for( ; anIter.More(); anIter.Next()) {
267 TopoDS_Face face = TopoDS::Face(anIter.Value());
268 TopoDS_Face face1 = TopoDS::Face(aContext->Apply(anIter.Value()));
269 if(face1.IsSame(aFace)) continue;
270 if(aMapFacesEdges.Contains(face)) {
271 aMapFacesEdges.ChangeFromKey(face).Append(edge);
274 TopTools_ListOfShape ListEdges;
275 ListEdges.Append(edge);
276 aMapFacesEdges.Add(face,ListEdges);
281 for(Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++) {
282 const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
283 TopTools_SequenceOfShape SeqEdges;
284 TopTools_ListIteratorOfListOfShape anIter(ListEdges);
285 for( ; anIter.More(); anIter.Next()) {
286 SeqEdges.Append(anIter.Value());
288 if(SeqEdges.Length()==1) continue;
290 if( MergeEdges(SeqEdges,aFace,Tol,E) ) {
291 // now we have only one edge - aChain.Value(1)
292 // we have to replace old ListEdges with this new edge
293 aContext->Replace(SeqEdges(1),E);
294 for(Standard_Integer j=2; j<=SeqEdges.Length(); j++) {
295 aContext->Remove(SeqEdges(j));
297 TopoDS_Face tmpF = TopoDS::Face(exp.Current());
298 if( !ChangedFaces.Contains(tmpF) )
299 ChangedFaces.Add(tmpF);
300 tmpF = TopoDS::Face(aMapFacesEdges.FindKey(i));
301 if( !ChangedFaces.Contains(tmpF) )
302 ChangedFaces.Add(tmpF);
306 } // end processing each face
308 // fix changed faces and replace them in the local context
309 for(Standard_Integer i=1; i<=ChangedFaces.Extent(); i++) {
310 TopoDS_Face aFace = TopoDS::Face(aContext->Apply(ChangedFaces.FindKey(i)));
311 Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFace);
312 sff->SetContext(myContext);
313 sff->SetPrecision(myTolerance);
314 sff->SetMinTolerance(myTolerance);
315 sff->SetMaxTolerance(Max(1.,myTolerance*1000.));
317 aContext->Replace(aFace,sff->Face());
320 if(ChangedFaces.Extent()>0) {
321 // fix changed shell and replace it in the local context
322 TopoDS_Shape aRes1 = aContext->Apply(aRes);
323 TopExp_Explorer expsh;
324 for(expsh.Init(aRes1, TopAbs_SHELL); expsh.More(); expsh.Next()) {
325 TopoDS_Shell aShell = TopoDS::Shell(expsh.Current());
326 Handle(ShapeFix_Shell) sfsh = new ShapeFix_Shell;
327 sfsh->FixFaceOrientation(aShell);
328 aContext->Replace(aShell,sfsh->Shell());
330 TopoDS_Shape aRes2 = aContext->Apply(aRes1);
331 // put new solid into global context
332 myContext->Replace(aSolid,aRes2);
335 } // end processing each solid
337 aResult = myContext->Apply(Shape);