0031075: Application Framework - reading STEP file into TDocStd_Document leads to...
[occt.git] / src / QABugs / QABugs_1.cxx
index def28ea..244aeb4 100644 (file)
@@ -39,7 +39,7 @@
 #include <Draw_Viewer.hxx>
 #include <Draw.hxx>
 
-#ifndef WNT
+#ifndef _WIN32
 extern Draw_Viewer dout;
 #else
 Standard_IMPORT Draw_Viewer dout;
@@ -51,14 +51,12 @@ Standard_IMPORT Draw_Viewer dout;
 #include <BRepBuilderAPI_MakeFace.hxx>
 #include <TopoDS.hxx>
 
-#if ! defined(WNT)
+#if ! defined(_WIN32)
 extern ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
 #else
 Standard_EXPORT ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
 #endif
 
-static TColStd_MapOfInteger theactivatedmodes(8);
-
 #include <AIS_PlaneTrihedron.hxx>
 #include <TopoDS_Face.hxx>
 #include <TopExp_Explorer.hxx>
@@ -66,123 +64,10 @@ static TColStd_MapOfInteger theactivatedmodes(8);
 #include <BRepAdaptor_Curve.hxx>
 #include <GC_MakePlane.hxx>
 
-static Standard_Integer OCC328bug (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
-{
-  Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
-  if(aContext.IsNull()) { 
-    di << argv[0] << "ERROR : use 'vinit' command before " << "\n";
-    return 1;
-  }
-
-  if ( argc != 3) {
-    di << "ERROR : Usage : " << argv[0] << " shape mode" << "\n";
-    return 1;
-  }
-  
-  Standard_Integer ChoosingMode = -1;
-  if ( strcmp (argv [2], "VERTEX") == 0 ) {
-    ChoosingMode = 1;
-  }
-  if ( strcmp (argv [2], "EDGE") == 0 ) {
-    ChoosingMode = 2;
-  }
-  if ( strcmp (argv [2], "WIRE") == 0 ) {
-    ChoosingMode = 3;
-  }
-  if ( strcmp (argv [2], "FACE") == 0 ) {
-    ChoosingMode = 4;
-  }
-  if ( strcmp (argv [2], "SHELL") == 0 ) {
-    ChoosingMode = 5;
-  }
-  if ( strcmp (argv [2], "SOLID") == 0 ) {
-    ChoosingMode = 6;
-  }
-  if ( strcmp (argv [2], "COMPOUND") == 0 ) {
-    ChoosingMode = 7;
-  }
-  if ( ChoosingMode == -1 ) {
-    di << "ERROR : " << argv[1] << " : vrong value of a mode" << "\n";
-    return 1;
-  }
-
-  Standard_Boolean updateviewer = Standard_True;
-
-  ViewerTest_DoubleMapOfInteractiveAndName& aMap = GetMapOfAIS();
-  
-  TCollection_AsciiString aName(argv[1]);
-  Handle(AIS_InteractiveObject) AISObj;
-  
-  if(!aMap.IsBound2(aName)) {
-    di << "Use 'vdisplay' before" << "\n";
-    return 1;
-  } else {
-    AISObj = Handle(AIS_InteractiveObject)::DownCast(aMap.Find2(aName));
-    if(AISObj.IsNull()){
-      di << argv[1] << " : No interactive object" << "\n";
-      return 1;
-    } 
-
-    if (!aContext->HasOpenedContext()) {
-      aContext->OpenLocalContext();
-    }
-
-    if(!theactivatedmodes.Contains(ChoosingMode)) {
-      aContext->ActivateStandardMode(AIS_Shape::SelectionType(ChoosingMode));
-      theactivatedmodes.Add(ChoosingMode);
-    }
-    aContext->Erase(AISObj, updateviewer);
-    aContext->UpdateCurrentViewer();
-
-    aContext->Display(AISObj, updateviewer);
-    aContext->UpdateCurrentViewer();
-
-    const TColStd_ListOfInteger& aList = aContext->ActivatedStandardModes();
-    Standard_Integer SelectMode;
-    TCollection_AsciiString SelectModeString;
-    TColStd_ListIteratorOfListOfInteger itr(aList);
-    for (; itr.More(); itr.Next()) {
-      SelectMode = itr.Value();
-      //cout << "SelectMode = " << SelectMode << endl;
-
-      switch (SelectMode)
-       {
-       case 1:
-         SelectModeString.Copy("VERTEX");
-         break;
-       case 2:
-         SelectModeString.Copy("EDGE");
-         break;
-       case 3:
-         SelectModeString.Copy("WIRE");
-         break;
-       case 4:
-         SelectModeString.Copy("FACE");
-         break;
-       case 5:
-         SelectModeString.Copy("SHELL");
-         break;
-       case 6:
-         SelectModeString.Copy("SOLID");
-         break;
-       case 7:
-         SelectModeString.Copy("COMPOUND");
-         break;
-       default:
-         SelectModeString.Copy("UNKNOWN");
-       }
-      di << "SelectMode = " << SelectModeString.ToCString() << "\n";
-
-    }
-  }
-
-  return 0;
-}
-
 static Standard_Integer OCC159bug (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
 {
   if ( argc != 2) {
-    di << "ERROR : Usage : " << argv[0] << " Doc" << "\n";
+    di << "ERROR : Usage : " << argv[0] << " Doc\n";
     return 1;
   }
   
@@ -197,23 +82,25 @@ static Standard_Integer OCC159bug (Draw_Interpretor& di, Standard_Integer argc,
 
   Handle(TDocStd_Document) OwnerD1 = Owner->GetDocument();    
   if (OwnerD1.IsNull()) {
-    di << "DocOwner1 = NULL" << "\n";
+    di << "DocOwner1 = NULL\n";
   } else {
-    di << "DocOwner1 = NOTNULL" << "\n";
+    di << "DocOwner1 = NOTNULL\n";
   }
+  OwnerD1.Nullify();
  
-  Handle(TDocStd_Application) A;
-  if (!DDocStd::Find(A)) return 1;
+  Handle(TDocStd_Application) A = DDocStd::GetApplication();
   A->Close(D); 
 
-  Handle(Draw_Drawable3D) DD = Draw::Get(argv[1],Standard_False);
-  dout.RemoveDrawable (DD);
+  if (Handle(Draw_Drawable3D) DD = Draw::GetExisting (argv[1]))
+  {
+    dout.RemoveDrawable (DD);
+  }
 
   Handle(TDocStd_Document) OwnerD2 = Owner->GetDocument();    
   if (OwnerD2.IsNull()) {
-    di << "DocOwner2 = NULL" << "\n";
+    di << "DocOwner2 = NULL\n";
   } else {
-    di << "DocOwner2 = NOTNULL" << "\n";
+    di << "DocOwner2 = NOTNULL\n";
   }
 
   Standard_Integer DocRefCount2 = D->GetRefCount();
@@ -225,7 +112,7 @@ static Standard_Integer OCC159bug (Draw_Interpretor& di, Standard_Integer argc,
 static Standard_Integer OCC145bug (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
 {
   if ( argc != 3) {
-    di << "ERROR : Usage : " << argv[0] << " Shape MaxNbr" << "\n";
+    di << "ERROR : Usage : " << argv[0] << " Shape MaxNbr\n";
     return 1;
   }
 
@@ -236,7 +123,7 @@ static Standard_Integer OCC145bug (Draw_Interpretor& di, Standard_Integer argc,
   TopoDS_Shape aShape;
 
   if (!BRepTools::Read(aShape, aFileName.ToCString(), aBld)) {
-    di << "ERROR :Could not read a shape!!!" << "\n";
+    di << "ERROR :Could not read a shape!!!\n";
     return 1;
   }
 
@@ -246,7 +133,7 @@ static Standard_Integer OCC145bug (Draw_Interpretor& di, Standard_Integer argc,
   for (i = 1; i <= aMaxNbr; i++) {
     BRepBuilderAPI_MakeFace aMF(aWire);
     if (!aMF.IsDone()) {
-      di << "ERROR : Could not make a face" << "\n";
+      di << "ERROR : Could not make a face\n";
       return 1;
     }
   }
@@ -257,23 +144,23 @@ static Standard_Integer OCC145bug (Draw_Interpretor& di, Standard_Integer argc,
 static Standard_Integer OCC73_SelectionMode (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
 {
   if ( argc < 3) {
-    di << "ERROR : Usage : " << argv[0] << " DOC entry [SelectionMode]" << "\n";
+    di << "ERROR : Usage : " << argv[0] << " DOC entry [SelectionMode]\n";
     return 1;
   }
 
   Handle(TDocStd_Document) D;
-  //cout << "OCC73_SelectionMode  1" << endl;
+  //std::cout << "OCC73_SelectionMode  1" << std::endl;
   if (!DDocStd::GetDocument(argv[1],D)) return 1;  
   TDF_Label L;
-  //cout << "OCC73_SelectionMode  2" << endl;
+  //std::cout << "OCC73_SelectionMode  2" << std::endl;
   if (!DDF::FindLabel(D->GetData(),argv[2],L)) return 1;  
 
   Handle(TPrsStd_AISViewer) viewer;
-  //cout << "OCC73_SelectionMode  3" << endl;
+  //std::cout << "OCC73_SelectionMode  3" << std::endl;
   if( !TPrsStd_AISViewer::Find(L, viewer) ) return 1;  
 
   Handle(TPrsStd_AISPresentation) prs;
-  //cout << "OCC73_SelectionMode  4" << endl;
+  //std::cout << "OCC73_SelectionMode  4" << std::endl;
   if(L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) {   
     if( argc == 4 ) {
       prs->SetSelectionMode((Standard_Integer)Draw::Atoi(argv[3]));
@@ -281,11 +168,11 @@ static Standard_Integer OCC73_SelectionMode (Draw_Interpretor& di, Standard_Inte
     }
     else {
       Standard_Integer SelectionMode = prs->SelectionMode();
-      //cout << "SelectionMode = " << SelectionMode << endl;
+      //std::cout << "SelectionMode = " << SelectionMode << std::endl;
       di<<SelectionMode;
     }
   }
-  //cout << "OCC73_SelectionMode  5" << endl;
+  //std::cout << "OCC73_SelectionMode  5" << std::endl;
 
   return 0;
 }
@@ -299,13 +186,13 @@ static Standard_Integer OCC10bug (Draw_Interpretor& di, Standard_Integer argc, c
   }
 
   if(argc != 4) {
-    di << "Usage : " << argv[0] << " name plane Length" << "\n";
+    di << "Usage : " << argv[0] << " name plane Length\n";
     return 1;
   }
 
   TopoDS_Shape S = DBRep::Get( argv[2] );
   if ( S.IsNull() ) {
-    di << "Shape is empty" << "\n";
+    di << "Shape is empty\n";
     return 1;
   }
 
@@ -318,14 +205,13 @@ static Standard_Integer OCC10bug (Draw_Interpretor& di, Standard_Integer argc, c
   Standard_Boolean IsBound = GetMapOfAIS().IsBound2(name);
   if (IsBound) {
     // on recupere la shape dans la map des objets displayes
-    Handle(AIS_InteractiveObject) aShape = 
-      Handle(AIS_InteractiveObject)::DownCast(GetMapOfAIS().Find2(name));
+    Handle(AIS_InteractiveObject) aShape = GetMapOfAIS().Find2(name);
       
     // On verifie que l'AIS InteraciveObject est bien 
     // un AIS_PlaneTrihedron
     if (aShape->Type()==AIS_KOI_Datum && aShape->Signature()==4) {
       // On downcast aShape de AIS_InteractiveObject a AIS_PlaneTrihedron
-      theAISPlaneTri = *(Handle(AIS_PlaneTrihedron)*) &aShape;
+      theAISPlaneTri = Handle(AIS_PlaneTrihedron)::DownCast (aShape);
 
       theAISPlaneTri->SetLength(Length);
   
@@ -370,7 +256,7 @@ static Standard_Integer OCC10bug (Draw_Interpretor& di, Standard_Integer argc, c
     theAISPlaneTri->SetLength(Length);
     
     GetMapOfAIS().Bind ( theAISPlaneTri, name);
-    aContext->Display(theAISPlaneTri );
+    aContext->Display (theAISPlaneTri, Standard_True);
   }
 
   Standard_Real getLength = theAISPlaneTri->GetLength();
@@ -378,9 +264,9 @@ static Standard_Integer OCC10bug (Draw_Interpretor& di, Standard_Integer argc, c
   di << "getLength = " << getLength << "\n";
 
   if (getLength == Length) {
-    di << "OCC10: OK" << "\n";
+    di << "OCC10: OK\n";
   } else {
-    di << "OCC10: ERROR" << "\n";
+    di << "OCC10: ERROR\n";
   }
 
   return 0;
@@ -390,12 +276,12 @@ static Standard_Integer OCC74bug_set (Draw_Interpretor& di, Standard_Integer arg
 {
   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
   if(aContext.IsNull()) { 
-    di << argv[0] << "ERROR : use 'vinit' command before " << "\n";
+    di << argv[0] << "ERROR : use 'vinit' command before \n";
     return 1;
   }
 
   if ( argc != 3) {
-    di << "ERROR : Usage : " << argv[0] << " shape mode; set selection mode" << "\n";
+    di << "ERROR : Usage : " << argv[0] << " shape mode; set selection mode\n";
     return 1;
   }
   
@@ -407,25 +293,19 @@ static Standard_Integer OCC74bug_set (Draw_Interpretor& di, Standard_Integer arg
   Handle(AIS_InteractiveObject) AISObj;
 
   Standard_Integer SelectMode = Draw::Atoi(argv[2]);
-  
-  if(!aMap.IsBound2(aName)) {
-    di << "Use 'vdisplay' before" << "\n";
+  if (!aMap.Find2 (aName, AISObj)
+    || AISObj.IsNull())
+  {
+    di << "Use 'vdisplay' before\n";
     return 1;
-  } else {
-    AISObj = Handle(AIS_InteractiveObject)::DownCast(aMap.Find2(aName));
-    if(AISObj.IsNull()){
-      di << argv[1] << " : No interactive object" << "\n";
-      return 1;
-    } 
-    AISObj->SetSelectionMode(SelectMode);
-    if (!aContext->HasOpenedContext()) {
-      aContext->OpenLocalContext();
-    }
-    aContext->Erase(AISObj, updateviewer);
-    aContext->UpdateCurrentViewer();
-    aContext->Display(AISObj, updateviewer);
-    aContext->UpdateCurrentViewer();
   }
+
+  aContext->Erase(AISObj, updateviewer);
+  aContext->UpdateCurrentViewer();
+  aContext->SetAutoActivateSelection (Standard_False);
+  aContext->Display(AISObj, updateviewer);
+  aContext->Activate (AISObj, SelectMode);
+  aContext->UpdateCurrentViewer();
   return 0;
 }
 
@@ -433,12 +313,12 @@ static Standard_Integer OCC74bug_get (Draw_Interpretor& di, Standard_Integer arg
 {
   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
   if(aContext.IsNull()) { 
-    di << argv[0] << "ERROR : use 'vinit' command before " << "\n";
+    di << argv[0] << "ERROR : use 'vinit' command before \n";
     return 1;
   }
 
   if ( argc != 2) {
-    di << "ERROR : Usage : " << argv[0] << " shape; get selection mode" << "\n";
+    di << "ERROR : Usage : " << argv[0] << " shape; get selection mode\n";
     return 1;
   }
 
@@ -446,20 +326,17 @@ static Standard_Integer OCC74bug_get (Draw_Interpretor& di, Standard_Integer arg
   
   TCollection_AsciiString aName(argv[1]);
   Handle(AIS_InteractiveObject) AISObj;
-
-  if(!aMap.IsBound2(aName)) {
-    di << "Use 'vdisplay' before" << "\n";
+  if (!aMap.Find2(aName, AISObj)
+   || AISObj.IsNull())
+  {
+    di << "Use 'vdisplay' before\n";
     return 1;
-  } else {
-    AISObj = Handle(AIS_InteractiveObject)::DownCast(aMap.Find2(aName));
-    if(AISObj.IsNull()){
-      di << argv[1] << " : No interactive object" << "\n";
-      return 1;
-    } 
-    Standard_Integer SelectMode = AISObj->SelectionMode();
-    di << SelectMode << "\n";
   }
 
+  TColStd_ListOfInteger anActivatedModes;
+  aContext->ActivatedModes (AISObj, anActivatedModes);
+  Standard_Integer aMode = anActivatedModes.IsEmpty() ? -1 : anActivatedModes.Last();
+  di << aMode << "\n";
   return 0;
 }
 
@@ -472,14 +349,14 @@ static Standard_Integer OCC74bug_get (Draw_Interpretor& di, Standard_Integer arg
 static Standard_Integer OCC361bug (Draw_Interpretor& di, Standard_Integer nb, const char ** a)
 {
   if ( nb != 2) {
-    di << "ERROR : Usage : " << a[0] << " Doc" << "\n";
-    di << "-1" << "\n";
+    di << "ERROR : Usage : " << a[0] << " Doc\n";
+    di << "-1\n";
     return -1;
   }
 
   Handle(TDocStd_Document) D;    
   if (!DDocStd::GetDocument(a[1],D)) {
-    di << "-2" << "\n";
+    di << "-2\n";
     return 1;  
   }
 
@@ -501,18 +378,149 @@ static Standard_Integer OCC361bug (Draw_Interpretor& di, Standard_Integer nb, co
 
   aTBox = aBuilder2.NamedShape()->Get();
   if(aTBox.Orientation() != TopAbs_REVERSED) {
-    di << "1" << "\n";
+    di << "1\n";
   } else {
-    di << "0" << "\n";
+    di << "0\n";
+  }
+  return 0;
+}
+
+#include <Graphic3d_Texture2Dmanual.hxx>
+#include <Image_AlienPixMap.hxx>
+#include <OSD_OpenFile.hxx>
+#include <Prs3d_ShadingAspect.hxx>
+#include <Standard_ArrayStreamBuffer.hxx>
+//=======================================================================
+//function : OCC30182
+//purpose  : Testing different interfaces of Image_AlienPixMap::Load()
+//=======================================================================
+static Standard_Integer OCC30182 (Draw_Interpretor& , Standard_Integer theNbArgs, const char** theArgVec)
+{
+  if (ViewerTest::CurrentView().IsNull())
+  {
+    std::cout << "Error: no active view\n";
+    return 1;
+  }
+
+  TCollection_AsciiString aPrsName, anImgPath;
+  Standard_Integer anOffset = 0;
+  Standard_Integer aSrc = 0; // 0 - file name, 1 - file stream, 2 - memory buffer
+  for (Standard_Integer anArgIter = 1; anArgIter < theNbArgs; ++anArgIter)
+  {
+    TCollection_AsciiString anArg (theArgVec[anArgIter]);
+    anArg.LowerCase();
+    if (anArg == "-offset"
+     && anArgIter + 1 < theNbArgs)
+    {
+      anOffset = Draw::Atoi (theArgVec[++anArgIter]);
+    }
+    else if (anArg == "-filename")
+    {
+      aSrc = 0;
+    }
+    else if (anArg == "-stream")
+    {
+      aSrc = 1;
+    }
+    else if (anArg == "-mem"
+          || anArg == "-memory")
+    {
+      aSrc = 2;
+    }
+    else if (aPrsName.IsEmpty())
+    {
+      aPrsName = theArgVec[anArgIter];
+    }
+    else if (anImgPath.IsEmpty())
+    {
+      anImgPath = theArgVec[anArgIter];
+    }
+    else
+    {
+      std::cout << "Syntax error at '" << anArg << "'\n";
+      return 1;
+    }
+  }
+  if (anImgPath.IsEmpty())
+  {
+    std::cout << "Syntax error: wrong number of arguments\n";
+    return 1;
   }
+
+  Handle(Image_AlienPixMap) anImage = new Image_AlienPixMap();
+  if (aSrc == 0)
+  {
+    if (!anImage->Load (anImgPath))
+    {
+      return 0;
+    }
+  }
+  else
+  {
+    std::ifstream aFile;
+    OSD_OpenStream (aFile, anImgPath.ToCString(), std::ios::in | std::ios::binary);
+    if (!aFile.is_open())
+    {
+      std::cout << "Syntax error: image file '" << anImgPath << "' cannot be found\n";
+      return 1;
+    }
+    if (anOffset != 0)
+    {
+      aFile.seekg (anOffset);
+    }
+
+    if (aSrc == 2)
+    {
+      aFile.seekg (0, std::ios::end);
+      Standard_Integer aLen = (Standard_Integer )aFile.tellg() - anOffset;
+      aFile.seekg (anOffset);
+      if (aLen <= 0)
+      {
+        std::cout << "Syntax error: wrong offset\n";
+        return 1;
+      }
+      NCollection_Array1<Standard_Byte> aBuff (1, aLen);
+      if (!aFile.read ((char* )&aBuff.ChangeFirst(), aBuff.Size()))
+      {
+        std::cout << "Error: unable to read file\n";
+        return 1;
+      }
+      aFile.close();
+      if (!anImage->Load (&aBuff.ChangeFirst(), aBuff.Size(), anImgPath))
+      {
+        return 0;
+      }
+    }
+    else
+    {
+      if (!anImage->Load (aFile, anImgPath))
+      {
+        return 0;
+      }
+    }
+  }
+
+  TopoDS_Shape aShape = BRepPrimAPI_MakeBox (100.0 * anImage->Ratio(), 100.0, 1.0).Shape();
+  Handle(AIS_Shape) aPrs = new AIS_Shape (aShape);
+  aPrs->SetDisplayMode (AIS_Shaded);
+  aPrs->Attributes()->SetupOwnShadingAspect();
+  const Handle(Graphic3d_AspectFillArea3d)& anAspect = aPrs->Attributes()->ShadingAspect()->Aspect();
+  anAspect->SetShadingModel (Graphic3d_TOSM_UNLIT);
+  anAspect->SetTextureMapOn (true);
+  anAspect->SetTextureMap (new Graphic3d_Texture2Dmanual (anImage));
+  if (anImage->IsTopDown())
+  {
+    anAspect->TextureMap()->GetParams()->SetTranslation(Graphic3d_Vec2 (0.0f, -1.0f));
+    anAspect->TextureMap()->GetParams()->SetScale      (Graphic3d_Vec2 (1.0f, -1.0f));
+  }
+
+  ViewerTest::Display (aPrsName, aPrs, true, true);
   return 0;
 }
 
 void QABugs::Commands_1(Draw_Interpretor& theCommands) {
   const char *group = "QABugs";
 
-  theCommands.Add ("OCC328", "OCC328 shape mode", __FILE__, OCC328bug, group);
-
   theCommands.Add ("OCC159", "OCC159 Doc", __FILE__, OCC159bug, group);
   theCommands.Add ("OCC145", "OCC145 Shape MaxNbr", __FILE__, OCC145bug, group);
 
@@ -523,7 +531,9 @@ void QABugs::Commands_1(Draw_Interpretor& theCommands) {
   theCommands.Add ("OCC74_set", "OCC74_set shape mode;   set selection mode", __FILE__, OCC74bug_set, group);
   theCommands.Add ("OCC74_get", "OCC74_get shape;   get selection mode", __FILE__, OCC74bug_get, group);
 
-  theCommands.Add("OCC361", "OCC361 Doc ", __FILE__, OCC361bug, group);
-
+  theCommands.Add ("OCC361", "OCC361 Doc ", __FILE__, OCC361bug, group);
+  theCommands.Add ("OCC30182",
+                   "OCC30182 name image [-offset Start] [-fileName] [-stream] [-memory]\n"
+                   "Decodes image either by passing file name, file stream or memory stream", __FILE__, OCC30182, group);
   return;
 }