1 // Copyright (C) 2007-2011 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_Tool.hxx>
52 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
53 #include <TColStd_HArray1OfByte.hxx>
55 #include <TDataStd_HArray1OfByte.hxx>
58 #include <Standard_Failure.hxx>
59 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
61 //=============================================================================
65 //=============================================================================
67 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
68 : GEOM_IOperations(theEngine, theDocID)
70 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
73 //=============================================================================
77 //=============================================================================
79 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
81 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
86 //=============================================================================
90 //=============================================================================
91 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) theOriginal)
95 if (theOriginal.IsNull()) return NULL;
97 //Add a new Copy object
98 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
100 //Add a Copy function for creation a copy object
101 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
103 //Check if the function is set correctly
104 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
106 GEOMImpl_ICopy aCI(aFunction);
108 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
109 if (aRefFunction.IsNull()) return NULL;
111 aCI.SetOriginal(aRefFunction);
113 //Compute the Copy value
115 #if OCC_VERSION_LARGE > 0x06010000
118 if (!GetSolver()->ComputeFunction(aFunction)) {
119 SetErrorCode("Copy driver failed");
123 catch (Standard_Failure) {
124 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
125 SetErrorCode(aFail->GetMessageString());
129 //Make a Python command
130 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
136 //=============================================================================
140 //=============================================================================
141 void GEOMImpl_IInsertOperations::Export
142 (const Handle(GEOM_Object) theOriginal,
143 const TCollection_AsciiString& theFileName,
144 const TCollection_AsciiString& theFormatName)
148 if (theOriginal.IsNull()) return;
150 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
151 if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
153 //Add a new result object
154 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
156 //Add an Export function
157 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
158 if (aFunction.IsNull()) return;
160 //Check if the function is set correctly
161 if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
163 Handle(TCollection_HAsciiString) aHLibName;
164 if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
167 TCollection_AsciiString aLibName = aHLibName->String();
170 GEOMImpl_IImportExport aCI (aFunction);
171 aCI.SetOriginal(aRefFunction);
172 aCI.SetFileName(theFileName);
173 aCI.SetFormatName(theFormatName);
174 aCI.SetPluginName(aLibName);
178 #if OCC_VERSION_LARGE > 0x06010000
181 if (!GetSolver()->ComputeFunction(aFunction)) {
182 SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
186 catch (Standard_Failure) {
187 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
188 SetErrorCode(aFail->GetMessageString());
192 //Make a Python command
193 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
194 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
199 //=============================================================================
203 //=============================================================================
204 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
205 (const TCollection_AsciiString& theFileName,
206 const TCollection_AsciiString& theFormatName)
210 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
212 //Add a new result object
213 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
215 //Add an Import function
216 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
217 if (aFunction.IsNull()) return result;
219 //Check if the function is set correctly
220 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
222 Handle(TCollection_HAsciiString) aHLibName;
223 if (!IsSupported(Standard_True, theFormatName.SubString(1,4), aHLibName)) {
226 TCollection_AsciiString aLibName = aHLibName->String();
229 GEOMImpl_IImportExport aCI (aFunction);
230 aCI.SetFileName(theFileName);
231 aCI.SetFormatName(theFormatName);
232 aCI.SetPluginName(aLibName);
233 //cout<<"IIO: theFormatName = "<<theFormatName.ToCString()<<endl;
237 #if OCC_VERSION_LARGE > 0x06010000
240 if (!GetSolver()->ComputeFunction(aFunction)) {
241 SetErrorCode("Import driver failed");
245 catch (Standard_Failure) {
246 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
247 SetErrorCode(aFail->GetMessageString());
251 //Make a Python command
252 if( theFormatName != "IGES_UNIT" ) {
253 GEOM::TPythonDump(aFunction) << result << " = geompy.ImportFile(\""
254 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
259 if( theFormatName == "IGES_UNIT" ) {
260 TopoDS_Shape S = aFunction->GetValue();
261 TopoDS_Vertex V = TopoDS::Vertex(S);
262 gp_Pnt P = BRep_Tool::Pnt(V);
263 double scale = P.X();
264 TCollection_AsciiString aUnitName = "UNIT_M";
265 if( fabs(scale-0.01) < 1.e-6 )
266 aUnitName = "UNIT_CM";
267 else if( fabs(scale-0.001) < 1.e-6 )
268 aUnitName = "UNIT_MM";
269 //cout<<"IIO: aUnitName = "<<aUnitName.ToCString()<<endl;
270 SetErrorCode(aUnitName);
276 //=============================================================================
280 //=============================================================================
281 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
282 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
283 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
285 if (theFormats.IsNull())
286 theFormats = new TColStd_HSequenceOfAsciiString;
290 if (thePatterns.IsNull())
291 thePatterns = new TColStd_HSequenceOfAsciiString;
293 thePatterns->Clear();
295 if (!InitResMgr()) return Standard_False;
297 // Read Import formats list from install directory
298 if (myResMgr->Find("Import")) {
299 TCollection_AsciiString aFormats (myResMgr->Value("Import"));
300 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
302 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
303 theFormats->Append(aToken);
307 // Read Import formats from user directory
308 if (myResMgrUser->Find("Import")) {
309 TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
310 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
312 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
313 int aLenFormats = theFormats->Length();
314 bool isFound = false;
315 for(int aInd=1;aInd<=aLenFormats;aInd++){
316 if( theFormats->Value(aInd) == aToken){
322 theFormats->Append(aToken);
326 // Read Patterns for each supported format
327 int j = 1, len = theFormats->Length();
328 for (; j <= len; j++) {
329 TCollection_AsciiString aKey, aPattern;
330 aKey = theFormats->Value(j) + ".ImportPattern";
331 if (myResMgr->Find(aKey.ToCString()))
332 aPattern = myResMgr->Value(aKey.ToCString());
333 else if(myResMgrUser->Find(aKey.ToCString()))
334 aPattern = myResMgrUser->Value(aKey.ToCString());
336 aKey = theFormats->Value(j) + ".Pattern";
337 if (myResMgr->Find(aKey.ToCString()))
338 aPattern = myResMgr->Value(aKey.ToCString());
339 else if(myResMgrUser->Find(aKey.ToCString()))
340 aPattern = myResMgrUser->Value(aKey.ToCString());
342 aPattern = theFormats->Value(j);
343 aPattern += " Files ( *.* )";
346 thePatterns->Append(aPattern);
349 return (!theFormats->IsEmpty());
352 //=============================================================================
356 //=============================================================================
357 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
358 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
359 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
361 if (theFormats.IsNull())
362 theFormats = new TColStd_HSequenceOfAsciiString;
366 if (thePatterns.IsNull())
367 thePatterns = new TColStd_HSequenceOfAsciiString;
369 thePatterns->Clear();
371 if (!InitResMgr()) return Standard_False;
373 // Read Export formats list from install directory
374 if (myResMgr->Find("Export")) {
375 TCollection_AsciiString aFormats (myResMgr->Value("Export"));
376 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
378 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
379 theFormats->Append(aToken);
383 // Read Export formats list from user directory
384 if (myResMgrUser->Find("Export")) {
385 TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
386 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
388 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
389 int aLenFormats = theFormats->Length();
390 bool isFound = false;
391 for(int aInd=1;aInd<=aLenFormats;aInd++){
392 if( theFormats->Value(aInd) == aToken){
398 theFormats->Append(aToken);
402 // Read Patterns for each supported format
403 int j = 1, len = theFormats->Length();
404 for (; j <= len; j++) {
405 TCollection_AsciiString aKey, aPattern;
406 aKey = theFormats->Value(j) + ".ExportPattern";
407 if (myResMgr->Find(aKey.ToCString()))
408 aPattern = myResMgr->Value(aKey.ToCString());
409 else if (myResMgrUser->Find(aKey.ToCString()))
410 aPattern = myResMgrUser->Value(aKey.ToCString());
412 aKey = theFormats->Value(j) + ".Pattern";
413 if (myResMgr->Find(aKey.ToCString()))
414 aPattern = myResMgr->Value(aKey.ToCString());
415 else if (myResMgrUser->Find(aKey.ToCString()))
416 aPattern = myResMgrUser->Value(aKey.ToCString());
418 aPattern = theFormats->Value(j);
419 aPattern += " Files ( *.* )";
422 thePatterns->Append(aPattern);
425 return (!theFormats->IsEmpty());
428 //=============================================================================
432 //=============================================================================
433 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
434 (const Standard_Boolean isImport,
435 const TCollection_AsciiString& theFormat,
436 Handle(TCollection_HAsciiString)& theLibName)
438 if (!InitResMgr()) return Standard_False;
440 // Import/Export mode
441 TCollection_AsciiString aMode;
442 //Standard_CString aMode;
443 if (isImport) aMode = "Import";
444 else aMode = "Export";
446 // Read supported formats for the certain mode from install directory
447 if (myResMgr->Find(aMode.ToCString())) {
448 TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
449 if (aFormats.Search(theFormat) > -1) {
450 // Read library name for the supported format
451 TCollection_AsciiString aKey (theFormat);
454 if (myResMgr->Find(aKey.ToCString())) {
455 TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
461 theLibName = new TCollection_HAsciiString (aLibName);
462 return Standard_True;
467 // Read supported formats for the certain mode from user directory
468 if (myResMgrUser->Find(aMode.ToCString())) {
469 TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
470 if (aFormats.Search(theFormat) > -1) {
471 // Read library name for the supported format
472 TCollection_AsciiString aKey (theFormat);
475 if (myResMgrUser->Find(aKey.ToCString())) {
476 TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
477 theLibName = new TCollection_HAsciiString (aLibName);
478 return Standard_True;
483 return Standard_False;
486 //=============================================================================
490 //=============================================================================
491 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
493 bool isResourceFound = false;
494 bool isResourceFoundUser = false;
495 TCollection_AsciiString aUserResDir,aResDir;
497 if (myResMgr.IsNull()) {
498 // Initialize the Resource Manager
499 TCollection_AsciiString aNull;
500 aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
502 aResDir += "\\share\\salome\\resources\\geom";
504 aResDir += "/share/salome/resources/geom";
507 myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
509 isResourceFound = true;
510 if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
511 // instead of complains in Resource_Manager
512 isResourceFound = false;
513 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
516 isResourceFound = true;
518 if (myResMgrUser.IsNull()) {
519 char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
520 TCollection_AsciiString aNull;
527 aUserResDir = getenv("HOME");
529 aUserResDir += "\\.salome\\resources";
531 aUserResDir += "/.salome/resources";
535 myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
537 isResourceFoundUser = true;
539 if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
540 // instead of complains in Resource_Manager
541 isResourceFoundUser = false;
545 isResourceFoundUser = true;
547 if(!isResourceFound && !isResourceFoundUser){
548 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
549 INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
552 return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
553 myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
556 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
560 if (theTextureFile.IsEmpty()) return 0;
562 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
563 Handle(TColStd_HArray1OfByte) aTexture;
565 Handle(TDataStd_HArray1OfByte) aTexture;
568 FILE* fp = fopen(theTextureFile.ToCString(), "r");
571 std::list<std::string> lines;
575 if ((fgets(buffer, 4096, fp)) == NULL) break;
576 int aLen = strlen(buffer);
577 if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
578 lines.push_back(buffer);
579 maxlen = std::max(maxlen, (int)strlen(buffer));
584 int lenbytes = maxlen/8;
585 if (maxlen%8) lenbytes++;
587 if (lenbytes == 0 || lines.empty())
590 std::list<unsigned char> bytedata;
591 std::list<std::string>::const_iterator it;
592 for (it = lines.begin(); it != lines.end(); ++it) {
593 std::string line = *it;
594 int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
595 for (int i = 0; i < lenline/8; i++) {
596 unsigned char byte = 0;
597 for (int j = 0; j < 8; j++)
598 byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
599 bytedata.push_back(byte);
601 for (int i = lenline/8; i < lenbytes; i++)
602 bytedata.push_back((unsigned char)0);
605 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
608 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
609 aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
611 aTexture = new TDataStd_HArray1OfByte (1, lines.size()*lenbytes);
614 std::list<unsigned char>::iterator bdit;
616 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
617 aTexture->SetValue(i, (Standard_Byte)(*bdit));
619 int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
620 if (aTextureId > 0) SetErrorCode(OK);
624 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
625 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
626 const Handle(TColStd_HArray1OfByte)& theTexture)
628 const Handle(TDataStd_HArray1OfByte)& theTexture)
632 int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
633 if (aTextureId > 0) SetErrorCode(OK);
637 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
638 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
640 Handle(TDataStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
642 int& theWidth, int& theHeight)
646 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
647 Handle(TColStd_HArray1OfByte) aTexture;
649 Handle(TDataStd_HArray1OfByte) aTexture;
652 theWidth = theHeight = 0;
653 TCollection_AsciiString aFileName;
655 if (theTextureId <= 0)
658 aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
660 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
665 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
668 std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());