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