Salome HOME
33d0127acf759da622a578405e839b3af52c8cb0
[modules/geom.git] / src / IGESPlugin / IGESPlugin_ImportDriver.cxx
1 // Copyright (C) 2014-2023  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& aFail ) {
186     anError = aFail.GetMessageString();
187     aResShape.Nullify();
188   }
189
190   if( aResShape.IsNull() ) {
191     StdFail_NotDone::Raise( anError.ToCString() );
192     return 0;
193   }
194
195   aFunction->SetValue( aResShape );
196
197   log->SetTouched(Label());
198
199   return 1;
200 }
201
202 //================================================================================
203 /*!
204  * \brief Returns a name of creation operation and names and values of creation parameters
205  */
206 //================================================================================
207
208 bool IGESPlugin_ImportDriver::
209 GetCreationInformation( std::string&             theOperationName,
210                         std::vector<GEOM_Param>& theParams )
211 {
212   if( Label().IsNull() ) return 0;
213   Handle(GEOM_Function) function = GEOM_Function::GetFunction( Label() );
214
215   IGESPlugin_IImport aCI( function );
216   Standard_Integer aType = function->GetType();
217
218   theOperationName = "ImportIGES";
219
220   switch ( aType ) {
221   case IMPORT_SHAPE:
222     AddParam( theParams, "File name", aCI.GetFileName() );
223     if( aCI.GetIsIgnoreUnits() )
224       AddParam( theParams, "Format", "IGES_SCALE" );
225     break;
226   default:
227     return false;
228   }
229   return true;
230 }
231
232 TCollection_AsciiString
233 IGESPlugin_ImportDriver::GetValue( const TCollection_AsciiString& theFileName,
234                                      const TCollection_AsciiString& theParameterName,
235                                      TCollection_AsciiString&       theError )
236 {
237   Handle(TCollection_HAsciiString) aValue;
238
239   if (theParameterName != "LEN_UNITS") {
240     theError = theParameterName + " parameter reading is not supported by IGES plugin";
241     return TCollection_AsciiString();
242   }
243
244   // Set "C" numeric locale to save numbers correctly
245   Kernel_Utils::Localizer loc;
246
247   IGESControl_Reader aReader;
248
249   Interface_Static::SetCVal("xstep.cascade.unit","M");
250
251   try {
252     OCC_CATCH_SIGNALS;
253
254     IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
255     if (status == IFSelect_RetDone) {
256       Handle(IGESData_IGESModel) aModel =
257         Handle(IGESData_IGESModel)::DownCast(aReader.Model());
258       if (!aModel.IsNull()) {
259         aValue = aModel->GlobalSection().UnitName();
260         //if (!aValue.IsNull()) {
261         //  Handle(TCollection_HAsciiString) aPrefix = new TCollection_HAsciiString ("UNIT_");
262         //  aValue->Prepend(aPrefix);
263         //}
264       }
265     }
266     else {
267       theError = theFileName + " reading failed";
268     }
269   }
270   catch (Standard_Failure& aFail) {
271     theError = aFail.GetMessageString();
272   }
273   if (!aValue.IsNull())
274     return aValue->String();
275   else
276     return TCollection_AsciiString();
277 }
278
279 IMPLEMENT_STANDARD_RTTIEXT( IGESPlugin_ImportDriver, GEOM_BaseDriver )