Salome HOME
Merge from V6_main 01/04/2013
[modules/geom.git] / src / IGESImport / IGESImport.cxx
1 // Copyright (C) 2007-2013  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
26 #include "utilities.h"
27
28 #include <Basics_Utils.hxx>
29
30 #include <IFSelect_ReturnStatus.hxx>
31 #include <IGESControl_Reader.hxx>
32 #include <IGESData_IGESModel.hxx>
33 #include <IGESData_IGESEntity.hxx>
34
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>
42
43 #include <TNaming_Builder.hxx>
44 #include <TDF_TagSource.hxx>
45 #include <TDataStd_Name.hxx>
46 #include <TDF_Label.hxx>
47
48 #include <TCollection_HAsciiString.hxx>
49 #include <TopoDS_Shape.hxx>
50 #include <TopoDS_Vertex.hxx>
51 #include <BRep_Builder.hxx>
52 #include <gp_Pnt.hxx>
53
54 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
55
56 #ifdef WNT
57  #if defined IGESIMPORT_EXPORTS || defined IGESImport_EXPORTS
58   #if defined WIN32
59    #define IGESIMPORT_EXPORT __declspec( dllexport )
60   #else
61    #define IGESIMPORT_EXPORT
62   #endif
63  #else
64   #if defined WIN32
65    #define IGESIMPORT_EXPORT __declspec( dllimport )
66   #else
67    #define IGESIMPORT_EXPORT
68   #endif
69  #endif
70 #else
71  #define IGESIMPORT_EXPORT
72 #endif
73
74 //=============================================================================
75 /*!
76  *
77  */
78 //=============================================================================
79
80 extern "C"
81 {
82   IGESIMPORT_EXPORT
83   Handle(TCollection_HAsciiString) GetValue (const TCollection_AsciiString& theFileName,
84                                              const TCollection_AsciiString& theParameterName,
85                                              TCollection_AsciiString&       theError)
86   {
87     Handle(TCollection_HAsciiString) aValue;
88
89     if (theParameterName != "LEN_UNITS") {
90       theError = theParameterName + " parameter reading is not supported by IGES plugin";
91       return aValue;
92     }
93
94     // Set "C" numeric locale to save numbers correctly
95     Kernel_Utils::Localizer loc;
96
97     IGESControl_Reader aReader;
98
99     Interface_Static::SetCVal("xstep.cascade.unit","M");
100
101     try {
102       OCC_CATCH_SIGNALS;
103
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();
110
111           //if (!aValue.IsNull()) {
112           //  Handle(TCollection_HAsciiString) aPrefix = new TCollection_HAsciiString ("UNIT_");
113           //  aValue->Prepend(aPrefix);
114           //}
115         }
116       }
117       else {
118         theError = theFileName + " reading failed";
119       }
120     }
121     catch (Standard_Failure) {
122       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
123       theError = aFail->GetMessageString();
124     }
125
126     return aValue;
127   }
128
129 IGESIMPORT_EXPORT
130   TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
131                        const TCollection_AsciiString& theFormatName,
132                        TCollection_AsciiString&       theError,
133                        const TDF_Label&               theShapeLabel)
134   {
135     TopoDS_Shape aResShape;
136
137     // Set "C" numeric locale to save numbers correctly
138     Kernel_Utils::Localizer loc;
139
140     IGESControl_Reader aReader;
141
142     Interface_Static::SetCVal("xstep.cascade.unit","M");
143
144     try {
145       OCC_CATCH_SIGNALS;
146
147       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
148
149       if (status == IFSelect_RetDone) {
150
151         // BEGIN: old code
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);
162               }
163               else if (aUnitName->String()=="CM") {
164                 P = gp_Pnt(0.01,0.0,0.0);
165               }
166             }
167           }
168           BRep_Builder B;
169           TopoDS_Vertex V;
170           B.MakeVertex(V,P,1.e-7);
171           aResShape = V;
172           return aResShape;
173         }
174         // END: old code
175
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();
183             aGS.SetUnitFlag(6);
184             aModel->SetGlobalSection(aGS);
185           }
186         }
187
188         MESSAGE("ImportIGES : all Geometry Transfer");
189         //OCC 5.1.2 porting
190         //     aReader.Clear();
191         //     aReader.TransferRoots(false);
192         aReader.ClearShapes();
193         aReader.TransferRoots();
194
195         MESSAGE("ImportIGES : count of shapes produced = " << aReader.NbShapes());
196         aResShape = aReader.OneShape();
197
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();
201         if (!TR.IsNull()) {
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;
207
208             // find target shape
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;
213
214             // create label and set shape
215             TDF_Label L;
216             TDF_TagSource aTag;
217             L = aTag.NewChild(theShapeLabel);
218             TNaming_Builder tnBuild (L);
219             tnBuild.Generated(S);
220
221             // set a name
222             TCollection_AsciiString string = ent->NameValue()->String();
223             string.LeftAdjust();
224             string.RightAdjust();
225             TCollection_ExtendedString str (string);
226             TDataStd_Name::Set(L, str);
227           }
228         }
229         // END: Store names
230       } else {
231 //        switch (status) {
232 //        case IFSelect_RetVoid:
233 //          theError = "Nothing created or No data to process";
234 //          break;
235 //        case IFSelect_RetError:
236 //          theError = "Error in command or input data";
237 //          break;
238 //        case IFSelect_RetFail:
239 //          theError = "Execution was run, but has failed";
240 //          break;
241 //        case IFSelect_RetStop:
242 //          theError = "Execution has been stopped. Quite possible, an exception was raised";
243 //          break;
244 //        default:
245 //          break;
246 //        }
247         theError = "Wrong format of the imported file. Can't import file.";
248         aResShape.Nullify();
249       }
250     }
251     catch(Standard_Failure) {
252       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
253       theError = aFail->GetMessageString();
254       aResShape.Nullify();
255     }
256     return aResShape;
257   }
258 }