1 // Copyright (C) 2007-2012 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 <GEOMImpl_CopyDriver.hxx>
27 #include <GEOMImpl_ExportDriver.hxx>
28 #include <GEOMImpl_ImportDriver.hxx>
29 #include <GEOMImpl_ICopy.hxx>
30 #include <GEOMImpl_IImportExport.hxx>
31 #include <GEOMImpl_Types.hxx>
33 #include <GEOM_Function.hxx>
34 #include <GEOM_PythonDump.hxx>
36 #include <Basics_OCCTVersion.hxx>
38 #include "utilities.h"
40 #include <Utils_ExceptHandlers.hxx>
42 #include <TFunction_DriverTable.hxx>
43 #include <TFunction_Driver.hxx>
44 #include <TFunction_Logbook.hxx>
45 #include <TDF_Tool.hxx>
48 #include <TopoDS_Vertex.hxx>
49 #include <BRep_Builder.hxx>
50 #include <BRep_Tool.hxx>
51 #include <BRepTools.hxx>
54 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
55 #include <TColStd_HArray1OfByte.hxx>
57 #include <TDataStd_HArray1OfByte.hxx>
60 #include <Standard_Failure.hxx>
61 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
63 //=============================================================================
67 //=============================================================================
68 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
69 : GEOM_IOperations(theEngine, theDocID)
71 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
74 //=============================================================================
78 //=============================================================================
79 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
81 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
84 //=============================================================================
88 //=============================================================================
89 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
93 if (theOriginal.IsNull()) return NULL;
95 //Add a new Copy object
96 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
98 //Add a Copy function for creation a copy object
99 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
101 //Check if the function is set correctly
102 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
104 GEOMImpl_ICopy aCI(aFunction);
106 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
107 if (aRefFunction.IsNull()) return NULL;
109 aCI.SetOriginal(aRefFunction);
111 //Compute the Copy value
113 #if OCC_VERSION_LARGE > 0x06010000
116 if (!GetSolver()->ComputeFunction(aFunction)) {
117 SetErrorCode("Copy driver failed");
121 catch (Standard_Failure) {
122 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
123 SetErrorCode(aFail->GetMessageString());
127 //Make a Python command
128 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
134 //=============================================================================
138 //=============================================================================
139 void GEOMImpl_IInsertOperations::Export
140 (const Handle(GEOM_Object) theOriginal,
141 const TCollection_AsciiString& theFileName,
142 const TCollection_AsciiString& theFormatName)
146 if (theOriginal.IsNull()) return;
148 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
149 if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
151 //Add a new result object
152 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
154 //Add an Export function
155 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
156 if (aFunction.IsNull()) return;
158 //Check if the function is set correctly
159 if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
161 Handle(TCollection_HAsciiString) aHLibName;
162 if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
165 TCollection_AsciiString aLibName = aHLibName->String();
168 GEOMImpl_IImportExport aCI (aFunction);
169 aCI.SetOriginal(aRefFunction);
170 aCI.SetFileName(theFileName);
171 aCI.SetFormatName(theFormatName);
172 aCI.SetPluginName(aLibName);
176 #if OCC_VERSION_LARGE > 0x06010000
179 if (!GetSolver()->ComputeFunction(aFunction)) {
180 SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
184 catch (Standard_Failure) {
185 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
186 SetErrorCode(aFail->GetMessageString());
190 //Make a Python command
191 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
192 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
197 //=============================================================================
201 //=============================================================================
202 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
203 (const TCollection_AsciiString& theFileName,
204 const TCollection_AsciiString& theFormatName)
208 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
210 //Add a new result object
211 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
213 //Add an Import function
214 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
215 if (aFunction.IsNull()) return result;
217 //Check if the function is set correctly
218 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
220 Handle(TCollection_HAsciiString) aHLibName;
221 if (!IsSupported(Standard_True, theFormatName.SubString(1,4), aHLibName)) {
224 TCollection_AsciiString aLibName = aHLibName->String();
227 GEOMImpl_IImportExport aCI (aFunction);
228 aCI.SetFileName(theFileName);
229 aCI.SetFormatName(theFormatName);
230 aCI.SetPluginName(aLibName);
234 #if OCC_VERSION_LARGE > 0x06010000
237 if (!GetSolver()->ComputeFunction(aFunction)) {
238 SetErrorCode("Import driver failed");
242 catch (Standard_Failure) {
243 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
244 SetErrorCode(aFail->GetMessageString());
248 //Make a Python command
249 if( theFormatName != "IGES_UNIT" ) {
250 GEOM::TPythonDump(aFunction) << result << " = geompy.ImportFile(\""
251 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
257 if (theFormatName == "IGES_UNIT") {
258 TopoDS_Shape S = aFunction->GetValue();
259 TopoDS_Vertex V = TopoDS::Vertex(S);
260 gp_Pnt P = BRep_Tool::Pnt(V);
261 double scale = P.X();
262 TCollection_AsciiString aUnitName = "UNIT_M";
263 if( fabs(scale-0.01) < 1.e-6 )
264 aUnitName = "UNIT_CM";
265 else if( fabs(scale-0.001) < 1.e-6 )
266 aUnitName = "UNIT_MM";
267 //cout<<"IIO: aUnitName = "<<aUnitName.ToCString()<<endl;
268 SetErrorCode(aUnitName);
275 //=============================================================================
279 //=============================================================================
280 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
281 (const TCollection_AsciiString& theFileName,
282 const TCollection_AsciiString& theFormatName,
283 const TCollection_AsciiString& theParameterName)
287 TCollection_AsciiString aValue, anError;
289 if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
291 Handle(TCollection_HAsciiString) aHLibName;
292 if (!IsSupported(Standard_True, theFormatName.SubString(1,4), aHLibName)) {
295 TCollection_AsciiString aLibName = aHLibName->String();
297 aValue = GEOMImpl_ImportDriver::ReadValue(theFileName, aLibName, theParameterName, anError);
298 if (anError.IsEmpty())
301 SetErrorCode(anError.ToCString());
306 //=============================================================================
310 //=============================================================================
311 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
312 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
313 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
315 if (theFormats.IsNull())
316 theFormats = new TColStd_HSequenceOfAsciiString;
320 if (thePatterns.IsNull())
321 thePatterns = new TColStd_HSequenceOfAsciiString;
323 thePatterns->Clear();
325 if (!InitResMgr()) return Standard_False;
327 // Read Import formats list from install directory
328 if (myResMgr->Find("Import")) {
329 TCollection_AsciiString aFormats (myResMgr->Value("Import"));
330 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
332 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
333 theFormats->Append(aToken);
337 // Read Import formats from user directory
338 if (myResMgrUser->Find("Import")) {
339 TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
340 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
342 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
343 int aLenFormats = theFormats->Length();
344 bool isFound = false;
345 for(int aInd=1;aInd<=aLenFormats;aInd++){
346 if( theFormats->Value(aInd) == aToken){
352 theFormats->Append(aToken);
356 // Read Patterns for each supported format
357 int j = 1, len = theFormats->Length();
358 for (; j <= len; j++) {
359 TCollection_AsciiString aKey, aPattern;
360 aKey = theFormats->Value(j) + ".ImportPattern";
361 if (myResMgr->Find(aKey.ToCString()))
362 aPattern = myResMgr->Value(aKey.ToCString());
363 else if(myResMgrUser->Find(aKey.ToCString()))
364 aPattern = myResMgrUser->Value(aKey.ToCString());
366 aKey = theFormats->Value(j) + ".Pattern";
367 if (myResMgr->Find(aKey.ToCString()))
368 aPattern = myResMgr->Value(aKey.ToCString());
369 else if(myResMgrUser->Find(aKey.ToCString()))
370 aPattern = myResMgrUser->Value(aKey.ToCString());
372 aPattern = theFormats->Value(j);
373 aPattern += " Files ( *.* )";
376 thePatterns->Append(aPattern);
379 return (!theFormats->IsEmpty());
382 //=============================================================================
386 //=============================================================================
387 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
388 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
389 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
391 if (theFormats.IsNull())
392 theFormats = new TColStd_HSequenceOfAsciiString;
396 if (thePatterns.IsNull())
397 thePatterns = new TColStd_HSequenceOfAsciiString;
399 thePatterns->Clear();
401 if (!InitResMgr()) return Standard_False;
403 // Read Export formats list from install directory
404 if (myResMgr->Find("Export")) {
405 TCollection_AsciiString aFormats (myResMgr->Value("Export"));
406 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
408 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
409 theFormats->Append(aToken);
413 // Read Export formats list from user directory
414 if (myResMgrUser->Find("Export")) {
415 TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
416 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
418 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
419 int aLenFormats = theFormats->Length();
420 bool isFound = false;
421 for(int aInd=1;aInd<=aLenFormats;aInd++){
422 if( theFormats->Value(aInd) == aToken){
428 theFormats->Append(aToken);
432 // Read Patterns for each supported format
433 int j = 1, len = theFormats->Length();
434 for (; j <= len; j++) {
435 TCollection_AsciiString aKey, aPattern;
436 aKey = theFormats->Value(j) + ".ExportPattern";
437 if (myResMgr->Find(aKey.ToCString()))
438 aPattern = myResMgr->Value(aKey.ToCString());
439 else if (myResMgrUser->Find(aKey.ToCString()))
440 aPattern = myResMgrUser->Value(aKey.ToCString());
442 aKey = theFormats->Value(j) + ".Pattern";
443 if (myResMgr->Find(aKey.ToCString()))
444 aPattern = myResMgr->Value(aKey.ToCString());
445 else if (myResMgrUser->Find(aKey.ToCString()))
446 aPattern = myResMgrUser->Value(aKey.ToCString());
448 aPattern = theFormats->Value(j);
449 aPattern += " Files ( *.* )";
452 thePatterns->Append(aPattern);
455 return (!theFormats->IsEmpty());
458 //=============================================================================
462 //=============================================================================
463 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
464 (const Standard_Boolean isImport,
465 const TCollection_AsciiString& theFormat,
466 Handle(TCollection_HAsciiString)& theLibName)
468 if (!InitResMgr()) return Standard_False;
470 // Import/Export mode
471 TCollection_AsciiString aMode;
472 //Standard_CString aMode;
473 if (isImport) aMode = "Import";
474 else aMode = "Export";
476 // Read supported formats for the certain mode from install directory
477 if (myResMgr->Find(aMode.ToCString())) {
478 TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
479 if (aFormats.Search(theFormat) > -1) {
480 // Read library name for the supported format
481 TCollection_AsciiString aKey (theFormat);
484 if (myResMgr->Find(aKey.ToCString())) {
485 TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
491 theLibName = new TCollection_HAsciiString (aLibName);
492 return Standard_True;
497 // Read supported formats for the certain mode from user directory
498 if (myResMgrUser->Find(aMode.ToCString())) {
499 TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
500 if (aFormats.Search(theFormat) > -1) {
501 // Read library name for the supported format
502 TCollection_AsciiString aKey (theFormat);
505 if (myResMgrUser->Find(aKey.ToCString())) {
506 TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
507 theLibName = new TCollection_HAsciiString (aLibName);
508 return Standard_True;
513 return Standard_False;
516 //=============================================================================
520 //=============================================================================
521 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
523 bool isResourceFound = false;
524 bool isResourceFoundUser = false;
525 TCollection_AsciiString aUserResDir,aResDir;
527 if (myResMgr.IsNull()) {
528 // Initialize the Resource Manager
529 TCollection_AsciiString aNull;
530 aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
532 aResDir += "\\share\\salome\\resources\\geom";
534 aResDir += "/share/salome/resources/geom";
537 myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
539 isResourceFound = true;
540 if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
541 // instead of complains in Resource_Manager
542 isResourceFound = false;
543 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
546 isResourceFound = true;
548 if (myResMgrUser.IsNull()) {
549 char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
550 TCollection_AsciiString aNull;
557 aUserResDir = getenv("HOME");
559 aUserResDir += "\\.salome\\resources";
561 aUserResDir += "/.salome/resources";
565 myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
567 isResourceFoundUser = true;
569 if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
570 // instead of complains in Resource_Manager
571 isResourceFoundUser = false;
575 isResourceFoundUser = true;
577 if(!isResourceFound && !isResourceFoundUser){
578 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
579 INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
582 return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
583 myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
586 //=============================================================================
590 //=============================================================================
591 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
595 //Add a new result object
596 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
598 //Add a Copy function
599 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
600 if (aFunction.IsNull()) return NULL;
602 //Check if the function is set correctly
603 if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
605 //Read a shape from the stream
608 BRepTools::Read(aShape, theStream, B);
609 if (aShape.IsNull()) {
610 SetErrorCode("RestoreShape error: BREP reading failed");
614 aFunction->SetValue(aShape);
616 //Special dump to avoid restored shapes publication.
617 //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
618 //GEOM::TPythonDump(aFunction) << "#";
620 GEOM::TPythonDump(aFunction) << result
621 << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
628 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
632 if (theTextureFile.IsEmpty()) return 0;
634 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
635 Handle(TColStd_HArray1OfByte) aTexture;
637 Handle(TDataStd_HArray1OfByte) aTexture;
640 FILE* fp = fopen(theTextureFile.ToCString(), "r");
643 std::list<std::string> lines;
647 if ((fgets(buffer, 4096, fp)) == NULL) break;
648 int aLen = strlen(buffer);
649 if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
650 lines.push_back(buffer);
651 maxlen = std::max(maxlen, (int)strlen(buffer));
656 int lenbytes = maxlen/8;
657 if (maxlen%8) lenbytes++;
659 if (lenbytes == 0 || lines.empty())
662 std::list<unsigned char> bytedata;
663 std::list<std::string>::const_iterator it;
664 for (it = lines.begin(); it != lines.end(); ++it) {
665 std::string line = *it;
666 int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
667 for (int i = 0; i < lenline/8; i++) {
668 unsigned char byte = 0;
669 for (int j = 0; j < 8; j++)
670 byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
671 bytedata.push_back(byte);
673 for (int i = lenline/8; i < lenbytes; i++)
674 bytedata.push_back((unsigned char)0);
677 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
680 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
681 aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
683 aTexture = new TDataStd_HArray1OfByte (1, lines.size()*lenbytes);
686 std::list<unsigned char>::iterator bdit;
688 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
689 aTexture->SetValue(i, (Standard_Byte)(*bdit));
691 int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
692 if (aTextureId > 0) SetErrorCode(OK);
696 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
697 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
698 const Handle(TColStd_HArray1OfByte)& theTexture)
700 const Handle(TDataStd_HArray1OfByte)& theTexture)
704 int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
705 if (aTextureId > 0) SetErrorCode(OK);
709 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
710 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
712 Handle(TDataStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
714 int& theWidth, int& theHeight)
718 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
719 Handle(TColStd_HArray1OfByte) aTexture;
721 Handle(TDataStd_HArray1OfByte) aTexture;
724 theWidth = theHeight = 0;
725 TCollection_AsciiString aFileName;
727 if (theTextureId <= 0)
730 aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
732 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
737 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
740 std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());