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
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_Tool.hxx>
53 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
54 #include <TColStd_HArray1OfByte.hxx>
56 #include <TDataStd_HArray1OfByte.hxx>
59 #include <Standard_Failure.hxx>
60 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
62 //=============================================================================
66 //=============================================================================
68 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
69 : GEOM_IOperations(theEngine, theDocID)
71 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
74 //=============================================================================
78 //=============================================================================
80 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
82 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
87 //=============================================================================
91 //=============================================================================
92 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) theOriginal)
96 if (theOriginal.IsNull()) return NULL;
98 //Add a new Copy object
99 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
101 //Add a Copy function for creation a copy object
102 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
104 //Check if the function is set correctly
105 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
107 GEOMImpl_ICopy aCI(aFunction);
109 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
110 if (aRefFunction.IsNull()) return NULL;
112 aCI.SetOriginal(aRefFunction);
114 //Compute the Copy value
116 #if OCC_VERSION_LARGE > 0x06010000
119 if (!GetSolver()->ComputeFunction(aFunction)) {
120 SetErrorCode("Copy driver failed");
124 catch (Standard_Failure) {
125 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
126 SetErrorCode(aFail->GetMessageString());
130 //Make a Python command
131 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
137 //=============================================================================
141 //=============================================================================
142 void GEOMImpl_IInsertOperations::Export
143 (const Handle(GEOM_Object) theOriginal,
144 const TCollection_AsciiString& theFileName,
145 const TCollection_AsciiString& theFormatName)
149 if (theOriginal.IsNull()) return;
151 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
152 if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
154 //Add a new result object
155 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
157 //Add an Export function
158 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
159 if (aFunction.IsNull()) return;
161 //Check if the function is set correctly
162 if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
164 Handle(TCollection_HAsciiString) aHLibName;
165 if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
168 TCollection_AsciiString aLibName = aHLibName->String();
171 GEOMImpl_IImportExport aCI (aFunction);
172 aCI.SetOriginal(aRefFunction);
173 aCI.SetFileName(theFileName);
174 aCI.SetFormatName(theFormatName);
175 aCI.SetPluginName(aLibName);
179 #if OCC_VERSION_LARGE > 0x06010000
182 if (!GetSolver()->ComputeFunction(aFunction)) {
183 SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
187 catch (Standard_Failure) {
188 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
189 SetErrorCode(aFail->GetMessageString());
193 //Make a Python command
194 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
195 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
200 //=============================================================================
204 //=============================================================================
205 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
206 (const TCollection_AsciiString& theFileName,
207 const TCollection_AsciiString& theFormatName)
211 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
213 //Add a new result object
214 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
216 //Add an Import function
217 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
218 if (aFunction.IsNull()) return result;
220 //Check if the function is set correctly
221 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
223 Handle(TCollection_HAsciiString) aHLibName;
224 if (!IsSupported(Standard_True, theFormatName.SubString(1,4), aHLibName)) {
227 TCollection_AsciiString aLibName = aHLibName->String();
230 GEOMImpl_IImportExport aCI (aFunction);
231 aCI.SetFileName(theFileName);
232 aCI.SetFormatName(theFormatName);
233 aCI.SetPluginName(aLibName);
234 //cout<<"IIO: theFormatName = "<<theFormatName.ToCString()<<endl;
238 #if OCC_VERSION_LARGE > 0x06010000
241 if (!GetSolver()->ComputeFunction(aFunction)) {
242 SetErrorCode("Import driver failed");
246 catch (Standard_Failure) {
247 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
248 SetErrorCode(aFail->GetMessageString());
252 //Make a Python command
253 if( theFormatName != "IGES_UNIT" ) {
254 GEOM::TPythonDump(aFunction) << result << " = geompy.ImportFile(\""
255 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
260 if( theFormatName == "IGES_UNIT" ) {
261 TopoDS_Shape S = aFunction->GetValue();
262 TopoDS_Vertex V = TopoDS::Vertex(S);
263 gp_Pnt P = BRep_Tool::Pnt(V);
264 double scale = P.X();
265 TCollection_AsciiString aUnitName = "UNIT_M";
266 if( fabs(scale-0.01) < 1.e-6 )
267 aUnitName = "UNIT_CM";
268 else if( fabs(scale-0.001) < 1.e-6 )
269 aUnitName = "UNIT_MM";
270 //cout<<"IIO: aUnitName = "<<aUnitName.ToCString()<<endl;
271 SetErrorCode(aUnitName);
277 //=============================================================================
281 //=============================================================================
282 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
283 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
284 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
286 if (theFormats.IsNull())
287 theFormats = new TColStd_HSequenceOfAsciiString;
291 if (thePatterns.IsNull())
292 thePatterns = new TColStd_HSequenceOfAsciiString;
294 thePatterns->Clear();
296 if (!InitResMgr()) return Standard_False;
298 // Read Import formats list from install directory
299 if (myResMgr->Find("Import")) {
300 TCollection_AsciiString aFormats (myResMgr->Value("Import"));
301 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
303 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
304 theFormats->Append(aToken);
308 // Read Import formats from user directory
309 if (myResMgrUser->Find("Import")) {
310 TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
311 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
313 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
314 int aLenFormats = theFormats->Length();
315 bool isFound = false;
316 for(int aInd=1;aInd<=aLenFormats;aInd++){
317 if( theFormats->Value(aInd) == aToken){
323 theFormats->Append(aToken);
327 // Read Patterns for each supported format
328 int j = 1, len = theFormats->Length();
329 for (; j <= len; j++) {
330 TCollection_AsciiString aKey, aPattern;
331 aKey = theFormats->Value(j) + ".ImportPattern";
332 if (myResMgr->Find(aKey.ToCString()))
333 aPattern = myResMgr->Value(aKey.ToCString());
334 else if(myResMgrUser->Find(aKey.ToCString()))
335 aPattern = myResMgrUser->Value(aKey.ToCString());
337 aKey = theFormats->Value(j) + ".Pattern";
338 if (myResMgr->Find(aKey.ToCString()))
339 aPattern = myResMgr->Value(aKey.ToCString());
340 else if(myResMgrUser->Find(aKey.ToCString()))
341 aPattern = myResMgrUser->Value(aKey.ToCString());
343 aPattern = theFormats->Value(j);
344 aPattern += " Files ( *.* )";
347 thePatterns->Append(aPattern);
350 return (!theFormats->IsEmpty());
353 //=============================================================================
357 //=============================================================================
358 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
359 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
360 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
362 if (theFormats.IsNull())
363 theFormats = new TColStd_HSequenceOfAsciiString;
367 if (thePatterns.IsNull())
368 thePatterns = new TColStd_HSequenceOfAsciiString;
370 thePatterns->Clear();
372 if (!InitResMgr()) return Standard_False;
374 // Read Export formats list from install directory
375 if (myResMgr->Find("Export")) {
376 TCollection_AsciiString aFormats (myResMgr->Value("Export"));
377 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
379 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
380 theFormats->Append(aToken);
384 // Read Export formats list from user directory
385 if (myResMgrUser->Find("Export")) {
386 TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
387 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
389 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
390 int aLenFormats = theFormats->Length();
391 bool isFound = false;
392 for(int aInd=1;aInd<=aLenFormats;aInd++){
393 if( theFormats->Value(aInd) == aToken){
399 theFormats->Append(aToken);
403 // Read Patterns for each supported format
404 int j = 1, len = theFormats->Length();
405 for (; j <= len; j++) {
406 TCollection_AsciiString aKey, aPattern;
407 aKey = theFormats->Value(j) + ".ExportPattern";
408 if (myResMgr->Find(aKey.ToCString()))
409 aPattern = myResMgr->Value(aKey.ToCString());
410 else if (myResMgrUser->Find(aKey.ToCString()))
411 aPattern = myResMgrUser->Value(aKey.ToCString());
413 aKey = theFormats->Value(j) + ".Pattern";
414 if (myResMgr->Find(aKey.ToCString()))
415 aPattern = myResMgr->Value(aKey.ToCString());
416 else if (myResMgrUser->Find(aKey.ToCString()))
417 aPattern = myResMgrUser->Value(aKey.ToCString());
419 aPattern = theFormats->Value(j);
420 aPattern += " Files ( *.* )";
423 thePatterns->Append(aPattern);
426 return (!theFormats->IsEmpty());
429 //=============================================================================
433 //=============================================================================
434 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
435 (const Standard_Boolean isImport,
436 const TCollection_AsciiString& theFormat,
437 Handle(TCollection_HAsciiString)& theLibName)
439 if (!InitResMgr()) return Standard_False;
441 // Import/Export mode
442 TCollection_AsciiString aMode;
443 //Standard_CString aMode;
444 if (isImport) aMode = "Import";
445 else aMode = "Export";
447 // Read supported formats for the certain mode from install directory
448 if (myResMgr->Find(aMode.ToCString())) {
449 TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
450 if (aFormats.Search(theFormat) > -1) {
451 // Read library name for the supported format
452 TCollection_AsciiString aKey (theFormat);
455 if (myResMgr->Find(aKey.ToCString())) {
456 TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
462 theLibName = new TCollection_HAsciiString (aLibName);
463 return Standard_True;
468 // Read supported formats for the certain mode from user directory
469 if (myResMgrUser->Find(aMode.ToCString())) {
470 TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
471 if (aFormats.Search(theFormat) > -1) {
472 // Read library name for the supported format
473 TCollection_AsciiString aKey (theFormat);
476 if (myResMgrUser->Find(aKey.ToCString())) {
477 TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
478 theLibName = new TCollection_HAsciiString (aLibName);
479 return Standard_True;
484 return Standard_False;
487 //=============================================================================
491 //=============================================================================
492 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
494 bool isResourceFound = false;
495 bool isResourceFoundUser = false;
496 TCollection_AsciiString aUserResDir,aResDir;
498 if (myResMgr.IsNull()) {
499 // Initialize the Resource Manager
500 TCollection_AsciiString aNull;
501 aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
503 aResDir += "\\share\\salome\\resources\\geom";
505 aResDir += "/share/salome/resources/geom";
508 myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
510 isResourceFound = true;
511 if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
512 // instead of complains in Resource_Manager
513 isResourceFound = false;
514 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
517 isResourceFound = true;
519 if (myResMgrUser.IsNull()) {
520 char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
521 TCollection_AsciiString aNull;
528 aUserResDir = getenv("HOME");
530 aUserResDir += "\\.salome\\resources";
532 aUserResDir += "/.salome/resources";
536 myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
538 isResourceFoundUser = true;
540 if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
541 // instead of complains in Resource_Manager
542 isResourceFoundUser = false;
546 isResourceFoundUser = true;
548 if(!isResourceFound && !isResourceFoundUser){
549 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
550 INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
553 return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
554 myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
557 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
561 if (theTextureFile.IsEmpty()) return 0;
563 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
564 Handle(TColStd_HArray1OfByte) aTexture;
566 Handle(TDataStd_HArray1OfByte) aTexture;
569 FILE* fp = fopen(theTextureFile.ToCString(), "r");
572 std::list<std::string> lines;
576 if ((fgets(buffer, 4096, fp)) == NULL) break;
577 int aLen = strlen(buffer);
578 if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
579 lines.push_back(buffer);
580 maxlen = std::max(maxlen, (int)strlen(buffer));
585 int lenbytes = maxlen/8;
586 if (maxlen%8) lenbytes++;
588 if (lenbytes == 0 || lines.empty())
591 std::list<unsigned char> bytedata;
592 std::list<std::string>::const_iterator it;
593 for (it = lines.begin(); it != lines.end(); ++it) {
594 std::string line = *it;
595 int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
596 for (int i = 0; i < lenline/8; i++) {
597 unsigned char byte = 0;
598 for (int j = 0; j < 8; j++)
599 byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
600 bytedata.push_back(byte);
602 for (int i = lenline/8; i < lenbytes; i++)
603 bytedata.push_back((unsigned char)0);
606 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
609 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
610 aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
612 aTexture = new TDataStd_HArray1OfByte (1, lines.size()*lenbytes);
615 std::list<unsigned char>::iterator bdit;
617 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
618 aTexture->SetValue(i, (Standard_Byte)(*bdit));
620 int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
621 if (aTextureId > 0) SetErrorCode(OK);
625 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
626 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
627 const Handle(TColStd_HArray1OfByte)& theTexture)
629 const Handle(TDataStd_HArray1OfByte)& theTexture)
633 int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
634 if (aTextureId > 0) SetErrorCode(OK);
638 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
639 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
641 Handle(TDataStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
643 int& theWidth, int& theHeight)
647 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
648 Handle(TColStd_HArray1OfByte) aTexture;
650 Handle(TDataStd_HArray1OfByte) aTexture;
653 theWidth = theHeight = 0;
654 TCollection_AsciiString aFileName;
656 if (theTextureId <= 0)
659 aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
661 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
666 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
669 std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());