1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: IGESImport.cxx
23 // Created: Wed May 19 14:36:35 2004
24 // Author: Pavel TELKOV
26 #include "utilities.h"
28 #include <Basics_Utils.hxx>
30 #include <IFSelect_ReturnStatus.hxx>
31 #include <IGESControl_Reader.hxx>
32 #include <IGESData_IGESModel.hxx>
33 #include <IGESData_IGESEntity.hxx>
35 #include <Interface_Static.hxx>
36 #include <Interface_InterfaceModel.hxx>
37 #include <XSControl_TransferReader.hxx>
38 #include <XSControl_WorkSession.hxx>
39 #include <Transfer_TransientProcess.hxx>
40 #include <Transfer_Binder.hxx>
41 #include <TransferBRep.hxx>
43 #include <TNaming_Builder.hxx>
44 #include <TDF_TagSource.hxx>
45 #include <TDataStd_Name.hxx>
46 #include <TDF_Label.hxx>
48 #include <TCollection_HAsciiString.hxx>
49 #include <TopoDS_Shape.hxx>
50 #include <TopoDS_Vertex.hxx>
51 #include <BRep_Builder.hxx>
54 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
57 #if defined IGESIMPORT_EXPORTS || defined IGESImport_EXPORTS
59 #define IGESIMPORT_EXPORT __declspec( dllexport )
61 #define IGESIMPORT_EXPORT
65 #define IGESIMPORT_EXPORT __declspec( dllimport )
67 #define IGESIMPORT_EXPORT
71 #define IGESIMPORT_EXPORT
74 //=============================================================================
78 //=============================================================================
83 Handle(TCollection_HAsciiString) GetValue (const TCollection_AsciiString& theFileName,
84 const TCollection_AsciiString& theParameterName,
85 TCollection_AsciiString& theError)
87 Handle(TCollection_HAsciiString) aValue;
89 if (theParameterName != "LEN_UNITS") {
90 theError = theParameterName + " parameter reading is not supported by IGES plugin";
94 // Set "C" numeric locale to save numbers correctly
95 Kernel_Utils::Localizer loc;
97 IGESControl_Reader aReader;
99 Interface_Static::SetCVal("xstep.cascade.unit","M");
104 IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
105 if (status == IFSelect_RetDone) {
106 Handle(IGESData_IGESModel) aModel =
107 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
108 if (!aModel.IsNull()) {
109 aValue = aModel->GlobalSection().UnitName();
111 if (!aValue.IsNull()) {
112 Handle(TCollection_HAsciiString) aPrefix = new TCollection_HAsciiString ("UNIT_");
113 aValue->Prepend(aPrefix);
118 theError = theFileName + " reading failed";
121 catch (Standard_Failure) {
122 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
123 theError = aFail->GetMessageString();
130 TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
131 const TCollection_AsciiString& theFormatName,
132 TCollection_AsciiString& theError,
133 const TDF_Label& theShapeLabel)
135 TopoDS_Shape aResShape;
137 // Set "C" numeric locale to save numbers correctly
138 Kernel_Utils::Localizer loc;
140 IGESControl_Reader aReader;
142 Interface_Static::SetCVal("xstep.cascade.unit","M");
147 IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
149 if (status == IFSelect_RetDone) {
152 if (theFormatName == "IGES_UNIT") {
153 Handle(IGESData_IGESModel) aModel =
154 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
155 gp_Pnt P (1.0, 0.0, 0.0);
156 if (!aModel.IsNull()) {
157 Handle(TCollection_HAsciiString) aUnitName =
158 aModel->GlobalSection().UnitName();
159 if (!aUnitName.IsNull()) {
160 if (aUnitName->String()=="MM") {
161 P = gp_Pnt(0.001,0.0,0.0);
163 else if (aUnitName->String()=="CM") {
164 P = gp_Pnt(0.01,0.0,0.0);
170 B.MakeVertex(V,P,1.e-7);
176 if (theFormatName == "IGES_SCALE") {
177 //cout<<"need re-scale a model"<<endl;
178 // set UnitFlag to 'meter'
179 Handle(IGESData_IGESModel) aModel =
180 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
181 if (!aModel.IsNull()) {
182 IGESData_GlobalSection aGS = aModel->GlobalSection();
184 aModel->SetGlobalSection(aGS);
188 MESSAGE("ImportIGES : all Geometry Transfer");
191 // aReader.TransferRoots(false);
192 aReader.ClearShapes();
193 aReader.TransferRoots();
195 MESSAGE("ImportIGES : count of shapes produced = " << aReader.NbShapes());
196 aResShape = aReader.OneShape();
198 // BEGIN: Store names of sub-shapes from file
199 Handle(Interface_InterfaceModel) Model = aReader.WS()->Model();
200 Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
202 Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
203 Standard_Integer nb = Model->NbEntities();
204 for (Standard_Integer i = 1; i <= nb; i++) {
205 Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast(Model->Value(i));
206 if (ent.IsNull() || ! ent->HasName()) continue;
209 Handle(Transfer_Binder) binder = TP->Find(ent);
210 if (binder.IsNull()) continue;
211 TopoDS_Shape S = TransferBRep::ShapeResult(binder);
212 if (S.IsNull()) continue;
214 // create label and set shape
217 L = aTag.NewChild(theShapeLabel);
218 TNaming_Builder tnBuild (L);
219 tnBuild.Generated(S);
222 TCollection_AsciiString string = ent->NameValue()->String();
224 string.RightAdjust();
225 TCollection_ExtendedString str (string);
226 TDataStd_Name::Set(L, str);
232 // case IFSelect_RetVoid:
233 // theError = "Nothing created or No data to process";
235 // case IFSelect_RetError:
236 // theError = "Error in command or input data";
238 // case IFSelect_RetFail:
239 // theError = "Execution was run, but has failed";
241 // case IFSelect_RetStop:
242 // theError = "Execution has been stopped. Quite possible, an exception was raised";
247 theError = "Wrong format of the imported file. Can't import file.";
251 catch(Standard_Failure) {
252 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
253 theError = aFail->GetMessageString();