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