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