Salome HOME
Merge from V6_main (04/10/2012)
[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(aFunction) << result << " = geompy.ImportFile(\""
251       << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
252   }
253
254   SetErrorCode(OK);
255
256   // OLD CODE: begin
257   if (theFormatName == "IGES_UNIT") {
258     TopoDS_Shape S = aFunction->GetValue();
259     TopoDS_Vertex V = TopoDS::Vertex(S);
260     gp_Pnt P = BRep_Tool::Pnt(V);
261     double scale = P.X();
262     TCollection_AsciiString aUnitName = "UNIT_M";
263     if( fabs(scale-0.01) < 1.e-6 )
264       aUnitName = "UNIT_CM";
265     else if( fabs(scale-0.001) < 1.e-6 )
266       aUnitName = "UNIT_MM";
267     //cout<<"IIO: aUnitName = "<<aUnitName.ToCString()<<endl;
268     SetErrorCode(aUnitName);
269   }
270   // OLD CODE: end
271
272   return result;
273 }
274
275 //=============================================================================
276 /*!
277  *  ReadValue
278  */
279 //=============================================================================
280 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
281                                  (const TCollection_AsciiString& theFileName,
282                                   const TCollection_AsciiString& theFormatName,
283                                   const TCollection_AsciiString& theParameterName)
284 {
285   SetErrorCode(KO);
286
287   TCollection_AsciiString aValue, anError;
288
289   if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
290
291   Handle(TCollection_HAsciiString) aHLibName;
292   if (!IsSupported(Standard_True, theFormatName.SubString(1,4), aHLibName)) {
293     return aValue;
294   }
295   TCollection_AsciiString aLibName = aHLibName->String();
296
297   aValue = GEOMImpl_ImportDriver::ReadValue(theFileName, aLibName, theParameterName, anError);
298   if (anError.IsEmpty())
299     SetErrorCode(OK);
300   else
301     SetErrorCode(anError.ToCString());
302
303   return aValue;
304 }
305
306 //=============================================================================
307 /*!
308  *  ImportTranslators
309  */
310 //=============================================================================
311 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
312                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
313                       Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
314 {
315   if (theFormats.IsNull())
316     theFormats = new TColStd_HSequenceOfAsciiString;
317   else
318     theFormats->Clear();
319
320   if (thePatterns.IsNull())
321     thePatterns = new TColStd_HSequenceOfAsciiString;
322   else
323     thePatterns->Clear();
324
325   if (!InitResMgr()) return Standard_False;
326
327   // Read Import formats list from install directory
328   if (myResMgr->Find("Import")) {
329     TCollection_AsciiString aFormats (myResMgr->Value("Import"));
330     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
331     int i = 1;
332     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
333       theFormats->Append(aToken);
334     }
335   }
336
337   // Read Import formats from user directory
338   if (myResMgrUser->Find("Import")) {
339     TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
340     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
341     int i = 1;
342     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
343       int aLenFormats = theFormats->Length();
344       bool isFound = false;
345       for(int aInd=1;aInd<=aLenFormats;aInd++){
346         if( theFormats->Value(aInd) == aToken){
347           isFound = true;
348           break;
349         }
350       }
351       if(!isFound)
352         theFormats->Append(aToken);
353     }
354   }
355
356   // Read Patterns for each supported format
357   int j = 1, len = theFormats->Length();
358   for (; j <= len; j++) {
359     TCollection_AsciiString aKey, aPattern;
360     aKey = theFormats->Value(j) + ".ImportPattern";
361     if (myResMgr->Find(aKey.ToCString()))
362       aPattern = myResMgr->Value(aKey.ToCString());
363     else if(myResMgrUser->Find(aKey.ToCString()))
364       aPattern = myResMgrUser->Value(aKey.ToCString());
365     else {
366       aKey = theFormats->Value(j) + ".Pattern";
367       if (myResMgr->Find(aKey.ToCString()))
368         aPattern = myResMgr->Value(aKey.ToCString());
369       else if(myResMgrUser->Find(aKey.ToCString()))
370         aPattern = myResMgrUser->Value(aKey.ToCString());
371       else {
372         aPattern = theFormats->Value(j);
373         aPattern += " Files ( *.* )";
374       }
375     }
376     thePatterns->Append(aPattern);
377   }
378
379   return (!theFormats->IsEmpty());
380 }
381
382 //=============================================================================
383 /*!
384  *  ExportTranslators
385  */
386 //=============================================================================
387 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
388                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
389                       Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
390 {
391   if (theFormats.IsNull())
392     theFormats = new TColStd_HSequenceOfAsciiString;
393   else
394     theFormats->Clear();
395
396   if (thePatterns.IsNull())
397     thePatterns = new TColStd_HSequenceOfAsciiString;
398   else
399     thePatterns->Clear();
400
401   if (!InitResMgr()) return Standard_False;
402
403   // Read Export formats list from install directory
404   if (myResMgr->Find("Export")) {
405     TCollection_AsciiString aFormats (myResMgr->Value("Export"));
406     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
407     int i = 1;
408     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
409       theFormats->Append(aToken);
410     }
411   }
412
413   // Read Export formats list from user directory
414   if (myResMgrUser->Find("Export")) {
415     TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
416     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
417     int i = 1;
418     for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
419       int aLenFormats = theFormats->Length();
420       bool isFound = false;
421       for(int aInd=1;aInd<=aLenFormats;aInd++){
422         if( theFormats->Value(aInd) == aToken){
423           isFound = true;
424           break;
425         }
426       }
427       if(!isFound)
428         theFormats->Append(aToken);
429     }
430   }
431
432   // Read Patterns for each supported format
433   int j = 1, len = theFormats->Length();
434   for (; j <= len; j++) {
435     TCollection_AsciiString aKey, aPattern;
436     aKey = theFormats->Value(j) + ".ExportPattern";
437     if (myResMgr->Find(aKey.ToCString()))
438       aPattern = myResMgr->Value(aKey.ToCString());
439     else if (myResMgrUser->Find(aKey.ToCString()))
440       aPattern = myResMgrUser->Value(aKey.ToCString());
441     else {
442       aKey = theFormats->Value(j) + ".Pattern";
443       if (myResMgr->Find(aKey.ToCString()))
444         aPattern = myResMgr->Value(aKey.ToCString());
445       else if (myResMgrUser->Find(aKey.ToCString()))
446         aPattern = myResMgrUser->Value(aKey.ToCString());
447       else {
448         aPattern = theFormats->Value(j);
449         aPattern += " Files ( *.* )";
450       }
451     }
452     thePatterns->Append(aPattern);
453   }
454
455   return (!theFormats->IsEmpty());
456 }
457
458 //=============================================================================
459 /*!
460  *  IsSupported
461  */
462 //=============================================================================
463 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
464                             (const Standard_Boolean isImport,
465                              const TCollection_AsciiString& theFormat,
466                              Handle(TCollection_HAsciiString)& theLibName)
467 {
468   if (!InitResMgr()) return Standard_False;
469
470   // Import/Export mode
471   TCollection_AsciiString aMode;
472   //Standard_CString aMode;
473   if (isImport) aMode = "Import";
474   else aMode = "Export";
475
476   // Read supported formats for the certain mode from install directory
477   if (myResMgr->Find(aMode.ToCString())) {
478     TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
479     if (aFormats.Search(theFormat) > -1) {
480       // Read library name for the supported format
481       TCollection_AsciiString aKey (theFormat);
482       aKey += ".";
483       aKey += aMode;
484       if (myResMgr->Find(aKey.ToCString())) {
485         TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));        
486 #ifndef WNT
487         aLibName += ".so";     
488 #else
489         aLibName += ".dll";
490 #endif
491         theLibName = new TCollection_HAsciiString (aLibName);
492         return Standard_True;
493       }
494     }
495   }
496   
497   // Read supported formats for the certain mode from user directory
498   if (myResMgrUser->Find(aMode.ToCString())) {
499     TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
500     if (aFormats.Search(theFormat) > -1) {
501       // Read library name for the supported format
502       TCollection_AsciiString aKey (theFormat);
503       aKey += ".";
504       aKey += aMode;
505       if (myResMgrUser->Find(aKey.ToCString())) {
506         TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
507         theLibName = new TCollection_HAsciiString (aLibName);
508         return Standard_True;
509       }
510     }
511   }
512   
513   return Standard_False;
514 }
515
516 //=============================================================================
517 /*!
518  *  InitResMgr
519  */
520 //=============================================================================
521 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
522 {
523   bool isResourceFound     = false;
524   bool isResourceFoundUser = false;
525   TCollection_AsciiString aUserResDir,aResDir;
526   
527   if (myResMgr.IsNull()) {
528     // Initialize the Resource Manager
529     TCollection_AsciiString aNull;
530     aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
531 #ifdef WNT
532     aResDir += "\\share\\salome\\resources\\geom";
533 #else
534     aResDir += "/share/salome/resources/geom";
535 #endif
536     
537     myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
538
539     isResourceFound = true;
540     if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
541       // instead of complains in Resource_Manager
542       isResourceFound = false;
543       INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
544     }
545   } else
546     isResourceFound = true;
547
548   if (myResMgrUser.IsNull()) {
549     char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
550     TCollection_AsciiString aNull;
551     if ( dir )
552     {
553       aUserResDir = dir;
554     }
555     else
556     {
557       aUserResDir = getenv("HOME");
558 #ifdef WNT
559       aUserResDir += "\\.salome\\resources";
560 #else
561       aUserResDir += "/.salome/resources";
562 #endif
563     }
564
565     myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
566
567     isResourceFoundUser = true;
568     
569     if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
570       // instead of complains in Resource_Manager
571       isResourceFoundUser = false;
572     }
573       
574   } else
575     isResourceFoundUser = true;
576     
577   if(!isResourceFound && !isResourceFoundUser){
578     INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
579     INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
580   }
581
582   return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
583            myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
584 }
585
586 //=============================================================================
587 /*!
588  *  RestoreShape
589  */
590 //=============================================================================
591 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
592 {
593   SetErrorCode(KO);
594
595   //Add a new result object
596   Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
597
598   //Add a Copy function
599   Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
600   if (aFunction.IsNull()) return NULL;
601
602   //Check if the function is set correctly
603   if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
604
605   //Read a shape from the stream
606   TopoDS_Shape aShape;
607   BRep_Builder B;
608   BRepTools::Read(aShape, theStream, B);
609   if (aShape.IsNull()) {
610     SetErrorCode("RestoreShape error: BREP reading failed");
611   }
612
613   //Set function value
614   aFunction->SetValue(aShape);
615
616   //Special dump to avoid restored shapes publication.
617   //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
618   //GEOM::TPythonDump(aFunction) << "#";
619
620   GEOM::TPythonDump(aFunction) << result
621     << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
622
623   SetErrorCode(OK);
624
625   return result;
626 }
627
628 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
629 {
630   SetErrorCode(KO);
631
632   if (theTextureFile.IsEmpty()) return 0;
633
634 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
635   Handle(TColStd_HArray1OfByte) aTexture;
636 #else
637   Handle(TDataStd_HArray1OfByte) aTexture;
638 #endif
639
640   FILE* fp = fopen(theTextureFile.ToCString(), "r");
641   if (!fp) return 0;
642
643   std::list<std::string> lines;
644   char buffer[4096];
645   int maxlen = 0;
646   while (!feof(fp)) {
647     if ((fgets(buffer, 4096, fp)) == NULL) break;
648     int aLen = strlen(buffer);
649     if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
650     lines.push_back(buffer);
651     maxlen = std::max(maxlen, (int)strlen(buffer));
652   }
653
654   fclose(fp);
655
656   int lenbytes = maxlen/8;
657   if (maxlen%8) lenbytes++;
658
659   if (lenbytes == 0 || lines.empty())
660     return 0;
661
662   std::list<unsigned char> bytedata;
663   std::list<std::string>::const_iterator it;
664   for (it = lines.begin(); it != lines.end(); ++it) {
665     std::string line = *it;
666     int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
667     for (int i = 0; i < lenline/8; i++) {
668       unsigned char byte = 0;
669       for (int j = 0; j < 8; j++)
670         byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
671       bytedata.push_back(byte);
672     }
673     for (int i = lenline/8; i < lenbytes; i++)
674       bytedata.push_back((unsigned char)0);
675   }
676
677   if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
678     return 0;
679
680 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
681   aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
682 #else
683   aTexture = new TDataStd_HArray1OfByte (1, lines.size()*lenbytes);
684 #endif
685
686   std::list<unsigned char>::iterator bdit;
687   int i;
688   for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
689     aTexture->SetValue(i, (Standard_Byte)(*bdit));
690
691   int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
692   if (aTextureId > 0) SetErrorCode(OK);
693   return aTextureId;
694 }
695   
696 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight, 
697 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
698                                            const Handle(TColStd_HArray1OfByte)& theTexture)
699 #else
700                                            const Handle(TDataStd_HArray1OfByte)& theTexture)
701 #endif
702 {
703   SetErrorCode(KO);
704   int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
705   if (aTextureId > 0) SetErrorCode(OK);
706   return aTextureId;
707 }
708
709 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
710 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
711 #else
712 Handle(TDataStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
713 #endif
714                                                                       int& theWidth, int& theHeight)
715 {
716   SetErrorCode(KO);
717   
718 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
719   Handle(TColStd_HArray1OfByte) aTexture;
720 #else
721   Handle(TDataStd_HArray1OfByte) aTexture;
722 #endif
723
724   theWidth = theHeight = 0;
725   TCollection_AsciiString aFileName;
726
727   if (theTextureId <= 0)
728     return aTexture;
729
730   aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
731
732   if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
733
734   return aTexture;
735 }
736
737 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
738 {
739   SetErrorCode(KO);
740   std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());
741   SetErrorCode(OK);
742   return id_list;
743 }