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