Salome HOME
Issue 0020904: [CEA 411] export VTK in GEOM
[modules/geom.git] / src / STEPImport / STEPImport.cxx
1 //  Copyright (C) 2007-2010  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
23 // File:        STEPImport.cxx
24 // Created:     Wed May 19 14:41:10 2004
25 // Author:      Pavel TELKOV
26 //              <ptv@mutex.nnov.opencascade.com>
27 //
28 #include "utilities.h"
29
30 #include <Basics_Utils.hxx>
31
32 #include <BRep_Builder.hxx>
33
34 #include <IFSelect_ReturnStatus.hxx>
35
36 #include <STEPControl_Reader.hxx>
37
38 #include <TCollection_AsciiString.hxx>
39 #include <TopoDS_Compound.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TDF_Label.hxx>
42 #include <Interface_Static.hxx>
43
44 #include <Standard_Failure.hxx>
45 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
46
47 #ifdef WNT
48  #if defined STEPIMPORT_EXPORTS || defined STEPImport_EXPORTS
49   #if defined WIN32
50    #define STEPIMPORT_EXPORT __declspec( dllexport )
51   #else
52    #define STEPIMPORT_EXPORT
53   #endif
54  #else
55   #if defined WIN32
56    #define STEPIMPORT_EXPORT __declspec( dllimport )
57   #else
58    #define STEPIMPORT_EXPORT
59   #endif
60  #endif
61 #else
62  #define STEPIMPORT_EXPORT
63 #endif
64
65 //=============================================================================
66 /*!
67  *  Import()
68  */
69 //=============================================================================
70
71 extern "C"
72 {
73 STEPIMPORT_EXPORT
74   TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
75                        const TCollection_AsciiString& /*theFormatName*/,
76                        TCollection_AsciiString&       theError,
77                        const TDF_Label&)
78   {
79     MESSAGE("Import STEP model from file " << theFileName.ToCString());
80     // Set "C" numeric locale to save numbers correctly
81     Kernel_Utils::Localizer loc;
82     TopoDS_Shape aResShape;
83     //VRV: OCC 4.0 migration
84     STEPControl_Reader aReader;
85     //VSR: 16/09/09: Convert to METERS
86     Interface_Static::SetCVal("xstep.cascade.unit","M");
87     //VRV: OCC 4.0 migration
88     TopoDS_Compound compound;
89     BRep_Builder B;
90     B.MakeCompound( compound );
91     try {
92 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
93       OCC_CATCH_SIGNALS;
94 #endif
95       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
96
97       if (status == IFSelect_RetDone) {
98         Standard_Boolean failsonly = Standard_False ;
99         aReader.PrintCheckLoad (failsonly, IFSelect_ItemsByEntity);
100         /* Root transfers */
101         Standard_Integer nbr = aReader.NbRootsForTransfer();
102         aReader.PrintCheckTransfer (failsonly, IFSelect_ItemsByEntity);
103
104         for ( Standard_Integer n=1; n <= nbr; n++) {
105           Standard_Boolean ok = aReader.TransferRoot(n);
106           /* Collecting resulting entities */
107           Standard_Integer nbs = aReader.NbShapes();
108           if (!ok || nbs == 0)
109           {
110             // THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM);
111             continue; // skip empty root
112           }
113           /* For a single entity */
114           else if (nbr == 1 && nbs == 1) {
115             aResShape = aReader.Shape(1);
116             break;
117           }
118
119           for ( Standard_Integer i=1; i<=nbs; i++ ) {
120             TopoDS_Shape aShape = aReader.Shape(i);
121             if ( aShape.IsNull() ) {
122               // THROW_SALOME_CORBA_EXCEPTION("Null shape in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM) ;
123               //return aResShape;
124               continue;
125             }
126             else {
127               B.Add( compound, aShape ) ;
128             }
129           }
130         }
131         if ( aResShape.IsNull() )
132           aResShape = compound;
133
134       } else {
135 //        switch (status) {
136 //        case IFSelect_RetVoid:
137 //          theError = "Nothing created or No data to process";
138 //          break;
139 //        case IFSelect_RetError:
140 //          theError = "Error in command or input data";
141 //          break;
142 //        case IFSelect_RetFail:
143 //          theError = "Execution was run, but has failed";
144 //          break;
145 //        case IFSelect_RetStop:
146 //          theError = "Execution has been stopped. Quite possible, an exception was raised";
147 //          break;
148 //        default:
149 //          break;
150 //        }
151         theError = "Wrong format of the imported file. Can't import file.";
152         aResShape.Nullify();
153       }
154     }
155     catch (Standard_Failure) {
156       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
157       theError = aFail->GetMessageString();
158       aResShape.Nullify();
159     }
160     // Return previous locale
161     return aResShape;
162   }
163 }