0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / BRepToIGES / BRepToIGES_BRSolid.cxx
1 // Created on: 1995-01-30
2 // Created by: Marie Jose MARTZ
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <BRep_Tool.hxx>
19 #include <BRepToIGES_BREntity.hxx>
20 #include <BRepToIGES_BRShell.hxx>
21 #include <BRepToIGES_BRSolid.hxx>
22 #include <BRepToIGES_BRWire.hxx>
23 #include <BRepTools.hxx>
24 #include <IGESBasic_Group.hxx>
25 #include <IGESData_HArray1OfIGESEntity.hxx>
26 #include <IGESData_IGESEntity.hxx>
27 #include <Interface_Macros.hxx>
28 #include <TColStd_HSequenceOfTransient.hxx>
29 #include <TopAbs_ShapeEnum.hxx>
30 #include <TopExp.hxx>
31 #include <TopExp_Explorer.hxx>
32 #include <TopoDS.hxx>
33 #include <TopoDS_Compound.hxx>
34 #include <TopoDS_CompSolid.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Face.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Shell.hxx>
39 #include <TopoDS_Solid.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <TopoDS_Wire.hxx>
42
43 // At first only the geometry is translated (point, curve...)
44 //=============================================================================
45 // BRepToIGES_BRSolid
46 //=============================================================================
47 BRepToIGES_BRSolid::BRepToIGES_BRSolid()
48 {
49 }
50
51
52 //=============================================================================
53 // BRepToIGES_BRSolid
54 //=============================================================================
55
56 BRepToIGES_BRSolid::BRepToIGES_BRSolid
57 (const BRepToIGES_BREntity& BR)
58 : BRepToIGES_BREntity(BR)
59 {
60 }
61
62
63 //=============================================================================
64 // TransferSolid
65 //=============================================================================
66
67 Handle(IGESData_IGESEntity) BRepToIGES_BRSolid ::TransferSolid(const TopoDS_Shape& start)
68 {
69   Handle(IGESData_IGESEntity) res;
70
71   if (start.IsNull())  return  res;
72
73   if (start.ShapeType() == TopAbs_SOLID) {
74     TopoDS_Solid M =  TopoDS::Solid(start);
75     res = TransferSolid(M);
76   }  
77   else if (start.ShapeType() == TopAbs_COMPSOLID) {
78     TopoDS_CompSolid C =  TopoDS::CompSolid(start);
79     res = TransferCompSolid(C);
80   }  
81   else if (start.ShapeType() == TopAbs_COMPOUND) {
82     TopoDS_Compound C =  TopoDS::Compound(start);
83     res = TransferCompound(C);
84   }  
85   else {
86     // error message
87   }  
88   return res;
89 }
90
91
92 //=============================================================================
93 // TransferSolid
94 // 
95 //=============================================================================
96
97 Handle(IGESData_IGESEntity) BRepToIGES_BRSolid ::TransferSolid(const TopoDS_Solid& start)
98 {
99   Handle(IGESData_IGESEntity) res;
100   if ( start.IsNull()) return res;
101
102   TopExp_Explorer Ex;
103   Handle(IGESData_IGESEntity) IShell;
104   BRepToIGES_BRShell BS(*this);
105   Handle(TColStd_HSequenceOfTransient) Seq = new TColStd_HSequenceOfTransient();
106
107   for (Ex.Init(start,TopAbs_SHELL); Ex.More(); Ex.Next()) {
108     TopoDS_Shell S = TopoDS::Shell(Ex.Current());
109     if (S.IsNull()) {
110       AddWarning(start," an Shell is a null entity");
111     }
112     else {
113       IShell = BS.TransferShell(S);
114       if (!IShell.IsNull()) Seq->Append(IShell);
115     }
116   }
117
118
119   Standard_Integer nbshells = Seq->Length();
120   Handle(IGESData_HArray1OfIGESEntity) Tab;
121   if ( nbshells >= 1) {
122     Tab =  new IGESData_HArray1OfIGESEntity(1,nbshells);
123     for (Standard_Integer itab = 1; itab <= nbshells; itab++) {
124       Handle(IGESData_IGESEntity) item = GetCasted(IGESData_IGESEntity, Seq->Value(itab));
125       Tab->SetValue(itab,item);
126     }
127   }
128
129   if (nbshells == 1) {
130     res = IShell;
131   }
132   else {
133     Handle(IGESBasic_Group) IGroup = new IGESBasic_Group;
134     IGroup->Init(Tab);
135     res = IGroup;
136   }
137
138   SetShapeResult ( start, res );
139
140   return res;
141 }
142
143
144 //=============================================================================
145 // TransferCompSolid
146 //=============================================================================
147
148 Handle(IGESData_IGESEntity) BRepToIGES_BRSolid ::TransferCompSolid(const TopoDS_CompSolid& start)
149 {
150   Handle(IGESData_IGESEntity) res;
151   if ( start.IsNull()) return res;
152
153   TopExp_Explorer Ex;
154   Handle(IGESData_IGESEntity) ISolid;
155   Handle(TColStd_HSequenceOfTransient) Seq = new TColStd_HSequenceOfTransient();
156
157   for (Ex.Init(start,TopAbs_SOLID); Ex.More(); Ex.Next()) {
158     TopoDS_Solid S = TopoDS::Solid(Ex.Current());
159     if (S.IsNull()) {
160       AddWarning(start," an Solid is a null entity");
161     }
162     else {
163       ISolid = TransferSolid(S);
164       if (!ISolid.IsNull()) Seq->Append(ISolid);
165     }
166   }
167
168
169   Standard_Integer nbsolids = Seq->Length();
170   Handle(IGESData_HArray1OfIGESEntity) Tab;
171   if ( nbsolids >= 1) {
172     Tab =  new IGESData_HArray1OfIGESEntity(1,nbsolids);
173     for (Standard_Integer itab = 1; itab <= nbsolids; itab++) {
174       Handle(IGESData_IGESEntity) item = GetCasted(IGESData_IGESEntity, Seq->Value(itab));
175       Tab->SetValue(itab,item);
176     }
177   }
178
179   if (nbsolids == 1) {
180     res = ISolid;
181   }
182   else {
183     Handle(IGESBasic_Group) IGroup = new IGESBasic_Group;
184     IGroup->Init(Tab);
185     res = IGroup;
186   }
187
188   SetShapeResult ( start, res );
189
190   return res;
191 }
192
193
194 //=============================================================================
195 // TransferCompound
196 //=============================================================================
197
198 Handle(IGESData_IGESEntity) BRepToIGES_BRSolid ::TransferCompound(const TopoDS_Compound& start)
199 {
200   Handle(IGESData_IGESEntity) res;
201   if ( start.IsNull()) return res;
202
203
204   TopExp_Explorer Ex;
205   Handle(IGESData_IGESEntity) IShape;
206   BRepToIGES_BRShell BS(*this);
207   BRepToIGES_BRWire BW(*this);
208   Handle(TColStd_HSequenceOfTransient) Seq = new TColStd_HSequenceOfTransient();
209
210   // take all Solids
211   for (Ex.Init(start, TopAbs_SOLID); Ex.More(); Ex.Next()) {
212     TopoDS_Solid S = TopoDS::Solid(Ex.Current());
213     if (S.IsNull()) {
214       AddWarning(start," a Solid is a null entity");
215     }
216     else {
217       IShape = TransferSolid(S);
218       if (!IShape.IsNull()) Seq->Append(IShape);
219     }
220   }
221
222   // take all isolated Shells 
223   for (Ex.Init(start, TopAbs_SHELL, TopAbs_SOLID); Ex.More(); Ex.Next()) {
224     TopoDS_Shell S = TopoDS::Shell(Ex.Current());
225     if (S.IsNull()) {
226       AddWarning(start," a Shell is a null entity");
227     }
228     else {
229       IShape = BS.TransferShell(S);
230       if (!IShape.IsNull()) Seq->Append(IShape);
231     }
232   }
233
234
235   // take all isolated Faces 
236   for (Ex.Init(start, TopAbs_FACE, TopAbs_SHELL); Ex.More(); Ex.Next()) {
237     TopoDS_Face S = TopoDS::Face(Ex.Current());
238     if (S.IsNull()) {
239       AddWarning(start," a Face is a null entity");
240     }
241     else {
242       IShape = BS.TransferFace(S);
243       if (!IShape.IsNull()) Seq->Append(IShape);
244     }
245   }
246
247
248   // take all isolated Wires 
249   for (Ex.Init(start, TopAbs_WIRE, TopAbs_FACE); Ex.More(); Ex.Next()) {
250     TopoDS_Wire S = TopoDS::Wire(Ex.Current());
251     if (S.IsNull()) {
252       AddWarning(start," a Wire is a null entity");
253     }
254     else {
255       IShape = BW.TransferWire(S);
256       if (!IShape.IsNull()) Seq->Append(IShape);
257     }
258   }
259
260
261   // take all isolated Edges 
262   for (Ex.Init(start, TopAbs_EDGE, TopAbs_WIRE); Ex.More(); Ex.Next()) {
263     TopoDS_Edge S = TopoDS::Edge(Ex.Current());
264     if (S.IsNull()) {
265       AddWarning(start," an Edge is a null entity");
266     }
267     else {
268       IShape = BW.TransferEdge(S, Standard_False);
269       if (!IShape.IsNull()) Seq->Append(IShape);
270     }
271   }
272
273
274   // take all isolated Vertices 
275   for (Ex.Init(start, TopAbs_VERTEX, TopAbs_EDGE); Ex.More(); Ex.Next()) {
276     TopoDS_Vertex S = TopoDS::Vertex(Ex.Current());
277     if (S.IsNull()) {
278       AddWarning(start," a Vertex is a null entity");
279     }
280     else {
281       IShape = BW.TransferVertex(S);
282       if (!IShape.IsNull()) Seq->Append(IShape);
283     }
284   }
285
286   // construct the group
287   Standard_Integer nbshapes = Seq->Length();
288   Handle(IGESData_HArray1OfIGESEntity) Tab;
289   if (nbshapes >=1) {
290     Tab =  new IGESData_HArray1OfIGESEntity(1,nbshapes);
291     for (Standard_Integer itab = 1; itab <= nbshapes; itab++) {
292       Handle(IGESData_IGESEntity) item = GetCasted(IGESData_IGESEntity, Seq->Value(itab));
293       Tab->SetValue(itab,item);
294     }
295   }
296
297   if (nbshapes == 1) {
298     res = IShape;
299   }
300   else {
301     Handle(IGESBasic_Group) IGroup = new IGESBasic_Group;
302     IGroup->Init(Tab);
303     res = IGroup;
304   }
305
306   SetShapeResult ( start, res );
307
308   return res;
309 }