]> SALOME platform Git repositories - modules/geom.git/blob - src/IGESImport/IGESImport.cxx
Salome HOME
0020324: EDF 717 GEOM : Import IGES File
[modules/geom.git] / src / IGESImport / IGESImport.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:        IGESImport.cxx
23 // Created:     Wed May 19 14:36:35 2004
24 // Author:      Pavel TELKOV
25 //              <ptv@mutex.nnov.opencascade.com>
26 //
27 #include "utilities.h"
28
29 #include <IFSelect_ReturnStatus.hxx>
30 #include <IGESControl_Reader.hxx>
31 #include <IGESData_IGESModel.hxx>
32
33 #include <TCollection_HAsciiString.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TDF_Label.hxx>
36
37 #include <TopoDS_Vertex.hxx>
38 #include <BRep_Builder.hxx>
39 #include <gp_Pnt.hxx>
40 #include <Interface_Static.hxx>
41
42 #ifdef WNT
43  #if defined IGESIMPORT_EXPORTS || defined IGESImport_EXPORTS
44   #if defined WIN32
45    #define IGESIMPORT_EXPORT __declspec( dllexport )
46   #else
47    #define IGESIMPORT_EXPORT
48   #endif
49  #else
50   #if defined WIN32
51    #define IGESIMPORT_EXPORT __declspec( dllimport )
52   #else
53    #define IGESIMPORT_EXPORT
54   #endif
55  #endif
56 #else
57  #define IGESIMPORT_EXPORT
58 #endif
59
60 //=============================================================================
61 /*!
62  *
63  */
64 //=============================================================================
65
66 extern "C"
67 {
68 IGESIMPORT_EXPORT
69   TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
70                        const TCollection_AsciiString& theFormatName,
71                        TCollection_AsciiString&       theError,
72                        const TDF_Label&)
73   {
74     IGESControl_Reader aReader;
75     TopoDS_Shape aResShape;
76     Interface_Static::SetCVal("xstep.cascade.unit","M");
77     try {
78       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
79
80       if (status == IFSelect_RetDone) {
81
82         if( theFormatName == "IGES_UNIT" ) {
83           Handle(IGESData_IGESModel) aModel = 
84             Handle(IGESData_IGESModel)::DownCast(aReader.Model());
85           gp_Pnt P(1.0,0.0,0.0);
86           if(!aModel.IsNull()) {
87             Handle(TCollection_HAsciiString) aUnitName = 
88               aModel->GlobalSection().UnitName();
89             //cout<<"aUnitName = "<<aUnitName->ToCString()<<endl;
90             //cout<<"aUnitFlag = "<<aModel->GlobalSection().UnitFlag()<<endl;
91             if( aUnitName->String()=="MM" ) {
92               P = gp_Pnt(0.001,0.0,0.0);
93             }
94             else if( aUnitName->String()=="CM" ) {
95               P = gp_Pnt(0.01,0.0,0.0);
96             }
97           }
98           BRep_Builder B;
99           TopoDS_Vertex V;
100           B.MakeVertex(V,P,1.e-7);
101           aResShape = V;
102           return aResShape;
103         }
104         if( theFormatName == "IGES_SCALE" ) {
105           //cout<<"need re-scale a model"<<endl;
106           // set UnitFlag to 'meter'
107           Handle(IGESData_IGESModel) aModel = 
108             Handle(IGESData_IGESModel)::DownCast(aReader.Model());
109           if(!aModel.IsNull()) {
110             IGESData_GlobalSection aGS = aModel->GlobalSection();
111             aGS.SetUnitFlag(6);
112             aModel->SetGlobalSection(aGS);
113           }
114         }
115
116         MESSAGE("ImportIGES : all Geometry Transfer");
117         //OCC 5.1.2 porting
118         //     aReader.Clear();
119         //     aReader.TransferRoots(false);
120         aReader.ClearShapes();
121         aReader.TransferRoots();
122
123         MESSAGE("ImportIGES : count of shapes produced = " << aReader.NbShapes());
124         aResShape = aReader.OneShape();
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 }