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>
51 #include <TDataStd_HArray1OfByte.hxx>
53 #include <Standard_Failure.hxx>
54 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
56 //=============================================================================
60 //=============================================================================
62 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
63 : GEOM_IOperations(theEngine, theDocID)
65 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
68 //=============================================================================
72 //=============================================================================
74 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
76 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
81 //=============================================================================
85 //=============================================================================
86 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) theOriginal)
90 if (theOriginal.IsNull()) return NULL;
92 //Add a new Copy object
93 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
95 //Add a Copy function for creation a copy object
96 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
98 //Check if the function is set correctly
99 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
101 GEOMImpl_ICopy aCI(aFunction);
103 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
104 if (aRefFunction.IsNull()) return NULL;
106 aCI.SetOriginal(aRefFunction);
108 //Compute the Copy value
110 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
113 if (!GetSolver()->ComputeFunction(aFunction)) {
114 SetErrorCode("Copy driver failed");
118 catch (Standard_Failure) {
119 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
120 SetErrorCode(aFail->GetMessageString());
124 //Make a Python command
125 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
131 //=============================================================================
135 //=============================================================================
136 void GEOMImpl_IInsertOperations::Export
137 (const Handle(GEOM_Object) theOriginal,
138 const TCollection_AsciiString& theFileName,
139 const TCollection_AsciiString& theFormatName)
143 if (theOriginal.IsNull()) return;
145 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
146 if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
148 //Add an Export function
149 Handle(GEOM_Function) aFunction = theOriginal->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
150 if (aFunction.IsNull()) return;
152 //Check if the function is set correctly
153 if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
155 Handle(TCollection_HAsciiString) aHLibName;
156 if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
159 TCollection_AsciiString aLibName = aHLibName->String();
162 GEOMImpl_IImportExport aCI (aFunction);
163 aCI.SetOriginal(aRefFunction);
164 aCI.SetFileName(theFileName);
165 aCI.SetFormatName(theFormatName);
166 aCI.SetPluginName(aLibName);
170 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
173 if (!GetSolver()->ComputeFunction(aFunction)) {
174 SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
178 catch (Standard_Failure) {
179 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
180 SetErrorCode(aFail->GetMessageString());
184 //Make a Python command
185 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
186 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
191 //=============================================================================
195 //=============================================================================
196 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
197 (const TCollection_AsciiString& theFileName,
198 const TCollection_AsciiString& theFormatName)
202 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
204 //Add a new result object
205 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
207 //Add an Import function
208 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
209 if (aFunction.IsNull()) return result;
211 //Check if the function is set correctly
212 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
214 Handle(TCollection_HAsciiString) aHLibName;
215 if (!IsSupported(Standard_True, theFormatName.SubString(1,4), aHLibName)) {
218 TCollection_AsciiString aLibName = aHLibName->String();
221 GEOMImpl_IImportExport aCI (aFunction);
222 aCI.SetFileName(theFileName);
223 aCI.SetFormatName(theFormatName);
224 aCI.SetPluginName(aLibName);
225 //cout<<"IIO: theFormatName = "<<theFormatName.ToCString()<<endl;
229 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
232 if (!GetSolver()->ComputeFunction(aFunction)) {
233 SetErrorCode("Import driver failed");
237 catch (Standard_Failure) {
238 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
239 SetErrorCode(aFail->GetMessageString());
243 //Make a Python command
244 if( theFormatName != "IGES_UNIT" ) {
245 GEOM::TPythonDump(aFunction) << result << " = geompy.Import(\""
246 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
251 if( theFormatName == "IGES_UNIT" ) {
252 TopoDS_Shape S = aFunction->GetValue();
253 TopoDS_Vertex V = TopoDS::Vertex(S);
254 gp_Pnt P = BRep_Tool::Pnt(V);
255 double scale = P.X();
256 TCollection_AsciiString aUnitName = "UNIT_M";
257 if( fabs(scale-0.01) < 1.e-6 )
258 aUnitName = "UNIT_CM";
259 else if( fabs(scale-0.001) < 1.e-6 )
260 aUnitName = "UNIT_MM";
261 //cout<<"IIO: aUnitName = "<<aUnitName.ToCString()<<endl;
262 SetErrorCode(aUnitName);
268 //=============================================================================
272 //=============================================================================
273 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
274 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
275 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
277 if (theFormats.IsNull())
278 theFormats = new TColStd_HSequenceOfAsciiString;
282 if (thePatterns.IsNull())
283 thePatterns = new TColStd_HSequenceOfAsciiString;
285 thePatterns->Clear();
287 if (!InitResMgr()) return Standard_False;
289 // Read Import formats list from install directory
290 if (myResMgr->Find("Import")) {
291 TCollection_AsciiString aFormats (myResMgr->Value("Import"));
292 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
294 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
295 theFormats->Append(aToken);
299 // Read Import formats from user directory
300 if (myResMgrUser->Find("Import")) {
301 TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
302 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
304 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
305 int aLenFormats = theFormats->Length();
306 bool isFound = false;
307 for(int aInd=1;aInd<=aLenFormats;aInd++){
308 if( theFormats->Value(aInd) == aToken){
314 theFormats->Append(aToken);
318 // Read Patterns for each supported format
319 int j = 1, len = theFormats->Length();
320 for (; j <= len; j++) {
321 TCollection_AsciiString aKey, aPattern;
322 aKey = theFormats->Value(j) + ".ImportPattern";
323 if (myResMgr->Find(aKey.ToCString()))
324 aPattern = myResMgr->Value(aKey.ToCString());
325 else if(myResMgrUser->Find(aKey.ToCString()))
326 aPattern = myResMgrUser->Value(aKey.ToCString());
328 aKey = theFormats->Value(j) + ".Pattern";
329 if (myResMgr->Find(aKey.ToCString()))
330 aPattern = myResMgr->Value(aKey.ToCString());
331 else if(myResMgrUser->Find(aKey.ToCString()))
332 aPattern = myResMgrUser->Value(aKey.ToCString());
334 aPattern = theFormats->Value(j);
335 aPattern += " Files ( *.* )";
338 thePatterns->Append(aPattern);
341 return (!theFormats->IsEmpty());
344 //=============================================================================
348 //=============================================================================
349 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
350 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
351 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
353 if (theFormats.IsNull())
354 theFormats = new TColStd_HSequenceOfAsciiString;
358 if (thePatterns.IsNull())
359 thePatterns = new TColStd_HSequenceOfAsciiString;
361 thePatterns->Clear();
363 if (!InitResMgr()) return Standard_False;
365 // Read Export formats list from install directory
366 if (myResMgr->Find("Export")) {
367 TCollection_AsciiString aFormats (myResMgr->Value("Export"));
368 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
370 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
371 theFormats->Append(aToken);
375 // Read Export formats list from user directory
376 if (myResMgrUser->Find("Export")) {
377 TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
378 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
380 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
381 int aLenFormats = theFormats->Length();
382 bool isFound = false;
383 for(int aInd=1;aInd<=aLenFormats;aInd++){
384 if( theFormats->Value(aInd) == aToken){
390 theFormats->Append(aToken);
394 // Read Patterns for each supported format
395 int j = 1, len = theFormats->Length();
396 for (; j <= len; j++) {
397 TCollection_AsciiString aKey, aPattern;
398 aKey = theFormats->Value(j) + ".ExportPattern";
399 if (myResMgr->Find(aKey.ToCString()))
400 aPattern = myResMgr->Value(aKey.ToCString());
401 else if (myResMgrUser->Find(aKey.ToCString()))
402 aPattern = myResMgrUser->Value(aKey.ToCString());
404 aKey = theFormats->Value(j) + ".Pattern";
405 if (myResMgr->Find(aKey.ToCString()))
406 aPattern = myResMgr->Value(aKey.ToCString());
407 else if (myResMgrUser->Find(aKey.ToCString()))
408 aPattern = myResMgrUser->Value(aKey.ToCString());
410 aPattern = theFormats->Value(j);
411 aPattern += " Files ( *.* )";
414 thePatterns->Append(aPattern);
417 return (!theFormats->IsEmpty());
420 //=============================================================================
424 //=============================================================================
425 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
426 (const Standard_Boolean isImport,
427 const TCollection_AsciiString& theFormat,
428 Handle(TCollection_HAsciiString)& theLibName)
430 if (!InitResMgr()) return Standard_False;
432 // Import/Export mode
433 TCollection_AsciiString aMode;
434 //Standard_CString aMode;
435 if (isImport) aMode = "Import";
436 else aMode = "Export";
438 // Read supported formats for the certain mode from install directory
439 if (myResMgr->Find(aMode.ToCString())) {
440 TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
441 if (aFormats.Search(theFormat) > -1) {
442 // Read library name for the supported format
443 TCollection_AsciiString aKey (theFormat);
446 if (myResMgr->Find(aKey.ToCString())) {
447 TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
453 theLibName = new TCollection_HAsciiString (aLibName);
454 return Standard_True;
459 // Read supported formats for the certain mode from user directory
460 if (myResMgrUser->Find(aMode.ToCString())) {
461 TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
462 if (aFormats.Search(theFormat) > -1) {
463 // Read library name for the supported format
464 TCollection_AsciiString aKey (theFormat);
467 if (myResMgrUser->Find(aKey.ToCString())) {
468 TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
469 theLibName = new TCollection_HAsciiString (aLibName);
470 return Standard_True;
475 return Standard_False;
478 //=============================================================================
482 //=============================================================================
483 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
485 bool isResourceFound = false;
486 bool isResourceFoundUser = false;
487 TCollection_AsciiString aUserResDir,aResDir;
489 if (myResMgr.IsNull()) {
490 // Initialize the Resource Manager
491 TCollection_AsciiString aNull;
492 aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
494 aResDir += "\\share\\salome\\resources\\geom";
496 aResDir += "/share/salome/resources/geom";
499 myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
501 isResourceFound = true;
502 if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
503 // instead of complains in Resource_Manager
504 isResourceFound = false;
505 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
508 isResourceFound = true;
510 if (myResMgrUser.IsNull()) {
511 char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
512 TCollection_AsciiString aNull;
519 aUserResDir = getenv("HOME");
521 aUserResDir += "\\.salome\\resources";
523 aUserResDir += "/.salome/resources";
527 myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
529 isResourceFoundUser = true;
531 if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
532 // instead of complains in Resource_Manager
533 isResourceFoundUser = false;
537 isResourceFoundUser = true;
539 if(!isResourceFound && !isResourceFoundUser){
540 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
541 INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
544 return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
545 myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
548 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
552 if (theTextureFile.IsEmpty()) return 0;
554 Handle(TDataStd_HArray1OfByte) aTexture;
556 FILE* fp = fopen(theTextureFile.ToCString(), "r");
559 std::list<std::string> lines;
563 if ((fgets(buffer, 4096, fp)) == NULL) break;
564 int aLen = strlen(buffer);
565 if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
566 lines.push_back(buffer);
567 maxlen = std::max(maxlen, (int)strlen(buffer));
572 int lenbytes = maxlen/8;
573 if (maxlen%8) lenbytes++;
575 if (lenbytes == 0 || lines.empty())
578 std::list<unsigned char> bytedata;
579 std::list<std::string>::const_iterator it;
580 for (it = lines.begin(); it != lines.end(); ++it) {
581 std::string line = *it;
582 int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
583 for (int i = 0; i < lenline/8; i++) {
584 unsigned char byte = 0;
585 for (int j = 0; j < 8; j++)
586 byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
587 bytedata.push_back(byte);
589 for (int i = lenline/8; i < lenbytes; i++)
590 bytedata.push_back((unsigned char)0);
593 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
596 aTexture = new TDataStd_HArray1OfByte(1, lines.size()*lenbytes);
597 std::list<unsigned char>::iterator bdit;
599 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
600 aTexture->SetValue(i, (Standard_Byte)(*bdit));
602 int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
603 if (aTextureId > 0) SetErrorCode(OK);
607 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
608 const Handle(TDataStd_HArray1OfByte)& theTexture)
611 int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
612 if (aTextureId > 0) SetErrorCode(OK);
616 Handle(TDataStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
617 int& theWidth, int& theHeight)
621 Handle(TDataStd_HArray1OfByte) aTexture;
622 theWidth = theHeight = 0;
623 TCollection_AsciiString aFileName;
625 if (theTextureId <= 0)
628 aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
630 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
635 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
638 std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());