]> SALOME platform Git repositories - modules/geom.git/blob - src/STEPImport/STEPImport.cxx
Salome HOME
3fb9cc511a2bec645797b29d5470f0c1cd9beced
[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/
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                        TCollection_AsciiString&       theError)
56   {
57     MESSAGE("Import STEP model from file " << theFileName.ToCString());
58     TopoDS_Shape aResShape;
59     //VRV: OCC 4.0 migration
60     STEPControl_Reader aReader;
61     //VRV: OCC 4.0 migration
62     TopoDS_Compound compound;
63     BRep_Builder B;
64     B.MakeCompound( compound );
65     try {
66       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
67
68       if (status == IFSelect_RetDone) {
69         Standard_Boolean failsonly = Standard_False ;
70         aReader.PrintCheckLoad (failsonly, IFSelect_ItemsByEntity);
71         /* Root transfers */
72         Standard_Integer nbr = aReader.NbRootsForTransfer();
73         aReader.PrintCheckTransfer (failsonly, IFSelect_ItemsByEntity);
74
75         for ( Standard_Integer n=1; n <= nbr; n++) {
76           Standard_Boolean ok = aReader.TransferRoot(n);
77           /* Collecting resulting entities */
78           Standard_Integer nbs = aReader.NbShapes();
79           if (!ok || nbs == 0)
80           {
81             // THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM);
82             continue; // skip empty root
83           }
84           /* For a single entity */
85           else if (nbr == 1 && nbs == 1) {
86             aResShape = aReader.Shape(1);
87             break;
88           }
89
90           for ( Standard_Integer i=1; i<=nbs; i++ ) {
91             TopoDS_Shape aShape = aReader.Shape(i);
92             if ( aShape.IsNull() ) {
93               // THROW_SALOME_CORBA_EXCEPTION("Null shape in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM) ;
94               //return aResShape;
95               continue;
96             }
97             else {
98               B.Add( compound, aShape ) ;
99             }
100           }
101         }
102         if ( aResShape.IsNull() )
103           aResShape = compound;
104
105       } else {
106 //        switch (status) {
107 //        case IFSelect_RetVoid:
108 //          theError = "Nothing created or No data to process";
109 //          break;
110 //        case IFSelect_RetError:
111 //          theError = "Error in command or input data";
112 //          break;
113 //        case IFSelect_RetFail:
114 //          theError = "Execution was run, but has failed";
115 //          break;
116 //        case IFSelect_RetStop:
117 //          theError = "Execution has been stopped. Quite possible, an exception was raised";
118 //          break;
119 //        default:
120 //          break;
121 //        }
122         theError = "Wrong format of the imported file. Can't import file.";
123         aResShape.Nullify();
124       }
125     }
126     catch (Standard_Failure) {
127       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
128       theError = aFail->GetMessageString();
129       aResShape.Nullify();
130     }
131     return aResShape;
132   }
133 }