]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMAlgo/BlockFix_UnionEdges.cxx
Salome HOME
c3b7baf15fd396b330b0124c0d226310d5b421d3
[modules/geom.git] / src / GEOMAlgo / BlockFix_UnionEdges.cxx
1 // File:      BlockFix_UnionEdges.cxx
2 // Created:   07.12.04 15:27:30
3 // Author:    Sergey KUUL
4
5
6 #include <BlockFix_UnionEdges.ixx>
7
8 #include <Approx_Curve3d.hxx>
9 #include <BRepAdaptor_HCompCurve.hxx>
10 #include <BRep_Builder.hxx>
11 #include <BRep_Tool.hxx>
12 #include <GC_MakeCircle.hxx>
13 #include <Geom_BSplineCurve.hxx>
14 #include <Geom_Circle.hxx>
15 #include <Geom_Curve.hxx>
16 #include <Geom_Line.hxx>
17 #include <Geom_TrimmedCurve.hxx>
18 #include <ShapeAnalysis_Edge.hxx>
19 #include <ShapeFix_Edge.hxx>
20 #include <ShapeFix_Face.hxx>
21 #include <ShapeFix_Shell.hxx>
22 #include <TColgp_SequenceOfPnt.hxx>
23 #include <TColStd_MapOfInteger.hxx>
24 #include <TopExp.hxx>
25 #include <TopExp_Explorer.hxx>
26 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
27 #include <TopTools_IndexedMapOfShape.hxx>
28 #include <TopTools_ListOfShape.hxx>
29 #include <TopTools_MapOfShape.hxx>
30 #include <TopTools_ListIteratorOfListOfShape.hxx>
31 #include <TopTools_SequenceOfShape.hxx>
32 #include <TopoDS.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS_Shell.hxx>
36 #include <TopoDS_Solid.hxx>
37 #include <TopoDS_Vertex.hxx>
38 #include <TopoDS_Iterator.hxx>
39
40
41 //=======================================================================
42 //function : BlockFix_UnionEdges()
43 //purpose  : Constructor
44 //=======================================================================
45
46 BlockFix_UnionEdges::BlockFix_UnionEdges (  )
47 {
48 }
49
50
51 //=======================================================================
52 //function : MergeEdges
53 //purpose  : auxilary
54 //=======================================================================
55 static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
56                                    const TopoDS_Face& aFace,
57                                    const Standard_Real Tol,
58                                    TopoDS_Edge& anEdge)
59 {
60   // make chain for union
61   BRep_Builder B;
62   ShapeAnalysis_Edge sae;
63   TopoDS_Edge FirstE = TopoDS::Edge(SeqEdges.Value(1));
64   TopoDS_Edge LastE = FirstE;
65   TopoDS_Vertex VF = sae.FirstVertex(FirstE);
66   TopoDS_Vertex VL = sae.LastVertex(LastE);
67   TopTools_SequenceOfShape aChain;
68   aChain.Append(FirstE);
69   TColStd_MapOfInteger IndUsedEdges;
70   IndUsedEdges.Add(1);
71   Standard_Integer j;
72   for(j=2; j<=SeqEdges.Length(); j++) {
73     for(Standard_Integer k=2; k<=SeqEdges.Length(); k++) {
74       if(IndUsedEdges.Contains(k)) continue;
75       TopoDS_Edge edge = TopoDS::Edge(SeqEdges.Value(k));
76       TopoDS_Vertex VF2 = sae.FirstVertex(edge);
77       TopoDS_Vertex VL2 = sae.LastVertex(edge);
78       if(sae.FirstVertex(edge).IsSame(VL)) {
79         aChain.Append(edge);
80         LastE = edge;
81         VL = sae.LastVertex(LastE);
82         IndUsedEdges.Add(k);
83       }
84       else if(sae.LastVertex(edge).IsSame(VF)) {
85         aChain.Prepend(edge);
86         FirstE = edge;
87         VF = sae.FirstVertex(FirstE);
88         IndUsedEdges.Add(k);
89       }
90     }
91   }
92   if(aChain.Length()<SeqEdges.Length()) {
93     cout<<"can not create correct chain..."<<endl;
94     return Standard_False;
95   }
96   // union edges in chain
97   // first step: union lines and circles
98   TopLoc_Location Loc;
99   Standard_Real fp1,lp1,fp2,lp2;
100   for(j=1; j<aChain.Length(); j++) {
101     TopoDS_Edge edge1 = TopoDS::Edge(aChain.Value(j));
102     Handle(Geom_Curve) c3d1 = BRep_Tool::Curve(edge1,Loc,fp1,lp1);
103     if(c3d1.IsNull()) break;
104     while(c3d1->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
105       Handle(Geom_TrimmedCurve) tc =
106         Handle(Geom_TrimmedCurve)::DownCast(c3d1);
107       c3d1 = tc->BasisCurve();
108     }
109     TopoDS_Edge edge2 = TopoDS::Edge(aChain.Value(j+1));
110     Handle(Geom_Curve) c3d2 = BRep_Tool::Curve(edge2,Loc,fp2,lp2);
111     if(c3d2.IsNull()) break;
112     while(c3d2->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
113       Handle(Geom_TrimmedCurve) tc =
114         Handle(Geom_TrimmedCurve)::DownCast(c3d2);
115       c3d2 = tc->BasisCurve();
116     }
117     if( c3d1->IsKind(STANDARD_TYPE(Geom_Line)) && c3d2->IsKind(STANDARD_TYPE(Geom_Line)) ) {
118       // union lines
119       Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(c3d1);
120       Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(c3d2);
121       gp_Dir Dir1 = L1->Position().Direction();
122       gp_Dir Dir2 = L2->Position().Direction();
123       if(!Dir1.IsEqual(Dir2,Precision::Angular())) continue;
124       // can union lines => create new edge
125       TopoDS_Vertex V1 = sae.FirstVertex(edge1);
126       gp_Pnt PV1 = BRep_Tool::Pnt(V1);
127       TopoDS_Vertex V2 = sae.LastVertex(edge2);
128       gp_Pnt PV2 = BRep_Tool::Pnt(V2);
129       gp_Vec Vec(PV1,PV2);
130       Handle(Geom_Line) L = new Geom_Line(gp_Ax1(PV1,Vec));
131       Standard_Real dist = PV1.Distance(PV2);
132       Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(L,0.0,dist);
133       TopoDS_Edge E;
134       B.MakeEdge (E,tc,Precision::Confusion());
135       B.Add (E,V1);  B.Add (E,V2);
136       B.UpdateVertex(V1, 0., E, 0.);
137       B.UpdateVertex(V2, dist, E, 0.);
138       ShapeFix_Edge sfe;
139       sfe.FixAddPCurve(E,aFace,Standard_False);
140       sfe.FixSameParameter(E);
141       aChain.Remove(j);
142       aChain.SetValue(j,E);
143       j--;
144     }
145     if( c3d1->IsKind(STANDARD_TYPE(Geom_Circle)) && c3d2->IsKind(STANDARD_TYPE(Geom_Circle)) ) {
146       // union circles
147       Handle(Geom_Circle) C1 = Handle(Geom_Circle)::DownCast(c3d1);
148       Handle(Geom_Circle) C2 = Handle(Geom_Circle)::DownCast(c3d2);
149       gp_Pnt P01 = C1->Location();
150       gp_Pnt P02 = C2->Location();
151       if(P01.Distance(P02)>Precision::Confusion()) continue;
152       // can union circles => create new edge
153       TopoDS_Vertex V1 = sae.FirstVertex(edge1);
154       gp_Pnt PV1 = BRep_Tool::Pnt(V1);
155       TopoDS_Vertex V2 = sae.LastVertex(edge2);
156       gp_Pnt PV2 = BRep_Tool::Pnt(V2);
157       TopoDS_Vertex VM = sae.LastVertex(edge1);
158       gp_Pnt PVM = BRep_Tool::Pnt(VM);
159       GC_MakeCircle MC(PV1,PVM,PV2);
160       Handle(Geom_Circle) C = MC.Value();
161       gp_Pnt P0 = C->Location();
162       gp_Dir D1(gp_Vec(P0,PV1));
163       gp_Dir D2(gp_Vec(P0,PV2));
164       Standard_Real fpar = C->XAxis().Direction().Angle(D1);
165       Standard_Real lpar = C->XAxis().Direction().Angle(D2);
166       Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
167       TopoDS_Edge E;
168       B.MakeEdge (E,tc,Precision::Confusion());
169       B.Add (E,V1);  B.Add (E,V2);
170       B.UpdateVertex(V1, fpar, E, 0.);
171       B.UpdateVertex(V2, lpar, E, 0.);
172       ShapeFix_Edge sfe;
173       sfe.FixAddPCurve(E,aFace,Standard_False);
174       sfe.FixSameParameter(E);
175       aChain.Remove(j);
176       aChain.SetValue(j,E);
177       j--;
178     }
179   }
180   if(j<aChain.Length()) {
181     cout<<"null curve3d in edge..."<<endl;
182     return Standard_False;
183   }
184   if(aChain.Length()>1) {
185     // second step: union edges with various curves
186     cout<<"can not make analitical union => make approximation"<<endl;
187     TopoDS_Wire W;
188     B.MakeWire(W);
189     for(j=1; j<=aChain.Length(); j++) {
190       TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
191       B.Add(W,edge);
192     }
193     Handle(BRepAdaptor_HCompCurve) Adapt = new BRepAdaptor_HCompCurve(W);
194     Approx_Curve3d Conv(Adapt,Tol,GeomAbs_C1,9,1000);
195     Handle(Geom_BSplineCurve) bc = Conv.Curve();
196     TopoDS_Edge E;
197     B.MakeEdge (E,bc,Precision::Confusion());
198     B.Add (E,VF);
199     B.Add (E,VL);
200     aChain.SetValue(1,E);
201   }
202
203   anEdge = TopoDS::Edge(aChain.Value(1));
204   return Standard_True;
205 }
206
207
208 //=======================================================================
209 //function : Perform
210 //purpose  : 
211 //=======================================================================
212
213 TopoDS_Shape BlockFix_UnionEdges::Perform(const TopoDS_Shape& Shape,
214                                           const Standard_Real Tol)
215 {
216   myContext = new ShapeBuild_ReShape;
217   myTolerance = Tol;
218   TopoDS_Shape aResult = myContext->Apply(Shape);
219   
220   // processing each solid
221   TopExp_Explorer exps;
222   for(exps.Init(Shape, TopAbs_SOLID); exps.More(); exps.Next()) {
223     TopoDS_Solid aSolid = TopoDS::Solid(exps.Current());
224
225     TopTools_IndexedMapOfShape ChangedFaces;
226     
227     // creating map of edge faces
228     TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
229     TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
230   
231     Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
232     TopoDS_Shape aRes = aSolid;
233     aRes = aContext->Apply(aSolid);
234
235     // processing each face
236     TopExp_Explorer exp;
237     for(exp.Init(aRes, TopAbs_FACE); exp.More(); exp.Next()) {
238       TopoDS_Face aFace =
239         TopoDS::Face(aContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
240       TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
241
242       for(TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
243         TopoDS_Edge edge = TopoDS::Edge(expe.Current());
244         if(!aMapEdgeFaces.Contains(edge)) continue;
245         const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
246         TopTools_ListIteratorOfListOfShape anIter(aList);
247         for( ; anIter.More(); anIter.Next()) {
248           TopoDS_Face face = TopoDS::Face(anIter.Value());
249           TopoDS_Face face1 = TopoDS::Face(aContext->Apply(anIter.Value()));
250           if(face1.IsSame(aFace)) continue;
251           if(aMapFacesEdges.Contains(face)) {
252             aMapFacesEdges.ChangeFromKey(face).Append(edge);
253           }
254           else {
255             TopTools_ListOfShape ListEdges;
256             ListEdges.Append(edge);
257             aMapFacesEdges.Add(face,ListEdges);
258           }
259         }
260       }
261       
262       for(Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++) {
263         const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
264         TopTools_SequenceOfShape SeqEdges;
265         TopTools_ListIteratorOfListOfShape anIter(ListEdges);
266         for( ; anIter.More(); anIter.Next()) {
267           SeqEdges.Append(anIter.Value());
268         }
269         if(SeqEdges.Length()==1) continue;
270         TopoDS_Edge E;
271         if( MergeEdges(SeqEdges,aFace,Tol,E) ) {
272           // now we have only one edge - aChain.Value(1)
273           // we have to replace old ListEdges with this new edge
274           aContext->Replace(SeqEdges(1),E);
275           for(Standard_Integer j=2; j<=SeqEdges.Length(); j++) {
276             aContext->Remove(SeqEdges(j));
277           }
278           TopoDS_Face tmpF = TopoDS::Face(exp.Current());
279           if( !ChangedFaces.Contains(tmpF) )
280             ChangedFaces.Add(tmpF);
281           tmpF = TopoDS::Face(aMapFacesEdges.FindKey(i));
282           if( !ChangedFaces.Contains(tmpF) )
283             ChangedFaces.Add(tmpF);
284         }
285       }
286       
287     } // end processing each face
288     
289     // fix changed faces and replace them in the local context
290     for(Standard_Integer i=1; i<=ChangedFaces.Extent(); i++) {
291       TopoDS_Face aFace = TopoDS::Face(aContext->Apply(ChangedFaces.FindKey(i)));
292       Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFace);
293       sff->SetContext(myContext);
294       sff->SetPrecision(myTolerance);
295       sff->SetMinTolerance(myTolerance);
296       sff->SetMaxTolerance(Max(1.,myTolerance*1000.));
297       sff->Perform();
298       aContext->Replace(aFace,sff->Face());
299     }
300
301     if(ChangedFaces.Extent()>0) {
302       // fix changed shell and replace it in the local context
303       TopoDS_Shape aRes1 = aContext->Apply(aRes);
304       TopExp_Explorer expsh;
305       for(expsh.Init(aRes1, TopAbs_SHELL); expsh.More(); expsh.Next()) {
306         TopoDS_Shell aShell = TopoDS::Shell(expsh.Current());
307         Handle(ShapeFix_Shell) sfsh = new ShapeFix_Shell;
308         sfsh->FixFaceOrientation(aShell);
309         aContext->Replace(aShell,sfsh->Shell());
310       }
311       TopoDS_Shape aRes2 = aContext->Apply(aRes1);
312       // put new solid into global context
313       myContext->Replace(aSolid,aRes2);
314     }
315
316   } // end processing each solid
317
318   aResult = myContext->Apply(Shape);
319   return aResult;
320 }