Salome HOME
Revert "Synchronize adm files"
[modules/geom.git] / src / IGESImport / IGESImport.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 WIN32
57   #if defined IGESIMPORT_EXPORTS || defined IGESImport_EXPORTS
58     #define IGESIMPORT_EXPORT __declspec( dllexport )
59   #else
60     #define IGESIMPORT_EXPORT __declspec( dllimport )
61   #endif
62 #else
63   #define IGESIMPORT_EXPORT
64 #endif
65
66 //=============================================================================
67 /*!
68  *
69  */
70 //=============================================================================
71
72 extern "C"
73 {
74   IGESIMPORT_EXPORT
75   Handle(TCollection_HAsciiString) GetValue (const TCollection_AsciiString& theFileName,
76                                              const TCollection_AsciiString& theParameterName,
77                                              TCollection_AsciiString&       theError)
78   {
79     Handle(TCollection_HAsciiString) aValue;
80
81     if (theParameterName != "LEN_UNITS") {
82       theError = theParameterName + " parameter reading is not supported by IGES plugin";
83       return aValue;
84     }
85
86     // Set "C" numeric locale to save numbers correctly
87     Kernel_Utils::Localizer loc;
88
89     IGESControl_Reader aReader;
90
91     Interface_Static::SetCVal("xstep.cascade.unit","M");
92
93     try {
94       OCC_CATCH_SIGNALS;
95
96       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
97       if (status == IFSelect_RetDone) {
98         Handle(IGESData_IGESModel) aModel =
99           Handle(IGESData_IGESModel)::DownCast(aReader.Model());
100         if (!aModel.IsNull()) {
101           aValue = aModel->GlobalSection().UnitName();
102
103           //if (!aValue.IsNull()) {
104           //  Handle(TCollection_HAsciiString) aPrefix = new TCollection_HAsciiString ("UNIT_");
105           //  aValue->Prepend(aPrefix);
106           //}
107         }
108       }
109       else {
110         theError = theFileName + " reading failed";
111       }
112     }
113     catch (Standard_Failure) {
114       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
115       theError = aFail->GetMessageString();
116     }
117
118     return aValue;
119   }
120
121 IGESIMPORT_EXPORT
122   TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
123                        const TCollection_AsciiString& theFormatName,
124                        TCollection_AsciiString&       theError,
125                        const TDF_Label&               theShapeLabel)
126   {
127     TopoDS_Shape aResShape;
128
129     // Set "C" numeric locale to save numbers correctly
130     Kernel_Utils::Localizer loc;
131
132     IGESControl_Reader aReader;
133
134     Interface_Static::SetCVal("xstep.cascade.unit","M");
135
136     try {
137       OCC_CATCH_SIGNALS;
138
139       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
140
141       if (status == IFSelect_RetDone) {
142
143         // BEGIN: old code
144         if (theFormatName == "IGES_UNIT") {
145           Handle(IGESData_IGESModel) aModel =
146             Handle(IGESData_IGESModel)::DownCast(aReader.Model());
147           gp_Pnt P (1.0, 0.0, 0.0);
148           if (!aModel.IsNull()) {
149             Handle(TCollection_HAsciiString) aUnitName =
150               aModel->GlobalSection().UnitName();
151             if (!aUnitName.IsNull()) {
152               if (aUnitName->String()=="MM") {
153                 P = gp_Pnt(0.001,0.0,0.0);
154               }
155               else if (aUnitName->String()=="CM") {
156                 P = gp_Pnt(0.01,0.0,0.0);
157               }
158             }
159           }
160           BRep_Builder B;
161           TopoDS_Vertex V;
162           B.MakeVertex(V,P,1.e-7);
163           aResShape = V;
164           return aResShape;
165         }
166         // END: old code
167
168         if (theFormatName == "IGES_SCALE") {
169           //cout<<"need re-scale a model"<<endl;
170           // set UnitFlag to 'meter'
171           Handle(IGESData_IGESModel) aModel =
172             Handle(IGESData_IGESModel)::DownCast(aReader.Model());
173           if (!aModel.IsNull()) {
174             IGESData_GlobalSection aGS = aModel->GlobalSection();
175             aGS.SetUnitFlag(6);
176             aModel->SetGlobalSection(aGS);
177           }
178         }
179
180         MESSAGE("ImportIGES : all Geometry Transfer");
181         //OCC 5.1.2 porting
182         //     aReader.Clear();
183         //     aReader.TransferRoots(false);
184         aReader.ClearShapes();
185         aReader.TransferRoots();
186
187         MESSAGE("ImportIGES : count of shapes produced = " << aReader.NbShapes());
188         aResShape = aReader.OneShape();
189
190         // BEGIN: Store names of sub-shapes from file
191         Handle(Interface_InterfaceModel) Model = aReader.WS()->Model();
192         Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
193         if (!TR.IsNull()) {
194           Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
195           Standard_Integer nb = Model->NbEntities();
196           for (Standard_Integer i = 1; i <= nb; i++) {
197             Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast(Model->Value(i));
198             if (ent.IsNull() || ! ent->HasName()) continue;
199
200             // find target shape
201             Handle(Transfer_Binder) binder = TP->Find(ent);
202             if (binder.IsNull()) continue;
203             TopoDS_Shape S = TransferBRep::ShapeResult(binder);
204             if (S.IsNull()) continue;
205
206             // create label and set shape
207             TDF_Label L;
208             TDF_TagSource aTag;
209             L = aTag.NewChild(theShapeLabel);
210             TNaming_Builder tnBuild (L);
211             tnBuild.Generated(S);
212
213             // set a name
214             TCollection_AsciiString string = ent->NameValue()->String();
215             string.LeftAdjust();
216             string.RightAdjust();
217             TCollection_ExtendedString str (string);
218             TDataStd_Name::Set(L, str);
219           }
220         }
221         // END: Store names
222       } else {
223 //        switch (status) {
224 //        case IFSelect_RetVoid:
225 //          theError = "Nothing created or No data to process";
226 //          break;
227 //        case IFSelect_RetError:
228 //          theError = "Error in command or input data";
229 //          break;
230 //        case IFSelect_RetFail:
231 //          theError = "Execution was run, but has failed";
232 //          break;
233 //        case IFSelect_RetStop:
234 //          theError = "Execution has been stopped. Quite possible, an exception was raised";
235 //          break;
236 //        default:
237 //          break;
238 //        }
239         theError = "Wrong format of the imported file. Can't import file.";
240         aResShape.Nullify();
241       }
242     }
243     catch(Standard_Failure) {
244       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
245       theError = aFail->GetMessageString();
246       aResShape.Nullify();
247     }
248     return aResShape;
249   }
250 }