Salome HOME
ENV: Windows porting.
[modules/geom.git] / src / STEPImport / STEPImport.cxx
1 // File:        STEPImport.cxx
2 // Created:     Wed May 19 14:41:10 2004
3 // Author:      Pavel TELKOV
4 //              <ptv@mutex.nnov.opencascade.com>
5
6 #include "utilities.h"
7
8 #include <BRep_Builder.hxx>
9
10 #include <IFSelect_ReturnStatus.hxx>
11
12 #include <STEPControl_Reader.hxx>
13
14 #include <TCollection_AsciiString.hxx>
15 #include <TopoDS_Compound.hxx>
16 #include <TopoDS_Shape.hxx>
17
18 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
19
20 #ifdef WNT
21 #include <SALOME_WNT.hxx>
22 #else
23 #define SALOME_WNT_EXPORT
24 #endif
25
26 //=============================================================================
27 /*!
28  *  Import()
29  */
30 //=============================================================================
31
32 extern "C"
33 {
34 SALOME_WNT_EXPORT
35   TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
36                        TCollection_AsciiString&       theError)
37   {
38     MESSAGE("Import STEP model from file " << theFileName.ToCString());
39     TopoDS_Shape aResShape;
40     //VRV: OCC 4.0 migration
41     STEPControl_Reader aReader;
42     //VRV: OCC 4.0 migration
43     TopoDS_Compound compound;
44     BRep_Builder B;
45     B.MakeCompound( compound );
46     try {
47       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
48
49       if (status == IFSelect_RetDone) {
50         Standard_Boolean failsonly = Standard_False ;
51         aReader.PrintCheckLoad (failsonly, IFSelect_ItemsByEntity);
52         /* Root transfers */
53         Standard_Integer nbr = aReader.NbRootsForTransfer();
54         aReader.PrintCheckTransfer (failsonly, IFSelect_ItemsByEntity);
55
56         for ( Standard_Integer n=1; n <= nbr; n++) {
57           Standard_Boolean ok = aReader.TransferRoot(n);
58           /* Collecting resulting entities */
59           Standard_Integer nbs = aReader.NbShapes();
60           if (!ok || nbs == 0)
61           {
62             // THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM);
63             continue; // skip empty root
64           }
65           /* For a single entity */
66           else if (nbr == 1 && nbs == 1) {
67             aResShape = aReader.Shape(1);
68             break;
69           }
70
71           for ( Standard_Integer i=1; i<=nbs; i++ ) {
72             TopoDS_Shape aShape = aReader.Shape(i);
73             if ( aShape.IsNull() ) {
74               // THROW_SALOME_CORBA_EXCEPTION("Null shape in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM) ;
75               //return aResShape;
76               continue;
77             }
78             else {
79               B.Add( compound, aShape ) ;
80             }
81           }
82         }
83         if ( aResShape.IsNull() )
84           aResShape = compound;
85
86       } else {
87 //        switch (status) {
88 //        case IFSelect_RetVoid:
89 //          theError = "Nothing created or No data to process";
90 //          break;
91 //        case IFSelect_RetError:
92 //          theError = "Error in command or input data";
93 //          break;
94 //        case IFSelect_RetFail:
95 //          theError = "Execution was run, but has failed";
96 //          break;
97 //        case IFSelect_RetStop:
98 //          theError = "Execution has been stopped. Quite possible, an exception was raised";
99 //          break;
100 //        default:
101 //          break;
102 //        }
103         theError = "Wrong format of the imported file. Can't import file.";
104         aResShape.Nullify();
105       }
106     }
107     catch (Standard_Failure) {
108       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
109       theError = aFail->GetMessageString();
110       aResShape.Nullify();
111     }
112     return aResShape;
113   }
114 }