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_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
47 //=============================================================================
51 //=============================================================================
53 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
54 : GEOM_IOperations(theEngine, theDocID)
56 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
59 //=============================================================================
63 //=============================================================================
65 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
67 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
72 //=============================================================================
76 //=============================================================================
77 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) theOriginal)
81 if (theOriginal.IsNull()) return NULL;
83 //Add a new Copy object
84 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
86 //Add a Copy function for creation a copy object
87 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
89 //Check if the function is set correctly
90 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
92 GEOMImpl_ICopy aCI(aFunction);
94 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
95 if (aRefFunction.IsNull()) return NULL;
97 aCI.SetOriginal(aRefFunction);
99 //Compute the Copy value
101 if (!GetSolver()->ComputeFunction(aFunction)) {
102 SetErrorCode("Copy driver failed");
106 catch (Standard_Failure) {
107 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
108 SetErrorCode(aFail->GetMessageString());
112 //Make a Python command
113 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
119 //=============================================================================
123 //=============================================================================
124 void GEOMImpl_IInsertOperations::Export
125 (const Handle(GEOM_Object) theOriginal,
126 const TCollection_AsciiString& theFileName,
127 const TCollection_AsciiString& theFormatName)
131 if (theOriginal.IsNull()) return;
133 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
134 if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
136 //Add an Export function
137 Handle(GEOM_Function) aFunction = theOriginal->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
138 if (aFunction.IsNull()) return;
140 //Check if the function is set correctly
141 if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
143 Handle(TCollection_HAsciiString) aHLibName;
144 if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
147 TCollection_AsciiString aLibName = aHLibName->String();
150 GEOMImpl_IImportExport aCI (aFunction);
151 aCI.SetOriginal(aRefFunction);
152 aCI.SetFileName(theFileName);
153 aCI.SetFormatName(theFormatName);
154 aCI.SetPluginName(aLibName);
158 if (!GetSolver()->ComputeFunction(aFunction)) {
159 SetErrorCode("Export driver failed");
163 catch (Standard_Failure) {
164 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
165 SetErrorCode(aFail->GetMessageString());
169 //Make a Python command
170 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
171 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
176 //=============================================================================
180 //=============================================================================
181 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
182 (const TCollection_AsciiString& theFileName,
183 const TCollection_AsciiString& theFormatName)
187 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
189 //Add a new result object
190 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
192 //Add an Import function
193 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
194 if (aFunction.IsNull()) return result;
196 //Check if the function is set correctly
197 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
199 Handle(TCollection_HAsciiString) aHLibName;
200 if (!IsSupported(Standard_True, theFormatName, aHLibName)) {
203 TCollection_AsciiString aLibName = aHLibName->String();
206 GEOMImpl_IImportExport aCI (aFunction);
207 aCI.SetFileName(theFileName);
208 aCI.SetFormatName(theFormatName);
209 aCI.SetPluginName(aLibName);
213 if (!GetSolver()->ComputeFunction(aFunction)) {
214 SetErrorCode("Import driver failed");
218 catch (Standard_Failure) {
219 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
220 SetErrorCode(aFail->GetMessageString());
224 //Make a Python command
225 GEOM::TPythonDump(aFunction) << result << " = geompy.Import(\""
226 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
232 //=============================================================================
236 //=============================================================================
237 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
238 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
239 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
241 if (theFormats.IsNull())
242 theFormats = new TColStd_HSequenceOfAsciiString;
246 if (thePatterns.IsNull())
247 thePatterns = new TColStd_HSequenceOfAsciiString;
249 thePatterns->Clear();
251 if (!InitResMgr()) return Standard_False;
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);
258 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
259 theFormats->Append(aToken);
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());
271 aKey = theFormats->Value(j) + ".Pattern";
272 if (myResMgr->Find(aKey.ToCString()))
273 aPattern = myResMgr->Value(aKey.ToCString());
275 aPattern = theFormats->Value(j);
276 aPattern += " Files ( *.* )";
279 thePatterns->Append(aPattern);
282 return (!theFormats->IsEmpty());
285 //=============================================================================
289 //=============================================================================
290 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
291 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
292 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
294 if (theFormats.IsNull())
295 theFormats = new TColStd_HSequenceOfAsciiString;
299 if (thePatterns.IsNull())
300 thePatterns = new TColStd_HSequenceOfAsciiString;
302 thePatterns->Clear();
304 if (!InitResMgr()) return Standard_False;
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);
311 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
312 theFormats->Append(aToken);
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());
324 aKey = theFormats->Value(j) + ".Pattern";
325 if (myResMgr->Find(aKey.ToCString()))
326 aPattern = myResMgr->Value(aKey.ToCString());
328 aPattern = theFormats->Value(j);
329 aPattern += " Files ( *.* )";
332 thePatterns->Append(aPattern);
335 return (!theFormats->IsEmpty());
338 //=============================================================================
342 //=============================================================================
343 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
344 (const Standard_Boolean isImport,
345 const TCollection_AsciiString& theFormat,
346 Handle(TCollection_HAsciiString)& theLibName)
348 if (!InitResMgr()) return Standard_False;
350 // Import/Export mode
351 TCollection_AsciiString aMode;
352 //Standard_CString aMode;
353 if (isImport) aMode = "Import";
354 else aMode = "Export";
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);
364 if (myResMgr->Find(aKey.ToCString())) {
365 TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
366 theLibName = new TCollection_HAsciiString (aLibName);
367 return Standard_True;
372 return Standard_False;
375 //=============================================================================
379 //=============================================================================
380 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
382 if (myResMgr.IsNull()) {
383 // Initialize the Resource Manager
384 TCollection_AsciiString aResDir (getenv("GEOM_ROOT_DIR"));
386 aResDir += "\\share\\salome\\resources";
388 aResDir += "/share/salome/resources";
390 char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
391 TCollection_AsciiString aUserResDir;
398 aUserResDir = getenv("HOME");
400 aUserResDir += "\\.salome\\resources";
402 aUserResDir += "/.salome/resources";
405 myResMgr = new Resource_Manager ("ImportExport", aResDir, aUserResDir, Standard_False);
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() );
414 return ( myResMgr->Find("Import") || myResMgr->Find("Export") );