Salome HOME
05f5b480c978ecf0cc57d7bc595ab11ca7d9919d
[modules/geom.git] / 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  #if defined STEPIMPORT_EXPORTS
42   #if defined WIN32
43    #define STEPIMPORT_EXPORT __declspec( dllexport )
44   #else
45    #define STEPIMPORT_EXPORT
46   #endif
47  #else
48   #if defined WIN32
49    #define STEPIMPORT_EXPORT __declspec( dllimport )
50   #else
51    #define STEPIMPORT_EXPORT
52   #endif
53  #endif
54 #else
55  #define STEPIMPORT_EXPORT
56 #endif
57
58 //=============================================================================
59 /*!
60  *  Import()
61  */
62 //=============================================================================
63
64 extern "C"
65 {
66 STEPIMPORT_EXPORT
67   TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
68                        const TCollection_AsciiString& /*theFormatName*/,
69                        TCollection_AsciiString&       theError)
70   {
71     MESSAGE("Import STEP model from file " << theFileName.ToCString());
72     TopoDS_Shape aResShape;
73     //VRV: OCC 4.0 migration
74     STEPControl_Reader aReader;
75     //VRV: OCC 4.0 migration
76     TopoDS_Compound compound;
77     BRep_Builder B;
78     B.MakeCompound( compound );
79     try {
80 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
81       OCC_CATCH_SIGNALS;
82 #endif
83       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
84
85       if (status == IFSelect_RetDone) {
86         Standard_Boolean failsonly = Standard_False ;
87         aReader.PrintCheckLoad (failsonly, IFSelect_ItemsByEntity);
88         /* Root transfers */
89         Standard_Integer nbr = aReader.NbRootsForTransfer();
90         aReader.PrintCheckTransfer (failsonly, IFSelect_ItemsByEntity);
91
92         for ( Standard_Integer n=1; n <= nbr; n++) {
93           Standard_Boolean ok = aReader.TransferRoot(n);
94           /* Collecting resulting entities */
95           Standard_Integer nbs = aReader.NbShapes();
96           if (!ok || nbs == 0)
97           {
98             // THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM);
99             continue; // skip empty root
100           }
101           /* For a single entity */
102           else if (nbr == 1 && nbs == 1) {
103             aResShape = aReader.Shape(1);
104             break;
105           }
106
107           for ( Standard_Integer i=1; i<=nbs; i++ ) {
108             TopoDS_Shape aShape = aReader.Shape(i);
109             if ( aShape.IsNull() ) {
110               // THROW_SALOME_CORBA_EXCEPTION("Null shape in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM) ;
111               //return aResShape;
112               continue;
113             }
114             else {
115               B.Add( compound, aShape ) ;
116             }
117           }
118         }
119         if ( aResShape.IsNull() )
120           aResShape = compound;
121
122       } else {
123 //        switch (status) {
124 //        case IFSelect_RetVoid:
125 //          theError = "Nothing created or No data to process";
126 //          break;
127 //        case IFSelect_RetError:
128 //          theError = "Error in command or input data";
129 //          break;
130 //        case IFSelect_RetFail:
131 //          theError = "Execution was run, but has failed";
132 //          break;
133 //        case IFSelect_RetStop:
134 //          theError = "Execution has been stopped. Quite possible, an exception was raised";
135 //          break;
136 //        default:
137 //          break;
138 //        }
139         theError = "Wrong format of the imported file. Can't import file.";
140         aResShape.Nullify();
141       }
142     }
143     catch (Standard_Failure) {
144       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
145       theError = aFail->GetMessageString();
146       aResShape.Nullify();
147     }
148     return aResShape;
149   }
150 }