1 // File: BlockFix_UnionEdges.cxx
2 // Created: 07.12.04 15:27:30
6 #include <BlockFix_UnionEdges.ixx>
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 <TColgp_SequenceOfPnt.hxx>
22 #include <TColStd_MapOfInteger.hxx>
24 #include <TopExp_Explorer.hxx>
25 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
26 #include <TopTools_IndexedMapOfShape.hxx>
27 #include <TopTools_ListOfShape.hxx>
28 #include <TopTools_MapOfShape.hxx>
29 #include <TopTools_ListIteratorOfListOfShape.hxx>
30 #include <TopTools_SequenceOfShape.hxx>
32 #include <TopoDS_Edge.hxx>
33 #include <TopoDS_Face.hxx>
34 #include <TopoDS_Solid.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopoDS_Iterator.hxx>
39 //=======================================================================
40 //function : BlockFix_UnionEdges()
41 //purpose : Constructor
42 //=======================================================================
44 BlockFix_UnionEdges::BlockFix_UnionEdges ( )
49 //=======================================================================
50 //function : MergeEdges
52 //=======================================================================
53 static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
54 const TopoDS_Face& aFace,
55 const Standard_Real Tol,
58 // make chain for union
60 ShapeAnalysis_Edge sae;
61 TopoDS_Edge FirstE = TopoDS::Edge(SeqEdges.Value(1));
62 TopoDS_Edge LastE = FirstE;
63 TopoDS_Vertex VF = sae.FirstVertex(FirstE);
64 TopoDS_Vertex VL = sae.LastVertex(LastE);
65 TopTools_SequenceOfShape aChain;
66 aChain.Append(FirstE);
67 TColStd_MapOfInteger IndUsedEdges;
70 for (j = 2; j <= SeqEdges.Length(); j++) {
71 for(Standard_Integer k=2; k<=SeqEdges.Length(); k++) {
72 if(IndUsedEdges.Contains(k)) continue;
73 TopoDS_Edge edge = TopoDS::Edge(SeqEdges.Value(k));
74 TopoDS_Vertex VF2 = sae.FirstVertex(edge);
75 TopoDS_Vertex VL2 = sae.LastVertex(edge);
76 if(sae.FirstVertex(edge).IsSame(VL)) {
79 VL = sae.LastVertex(LastE);
82 else if(sae.LastVertex(edge).IsSame(VF)) {
85 VF = sae.FirstVertex(FirstE);
90 if(aChain.Length()<SeqEdges.Length()) {
91 cout<<"can not create correct chain..."<<endl;
92 return Standard_False;
94 // union edges in chain
95 // first step: union lines and circles
97 Standard_Real fp1,lp1,fp2,lp2;
98 for (j = 1; j < aChain.Length(); j++) {
99 TopoDS_Edge edge1 = TopoDS::Edge(aChain.Value(j));
100 Handle(Geom_Curve) c3d1 = BRep_Tool::Curve(edge1,Loc,fp1,lp1);
101 if(c3d1.IsNull()) break;
102 while(c3d1->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
103 Handle(Geom_TrimmedCurve) tc =
104 Handle(Geom_TrimmedCurve)::DownCast(c3d1);
105 c3d1 = tc->BasisCurve();
107 TopoDS_Edge edge2 = TopoDS::Edge(aChain.Value(j+1));
108 Handle(Geom_Curve) c3d2 = BRep_Tool::Curve(edge2,Loc,fp2,lp2);
109 if(c3d2.IsNull()) break;
110 while(c3d2->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
111 Handle(Geom_TrimmedCurve) tc =
112 Handle(Geom_TrimmedCurve)::DownCast(c3d2);
113 c3d2 = tc->BasisCurve();
115 if( c3d1->IsKind(STANDARD_TYPE(Geom_Line)) && c3d2->IsKind(STANDARD_TYPE(Geom_Line)) ) {
116 Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(c3d1);
117 Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(c3d2);
118 gp_Dir Dir1 = L1->Position().Direction();
119 gp_Dir Dir2 = L2->Position().Direction();
120 if(!Dir1.IsEqual(Dir2,Precision::Angular())) continue;
121 // can union lines => create new edge
122 TopoDS_Vertex V1 = sae.FirstVertex(edge1);
123 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
124 TopoDS_Vertex V2 = sae.LastVertex(edge2);
125 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
127 Handle(Geom_Line) L = new Geom_Line(gp_Ax1(PV1,Vec));
128 Standard_Real dist = PV1.Distance(PV2);
129 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(L,0.0,dist);
131 B.MakeEdge (E,tc,Precision::Confusion());
132 B.Add (E,V1); B.Add (E,V2);
133 B.UpdateVertex(V1, 0., E, 0.);
134 B.UpdateVertex(V2, dist, E, 0.);
136 sfe.FixAddPCurve(E,aFace,Standard_False);
137 sfe.FixSameParameter(E);
139 aChain.SetValue(j,E);
142 if( c3d1->IsKind(STANDARD_TYPE(Geom_Circle)) && c3d2->IsKind(STANDARD_TYPE(Geom_Circle)) ) {
143 Handle(Geom_Circle) C1 = Handle(Geom_Circle)::DownCast(c3d1);
144 Handle(Geom_Circle) C2 = Handle(Geom_Circle)::DownCast(c3d2);
145 gp_Pnt P01 = C1->Location();
146 gp_Pnt P02 = C2->Location();
147 if(P01.Distance(P02)>Precision::Confusion()) continue;
148 // can union circles => create new edge
149 TopoDS_Vertex V1 = sae.FirstVertex(edge1);
150 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
151 TopoDS_Vertex V2 = sae.LastVertex(edge2);
152 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
153 TopoDS_Vertex VM = sae.LastVertex(edge1);
154 gp_Pnt PVM = BRep_Tool::Pnt(VM);
155 Standard_Boolean IsClosed = Standard_False;
157 IsClosed = Standard_True;
158 TopExp_Explorer expe;
159 Standard_Boolean HasOtherEdges = Standard_False;
160 for(expe.Init(aFace, TopAbs_EDGE); expe.More(); expe.Next()) {
161 TopoDS_Edge Etmp = TopoDS::Edge(expe.Current());
162 if( Etmp.IsSame(edge1) || Etmp.IsSame(edge2) ) continue;
163 if( sae.FirstVertex(Etmp).IsSame(V1) ||
164 sae.LastVertex(Etmp).IsSame(V1) ) {
165 HasOtherEdges = Standard_True;
170 Standard_Real par = (fp2+lp2)/2.;
174 V1 = sae.FirstVertex(edge2);
175 PV1 = BRep_Tool::Pnt(V1);
177 VM = sae.LastVertex(edge2);
178 PVM = BRep_Tool::Pnt(VM);
179 Standard_Real par = (fp1+lp1)/2.;
183 GC_MakeCircle MC(PV1,PVM,PV2);
184 Handle(Geom_Circle) C = MC.Value();
185 gp_Pnt P0 = C->Location();
186 gp_Dir D1(gp_Vec(P0,PV1));
187 gp_Dir D2(gp_Vec(P0,PV2));
188 Standard_Real fpar = C->XAxis().Direction().Angle(D1);
193 lpar = C->XAxis().Direction().Angle(D2);
194 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
196 B.MakeEdge (E,tc,Precision::Confusion());
197 B.Add (E,V1); B.Add (E,V2);
198 B.UpdateVertex(V1, fpar, E, 0.);
199 B.UpdateVertex(V2, lpar, E, 0.);
201 sfe.FixAddPCurve(E,aFace,Standard_False);
202 sfe.FixSameParameter(E);
204 aChain.SetValue(j,E);
208 if(j<aChain.Length()) {
209 cout<<"null curve3d in edge..."<<endl;
210 return Standard_False;
212 if(aChain.Length()>1) {
213 // second step: union edges with various curves
214 cout<<"can not make analitical union => make approximation"<<endl;
217 for(j=1; j<=aChain.Length(); j++) {
218 TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
221 Handle(BRepAdaptor_HCompCurve) Adapt = new BRepAdaptor_HCompCurve(W);
222 Approx_Curve3d Conv(Adapt,Tol,GeomAbs_C1,9,1000);
223 Handle(Geom_BSplineCurve) bc = Conv.Curve();
225 B.MakeEdge (E,bc,Precision::Confusion());
229 //Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace,L);
231 //if(!L.IsIdentity()) {
232 // TopoDS_Edge aPCEdge = TopoDS::Edge(E.Moved(L.Inverted()));
233 // sfe.FixAddPCurve(aPCEdge,aFace,Standard_False);
234 // Handle(Geom2d_Curve) c2d;
235 // Standard_Real fp,lp;
236 // sae.PCurve(aPCEdge,aFace,c2d,fp,lp);
237 // B.UpdateEdge(E,c2d,aFace,0.);
238 // B.Range(E,aFace,fp,lp);
240 // B.UpdateEdge(aPCEdge,c2d,aFace,0.);
244 // sfe.FixAddPCurve(E,aFace,Standard_False);
246 //sfe.FixSameParameter(E);
247 aChain.SetValue(1,E);
250 anEdge = TopoDS::Edge(aChain.Value(1));
251 return Standard_True;
255 //=======================================================================
258 //=======================================================================
260 TopoDS_Shape BlockFix_UnionEdges::Perform(const TopoDS_Shape& Shape,
261 const Standard_Real Tol)
263 myContext = new ShapeBuild_ReShape;
265 TopoDS_Shape aResult = myContext->Apply(Shape);
267 TopTools_IndexedMapOfShape ChangedFaces;
269 // processing each solid
270 TopExp_Explorer exps;
271 for(exps.Init(Shape, TopAbs_SOLID); exps.More(); exps.Next()) {
272 TopoDS_Solid aSolid = TopoDS::Solid(exps.Current());
274 // creating map of edge faces
275 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
276 TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
278 // processing each face
280 for(exp.Init(aSolid, TopAbs_FACE); exp.More(); exp.Next()) {
281 TopoDS_Face aFace = TopoDS::Face(myContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
282 TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
284 for(TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
285 TopoDS_Edge edge = TopoDS::Edge(expe.Current());
286 if(!aMapEdgeFaces.Contains(edge)) continue;
287 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
288 TopTools_ListIteratorOfListOfShape anIter(aList);
289 for( ; anIter.More(); anIter.Next()) {
290 TopoDS_Face face = TopoDS::Face(anIter.Value());
291 TopoDS_Face face1 = TopoDS::Face(myContext->Apply(anIter.Value()));
292 if(face1.IsSame(aFace)) continue;
293 if(aMapFacesEdges.Contains(face)) {
294 aMapFacesEdges.ChangeFromKey(face).Append(edge);
297 TopTools_ListOfShape ListEdges;
298 ListEdges.Append(edge);
299 aMapFacesEdges.Add(face,ListEdges);
304 for(Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++) {
305 const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
306 TopTools_SequenceOfShape SeqEdges;
307 TopTools_ListIteratorOfListOfShape anIter(ListEdges);
308 for( ; anIter.More(); anIter.Next()) {
309 SeqEdges.Append(anIter.Value());
311 if(SeqEdges.Length()==1) continue;
313 if( MergeEdges(SeqEdges,aFace,Tol,E) ) {
314 // now we have only one edge - aChain.Value(1)
315 // we have to replace old ListEdges with this new edge
316 myContext->Replace(SeqEdges(1),E);
317 for(Standard_Integer j=2; j<=SeqEdges.Length(); j++) {
318 myContext->Remove(SeqEdges(j));
320 TopoDS_Face tmpF = TopoDS::Face(exp.Current());
321 if( !ChangedFaces.Contains(tmpF) )
322 ChangedFaces.Add(tmpF);
323 tmpF = TopoDS::Face(aMapFacesEdges.FindKey(i));
324 if( !ChangedFaces.Contains(tmpF) )
325 ChangedFaces.Add(tmpF);
329 } // end processing each face
331 } // end processing each solid
333 for(Standard_Integer i=1; i<=ChangedFaces.Extent(); i++) {
334 TopoDS_Face aFace = TopoDS::Face(myContext->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 //Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
342 //sfw->SetContext(myContext);
343 //sfw->SetPrecision(myTolerance);
344 //sfw->SetMinTolerance(myTolerance);
345 //sfw->SetMaxTolerance(Max(1.,myTolerance*1000.));
346 //sfw->SetFace(aFace);
347 //for ( TopoDS_Iterator iter(aFace,Standard_False); iter.More(); iter.Next()) {
348 // TopoDS_Wire wire = TopoDS::Wire ( iter.Value() );
350 // sfw->FixReorder();
351 // sfw->FixShifted();
355 aResult = myContext->Apply(Shape);