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