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