Salome HOME
0021031: EDF 1646 GEOM: Modify Location leads to Position driver failed
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IInsertOperations.cxx
index 102ebba74e2438d4f4918c9c73a8746c06a1b7e6..efb8255fa11b2e6e157c705ffe7817e0fd29dd60 100644 (file)
@@ -1,4 +1,4 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+//  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -19,6 +19,7 @@
 //
 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 #include <Standard_Stream.hxx>
 
 #include <GEOMImpl_IInsertOperations.hxx>
@@ -48,6 +49,7 @@
 #include <TopoDS_Vertex.hxx>
 #include <BRep_Tool.hxx>
 #include <gp_Pnt.hxx>
+#include <TDataStd_HArray1OfByte.hxx>
 
 #include <Standard_Failure.hxx>
 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
@@ -134,8 +136,8 @@ Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) the
 //=============================================================================
 void GEOMImpl_IInsertOperations::Export
                      (const Handle(GEOM_Object)      theOriginal,
-                     const TCollection_AsciiString& theFileName,
-                     const TCollection_AsciiString& theFormatName)
+                      const TCollection_AsciiString& theFileName,
+                      const TCollection_AsciiString& theFormatName)
 {
   SetErrorCode(KO);
 
@@ -194,7 +196,7 @@ void GEOMImpl_IInsertOperations::Export
 //=============================================================================
 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
                                  (const TCollection_AsciiString& theFileName,
-                                 const TCollection_AsciiString& theFormatName)
+                                  const TCollection_AsciiString& theFormatName)
 {
   SetErrorCode(KO);
 
@@ -240,8 +242,10 @@ Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
   }
 
   //Make a Python command
-  GEOM::TPythonDump(aFunction) << result << " = geompy.Import(\""
-    << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
+  if( theFormatName != "IGES_UNIT" ) {
+    GEOM::TPythonDump(aFunction) << result << " = geompy.Import(\""
+      << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
+  }
 
   SetErrorCode(OK);
 
@@ -269,7 +273,7 @@ Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
 //=============================================================================
 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
-                     Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
+                      Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
 {
   if (theFormats.IsNull())
     theFormats = new TColStd_HSequenceOfAsciiString;
@@ -302,13 +306,13 @@ Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
       int aLenFormats = theFormats->Length();
       bool isFound = false;
       for(int aInd=1;aInd<=aLenFormats;aInd++){
-       if( theFormats->Value(aInd) == aToken){
-         isFound = true;
-         break;
-       }
+        if( theFormats->Value(aInd) == aToken){
+          isFound = true;
+          break;
+        }
       }
       if(!isFound)
-       theFormats->Append(aToken);
+        theFormats->Append(aToken);
     }
   }
 
@@ -326,7 +330,7 @@ Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
       if (myResMgr->Find(aKey.ToCString()))
         aPattern = myResMgr->Value(aKey.ToCString());
       else if(myResMgrUser->Find(aKey.ToCString()))
-       aPattern = myResMgrUser->Value(aKey.ToCString());
+        aPattern = myResMgrUser->Value(aKey.ToCString());
       else {
         aPattern = theFormats->Value(j);
         aPattern += " Files ( *.* )";
@@ -345,7 +349,7 @@ Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
 //=============================================================================
 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
-                     Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
+                      Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
 {
   if (theFormats.IsNull())
     theFormats = new TColStd_HSequenceOfAsciiString;
@@ -378,13 +382,13 @@ Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
       int aLenFormats = theFormats->Length();
       bool isFound = false;
       for(int aInd=1;aInd<=aLenFormats;aInd++){
-       if( theFormats->Value(aInd) == aToken){
-         isFound = true;
-         break;
-       }
+        if( theFormats->Value(aInd) == aToken){
+          isFound = true;
+          break;
+        }
       }
       if(!isFound)
-       theFormats->Append(aToken);
+        theFormats->Append(aToken);
     }
   }
 
@@ -402,7 +406,7 @@ Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
       if (myResMgr->Find(aKey.ToCString()))
         aPattern = myResMgr->Value(aKey.ToCString());
       else if (myResMgrUser->Find(aKey.ToCString()))
-       aPattern = myResMgrUser->Value(aKey.ToCString());
+        aPattern = myResMgrUser->Value(aKey.ToCString());
       else {
         aPattern = theFormats->Value(j);
         aPattern += " Files ( *.* )";
@@ -539,5 +543,100 @@ Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
   }
 
   return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
-          myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
+           myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
+}
+
+int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
+{
+  SetErrorCode(KO);
+
+  if (theTextureFile.IsEmpty()) return 0;
+
+  Handle(TDataStd_HArray1OfByte) aTexture;
+
+  FILE* fp = fopen(theTextureFile.ToCString(), "r");
+  if (!fp) return 0;
+
+  std::list<std::string> lines;
+  char buffer[4096];
+  int maxlen = 0;
+  while (!feof(fp)) {
+    if ((fgets(buffer, 4096, fp)) == NULL) break;
+    int aLen = strlen(buffer);
+    if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
+    lines.push_back(buffer);
+    maxlen = std::max(maxlen, (int)strlen(buffer));
+  }
+
+  fclose(fp);
+
+  int lenbytes = maxlen/8;
+  if (maxlen%8) lenbytes++;
+
+  if (lenbytes == 0 || lines.empty())
+    return 0;
+
+  std::list<unsigned char> bytedata;
+  std::list<std::string>::const_iterator it;
+  for (it = lines.begin(); it != lines.end(); ++it) {
+    std::string line = *it;
+    int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
+    for (int i = 0; i < lenline/8; i++) {
+      unsigned char byte = 0;
+      for (int j = 0; j < 8; j++)
+        byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
+      bytedata.push_back(byte);
+    }
+    for (int i = lenline/8; i < lenbytes; i++)
+      bytedata.push_back((unsigned char)0);
+  }
+
+  if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
+    return 0;
+
+  aTexture = new TDataStd_HArray1OfByte(1, lines.size()*lenbytes);
+  std::list<unsigned char>::iterator bdit;
+  int i;
+  for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
+    aTexture->SetValue(i, (Standard_Byte)(*bdit));
+
+  int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
+  if (aTextureId > 0) SetErrorCode(OK);
+  return aTextureId;
+}
+  
+int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight, 
+                                           const Handle(TDataStd_HArray1OfByte)& theTexture)
+{
+  SetErrorCode(KO);
+  int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
+  if (aTextureId > 0) SetErrorCode(OK);
+  return aTextureId;
+}
+
+Handle(TDataStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
+                                                                      int& theWidth, int& theHeight)
+{
+  SetErrorCode(KO);
+  
+  Handle(TDataStd_HArray1OfByte) aTexture;
+  theWidth = theHeight = 0;
+  TCollection_AsciiString aFileName;
+
+  if (theTextureId <= 0)
+    return aTexture;
+
+  aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
+
+  if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
+
+  return aTexture;
+}
+
+std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
+{
+  SetErrorCode(KO);
+  std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());
+  SetErrorCode(OK);
+  return id_list;
 }