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