1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include <Standard_Stream.hxx>
24 #include <GEOMImpl_IInsertOperations.hxx>
26 #include "utilities.h"
28 #include <Utils_ExceptHandlers.hxx>
30 #include <TFunction_DriverTable.hxx>
31 #include <TFunction_Driver.hxx>
32 #include <TFunction_Logbook.hxx>
33 #include <TDF_Tool.hxx>
35 #include <GEOM_Function.hxx>
36 #include <GEOM_PythonDump.hxx>
38 #include <GEOMImpl_CopyDriver.hxx>
39 #include <GEOMImpl_ExportDriver.hxx>
40 #include <GEOMImpl_ImportDriver.hxx>
42 #include <GEOMImpl_ICopy.hxx>
43 #include <GEOMImpl_IImportExport.hxx>
45 #include <GEOMImpl_Types.hxx>
48 #include <TopoDS_Vertex.hxx>
49 #include <BRep_Tool.hxx>
52 #include <Standard_Failure.hxx>
53 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
55 //=============================================================================
59 //=============================================================================
61 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
62 : GEOM_IOperations(theEngine, theDocID)
64 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
67 //=============================================================================
71 //=============================================================================
73 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
75 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
80 //=============================================================================
84 //=============================================================================
85 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) theOriginal)
89 if (theOriginal.IsNull()) return NULL;
91 //Add a new Copy object
92 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
94 //Add a Copy function for creation a copy object
95 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
97 //Check if the function is set correctly
98 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
100 GEOMImpl_ICopy aCI(aFunction);
102 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
103 if (aRefFunction.IsNull()) return NULL;
105 aCI.SetOriginal(aRefFunction);
107 //Compute the Copy value
109 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
112 if (!GetSolver()->ComputeFunction(aFunction)) {
113 SetErrorCode("Copy driver failed");
117 catch (Standard_Failure) {
118 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
119 SetErrorCode(aFail->GetMessageString());
123 //Make a Python command
124 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
130 //=============================================================================
134 //=============================================================================
135 void GEOMImpl_IInsertOperations::Export
136 (const Handle(GEOM_Object) theOriginal,
137 const TCollection_AsciiString& theFileName,
138 const TCollection_AsciiString& theFormatName)
142 if (theOriginal.IsNull()) return;
144 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
145 if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
147 //Add an Export function
148 Handle(GEOM_Function) aFunction = theOriginal->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
149 if (aFunction.IsNull()) return;
151 //Check if the function is set correctly
152 if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
154 Handle(TCollection_HAsciiString) aHLibName;
155 if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
158 TCollection_AsciiString aLibName = aHLibName->String();
161 GEOMImpl_IImportExport aCI (aFunction);
162 aCI.SetOriginal(aRefFunction);
163 aCI.SetFileName(theFileName);
164 aCI.SetFormatName(theFormatName);
165 aCI.SetPluginName(aLibName);
169 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
172 if (!GetSolver()->ComputeFunction(aFunction)) {
173 SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
177 catch (Standard_Failure) {
178 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
179 SetErrorCode(aFail->GetMessageString());
183 //Make a Python command
184 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
185 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
190 //=============================================================================
194 //=============================================================================
195 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
196 (const TCollection_AsciiString& theFileName,
197 const TCollection_AsciiString& theFormatName)
201 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
203 //Add a new result object
204 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
206 //Add an Import function
207 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
208 if (aFunction.IsNull()) return result;
210 //Check if the function is set correctly
211 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
213 Handle(TCollection_HAsciiString) aHLibName;
214 if (!IsSupported(Standard_True, theFormatName.SubString(1,4), aHLibName)) {
217 TCollection_AsciiString aLibName = aHLibName->String();
220 GEOMImpl_IImportExport aCI (aFunction);
221 aCI.SetFileName(theFileName);
222 aCI.SetFormatName(theFormatName);
223 aCI.SetPluginName(aLibName);
224 //cout<<"IIO: theFormatName = "<<theFormatName.ToCString()<<endl;
228 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
231 if (!GetSolver()->ComputeFunction(aFunction)) {
232 SetErrorCode("Import driver failed");
236 catch (Standard_Failure) {
237 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
238 SetErrorCode(aFail->GetMessageString());
242 //Make a Python command
243 GEOM::TPythonDump(aFunction) << result << " = geompy.Import(\""
244 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
248 if( theFormatName == "IGES_UNIT" ) {
249 TopoDS_Shape S = aFunction->GetValue();
250 TopoDS_Vertex V = TopoDS::Vertex(S);
251 gp_Pnt P = BRep_Tool::Pnt(V);
252 double scale = P.X();
253 TCollection_AsciiString aUnitName = "UNIT_M";
254 if( fabs(scale-0.01) < 1.e-6 )
255 aUnitName = "UNIT_CM";
256 else if( fabs(scale-0.001) < 1.e-6 )
257 aUnitName = "UNIT_MM";
258 //cout<<"IIO: aUnitName = "<<aUnitName.ToCString()<<endl;
259 SetErrorCode(aUnitName);
265 //=============================================================================
269 //=============================================================================
270 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
271 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
272 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
274 if (theFormats.IsNull())
275 theFormats = new TColStd_HSequenceOfAsciiString;
279 if (thePatterns.IsNull())
280 thePatterns = new TColStd_HSequenceOfAsciiString;
282 thePatterns->Clear();
284 if (!InitResMgr()) return Standard_False;
286 // Read Import formats list from install directory
287 if (myResMgr->Find("Import")) {
288 TCollection_AsciiString aFormats (myResMgr->Value("Import"));
289 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
291 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
292 theFormats->Append(aToken);
296 // Read Import formats from user directory
297 if (myResMgrUser->Find("Import")) {
298 TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
299 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
301 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
302 int aLenFormats = theFormats->Length();
303 bool isFound = false;
304 for(int aInd=1;aInd<=aLenFormats;aInd++){
305 if( theFormats->Value(aInd) == aToken){
311 theFormats->Append(aToken);
315 // Read Patterns for each supported format
316 int j = 1, len = theFormats->Length();
317 for (; j <= len; j++) {
318 TCollection_AsciiString aKey, aPattern;
319 aKey = theFormats->Value(j) + ".ImportPattern";
320 if (myResMgr->Find(aKey.ToCString()))
321 aPattern = myResMgr->Value(aKey.ToCString());
322 else if(myResMgrUser->Find(aKey.ToCString()))
323 aPattern = myResMgrUser->Value(aKey.ToCString());
325 aKey = theFormats->Value(j) + ".Pattern";
326 if (myResMgr->Find(aKey.ToCString()))
327 aPattern = myResMgr->Value(aKey.ToCString());
328 else if(myResMgrUser->Find(aKey.ToCString()))
329 aPattern = myResMgrUser->Value(aKey.ToCString());
331 aPattern = theFormats->Value(j);
332 aPattern += " Files ( *.* )";
335 thePatterns->Append(aPattern);
338 return (!theFormats->IsEmpty());
341 //=============================================================================
345 //=============================================================================
346 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
347 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
348 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
350 if (theFormats.IsNull())
351 theFormats = new TColStd_HSequenceOfAsciiString;
355 if (thePatterns.IsNull())
356 thePatterns = new TColStd_HSequenceOfAsciiString;
358 thePatterns->Clear();
360 if (!InitResMgr()) return Standard_False;
362 // Read Export formats list from install directory
363 if (myResMgr->Find("Export")) {
364 TCollection_AsciiString aFormats (myResMgr->Value("Export"));
365 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
367 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
368 theFormats->Append(aToken);
372 // Read Export formats list from user directory
373 if (myResMgrUser->Find("Export")) {
374 TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
375 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
377 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
378 int aLenFormats = theFormats->Length();
379 bool isFound = false;
380 for(int aInd=1;aInd<=aLenFormats;aInd++){
381 if( theFormats->Value(aInd) == aToken){
387 theFormats->Append(aToken);
391 // Read Patterns for each supported format
392 int j = 1, len = theFormats->Length();
393 for (; j <= len; j++) {
394 TCollection_AsciiString aKey, aPattern;
395 aKey = theFormats->Value(j) + ".ExportPattern";
396 if (myResMgr->Find(aKey.ToCString()))
397 aPattern = myResMgr->Value(aKey.ToCString());
398 else if (myResMgrUser->Find(aKey.ToCString()))
399 aPattern = myResMgrUser->Value(aKey.ToCString());
401 aKey = theFormats->Value(j) + ".Pattern";
402 if (myResMgr->Find(aKey.ToCString()))
403 aPattern = myResMgr->Value(aKey.ToCString());
404 else if (myResMgrUser->Find(aKey.ToCString()))
405 aPattern = myResMgrUser->Value(aKey.ToCString());
407 aPattern = theFormats->Value(j);
408 aPattern += " Files ( *.* )";
411 thePatterns->Append(aPattern);
414 return (!theFormats->IsEmpty());
417 //=============================================================================
421 //=============================================================================
422 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
423 (const Standard_Boolean isImport,
424 const TCollection_AsciiString& theFormat,
425 Handle(TCollection_HAsciiString)& theLibName)
427 if (!InitResMgr()) return Standard_False;
429 // Import/Export mode
430 TCollection_AsciiString aMode;
431 //Standard_CString aMode;
432 if (isImport) aMode = "Import";
433 else aMode = "Export";
435 // Read supported formats for the certain mode from install directory
436 if (myResMgr->Find(aMode.ToCString())) {
437 TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
438 if (aFormats.Search(theFormat) > -1) {
439 // Read library name for the supported format
440 TCollection_AsciiString aKey (theFormat);
443 if (myResMgr->Find(aKey.ToCString())) {
444 TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
450 theLibName = new TCollection_HAsciiString (aLibName);
451 return Standard_True;
456 // Read supported formats for the certain mode from user directory
457 if (myResMgrUser->Find(aMode.ToCString())) {
458 TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
459 if (aFormats.Search(theFormat) > -1) {
460 // Read library name for the supported format
461 TCollection_AsciiString aKey (theFormat);
464 if (myResMgrUser->Find(aKey.ToCString())) {
465 TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
466 theLibName = new TCollection_HAsciiString (aLibName);
467 return Standard_True;
472 return Standard_False;
475 //=============================================================================
479 //=============================================================================
480 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
482 bool isResourceFound = false;
483 bool isResourceFoundUser = false;
484 TCollection_AsciiString aUserResDir,aResDir;
486 if (myResMgr.IsNull()) {
487 // Initialize the Resource Manager
488 TCollection_AsciiString aNull;
489 aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
491 aResDir += "\\share\\salome\\resources\\geom";
493 aResDir += "/share/salome/resources/geom";
496 myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
498 isResourceFound = true;
499 if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
500 // instead of complains in Resource_Manager
501 isResourceFound = false;
502 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
505 isResourceFound = true;
507 if (myResMgrUser.IsNull()) {
508 char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
509 TCollection_AsciiString aNull;
516 aUserResDir = getenv("HOME");
518 aUserResDir += "\\.salome\\resources";
520 aUserResDir += "/.salome/resources";
524 myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
526 isResourceFoundUser = true;
528 if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
529 // instead of complains in Resource_Manager
530 isResourceFoundUser = false;
534 isResourceFoundUser = true;
536 if(!isResourceFound && !isResourceFoundUser){
537 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
538 INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
541 return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
542 myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));