Salome HOME
merge from V4_1_0_maintainance branch (from tag mergeto_BR_QT4_Dev_29Jul08)
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IInsertOperations.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #include <Standard_Stream.hxx>
21
22 #include <GEOMImpl_IInsertOperations.hxx>
23
24 #include "utilities.h"
25 #include <OpUtil.hxx>
26 #include <Utils_ExceptHandlers.hxx>
27
28 #include <TFunction_DriverTable.hxx>
29 #include <TFunction_Driver.hxx>
30 #include <TFunction_Logbook.hxx>
31 #include <TDF_Tool.hxx>
32
33 #include <GEOM_Function.hxx>
34 #include <GEOM_PythonDump.hxx>
35
36 #include <GEOMImpl_CopyDriver.hxx>
37 #include <GEOMImpl_ExportDriver.hxx>
38 #include <GEOMImpl_ImportDriver.hxx>
39
40 #include <GEOMImpl_ICopy.hxx>
41 #include <GEOMImpl_IImportExport.hxx>
42
43 #include <GEOMImpl_Types.hxx>
44
45 #include <Standard_Failure.hxx>
46 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
47
48 //=============================================================================
49 /*!
50  *   constructor:
51  */
52 //=============================================================================
53
54 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
55 : GEOM_IOperations(theEngine, theDocID)
56 {
57   MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
58 }
59
60 //=============================================================================
61 /*!
62  *  destructor
63  */
64 //=============================================================================
65
66 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
67 {
68   MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
69 }
70
71
72
73 //=============================================================================
74 /*!
75  *  MakeCopy
76  */
77 //=============================================================================
78 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) theOriginal)
79 {
80   SetErrorCode(KO);
81
82   if (theOriginal.IsNull()) return NULL;
83
84   //Add a new Copy object
85   Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
86
87   //Add a Copy function for creation a copy object
88   Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
89
90   //Check if the function is set correctly
91   if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
92
93   GEOMImpl_ICopy aCI(aFunction);
94
95   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
96   if (aRefFunction.IsNull()) return NULL;
97
98   aCI.SetOriginal(aRefFunction);
99
100   //Compute the Copy value
101   try {
102 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
103     OCC_CATCH_SIGNALS;
104 #endif
105     if (!GetSolver()->ComputeFunction(aFunction)) {
106       SetErrorCode("Copy driver failed");
107       return NULL;
108     }
109   }
110   catch (Standard_Failure) {
111     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
112     SetErrorCode(aFail->GetMessageString());
113     return NULL;
114   }
115
116   //Make a Python command
117   GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
118
119   SetErrorCode(OK);
120   return aCopy;
121 }
122
123 //=============================================================================
124 /*!
125  *  Export
126  */
127 //=============================================================================
128 void GEOMImpl_IInsertOperations::Export
129                      (const Handle(GEOM_Object)      theOriginal,
130                       const TCollection_AsciiString& theFileName,
131                       const TCollection_AsciiString& theFormatName)
132 {
133   SetErrorCode(KO);
134
135   if (theOriginal.IsNull()) return;
136
137   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
138   if (aRefFunction.IsNull()) return;  //There is no function which creates an object to be exported
139
140   //Add an Export function
141   Handle(GEOM_Function) aFunction = theOriginal->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
142   if (aFunction.IsNull()) return;
143
144   //Check if the function is set correctly
145   if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
146
147   Handle(TCollection_HAsciiString) aHLibName;
148   if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
149     return;
150   }
151   TCollection_AsciiString aLibName = aHLibName->String();
152
153   //Set parameters
154   GEOMImpl_IImportExport aCI (aFunction);
155   aCI.SetOriginal(aRefFunction);
156   aCI.SetFileName(theFileName);
157   aCI.SetFormatName(theFormatName);
158   aCI.SetPluginName(aLibName);
159
160   //Perform the Export
161   try {
162 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
163     OCC_CATCH_SIGNALS;
164 #endif
165     if (!GetSolver()->ComputeFunction(aFunction)) {
166       SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
167       return;
168     }
169   }
170   catch (Standard_Failure) {
171     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
172     SetErrorCode(aFail->GetMessageString());
173     return;
174   }
175
176   //Make a Python command
177   GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
178     << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
179
180   SetErrorCode(OK);
181 }
182
183 //=============================================================================
184 /*!
185  *  Import
186  */
187 //=============================================================================
188 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
189                                  (const TCollection_AsciiString& theFileName,
190                                   const TCollection_AsciiString& theFormatName)
191 {
192   SetErrorCode(KO);
193
194   if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
195
196   //Add a new result object
197   Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
198
199   //Add an Import function
200   Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
201   if (aFunction.IsNull()) return result;
202
203   //Check if the function is set correctly
204   if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
205
206   Handle(TCollection_HAsciiString) aHLibName;
207   if (!IsSupported(Standard_True, theFormatName, aHLibName)) {
208     return result;
209   }
210   TCollection_AsciiString aLibName = aHLibName->String();
211
212   //Set parameters
213   GEOMImpl_IImportExport aCI (aFunction);
214   aCI.SetFileName(theFileName);
215   aCI.SetFormatName(theFormatName);
216   aCI.SetPluginName(aLibName);
217
218   //Perform the Import
219   try {
220 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
221     OCC_CATCH_SIGNALS;
222 #endif
223     if (!GetSolver()->ComputeFunction(aFunction)) {
224       SetErrorCode("Import driver failed");
225       return NULL;
226     }
227   }
228   catch (Standard_Failure) {
229     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
230     SetErrorCode(aFail->GetMessageString());
231     return NULL;
232   }
233
234   //Make a Python command
235   GEOM::TPythonDump(aFunction) << result << " = geompy.Import(\""
236     << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
237
238   SetErrorCode(OK);
239   return result;
240 }
241
242 //=============================================================================
243 /*!
244  *  ImportTranslators
245  */
246 //=============================================================================
247 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
248                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
249                       Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
250 {
251   if (theFormats.IsNull())
252     theFormats = new TColStd_HSequenceOfAsciiString;
253   else
254     theFormats->Clear();
255
256   if (thePatterns.IsNull())
257     thePatterns = new TColStd_HSequenceOfAsciiString;
258   else
259     thePatterns->Clear();
260
261   if (!InitResMgr()) return Standard_False;
262
263   // Read Import formats list from install directory
264   if (myResMgr->Find("Import")) {
265     TCollection_AsciiString aFormats (myResMgr->Value("Import"));
266     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
267     int i = 1;
268     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
269       theFormats->Append(aToken);
270     }
271   }
272
273   // Read Import formats from user directory
274   if (myResMgrUser->Find("Import")) {
275     TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
276     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
277     int i = 1;
278     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
279       int aLenFormats = theFormats->Length();
280       bool isFound = false;
281       for(int aInd=1;aInd<=aLenFormats;aInd++){
282         if( theFormats->Value(aInd) == aToken){
283           isFound = true;
284           break;
285         }
286       }
287       if(!isFound)
288         theFormats->Append(aToken);
289     }
290   }
291
292   // Read Patterns for each supported format
293   int j = 1, len = theFormats->Length();
294   for (; j <= len; j++) {
295     TCollection_AsciiString aKey, aPattern;
296     aKey = theFormats->Value(j) + ".ImportPattern";
297     if (myResMgr->Find(aKey.ToCString()))
298       aPattern = myResMgr->Value(aKey.ToCString());
299     else if(myResMgrUser->Find(aKey.ToCString()))
300       aPattern = myResMgrUser->Value(aKey.ToCString());
301     else {
302       aKey = theFormats->Value(j) + ".Pattern";
303       if (myResMgr->Find(aKey.ToCString()))
304         aPattern = myResMgr->Value(aKey.ToCString());
305       else if(myResMgrUser->Find(aKey.ToCString()))
306         aPattern = myResMgrUser->Value(aKey.ToCString());
307       else {
308         aPattern = theFormats->Value(j);
309         aPattern += " Files ( *.* )";
310       }
311     }
312     thePatterns->Append(aPattern);
313   }
314
315   return (!theFormats->IsEmpty());
316 }
317
318 //=============================================================================
319 /*!
320  *  ExportTranslators
321  */
322 //=============================================================================
323 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
324                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
325                       Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
326 {
327   if (theFormats.IsNull())
328     theFormats = new TColStd_HSequenceOfAsciiString;
329   else
330     theFormats->Clear();
331
332   if (thePatterns.IsNull())
333     thePatterns = new TColStd_HSequenceOfAsciiString;
334   else
335     thePatterns->Clear();
336
337   if (!InitResMgr()) return Standard_False;
338
339   // Read Export formats list from install directory
340   if (myResMgr->Find("Export")) {
341     TCollection_AsciiString aFormats (myResMgr->Value("Export"));
342     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
343     int i = 1;
344     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
345       theFormats->Append(aToken);
346     }
347   }
348
349   // Read Export formats list from user directory
350   if (myResMgrUser->Find("Export")) {
351     TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
352     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
353     int i = 1;
354     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
355       int aLenFormats = theFormats->Length();
356       bool isFound = false;
357       for(int aInd=1;aInd<=aLenFormats;aInd++){
358         if( theFormats->Value(aInd) == aToken){
359           isFound = true;
360           break;
361         }
362       }
363       if(!isFound)
364         theFormats->Append(aToken);
365     }
366   }
367
368   // Read Patterns for each supported format
369   int j = 1, len = theFormats->Length();
370   for (; j <= len; j++) {
371     TCollection_AsciiString aKey, aPattern;
372     aKey = theFormats->Value(j) + ".ExportPattern";
373     if (myResMgr->Find(aKey.ToCString()))
374       aPattern = myResMgr->Value(aKey.ToCString());
375     else if (myResMgrUser->Find(aKey.ToCString()))
376       aPattern = myResMgrUser->Value(aKey.ToCString());
377     else {
378       aKey = theFormats->Value(j) + ".Pattern";
379       if (myResMgr->Find(aKey.ToCString()))
380         aPattern = myResMgr->Value(aKey.ToCString());
381       else if (myResMgrUser->Find(aKey.ToCString()))
382         aPattern = myResMgrUser->Value(aKey.ToCString());
383       else {
384         aPattern = theFormats->Value(j);
385         aPattern += " Files ( *.* )";
386       }
387     }
388     thePatterns->Append(aPattern);
389   }
390
391   return (!theFormats->IsEmpty());
392 }
393
394 //=============================================================================
395 /*!
396  *  IsSupported
397  */
398 //=============================================================================
399 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
400                             (const Standard_Boolean isImport,
401                              const TCollection_AsciiString& theFormat,
402                              Handle(TCollection_HAsciiString)& theLibName)
403 {
404   if (!InitResMgr()) return Standard_False;
405
406   // Import/Export mode
407   TCollection_AsciiString aMode;
408   //Standard_CString aMode;
409   if (isImport) aMode = "Import";
410   else aMode = "Export";
411
412   
413   // Read supported formats for the certain mode from install directory
414   if (myResMgr->Find(aMode.ToCString())) {
415     TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
416     if (aFormats.Search(theFormat) > -1) {
417       // Read library name for the supported format
418       TCollection_AsciiString aKey (theFormat);
419       aKey += ".";
420       aKey += aMode;
421       if (myResMgr->Find(aKey.ToCString())) {
422         TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));        
423 #ifndef WNT
424         aLibName += ".so";     
425 #else
426         aLibName += ".dll";
427 #endif
428         theLibName = new TCollection_HAsciiString (aLibName);
429         return Standard_True;
430       }
431     }
432   }
433   
434   // Read supported formats for the certain mode from user directory
435   if (myResMgrUser->Find(aMode.ToCString())) {
436     TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
437     if (aFormats.Search(theFormat) > -1) {
438       // Read library name for the supported format
439       TCollection_AsciiString aKey (theFormat);
440       aKey += ".";
441       aKey += aMode;
442       if (myResMgrUser->Find(aKey.ToCString())) {
443         TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
444         theLibName = new TCollection_HAsciiString (aLibName);
445         return Standard_True;
446       }
447     }
448   }
449   
450   return Standard_False;
451 }
452
453 //=============================================================================
454 /*!
455  *  InitResMgr
456  */
457 //=============================================================================
458 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
459 {
460   bool isResourceFound     = false;
461   bool isResourceFoundUser = false;
462   TCollection_AsciiString aUserResDir,aResDir;
463   
464   if (myResMgr.IsNull()) {
465     // Initialize the Resource Manager
466     TCollection_AsciiString aNull;
467     aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
468 #ifdef WNT
469     aResDir += "\\share\\salome\\resources\\geom";
470 #else
471     aResDir += "/share/salome/resources/geom";
472 #endif
473     
474     myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
475
476     isResourceFound = true;
477     if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
478       // instead of complains in Resource_Manager
479       isResourceFound = false;
480       INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
481     }
482   } else
483     isResourceFound = true;
484
485   if (myResMgrUser.IsNull()) {
486     char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
487     TCollection_AsciiString aNull;
488     if ( dir )
489     {
490       aUserResDir = dir;
491     }
492     else
493     {
494       aUserResDir = getenv("HOME");
495 #ifdef WNT
496       aUserResDir += "\\.salome\\resources";
497 #else
498       aUserResDir += "/.salome/resources";
499 #endif
500     }
501
502     myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
503
504     isResourceFoundUser = true;
505     
506     if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
507       // instead of complains in Resource_Manager
508       isResourceFoundUser = false;
509     }
510       
511   } else
512     isResourceFoundUser = true;
513     
514   if(!isResourceFound && !isResourceFoundUser){
515     INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
516     INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
517   }
518
519   return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
520            myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
521 }