Salome HOME
ENV: Windows porting.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IInsertOperations.cxx
1 #include <Standard_Stream.hxx>
2
3 #include <GEOMImpl_IInsertOperations.hxx>
4
5 #include "utilities.h"
6 #include <OpUtil.hxx>
7 #include <Utils_ExceptHandlers.hxx>
8
9 #include <TFunction_DriverTable.hxx>
10 #include <TFunction_Driver.hxx>
11 #include <TFunction_Logbook.hxx>
12 #include <TDF_Tool.hxx>
13
14 #include <GEOM_Function.hxx>
15 #include <GEOM_PythonDump.hxx>
16
17 #include <GEOMImpl_CopyDriver.hxx>
18 #include <GEOMImpl_ExportDriver.hxx>
19 #include <GEOMImpl_ImportDriver.hxx>
20
21 #include <GEOMImpl_ICopy.hxx>
22 #include <GEOMImpl_IImportExport.hxx>
23
24 #include <GEOMImpl_Types.hxx>
25
26 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
27
28 //=============================================================================
29 /*!
30  *   constructor:
31  */
32 //=============================================================================
33
34 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
35 : GEOM_IOperations(theEngine, theDocID)
36 {
37   MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
38 }
39
40 //=============================================================================
41 /*!
42  *  destructor
43  */
44 //=============================================================================
45
46 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
47 {
48   MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
49 }
50
51
52
53 //=============================================================================
54 /*!
55  *  MakeCopy
56  */
57 //=============================================================================
58 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) theOriginal)
59 {
60   SetErrorCode(KO);
61
62   if (theOriginal.IsNull()) return NULL;
63
64   //Add a new Copy object
65   Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
66
67   //Add a Copy function for creation a copy object
68   Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
69
70   //Check if the function is set correctly
71   if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
72
73   GEOMImpl_ICopy aCI(aFunction);
74
75   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
76   if (aRefFunction.IsNull()) return NULL;
77
78   aCI.SetOriginal(aRefFunction);
79
80   //Compute the Copy value
81   try {
82     if (!GetSolver()->ComputeFunction(aFunction)) {
83       SetErrorCode("Copy driver failed");
84       return NULL;
85     }
86   }
87   catch (Standard_Failure) {
88     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
89     SetErrorCode(aFail->GetMessageString());
90     return NULL;
91   }
92
93   //Make a Python command
94   GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
95
96   SetErrorCode(OK);
97   return aCopy;
98 }
99
100 //=============================================================================
101 /*!
102  *  Export
103  */
104 //=============================================================================
105 void GEOMImpl_IInsertOperations::Export
106                      (const Handle(GEOM_Object) theOriginal,
107                       const char*               theFileName,
108                       const char*               theFormatName)
109 {
110   SetErrorCode(KO);
111
112   if (theOriginal.IsNull()) return;
113
114   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
115   if (aRefFunction.IsNull()) return;  //There is no function which creates an object to be exported
116
117   //Add an Export function
118   Handle(GEOM_Function) aFunction = theOriginal->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
119   if (aFunction.IsNull()) return;
120
121   //Check if the function is set correctly
122   if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
123
124   //Set parameters
125   GEOMImpl_IImportExport aCI (aFunction);
126   aCI.SetOriginal(aRefFunction);
127   char* aFileName = (char*)theFileName;
128   aCI.SetFileName(aFileName);
129
130   char* aFormatName = (char*)theFormatName;
131   Handle(TCollection_HAsciiString) aHLibName;
132   if (!IsSupported(Standard_False, aFormatName, aHLibName)) {
133     return;
134   }
135   TCollection_AsciiString aLibName = aHLibName->String();
136   aCI.SetPluginName(aLibName);
137
138   //Perform the Export
139   try {
140     if (!GetSolver()->ComputeFunction(aFunction)) {
141       SetErrorCode("Export driver failed");
142       return;
143     }
144   }
145   catch (Standard_Failure) {
146     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
147     SetErrorCode(aFail->GetMessageString());
148     return;
149   }
150
151   //Make a Python command
152   GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal
153     << ", \"" << theFileName << "\", \"" << theFormatName << "\")";
154
155   SetErrorCode(OK);
156 }
157
158 //=============================================================================
159 /*!
160  *  Import
161  */
162 //=============================================================================
163 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
164                      (const char* theFileName,
165                       const char* theFormatName)
166 {
167   SetErrorCode(KO);
168
169   if (!theFileName || !theFormatName) return NULL;
170
171   //Add a new result object
172   Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
173
174   //Add an Import function
175   Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
176   if (aFunction.IsNull()) return result;
177
178   //Check if the function is set correctly
179   if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
180
181   //Set parameters
182   GEOMImpl_IImportExport aCI (aFunction);
183   char* aFileName = (char*)theFileName;
184   aCI.SetFileName(aFileName);
185
186   char* aFormatName = (char*)theFormatName;
187   Handle(TCollection_HAsciiString) aHLibName;
188   if (!IsSupported(Standard_True, aFormatName, aHLibName)) {
189     return result;
190   }
191   TCollection_AsciiString aLibName = aHLibName->String();
192   aCI.SetPluginName(aLibName);
193
194   //Perform the Import
195   try {
196     if (!GetSolver()->ComputeFunction(aFunction)) {
197       SetErrorCode("Import driver failed");
198       return NULL;
199     }
200   }
201   catch (Standard_Failure) {
202     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
203     SetErrorCode(aFail->GetMessageString());
204     return NULL;
205   }
206
207   //Make a Python command
208   GEOM::TPythonDump(aFunction) << result << " = geompy.Import(\""
209     << theFileName << "\", \"" << theFormatName << "\")";
210
211   SetErrorCode(OK);
212   return result;
213 }
214
215 //=============================================================================
216 /*!
217  *  ImportTranslators
218  */
219 //=============================================================================
220 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
221                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
222                       Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
223 {
224   if (theFormats.IsNull())
225     theFormats = new TColStd_HSequenceOfAsciiString;
226   else
227     theFormats->Clear();
228
229   if (thePatterns.IsNull())
230     thePatterns = new TColStd_HSequenceOfAsciiString;
231   else
232     thePatterns->Clear();
233
234   if (!InitResMgr()) return Standard_False;
235
236   // Read Import formats list
237   if (myResMgr->Find("Import")) {
238     TCollection_AsciiString aFormats (myResMgr->Value("Import"));
239     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
240     int i = 1;
241     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
242       theFormats->Append(aToken);
243     }
244   }
245
246   // Read Patterns for each supported format
247   int j = 1, len = theFormats->Length();
248   for (; j <= len; j++) {
249     TCollection_AsciiString aPattern;
250     TCollection_AsciiString aKey (theFormats->Value(j));
251     aKey += ".Pattern";
252     if (myResMgr->Find(aKey.ToCString()))
253       aPattern = myResMgr->Value(aKey.ToCString());
254     else {
255       aPattern = theFormats->Value(j);
256       aPattern += " Files ( *.* )";
257     }
258     thePatterns->Append(aPattern);
259   }
260
261   return (!theFormats->IsEmpty());
262 }
263
264 //=============================================================================
265 /*!
266  *  ExportTranslators
267  */
268 //=============================================================================
269 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
270                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
271                       Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
272 {
273   if (theFormats.IsNull())
274     theFormats = new TColStd_HSequenceOfAsciiString;
275   else
276     theFormats->Clear();
277
278   if (thePatterns.IsNull())
279     thePatterns = new TColStd_HSequenceOfAsciiString;
280   else
281     thePatterns->Clear();
282
283   if (!InitResMgr()) return Standard_False;
284
285   // Read Export formats list
286   if (myResMgr->Find("Export")) {
287     TCollection_AsciiString aFormats (myResMgr->Value("Export"));
288     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
289     int i = 1;
290     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
291       theFormats->Append(aToken);
292     }
293   }
294
295   // Read Patterns for each supported format
296   int j = 1, len = theFormats->Length();
297   for (; j <= len; j++) {
298     TCollection_AsciiString aPattern;
299     TCollection_AsciiString aKey (theFormats->Value(j));
300     aKey += ".Pattern";
301     if (myResMgr->Find(aKey.ToCString()))
302       aPattern = myResMgr->Value(aKey.ToCString());
303     else {
304       aPattern = theFormats->Value(j);
305       aPattern += " Files ( *.* )";
306     }
307     thePatterns->Append(aPattern);
308   }
309
310   return (!theFormats->IsEmpty());
311 }
312
313 //=============================================================================
314 /*!
315  *  IsSupported
316  */
317 //=============================================================================
318 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
319                             (const Standard_Boolean isImport,
320                              const TCollection_AsciiString theFormat,
321                              Handle(TCollection_HAsciiString)& theLibName)
322 {
323   if (!InitResMgr()) return Standard_False;
324
325   // Import/Export mode
326   Standard_CString aMode;
327   if (isImport) aMode = "Import";
328   else aMode = "Export";
329
330   // Read supported formats for the certain mode
331   if (myResMgr->Find(aMode)) {
332     TCollection_AsciiString aFormats (myResMgr->Value(aMode));
333     if (aFormats.Search(theFormat) > -1) {
334       // Read library name for the supported format
335       TCollection_AsciiString aKey (theFormat);
336       aKey += ".";
337       aKey += aMode;
338       if (myResMgr->Find(aKey.ToCString())) {
339         TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
340         theLibName = new TCollection_HAsciiString (aLibName);
341         return Standard_True;
342       }
343     }
344   }
345
346   return Standard_False;
347 }
348
349 //=============================================================================
350 /*!
351  *  InitResMgr
352  */
353 //=============================================================================
354 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
355 {
356   if (myResMgr.IsNull()) {
357     // Initialize the Resource Manager
358     TCollection_AsciiString aResDir (getenv("GEOM_ROOT_DIR"));
359 #ifdef WNT
360     aResDir += "\\share\\salome\\resources";
361 #else
362     aResDir += "/share/salome/resources";
363 #endif
364     TCollection_AsciiString aUserResDir (getenv("HOME"));
365 #ifdef WNT
366     aUserResDir += "\\.salome\\resources";
367 #else
368     aUserResDir += "/.salome/resources";
369 #endif
370     myResMgr = new Resource_Manager ("ImportExport", aResDir, aUserResDir, Standard_False);
371
372     if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
373       // instead of complains in Resource_Manager
374       INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString() <<
375             " and in " << aUserResDir.ToCString() );
376     }
377   }
378
379   return ( myResMgr->Find("Import") || myResMgr->Find("Export") );
380 }