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