9d2003a2bbbc6f5a32839613b4bc06170b1a8183
[occt.git] / src / BRepToIGES / BRepToIGES_BRShell.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 //:n3 abv 8 Feb 99: PRO17820: BRepTools::OuterWire() -> ShapeAnalysis::OuterWire
18 //szv#4 S4163
19
20 #include <BRep_Builder.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRepToIGES_BREntity.hxx>
23 #include <BRepToIGES_BRShell.hxx>
24 #include <BRepToIGES_BRWire.hxx>
25 #include <BRepTools.hxx>
26 #include <Geom2d_Curve.hxx>
27 #include <Geom_ConicalSurface.hxx>
28 #include <Geom_CylindricalSurface.hxx>
29 #include <Geom_RectangularTrimmedSurface.hxx>
30 #include <Geom_SphericalSurface.hxx>
31 #include <Geom_Surface.hxx>
32 #include <Geom_SurfaceOfRevolution.hxx>
33 #include <Geom_ToroidalSurface.hxx>
34 #include <GeomToIGES_GeomSurface.hxx>
35 #include <gp.hxx>
36 #include <gp_Trsf.hxx>
37 #include <IGESBasic_Group.hxx>
38 #include <IGESData_HArray1OfIGESEntity.hxx>
39 #include <IGESData_IGESEntity.hxx>
40 #include <IGESGeom_CurveOnSurface.hxx>
41 #include <IGESGeom_HArray1OfCurveOnSurface.hxx>
42 #include <IGESGeom_SurfaceOfRevolution.hxx>
43 #include <IGESGeom_TrimmedSurface.hxx>
44 #include <Interface_Macros.hxx>
45 #include <Message_ProgressIndicator.hxx>
46 #include <NCollection_IncAllocator.hxx>
47 #include <NCollection_Map.hxx>
48 #include <ShapeAlgo.hxx>
49 #include <ShapeAlgo_AlgoContainer.hxx>
50 #include <TColStd_HSequenceOfTransient.hxx>
51 #include <TopAbs_ShapeEnum.hxx>
52 #include <TopExp.hxx>
53 #include <TopExp_Explorer.hxx>
54 #include <TopLoc_Location.hxx>
55 #include <TopoDS.hxx>
56 #include <TopoDS_Edge.hxx>
57 #include <TopoDS_Face.hxx>
58 #include <TopoDS_Shape.hxx>
59 #include <TopoDS_Shell.hxx>
60 #include <TopoDS_Vertex.hxx>
61 #include <TopoDS_Wire.hxx>
62 #include <TopTools_ShapeMapHasher.hxx>
63 #include <Transfer_FinderProcess.hxx>
64
65 //=============================================================================
66 // BRepToIGES_BRShell
67 //=============================================================================
68 BRepToIGES_BRShell::BRepToIGES_BRShell()
69 {
70 }
71
72
73 //=============================================================================
74 // BRepToIGES_BRShell
75 //=============================================================================
76
77 BRepToIGES_BRShell::BRepToIGES_BRShell
78 (const BRepToIGES_BREntity& BR)
79 : BRepToIGES_BREntity(BR)
80 {
81 }
82
83
84 //=============================================================================
85 // TransferShell
86 //=============================================================================
87
88 Handle(IGESData_IGESEntity) BRepToIGES_BRShell ::TransferShell(const TopoDS_Shape& start)
89 {
90   Handle(IGESData_IGESEntity) res;
91
92   if (start.IsNull())  return  res;
93
94   if (start.ShapeType() == TopAbs_FACE) {
95     TopoDS_Face F =  TopoDS::Face(start);
96     res = TransferFace(F);
97   }  
98   else if (start.ShapeType() == TopAbs_SHELL) {
99     TopoDS_Shell S =  TopoDS::Shell(start);
100     res = TransferShell(S);
101   }  
102   else {
103     // message d`erreur
104   }  
105   return res;
106 }
107
108
109 //=============================================================================
110 // TransferFace
111 // 
112 //=============================================================================
113
114 Handle(IGESData_IGESEntity) BRepToIGES_BRShell ::TransferFace(const TopoDS_Face& start)
115 {
116   Handle(IGESData_IGESEntity) res;
117
118   Handle(Message_ProgressIndicator) progress = GetTransferProcess()->GetProgress();
119   if ( ! progress.IsNull() ) {
120     if ( progress->UserBreak() ) return res;
121     progress->Increment();
122   }
123   
124   if ( start.IsNull()) {
125     return res;
126   }
127
128   // pour explorer la face , il faut la mettre fORWARD.
129   TopoDS_Face myface;
130   if (start.Orientation() == TopAbs_REVERSED) {
131     //create face with redirected surface
132     BRep_Builder B;
133     TopLoc_Location aLoc;
134     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(start, aLoc);
135     while (aSurf->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface)))
136     {
137       // take basis surface, because pcurves will be transformed, so trim will be shifted,
138       // accorded to new face bounds
139       Handle(Geom_RectangularTrimmedSurface) aTrimmedSurf = 
140         Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
141       aSurf = aTrimmedSurf->BasisSurface();
142     }
143     aSurf = aSurf->UReversed();
144     Standard_Real aTol = BRep_Tool::Tolerance(start);
145     B.MakeFace(myface, aSurf, aLoc ,aTol);
146     // set specifics flags of a Face
147     B.NaturalRestriction(myface, BRep_Tool::NaturalRestriction(start));
148     //add wires
149     TopoDS_Wire anOuter = TopoDS::Wire(ShapeAlgo::AlgoContainer()->OuterWire(start));
150     TopExp_Explorer ex;
151     for (ex.Init(start,TopAbs_WIRE); ex.More(); ex.Next()) {
152       TopoDS_Wire W = TopoDS::Wire(ex.Current());
153       if (!W.IsNull() && W.IsSame(anOuter)) {
154         B.Add(myface, W);
155         break;
156       }
157     }     
158     for (ex.Init(start,TopAbs_WIRE); ex.More(); ex.Next()) {
159       TopoDS_Wire W = TopoDS::Wire(ex.Current());
160       if (!W.IsNull() && !W.IsSame(anOuter)) {
161         B.Add(myface, W);
162       }
163     }
164
165     // mirror pcurves
166     Standard_Real U1, U2, V1, V2;
167     aSurf->Bounds(U1, U2, V1, V2);
168     Standard_Real aCenter = 0.5 * (U1 + U2);
169     gp_Trsf2d T;
170     gp_Ax2d axis(gp_Pnt2d(aCenter, V1), gp_Dir2d(0.,1.));
171     T.SetMirror(axis);
172     NCollection_Map<TopoDS_Shape, TopTools_ShapeMapHasher> aMap (101, new NCollection_IncAllocator);
173     for (ex.Init(myface,TopAbs_EDGE);ex.More(); ex.Next()) {
174       TopoDS_Edge anEdge = TopoDS::Edge(ex.Current());
175       if (!aMap.Add(anEdge))
176         // seam edge has been already updated
177         continue;
178       Standard_Real f, l;
179       Handle(Geom2d_Curve) aCurve1, aCurve2;
180       aCurve1 = BRep_Tool::CurveOnSurface(anEdge, start, f, l);
181       aTol = BRep_Tool::Tolerance(anEdge);
182       if (!aCurve1.IsNull()) {
183         aCurve1 = Handle(Geom2d_Curve)::DownCast(aCurve1->Transformed(T));
184         if (BRepTools::IsReallyClosed(anEdge, start)) {
185           TopoDS_Edge revEdge = TopoDS::Edge(anEdge.Reversed());
186           aCurve2 = BRep_Tool::CurveOnSurface(revEdge, start, f, l);
187           if (!aCurve2.IsNull()) {
188             aCurve2 = Handle(Geom2d_Curve)::DownCast(aCurve2->Transformed(T));
189             if (anEdge.Orientation() == TopAbs_FORWARD)
190               B.UpdateEdge(anEdge, aCurve1, aCurve2, myface, aTol);
191             else
192               B.UpdateEdge(anEdge, aCurve2, aCurve1, myface, aTol);
193           }
194           else {
195             B.UpdateEdge(anEdge, aCurve1, myface, aTol);
196           }
197         }
198         else {
199           B.UpdateEdge(anEdge, aCurve1, myface, aTol);
200         }
201         // set range for degenerated edges
202         if (BRep_Tool::Degenerated(anEdge)) {
203           B.Range(anEdge, myface, f, l);
204         }
205       }
206     }
207   }
208   else {
209     myface = start;
210   }
211
212   //Standard_Integer Nb = 0; //szv#4:S4163:12Mar99 unused
213   Standard_Real Length = 1.;
214   Handle(IGESData_IGESEntity) ISurf;
215
216   // returns the face surface
217   // ------------------------
218
219   Handle(Geom_Surface) Surf = BRep_Tool::Surface(myface);
220   Handle(Geom_Surface) Surf1;
221
222   if (!Surf.IsNull()) {
223     Standard_Real U1, U2, V1, V2;
224     // pour limiter les surfaces de base
225     BRepTools::UVBounds(myface, U1, U2, V1, V2);  
226     GeomToIGES_GeomSurface GS;
227     GS.SetModel(GetModel());
228     ISurf = GS.TransferSurface(Surf, U1, U2, V1, V2);
229     if (ISurf.IsNull()) {
230       AddWarning (start, "the basic surface is a null entity");
231       return res;
232     }
233     Length = GS.Length();
234
235     // modif mjm du 17/07/97
236     if (Surf->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) { 
237       DeclareAndCast(Geom_RectangularTrimmedSurface, rectang, Surf);
238       Surf1 = rectang->BasisSurface();
239     }
240     else 
241       Surf1 = Surf;
242   }
243
244
245   // returns the wires of the face
246   // -----------------------------
247
248   BRepToIGES_BRWire BW(*this);
249   Standard_Integer Imode = 0; 
250   Standard_Integer Iprefer = 0; 
251   Handle(IGESData_IGESEntity) ICurve2d;
252
253   // outer wire
254   //:n3  TopoDS_Wire Outer = BRepTools::OuterWire(myface);
255   TopoDS_Wire Outer = ShapeAlgo::AlgoContainer()->OuterWire(myface); //:n3 
256   Handle(IGESGeom_CurveOnSurface) IOuter = new IGESGeom_CurveOnSurface;
257   if (!Outer.IsNull()) {
258     Handle(IGESData_IGESEntity) ICurve3d = 
259       BW.TransferWire(Outer, myface, ICurve2d, Length);
260     if ((!ICurve3d.IsNull()) && (!ICurve2d.IsNull())) Iprefer = 3; 
261     if ((!ICurve3d.IsNull()) && (ICurve2d.IsNull())) Iprefer = 2; 
262     if ((ICurve3d.IsNull()) && (!ICurve2d.IsNull())) Iprefer = 1; 
263     IOuter -> Init (Imode, ISurf, ICurve2d, ICurve3d, Iprefer);
264   }
265
266   // inners wires
267   TopExp_Explorer Ex;
268   Handle(TColStd_HSequenceOfTransient) Seq = new TColStd_HSequenceOfTransient();
269
270   for (Ex.Init(myface,TopAbs_WIRE); Ex.More(); Ex.Next()) {
271     TopoDS_Wire W = TopoDS::Wire(Ex.Current());
272     Handle(IGESGeom_CurveOnSurface) Curve = new IGESGeom_CurveOnSurface;
273     if (W.IsNull()) {
274       AddWarning(start," an Wire is a null entity");
275     }
276     else if (!W.IsSame(Outer)) {
277       Handle(IGESData_IGESEntity) ICurve3d = 
278               BW.TransferWire(W, myface, ICurve2d, Length);
279       if ((!ICurve3d.IsNull()) && (!ICurve2d.IsNull())) Iprefer = 3; 
280       if ((!ICurve3d.IsNull()) && (ICurve2d.IsNull())) Iprefer = 2; 
281       if ((ICurve3d.IsNull()) && (!ICurve2d.IsNull())) Iprefer = 1; 
282       Curve-> Init (Imode, ISurf, ICurve2d, ICurve3d, Iprefer);
283       if (!Curve.IsNull()) Seq->Append(Curve);
284     }
285   }
286
287   // all inners edges not in a wire
288   for (Ex.Init(myface,TopAbs_EDGE,TopAbs_WIRE); Ex.More(); Ex.Next()) {
289     TopoDS_Edge E = TopoDS::Edge(Ex.Current());
290     Handle(IGESGeom_CurveOnSurface) Curve = new IGESGeom_CurveOnSurface;
291     if (E.IsNull()) {
292       AddWarning(start," an Edge is a null entity");
293     }
294     else {
295       Handle(IGESData_IGESEntity) ICurve3d = BW.TransferEdge(E, Standard_False);
296       Handle(IGESData_IGESEntity) newICurve2d = BW.TransferEdge(E, myface, Length, Standard_False);
297       if ((!ICurve3d.IsNull()) && (!newICurve2d.IsNull())) Iprefer = 3; 
298       if ((!ICurve3d.IsNull()) && (newICurve2d.IsNull())) Iprefer = 2; 
299       if ((ICurve3d.IsNull()) && (!newICurve2d.IsNull())) Iprefer = 1; 
300       Curve-> Init (Imode, ISurf, newICurve2d, ICurve3d, Iprefer);
301       if (!Curve.IsNull()) Seq->Append(Curve);
302     }
303   }
304
305
306   Standard_Integer nbent = Seq->Length();
307   Handle(IGESGeom_HArray1OfCurveOnSurface) Tab;
308   if (nbent >=1) {
309     Tab = new IGESGeom_HArray1OfCurveOnSurface(1,nbent);
310     for (Standard_Integer itab = 1; itab <= nbent; itab++) {
311       Handle(IGESGeom_CurveOnSurface) item = GetCasted(IGESGeom_CurveOnSurface, Seq->Value(itab));
312       Tab->SetValue(itab,item);
313     }
314   }
315
316   // returns the TrimmedSurface
317   // --------------------------
318   Handle(IGESGeom_TrimmedSurface) TrimmedSurf = new IGESGeom_TrimmedSurface;
319   if (BRep_Tool::NaturalRestriction(start)) {
320     //if face bounds and surface bounds are same, outer wire is unnecessary
321     TrimmedSurf-> Init (ISurf, Standard_False, NULL, Tab);
322   }
323   else
324     TrimmedSurf-> Init (ISurf, Standard_True, IOuter, Tab);
325
326   res = TrimmedSurf;
327   SetShapeResult ( start, res );
328   return res;
329 }
330
331
332 //=============================================================================
333 // TransferShell
334 //=============================================================================
335
336 Handle(IGESData_IGESEntity) BRepToIGES_BRShell::TransferShell(const TopoDS_Shell& start)
337 {
338   Handle(IGESData_IGESEntity) res;
339   if ( start.IsNull()) return res;
340
341   TopExp_Explorer Ex;
342   Handle(IGESBasic_Group) IGroup = new IGESBasic_Group;
343   Handle(TColStd_HSequenceOfTransient) Seq = new TColStd_HSequenceOfTransient();
344   Handle(IGESData_IGESEntity) IFace;
345
346   for (Ex.Init(start,TopAbs_FACE); Ex.More(); Ex.Next()) {
347     TopoDS_Face F = TopoDS::Face(Ex.Current());
348     if (F.IsNull()) {
349       AddWarning(start," a Face is a null entity");
350     }
351     else {
352       IFace = TransferFace(F);
353       if (!IFace.IsNull()) Seq->Append(IFace);
354     }
355   }
356
357
358   Standard_Integer nbfaces = Seq->Length();  
359   Handle(IGESData_HArray1OfIGESEntity) Tab;
360   if ( nbfaces >= 1) {
361     Tab = new IGESData_HArray1OfIGESEntity(1,nbfaces);
362     for (Standard_Integer itab = 1; itab <= nbfaces; itab++) {
363       Handle(IGESData_IGESEntity) item = GetCasted(IGESData_IGESEntity, Seq->Value(itab));
364       Tab->SetValue(itab,item);
365     }
366   }
367
368   if (nbfaces == 1) {
369     res = IFace;
370   }
371   else {
372     IGroup->Init(Tab);
373     res = IGroup;
374   }
375
376   SetShapeResult ( start, res );
377
378   return res;
379 }
380
381
382
383