1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include <Standard_Stream.hxx>
24 #include <GEOMImpl_IInsertOperations.hxx>
26 #include "utilities.h"
28 #include <Utils_ExceptHandlers.hxx>
30 #include <TFunction_DriverTable.hxx>
31 #include <TFunction_Driver.hxx>
32 #include <TFunction_Logbook.hxx>
33 #include <TDF_Tool.hxx>
35 #include <GEOM_Function.hxx>
36 #include <GEOM_PythonDump.hxx>
38 #include <GEOMImpl_CopyDriver.hxx>
39 #include <GEOMImpl_ExportDriver.hxx>
40 #include <GEOMImpl_ImportDriver.hxx>
42 #include <GEOMImpl_ICopy.hxx>
43 #include <GEOMImpl_IImportExport.hxx>
45 #include <GEOMImpl_Types.hxx>
47 #include <Standard_Failure.hxx>
48 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
50 //=============================================================================
54 //=============================================================================
56 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
57 : GEOM_IOperations(theEngine, theDocID)
59 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
62 //=============================================================================
66 //=============================================================================
68 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
70 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
75 //=============================================================================
79 //=============================================================================
80 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) theOriginal)
84 if (theOriginal.IsNull()) return NULL;
86 //Add a new Copy object
87 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
89 //Add a Copy function for creation a copy object
90 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
92 //Check if the function is set correctly
93 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
95 GEOMImpl_ICopy aCI(aFunction);
97 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
98 if (aRefFunction.IsNull()) return NULL;
100 aCI.SetOriginal(aRefFunction);
102 //Compute the Copy value
104 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
107 if (!GetSolver()->ComputeFunction(aFunction)) {
108 SetErrorCode("Copy driver failed");
112 catch (Standard_Failure) {
113 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
114 SetErrorCode(aFail->GetMessageString());
118 //Make a Python command
119 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
125 //=============================================================================
129 //=============================================================================
130 void GEOMImpl_IInsertOperations::Export
131 (const Handle(GEOM_Object) theOriginal,
132 const TCollection_AsciiString& theFileName,
133 const TCollection_AsciiString& theFormatName)
137 if (theOriginal.IsNull()) return;
139 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
140 if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
142 //Add an Export function
143 Handle(GEOM_Function) aFunction = theOriginal->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
144 if (aFunction.IsNull()) return;
146 //Check if the function is set correctly
147 if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
149 Handle(TCollection_HAsciiString) aHLibName;
150 if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
153 TCollection_AsciiString aLibName = aHLibName->String();
156 GEOMImpl_IImportExport aCI (aFunction);
157 aCI.SetOriginal(aRefFunction);
158 aCI.SetFileName(theFileName);
159 aCI.SetFormatName(theFormatName);
160 aCI.SetPluginName(aLibName);
164 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
167 if (!GetSolver()->ComputeFunction(aFunction)) {
168 SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
172 catch (Standard_Failure) {
173 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
174 SetErrorCode(aFail->GetMessageString());
178 //Make a Python command
179 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
180 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
185 //=============================================================================
189 //=============================================================================
190 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
191 (const TCollection_AsciiString& theFileName,
192 const TCollection_AsciiString& theFormatName)
196 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
198 //Add a new result object
199 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
201 //Add an Import function
202 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
203 if (aFunction.IsNull()) return result;
205 //Check if the function is set correctly
206 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
208 Handle(TCollection_HAsciiString) aHLibName;
209 if (!IsSupported(Standard_True, theFormatName, aHLibName)) {
212 TCollection_AsciiString aLibName = aHLibName->String();
215 GEOMImpl_IImportExport aCI (aFunction);
216 aCI.SetFileName(theFileName);
217 aCI.SetFormatName(theFormatName);
218 aCI.SetPluginName(aLibName);
222 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
225 if (!GetSolver()->ComputeFunction(aFunction)) {
226 SetErrorCode("Import driver failed");
230 catch (Standard_Failure) {
231 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
232 SetErrorCode(aFail->GetMessageString());
236 //Make a Python command
237 GEOM::TPythonDump(aFunction) << result << " = geompy.Import(\""
238 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
244 //=============================================================================
248 //=============================================================================
249 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
250 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
251 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
253 if (theFormats.IsNull())
254 theFormats = new TColStd_HSequenceOfAsciiString;
258 if (thePatterns.IsNull())
259 thePatterns = new TColStd_HSequenceOfAsciiString;
261 thePatterns->Clear();
263 if (!InitResMgr()) return Standard_False;
265 // Read Import formats list from install directory
266 if (myResMgr->Find("Import")) {
267 TCollection_AsciiString aFormats (myResMgr->Value("Import"));
268 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
270 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
271 theFormats->Append(aToken);
275 // Read Import formats from user directory
276 if (myResMgrUser->Find("Import")) {
277 TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
278 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
280 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
281 int aLenFormats = theFormats->Length();
282 bool isFound = false;
283 for(int aInd=1;aInd<=aLenFormats;aInd++){
284 if( theFormats->Value(aInd) == aToken){
290 theFormats->Append(aToken);
294 // Read Patterns for each supported format
295 int j = 1, len = theFormats->Length();
296 for (; j <= len; j++) {
297 TCollection_AsciiString aKey, aPattern;
298 aKey = theFormats->Value(j) + ".ImportPattern";
299 if (myResMgr->Find(aKey.ToCString()))
300 aPattern = myResMgr->Value(aKey.ToCString());
301 else if(myResMgrUser->Find(aKey.ToCString()))
302 aPattern = myResMgrUser->Value(aKey.ToCString());
304 aKey = theFormats->Value(j) + ".Pattern";
305 if (myResMgr->Find(aKey.ToCString()))
306 aPattern = myResMgr->Value(aKey.ToCString());
307 else if(myResMgrUser->Find(aKey.ToCString()))
308 aPattern = myResMgrUser->Value(aKey.ToCString());
310 aPattern = theFormats->Value(j);
311 aPattern += " Files ( *.* )";
314 thePatterns->Append(aPattern);
317 return (!theFormats->IsEmpty());
320 //=============================================================================
324 //=============================================================================
325 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
326 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
327 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
329 if (theFormats.IsNull())
330 theFormats = new TColStd_HSequenceOfAsciiString;
334 if (thePatterns.IsNull())
335 thePatterns = new TColStd_HSequenceOfAsciiString;
337 thePatterns->Clear();
339 if (!InitResMgr()) return Standard_False;
341 // Read Export formats list from install directory
342 if (myResMgr->Find("Export")) {
343 TCollection_AsciiString aFormats (myResMgr->Value("Export"));
344 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
346 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
347 theFormats->Append(aToken);
351 // Read Export formats list from user directory
352 if (myResMgrUser->Find("Export")) {
353 TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
354 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
356 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
357 int aLenFormats = theFormats->Length();
358 bool isFound = false;
359 for(int aInd=1;aInd<=aLenFormats;aInd++){
360 if( theFormats->Value(aInd) == aToken){
366 theFormats->Append(aToken);
370 // Read Patterns for each supported format
371 int j = 1, len = theFormats->Length();
372 for (; j <= len; j++) {
373 TCollection_AsciiString aKey, aPattern;
374 aKey = theFormats->Value(j) + ".ExportPattern";
375 if (myResMgr->Find(aKey.ToCString()))
376 aPattern = myResMgr->Value(aKey.ToCString());
377 else if (myResMgrUser->Find(aKey.ToCString()))
378 aPattern = myResMgrUser->Value(aKey.ToCString());
380 aKey = theFormats->Value(j) + ".Pattern";
381 if (myResMgr->Find(aKey.ToCString()))
382 aPattern = myResMgr->Value(aKey.ToCString());
383 else if (myResMgrUser->Find(aKey.ToCString()))
384 aPattern = myResMgrUser->Value(aKey.ToCString());
386 aPattern = theFormats->Value(j);
387 aPattern += " Files ( *.* )";
390 thePatterns->Append(aPattern);
393 return (!theFormats->IsEmpty());
396 //=============================================================================
400 //=============================================================================
401 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
402 (const Standard_Boolean isImport,
403 const TCollection_AsciiString& theFormat,
404 Handle(TCollection_HAsciiString)& theLibName)
406 if (!InitResMgr()) return Standard_False;
408 // Import/Export mode
409 TCollection_AsciiString aMode;
410 //Standard_CString aMode;
411 if (isImport) aMode = "Import";
412 else aMode = "Export";
415 // Read supported formats for the certain mode from install directory
416 if (myResMgr->Find(aMode.ToCString())) {
417 TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
418 if (aFormats.Search(theFormat) > -1) {
419 // Read library name for the supported format
420 TCollection_AsciiString aKey (theFormat);
423 if (myResMgr->Find(aKey.ToCString())) {
424 TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
430 theLibName = new TCollection_HAsciiString (aLibName);
431 return Standard_True;
436 // Read supported formats for the certain mode from user directory
437 if (myResMgrUser->Find(aMode.ToCString())) {
438 TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
439 if (aFormats.Search(theFormat) > -1) {
440 // Read library name for the supported format
441 TCollection_AsciiString aKey (theFormat);
444 if (myResMgrUser->Find(aKey.ToCString())) {
445 TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
446 theLibName = new TCollection_HAsciiString (aLibName);
447 return Standard_True;
452 return Standard_False;
455 //=============================================================================
459 //=============================================================================
460 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
462 bool isResourceFound = false;
463 bool isResourceFoundUser = false;
464 TCollection_AsciiString aUserResDir,aResDir;
466 if (myResMgr.IsNull()) {
467 // Initialize the Resource Manager
468 TCollection_AsciiString aNull;
469 aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
471 aResDir += "\\share\\salome\\resources\\geom";
473 aResDir += "/share/salome/resources/geom";
476 myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
478 isResourceFound = true;
479 if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
480 // instead of complains in Resource_Manager
481 isResourceFound = false;
482 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
485 isResourceFound = true;
487 if (myResMgrUser.IsNull()) {
488 char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
489 TCollection_AsciiString aNull;
496 aUserResDir = getenv("HOME");
498 aUserResDir += "\\.salome\\resources";
500 aUserResDir += "/.salome/resources";
504 myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
506 isResourceFoundUser = true;
508 if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
509 // instead of complains in Resource_Manager
510 isResourceFoundUser = false;
514 isResourceFoundUser = true;
516 if(!isResourceFound && !isResourceFoundUser){
517 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
518 INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
521 return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
522 myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));