]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_IInsertOperations.cxx
Salome HOME
Merging with WPdev
[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("Export driver failed");
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
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 Patterns for each supported format
274   int j = 1, len = theFormats->Length();
275   for (; j <= len; j++) {
276     TCollection_AsciiString aKey, aPattern;
277     aKey = theFormats->Value(j) + ".ImportPattern";
278     if (myResMgr->Find(aKey.ToCString()))
279       aPattern = myResMgr->Value(aKey.ToCString());
280     else {
281       aKey = theFormats->Value(j) + ".Pattern";
282       if (myResMgr->Find(aKey.ToCString()))
283         aPattern = myResMgr->Value(aKey.ToCString());
284       else {
285         aPattern = theFormats->Value(j);
286         aPattern += " Files ( *.* )";
287       }
288     }
289     thePatterns->Append(aPattern);
290   }
291
292   return (!theFormats->IsEmpty());
293 }
294
295 //=============================================================================
296 /*!
297  *  ExportTranslators
298  */
299 //=============================================================================
300 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
301                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
302                       Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
303 {
304   if (theFormats.IsNull())
305     theFormats = new TColStd_HSequenceOfAsciiString;
306   else
307     theFormats->Clear();
308
309   if (thePatterns.IsNull())
310     thePatterns = new TColStd_HSequenceOfAsciiString;
311   else
312     thePatterns->Clear();
313
314   if (!InitResMgr()) return Standard_False;
315
316   // Read Export formats list
317   if (myResMgr->Find("Export")) {
318     TCollection_AsciiString aFormats (myResMgr->Value("Export"));
319     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
320     int i = 1;
321     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
322       theFormats->Append(aToken);
323     }
324   }
325
326   // Read Patterns for each supported format
327   int j = 1, len = theFormats->Length();
328   for (; j <= len; j++) {
329     TCollection_AsciiString aKey, aPattern;
330     aKey = theFormats->Value(j) + ".ExportPattern";
331     if (myResMgr->Find(aKey.ToCString()))
332       aPattern = myResMgr->Value(aKey.ToCString());
333     else {
334       aKey = theFormats->Value(j) + ".Pattern";
335       if (myResMgr->Find(aKey.ToCString()))
336         aPattern = myResMgr->Value(aKey.ToCString());
337       else {
338         aPattern = theFormats->Value(j);
339         aPattern += " Files ( *.* )";
340       }
341     }
342     thePatterns->Append(aPattern);
343   }
344
345   return (!theFormats->IsEmpty());
346 }
347
348 //=============================================================================
349 /*!
350  *  IsSupported
351  */
352 //=============================================================================
353 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
354                             (const Standard_Boolean isImport,
355                              const TCollection_AsciiString& theFormat,
356                              Handle(TCollection_HAsciiString)& theLibName)
357 {
358   if (!InitResMgr()) return Standard_False;
359
360   // Import/Export mode
361   TCollection_AsciiString aMode;
362   //Standard_CString aMode;
363   if (isImport) aMode = "Import";
364   else aMode = "Export";
365
366   // Read supported formats for the certain mode
367   if (myResMgr->Find(aMode.ToCString())) {
368     TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
369     if (aFormats.Search(theFormat) > -1) {
370       // Read library name for the supported format
371       TCollection_AsciiString aKey (theFormat);
372       aKey += ".";
373       aKey += aMode;
374       if (myResMgr->Find(aKey.ToCString())) {
375         TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));        
376 #ifndef WNT
377         aLibName += ".so";     
378 #else
379         aLibName += ".dll";
380 #endif
381         theLibName = new TCollection_HAsciiString (aLibName);
382         return Standard_True;
383       }
384     }
385   }
386
387   return Standard_False;
388 }
389
390 //=============================================================================
391 /*!
392  *  InitResMgr
393  */
394 //=============================================================================
395 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
396 {
397   if (myResMgr.IsNull()) {
398     // Initialize the Resource Manager
399     TCollection_AsciiString aResDir (getenv("GEOM_ROOT_DIR"));
400 #ifdef WNT
401     aResDir += "\\share\\salome\\resources\\geom";
402 #else
403     aResDir += "/share/salome/resources/geom";
404 #endif
405     char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
406     TCollection_AsciiString aUserResDir;
407     if ( dir )
408     {
409       aUserResDir = dir;
410     }
411     else
412     {
413       aUserResDir = getenv("HOME");
414 #ifdef WNT
415       aUserResDir += "\\.salome\\resources";
416 #else
417       aUserResDir += "/.salome/resources";
418 #endif
419     }
420     myResMgr = new Resource_Manager ("ImportExport", aResDir, aUserResDir, Standard_False);
421
422     if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
423       // instead of complains in Resource_Manager
424       INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString() <<
425             " and in " << aUserResDir.ToCString() );
426     }
427   }
428
429   return ( myResMgr->Find("Import") || myResMgr->Find("Export") );
430 }