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"
16 #include "GEOMImpl_CopyDriver.hxx"
17 #include "GEOMImpl_ExportDriver.hxx"
18 #include "GEOMImpl_ImportDriver.hxx"
20 #include "GEOMImpl_ICopy.hxx"
21 #include "GEOMImpl_IImportExport.hxx"
23 #include "GEOMImpl_Types.hxx"
25 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
27 //=============================================================================
31 //=============================================================================
33 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
34 : GEOM_IOperations(theEngine, theDocID)
36 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
39 //=============================================================================
43 //=============================================================================
45 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
47 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
52 //=============================================================================
56 //=============================================================================
57 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) theOriginal)
61 if (theOriginal.IsNull()) return NULL;
63 //Add a new Copy object
64 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
66 //Add a Copy function for creation a copy object
67 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
69 //Check if the function is set correctly
70 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
72 GEOMImpl_ICopy aCI(aFunction);
74 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
75 if (aRefFunction.IsNull()) return NULL;
77 aCI.SetOriginal(aRefFunction);
79 //Compute the Copy value
81 if (!GetSolver()->ComputeFunction(aFunction)) {
82 SetErrorCode("Copy driver failed");
86 catch (Standard_Failure) {
87 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
88 SetErrorCode(aFail->GetMessageString());
92 //Make a Python command
93 TCollection_AsciiString anEntry, aDescr;
94 TDF_Tool::Entry(aCopy->GetEntry(), anEntry);
95 aDescr += (anEntry+" = IInsertOperations.MakeCopy(");
96 TDF_Tool::Entry(theOriginal->GetEntry(), anEntry);
97 aDescr += (anEntry+")");
99 aFunction->SetDescription(aDescr);
105 //=============================================================================
109 //=============================================================================
110 void GEOMImpl_IInsertOperations::Export
111 (const Handle(GEOM_Object) theOriginal,
112 const char* theFileName,
113 const char* theFormatName)
117 if (theOriginal.IsNull()) return;
119 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
120 if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
122 //Add an Export function
123 Handle(GEOM_Function) aFunction = theOriginal->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
124 if (aFunction.IsNull()) return;
126 //Check if the function is set correctly
127 if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
130 GEOMImpl_IImportExport aCI (aFunction);
131 aCI.SetOriginal(aRefFunction);
132 char* aFileName = (char*)theFileName;
133 aCI.SetFileName(aFileName);
135 char* aFormatName = (char*)theFormatName;
136 Handle(TCollection_HAsciiString) aHLibName;
137 if (!IsSupported(Standard_False, aFormatName, aHLibName)) {
140 TCollection_AsciiString aLibName = aHLibName->String();
141 aCI.SetPluginName(aLibName);
145 if (!GetSolver()->ComputeFunction(aFunction)) {
146 SetErrorCode("Export driver failed");
150 catch (Standard_Failure) {
151 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
152 SetErrorCode(aFail->GetMessageString());
156 //Make a Python command
157 TCollection_AsciiString anEntry, aDescr;
158 aDescr = "IInsertOperations.Export(";
159 TDF_Tool::Entry(theOriginal->GetEntry(), anEntry);
160 aDescr += (anEntry + ", ");
161 aDescr += (TCollection_AsciiString(aFileName) + ", ");
162 aDescr += (TCollection_AsciiString(aFormatName) + ")");
164 aFunction->SetDescription(aDescr);
169 //=============================================================================
173 //=============================================================================
174 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
175 (const char* theFileName,
176 const char* theFormatName)
180 if (!theFileName || !theFormatName) return NULL;
182 //Add a new result object
183 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
185 //Add an Import function
186 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
187 if (aFunction.IsNull()) return result;
189 //Check if the function is set correctly
190 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
193 GEOMImpl_IImportExport aCI (aFunction);
194 char* aFileName = (char*)theFileName;
195 aCI.SetFileName(aFileName);
197 char* aFormatName = (char*)theFormatName;
198 Handle(TCollection_HAsciiString) aHLibName;
199 if (!IsSupported(Standard_True, aFormatName, aHLibName)) {
202 TCollection_AsciiString aLibName = aHLibName->String();
203 aCI.SetPluginName(aLibName);
207 if (!GetSolver()->ComputeFunction(aFunction)) {
208 SetErrorCode("Import driver failed");
212 catch (Standard_Failure) {
213 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
214 SetErrorCode(aFail->GetMessageString());
218 //Make a Python command
219 TCollection_AsciiString anEntry, aDescr;
220 TDF_Tool::Entry(result->GetEntry(), anEntry);
221 aDescr += (anEntry + " = IInsertOperations.Import(");
222 aDescr += (TCollection_AsciiString(aFileName) + ", ");
223 aDescr += (TCollection_AsciiString(aFormatName) + ")");
225 aFunction->SetDescription(aDescr);
231 //=============================================================================
235 //=============================================================================
236 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
237 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
238 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
240 if (theFormats.IsNull())
241 theFormats = new TColStd_HSequenceOfAsciiString;
245 if (thePatterns.IsNull())
246 thePatterns = new TColStd_HSequenceOfAsciiString;
248 thePatterns->Clear();
250 if (!InitResMgr()) return Standard_False;
252 // Read Import formats list
253 if (myResMgr->Find("Import")) {
254 TCollection_AsciiString aFormats (myResMgr->Value("Import"));
255 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
257 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
258 theFormats->Append(aToken);
262 // Read Patterns for each supported format
263 int j = 1, len = theFormats->Length();
264 for (; j <= len; j++) {
265 TCollection_AsciiString aPattern;
266 TCollection_AsciiString aKey (theFormats->Value(j));
268 if (myResMgr->Find(aKey.ToCString()))
269 aPattern = myResMgr->Value(aKey.ToCString());
271 aPattern = theFormats->Value(j);
272 aPattern += " Files ( *.* )";
274 thePatterns->Append(aPattern);
277 return (!theFormats->IsEmpty());
280 //=============================================================================
284 //=============================================================================
285 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
286 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
287 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
289 if (theFormats.IsNull())
290 theFormats = new TColStd_HSequenceOfAsciiString;
294 if (thePatterns.IsNull())
295 thePatterns = new TColStd_HSequenceOfAsciiString;
297 thePatterns->Clear();
299 if (!InitResMgr()) return Standard_False;
301 // Read Export formats list
302 if (myResMgr->Find("Export")) {
303 TCollection_AsciiString aFormats (myResMgr->Value("Export"));
304 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
306 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
307 theFormats->Append(aToken);
311 // Read Patterns for each supported format
312 int j = 1, len = theFormats->Length();
313 for (; j <= len; j++) {
314 TCollection_AsciiString aPattern;
315 TCollection_AsciiString aKey (theFormats->Value(j));
317 if (myResMgr->Find(aKey.ToCString()))
318 aPattern = myResMgr->Value(aKey.ToCString());
320 aPattern = theFormats->Value(j);
321 aPattern += " Files ( *.* )";
323 thePatterns->Append(aPattern);
326 return (!theFormats->IsEmpty());
329 //=============================================================================
333 //=============================================================================
334 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
335 (const Standard_Boolean isImport,
336 const TCollection_AsciiString theFormat,
337 Handle(TCollection_HAsciiString)& theLibName)
339 if (!InitResMgr()) return Standard_False;
341 // Import/Export mode
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)) {
348 TCollection_AsciiString aFormats (myResMgr->Value(aMode));
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 TCollection_AsciiString aUserResDir (getenv("HOME"));
382 aUserResDir += "\\.salome\\resources";
384 aUserResDir += "/.salome/resources";
386 myResMgr = new Resource_Manager ("ImportExport", aResDir, aUserResDir, Standard_True);
389 if (myResMgr->Find("Import") || myResMgr->Find("Export"))
390 return Standard_True;
392 return Standard_False;