Salome HOME
Join modifications from branch BR_For_OCT_611: migration to OCCT6.1.1 with new except...
[modules/geom.git] / src / STEPImport / STEPImport.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File:        STEPImport.cxx
21 // Created:     Wed May 19 14:41:10 2004
22 // Author:      Pavel TELKOV
23 //              <ptv@mutex.nnov.opencascade.com>
24
25 #include "utilities.h"
26
27 #include <BRep_Builder.hxx>
28
29 #include <IFSelect_ReturnStatus.hxx>
30
31 #include <STEPControl_Reader.hxx>
32
33 #include <TCollection_AsciiString.hxx>
34 #include <TopoDS_Compound.hxx>
35 #include <TopoDS_Shape.hxx>
36
37 #include <Standard_Failure.hxx>
38 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
39
40 #ifdef WNT
41 #include <SALOME_WNT.hxx>
42 #else
43 #define SALOME_WNT_EXPORT
44 #endif
45
46 //=============================================================================
47 /*!
48  *  Import()
49  */
50 //=============================================================================
51
52 extern "C"
53 {
54 SALOME_WNT_EXPORT
55   TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
56                        const TCollection_AsciiString& /*theFormatName*/,
57                        TCollection_AsciiString&       theError)
58   {
59     MESSAGE("Import STEP model from file " << theFileName.ToCString());
60     TopoDS_Shape aResShape;
61     //VRV: OCC 4.0 migration
62     STEPControl_Reader aReader;
63     //VRV: OCC 4.0 migration
64     TopoDS_Compound compound;
65     BRep_Builder B;
66     B.MakeCompound( compound );
67     try {
68 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
69       OCC_CATCH_SIGNALS;
70 #endif
71       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
72
73       if (status == IFSelect_RetDone) {
74         Standard_Boolean failsonly = Standard_False ;
75         aReader.PrintCheckLoad (failsonly, IFSelect_ItemsByEntity);
76         /* Root transfers */
77         Standard_Integer nbr = aReader.NbRootsForTransfer();
78         aReader.PrintCheckTransfer (failsonly, IFSelect_ItemsByEntity);
79
80         for ( Standard_Integer n=1; n <= nbr; n++) {
81           Standard_Boolean ok = aReader.TransferRoot(n);
82           /* Collecting resulting entities */
83           Standard_Integer nbs = aReader.NbShapes();
84           if (!ok || nbs == 0)
85           {
86             // THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM);
87             continue; // skip empty root
88           }
89           /* For a single entity */
90           else if (nbr == 1 && nbs == 1) {
91             aResShape = aReader.Shape(1);
92             break;
93           }
94
95           for ( Standard_Integer i=1; i<=nbs; i++ ) {
96             TopoDS_Shape aShape = aReader.Shape(i);
97             if ( aShape.IsNull() ) {
98               // THROW_SALOME_CORBA_EXCEPTION("Null shape in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM) ;
99               //return aResShape;
100               continue;
101             }
102             else {
103               B.Add( compound, aShape ) ;
104             }
105           }
106         }
107         if ( aResShape.IsNull() )
108           aResShape = compound;
109
110       } else {
111 //        switch (status) {
112 //        case IFSelect_RetVoid:
113 //          theError = "Nothing created or No data to process";
114 //          break;
115 //        case IFSelect_RetError:
116 //          theError = "Error in command or input data";
117 //          break;
118 //        case IFSelect_RetFail:
119 //          theError = "Execution was run, but has failed";
120 //          break;
121 //        case IFSelect_RetStop:
122 //          theError = "Execution has been stopped. Quite possible, an exception was raised";
123 //          break;
124 //        default:
125 //          break;
126 //        }
127         theError = "Wrong format of the imported file. Can't import file.";
128         aResShape.Nullify();
129       }
130     }
131     catch (Standard_Failure) {
132       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
133       theError = aFail->GetMessageString();
134       aResShape.Nullify();
135     }
136     return aResShape;
137   }
138 }