]> SALOME platform Git repositories - modules/geom.git/blob - src/IGESImport/IGESImport.cxx
Salome HOME
Merge from V5_1_main 14/05/2010
[modules/geom.git] / src / IGESImport / IGESImport.cxx
1 //  Copyright (C) 2007-2010  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
23 // File:        IGESImport.cxx
24 // Created:     Wed May 19 14:36:35 2004
25 // Author:      Pavel TELKOV
26 //              <ptv@mutex.nnov.opencascade.com>
27 //
28 #include "utilities.h"
29
30 #include <Basics_Utils.hxx>
31
32 #include <IFSelect_ReturnStatus.hxx>
33 #include <IGESControl_Reader.hxx>
34 #include <IGESData_IGESModel.hxx>
35
36 #include <TCollection_HAsciiString.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TDF_Label.hxx>
39
40 #include <TopoDS_Vertex.hxx>
41 #include <BRep_Builder.hxx>
42 #include <gp_Pnt.hxx>
43 #include <Interface_Static.hxx>
44
45 #ifdef WNT
46  #if defined IGESIMPORT_EXPORTS || defined IGESImport_EXPORTS
47   #if defined WIN32
48    #define IGESIMPORT_EXPORT __declspec( dllexport )
49   #else
50    #define IGESIMPORT_EXPORT
51   #endif
52  #else
53   #if defined WIN32
54    #define IGESIMPORT_EXPORT __declspec( dllimport )
55   #else
56    #define IGESIMPORT_EXPORT
57   #endif
58  #endif
59 #else
60  #define IGESIMPORT_EXPORT
61 #endif
62
63 //=============================================================================
64 /*!
65  *
66  */
67 //=============================================================================
68
69 extern "C"
70 {
71 IGESIMPORT_EXPORT
72   TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
73                        const TCollection_AsciiString& theFormatName,
74                        TCollection_AsciiString&       theError,
75                        const TDF_Label&)
76   {
77     // Set "C" numeric locale to save numbers correctly
78     Kernel_Utils::Localizer loc;
79
80     IGESControl_Reader aReader;
81     TopoDS_Shape aResShape;
82     Interface_Static::SetCVal("xstep.cascade.unit","M");
83     try {
84       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
85
86       if (status == IFSelect_RetDone) {
87
88         if( theFormatName == "IGES_UNIT" ) {
89           Handle(IGESData_IGESModel) aModel = 
90             Handle(IGESData_IGESModel)::DownCast(aReader.Model());
91           gp_Pnt P(1.0,0.0,0.0);
92           if(!aModel.IsNull()) {
93             Handle(TCollection_HAsciiString) aUnitName = 
94               aModel->GlobalSection().UnitName();
95             //cout<<"aUnitName = "<<aUnitName->ToCString()<<endl;
96             //cout<<"aUnitFlag = "<<aModel->GlobalSection().UnitFlag()<<endl;
97             if( aUnitName->String()=="MM" ) {
98               P = gp_Pnt(0.001,0.0,0.0);
99             }
100             else if( aUnitName->String()=="CM" ) {
101               P = gp_Pnt(0.01,0.0,0.0);
102             }
103           }
104           BRep_Builder B;
105           TopoDS_Vertex V;
106           B.MakeVertex(V,P,1.e-7);
107           aResShape = V;
108           return aResShape;
109         }
110         if( theFormatName == "IGES_SCALE" ) {
111           //cout<<"need re-scale a model"<<endl;
112           // set UnitFlag to 'meter'
113           Handle(IGESData_IGESModel) aModel = 
114             Handle(IGESData_IGESModel)::DownCast(aReader.Model());
115           if(!aModel.IsNull()) {
116             IGESData_GlobalSection aGS = aModel->GlobalSection();
117             aGS.SetUnitFlag(6);
118             aModel->SetGlobalSection(aGS);
119           }
120         }
121
122         MESSAGE("ImportIGES : all Geometry Transfer");
123         //OCC 5.1.2 porting
124         //     aReader.Clear();
125         //     aReader.TransferRoots(false);
126         aReader.ClearShapes();
127         aReader.TransferRoots();
128
129         MESSAGE("ImportIGES : count of shapes produced = " << aReader.NbShapes());
130         aResShape = aReader.OneShape();
131
132       } else {
133 //        switch (status) {
134 //        case IFSelect_RetVoid:
135 //          theError = "Nothing created or No data to process";
136 //          break;
137 //        case IFSelect_RetError:
138 //          theError = "Error in command or input data";
139 //          break;
140 //        case IFSelect_RetFail:
141 //          theError = "Execution was run, but has failed";
142 //          break;
143 //        case IFSelect_RetStop:
144 //          theError = "Execution has been stopped. Quite possible, an exception was raised";
145 //          break;
146 //        default:
147 //          break;
148 //        }
149         theError = "Wrong format of the imported file. Can't import file.";
150         aResShape.Nullify();
151       }
152     }
153     catch(Standard_Failure) {
154       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
155       theError = aFail->GetMessageString();
156       aResShape.Nullify();
157     }
158     return aResShape;
159   }
160 }