0031501: Foundation Classes, Message_Printer - remove theToPutEndl argument -- prepar...
[occt.git] / src / StepToTopoDS / StepToTopoDS_TranslateFace.cxx
1 // Created on: 1995-01-03
2 // Created by: Frederic MAUPAS
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 // pdn 30.11.98: fixes improved
18 // pdn 20.12.98: to keep pcurves
19 // pdn 28.12.98: PRO10366 shifting pcurve between two singularities
20 //:p4 abv, pdn 23.02.99: PRO9234 #15720: call BRepTools::Update() for faces
21 //:q7 abv 23.03.99: bm4_al_eye.stp #53710: remove pseudo-seams
22 //    rln 31.03.99 S4135: prohibit fixing intersection of non-adjacent edges (temporarily)
23 //#4  szv          S4163: optimization
24 //%19 pdn 17.04.99 using ShapeFix_Wire::FixEdgeCurves instead of ShapeFix_PCurves
25 //    smh 31.01.01 Bad data in file : case of vertex loop on plane face
26 // sln 01.10.2001 BUC61003. StepToTopoDS_TranslateFace::Init function is corrected (verifying  Handle(...).IsNull() is added)
27
28 #include <BRep_Builder.hxx>
29 #include <BRep_TEdge.hxx>
30 #include <BRep_Tool.hxx>
31 #include <BRep_TVertex.hxx>
32 #include <BRepBuilderAPI_MakeFace.hxx>
33 #include <BRepTools.hxx>
34 #include <Geom2d_Curve.hxx>
35 #include <Geom_BoundedSurface.hxx>
36 #include <Geom_BSplineSurface.hxx>
37 #include <Geom_Plane.hxx>
38 #include <Geom_SphericalSurface.hxx>
39 #include <Geom_Surface.hxx>
40 #include <Geom_SurfaceOfRevolution.hxx>
41 #include <Geom_ToroidalSurface.hxx>
42 #include <GeomAbs_Shape.hxx>
43 #include <GeomAdaptor_HSurface.hxx>
44 #include <GeomAdaptor_Surface.hxx>
45 #include <Precision.hxx>
46 #include <ShapeAlgo.hxx>
47 #include <ShapeAlgo_AlgoContainer.hxx>
48 #include <StdFail_NotDone.hxx>
49 #include <StepGeom_BSplineSurface.hxx>
50 #include <StepGeom_BSplineSurfaceForm.hxx>
51 #include <StepGeom_OffsetSurface.hxx>
52 #include <StepGeom_Surface.hxx>
53 #include <StepShape_Edge.hxx>
54 #include <StepShape_EdgeCurve.hxx>
55 #include <StepShape_EdgeLoop.hxx>
56 #include <StepShape_FaceBound.hxx>
57 #include <StepShape_FaceOuterBound.hxx>
58 #include <StepShape_FaceSurface.hxx>
59 #include <StepShape_OrientedEdge.hxx>
60 #include <StepShape_PolyLoop.hxx>
61 #include <StepShape_VertexLoop.hxx>
62 #include <StepToGeom.hxx>
63 #include <StepToTopoDS.hxx>
64 #include <StepToTopoDS_NMTool.hxx>
65 #include <StepToTopoDS_Tool.hxx>
66 #include <StepToTopoDS_TranslateEdgeLoop.hxx>
67 #include <StepToTopoDS_TranslateFace.hxx>
68 #include <StepToTopoDS_TranslatePolyLoop.hxx>
69 #include <StepToTopoDS_TranslateVertexLoop.hxx>
70 #include <TCollection_HAsciiString.hxx>
71 #include <TopExp.hxx>
72 #include <TopExp_Explorer.hxx>
73 #include <TopoDS.hxx>
74 #include <TopoDS_Edge.hxx>
75 #include <TopoDS_Face.hxx>
76 #include <TopoDS_Iterator.hxx>
77 #include <TopoDS_Shape.hxx>
78 #include <TopoDS_Vertex.hxx>
79 #include <TopoDS_Wire.hxx>
80 #include <Transfer_TransientProcess.hxx>
81 #include <StepGeom_RectangularTrimmedSurface.hxx>
82 #include <StepGeom_ToroidalSurface.hxx>
83
84 //#3 rln 16/02/98
85 //#include <GeomAdaptor_Curve.hxx>
86 //#include <GeomAdaptor_CurveOnSurface.hxx>
87 //#3 rln 16/02/98
88 //rln 28/01/98
89 //rln 28/01/98
90 //  Provisoire, pour VertexLoop
91 //:d4
92 // To proceed with I-DEAS-like STP (ssv; 15.11.2010)
93 //#define DEBUG
94 // ============================================================================
95 // Method  : StepToTopoDS_TranslateFace::StepToTopoDS_TranslateFace
96 // Purpose : Empty Constructor
97 // ============================================================================
98 StepToTopoDS_TranslateFace::StepToTopoDS_TranslateFace()
99 {
100   done = Standard_False;
101 }
102
103 // ============================================================================
104 // Method  : StepToTopoDS_TranslateFace::StepToTopoDS_TranslateFace
105 // Purpose : Constructor with a FaceSurface and a Tool
106 // ============================================================================
107
108 StepToTopoDS_TranslateFace::StepToTopoDS_TranslateFace
109 (const Handle(StepShape_FaceSurface)& FS, StepToTopoDS_Tool& T, StepToTopoDS_NMTool& NMTool)
110 {
111   Init(FS, T, NMTool);
112 }
113
114 // ============================================================================
115 // Method  : Init
116 // Purpose : Init with a FaceSurface and a Tool
117 // ============================================================================
118
119 static inline Standard_Boolean isReversed(const Handle(StepGeom_Surface)& theStepSurf)
120 {
121   Handle(StepGeom_ToroidalSurface) aStepTorSur;
122   if(theStepSurf->IsKind(STANDARD_TYPE(StepGeom_RectangularTrimmedSurface)))
123     return isReversed(Handle(StepGeom_RectangularTrimmedSurface)::DownCast(theStepSurf)->BasisSurface());
124   
125   else
126     aStepTorSur = Handle(StepGeom_ToroidalSurface)::DownCast(theStepSurf);
127   
128   return (!aStepTorSur.IsNull() && aStepTorSur->MajorRadius() < 0 ? Standard_True : Standard_False);
129 }
130
131 // ============================================================================
132 // Method  : Init
133 // Purpose : Init with a FaceSurface and a Tool
134 // ============================================================================
135
136 void StepToTopoDS_TranslateFace::Init
137 (const Handle(StepShape_FaceSurface)& FS, StepToTopoDS_Tool& aTool, StepToTopoDS_NMTool& NMTool)
138 {
139   done = Standard_True;
140   if (aTool.IsBound(FS)) {
141     myResult = TopoDS::Face(aTool.Find(FS));
142     myError  = StepToTopoDS_TranslateFaceDone;
143     done     = Standard_True;
144     return;
145   }
146   
147   Handle(Transfer_TransientProcess) TP = aTool.TransientProcess();
148   
149   // ----------------------------------------------
150   // Map the Face Geometry and create a TopoDS_Face
151   // ----------------------------------------------
152   
153   Handle(StepGeom_Surface) StepSurf = FS->FaceGeometry();
154
155    // sln 01.10.2001 BUC61003. If corresponding entity was read with error StepSurface may be NULL. In this case we exit from function
156   if ( StepSurf.IsNull() ) {
157     TP->AddFail(StepSurf," Surface has not been created");
158     myError = StepToTopoDS_TranslateFaceOther;
159     done = Standard_False;
160     return;
161   }
162
163   // [BEGIN] Added to process non-manifold topology (ssv; 14.11.2010)
164   if ( NMTool.IsActive() && NMTool.IsBound(StepSurf) ) {
165     TopoDS_Shape existingShape = NMTool.Find(StepSurf);
166     // Reverse shape's orientation for the next shell
167     existingShape.Reverse();
168     myResult = existingShape;
169     myError  = StepToTopoDS_TranslateFaceDone;
170     done = Standard_True;
171     return;
172   }
173   // [END] Added to process non-manifold topology (ssv; 14.11.2010)
174
175   if (StepSurf->IsKind(STANDARD_TYPE(StepGeom_OffsetSurface))) //:d4 abv 12 Mar 98
176     TP->AddWarning(StepSurf," Type OffsetSurface is out of scope of AP 214");
177   Handle(Geom_Surface) GeomSurf = StepToGeom::MakeSurface (StepSurf);
178   if (GeomSurf.IsNull())
179   {
180     TP->AddFail(StepSurf," Surface has not been created");
181     myError = StepToTopoDS_TranslateFaceOther;
182     done = Standard_False;
183     return;
184   }
185   // pdn to force bsplsurf to be periodic
186   Handle(StepGeom_BSplineSurface) sgbss = Handle(StepGeom_BSplineSurface)::DownCast(StepSurf);
187   if (!sgbss.IsNull()) {
188     Handle(Geom_Surface) periodicSurf = ShapeAlgo::AlgoContainer()->ConvertToPeriodic(GeomSurf);
189     if (!periodicSurf.IsNull()) {
190       TP->AddWarning(StepSurf, "Surface forced to be periodic");
191       GeomSurf = periodicSurf;
192     }
193   }
194     
195   Standard_Boolean sameSenseFace = FS->SameSense();
196
197   //fix for bug 0026376 Solid Works wrote face based on toroidal surface having negative major radius
198   //seems that such case is interpreted  by "Solid Works" and "ProE" as face having reversed orientation.
199   Standard_Boolean sameSense = (isReversed(StepSurf) ? !sameSenseFace : sameSenseFace);
200   
201   // -- Statistics --
202   aTool.AddContinuity (GeomSurf);
203   
204   TopoDS_Face   F;
205   BRep_Builder B;
206   B.MakeFace ( F, GeomSurf, Precision::Confusion() );
207   
208   // ----------------------------------
209   // Iterate on each FaceBounds (Wires)
210   // ----------------------------------
211   
212   Handle(StepShape_FaceBound) FaceBound;
213   Handle(StepShape_Loop)      Loop;
214   
215   StepToTopoDS_TranslateVertexLoop myTranVL;
216   StepToTopoDS_TranslatePolyLoop   myTranPL;
217   StepToTopoDS_TranslateEdgeLoop   myTranEdgeLoop;
218   
219   Standard_Integer NbBnd = FS->NbBounds();
220
221   // --  Critere de couture simple (CKY, JAN97)
222   // surface periodique (typiquement un cylindre)
223   // 2 face bounds, chacun avec un edge loop d une seule edge
224   //  cette edge est fermee, c-a-d vtx-deb = vtx-fin (pour les deux edges)
225   // est-ce suffisant (verifier que ce sont deux outer-bounds ... ?? comment ?)
226   // Alors on peut dire : face a deux bords dont la couture manque
227   // La couture est entre les deux vertex
228
229   for (Standard_Integer i = 1; i <= NbBnd; i ++) {
230
231 #ifdef OCCT_DEBUG
232     std::cout << "    Processing Wire : " << i << std::endl;
233 #endif    
234     FaceBound = FS->BoundsValue(i);
235     Loop      = FaceBound->Bound();
236     
237     // ------------------------
238     // The Loop is a VertexLoop
239     // ------------------------
240     
241     if (Loop->IsKind(STANDARD_TYPE(StepShape_VertexLoop))) {
242 //:S4136      STF.Closed() = Standard_False;
243 //  PROBLEME si SPHERE ou TORE
244 //  Il faudra faire un wire complet, a condition que le point porte sur la face
245 //  En attendant, on ne fait rien
246       Handle(StepShape_VertexLoop) VL = Handle(StepShape_VertexLoop)::DownCast(Loop);
247
248       // abv 10.07.00 pr1sy.stp: vertex_loop can be wrong; so just make natural bounds
249       if (GeomSurf->IsKind (STANDARD_TYPE(Geom_SphericalSurface)) ||
250           GeomSurf->IsKind (STANDARD_TYPE(Geom_BSplineSurface)) || 
251           GeomSurf->IsKind (STANDARD_TYPE(Geom_SurfaceOfRevolution)))
252       {
253         if (NbBnd ==1 || FaceBound->IsKind(STANDARD_TYPE(StepShape_FaceOuterBound))) {
254           BRepBuilderAPI_MakeFace mf (GeomSurf, Precision());
255           for (TopoDS_Iterator it(mf); it.More(); it.Next()) 
256             B.Add (F, it.Value());
257           continue;
258         }
259       }
260       
261       if (GeomSurf->IsKind(STANDARD_TYPE(Geom_ToroidalSurface))) {
262         continue;
263       }
264       if (GeomSurf->IsKind(STANDARD_TYPE(Geom_Plane))) {
265         TP->AddWarning(VL, "VertexLoop on plane is ignored");
266         continue; //smh : BUC60809
267       }
268       myTranVL.SetPrecision(Precision());//gka
269       myTranVL.SetMaxTol(MaxTol());
270       myTranVL.Init(VL, aTool, NMTool);
271       if (myTranVL.IsDone()) {
272         B.Add(F, myTranVL.Value());
273       }
274       else {
275         TP->AddWarning(VL, " a VertexLoop not mapped to TopoDS");
276       }
277     }
278     
279     // ----------------------
280     // The Loop is a PolyLoop
281     // ----------------------
282     
283     else if (Loop->IsKind(STANDARD_TYPE(StepShape_PolyLoop))) {
284 //:S4136      STF.Closed() = Standard_False;
285       Handle(StepShape_PolyLoop) PL = Handle(StepShape_PolyLoop)::DownCast(Loop);
286       F.Orientation ( FS->SameSense() ? TopAbs_FORWARD : TopAbs_REVERSED);
287       myTranPL.SetPrecision(Precision()); //gka
288       myTranPL.SetMaxTol(MaxTol());
289       myTranPL.Init(PL, aTool, GeomSurf, F);
290       if (myTranPL.IsDone()) {
291         TopoDS_Wire W = TopoDS::Wire(myTranPL.Value());
292         W.Orientation(FaceBound->Orientation() ? TopAbs_FORWARD : TopAbs_REVERSED);
293         B.Add(F, W);
294       }
295       else {
296         TP->AddWarning(PL, " a PolyLoop not mapped to TopoDS");
297       }
298     }
299     
300     // -----------------------
301     // The Loop is an EdgeLoop
302     // -----------------------
303     
304     else if (Loop->IsKind(STANDARD_TYPE(StepShape_EdgeLoop))) {
305       //:S4136      if (STF.Closed()) {
306       //:S4136  Handle(StepShape_EdgeLoop) EL = 
307       //:S4136    Handle(StepShape_EdgeLoop)::DownCast(FaceBound->Bound());
308       //:S4136  if (EL->NbEdgeList() != 1) STF.Closed() = Standard_False;
309       //:S4136      }
310
311       TopoDS_Wire   W;
312       myTranEdgeLoop.SetPrecision(Precision());  //gka
313       myTranEdgeLoop.SetMaxTol(MaxTol());
314       myTranEdgeLoop.Init(FaceBound, F, GeomSurf, StepSurf, sameSense, aTool, NMTool);
315
316       if (myTranEdgeLoop.IsDone()) {
317         W = TopoDS::Wire(myTranEdgeLoop.Value());
318
319         // STEP Face_Surface orientation :
320         // if the topological orientation is opposite to the geometric
321         // orientation of the surface => the underlying topological 
322         // orientation are not implicitly reversed
323         // this is the case in CAS.CADE => If the face_surface is reversed,
324         // the wire orientation has to be explictly reversed
325         if (FaceBound->Orientation()) {
326           // *DTH*        if (sameSense || GeomSurf->IsKind(STANDARD_TYPE(Geom_Plane)))
327           W.Orientation(sameSense ? TopAbs_FORWARD : TopAbs_REVERSED);
328         }
329         else {
330           // *DTH*        if (sameSense || GeomSurf->IsKind(STANDARD_TYPE(Geom_Plane)))
331           W.Orientation(sameSense ? TopAbs_REVERSED : TopAbs_FORWARD);
332         }
333         // -----------------------------
334         // The Wire is added to the Face      
335         // -----------------------------
336
337         B.Add(F, W);
338       }
339       else {
340         // Il y a eu un probleme dans le mapping : On perd la Face
341         // (facon de parler ...) Pas de moyen aujourd hui de recuperer
342         // au moins toutes les geometries (Points, Courbes 3D, Surface)
343         TP->AddFail(Loop, " EdgeLoop not mapped to TopoDS");
344
345         // CKY JAN-97 : un Wire manque, eh bien on continue quand meme !!
346         //  sauf si OuterBound : la c est quand meme pas bien normal ...
347         if (FaceBound->IsKind(STANDARD_TYPE(StepShape_FaceOuterBound))) {
348           TP->AddWarning(FS, "No Outer Bound : Face not done");
349         }
350         continue;
351       }
352     }    
353     else { 
354       // Type not yet implemented or non sens
355       TP->AddFail(Loop," Type of loop not yet implemented");
356 #ifdef OCCT_DEBUG
357       std::cout << Loop->DynamicType() << std::endl;
358 #endif
359       continue;
360     }
361   }
362
363   F.Orientation ( FS->SameSense() ? TopAbs_FORWARD : TopAbs_REVERSED);
364   aTool.Bind(FS,F);
365
366   // Register face in NM tool (ssv; 14.11.2010)
367   if ( NMTool.IsActive() )
368     NMTool.Bind(StepSurf, F);
369
370   myResult = F;
371   myError  = StepToTopoDS_TranslateFaceDone;
372   done     = Standard_True;
373 }
374
375
376 // ============================================================================
377 // Method  : Value 
378 // Purpose : Return the mapped Shape
379 // ============================================================================
380
381 const TopoDS_Shape& StepToTopoDS_TranslateFace::Value() const 
382 {
383   StdFail_NotDone_Raise_if (!done, "StepToTopoDS_TranslateFace::Value() - no result");
384   return myResult;
385 }
386
387 // ============================================================================
388 // Method  : Error
389 // Purpose : Return the TranslateFace error
390 // ============================================================================
391
392 StepToTopoDS_TranslateFaceError StepToTopoDS_TranslateFace::Error() const
393 {
394   return myError;
395 }