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 char* theFileName,
108 const char* 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 char* aFileName = (char*)theFileName;
128 aCI.SetFileName(aFileName);
130 char* aFormatName = (char*)theFormatName;
131 Handle(TCollection_HAsciiString) aHLibName;
132 if (!IsSupported(Standard_False, aFormatName, aHLibName)) {
135 TCollection_AsciiString aLibName = aHLibName->String();
136 aCI.SetPluginName(aLibName);
140 if (!GetSolver()->ComputeFunction(aFunction)) {
141 SetErrorCode("Export driver failed");
145 catch (Standard_Failure) {
146 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
147 SetErrorCode(aFail->GetMessageString());
151 //Make a Python command
152 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal
153 << ", \"" << theFileName << "\", \"" << theFormatName << "\")";
158 //=============================================================================
162 //=============================================================================
163 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
164 (const char* theFileName,
165 const char* theFormatName)
169 if (!theFileName || !theFormatName) return NULL;
171 //Add a new result object
172 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
174 //Add an Import function
175 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
176 if (aFunction.IsNull()) return result;
178 //Check if the function is set correctly
179 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
182 GEOMImpl_IImportExport aCI (aFunction);
183 char* aFileName = (char*)theFileName;
184 aCI.SetFileName(aFileName);
186 char* aFormatName = (char*)theFormatName;
187 Handle(TCollection_HAsciiString) aHLibName;
188 if (!IsSupported(Standard_True, aFormatName, aHLibName)) {
191 TCollection_AsciiString aLibName = aHLibName->String();
192 aCI.SetPluginName(aLibName);
196 if (!GetSolver()->ComputeFunction(aFunction)) {
197 SetErrorCode("Import driver failed");
201 catch (Standard_Failure) {
202 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
203 SetErrorCode(aFail->GetMessageString());
207 //Make a Python command
208 GEOM::TPythonDump(aFunction) << result << " = geompy.Import(\""
209 << theFileName << "\", \"" << theFormatName << "\")";
215 //=============================================================================
219 //=============================================================================
220 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
221 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
222 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
224 if (theFormats.IsNull())
225 theFormats = new TColStd_HSequenceOfAsciiString;
229 if (thePatterns.IsNull())
230 thePatterns = new TColStd_HSequenceOfAsciiString;
232 thePatterns->Clear();
234 if (!InitResMgr()) return Standard_False;
236 // Read Import formats list
237 if (myResMgr->Find("Import")) {
238 TCollection_AsciiString aFormats (myResMgr->Value("Import"));
239 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
241 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
242 theFormats->Append(aToken);
246 // Read Patterns for each supported format
247 int j = 1, len = theFormats->Length();
248 for (; j <= len; j++) {
249 TCollection_AsciiString aPattern;
250 TCollection_AsciiString aKey (theFormats->Value(j));
252 if (myResMgr->Find(aKey.ToCString()))
253 aPattern = myResMgr->Value(aKey.ToCString());
255 aPattern = theFormats->Value(j);
256 aPattern += " Files ( *.* )";
258 thePatterns->Append(aPattern);
261 return (!theFormats->IsEmpty());
264 //=============================================================================
268 //=============================================================================
269 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
270 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
271 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
273 if (theFormats.IsNull())
274 theFormats = new TColStd_HSequenceOfAsciiString;
278 if (thePatterns.IsNull())
279 thePatterns = new TColStd_HSequenceOfAsciiString;
281 thePatterns->Clear();
283 if (!InitResMgr()) return Standard_False;
285 // Read Export formats list
286 if (myResMgr->Find("Export")) {
287 TCollection_AsciiString aFormats (myResMgr->Value("Export"));
288 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
290 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
291 theFormats->Append(aToken);
295 // Read Patterns for each supported format
296 int j = 1, len = theFormats->Length();
297 for (; j <= len; j++) {
298 TCollection_AsciiString aPattern;
299 TCollection_AsciiString aKey (theFormats->Value(j));
301 if (myResMgr->Find(aKey.ToCString()))
302 aPattern = myResMgr->Value(aKey.ToCString());
304 aPattern = theFormats->Value(j);
305 aPattern += " Files ( *.* )";
307 thePatterns->Append(aPattern);
310 return (!theFormats->IsEmpty());
313 //=============================================================================
317 //=============================================================================
318 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
319 (const Standard_Boolean isImport,
320 const TCollection_AsciiString theFormat,
321 Handle(TCollection_HAsciiString)& theLibName)
323 if (!InitResMgr()) return Standard_False;
325 // Import/Export mode
326 Standard_CString aMode;
327 if (isImport) aMode = "Import";
328 else aMode = "Export";
330 // Read supported formats for the certain mode
331 if (myResMgr->Find(aMode)) {
332 TCollection_AsciiString aFormats (myResMgr->Value(aMode));
333 if (aFormats.Search(theFormat) > -1) {
334 // Read library name for the supported format
335 TCollection_AsciiString aKey (theFormat);
338 if (myResMgr->Find(aKey.ToCString())) {
339 TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
340 theLibName = new TCollection_HAsciiString (aLibName);
341 return Standard_True;
346 return Standard_False;
349 //=============================================================================
353 //=============================================================================
354 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
356 if (myResMgr.IsNull()) {
357 // Initialize the Resource Manager
358 TCollection_AsciiString aResDir (getenv("GEOM_ROOT_DIR"));
360 aResDir += "\\share\\salome\\resources";
362 aResDir += "/share/salome/resources";
364 TCollection_AsciiString aUserResDir (getenv("HOME"));
366 aUserResDir += "\\.salome\\resources";
368 aUserResDir += "/.salome/resources";
370 myResMgr = new Resource_Manager ("ImportExport", aResDir, aUserResDir, Standard_True);
373 if (myResMgr->Find("Import") || myResMgr->Find("Export"))
374 return Standard_True;
376 return Standard_False;