Salome HOME
PAL12003: Problem when importing IGS geometry. Export to IGES 5.3 (supporting BRep...
[modules/geom.git] / src / STEPImport / STEPImport.cxx
1 // File:        STEPImport.cxx
2 // Created:     Wed May 19 14:41:10 2004
3 // Author:      Pavel TELKOV
4 //              <ptv@mutex.nnov.opencascade.com>
5
6 #include "utilities.h"
7
8 #include <BRep_Builder.hxx>
9
10 #include <IFSelect_ReturnStatus.hxx>
11
12 #include <STEPControl_Reader.hxx>
13
14 #include <TCollection_AsciiString.hxx>
15 #include <TopoDS_Compound.hxx>
16 #include <TopoDS_Shape.hxx>
17
18 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
19
20 //=============================================================================
21 /*!
22  *  Import()
23  */
24 //=============================================================================
25
26 extern "C"
27 {
28 #ifdef WNT
29   __declspec(__dllexport)
30 #endif
31   TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
32                        const TCollection_AsciiString& /*theFormatName*/,
33                        TCollection_AsciiString&       theError)
34   {
35     MESSAGE("Import STEP model from file " << theFileName.ToCString());
36     TopoDS_Shape aResShape;
37     //VRV: OCC 4.0 migration
38     STEPControl_Reader aReader;
39     //VRV: OCC 4.0 migration
40     TopoDS_Compound compound;
41     BRep_Builder B;
42     B.MakeCompound( compound );
43     try {
44       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
45
46       if (status == IFSelect_RetDone) {
47         Standard_Boolean failsonly = Standard_False ;
48         aReader.PrintCheckLoad (failsonly, IFSelect_ItemsByEntity);
49         /* Root transfers */
50         Standard_Integer nbr = aReader.NbRootsForTransfer();
51         aReader.PrintCheckTransfer (failsonly, IFSelect_ItemsByEntity);
52
53         for ( Standard_Integer n=1; n <= nbr; n++) {
54           Standard_Boolean ok = aReader.TransferRoot(n);
55           /* Collecting resulting entities */
56           Standard_Integer nbs = aReader.NbShapes();
57           if (!ok || nbs == 0)
58           {
59             // THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM);
60             continue; // skip empty root
61           }
62           /* For a single entity */
63           else if (nbr == 1 && nbs == 1) {
64             aResShape = aReader.Shape(1);
65             break;
66           }
67
68           for ( Standard_Integer i=1; i<=nbs; i++ ) {
69             TopoDS_Shape aShape = aReader.Shape(i);
70             if ( aShape.IsNull() ) {
71               // THROW_SALOME_CORBA_EXCEPTION("Null shape in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM) ;
72               //return aResShape;
73               continue;
74             }
75             else {
76               B.Add( compound, aShape ) ;
77             }
78           }
79         }
80         if ( aResShape.IsNull() )
81           aResShape = compound;
82
83       } else {
84 //        switch (status) {
85 //        case IFSelect_RetVoid:
86 //          theError = "Nothing created or No data to process";
87 //          break;
88 //        case IFSelect_RetError:
89 //          theError = "Error in command or input data";
90 //          break;
91 //        case IFSelect_RetFail:
92 //          theError = "Execution was run, but has failed";
93 //          break;
94 //        case IFSelect_RetStop:
95 //          theError = "Execution has been stopped. Quite possible, an exception was raised";
96 //          break;
97 //        default:
98 //          break;
99 //        }
100         theError = "Wrong format of the imported file. Can't import file.";
101         aResShape.Nullify();
102       }
103     }
104     catch (Standard_Failure) {
105       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
106       theError = aFail->GetMessageString();
107       aResShape.Nullify();
108     }
109     return aResShape;
110   }
111 }