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