]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_IInsertOperations.cxx
Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[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_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   Handle(TCollection_HAsciiString) aHLibName;
144   if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
145     return;
146   }
147   TCollection_AsciiString aLibName = aHLibName->String();
148
149   //Set parameters
150   GEOMImpl_IImportExport aCI (aFunction);
151   aCI.SetOriginal(aRefFunction);
152   aCI.SetFileName(theFileName);
153   aCI.SetFormatName(theFormatName);
154   aCI.SetPluginName(aLibName);
155
156   //Perform the Export
157   try {
158     if (!GetSolver()->ComputeFunction(aFunction)) {
159       SetErrorCode("Export driver failed");
160       return;
161     }
162   }
163   catch (Standard_Failure) {
164     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
165     SetErrorCode(aFail->GetMessageString());
166     return;
167   }
168
169   //Make a Python command
170   GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
171     << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
172
173   SetErrorCode(OK);
174 }
175
176 //=============================================================================
177 /*!
178  *  Import
179  */
180 //=============================================================================
181 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
182                                  (const TCollection_AsciiString& theFileName,
183                                   const TCollection_AsciiString& theFormatName)
184 {
185   SetErrorCode(KO);
186
187   if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
188
189   //Add a new result object
190   Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
191
192   //Add an Import function
193   Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
194   if (aFunction.IsNull()) return result;
195
196   //Check if the function is set correctly
197   if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
198
199   Handle(TCollection_HAsciiString) aHLibName;
200   if (!IsSupported(Standard_True, theFormatName, aHLibName)) {
201     return result;
202   }
203   TCollection_AsciiString aLibName = aHLibName->String();
204
205   //Set parameters
206   GEOMImpl_IImportExport aCI (aFunction);
207   aCI.SetFileName(theFileName);
208   aCI.SetFormatName(theFormatName);
209   aCI.SetPluginName(aLibName);
210
211   //Perform the Import
212   try {
213     if (!GetSolver()->ComputeFunction(aFunction)) {
214       SetErrorCode("Import driver failed");
215       return NULL;
216     }
217   }
218   catch (Standard_Failure) {
219     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
220     SetErrorCode(aFail->GetMessageString());
221     return NULL;
222   }
223
224   //Make a Python command
225   GEOM::TPythonDump(aFunction) << result << " = geompy.Import(\""
226     << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
227
228   SetErrorCode(OK);
229   return result;
230 }
231
232 //=============================================================================
233 /*!
234  *  ImportTranslators
235  */
236 //=============================================================================
237 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
238                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
239                       Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
240 {
241   if (theFormats.IsNull())
242     theFormats = new TColStd_HSequenceOfAsciiString;
243   else
244     theFormats->Clear();
245
246   if (thePatterns.IsNull())
247     thePatterns = new TColStd_HSequenceOfAsciiString;
248   else
249     thePatterns->Clear();
250
251   if (!InitResMgr()) return Standard_False;
252
253   // Read Import formats list
254   if (myResMgr->Find("Import")) {
255     TCollection_AsciiString aFormats (myResMgr->Value("Import"));
256     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
257     int i = 1;
258     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
259       theFormats->Append(aToken);
260     }
261   }
262
263   // Read Patterns for each supported format
264   int j = 1, len = theFormats->Length();
265   for (; j <= len; j++) {
266     TCollection_AsciiString aKey, aPattern;
267     aKey = theFormats->Value(j) + ".ImportPattern";
268     if (myResMgr->Find(aKey.ToCString()))
269       aPattern = myResMgr->Value(aKey.ToCString());
270     else {
271       aKey = theFormats->Value(j) + ".Pattern";
272       if (myResMgr->Find(aKey.ToCString()))
273         aPattern = myResMgr->Value(aKey.ToCString());
274       else {
275         aPattern = theFormats->Value(j);
276         aPattern += " Files ( *.* )";
277       }
278     }
279     thePatterns->Append(aPattern);
280   }
281
282   return (!theFormats->IsEmpty());
283 }
284
285 //=============================================================================
286 /*!
287  *  ExportTranslators
288  */
289 //=============================================================================
290 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
291                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
292                       Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
293 {
294   if (theFormats.IsNull())
295     theFormats = new TColStd_HSequenceOfAsciiString;
296   else
297     theFormats->Clear();
298
299   if (thePatterns.IsNull())
300     thePatterns = new TColStd_HSequenceOfAsciiString;
301   else
302     thePatterns->Clear();
303
304   if (!InitResMgr()) return Standard_False;
305
306   // Read Export formats list
307   if (myResMgr->Find("Export")) {
308     TCollection_AsciiString aFormats (myResMgr->Value("Export"));
309     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
310     int i = 1;
311     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
312       theFormats->Append(aToken);
313     }
314   }
315
316   // Read Patterns for each supported format
317   int j = 1, len = theFormats->Length();
318   for (; j <= len; j++) {
319     TCollection_AsciiString aKey, aPattern;
320     aKey = theFormats->Value(j) + ".ExportPattern";
321     if (myResMgr->Find(aKey.ToCString()))
322       aPattern = myResMgr->Value(aKey.ToCString());
323     else {
324       aKey = theFormats->Value(j) + ".Pattern";
325       if (myResMgr->Find(aKey.ToCString()))
326         aPattern = myResMgr->Value(aKey.ToCString());
327       else {
328         aPattern = theFormats->Value(j);
329         aPattern += " Files ( *.* )";
330       }
331     }
332     thePatterns->Append(aPattern);
333   }
334
335   return (!theFormats->IsEmpty());
336 }
337
338 //=============================================================================
339 /*!
340  *  IsSupported
341  */
342 //=============================================================================
343 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
344                             (const Standard_Boolean isImport,
345                              const TCollection_AsciiString& theFormat,
346                              Handle(TCollection_HAsciiString)& theLibName)
347 {
348   if (!InitResMgr()) return Standard_False;
349
350   // Import/Export mode
351   TCollection_AsciiString aMode;
352   //Standard_CString aMode;
353   if (isImport) aMode = "Import";
354   else aMode = "Export";
355
356   // Read supported formats for the certain mode
357   if (myResMgr->Find(aMode.ToCString())) {
358     TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
359     if (aFormats.Search(theFormat) > -1) {
360       // Read library name for the supported format
361       TCollection_AsciiString aKey (theFormat);
362       aKey += ".";
363       aKey += aMode;
364       if (myResMgr->Find(aKey.ToCString())) {
365         TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
366         theLibName = new TCollection_HAsciiString (aLibName);
367         return Standard_True;
368       }
369     }
370   }
371
372   return Standard_False;
373 }
374
375 //=============================================================================
376 /*!
377  *  InitResMgr
378  */
379 //=============================================================================
380 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
381 {
382   if (myResMgr.IsNull()) {
383     // Initialize the Resource Manager
384     TCollection_AsciiString aResDir (getenv("GEOM_ROOT_DIR"));
385 #ifdef WNT
386     aResDir += "\\share\\salome\\resources";
387 #else
388     aResDir += "/share/salome/resources";
389 #endif
390     char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
391     TCollection_AsciiString aUserResDir;
392     if ( dir )
393     {
394       aUserResDir = dir;
395     }
396     else
397     {
398       aUserResDir = getenv("HOME");
399 #ifdef WNT
400       aUserResDir += "\\.salome\\resources";
401 #else
402       aUserResDir += "/.salome/resources";
403 #endif
404     }
405     myResMgr = new Resource_Manager ("ImportExport", aResDir, aUserResDir, Standard_False);
406
407     if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
408       // instead of complains in Resource_Manager
409       INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString() <<
410             " and in " << aUserResDir.ToCString() );
411     }
412   }
413
414   return ( myResMgr->Find("Import") || myResMgr->Find("Export") );
415 }