Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IInsertOperations.cxx
1 using namespace std;
2
3 #include "GEOMImpl_IInsertOperations.hxx"
4
5 #include "utilities.h"
6 #include "OpUtil.hxx"
7 #include "Utils_ExceptHandlers.hxx"
8
9 #include <TFunction_DriverTable.hxx>
10 #include <TFunction_Driver.hxx>
11 #include <TFunction_Logbook.hxx>
12 #include <TDF_Tool.hxx>
13
14 #include "GEOM_Function.hxx"
15
16 #include "GEOMImpl_CopyDriver.hxx"
17 #include "GEOMImpl_ExportDriver.hxx"
18 #include "GEOMImpl_ImportDriver.hxx"
19
20 #include "GEOMImpl_ICopy.hxx"
21 #include "GEOMImpl_IImportExport.hxx"
22
23 #include "GEOMImpl_Types.hxx"
24
25 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
26
27 //=============================================================================
28 /*!
29  *   constructor:
30  */
31 //=============================================================================
32
33 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
34 : GEOM_IOperations(theEngine, theDocID)
35 {
36   MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
37 }
38
39 //=============================================================================
40 /*!
41  *  destructor
42  */
43 //=============================================================================
44
45 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
46 {
47   MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
48 }
49
50
51
52 //=============================================================================
53 /*!
54  *  MakeCopy
55  */
56 //=============================================================================
57 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) theOriginal)
58 {
59   SetErrorCode(KO);
60
61   if (theOriginal.IsNull()) return NULL;
62
63   //Add a new Copy object
64   Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
65
66   //Add a Copy function for creation a copy object
67   Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
68
69   //Check if the function is set correctly
70   if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
71
72   GEOMImpl_ICopy aCI(aFunction);
73
74   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
75   if (aRefFunction.IsNull()) return NULL;
76
77   aCI.SetOriginal(aRefFunction);
78
79   //Compute the Copy value
80   try {
81     if (!GetSolver()->ComputeFunction(aFunction)) {
82       SetErrorCode("Copy driver failed");
83       return NULL;
84     }
85   }
86   catch (Standard_Failure) {
87     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
88     SetErrorCode(aFail->GetMessageString());
89     return NULL;
90   }
91
92   //Make a Python command
93   TCollection_AsciiString anEntry, aDescr;
94   TDF_Tool::Entry(aCopy->GetEntry(), anEntry);
95   aDescr += (anEntry+" = IInsertOperations.MakeCopy(");
96   TDF_Tool::Entry(theOriginal->GetEntry(), anEntry);
97   aDescr += (anEntry+")");
98
99   aFunction->SetDescription(aDescr);
100
101   SetErrorCode(OK);
102   return aCopy;
103 }
104
105 //=============================================================================
106 /*!
107  *  Export
108  */
109 //=============================================================================
110 void GEOMImpl_IInsertOperations::Export
111                      (const Handle(GEOM_Object) theOriginal,
112                       const char*               theFileName,
113                       const char*               theFormatName)
114 {
115   SetErrorCode(KO);
116
117   if (theOriginal.IsNull()) return;
118
119   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
120   if (aRefFunction.IsNull()) return;  //There is no function which creates an object to be exported
121
122   //Add an Export function
123   Handle(GEOM_Function) aFunction = theOriginal->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
124   if (aFunction.IsNull()) return;
125
126   //Check if the function is set correctly
127   if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
128
129   //Set parameters
130   GEOMImpl_IImportExport aCI (aFunction);
131   aCI.SetOriginal(aRefFunction);
132   char* aFileName = (char*)theFileName;
133   aCI.SetFileName(aFileName);
134
135   char* aFormatName = (char*)theFormatName;
136   Handle(TCollection_HAsciiString) aHLibName;
137   if (!IsSupported(Standard_False, aFormatName, aHLibName)) {
138     return;
139   }
140   TCollection_AsciiString aLibName = aHLibName->String();
141   aCI.SetPluginName(aLibName);
142
143   //Perform the Export
144   try {
145     if (!GetSolver()->ComputeFunction(aFunction)) {
146       SetErrorCode("Export driver failed");
147       return;
148     }
149   }
150   catch (Standard_Failure) {
151     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
152     SetErrorCode(aFail->GetMessageString());
153     return;
154   }
155
156   //Make a Python command
157   TCollection_AsciiString anEntry, aDescr;
158   aDescr = "IInsertOperations.Export(";
159   TDF_Tool::Entry(theOriginal->GetEntry(), anEntry);
160   aDescr += (anEntry + ", ");
161   aDescr += (TCollection_AsciiString(aFileName) + ", ");
162   aDescr += (TCollection_AsciiString(aFormatName) + ")");
163
164   aFunction->SetDescription(aDescr);
165
166   SetErrorCode(OK);
167 }
168
169 //=============================================================================
170 /*!
171  *  Import
172  */
173 //=============================================================================
174 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
175                      (const char* theFileName,
176                       const char* theFormatName)
177 {
178   SetErrorCode(KO);
179
180   if (!theFileName || !theFormatName) return NULL;
181
182   //Add a new result object
183   Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
184
185   //Add an Import function
186   Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
187   if (aFunction.IsNull()) return result;
188
189   //Check if the function is set correctly
190   if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
191
192   //Set parameters
193   GEOMImpl_IImportExport aCI (aFunction);
194   char* aFileName = (char*)theFileName;
195   aCI.SetFileName(aFileName);
196
197   char* aFormatName = (char*)theFormatName;
198   Handle(TCollection_HAsciiString) aHLibName;
199   if (!IsSupported(Standard_True, aFormatName, aHLibName)) {
200     return result;
201   }
202   TCollection_AsciiString aLibName = aHLibName->String();
203   aCI.SetPluginName(aLibName);
204
205   //Perform the Import
206   try {
207     if (!GetSolver()->ComputeFunction(aFunction)) {
208       SetErrorCode("Import driver failed");
209       return NULL;
210     }
211   }
212   catch (Standard_Failure) {
213     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
214     SetErrorCode(aFail->GetMessageString());
215     return NULL;
216   }
217
218   //Make a Python command
219   TCollection_AsciiString anEntry, aDescr;
220   TDF_Tool::Entry(result->GetEntry(), anEntry);
221   aDescr += (anEntry + " = IInsertOperations.Import(");
222   aDescr += (TCollection_AsciiString(aFileName) + ", ");
223   aDescr += (TCollection_AsciiString(aFormatName) + ")");
224
225   aFunction->SetDescription(aDescr);
226
227   SetErrorCode(OK);
228   return result;
229 }
230
231 //=============================================================================
232 /*!
233  *  ImportTranslators
234  */
235 //=============================================================================
236 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
237                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
238                       Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
239 {
240   if (theFormats.IsNull())
241     theFormats = new TColStd_HSequenceOfAsciiString;
242   else
243     theFormats->Clear();
244
245   if (thePatterns.IsNull())
246     thePatterns = new TColStd_HSequenceOfAsciiString;
247   else
248     thePatterns->Clear();
249
250   if (!InitResMgr()) return Standard_False;
251
252   // Read Import formats list
253   if (myResMgr->Find("Import")) {
254     TCollection_AsciiString aFormats (myResMgr->Value("Import"));
255     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
256     int i = 1;
257     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
258       theFormats->Append(aToken);
259     }
260   }
261
262   // Read Patterns for each supported format
263   int j = 1, len = theFormats->Length();
264   for (; j <= len; j++) {
265     TCollection_AsciiString aPattern;
266     TCollection_AsciiString aKey (theFormats->Value(j));
267     aKey += ".Pattern";
268     if (myResMgr->Find(aKey.ToCString()))
269       aPattern = myResMgr->Value(aKey.ToCString());
270     else {
271       aPattern = theFormats->Value(j);
272       aPattern += " Files ( *.* )";
273     }
274     thePatterns->Append(aPattern);
275   }
276
277   return (!theFormats->IsEmpty());
278 }
279
280 //=============================================================================
281 /*!
282  *  ExportTranslators
283  */
284 //=============================================================================
285 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
286                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
287                       Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
288 {
289   if (theFormats.IsNull())
290     theFormats = new TColStd_HSequenceOfAsciiString;
291   else
292     theFormats->Clear();
293
294   if (thePatterns.IsNull())
295     thePatterns = new TColStd_HSequenceOfAsciiString;
296   else
297     thePatterns->Clear();
298
299   if (!InitResMgr()) return Standard_False;
300
301   // Read Export formats list
302   if (myResMgr->Find("Export")) {
303     TCollection_AsciiString aFormats (myResMgr->Value("Export"));
304     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
305     int i = 1;
306     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
307       theFormats->Append(aToken);
308     }
309   }
310
311   // Read Patterns for each supported format
312   int j = 1, len = theFormats->Length();
313   for (; j <= len; j++) {
314     TCollection_AsciiString aPattern;
315     TCollection_AsciiString aKey (theFormats->Value(j));
316     aKey += ".Pattern";
317     if (myResMgr->Find(aKey.ToCString()))
318       aPattern = myResMgr->Value(aKey.ToCString());
319     else {
320       aPattern = theFormats->Value(j);
321       aPattern += " Files ( *.* )";
322     }
323     thePatterns->Append(aPattern);
324   }
325
326   return (!theFormats->IsEmpty());
327 }
328
329 //=============================================================================
330 /*!
331  *  IsSupported
332  */
333 //=============================================================================
334 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
335                             (const Standard_Boolean isImport,
336                              const TCollection_AsciiString theFormat,
337                              Handle(TCollection_HAsciiString)& theLibName)
338 {
339   if (!InitResMgr()) return Standard_False;
340
341   // Import/Export mode
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)) {
348     TCollection_AsciiString aFormats (myResMgr->Value(aMode));
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     TCollection_AsciiString aUserResDir (getenv("HOME"));
381 #ifdef WNT
382     aUserResDir += "\\.salome\\resources";
383 #else
384     aUserResDir += "/.salome/resources";
385 #endif
386     myResMgr = new Resource_Manager ("ImportExport", aResDir, aUserResDir, Standard_True);
387   }
388
389   if (myResMgr->Find("Import") || myResMgr->Find("Export"))
390     return Standard_True;
391
392   return Standard_False;
393 }