]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMAlgo/BlockFix_CheckTool.cxx
Salome HOME
Merge with OCC_development_01
[modules/geom.git] / src / GEOMAlgo / BlockFix_CheckTool.cxx
1 // File:      BlockFix_CheckTool.cxx
2 // Created:   17.12.04 11:15:25
3 // Author:    Sergey KUUL
4 // Copyright: Open CASCADE SA 2004
5
6 #include <BlockFix_CheckTool.ixx>
7
8 #include <BRep_Tool.hxx>
9 #include <BlockFix_UnionEdges.hxx>
10 #include <BlockFix_UnionFaces.hxx>
11 #include <TopExp.hxx>
12 #include <TopExp_Explorer.hxx>
13 #include <TopoDS.hxx>
14 #include <TopoDS_Edge.hxx>
15 #include <TopoDS_Face.hxx>
16 #include <TopoDS_Solid.hxx>
17 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
18 #include <TopTools_MapOfShape.hxx>
19 #include <TopTools_ListOfShape.hxx>
20 #include <TopTools_ListIteratorOfListOfShape.hxx>
21
22
23 //=======================================================================
24 //function : BlockFix_CheckTool()
25 //purpose  : Constructor
26 //=======================================================================
27
28 BlockFix_CheckTool::BlockFix_CheckTool( )
29 {
30   myHasCheck = Standard_False;
31   myPossibleBlocks.Clear();
32 }
33
34
35 //=======================================================================
36 //function : SetShape
37 //purpose  : 
38 //=======================================================================
39
40 void BlockFix_CheckTool::SetShape(const TopoDS_Shape& aShape)
41 {
42   myHasCheck = Standard_False;
43   myShape = aShape;
44   myPossibleBlocks.Clear();
45 }
46
47
48 //=======================================================================
49 //function : Perform
50 //purpose  : 
51 //=======================================================================
52
53 void BlockFix_CheckTool::Perform() 
54 {
55   myNbSolids=0;
56   myNbBlocks=0;
57   myNbUF=0;
58   myNbUE=0;
59   myNbUFUE=0;
60   TopExp_Explorer exps;
61   for(exps.Init(myShape, TopAbs_SOLID); exps.More(); exps.Next()) {
62     TopoDS_Solid aSolid = TopoDS::Solid(exps.Current());
63     myNbSolids++;
64     Standard_Boolean IsBlock=Standard_True;
65     Standard_Boolean MayBeUF=Standard_False;
66     Standard_Boolean MayBeUE=Standard_False;
67     Standard_Integer nf=0;
68     TopExp_Explorer expf;
69     for(expf.Init(aSolid, TopAbs_FACE); expf.More(); expf.Next()) nf++;
70
71     if(nf<6) {
72       IsBlock=Standard_False;
73     }
74     else if(nf>6) {
75       IsBlock=Standard_False;
76       // check faces unification
77       TopTools_SequenceOfShape faces;
78       for( expf.Init(aSolid, TopAbs_FACE); expf.More(); expf.Next()) {
79         TopoDS_Face aFace = TopoDS::Face(expf.Current());
80         faces.Append(aFace);
81       }
82       Standard_Boolean HasFacesForUnification = Standard_False;
83       for(Standard_Integer i=1; i<faces.Length() && !HasFacesForUnification; i++) {
84         TopoDS_Face F1 = TopoDS::Face(faces.Value(i));
85         TopTools_MapOfShape Edges;
86         for(TopExp_Explorer expe(F1,TopAbs_EDGE); expe.More(); expe.Next())
87           Edges.Add(expe.Current().Oriented(TopAbs_FORWARD));
88         TopLoc_Location L1;
89         Handle(Geom_Surface) S1 = BRep_Tool::Surface(F1,L1);
90         for(Standard_Integer j=i+1; j<=faces.Length() && !HasFacesForUnification; j++) {
91           TopoDS_Face F2 = TopoDS::Face(faces.Value(j));
92           TopLoc_Location L2;
93           Handle(Geom_Surface) S2 = BRep_Tool::Surface(F2,L2);
94           if( S1==S2 && L1==L2 ) {
95             // faces have equal based surface
96             // now check common edge
97             for(TopExp_Explorer expe2(F2,TopAbs_EDGE); expe2.More(); expe2.Next()) {
98               if(Edges.Contains(expe2.Current().Oriented(TopAbs_FORWARD))) {
99                 HasFacesForUnification = Standard_True;
100                 break;
101               }
102             }
103           }
104         }
105       }
106       if(HasFacesForUnification) {
107         MayBeUF=Standard_True;
108       }
109     }
110
111     Standard_Integer ne=0;
112     TopExp_Explorer expe;
113     for(expe.Init(aSolid, TopAbs_EDGE); expe.More(); expe.Next()) ne++;
114     ne = ne/2;
115     if(ne<12)
116       IsBlock=Standard_False;
117     if(ne>12) {
118       IsBlock=Standard_False;
119       // check edges unification
120       // creating map of edge faces
121       TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
122       TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
123       for(expf.Init(aSolid, TopAbs_FACE); expf.More(); expf.Next()) {
124         TopoDS_Face aFace = TopoDS::Face(expf.Current());
125         TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
126         for(expe.Init(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
127           TopoDS_Edge edge = TopoDS::Edge(expe.Current());
128           if(!aMapEdgeFaces.Contains(edge)) continue;
129           const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
130           TopTools_ListIteratorOfListOfShape anIter(aList);
131           for( ; anIter.More(); anIter.Next()) {
132             TopoDS_Face face = TopoDS::Face(anIter.Value());
133             if(face.IsSame(aFace)) continue;
134             if(aMapFacesEdges.Contains(face)) {
135               aMapFacesEdges.ChangeFromKey(face).Append(edge);
136             }
137             else {
138               TopTools_ListOfShape ListEdges;
139               ListEdges.Append(edge);
140               aMapFacesEdges.Add(face,ListEdges);
141             }
142           }
143         }
144         Standard_Integer i = 1;
145         for (; i <= aMapFacesEdges.Extent(); i++) {
146           const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
147           if (ListEdges.Extent() > 1) break;
148         }
149         if (i <= aMapFacesEdges.Extent()) {
150           MayBeUE = Standard_True;
151           break;
152         }
153       }
154     }
155
156     if(IsBlock) 
157       myNbBlocks++;
158     else {
159       if(MayBeUF) {
160         myPossibleBlocks.Append(aSolid);
161         if(MayBeUE)
162           myNbUFUE++;
163         else
164           myNbUF++;
165       }
166       else if(MayBeUE) {
167         myNbUE++;
168         myPossibleBlocks.Append(aSolid);
169       }
170     }
171
172   }
173
174   myHasCheck = Standard_True;
175 }
176
177
178 //=======================================================================
179 //function : NbPossibleBlocks
180 //purpose  : 
181 //=======================================================================
182
183 Standard_Integer BlockFix_CheckTool::NbPossibleBlocks() const
184 {
185   return myPossibleBlocks.Length();
186 }
187
188
189 //=======================================================================
190 //function : PossibleBlock
191 //purpose  : 
192 //=======================================================================
193
194 TopoDS_Shape BlockFix_CheckTool::PossibleBlock(const Standard_Integer num) const
195 {
196   TopoDS_Shape res;
197   if( num>0 && num<=myPossibleBlocks.Length() ) 
198     res = myPossibleBlocks.Value(num);
199   return res;
200 }
201
202
203 //=======================================================================
204 //function : DumpCheckResult
205 //purpose  : 
206 //=======================================================================
207
208 void BlockFix_CheckTool::DumpCheckResult(Standard_OStream& S) const
209 {
210   if(!myHasCheck)
211     S<<"Check not performed!"<<endl;
212   else {
213     S<<"dump results of check:"<<endl;
214     S<<"  total number of solids = "<<myNbSolids<<endl;
215     S<<"  including: number of good blocks = "<<myNbBlocks<<endl;
216     S<<"             number of possible blocks = "<<NbPossibleBlocks()<<endl;
217     Standard_Integer nbtmp = myNbSolids - myNbBlocks - NbPossibleBlocks();
218     S<<"             number of impossible blocks = "<<nbtmp<<endl;
219     S<<"             including: need unionfaces = "<<myNbUF<<endl;
220     S<<"                        need unionedges = "<<myNbUE<<endl;
221     S<<"                        need both unionfaces and unionedges = "<<myNbUFUE<<endl;
222   }
223 }
224