1 // Copyright (C) 2007-2013 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
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_IInsertOperations.hxx>
27 #include <GEOMImpl_CopyDriver.hxx>
28 #include <GEOMImpl_ExportDriver.hxx>
29 #include <GEOMImpl_ImportDriver.hxx>
30 #include <GEOMImpl_ICopy.hxx>
31 #include <GEOMImpl_IImportExport.hxx>
32 #include <GEOMImpl_Types.hxx>
34 #include <GEOM_Function.hxx>
35 #include <GEOM_PythonDump.hxx>
37 #include <Basics_OCCTVersion.hxx>
39 #include "utilities.h"
41 #include <Utils_ExceptHandlers.hxx>
43 #include <TFunction_DriverTable.hxx>
44 #include <TFunction_Driver.hxx>
45 #include <TFunction_Logbook.hxx>
46 #include <TDF_Tool.hxx>
49 #include <TopoDS_Vertex.hxx>
50 #include <BRep_Builder.hxx>
51 #include <BRep_Tool.hxx>
52 #include <BRepTools.hxx>
55 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
56 #include <TColStd_HArray1OfByte.hxx>
58 #include <TDataStd_HArray1OfByte.hxx>
61 #include <Standard_Failure.hxx>
62 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
64 //=============================================================================
68 //=============================================================================
69 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
70 : GEOM_IOperations(theEngine, theDocID)
72 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
75 //=============================================================================
79 //=============================================================================
80 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
82 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
85 //=============================================================================
89 //=============================================================================
90 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
94 if (theOriginal.IsNull()) return NULL;
96 //Add a new Copy object
97 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
99 //Add a Copy function for creation a copy object
100 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
102 //Check if the function is set correctly
103 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
105 GEOMImpl_ICopy aCI(aFunction);
107 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
108 if (aRefFunction.IsNull()) return NULL;
110 aCI.SetOriginal(aRefFunction);
112 //Compute the Copy value
114 #if OCC_VERSION_LARGE > 0x06010000
117 if (!GetSolver()->ComputeFunction(aFunction)) {
118 SetErrorCode("Copy driver failed");
122 catch (Standard_Failure) {
123 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
124 SetErrorCode(aFail->GetMessageString());
128 //Make a Python command
129 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
135 //=============================================================================
139 //=============================================================================
140 void GEOMImpl_IInsertOperations::Export
141 (const Handle(GEOM_Object) theOriginal,
142 const TCollection_AsciiString& theFileName,
143 const TCollection_AsciiString& theFormatName)
147 if (theOriginal.IsNull()) return;
149 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
150 if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
152 //Add a new result object
153 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
155 //Add an Export function
156 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
157 if (aFunction.IsNull()) return;
159 //Check if the function is set correctly
160 if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
162 Handle(TCollection_HAsciiString) aHLibName;
163 if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
166 TCollection_AsciiString aLibName = aHLibName->String();
169 GEOMImpl_IImportExport aCI (aFunction);
170 aCI.SetOriginal(aRefFunction);
171 aCI.SetFileName(theFileName);
172 aCI.SetFormatName(theFormatName);
173 aCI.SetPluginName(aLibName);
177 #if OCC_VERSION_LARGE > 0x06010000
180 if (!GetSolver()->ComputeFunction(aFunction)) {
181 SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
185 catch (Standard_Failure) {
186 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
187 SetErrorCode(aFail->GetMessageString());
191 //Make a Python command
192 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
193 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
198 //=============================================================================
202 //=============================================================================
203 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
204 (const TCollection_AsciiString& theFileName,
205 const TCollection_AsciiString& theFormatName)
209 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
211 //Add a new result object
212 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
214 //Add an Import function
215 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
216 if (aFunction.IsNull()) return result;
218 //Check if the function is set correctly
219 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
221 Handle(TCollection_HAsciiString) aHLibName;
222 if (!IsSupported(Standard_True, theFormatName.SubString(1,4), aHLibName)) {
225 TCollection_AsciiString aLibName = aHLibName->String();
228 GEOMImpl_IImportExport aCI (aFunction);
229 aCI.SetFileName(theFileName);
230 aCI.SetFormatName(theFormatName);
231 aCI.SetPluginName(aLibName);
235 #if OCC_VERSION_LARGE > 0x06010000
238 if (!GetSolver()->ComputeFunction(aFunction)) {
239 SetErrorCode("Import driver failed");
243 catch (Standard_Failure) {
244 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
245 SetErrorCode(aFail->GetMessageString());
249 //Make a Python command
250 if (theFormatName != "IGES_UNIT") {
251 GEOM::TPythonDump pd (aFunction);
252 if (theFormatName == "BREP")
253 pd << result << " = geompy.ImportBREP(\"" << theFileName.ToCString() << "\")";
254 else if (theFormatName == "IGES")
255 pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\")";
256 else if (theFormatName == "IGES_SCALE")
257 pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\", True)";
258 else if (theFormatName == "STEP")
259 pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\")";
260 else if (theFormatName == "STEP_SCALE")
261 pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\", True)";
263 pd << result << " = geompy.ImportFile(\""
264 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
271 if (theFormatName == "IGES_UNIT") {
272 TopoDS_Shape S = aFunction->GetValue();
273 TopoDS_Vertex V = TopoDS::Vertex(S);
274 gp_Pnt P = BRep_Tool::Pnt(V);
275 double scale = P.X();
276 TCollection_AsciiString aUnitName = "UNIT_M";
277 if (fabs(scale-0.01) < 1.e-6)
278 aUnitName = "UNIT_CM";
279 else if (fabs(scale-0.001) < 1.e-6)
280 aUnitName = "UNIT_MM";
281 //cout<<"IIO: aUnitName = "<<aUnitName.ToCString()<<endl;
282 SetErrorCode(aUnitName);
289 //=============================================================================
293 //=============================================================================
294 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
295 (const TCollection_AsciiString& theFileName,
296 const TCollection_AsciiString& theFormatName,
297 const TCollection_AsciiString& theParameterName)
301 TCollection_AsciiString aValue, anError;
303 if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
305 Handle(TCollection_HAsciiString) aHLibName;
306 if (!IsSupported(Standard_True, theFormatName.SubString(1,4), aHLibName)) {
309 TCollection_AsciiString aLibName = aHLibName->String();
311 aValue = GEOMImpl_ImportDriver::ReadValue(theFileName, aLibName, theParameterName, anError);
312 if (anError.IsEmpty())
315 SetErrorCode(anError.ToCString());
320 //=============================================================================
324 //=============================================================================
325 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
326 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
327 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
329 if (theFormats.IsNull())
330 theFormats = new TColStd_HSequenceOfAsciiString;
334 if (thePatterns.IsNull())
335 thePatterns = new TColStd_HSequenceOfAsciiString;
337 thePatterns->Clear();
339 if (!InitResMgr()) return Standard_False;
341 // Read Import formats list from install directory
342 if (myResMgr->Find("Import")) {
343 TCollection_AsciiString aFormats (myResMgr->Value("Import"));
344 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
346 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
347 theFormats->Append(aToken);
351 // Read Import formats from user directory
352 if (myResMgrUser->Find("Import")) {
353 TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
354 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
356 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
357 int aLenFormats = theFormats->Length();
358 bool isFound = false;
359 for(int aInd=1;aInd<=aLenFormats;aInd++){
360 if( theFormats->Value(aInd) == aToken){
366 theFormats->Append(aToken);
370 // Read Patterns for each supported format
371 int j = 1, len = theFormats->Length();
372 for (; j <= len; j++) {
373 TCollection_AsciiString aKey, aPattern;
374 aKey = theFormats->Value(j) + ".ImportPattern";
375 if (myResMgr->Find(aKey.ToCString()))
376 aPattern = myResMgr->Value(aKey.ToCString());
377 else if(myResMgrUser->Find(aKey.ToCString()))
378 aPattern = myResMgrUser->Value(aKey.ToCString());
380 aKey = theFormats->Value(j) + ".Pattern";
381 if (myResMgr->Find(aKey.ToCString()))
382 aPattern = myResMgr->Value(aKey.ToCString());
383 else if(myResMgrUser->Find(aKey.ToCString()))
384 aPattern = myResMgrUser->Value(aKey.ToCString());
386 aPattern = theFormats->Value(j);
387 aPattern += " Files ( *.* )";
390 thePatterns->Append(aPattern);
393 return (!theFormats->IsEmpty());
396 //=============================================================================
400 //=============================================================================
401 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
402 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
403 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
405 if (theFormats.IsNull())
406 theFormats = new TColStd_HSequenceOfAsciiString;
410 if (thePatterns.IsNull())
411 thePatterns = new TColStd_HSequenceOfAsciiString;
413 thePatterns->Clear();
415 if (!InitResMgr()) return Standard_False;
417 // Read Export formats list from install directory
418 if (myResMgr->Find("Export")) {
419 TCollection_AsciiString aFormats (myResMgr->Value("Export"));
420 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
422 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
423 theFormats->Append(aToken);
427 // Read Export formats list from user directory
428 if (myResMgrUser->Find("Export")) {
429 TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
430 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
432 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
433 int aLenFormats = theFormats->Length();
434 bool isFound = false;
435 for(int aInd=1;aInd<=aLenFormats;aInd++){
436 if( theFormats->Value(aInd) == aToken){
442 theFormats->Append(aToken);
446 // Read Patterns for each supported format
447 int j = 1, len = theFormats->Length();
448 for (; j <= len; j++) {
449 TCollection_AsciiString aKey, aPattern;
450 aKey = theFormats->Value(j) + ".ExportPattern";
451 if (myResMgr->Find(aKey.ToCString()))
452 aPattern = myResMgr->Value(aKey.ToCString());
453 else if (myResMgrUser->Find(aKey.ToCString()))
454 aPattern = myResMgrUser->Value(aKey.ToCString());
456 aKey = theFormats->Value(j) + ".Pattern";
457 if (myResMgr->Find(aKey.ToCString()))
458 aPattern = myResMgr->Value(aKey.ToCString());
459 else if (myResMgrUser->Find(aKey.ToCString()))
460 aPattern = myResMgrUser->Value(aKey.ToCString());
462 aPattern = theFormats->Value(j);
463 aPattern += " Files ( *.* )";
466 thePatterns->Append(aPattern);
469 return (!theFormats->IsEmpty());
472 //=============================================================================
476 //=============================================================================
477 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
478 (const Standard_Boolean isImport,
479 const TCollection_AsciiString& theFormat,
480 Handle(TCollection_HAsciiString)& theLibName)
482 if (!InitResMgr()) return Standard_False;
484 // Import/Export mode
485 TCollection_AsciiString aMode;
486 //Standard_CString aMode;
487 if (isImport) aMode = "Import";
488 else aMode = "Export";
490 // Read supported formats for the certain mode from install directory
491 if (myResMgr->Find(aMode.ToCString())) {
492 TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
493 if (aFormats.Search(theFormat) > -1) {
494 // Read library name for the supported format
495 TCollection_AsciiString aKey (theFormat);
498 if (myResMgr->Find(aKey.ToCString())) {
499 TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
505 theLibName = new TCollection_HAsciiString (aLibName);
506 return Standard_True;
511 // Read supported formats for the certain mode from user directory
512 if (myResMgrUser->Find(aMode.ToCString())) {
513 TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
514 if (aFormats.Search(theFormat) > -1) {
515 // Read library name for the supported format
516 TCollection_AsciiString aKey (theFormat);
519 if (myResMgrUser->Find(aKey.ToCString())) {
520 TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
521 theLibName = new TCollection_HAsciiString (aLibName);
522 return Standard_True;
527 return Standard_False;
530 //=============================================================================
534 //=============================================================================
535 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
537 bool isResourceFound = false;
538 bool isResourceFoundUser = false;
539 TCollection_AsciiString aUserResDir,aResDir;
541 if (myResMgr.IsNull()) {
542 // Initialize the Resource Manager
543 TCollection_AsciiString aNull;
544 aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
546 aResDir += "\\share\\salome\\resources\\geom";
548 aResDir += "/share/salome/resources/geom";
551 myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
553 isResourceFound = true;
554 if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
555 // instead of complains in Resource_Manager
556 isResourceFound = false;
557 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
560 isResourceFound = true;
562 if (myResMgrUser.IsNull()) {
563 char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
564 TCollection_AsciiString aNull;
571 aUserResDir = getenv("HOME");
573 aUserResDir += "\\.salome\\resources";
575 aUserResDir += "/.salome/resources";
579 myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
581 isResourceFoundUser = true;
583 if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
584 // instead of complains in Resource_Manager
585 isResourceFoundUser = false;
589 isResourceFoundUser = true;
591 if(!isResourceFound && !isResourceFoundUser){
592 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
593 INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
596 return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
597 myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
600 //=============================================================================
604 //=============================================================================
605 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
609 //Add a new result object
610 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
612 //Add a Copy function
613 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
614 if (aFunction.IsNull()) return NULL;
616 //Check if the function is set correctly
617 if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
619 //Read a shape from the stream
622 BRepTools::Read(aShape, theStream, B);
623 if (aShape.IsNull()) {
624 SetErrorCode("RestoreShape error: BREP reading failed");
628 aFunction->SetValue(aShape);
630 //Special dump to avoid restored shapes publication.
631 //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
632 //GEOM::TPythonDump(aFunction) << "#";
634 GEOM::TPythonDump(aFunction) << result
635 << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
642 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
646 if (theTextureFile.IsEmpty()) return 0;
648 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
649 Handle(TColStd_HArray1OfByte) aTexture;
651 Handle(TDataStd_HArray1OfByte) aTexture;
654 FILE* fp = fopen(theTextureFile.ToCString(), "r");
657 std::list<std::string> lines;
661 if ((fgets(buffer, 4096, fp)) == NULL) break;
662 int aLen = strlen(buffer);
663 if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
664 lines.push_back(buffer);
665 maxlen = std::max(maxlen, (int)strlen(buffer));
670 int lenbytes = maxlen/8;
671 if (maxlen%8) lenbytes++;
673 if (lenbytes == 0 || lines.empty())
676 std::list<unsigned char> bytedata;
677 std::list<std::string>::const_iterator it;
678 for (it = lines.begin(); it != lines.end(); ++it) {
679 std::string line = *it;
680 int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
681 for (int i = 0; i < lenline/8; i++) {
682 unsigned char byte = 0;
683 for (int j = 0; j < 8; j++)
684 byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
685 bytedata.push_back(byte);
687 for (int i = lenline/8; i < lenbytes; i++)
688 bytedata.push_back((unsigned char)0);
691 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
694 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
695 aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
697 aTexture = new TDataStd_HArray1OfByte (1, lines.size()*lenbytes);
700 std::list<unsigned char>::iterator bdit;
702 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
703 aTexture->SetValue(i, (Standard_Byte)(*bdit));
705 int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
706 if (aTextureId > 0) SetErrorCode(OK);
710 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
711 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
712 const Handle(TColStd_HArray1OfByte)& theTexture)
714 const Handle(TDataStd_HArray1OfByte)& theTexture)
718 int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
719 if (aTextureId > 0) SetErrorCode(OK);
723 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
724 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
726 Handle(TDataStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
728 int& theWidth, int& theHeight)
732 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
733 Handle(TColStd_HArray1OfByte) aTexture;
735 Handle(TDataStd_HArray1OfByte) aTexture;
738 theWidth = theHeight = 0;
739 TCollection_AsciiString aFileName;
741 if (theTextureId <= 0)
744 aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
746 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
751 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
754 std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());