0026937: Eliminate NO_CXX_EXCEPTION macro support
[occt.git] / src / StepToTopoDS / StepToTopoDS_Builder.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 //:i6 abv 17 Sep 98: ProSTEP TR9 r0601-ct.stp: to be able read GeometricSet
18 //gka 11.01.99 file PRO7755.stp #2018: work-around error in BRepLib_MakeFace
19 //:n4 abv 12.02.99: S4132: treatment of GEOMETRIC_SETs implemented
20 //:o7 abv 18.02.99: bm1_sy_fuel.stp #1427(items 1,2) protection against null entity
21 //abv,gka 05.04.99 S4136: parameter names changed; avoid extra call to FixSameParameter
22
23 #include <BRep_Builder.hxx>
24 #include <BRepBuilderAPI_MakeEdge.hxx>
25 #include <BRepBuilderAPI_MakeFace.hxx>
26 #include <BRepBuilderAPI_MakeVertex.hxx>
27 #include <BRepLib.hxx>
28 #include <Geom_BoundedSurface.hxx>
29 #include <Geom_CartesianPoint.hxx>
30 #include <Geom_Curve.hxx>
31 #include <Geom_RectangularTrimmedSurface.hxx>
32 #include <Geom_Surface.hxx>
33 #include <Interface_Static.hxx>
34 #include <Message_Messenger.hxx>
35 #include <Message_ProgressSentry.hxx>
36 #include <Precision.hxx>
37 #include <ShapeFix_ShapeTolerance.hxx>
38 #include <Standard_ErrorHandler.hxx>
39 #include <Standard_Failure.hxx>
40 #include <StdFail_NotDone.hxx>
41 #include <STEPControl_ActorRead.hxx>
42 #include <StepGeom_CartesianPoint.hxx>
43 #include <StepGeom_CompositeCurve.hxx>
44 #include <StepGeom_Curve.hxx>
45 #include <StepGeom_CurveBoundedSurface.hxx>
46 #include <StepGeom_GeometricRepresentationItem.hxx>
47 #include <StepGeom_RectangularCompositeSurface.hxx>
48 #include <StepGeom_Surface.hxx>
49 #include <StepGeom_SurfacePatch.hxx>
50 #include <StepShape_BrepWithVoids.hxx>
51 #include <StepShape_ClosedShell.hxx>
52 #include <StepShape_ConnectedEdgeSet.hxx>
53 #include <StepShape_EdgeBasedWireframeModel.hxx>
54 #include <StepShape_FaceBasedSurfaceModel.hxx>
55 #include <StepShape_FaceSurface.hxx>
56 #include <StepShape_FacetedBrep.hxx>
57 #include <StepShape_FacetedBrepAndBrepWithVoids.hxx>
58 #include <StepShape_GeometricSet.hxx>
59 #include <StepShape_GeometricSetSelect.hxx>
60 #include <StepShape_HArray1OfConnectedEdgeSet.hxx>
61 #include <StepShape_HArray1OfConnectedFaceSet.hxx>
62 #include <StepShape_HArray1OfEdge.hxx>
63 #include <StepShape_HArray1OfFace.hxx>
64 #include <StepShape_ManifoldSolidBrep.hxx>
65 #include <StepShape_OpenShell.hxx>
66 #include <StepShape_OrientedClosedShell.hxx>
67 #include <StepShape_Shell.hxx>
68 #include <StepShape_ShellBasedSurfaceModel.hxx>
69 #include <StepToGeom.hxx>
70 #include <StepToTopoDS_Builder.hxx>
71 #include <StepToTopoDS_DataMapOfTRI.hxx>
72 #include <StepToTopoDS_NMTool.hxx>
73 #include <StepToTopoDS_Tool.hxx>
74 #include <StepToTopoDS_TranslateCompositeCurve.hxx>
75 #include <StepToTopoDS_TranslateCurveBoundedSurface.hxx>
76 #include <StepToTopoDS_TranslateEdge.hxx>
77 #include <StepToTopoDS_TranslateFace.hxx>
78 #include <StepToTopoDS_TranslateShell.hxx>
79 #include <TopExp_Explorer.hxx>
80 #include <TopoDS.hxx>
81 #include <TopoDS_Compound.hxx>
82 #include <TopoDS_Edge.hxx>
83 #include <TopoDS_Face.hxx>
84 #include <TopoDS_Shape.hxx>
85 #include <TopoDS_Shell.hxx>
86 #include <TopoDS_Solid.hxx>
87 #include <Transfer_ActorOfTransientProcess.hxx>
88 #include <Transfer_TransientProcess.hxx>
89 #include <TransferBRep.hxx>
90
91 #include <stdio.h>
92 //#include <StepShape_VertexShell.hxx>
93 //:e0
94 static void ResetPreci (const TopoDS_Shape& S, Standard_Real maxtol)
95 {
96   //:S4136
97   Standard_Integer modetol = Interface_Static::IVal("read.maxprecision.mode");
98   if (modetol) {
99     ShapeFix_ShapeTolerance STU;
100     STU.LimitTolerance (S,Precision::Confusion(),maxtol);
101   }
102 //  Standard_Real ratio = Interface_Static::RVal("XSTEP.readprecision.ratio");
103 //  if (ratio >= 1) {
104 //    Standard_Real lastpre = Interface_Static::RVal("lastpreci");
105 //    ShapeFix_ShapeTolerance STU;
106 //    STU.LimitTolerance (S,lastpre/ratio,lastpre*ratio);
107 //  }
108 }
109
110 // ============================================================================
111 // Method  : StepToTopoDS_Builder::StepToTopoDS_Builder
112 // Purpose : Empty constructor
113 // ============================================================================
114
115 StepToTopoDS_Builder::StepToTopoDS_Builder()
116 {
117   done = Standard_False;
118 }
119
120 // ============================================================================
121 // Method  : StepToTopoDS_Builder::StepToTopoDS_Builder
122 // Purpose : Constructor with a ManifoldSolidBrep
123 // ============================================================================
124
125 StepToTopoDS_Builder::StepToTopoDS_Builder
126 (const Handle(StepShape_ManifoldSolidBrep)& aManifoldSolid, 
127  const Handle(Transfer_TransientProcess)& TP)
128 {
129   Init(aManifoldSolid, TP);
130 }
131
132 // ============================================================================
133 // Method  : StepToTopoDS_Builder::StepToTopoDS_Builder
134 // Purpose : Constructor woth a BrepWithVoids
135 // ============================================================================
136
137 StepToTopoDS_Builder::StepToTopoDS_Builder
138 (const Handle(StepShape_BrepWithVoids)& aBRepWithVoids,
139  const Handle(Transfer_TransientProcess)& TP)
140 {
141   Init(aBRepWithVoids, TP);
142 }
143
144 // ============================================================================
145 // Method  : StepToTopoDS_Builder::StepToTopoDS_Builder
146 // Purpose : Constructor with a FacetedBrep
147 // ============================================================================
148
149 StepToTopoDS_Builder::StepToTopoDS_Builder
150 (const Handle(StepShape_FacetedBrep)& aFB,
151  const Handle(Transfer_TransientProcess)& TP)
152 {
153   Init(aFB, TP);
154 }
155
156 // ============================================================================
157 // Method  : StepToTopoDS_Builder::StepToTopoDS_Builder
158 // Purpose : Constructor with a FacetedBrepAndBrepWithVoids
159 // ============================================================================
160
161 StepToTopoDS_Builder::StepToTopoDS_Builder
162 (const Handle(StepShape_FacetedBrepAndBrepWithVoids)& aFBABWV,
163  const Handle(Transfer_TransientProcess)& TP)
164 {
165   Init(aFBABWV, TP);
166 }
167
168 // ============================================================================
169 // Method  : StepToTopoDS_Builder::StepToTopoDS_Builder
170 // Purpose : Constructor with a ShellBasedSurfaceModel
171 // ============================================================================
172
173 StepToTopoDS_Builder::StepToTopoDS_Builder
174 (const Handle(StepShape_ShellBasedSurfaceModel)& aSBSM,
175  const Handle(Transfer_TransientProcess)& TP,
176  StepToTopoDS_NMTool& NMTool)
177 {
178   Init(aSBSM, TP, NMTool);
179 }
180
181 // ============================================================================
182 // Method  : Init
183 // Purpose : Init with a ManifoldSolidBrep
184 // ============================================================================
185
186 void StepToTopoDS_Builder::Init
187 (const Handle(StepShape_ManifoldSolidBrep)& aManifoldSolid,
188  const Handle(Transfer_TransientProcess)& TP)
189 {
190   Handle(Message_Messenger) sout = TP->Messenger();
191   // Initialisation of the Tool
192
193   StepToTopoDS_Tool         myTool;
194   StepToTopoDS_DataMapOfTRI aMap;
195
196   myTool.Init(aMap, TP);
197
198   // Start Mapping
199
200   Handle(StepShape_ConnectedFaceSet) aShell;
201   aShell = aManifoldSolid->Outer();
202
203   StepToTopoDS_TranslateShell myTranShell;
204   myTranShell.SetPrecision(Precision());
205   myTranShell.SetMaxTol(MaxTol());
206   // Non-manifold topology is not referenced by ManifoldSolidBrep (ssv; 14.11.2010)
207   StepToTopoDS_NMTool dummyNMTool;
208   myTranShell.Init(aShell, myTool, dummyNMTool);
209
210   if (myTranShell.IsDone()) {
211     TopoDS_Shape Sh = myTranShell.Value();
212     Sh.Closed(Standard_True);
213     //BRepLib::SameParameter(Sh);
214     TopoDS_Solid S;
215     BRep_Builder B;
216     B.MakeSolid(S);
217     B.Add(S,Sh);
218     myResult = S;
219     myError  = StepToTopoDS_BuilderDone;
220     done     = Standard_True;
221
222     // Get Statistics :
223
224     if ( TP->TraceLevel() > 2 )
225     {
226       sout << "Geometric Statitics : " << endl;
227       sout << "   Surface Continuity : - C0 : " << myTool.C0Surf() << endl;
228       sout << "                        - C1 : " << myTool.C1Surf() << endl;
229       sout << "                        - C2 : " << myTool.C2Surf() << endl;
230       sout << "   Curve Continuity :   - C0 : " << myTool.C0Cur3() << endl;
231       sout << "                        - C1 : " << myTool.C1Cur3() << endl;
232       sout << "                        - C2 : " << myTool.C2Cur3() << endl;
233       sout << "   PCurve Continuity :  - C0 : " << myTool.C0Cur2() << endl;
234       sout << "                        - C1 : " << myTool.C1Cur2() << endl;
235       sout << "                        - C2 : " << myTool.C2Cur2() << endl;
236     }
237
238 //:S4136    ShapeFix::SameParameter (S,Standard_False);
239     ResetPreci (S, MaxTol());
240   }
241   else {
242     TP->AddWarning(aShell," OuterShell from ManifoldSolidBrep not mapped to TopoDS");
243     myError  = StepToTopoDS_BuilderOther;
244     done     = Standard_False;
245   }
246 }
247
248 // ============================================================================
249 // Method  : Init
250 // Purpose : Init with a BrepWithVoids
251 // ============================================================================
252
253 void StepToTopoDS_Builder::Init
254 (const Handle(StepShape_BrepWithVoids)& aBRepWithVoids,
255  const Handle(Transfer_TransientProcess)& TP)
256 {
257   Handle(Message_Messenger) sout = TP->Messenger();
258  // Initialisation of the Tool
259   StepToTopoDS_Tool         myTool;
260   StepToTopoDS_DataMapOfTRI aMap;
261
262   myTool.Init(aMap, TP);
263
264   // Start Mapping
265
266   Standard_Integer Nb = aBRepWithVoids->NbVoids();
267   Handle(StepShape_ClosedShell) aCShell;
268   TopoDS_Solid S;
269   TopoDS_Shape Sh;
270   BRep_Builder B;
271   B.MakeSolid(S);
272   
273   Message_ProgressSentry PS ( TP->GetProgress(), "Shell", 0, Nb+1, 1 );
274
275   StepToTopoDS_TranslateShell myTranShell;
276
277   myTranShell.SetPrecision(Precision());//gka
278   myTranShell.SetMaxTol(MaxTol());
279   // OuterBound
280
281   aCShell = Handle(StepShape_ClosedShell)::DownCast(aBRepWithVoids->Outer());
282   // Non-manifold topology is not referenced by BrepWithVoids (ssv; 14.11.2010)
283   StepToTopoDS_NMTool dummyNMTool;
284   myTranShell.Init(aCShell, myTool, dummyNMTool);
285   
286   PS.Next();
287
288   if (myTranShell.IsDone()) {
289     Sh = myTranShell.Value();
290     Sh.Closed(Standard_True);
291     //BRepLib::SameParameter(Sh);
292     B.MakeSolid(S);
293     B.Add(S,Sh);
294     myResult = S;
295     myError  = StepToTopoDS_BuilderDone;
296     done     = Standard_True;
297   }
298   else {
299     TP->AddWarning(aCShell," OuterShell from BrepWithVoids not mapped to TopoDS");
300     myError  = StepToTopoDS_BuilderOther;
301     done     = Standard_False;
302     return;
303   }
304
305   // Voids
306
307   for (Standard_Integer i=1; i<=Nb && PS.More(); i++, PS.Next()) {
308
309     aCShell = aBRepWithVoids->VoidsValue(i);
310     myTranShell.Init(aCShell, myTool, dummyNMTool);
311     if (myTranShell.IsDone()) {
312       Sh = myTranShell.Value();
313       Sh.Closed(Standard_True);
314
315       //:e0 abv 25 Mar 98: void should be an OrientedShell 
316       Handle(StepShape_OrientedClosedShell) aOCShell = aBRepWithVoids->VoidsValue(i);
317       if ( ! aOCShell.IsNull() && ! aOCShell->Orientation() ) Sh.Reverse();
318       
319       //BRepLib::SameParameter(Sh);
320       B.Add(S,Sh);
321     }
322     else {
323       TP->AddWarning
324         (aCShell," A Void from BrepWithVoids not mapped to TopoDS");
325     }
326   }
327   myResult = S;
328
329
330   // Get Statistics :
331   
332   if ( TP->TraceLevel() > 2 )
333   {
334     sout << "Geometric Statitics : " << endl;
335     sout << "   Surface Continuity : - C0 : " << myTool.C0Surf() << endl;
336     sout << "                        - C1 : " << myTool.C1Surf() << endl;
337     sout << "                        - C2 : " << myTool.C2Surf() << endl;
338     sout << "   Curve Continuity :   - C0 : " << myTool.C0Cur3() << endl;
339     sout << "                        - C1 : " << myTool.C1Cur3() << endl;
340     sout << "                        - C2 : " << myTool.C2Cur3() << endl;
341     sout << "   PCurve Continuity :  - C0 : " << myTool.C0Cur2() << endl;
342     sout << "                        - C1 : " << myTool.C1Cur2() << endl;
343     sout << "                        - C2 : " << myTool.C2Cur2() << endl;
344   }
345
346 //:S4136  ShapeFix::SameParameter (S,Standard_False);
347   ResetPreci (S, MaxTol());
348 }
349
350 // ============================================================================
351 // Method  : Init
352 // Purpose : Init with a FacetedBrep
353 // ============================================================================
354
355 void StepToTopoDS_Builder::Init(const Handle(StepShape_FacetedBrep)& aFB,
356                                const Handle(Transfer_TransientProcess)& TP)
357 {
358   // Initialisation of the Tool
359
360   StepToTopoDS_Tool         myTool;
361   StepToTopoDS_DataMapOfTRI aMap;
362
363   myTool.Init(aMap, TP);
364
365   // Start Mapping
366
367   Handle(StepShape_ClosedShell) aCShell;
368   aCShell = Handle(StepShape_ClosedShell)::DownCast(aFB->Outer());
369   TopoDS_Shape Sh;
370
371   StepToTopoDS_TranslateShell myTranShell; 
372   myTranShell.SetPrecision(Precision()); //gka
373   myTranShell.SetMaxTol(MaxTol());  
374   // Non-manifold topology is not referenced by FacetedBrep (ss; 14.11.2010)
375   StepToTopoDS_NMTool dummyNMTool;
376   myTranShell.Init(aCShell, myTool, dummyNMTool);
377
378   if (myTranShell.IsDone()) {
379     Sh = myTranShell.Value();
380     Sh.Closed(Standard_True);
381     //BRepLib::SameParameter(Sh);
382     myTool.ClearEdgeMap();
383     myTool.ClearVertexMap();
384     TopoDS_Solid S;
385     BRep_Builder B;
386     B.MakeSolid(S);
387     B.Add(S,Sh);
388     myResult = S;
389     myError  = StepToTopoDS_BuilderDone;
390     done     = Standard_True;
391   }
392   else {
393     TP->AddWarning
394       (aCShell," OuterShell from FacetedBrep not mapped to TopoDS");
395     myError  = StepToTopoDS_BuilderOther;
396     done     = Standard_True;
397   }
398 }
399
400 // ============================================================================
401 // Method  : Init
402 // Purpose : Init with a FacetedBrepAndBrepWithVoids
403 // ============================================================================
404
405 void StepToTopoDS_Builder::Init
406 (const Handle(StepShape_FacetedBrepAndBrepWithVoids)& aFBABWV,
407  const Handle(Transfer_TransientProcess)& TP)
408 {
409   // Initialisation of the Tool
410
411   StepToTopoDS_Tool         myTool;
412   StepToTopoDS_DataMapOfTRI aMap;
413
414   myTool.Init(aMap, TP);
415
416   // Start Mapping
417
418   Handle(StepShape_ClosedShell) aCShell;
419   aCShell = Handle(StepShape_ClosedShell)::DownCast(aFBABWV->Outer());
420   TopoDS_Shape Sh;
421
422   StepToTopoDS_TranslateShell myTranShell;
423   myTranShell.SetPrecision(Precision()); //gka
424   myTranShell.SetMaxTol(MaxTol());
425   // Non-manifold topology is not referenced by FacetedBrepAndBrepWithVoids (ss; 14.11.2010)
426   StepToTopoDS_NMTool dummyNMTool;
427   myTranShell.Init(aCShell, myTool, dummyNMTool);
428
429   if (myTranShell.IsDone()) {
430     Sh = myTranShell.Value();
431     Sh.Closed(Standard_True);
432     //BRepLib::SameParameter(Sh);
433     TopoDS_Solid S;
434     BRep_Builder B;
435     B.MakeSolid(S);
436     B.Add(S,Sh);
437     Standard_Integer Nb, i;
438     Nb = aFBABWV->NbVoids();
439     for ( i=1; i<=Nb; i++ ) {
440       aCShell = aFBABWV->VoidsValue(i);
441       myTranShell.Init(aCShell, myTool, dummyNMTool);
442       if (myTranShell.IsDone()) {
443         Sh = myTranShell.Value();
444         Sh.Closed(Standard_True);
445         //BRepLib::SameParameter(Sh);
446         B.Add(S,Sh);
447       }
448       else {
449         TP->AddWarning
450           (aCShell," A Void from FacetedBrepAndBrepWithVoids not mapped to TopoDS");
451       }
452     }
453     myResult = S;
454     myError  = StepToTopoDS_BuilderDone;
455     done     = Standard_True;
456   }
457   else {
458     TP->AddWarning
459       (aCShell," OuterShell from FacetedBrepAndBrepWithVoids not mapped to TopoDS");
460     done    = Standard_False;
461     myError = StepToTopoDS_BuilderOther;
462   }
463 }
464
465 // ============================================================================
466 // Method  : Init
467 // Purpose : Init with a ShellBasedSurfaceModel
468 // ============================================================================
469
470 void StepToTopoDS_Builder::Init
471 (const Handle(StepShape_ShellBasedSurfaceModel)& aSBSM,
472  const Handle(Transfer_TransientProcess)& TP,
473  StepToTopoDS_NMTool& NMTool)
474 {
475   Handle(Message_Messenger) sout = TP->Messenger();
476   // Initialisation of the Tool
477
478   StepToTopoDS_Tool         myTool;
479   StepToTopoDS_DataMapOfTRI aMap;
480
481   myTool.Init(aMap, TP);
482
483   // Start Mapping
484
485   Standard_Integer Nb = aSBSM->NbSbsmBoundary();
486   StepShape_Shell aShell;
487   //Handle(StepShape_VertexShell) aVertexShell;
488   Handle(StepShape_OpenShell)   aOpenShell;
489   Handle(StepShape_ClosedShell) aClosedShell;
490   TopoDS_Compound S;
491   //TopoDS_Shape Sh;
492   TopoDS_Shape Shl;
493   BRep_Builder B;
494   B.MakeCompound(S);
495   StepToTopoDS_TranslateShell myTranShell;
496   
497   myTranShell.SetPrecision(Precision());
498   myTranShell.SetMaxTol(MaxTol());
499
500   Message_ProgressSentry PS ( TP->GetProgress(), "Shell", 0, Nb, 1 );
501   for (Standard_Integer i=1; i<=Nb && PS.More(); i++, PS.Next()) {
502   //for (Standard_Integer i=1; i<=Nb; i++) {
503     aShell = aSBSM->SbsmBoundaryValue(i);
504     //aVertexShell = aShell.VertexShell();
505     aOpenShell   = aShell.OpenShell();
506     aClosedShell = aShell.ClosedShell();
507     //if (!aVertexShell.IsNull()) {
508     //TP->AddWarning
509     //(aVertexShell,
510     //" VertexShell from ShellBasedSurfaceModel not mapped to TopoDS");
511     //}
512     //else 
513     if (!aOpenShell.IsNull()) {
514       myTranShell.Init(aOpenShell, myTool, NMTool);
515       if (myTranShell.IsDone()) {
516         //Sh = myTranShell.Value();
517         //Sh.Closed(Standard_False);
518         //BRepLib::SameParameter(Sh);
519         //B.Add(S,Sh);
520         Shl = TopoDS::Shell(myTranShell.Value());
521         Shl.Closed(Standard_False);
522         B.Add(S,Shl);
523       }
524       else { 
525         TP->AddWarning
526           (aOpenShell," OpenShell from ShellBasedSurfaceModel not mapped to TopoDS");
527       }
528     }
529     else if (!aClosedShell.IsNull()) {
530       myTranShell.Init(aClosedShell, myTool, NMTool);
531       if (myTranShell.IsDone()) {
532         //Sh = myTranShell.Value();
533         //Sh.Closed(Standard_True);
534         //BRepLib::SameParameter(Sh);
535         //B.Add(S,Sh);
536         Shl = TopoDS::Shell(myTranShell.Value());
537         Shl.Closed(Standard_True);
538         B.Add(S,Shl);
539       }
540       else { 
541         TP->AddWarning
542           (aClosedShell," ClosedShell from ShellBasedSurfaceModel not mapped to TopoDS");
543       }
544     }
545   }
546   if(Nb>1)
547     myResult = S;
548   else
549     myResult = Shl;
550   myError  = StepToTopoDS_BuilderDone;
551   done     = Standard_True;
552
553   // Get Statistics :
554   
555   if ( TP->TraceLevel() > 2 )
556   {
557     sout << "Geometric Statitics : " << endl;
558     sout << "   Surface Continuity : - C0 : " << myTool.C0Surf() << endl;
559     sout << "                        - C1 : " << myTool.C1Surf() << endl;
560     sout << "                        - C2 : " << myTool.C2Surf() << endl;
561     sout << "   Curve Continuity :   - C0 : " << myTool.C0Cur3() << endl;
562     sout << "                        - C1 : " << myTool.C1Cur3() << endl;
563     sout << "                        - C2 : " << myTool.C2Cur3() << endl;
564     sout << "   PCurve Continuity :  - C0 : " << myTool.C0Cur2() << endl;
565     sout << "                        - C1 : " << myTool.C1Cur2() << endl;
566     sout << "                        - C2 : " << myTool.C2Cur2() << endl;
567   }
568
569 //:S4136  ShapeFix::SameParameter (S,Standard_False);
570   ResetPreci (S, MaxTol());
571   ResetPreci (Shl, MaxTol()); //skl
572 }
573
574 // ============================================================================
575 // Method  : Init
576 // Purpose : Init with a EdgeBasedWireframeModel
577 // ============================================================================
578
579 void StepToTopoDS_Builder::Init (const Handle(StepShape_EdgeBasedWireframeModel)& aEBWM,
580                                  const Handle(Transfer_TransientProcess)& TP)
581 {
582   myResult.Nullify();
583   
584   Handle(StepShape_HArray1OfConnectedEdgeSet) boundary = aEBWM->EbwmBoundary();
585   if ( boundary.IsNull() || boundary->Length() <1 ) {
586     TP->AddWarning ( aEBWM, "List of boundaries is empty" );
587     return;
588   }
589
590   StepToTopoDS_Tool myTool;
591   StepToTopoDS_DataMapOfTRI aMap;
592   myTool.Init(aMap, TP);
593
594   StepToTopoDS_TranslateEdge myTranEdge;
595   myTranEdge.SetPrecision(Precision());
596   myTranEdge.SetMaxTol(MaxTol());
597   
598   TopoDS_Compound C;
599   BRep_Builder B;
600   B.MakeCompound ( C );
601
602   StepToTopoDS_NMTool dummyNMTool;
603   
604   for ( Standard_Integer i=1; i <= boundary->Length(); i++ ) {
605     Handle(StepShape_ConnectedEdgeSet) ces = boundary->Value(i);
606     if ( ces.IsNull() ) continue;
607     Handle(StepShape_HArray1OfEdge) edges = ces->CesEdges();
608     if ( edges.IsNull() || edges->Length() <1 ) {
609       TP->AddWarning ( ces, "No edges in connected_edge_set" );
610       continue;
611     }
612     TopoDS_Wire W;
613     for ( Standard_Integer j=1; j <= edges->Length(); j++ ) {
614       myTranEdge.Init (edges->Value(j), myTool, dummyNMTool);
615       if ( ! myTranEdge.IsDone() ) continue;
616       TopoDS_Edge E = TopoDS::Edge(myTranEdge.Value());
617       if (E.IsNull()) continue;  // NULL, on saute
618       if ( W.IsNull() ) B.MakeWire ( W );
619       B.Add ( W, E );
620     }
621     if ( W.IsNull() ) continue;
622     W.Closed (BRep_Tool::IsClosed (W));
623     B.Add ( C, W );
624     if ( myResult.IsNull() ) myResult = W;
625     else myResult = C;
626   }
627   
628   myError  = ( myResult.IsNull() ? StepToTopoDS_BuilderDone : StepToTopoDS_BuilderOther );
629   done     = ! myResult.IsNull();
630
631   ResetPreci (myResult, MaxTol());
632 }
633
634 // ============================================================================
635 // Method  : Init
636 // Purpose : Init with a FaceBasedSurfaceModel
637 // ============================================================================
638
639 void StepToTopoDS_Builder::Init (const Handle(StepShape_FaceBasedSurfaceModel)& aFBSM,
640                                  const Handle(Transfer_TransientProcess)& TP)
641 {
642   myResult.Nullify();
643   
644   Handle(StepShape_HArray1OfConnectedFaceSet) boundary = aFBSM->FbsmFaces();
645   if ( boundary.IsNull() || boundary->Length() <1 ) {
646     TP->AddWarning ( aFBSM, "List of faces is empty" );
647     return;
648   }
649
650   StepToTopoDS_Tool myTool;
651   StepToTopoDS_DataMapOfTRI aMap;
652   myTool.Init(aMap, TP);
653
654   StepToTopoDS_TranslateFace myTranFace;
655   myTranFace.SetPrecision(Precision());
656   myTranFace.SetMaxTol(MaxTol());
657   
658   TopoDS_Compound C;
659   BRep_Builder B;
660   B.MakeCompound ( C );
661
662   // Non-manifold topology is not mapped via FaceBasedSurfaceModel (ssv; 14.11.2010)
663   StepToTopoDS_NMTool dummyNMTool;
664   
665   for ( Standard_Integer i=1; i <= boundary->Length(); i++ ) {
666     Handle(StepShape_ConnectedFaceSet) cfs = boundary->Value(i);
667     if ( cfs.IsNull() ) continue;
668     Handle(StepShape_HArray1OfFace) faces = cfs->CfsFaces();
669     if ( faces.IsNull() || faces->Length() <1 ) {
670       TP->AddWarning ( cfs, "No faces in connected_face_set" );
671       continue;
672     }
673     TopoDS_Shell S;
674     for ( Standard_Integer j=1; j <= faces->Length(); j++ ) {
675       Handle(StepShape_FaceSurface) fs = Handle(StepShape_FaceSurface)::DownCast ( faces->Value(j) );
676       myTranFace.Init(fs, myTool, dummyNMTool);
677       if ( ! myTranFace.IsDone() ) continue;
678       TopoDS_Face F = TopoDS::Face(myTranFace.Value());
679       if (F.IsNull()) continue;  // NULL, on saute
680       if ( S.IsNull() ) B.MakeShell ( S );
681       B.Add ( S, F );
682     }
683     if ( S.IsNull() ) continue;
684     S.Closed (BRep_Tool::IsClosed (S));
685     B.Add ( C, S );
686     if ( myResult.IsNull() ) myResult = S;
687     else myResult = C;
688   }
689   
690   myError  = ( myResult.IsNull() ? StepToTopoDS_BuilderDone : StepToTopoDS_BuilderOther );
691   done     = ! myResult.IsNull();
692
693   ResetPreci (myResult, MaxTol());
694 }
695
696
697 // ***start DTH Apr/6
698 // ============================================================================
699 // Method  : Init
700 // Purpose : Init with a GeometricCurveSet
701 // ============================================================================
702 //:i6 abv 17 Sep 98: ProSTEP TR9 r0601-ct.stp: to be able read GS: GeometricCurveSet -> GeometricSet
703
704 static TopoDS_Face TranslateBoundedSurf (const Handle(StepGeom_Surface) &surf,
705                                          const Standard_Real TolDegen)
706 {
707   TopoDS_Face res;
708
709   Handle(Geom_Surface) theSurf = StepToGeom::MakeSurface (surf);
710   if (theSurf.IsNull() || //:i6: protection
711       !theSurf->IsKind(STANDARD_TYPE(Geom_BoundedSurface))) return res;
712
713   BRepBuilderAPI_MakeFace myMkFace;
714   Handle(Geom_RectangularTrimmedSurface) RS = 
715                     Handle(Geom_RectangularTrimmedSurface)::DownCast(theSurf);
716
717   if (!RS.IsNull())
718   {
719     Standard_Real umin, umax, vmin, vmax;
720     theSurf->Bounds(umin, umax, vmin, vmax);
721
722     myMkFace.Init(RS->BasisSurface(), umin, umax, vmin, vmax, TolDegen);
723   }
724   else
725   {
726     myMkFace.Init(theSurf, Standard_True, TolDegen);
727   }
728
729   return myMkFace.Face();
730 }
731
732 void StepToTopoDS_Builder::Init
733 (const Handle(StepShape_GeometricSet)& GCS,
734  const Handle(Transfer_TransientProcess)& TP,
735  const Handle(Transfer_ActorOfTransientProcess)& RA,
736  const Standard_Boolean isManifold)
737 {
738   // Initialisation of the Tool
739
740 //  StepToTopoDS_Tool         myTool;
741 //  StepToTopoDS_DataMapOfTRI aMap;
742
743 //  myTool.Init(aMap, TP);
744
745   // Start Mapping
746   TopoDS_Compound S;
747   BRep_Builder B;
748   B.MakeCompound(S);
749   TopoDS_Edge theEdge;
750   Standard_Integer i;
751   Standard_Real preci = Precision();   //gka
752   Standard_Real maxtol = MaxTol();
753   Standard_Integer nbElem = GCS->NbElements();
754   for (i = 1; i <= nbElem ; i++) {
755     StepShape_GeometricSetSelect aGSS = GCS->ElementsValue(i);
756     Handle(Standard_Transient) ent = aGSS.Value();
757
758     TopoDS_Shape res = TransferBRep::ShapeResult ( TP, ent );
759     if ( ! res.IsNull() ) { // already translated
760       B.Add ( S, res );
761       continue;
762     }
763     //:o7 abv 18 Feb 99: bm1_sy_fuel.stp #1427(1,2) protection against null entity
764     if ( ent.IsNull() ) {
765       char buff[100];
766       sprintf ( buff, "Entity %d is a Null entity", i );
767       TP->AddWarning (GCS,buff);
768       continue;
769     } 
770     // try curve
771     else if ( ent->IsKind(STANDARD_TYPE(StepGeom_Curve)) ) {
772       Handle(StepGeom_Curve) aCrv = Handle(StepGeom_Curve)::DownCast ( ent );
773
774       // try composite_curve
775       Handle(StepGeom_CompositeCurve) CC = Handle(StepGeom_CompositeCurve)::DownCast(aCrv);
776       if ( ! CC.IsNull() ) {
777         StepToTopoDS_TranslateCompositeCurve TrCC;
778         TrCC.SetPrecision(preci);
779         TrCC.SetMaxTol(maxtol);
780         TrCC.Init( CC, TP );
781         if ( TrCC.IsDone() ) 
782         {
783           if (TrCC.IsInfiniteSegment())
784           {
785             BRep_Builder aB;
786             TopoDS_Compound aComp;
787             aB.MakeCompound(aComp);
788             TopExp_Explorer anExp;
789             for (anExp.Init (TrCC.Value(), TopAbs_EDGE); anExp.More(); anExp.Next())
790               aB.Add (aComp, anExp.Current());
791             res = aComp;
792           }
793           else
794             res = TrCC.Value();
795         }
796       }
797       else { // try other curves
798         Handle(Geom_Curve) aGeomCrv;
799         try {
800           OCC_CATCH_SIGNALS
801           aGeomCrv = StepToGeom::MakeCurve (aCrv);
802         }
803         catch(Standard_Failure const& anException) {
804           Handle(Message_Messenger) sout = TP->Messenger();
805           sout<<"StepToTopoDS, GeometricSet, elem "<<i<<" of "<<nbElem<<": exception ";
806           sout<<anException.GetMessageString() << endl;
807         }
808         if ( ! aGeomCrv.IsNull() ) {
809           BRepBuilderAPI_MakeEdge anEdge(aGeomCrv, aGeomCrv->FirstParameter(), aGeomCrv->LastParameter());
810           if ( anEdge.IsDone() ) res = anEdge.Edge();
811         }
812       }
813     }
814     // try point
815     else if ( ent->IsKind(STANDARD_TYPE(StepGeom_CartesianPoint)) ) {
816       Handle(StepGeom_CartesianPoint) aPnt = Handle(StepGeom_CartesianPoint)::DownCast ( ent );
817       Handle(Geom_CartesianPoint) thePnt = StepToGeom::MakeCartesianPoint (aPnt);
818       if (! thePnt.IsNull()) {
819         BRepBuilderAPI_MakeVertex myMkVtx(thePnt->Pnt());
820         if ( myMkVtx.IsDone() ) res = myMkVtx.Vertex();
821       }
822     }
823     // Element should finally be a Surface
824     else if ( ent->IsKind(STANDARD_TYPE(StepGeom_Surface)) ) {
825       Handle(StepGeom_Surface) aSurf = 
826         Handle(StepGeom_Surface)::DownCast(ent);
827         
828       // try curve_bounded_surf
829       if ( ent->IsKind(STANDARD_TYPE(StepGeom_CurveBoundedSurface)) ) {
830         Handle(StepGeom_CurveBoundedSurface) CBS = 
831           Handle(StepGeom_CurveBoundedSurface)::DownCast(aSurf);
832         StepToTopoDS_TranslateCurveBoundedSurface TrCBS;
833         TrCBS.SetPrecision(preci);
834         TrCBS.SetMaxTol(maxtol);
835         
836         TrCBS.Init( CBS, TP );
837         if ( TrCBS.IsDone() ) res = TrCBS.Value();
838       }
839       // try RectangularCompositeSurface
840       else if ( ent->IsKind(STANDARD_TYPE(StepGeom_RectangularCompositeSurface)) ) {
841         Handle(StepGeom_RectangularCompositeSurface) RCS = 
842           Handle(StepGeom_RectangularCompositeSurface)::DownCast(aSurf);
843         Standard_Integer nbi = RCS->NbSegmentsI();
844         Standard_Integer nbj = RCS->NbSegmentsJ();
845         TopoDS_Compound C;
846         B.MakeCompound ( C );
847         for ( Standard_Integer ii=1; ii <= nbi; ii++ ) 
848           for ( Standard_Integer j=1; j <= nbj; j++ ) {
849             Handle(StepGeom_SurfacePatch) patch = RCS->SegmentsValue ( ii, j );
850             TopoDS_Face f = TranslateBoundedSurf (patch->ParentSurface(), preci);
851             if ( ! f.IsNull() ) B.Add ( C, f );
852           }
853         res = C;
854       }
855       // try other surfs
856       else res = TranslateBoundedSurf (aSurf, preci);
857     }
858     else if ( ent->IsKind(STANDARD_TYPE(StepGeom_GeometricRepresentationItem)) )
859     {
860       Handle(StepGeom_GeometricRepresentationItem) GRI = 
861         Handle(StepGeom_GeometricRepresentationItem)::DownCast(ent);
862       if (!RA.IsNull())
863       {
864         Handle(STEPControl_ActorRead) anActor = Handle(STEPControl_ActorRead)::DownCast(RA);
865         Handle(Transfer_Binder) binder;
866         if( !anActor.IsNull())
867           binder = anActor->TransferShape(GRI, TP, isManifold);
868         if (!binder.IsNull())
869         {
870           res = TransferBRep::ShapeResult(binder);
871         }
872       }
873     }
874     else TP->AddWarning (ent," Entity is not a Curve, Point, Surface or GeometricRepresentationItem");
875     if ( ! res.IsNull() ) {
876       B.Add(S, res);
877       TransferBRep::SetShapeResult ( TP, ent, res );
878     }
879     else TP->AddWarning (ent," Entity not mapped to TopoDS");
880   }
881   myResult = S;
882   myError  = StepToTopoDS_BuilderDone;
883   done     = Standard_True;
884 }
885 // ***end DTH Apr/6 
886
887
888 // ============================================================================
889 // Method  : Value
890 // Purpose : Returns the result of the mapping
891 // ============================================================================
892
893 const TopoDS_Shape& StepToTopoDS_Builder::Value() const 
894 {
895   StdFail_NotDone_Raise_if(!done,"");
896   return myResult;
897 }
898
899 // ============================================================================
900 // Method  : Error
901 // Purpose : Returns the Builder Error code
902 // ============================================================================
903
904 StepToTopoDS_BuilderError StepToTopoDS_Builder::Error() const 
905 {
906   return myError;
907 }
908