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/
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;
144 GEOMImpl_IImportExport aCI (aFunction);
145 aCI.SetOriginal(aRefFunction);
146 aCI.SetFileName(theFileName);
148 Handle(TCollection_HAsciiString) aHLibName;
149 if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
152 TCollection_AsciiString aLibName = aHLibName->String();
153 aCI.SetPluginName(aLibName);
157 if (!GetSolver()->ComputeFunction(aFunction)) {
158 SetErrorCode("Export driver failed");
162 catch (Standard_Failure) {
163 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
164 SetErrorCode(aFail->GetMessageString());
168 //Make a Python command
169 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
170 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
175 //=============================================================================
179 //=============================================================================
180 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
181 (const TCollection_AsciiString& theFileName,
182 const TCollection_AsciiString& theFormatName)
186 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
188 //Add a new result object
189 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
191 //Add an Import function
192 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
193 if (aFunction.IsNull()) return result;
195 //Check if the function is set correctly
196 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
199 GEOMImpl_IImportExport aCI (aFunction);
200 aCI.SetFileName(theFileName);
202 Handle(TCollection_HAsciiString) aHLibName;
203 if (!IsSupported(Standard_True, theFormatName, aHLibName)) {
206 TCollection_AsciiString aLibName = aHLibName->String();
207 aCI.SetPluginName(aLibName);
211 if (!GetSolver()->ComputeFunction(aFunction)) {
212 SetErrorCode("Import driver failed");
216 catch (Standard_Failure) {
217 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
218 SetErrorCode(aFail->GetMessageString());
222 //Make a Python command
223 GEOM::TPythonDump(aFunction) << result << " = geompy.Import(\""
224 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
230 //=============================================================================
234 //=============================================================================
235 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
236 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
237 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
239 if (theFormats.IsNull())
240 theFormats = new TColStd_HSequenceOfAsciiString;
244 if (thePatterns.IsNull())
245 thePatterns = new TColStd_HSequenceOfAsciiString;
247 thePatterns->Clear();
249 if (!InitResMgr()) return Standard_False;
251 // Read Import formats list
252 if (myResMgr->Find("Import")) {
253 TCollection_AsciiString aFormats (myResMgr->Value("Import"));
254 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
256 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
257 theFormats->Append(aToken);
261 // Read Patterns for each supported format
262 int j = 1, len = theFormats->Length();
263 for (; j <= len; j++) {
264 TCollection_AsciiString aPattern;
265 TCollection_AsciiString aKey (theFormats->Value(j));
267 if (myResMgr->Find(aKey.ToCString()))
268 aPattern = myResMgr->Value(aKey.ToCString());
270 aPattern = theFormats->Value(j);
271 aPattern += " Files ( *.* )";
273 thePatterns->Append(aPattern);
276 return (!theFormats->IsEmpty());
279 //=============================================================================
283 //=============================================================================
284 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
285 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
286 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
288 if (theFormats.IsNull())
289 theFormats = new TColStd_HSequenceOfAsciiString;
293 if (thePatterns.IsNull())
294 thePatterns = new TColStd_HSequenceOfAsciiString;
296 thePatterns->Clear();
298 if (!InitResMgr()) return Standard_False;
300 // Read Export formats list
301 if (myResMgr->Find("Export")) {
302 TCollection_AsciiString aFormats (myResMgr->Value("Export"));
303 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
305 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
306 theFormats->Append(aToken);
310 // Read Patterns for each supported format
311 int j = 1, len = theFormats->Length();
312 for (; j <= len; j++) {
313 TCollection_AsciiString aPattern;
314 TCollection_AsciiString aKey (theFormats->Value(j));
316 if (myResMgr->Find(aKey.ToCString()))
317 aPattern = myResMgr->Value(aKey.ToCString());
319 aPattern = theFormats->Value(j);
320 aPattern += " Files ( *.* )";
322 thePatterns->Append(aPattern);
325 return (!theFormats->IsEmpty());
328 //=============================================================================
332 //=============================================================================
333 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
334 (const Standard_Boolean isImport,
335 const TCollection_AsciiString& theFormat,
336 Handle(TCollection_HAsciiString)& theLibName)
338 if (!InitResMgr()) return Standard_False;
340 // Import/Export mode
341 TCollection_AsciiString aMode;
342 //Standard_CString aMode;
343 if (isImport) aMode = "Import";
344 else aMode = "Export";
346 // Read supported formats for the certain mode
347 if (myResMgr->Find(aMode.ToCString())) {
348 TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
349 if (aFormats.Search(theFormat) > -1) {
350 // Read library name for the supported format
351 TCollection_AsciiString aKey (theFormat);
354 if (myResMgr->Find(aKey.ToCString())) {
355 TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
356 theLibName = new TCollection_HAsciiString (aLibName);
357 return Standard_True;
362 return Standard_False;
365 //=============================================================================
369 //=============================================================================
370 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
372 if (myResMgr.IsNull()) {
373 // Initialize the Resource Manager
374 TCollection_AsciiString aResDir (getenv("GEOM_ROOT_DIR"));
376 aResDir += "\\share\\salome\\resources";
378 aResDir += "/share/salome/resources";
380 char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
381 TCollection_AsciiString aUserResDir;
388 aUserResDir = getenv("HOME");
390 aUserResDir += "\\.salome\\resources";
392 aUserResDir += "/.salome/resources";
395 myResMgr = new Resource_Manager ("ImportExport", aResDir, aUserResDir, Standard_False);
397 if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
398 // instead of complains in Resource_Manager
399 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString() <<
400 " and in " << aUserResDir.ToCString() );
404 return ( myResMgr->Find("Import") || myResMgr->Find("Export") );