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