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