1 #include <Standard_Stream.hxx>
3 #include <GEOMImpl_IInsertOperations.hxx>
7 #include <Utils_ExceptHandlers.hxx>
9 #include <TFunction_DriverTable.hxx>
10 #include <TFunction_Driver.hxx>
11 #include <TFunction_Logbook.hxx>
12 #include <TDF_Tool.hxx>
14 #include <GEOM_Function.hxx>
15 #include <GEOM_PythonDump.hxx>
17 #include <GEOMImpl_CopyDriver.hxx>
18 #include <GEOMImpl_ExportDriver.hxx>
19 #include <GEOMImpl_ImportDriver.hxx>
21 #include <GEOMImpl_ICopy.hxx>
22 #include <GEOMImpl_IImportExport.hxx>
24 #include <GEOMImpl_Types.hxx>
26 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
28 //=============================================================================
32 //=============================================================================
34 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
35 : GEOM_IOperations(theEngine, theDocID)
37 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
40 //=============================================================================
44 //=============================================================================
46 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
48 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
53 //=============================================================================
57 //=============================================================================
58 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) theOriginal)
62 if (theOriginal.IsNull()) return NULL;
64 //Add a new Copy object
65 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
67 //Add a Copy function for creation a copy object
68 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
70 //Check if the function is set correctly
71 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
73 GEOMImpl_ICopy aCI(aFunction);
75 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
76 if (aRefFunction.IsNull()) return NULL;
78 aCI.SetOriginal(aRefFunction);
80 //Compute the Copy value
82 if (!GetSolver()->ComputeFunction(aFunction)) {
83 SetErrorCode("Copy driver failed");
87 catch (Standard_Failure) {
88 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
89 SetErrorCode(aFail->GetMessageString());
93 //Make a Python command
94 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
100 //=============================================================================
104 //=============================================================================
105 void GEOMImpl_IInsertOperations::Export
106 (const Handle(GEOM_Object) theOriginal,
107 const TCollection_AsciiString& theFileName,
108 const TCollection_AsciiString& theFormatName)
112 if (theOriginal.IsNull()) return;
114 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
115 if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
117 //Add an Export function
118 Handle(GEOM_Function) aFunction = theOriginal->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
119 if (aFunction.IsNull()) return;
121 //Check if the function is set correctly
122 if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
125 GEOMImpl_IImportExport aCI (aFunction);
126 aCI.SetOriginal(aRefFunction);
127 aCI.SetFileName(theFileName);
129 Handle(TCollection_HAsciiString) aHLibName;
130 if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
133 TCollection_AsciiString aLibName = aHLibName->String();
134 aCI.SetPluginName(aLibName);
138 if (!GetSolver()->ComputeFunction(aFunction)) {
139 SetErrorCode("Export driver failed");
143 catch (Standard_Failure) {
144 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
145 SetErrorCode(aFail->GetMessageString());
149 //Make a Python command
150 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
151 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
156 //=============================================================================
160 //=============================================================================
161 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
162 (const TCollection_AsciiString& theFileName,
163 const TCollection_AsciiString& theFormatName)
167 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
169 //Add a new result object
170 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
172 //Add an Import function
173 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
174 if (aFunction.IsNull()) return result;
176 //Check if the function is set correctly
177 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
180 GEOMImpl_IImportExport aCI (aFunction);
181 aCI.SetFileName(theFileName);
183 Handle(TCollection_HAsciiString) aHLibName;
184 if (!IsSupported(Standard_True, theFormatName, aHLibName)) {
187 TCollection_AsciiString aLibName = aHLibName->String();
188 aCI.SetPluginName(aLibName);
192 if (!GetSolver()->ComputeFunction(aFunction)) {
193 SetErrorCode("Import driver failed");
197 catch (Standard_Failure) {
198 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
199 SetErrorCode(aFail->GetMessageString());
203 //Make a Python command
204 GEOM::TPythonDump(aFunction) << result << " = geompy.Import(\""
205 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
211 //=============================================================================
215 //=============================================================================
216 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
217 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
218 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
220 if (theFormats.IsNull())
221 theFormats = new TColStd_HSequenceOfAsciiString;
225 if (thePatterns.IsNull())
226 thePatterns = new TColStd_HSequenceOfAsciiString;
228 thePatterns->Clear();
230 if (!InitResMgr()) return Standard_False;
232 // Read Import formats list
233 if (myResMgr->Find("Import")) {
234 TCollection_AsciiString aFormats (myResMgr->Value("Import"));
235 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
237 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
238 theFormats->Append(aToken);
242 // Read Patterns for each supported format
243 int j = 1, len = theFormats->Length();
244 for (; j <= len; j++) {
245 TCollection_AsciiString aPattern;
246 TCollection_AsciiString aKey (theFormats->Value(j));
248 if (myResMgr->Find(aKey.ToCString()))
249 aPattern = myResMgr->Value(aKey.ToCString());
251 aPattern = theFormats->Value(j);
252 aPattern += " Files ( *.* )";
254 thePatterns->Append(aPattern);
257 return (!theFormats->IsEmpty());
260 //=============================================================================
264 //=============================================================================
265 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
266 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
267 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
269 if (theFormats.IsNull())
270 theFormats = new TColStd_HSequenceOfAsciiString;
274 if (thePatterns.IsNull())
275 thePatterns = new TColStd_HSequenceOfAsciiString;
277 thePatterns->Clear();
279 if (!InitResMgr()) return Standard_False;
281 // Read Export formats list
282 if (myResMgr->Find("Export")) {
283 TCollection_AsciiString aFormats (myResMgr->Value("Export"));
284 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
286 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
287 theFormats->Append(aToken);
291 // Read Patterns for each supported format
292 int j = 1, len = theFormats->Length();
293 for (; j <= len; j++) {
294 TCollection_AsciiString aPattern;
295 TCollection_AsciiString aKey (theFormats->Value(j));
297 if (myResMgr->Find(aKey.ToCString()))
298 aPattern = myResMgr->Value(aKey.ToCString());
300 aPattern = theFormats->Value(j);
301 aPattern += " Files ( *.* )";
303 thePatterns->Append(aPattern);
306 return (!theFormats->IsEmpty());
309 //=============================================================================
313 //=============================================================================
314 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
315 (const Standard_Boolean isImport,
316 const TCollection_AsciiString& theFormat,
317 Handle(TCollection_HAsciiString)& theLibName)
319 if (!InitResMgr()) return Standard_False;
321 // Import/Export mode
322 TCollection_AsciiString aMode;
323 //Standard_CString aMode;
324 if (isImport) aMode = "Import";
325 else aMode = "Export";
327 // Read supported formats for the certain mode
328 if (myResMgr->Find(aMode.ToCString())) {
329 TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
330 if (aFormats.Search(theFormat) > -1) {
331 // Read library name for the supported format
332 TCollection_AsciiString aKey (theFormat);
335 if (myResMgr->Find(aKey.ToCString())) {
336 TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
337 theLibName = new TCollection_HAsciiString (aLibName);
338 return Standard_True;
343 return Standard_False;
346 //=============================================================================
350 //=============================================================================
351 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
353 if (myResMgr.IsNull()) {
354 // Initialize the Resource Manager
355 TCollection_AsciiString aResDir (getenv("GEOM_ROOT_DIR"));
357 aResDir += "\\share\\salome\\resources";
359 aResDir += "/share/salome/resources";
361 char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
362 TCollection_AsciiString aUserResDir;
369 aUserResDir = getenv("HOME");
371 aUserResDir += "\\.salome\\resources";
373 aUserResDir += "/.salome/resources";
376 myResMgr = new Resource_Manager ("ImportExport", aResDir, aUserResDir, Standard_False);
378 if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
379 // instead of complains in Resource_Manager
380 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString() <<
381 " and in " << aUserResDir.ToCString() );
385 return ( myResMgr->Find("Import") || myResMgr->Find("Export") );