]> SALOME platform Git repositories - modules/geom.git/blob - src/IGESPlugin/IGESPlugin_ImportDriver.cxx
Salome HOME
0023450: Fields are not displayed in GEOM
[modules/geom.git] / src / IGESPlugin / IGESPlugin_ImportDriver.cxx
1 // Copyright (C) 2014-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // internal includes
21 #include "IGESPlugin_ImportDriver.hxx"
22 #include "IGESPlugin_IImport.hxx"
23
24 // KERNEL includes
25 #include <utilities.h>
26 #include <Basics_Utils.hxx>
27
28 // GEOM includes
29 #include "GEOM_Function.hxx"
30 #include "GEOMImpl_Types.hxx"
31
32 // OOCT includes
33 #include <IFSelect_ReturnStatus.hxx>
34 #include <IGESControl_Reader.hxx>
35 #include <IGESData_IGESModel.hxx>
36 #include <IGESData_IGESEntity.hxx>
37
38 #include <Interface_Static.hxx>
39 #include <Interface_InterfaceModel.hxx>
40 #include <XSControl_TransferReader.hxx>
41 #include <XSControl_WorkSession.hxx>
42 #include <Transfer_TransientProcess.hxx>
43 #include <Transfer_Binder.hxx>
44 #include <TransferBRep.hxx>
45
46 #include <TNaming_Builder.hxx>
47 #include <TDF_TagSource.hxx>
48 #include <TDataStd_Name.hxx>
49 #include <TDF_Label.hxx>
50
51 #include <TCollection_HAsciiString.hxx>
52 #include <TopoDS_Shape.hxx>
53 #include <TopoDS_Vertex.hxx>
54 #include <BRep_Builder.hxx>
55 #include <gp_Pnt.hxx>
56
57 #include <StdFail_NotDone.hxx>
58 #include <Standard_Failure.hxx>
59 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
60
61 //=======================================================================
62 //function : GetID
63 //purpose  :
64 //=======================================================================
65 const Standard_GUID& IGESPlugin_ImportDriver::GetID()
66 {
67   static Standard_GUID aGUID("3dbb09b8-659f-4a0f-88b9-d9968c43a448");
68   return aGUID;
69 }
70
71 //=======================================================================
72 //function : IGESPlugin_ImportDriver
73 //purpose  :
74 //=======================================================================
75 IGESPlugin_ImportDriver::IGESPlugin_ImportDriver()
76 {
77 }
78
79 //=======================================================================
80 //function : Execute
81 //purpose  :
82 //=======================================================================
83 Standard_Integer IGESPlugin_ImportDriver::Execute(Handle(TFunction_Logbook)& log) const
84 {
85   if( Label().IsNull() ) return 0;
86   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
87
88   IGESPlugin_IImport aData( aFunction );
89
90   TCollection_AsciiString aFileName = aData.GetFileName().ToCString();
91   bool anIsIgnoreUnits = aData.GetIsIgnoreUnits();
92   TDF_Label aShapeLabel = aFunction->GetNamingEntry();
93
94   TopoDS_Shape aResShape;
95   TCollection_AsciiString anError;
96
97   // Set "C" numeric locale to save numbers correctly
98   Kernel_Utils::Localizer loc;
99
100   IGESControl_Reader aReader;
101
102   Interface_Static::SetCVal( "xstep.cascade.unit", "M" );
103
104   try {
105     OCC_CATCH_SIGNALS;
106
107     IFSelect_ReturnStatus status = aReader.ReadFile( aFileName.ToCString() );
108
109     if (status == IFSelect_RetDone) {
110       if( anIsIgnoreUnits ) {
111         // need re-scale a model, 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       // BEGIN: Store names of sub-shapes from file
132       Handle(Interface_InterfaceModel) Model = aReader.WS()->Model();
133       Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
134       if (!TR.IsNull()) {
135         Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
136         Standard_Integer nb = Model->NbEntities();
137         for (Standard_Integer i = 1; i <= nb; i++) {
138           Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast(Model->Value(i));
139           if (ent.IsNull() || ! ent->HasName()) continue;
140
141           // find target shape
142           Handle(Transfer_Binder) binder = TP->Find( ent );
143           if( binder.IsNull() ) continue;
144           TopoDS_Shape S = TransferBRep::ShapeResult( binder );
145           if( S.IsNull() ) continue;
146
147           // create label and set shape
148           TDF_Label L;
149           TDF_TagSource aTag;
150           L = aTag.NewChild( aShapeLabel );
151           TNaming_Builder tnBuild (L);
152           tnBuild.Generated(S);
153
154           // set a name
155           TCollection_AsciiString string = ent->NameValue()->String();
156           string.LeftAdjust();
157           string.RightAdjust();
158           TCollection_ExtendedString str (string);
159           TDataStd_Name::Set(L, str);
160         }
161       }
162       // END: Store names
163     }
164     else {
165       switch (status) {
166         case IFSelect_RetVoid:
167           anError = "Nothing created or No data to process";
168           break;
169         case IFSelect_RetError:
170           anError = "Error in command or input data";
171           break;
172         case IFSelect_RetFail:
173           anError = "Execution was run, but has failed";
174           break;
175         case IFSelect_RetStop:
176           anError = "Execution has been stopped. Quite possible, an exception was raised";
177           break;
178         default:
179           break;
180         }
181       anError = "Wrong format of the imported file. Can't import file.";
182       aResShape.Nullify();
183     }
184   }
185   catch( Standard_Failure ) {
186     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
187     anError = aFail->GetMessageString();
188     aResShape.Nullify();
189   }
190
191   if( aResShape.IsNull() ) {
192     StdFail_NotDone::Raise( anError.ToCString() );
193     return 0;
194   }
195
196   aFunction->SetValue( aResShape );
197
198   log->SetTouched(Label());
199
200   return 1;
201 }
202
203 //================================================================================
204 /*!
205  * \brief Returns a name of creation operation and names and values of creation parameters
206  */
207 //================================================================================
208
209 bool IGESPlugin_ImportDriver::
210 GetCreationInformation( std::string&             theOperationName,
211                         std::vector<GEOM_Param>& theParams )
212 {
213   if( Label().IsNull() ) return 0;
214   Handle(GEOM_Function) function = GEOM_Function::GetFunction( Label() );
215
216   IGESPlugin_IImport aCI( function );
217   Standard_Integer aType = function->GetType();
218
219   theOperationName = "ImportIGES";
220
221   switch ( aType ) {
222   case IMPORT_SHAPE:
223     AddParam( theParams, "File name", aCI.GetFileName() );
224     if( aCI.GetIsIgnoreUnits() )
225       AddParam( theParams, "Format", "IGES_SCALE" );
226     break;
227   default:
228     return false;
229   }
230   return true;
231 }
232
233 TCollection_AsciiString
234 IGESPlugin_ImportDriver::GetValue( const TCollection_AsciiString& theFileName,
235                                      const TCollection_AsciiString& theParameterName,
236                                      TCollection_AsciiString&       theError )
237 {
238   Handle(TCollection_HAsciiString) aValue;
239
240   if (theParameterName != "LEN_UNITS") {
241     theError = theParameterName + " parameter reading is not supported by IGES plugin";
242     return TCollection_AsciiString();
243   }
244
245   // Set "C" numeric locale to save numbers correctly
246   Kernel_Utils::Localizer loc;
247
248   IGESControl_Reader aReader;
249
250   Interface_Static::SetCVal("xstep.cascade.unit","M");
251
252   try {
253     OCC_CATCH_SIGNALS;
254
255     IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
256     if (status == IFSelect_RetDone) {
257       Handle(IGESData_IGESModel) aModel =
258         Handle(IGESData_IGESModel)::DownCast(aReader.Model());
259       if (!aModel.IsNull()) {
260         aValue = aModel->GlobalSection().UnitName();
261         //if (!aValue.IsNull()) {
262         //  Handle(TCollection_HAsciiString) aPrefix = new TCollection_HAsciiString ("UNIT_");
263         //  aValue->Prepend(aPrefix);
264         //}
265       }
266     }
267     else {
268       theError = theFileName + " reading failed";
269     }
270   }
271   catch (Standard_Failure) {
272     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
273     theError = aFail->GetMessageString();
274   }
275   if (!aValue.IsNull())
276     return aValue->String();
277   else
278     return TCollection_AsciiString();
279 }
280
281 IMPLEMENT_STANDARD_RTTIEXT( IGESPlugin_ImportDriver, GEOM_BaseDriver );