Salome HOME
4fecfbca9deeea85fbc54815c2a649dfa388237e
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IInsertOperations.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21
22 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_IInsertOperations.hxx>
25
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>
32
33 #include <GEOM_Function.hxx>
34 #include <GEOM_PythonDump.hxx>
35
36 #include <Basics_OCCTVersion.hxx>
37
38 #include "utilities.h"
39 #include <OpUtil.hxx>
40 #include <Utils_ExceptHandlers.hxx>
41
42 #include <TFunction_DriverTable.hxx>
43 #include <TFunction_Driver.hxx>
44 #include <TFunction_Logbook.hxx>
45 #include <TDF_Tool.hxx>
46
47 #include <TopoDS.hxx>
48 #include <TopoDS_Vertex.hxx>
49 #include <BRep_Builder.hxx>
50 #include <BRep_Tool.hxx>
51 #include <BRepTools.hxx>
52 #include <gp_Pnt.hxx>
53
54 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
55 #include <TColStd_HArray1OfByte.hxx>
56 #else
57 #include <TDataStd_HArray1OfByte.hxx>
58 #endif
59
60 #include <Standard_Failure.hxx>
61 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
62
63 //=============================================================================
64 /*!
65  *  constructor
66  */
67 //=============================================================================
68 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
69 : GEOM_IOperations(theEngine, theDocID)
70 {
71   MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
72 }
73
74 //=============================================================================
75 /*!
76  *  destructor
77  */
78 //=============================================================================
79 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
80 {
81   MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
82 }
83
84 //=============================================================================
85 /*!
86  *  MakeCopy
87  */
88 //=============================================================================
89 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
90 {
91   SetErrorCode(KO);
92
93   if (theOriginal.IsNull()) return NULL;
94
95   //Add a new Copy object
96   Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
97
98   //Add a Copy function for creation a copy object
99   Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
100
101   //Check if the function is set correctly
102   if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
103
104   GEOMImpl_ICopy aCI(aFunction);
105
106   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
107   if (aRefFunction.IsNull()) return NULL;
108
109   aCI.SetOriginal(aRefFunction);
110
111   //Compute the Copy value
112   try {
113 #if OCC_VERSION_LARGE > 0x06010000
114     OCC_CATCH_SIGNALS;
115 #endif
116     if (!GetSolver()->ComputeFunction(aFunction)) {
117       SetErrorCode("Copy driver failed");
118       return NULL;
119     }
120   }
121   catch (Standard_Failure) {
122     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
123     SetErrorCode(aFail->GetMessageString());
124     return NULL;
125   }
126
127   //Make a Python command
128   GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
129
130   SetErrorCode(OK);
131   return aCopy;
132 }
133
134 //=============================================================================
135 /*!
136  *  Export
137  */
138 //=============================================================================
139 void GEOMImpl_IInsertOperations::Export
140                      (const Handle(GEOM_Object)      theOriginal,
141                       const TCollection_AsciiString& theFileName,
142                       const TCollection_AsciiString& theFormatName)
143 {
144   SetErrorCode(KO);
145
146   if (theOriginal.IsNull()) return;
147
148   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
149   if (aRefFunction.IsNull()) return;  //There is no function which creates an object to be exported
150
151   //Add a new result object
152   Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
153
154   //Add an Export function
155   Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
156   if (aFunction.IsNull()) return;
157
158   //Check if the function is set correctly
159   if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
160
161   Handle(TCollection_HAsciiString) aHLibName;
162   if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
163     return;
164   }
165   TCollection_AsciiString aLibName = aHLibName->String();
166
167   //Set parameters
168   GEOMImpl_IImportExport aCI (aFunction);
169   aCI.SetOriginal(aRefFunction);
170   aCI.SetFileName(theFileName);
171   aCI.SetFormatName(theFormatName);
172   aCI.SetPluginName(aLibName);
173
174   //Perform the Export
175   try {
176 #if OCC_VERSION_LARGE > 0x06010000
177     OCC_CATCH_SIGNALS;
178 #endif
179     if (!GetSolver()->ComputeFunction(aFunction)) {
180       SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
181       return;
182     }
183   }
184   catch (Standard_Failure) {
185     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
186     SetErrorCode(aFail->GetMessageString());
187     return;
188   }
189
190   //Make a Python command
191   GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
192     << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
193
194   SetErrorCode(OK);
195 }
196
197 //=============================================================================
198 /*!
199  *  Import
200  */
201 //=============================================================================
202 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
203                                  (const TCollection_AsciiString& theFileName,
204                                   const TCollection_AsciiString& theFormatName)
205 {
206   SetErrorCode(KO);
207
208   if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
209
210   //Add a new result object
211   Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
212
213   //Add an Import function
214   Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
215   if (aFunction.IsNull()) return result;
216
217   //Check if the function is set correctly
218   if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
219
220   Handle(TCollection_HAsciiString) aHLibName;
221   if (!IsSupported(Standard_True, theFormatName.SubString(1,4), aHLibName)) {
222     return result;
223   }
224   TCollection_AsciiString aLibName = aHLibName->String();
225
226   //Set parameters
227   GEOMImpl_IImportExport aCI (aFunction);
228   aCI.SetFileName(theFileName);
229   aCI.SetFormatName(theFormatName);
230   aCI.SetPluginName(aLibName);
231
232   //Perform the Import
233   try {
234 #if OCC_VERSION_LARGE > 0x06010000
235     OCC_CATCH_SIGNALS;
236 #endif
237     if (!GetSolver()->ComputeFunction(aFunction)) {
238       SetErrorCode("Import driver failed");
239       return NULL;
240     }
241   }
242   catch (Standard_Failure) {
243     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
244     SetErrorCode(aFail->GetMessageString());
245     return NULL;
246   }
247
248   //Make a Python command
249   if (theFormatName != "IGES_UNIT") {
250     GEOM::TPythonDump pd (aFunction);
251     if (theFormatName == "BREP")
252       pd << result << " = geompy.ImportBREP(\"" << theFileName.ToCString() << "\")";
253     else if (theFormatName == "IGES")
254       pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\")";
255     else if (theFormatName == "IGES_SCALE")
256       pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\", True)";
257     else if (theFormatName == "STEP")
258       pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\")";
259     else if (theFormatName == "STEP_SCALE")
260       pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\", True)";
261     else {
262       pd << result << " = geompy.ImportFile(\""
263          << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
264     }
265   }
266
267   SetErrorCode(OK);
268
269   // OLD CODE: begin
270   if (theFormatName == "IGES_UNIT") {
271     TopoDS_Shape S = aFunction->GetValue();
272     TopoDS_Vertex V = TopoDS::Vertex(S);
273     gp_Pnt P = BRep_Tool::Pnt(V);
274     double scale = P.X();
275     TCollection_AsciiString aUnitName = "UNIT_M";
276     if (fabs(scale-0.01) < 1.e-6)
277       aUnitName = "UNIT_CM";
278     else if (fabs(scale-0.001) < 1.e-6)
279       aUnitName = "UNIT_MM";
280     //cout<<"IIO: aUnitName = "<<aUnitName.ToCString()<<endl;
281     SetErrorCode(aUnitName);
282   }
283   // OLD CODE: end
284
285   return result;
286 }
287
288 //=============================================================================
289 /*!
290  *  ReadValue
291  */
292 //=============================================================================
293 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
294                                  (const TCollection_AsciiString& theFileName,
295                                   const TCollection_AsciiString& theFormatName,
296                                   const TCollection_AsciiString& theParameterName)
297 {
298   SetErrorCode(KO);
299
300   TCollection_AsciiString aValue, anError;
301
302   if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
303
304   Handle(TCollection_HAsciiString) aHLibName;
305   if (!IsSupported(Standard_True, theFormatName.SubString(1,4), aHLibName)) {
306     return aValue;
307   }
308   TCollection_AsciiString aLibName = aHLibName->String();
309
310   aValue = GEOMImpl_ImportDriver::ReadValue(theFileName, aLibName, theParameterName, anError);
311   if (anError.IsEmpty())
312     SetErrorCode(OK);
313   else
314     SetErrorCode(anError.ToCString());
315
316   return aValue;
317 }
318
319 //=============================================================================
320 /*!
321  *  ImportTranslators
322  */
323 //=============================================================================
324 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
325                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
326                       Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
327 {
328   if (theFormats.IsNull())
329     theFormats = new TColStd_HSequenceOfAsciiString;
330   else
331     theFormats->Clear();
332
333   if (thePatterns.IsNull())
334     thePatterns = new TColStd_HSequenceOfAsciiString;
335   else
336     thePatterns->Clear();
337
338   if (!InitResMgr()) return Standard_False;
339
340   // Read Import formats list from install directory
341   if (myResMgr->Find("Import")) {
342     TCollection_AsciiString aFormats (myResMgr->Value("Import"));
343     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
344     int i = 1;
345     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
346       theFormats->Append(aToken);
347     }
348   }
349
350   // Read Import formats from user directory
351   if (myResMgrUser->Find("Import")) {
352     TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
353     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
354     int i = 1;
355     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
356       int aLenFormats = theFormats->Length();
357       bool isFound = false;
358       for(int aInd=1;aInd<=aLenFormats;aInd++){
359         if( theFormats->Value(aInd) == aToken){
360           isFound = true;
361           break;
362         }
363       }
364       if(!isFound)
365         theFormats->Append(aToken);
366     }
367   }
368
369   // Read Patterns for each supported format
370   int j = 1, len = theFormats->Length();
371   for (; j <= len; j++) {
372     TCollection_AsciiString aKey, aPattern;
373     aKey = theFormats->Value(j) + ".ImportPattern";
374     if (myResMgr->Find(aKey.ToCString()))
375       aPattern = myResMgr->Value(aKey.ToCString());
376     else if(myResMgrUser->Find(aKey.ToCString()))
377       aPattern = myResMgrUser->Value(aKey.ToCString());
378     else {
379       aKey = theFormats->Value(j) + ".Pattern";
380       if (myResMgr->Find(aKey.ToCString()))
381         aPattern = myResMgr->Value(aKey.ToCString());
382       else if(myResMgrUser->Find(aKey.ToCString()))
383         aPattern = myResMgrUser->Value(aKey.ToCString());
384       else {
385         aPattern = theFormats->Value(j);
386         aPattern += " Files ( *.* )";
387       }
388     }
389     thePatterns->Append(aPattern);
390   }
391
392   return (!theFormats->IsEmpty());
393 }
394
395 //=============================================================================
396 /*!
397  *  ExportTranslators
398  */
399 //=============================================================================
400 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
401                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
402                       Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
403 {
404   if (theFormats.IsNull())
405     theFormats = new TColStd_HSequenceOfAsciiString;
406   else
407     theFormats->Clear();
408
409   if (thePatterns.IsNull())
410     thePatterns = new TColStd_HSequenceOfAsciiString;
411   else
412     thePatterns->Clear();
413
414   if (!InitResMgr()) return Standard_False;
415
416   // Read Export formats list from install directory
417   if (myResMgr->Find("Export")) {
418     TCollection_AsciiString aFormats (myResMgr->Value("Export"));
419     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
420     int i = 1;
421     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
422       theFormats->Append(aToken);
423     }
424   }
425
426   // Read Export formats list from user directory
427   if (myResMgrUser->Find("Export")) {
428     TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
429     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
430     int i = 1;
431     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
432       int aLenFormats = theFormats->Length();
433       bool isFound = false;
434       for(int aInd=1;aInd<=aLenFormats;aInd++){
435         if( theFormats->Value(aInd) == aToken){
436           isFound = true;
437           break;
438         }
439       }
440       if(!isFound)
441         theFormats->Append(aToken);
442     }
443   }
444
445   // Read Patterns for each supported format
446   int j = 1, len = theFormats->Length();
447   for (; j <= len; j++) {
448     TCollection_AsciiString aKey, aPattern;
449     aKey = theFormats->Value(j) + ".ExportPattern";
450     if (myResMgr->Find(aKey.ToCString()))
451       aPattern = myResMgr->Value(aKey.ToCString());
452     else if (myResMgrUser->Find(aKey.ToCString()))
453       aPattern = myResMgrUser->Value(aKey.ToCString());
454     else {
455       aKey = theFormats->Value(j) + ".Pattern";
456       if (myResMgr->Find(aKey.ToCString()))
457         aPattern = myResMgr->Value(aKey.ToCString());
458       else if (myResMgrUser->Find(aKey.ToCString()))
459         aPattern = myResMgrUser->Value(aKey.ToCString());
460       else {
461         aPattern = theFormats->Value(j);
462         aPattern += " Files ( *.* )";
463       }
464     }
465     thePatterns->Append(aPattern);
466   }
467
468   return (!theFormats->IsEmpty());
469 }
470
471 //=============================================================================
472 /*!
473  *  IsSupported
474  */
475 //=============================================================================
476 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
477                             (const Standard_Boolean isImport,
478                              const TCollection_AsciiString& theFormat,
479                              Handle(TCollection_HAsciiString)& theLibName)
480 {
481   if (!InitResMgr()) return Standard_False;
482
483   // Import/Export mode
484   TCollection_AsciiString aMode;
485   //Standard_CString aMode;
486   if (isImport) aMode = "Import";
487   else aMode = "Export";
488
489   // Read supported formats for the certain mode from install directory
490   if (myResMgr->Find(aMode.ToCString())) {
491     TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
492     if (aFormats.Search(theFormat) > -1) {
493       // Read library name for the supported format
494       TCollection_AsciiString aKey (theFormat);
495       aKey += ".";
496       aKey += aMode;
497       if (myResMgr->Find(aKey.ToCString())) {
498         TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));        
499 #ifndef WNT
500         aLibName += ".so";     
501 #else
502         aLibName += ".dll";
503 #endif
504         theLibName = new TCollection_HAsciiString (aLibName);
505         return Standard_True;
506       }
507     }
508   }
509   
510   // Read supported formats for the certain mode from user directory
511   if (myResMgrUser->Find(aMode.ToCString())) {
512     TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
513     if (aFormats.Search(theFormat) > -1) {
514       // Read library name for the supported format
515       TCollection_AsciiString aKey (theFormat);
516       aKey += ".";
517       aKey += aMode;
518       if (myResMgrUser->Find(aKey.ToCString())) {
519         TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
520         theLibName = new TCollection_HAsciiString (aLibName);
521         return Standard_True;
522       }
523     }
524   }
525   
526   return Standard_False;
527 }
528
529 //=============================================================================
530 /*!
531  *  InitResMgr
532  */
533 //=============================================================================
534 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
535 {
536   bool isResourceFound     = false;
537   bool isResourceFoundUser = false;
538   TCollection_AsciiString aUserResDir,aResDir;
539   
540   if (myResMgr.IsNull()) {
541     // Initialize the Resource Manager
542     TCollection_AsciiString aNull;
543     aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
544 #ifdef WNT
545     aResDir += "\\share\\salome\\resources\\geom";
546 #else
547     aResDir += "/share/salome/resources/geom";
548 #endif
549     
550     myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
551
552     isResourceFound = true;
553     if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
554       // instead of complains in Resource_Manager
555       isResourceFound = false;
556       INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
557     }
558   } else
559     isResourceFound = true;
560
561   if (myResMgrUser.IsNull()) {
562     char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
563     TCollection_AsciiString aNull;
564     if ( dir )
565     {
566       aUserResDir = dir;
567     }
568     else
569     {
570       aUserResDir = getenv("HOME");
571 #ifdef WNT
572       aUserResDir += "\\.salome\\resources";
573 #else
574       aUserResDir += "/.salome/resources";
575 #endif
576     }
577
578     myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
579
580     isResourceFoundUser = true;
581     
582     if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
583       // instead of complains in Resource_Manager
584       isResourceFoundUser = false;
585     }
586       
587   } else
588     isResourceFoundUser = true;
589     
590   if(!isResourceFound && !isResourceFoundUser){
591     INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
592     INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
593   }
594
595   return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
596            myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
597 }
598
599 //=============================================================================
600 /*!
601  *  RestoreShape
602  */
603 //=============================================================================
604 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
605 {
606   SetErrorCode(KO);
607
608   //Add a new result object
609   Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
610
611   //Add a Copy function
612   Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
613   if (aFunction.IsNull()) return NULL;
614
615   //Check if the function is set correctly
616   if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
617
618   //Read a shape from the stream
619   TopoDS_Shape aShape;
620   BRep_Builder B;
621   BRepTools::Read(aShape, theStream, B);
622   if (aShape.IsNull()) {
623     SetErrorCode("RestoreShape error: BREP reading failed");
624   }
625
626   //Set function value
627   aFunction->SetValue(aShape);
628
629   //Special dump to avoid restored shapes publication.
630   //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
631   //GEOM::TPythonDump(aFunction) << "#";
632
633   GEOM::TPythonDump(aFunction) << result
634     << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
635
636   SetErrorCode(OK);
637
638   return result;
639 }
640
641 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
642 {
643   SetErrorCode(KO);
644
645   if (theTextureFile.IsEmpty()) return 0;
646
647 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
648   Handle(TColStd_HArray1OfByte) aTexture;
649 #else
650   Handle(TDataStd_HArray1OfByte) aTexture;
651 #endif
652
653   FILE* fp = fopen(theTextureFile.ToCString(), "r");
654   if (!fp) return 0;
655
656   std::list<std::string> lines;
657   char buffer[4096];
658   int maxlen = 0;
659   while (!feof(fp)) {
660     if ((fgets(buffer, 4096, fp)) == NULL) break;
661     int aLen = strlen(buffer);
662     if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
663     lines.push_back(buffer);
664     maxlen = std::max(maxlen, (int)strlen(buffer));
665   }
666
667   fclose(fp);
668
669   int lenbytes = maxlen/8;
670   if (maxlen%8) lenbytes++;
671
672   if (lenbytes == 0 || lines.empty())
673     return 0;
674
675   std::list<unsigned char> bytedata;
676   std::list<std::string>::const_iterator it;
677   for (it = lines.begin(); it != lines.end(); ++it) {
678     std::string line = *it;
679     int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
680     for (int i = 0; i < lenline/8; i++) {
681       unsigned char byte = 0;
682       for (int j = 0; j < 8; j++)
683         byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
684       bytedata.push_back(byte);
685     }
686     for (int i = lenline/8; i < lenbytes; i++)
687       bytedata.push_back((unsigned char)0);
688   }
689
690   if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
691     return 0;
692
693 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
694   aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
695 #else
696   aTexture = new TDataStd_HArray1OfByte (1, lines.size()*lenbytes);
697 #endif
698
699   std::list<unsigned char>::iterator bdit;
700   int i;
701   for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
702     aTexture->SetValue(i, (Standard_Byte)(*bdit));
703
704   int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
705   if (aTextureId > 0) SetErrorCode(OK);
706   return aTextureId;
707 }
708   
709 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight, 
710 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
711                                            const Handle(TColStd_HArray1OfByte)& theTexture)
712 #else
713                                            const Handle(TDataStd_HArray1OfByte)& theTexture)
714 #endif
715 {
716   SetErrorCode(KO);
717   int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
718   if (aTextureId > 0) SetErrorCode(OK);
719   return aTextureId;
720 }
721
722 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
723 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
724 #else
725 Handle(TDataStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
726 #endif
727                                                                       int& theWidth, int& theHeight)
728 {
729   SetErrorCode(KO);
730   
731 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
732   Handle(TColStd_HArray1OfByte) aTexture;
733 #else
734   Handle(TDataStd_HArray1OfByte) aTexture;
735 #endif
736
737   theWidth = theHeight = 0;
738   TCollection_AsciiString aFileName;
739
740   if (theTextureId <= 0)
741     return aTexture;
742
743   aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
744
745   if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
746
747   return aTexture;
748 }
749
750 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
751 {
752   SetErrorCode(KO);
753   std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());
754   SetErrorCode(OK);
755   return id_list;
756 }