1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Standard_Stream.hxx>
22 #include <GEOMImpl_IInsertOperations.hxx>
24 #include "utilities.h"
26 #include <Utils_ExceptHandlers.hxx>
28 #include <TFunction_DriverTable.hxx>
29 #include <TFunction_Driver.hxx>
30 #include <TFunction_Logbook.hxx>
31 #include <TDF_Tool.hxx>
33 #include <GEOM_Function.hxx>
34 #include <GEOM_PythonDump.hxx>
36 #include <GEOMImpl_CopyDriver.hxx>
37 #include <GEOMImpl_ExportDriver.hxx>
38 #include <GEOMImpl_ImportDriver.hxx>
40 #include <GEOMImpl_ICopy.hxx>
41 #include <GEOMImpl_IImportExport.hxx>
43 #include <GEOMImpl_Types.hxx>
45 #include <Standard_Failure.hxx>
46 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
48 //=============================================================================
52 //=============================================================================
54 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
55 : GEOM_IOperations(theEngine, theDocID)
57 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
60 //=============================================================================
64 //=============================================================================
66 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
68 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
73 //=============================================================================
77 //=============================================================================
78 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) theOriginal)
82 if (theOriginal.IsNull()) return NULL;
84 //Add a new Copy object
85 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
87 //Add a Copy function for creation a copy object
88 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
90 //Check if the function is set correctly
91 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
93 GEOMImpl_ICopy aCI(aFunction);
95 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
96 if (aRefFunction.IsNull()) return NULL;
98 aCI.SetOriginal(aRefFunction);
100 //Compute the Copy value
102 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
105 if (!GetSolver()->ComputeFunction(aFunction)) {
106 SetErrorCode("Copy driver failed");
110 catch (Standard_Failure) {
111 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
112 SetErrorCode(aFail->GetMessageString());
116 //Make a Python command
117 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
123 //=============================================================================
127 //=============================================================================
128 void GEOMImpl_IInsertOperations::Export
129 (const Handle(GEOM_Object) theOriginal,
130 const TCollection_AsciiString& theFileName,
131 const TCollection_AsciiString& theFormatName)
135 if (theOriginal.IsNull()) return;
137 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
138 if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
140 //Add an Export function
141 Handle(GEOM_Function) aFunction = theOriginal->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
142 if (aFunction.IsNull()) return;
144 //Check if the function is set correctly
145 if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
147 Handle(TCollection_HAsciiString) aHLibName;
148 if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
151 TCollection_AsciiString aLibName = aHLibName->String();
154 GEOMImpl_IImportExport aCI (aFunction);
155 aCI.SetOriginal(aRefFunction);
156 aCI.SetFileName(theFileName);
157 aCI.SetFormatName(theFormatName);
158 aCI.SetPluginName(aLibName);
162 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
165 if (!GetSolver()->ComputeFunction(aFunction)) {
166 SetErrorCode("Export driver failed");
170 catch (Standard_Failure) {
171 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
172 SetErrorCode(aFail->GetMessageString());
176 //Make a Python command
177 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
178 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
183 //=============================================================================
187 //=============================================================================
188 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
189 (const TCollection_AsciiString& theFileName,
190 const TCollection_AsciiString& theFormatName)
194 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
196 //Add a new result object
197 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
199 //Add an Import function
200 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
201 if (aFunction.IsNull()) return result;
203 //Check if the function is set correctly
204 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
206 Handle(TCollection_HAsciiString) aHLibName;
207 if (!IsSupported(Standard_True, theFormatName, aHLibName)) {
210 TCollection_AsciiString aLibName = aHLibName->String();
213 GEOMImpl_IImportExport aCI (aFunction);
214 aCI.SetFileName(theFileName);
215 aCI.SetFormatName(theFormatName);
216 aCI.SetPluginName(aLibName);
220 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
223 if (!GetSolver()->ComputeFunction(aFunction)) {
224 SetErrorCode("Import driver failed");
228 catch (Standard_Failure) {
229 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
230 SetErrorCode(aFail->GetMessageString());
234 //Make a Python command
235 GEOM::TPythonDump(aFunction) << result << " = geompy.Import(\""
236 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
242 //=============================================================================
246 //=============================================================================
247 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
248 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
249 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
251 if (theFormats.IsNull())
252 theFormats = new TColStd_HSequenceOfAsciiString;
256 if (thePatterns.IsNull())
257 thePatterns = new TColStd_HSequenceOfAsciiString;
259 thePatterns->Clear();
261 if (!InitResMgr()) return Standard_False;
263 // Read Import formats list
264 if (myResMgr->Find("Import")) {
265 TCollection_AsciiString aFormats (myResMgr->Value("Import"));
266 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
268 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
269 theFormats->Append(aToken);
273 // Read Patterns for each supported format
274 int j = 1, len = theFormats->Length();
275 for (; j <= len; j++) {
276 TCollection_AsciiString aKey, aPattern;
277 aKey = theFormats->Value(j) + ".ImportPattern";
278 if (myResMgr->Find(aKey.ToCString()))
279 aPattern = myResMgr->Value(aKey.ToCString());
281 aKey = theFormats->Value(j) + ".Pattern";
282 if (myResMgr->Find(aKey.ToCString()))
283 aPattern = myResMgr->Value(aKey.ToCString());
285 aPattern = theFormats->Value(j);
286 aPattern += " Files ( *.* )";
289 thePatterns->Append(aPattern);
292 return (!theFormats->IsEmpty());
295 //=============================================================================
299 //=============================================================================
300 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
301 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
302 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
304 if (theFormats.IsNull())
305 theFormats = new TColStd_HSequenceOfAsciiString;
309 if (thePatterns.IsNull())
310 thePatterns = new TColStd_HSequenceOfAsciiString;
312 thePatterns->Clear();
314 if (!InitResMgr()) return Standard_False;
316 // Read Export formats list
317 if (myResMgr->Find("Export")) {
318 TCollection_AsciiString aFormats (myResMgr->Value("Export"));
319 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
321 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
322 theFormats->Append(aToken);
326 // Read Patterns for each supported format
327 int j = 1, len = theFormats->Length();
328 for (; j <= len; j++) {
329 TCollection_AsciiString aKey, aPattern;
330 aKey = theFormats->Value(j) + ".ExportPattern";
331 if (myResMgr->Find(aKey.ToCString()))
332 aPattern = myResMgr->Value(aKey.ToCString());
334 aKey = theFormats->Value(j) + ".Pattern";
335 if (myResMgr->Find(aKey.ToCString()))
336 aPattern = myResMgr->Value(aKey.ToCString());
338 aPattern = theFormats->Value(j);
339 aPattern += " Files ( *.* )";
342 thePatterns->Append(aPattern);
345 return (!theFormats->IsEmpty());
348 //=============================================================================
352 //=============================================================================
353 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
354 (const Standard_Boolean isImport,
355 const TCollection_AsciiString& theFormat,
356 Handle(TCollection_HAsciiString)& theLibName)
358 if (!InitResMgr()) return Standard_False;
360 // Import/Export mode
361 TCollection_AsciiString aMode;
362 //Standard_CString aMode;
363 if (isImport) aMode = "Import";
364 else aMode = "Export";
366 // Read supported formats for the certain mode
367 if (myResMgr->Find(aMode.ToCString())) {
368 TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
369 if (aFormats.Search(theFormat) > -1) {
370 // Read library name for the supported format
371 TCollection_AsciiString aKey (theFormat);
374 if (myResMgr->Find(aKey.ToCString())) {
375 TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
376 theLibName = new TCollection_HAsciiString (aLibName);
377 return Standard_True;
382 return Standard_False;
385 //=============================================================================
389 //=============================================================================
390 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
392 if (myResMgr.IsNull()) {
393 // Initialize the Resource Manager
394 TCollection_AsciiString aResDir (getenv("GEOM_ROOT_DIR"));
396 aResDir += "\\share\\salome\\resources\\geom";
398 aResDir += "/share/salome/resources/geom";
400 char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
401 TCollection_AsciiString aUserResDir;
408 aUserResDir = getenv("HOME");
410 aUserResDir += "\\.salome\\resources";
412 aUserResDir += "/.salome/resources";
415 myResMgr = new Resource_Manager ("ImportExport", aResDir, aUserResDir, Standard_False);
417 if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
418 // instead of complains in Resource_Manager
419 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString() <<
420 " and in " << aUserResDir.ToCString() );
424 return ( myResMgr->Find("Import") || myResMgr->Find("Export") );