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