0024574: ICC compiler warnings on Windows
[occt.git] / src / ShapeExtend / ShapeExtend_Explorer.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 //szv#4 S4163
15
16 #include <BRep_Builder.hxx>
17 #include <ShapeExtend_Explorer.hxx>
18 #include <Standard_TypeMismatch.hxx>
19 #include <TopExp_Explorer.hxx>
20 #include <TopoDS_Compound.hxx>
21 #include <TopoDS_Iterator.hxx>
22 #include <TopoDS_Shape.hxx>
23 #include <TopoDS_Shell.hxx>
24 #include <TopoDS_Wire.hxx>
25 #include <TopTools_ListIteratorOfListOfShape.hxx>
26
27 //=======================================================================
28 //function : ShapeExtend_Explorer
29 //purpose  : 
30 //=======================================================================
31 ShapeExtend_Explorer::ShapeExtend_Explorer()
32 {
33 }
34
35 //=======================================================================
36 //function : CompoundFromSeq
37 //purpose  : 
38 //=======================================================================
39
40 TopoDS_Shape ShapeExtend_Explorer::CompoundFromSeq
41   (const Handle(TopTools_HSequenceOfShape)& seqval) const
42 {
43   BRep_Builder B;
44   TopoDS_Compound C;
45   B.MakeCompound(C);
46   Standard_Integer i,n = seqval->Length();
47   for (i = 1; i <= n ; i ++)  B.Add(C,seqval->Value(i));
48   return C;
49 }
50
51 //=======================================================================
52 //function : SeqFromCompound
53 //purpose  : 
54 //=======================================================================
55
56 static void FillList (const Handle(TopTools_HSequenceOfShape)& list,
57                       const TopoDS_Shape& comp, const Standard_Boolean expcomp)
58 {
59   for (TopoDS_Iterator it (comp); it.More(); it.Next()) {
60     TopoDS_Shape sub = it.Value();
61     if (sub.ShapeType() != TopAbs_COMPOUND) list->Append (sub);
62     else if (!expcomp) list->Append (sub);
63     else FillList (list,sub,expcomp);
64   }
65 }
66
67 Handle(TopTools_HSequenceOfShape) ShapeExtend_Explorer::SeqFromCompound
68        (const TopoDS_Shape& comp, const Standard_Boolean expcomp) const
69 {
70   Handle(TopTools_HSequenceOfShape) list = new TopTools_HSequenceOfShape();
71   if (comp.IsNull()) return list;
72   if (comp.ShapeType() != TopAbs_COMPOUND) {
73     list->Append (comp);
74     return list;
75   }
76   FillList (list,comp,expcomp);
77   return list;
78 }
79
80 //=======================================================================
81 //function : ListFromSeq
82 //purpose  : 
83 //=======================================================================
84
85 void ShapeExtend_Explorer::ListFromSeq (const Handle(TopTools_HSequenceOfShape)& seqval,
86                                        TopTools_ListOfShape& lisval,
87                                        const Standard_Boolean clear) const
88 {
89   if (clear) lisval.Clear();
90   if (seqval.IsNull()) return;
91   Standard_Integer i, nb = seqval->Length();
92   for (i = 1; i <= nb; i ++) lisval.Append (seqval->Value(i));
93 }
94
95 //=======================================================================
96 //function : SeqFromList
97 //purpose  : 
98 //=======================================================================
99
100 Handle(TopTools_HSequenceOfShape) ShapeExtend_Explorer::SeqFromList
101        (const TopTools_ListOfShape& lisval) const
102 {
103   Handle(TopTools_HSequenceOfShape) seqval = new TopTools_HSequenceOfShape();
104   TopTools_ListIteratorOfListOfShape it;
105   for (it.Initialize(lisval); it.More(); it.Next()) seqval->Append (it.Value());
106   return seqval;
107 }
108
109 //=======================================================================
110 //function : ShapeType
111 //purpose  : 
112 //=======================================================================
113
114 TopAbs_ShapeEnum ShapeExtend_Explorer::ShapeType (const TopoDS_Shape& shape,
115                                                  const Standard_Boolean compound) const
116 {
117   if (shape.IsNull()) return TopAbs_SHAPE;
118   TopAbs_ShapeEnum res = shape.ShapeType();
119   if (!compound || res != TopAbs_COMPOUND) return res;
120   res = TopAbs_SHAPE;
121   for (TopoDS_Iterator iter(shape); iter.More(); iter.Next()) {
122     TopoDS_Shape sh = iter.Value();
123     if (sh.IsNull()) continue;
124     TopAbs_ShapeEnum typ = sh.ShapeType();
125     if (typ == TopAbs_COMPOUND) typ = ShapeType (sh,compound);
126     if (res == TopAbs_SHAPE) res = typ;
127     //   Egalite : OK;  Pseudo-Egalite : EDGE/WIRE ou FACE/SHELL
128     else if (res == TopAbs_EDGE  && typ == TopAbs_WIRE)  res = typ;
129     else if (res == TopAbs_WIRE  && typ == TopAbs_EDGE)  continue;
130     else if (res == TopAbs_FACE  && typ == TopAbs_SHELL) res = typ;
131     else if (res == TopAbs_SHELL && typ == TopAbs_FACE)  continue;
132     else if (res != typ) return TopAbs_COMPOUND;
133   }
134   return res;
135 }
136
137 //=======================================================================
138 //function : SortedCompound
139 //purpose  : 
140 //=======================================================================
141
142 TopoDS_Shape ShapeExtend_Explorer::SortedCompound (const TopoDS_Shape& shape,
143                                                   const TopAbs_ShapeEnum type,
144                                                   const Standard_Boolean explore,
145                                                   const Standard_Boolean compound) const
146 {
147   if (shape.IsNull()) return shape;
148   TopAbs_ShapeEnum typ = shape.ShapeType();
149   TopoDS_Shape sh, sh0;
150   Standard_Integer nb = 0;
151
152   //  Compound : on le prend, soit tel quel, soit son contenu
153   if (typ == TopAbs_COMPOUND || typ == TopAbs_COMPSOLID) {
154     TopoDS_Compound C;
155     BRep_Builder B;
156     B.MakeCompound (C);
157     for (TopoDS_Iterator it(shape); it.More(); it.Next()) {
158       sh0 = SortedCompound (it.Value(),type,explore,compound);
159       if (sh0.IsNull()) continue;
160       sh = sh0;
161       typ = sh.ShapeType();
162       if (typ == TopAbs_COMPOUND && !compound) {
163         for (TopoDS_Iterator it2 (sh); it2.More(); it2.Next())
164           {  nb ++;  sh = it2.Value();  B.Add (C, sh);  }
165       }
166       else  {  nb ++;  B.Add (C,sh);  }
167     }
168     if (nb == 0) C.Nullify();
169     else if (nb == 1) return sh;
170     return C;
171   }
172
173   //   Egalite : OK;  Pseudo-Egalite : EDGE/WIRE ou FACE/SHELL
174   if (typ == type) return shape;
175   if (typ == TopAbs_EDGE && type == TopAbs_WIRE) {
176     BRep_Builder B;
177     TopoDS_Wire W;
178     B.MakeWire (W);
179     B.Add (W,shape);
180     return W;
181   }
182   if (typ == TopAbs_FACE && type == TopAbs_SHELL) {
183     BRep_Builder B;
184     TopoDS_Shell S;
185     B.MakeShell (S);
186     B.Add (S,shape);
187     return S;
188   }
189
190   //   Le reste : selon exploration
191   if (!explore) {
192     TopoDS_Shape nulsh;
193     return nulsh;
194   }
195
196   //  Ici, on doit explorer
197   //  SOLID + mode COMPOUND : reconduire les SHELLs
198   if (typ == TopAbs_SOLID && compound) {
199     TopoDS_Compound C;
200     BRep_Builder B;
201     B.MakeCompound (C);
202     for (TopoDS_Iterator it(shape); it.More(); it.Next()) {
203       sh0 = SortedCompound (it.Value(),type,explore,compound);
204       if (sh0.IsNull()) continue;
205       sh = sh0;
206       nb ++;  B.Add (C,sh);
207     }
208     if (nb == 0) C.Nullify();
209     else if (nb == 1) return sh;
210     return C;
211   }
212
213   //  Exploration classique
214   TopoDS_Compound CC;
215   BRep_Builder BB;
216   BB.MakeCompound(CC);
217   //Standard_Integer iena = Standard_False; //szv#4:S4163:12Mar99 unused
218   for (TopExp_Explorer aExp (shape,type); aExp.More(); aExp.Next()) {
219     nb ++;  sh = aExp.Current();
220     BB.Add (CC,sh);
221   }
222   if (nb == 0) CC.Nullify();
223   else if (nb == 1) return sh;
224   return CC;
225 }
226
227 //=======================================================================
228 //function : DispatchList
229 //purpose  : 
230 //=======================================================================
231
232 void ShapeExtend_Explorer::DispatchList (const Handle(TopTools_HSequenceOfShape)& list,
233                                          Handle(TopTools_HSequenceOfShape)& vertices,
234                                          Handle(TopTools_HSequenceOfShape)& edges,
235                                          Handle(TopTools_HSequenceOfShape)& wires,
236                                          Handle(TopTools_HSequenceOfShape)& faces,
237                                          Handle(TopTools_HSequenceOfShape)& shells,
238                                          Handle(TopTools_HSequenceOfShape)& solids,
239                                          Handle(TopTools_HSequenceOfShape)& compsols,
240                                          Handle(TopTools_HSequenceOfShape)& compounds) const
241 {
242   if (list.IsNull()) return;
243   if (vertices.IsNull())  vertices  = new TopTools_HSequenceOfShape();
244   if (edges.IsNull())     edges     = new TopTools_HSequenceOfShape();
245   if (wires.IsNull())     wires     = new TopTools_HSequenceOfShape();
246   if (faces.IsNull())     faces     = new TopTools_HSequenceOfShape();
247   if (shells.IsNull())    shells    = new TopTools_HSequenceOfShape();
248   if (solids.IsNull())    solids    = new TopTools_HSequenceOfShape();
249   if (compsols.IsNull())  compsols  = new TopTools_HSequenceOfShape();
250   if (compounds.IsNull()) compounds = new TopTools_HSequenceOfShape();
251
252   Standard_Integer i,nb = list->Length();
253   for (i = 1; i <= nb; i ++) {
254     TopoDS_Shape sh = list->Value(i);
255     if (sh.IsNull()) continue;
256     switch (sh.ShapeType()) {
257       case TopAbs_VERTEX    : vertices->Append(sh); break;
258       case TopAbs_EDGE      : edges->Append(sh); break;
259       case TopAbs_WIRE      : wires->Append(sh); break;
260       case TopAbs_FACE      : faces->Append(sh); break;
261       case TopAbs_SHELL     : shells->Append(sh); break;
262       case TopAbs_SOLID     : solids->Append(sh); break;
263       case TopAbs_COMPSOLID : compsols->Append(sh); break;
264       case TopAbs_COMPOUND  : compounds->Append(sh); break;
265       default : break;
266     }
267   }
268 }