0023432: Connected Interactive Objects computed without Interactive Context
[occt.git] / src / ViewerTest / ViewerTest.cxx
index 6d63e65..d062cb5 100755 (executable)
@@ -1,8 +1,24 @@
-// File:       ViewerTest.cxx
-// Created:    Wed Jul 23 14:14:01 1997
-// Author:     Henri JEANNIN
-// Modified by  Eric Gouthiere [sep-oct 98] -> add commands for display...
+// Created on: 1997-07-23
+// Created by: Henri JEANNIN
+// Copyright (c) 1997-1999 Matra Datavision
+// Copyright (c) 1999-2012 OPEN CASCADE SAS
+//
+// The content of this file is subject to the Open CASCADE Technology Public
+// License Version 6.5 (the "License"). You may not use the content of this file
+// except in compliance with the License. Please obtain a copy of the License
+// at http://www.opencascade.org and read it completely before using this file.
+//
+// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
+// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
 //
+// The Original Code and all software distributed under the License is
+// distributed on an "AS IS" basis, without warranty of any kind, and the
+// Initial Developer hereby disclaims all such warranties, including without
+// limitation, any warranties of merchantability, fitness for a particular
+// purpose or non-infringement. Please see the License for the specific terms
+// and conditions governing the rights and limitations under the License.
+
+// Modified by  Eric Gouthiere [sep-oct 98] -> add commands for display...
 // Modified by  Robert Coublanc [nov 16-17-18 1998]
 //             -split ViewerTest.cxx into 3 files : ViewerTest.cxx,
 //                                                  ViewerTest_ObjectCommands.cxx
@@ -27,6 +43,7 @@
 #include <BRepAdaptor_Curve.hxx>
 #include <StdSelect_ShapeTypeFilter.hxx>
 #include <AIS.hxx>
+#include <AIS_Drawer.hxx>
 #include <AIS_InteractiveObject.hxx>
 #include <AIS_Trihedron.hxx>
 #include <AIS_Axis.hxx>
 #include <AIS_SignatureFilter.hxx>
 #include <AIS_ListOfInteractive.hxx>
 #include <AIS_ListIteratorOfListOfInteractive.hxx>
-#include <Image_PixMap.hxx>
+#include <Aspect_InteriorStyle.hxx>
+#include <Graphic3d_AspectFillArea3d.hxx>
+#include <Image_AlienPixMap.hxx>
+#include <Prs3d_ShadingAspect.hxx>
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #include <NIS_Triangulated.hxx>
 extern int ViewerMainLoop(Standard_Integer argc, const char** argv);
 
-//=======================================================================
-//function : GetColorFromName
-//purpose  : get the Quantity_NameOfColor from a string
-//=======================================================================
-
-#include <Quantity_NameOfColor.hxx>
 #include <Quantity_Color.hxx>
 #include <Quantity_NameOfColor.hxx>
 
@@ -76,22 +90,23 @@ extern int ViewerMainLoop(Standard_Integer argc, const char** argv);
 #define DEFAULT_COLOR    Quantity_NOC_GOLDENROD
 #define DEFAULT_MATERIAL Graphic3d_NOM_BRASS
 
-static Quantity_NameOfColor GetColorFromName( const char *name )
-{
-  Quantity_NameOfColor ret = DEFAULT_COLOR;
+//=======================================================================
+//function : GetColorFromName
+//purpose  : get the Quantity_NameOfColor from a string
+//=======================================================================
 
-  Standard_Boolean Found = Standard_False;
-  Standard_CString colstring;
-  for(Standard_Integer i=0;i<=514 && !Found;i++)
+Quantity_NameOfColor ViewerTest::GetColorFromName (const Standard_CString theName)
+{
+  for (Standard_Integer anIter = Quantity_NOC_BLACK; anIter <= Quantity_NOC_WHITE; ++anIter)
+  {
+    Standard_CString aColorName = Quantity_Color::StringName (Quantity_NameOfColor (anIter));
+    if (strcasecmp (theName, aColorName) == 0)
     {
-      colstring = Quantity_Color::StringName(Quantity_NameOfColor(i));
-      if (!strcasecmp(name,colstring)) {
-       ret = (Quantity_NameOfColor)i;
-       Found = Standard_True;
-      }
+      return Quantity_NameOfColor (anIter);
     }
+  }
 
-  return ret;
+  return DEFAULT_COLOR;
 }
 
 //=======================================================================
@@ -114,7 +129,6 @@ static Graphic3d_NameOfMaterial GetMaterialFromName( const char *name )
   else if ( !strcasecmp(name,"STONE" ) )        mat = Graphic3d_NOM_STONE;
   else if ( !strcasecmp(name,"CHROME" ) )       mat = Graphic3d_NOM_CHROME;
   else if ( !strcasecmp(name,"ALUMINIUM" ) )     mat = Graphic3d_NOM_ALUMINIUM;
-  else if ( !strcasecmp(name,"STONE" ) )        mat = Graphic3d_NOM_STONE;
   else if ( !strcasecmp(name,"NEON_PHC" ) )     mat = Graphic3d_NOM_NEON_PHC;
   else if ( !strcasecmp(name,"NEON_GNC" ) )     mat = Graphic3d_NOM_NEON_GNC;
   else if ( !strcasecmp(name,"PLASTER" ) )      mat = Graphic3d_NOM_PLASTER;
@@ -294,6 +308,12 @@ Standard_EXPORT Standard_Boolean VDisplayAISObject (const TCollection_AsciiStrin
     aMap.UnBind2 (theName);
   }
 
+  if (theAISObj.IsNull())
+  {
+    // object with specified name already unbound
+    return Standard_True;
+  }
+
   // unbind AIS object if was bound with another name
   aMap.UnBind1 (theAISObj);
 
@@ -738,6 +758,63 @@ static int VDebug(Draw_Interpretor& di, Standard_Integer , const char** )
   return 0;
 }
 
+//==============================================================================
+//function : VSelPrecision
+//purpose  : To set the selection precision mode and tolerance value
+//Draw arg : Selection precision mode (0 for window, 1 for view) and tolerance
+//           value (integer number of pixel for window mode, double value of
+//           sensitivity for view mode). Without arguments the function just
+//           prints the current precision mode and the corresponding tolerance.
+//==============================================================================
+static int VSelPrecision(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
+{
+  if( argc > 3 )
+  {
+    di << "Use: " << argv[0] << " [precision_mode [tolerance_value]]\n";
+    return 1;
+  }
+
+  Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
+  if( aContext.IsNull() )
+    return 1;
+
+  if( argc == 1 )
+  {
+    StdSelect_SensitivityMode aMode = aContext->SensitivityMode();
+    if( aMode == StdSelect_SM_WINDOW )
+    {
+      Standard_Integer aPixelTolerance = aContext->PixelTolerance();
+      di << "Precision mode  : 0 (window)\n";
+      di << "Pixel tolerance : " << aPixelTolerance << "\n";
+    }
+    else if( aMode == StdSelect_SM_VIEW )
+    {
+      Standard_Real aSensitivity = aContext->Sensitivity();
+      di << "Precision mode : 1 (view)\n";
+      di << "Sensitivity    : " << aSensitivity << "\n";
+    }
+  }
+  else if( argc > 1 )
+  {
+    StdSelect_SensitivityMode aMode = ( StdSelect_SensitivityMode )atoi( argv[1] );
+    aContext->SetSensitivityMode( aMode );
+    if( argc > 2 )
+    {
+      if( aMode == StdSelect_SM_WINDOW )
+      {
+        Standard_Integer aPixelTolerance = atoi( argv[2] );
+        aContext->SetPixelTolerance( aPixelTolerance );
+      }
+      else if( aMode == StdSelect_SM_VIEW )
+      {
+        Standard_Real aSensitivity = atof( argv[2] );
+        aContext->SetSensitivity( aSensitivity );
+      }
+    }
+  }
+  return 0;
+}
+
 //==============================================================================
 //function : VDump
 //purpose  : To dump the active view snapshot to image file
@@ -751,22 +828,21 @@ static Standard_Integer VDump (Draw_Interpretor& di, Standard_Integer argc, cons
     return 1;
   }
 
-  Image_TypeOfImage aBufferType = Image_TOI_RGB;
-
+  Graphic3d_BufferType aBufferType = Graphic3d_BT_RGB;
   if (argc > 2)
   {
     TCollection_AsciiString aBuffTypeStr (argv[2]);
     if (TCollection_AsciiString::ISSIMILAR (aBuffTypeStr, TCollection_AsciiString ("rgb")))
     {
-      aBufferType = Image_TOI_RGB;
+      aBufferType = Graphic3d_BT_RGB;
     }
     else if (TCollection_AsciiString::ISSIMILAR (aBuffTypeStr, TCollection_AsciiString ("rgba")))
     {
-      aBufferType = Image_TOI_RGBA;
+      aBufferType = Graphic3d_BT_RGBA;
     }
     else if (TCollection_AsciiString::ISSIMILAR (aBuffTypeStr, TCollection_AsciiString ("depth")))
     {
-      aBufferType = Image_TOI_FLOAT;
+      aBufferType = Graphic3d_BT_Depth;
     }
   }
 
@@ -776,22 +852,41 @@ static Standard_Integer VDump (Draw_Interpretor& di, Standard_Integer argc, cons
   Handle(AIS_InteractiveContext) IC;
   Handle(V3d_View) view;
   GetCtxAndView (IC, view);
-  if (!view.IsNull())
+  if (view.IsNull())
   {
-    if (aWidth > 0 && aHeight > 0)
-    {
-      return view->ToPixMap (aWidth, aHeight, aBufferType)->Dump (argv[1]) ? 0 : 1;
-    }
-    else
+    di << "Cannot find an active viewer/view\n";
+    return 1;
+  }
+
+  if (aWidth <= 0 || aHeight <= 0)
+  {
+    if (!view->Dump (argv[1], aBufferType))
     {
-      return view->Dump (argv[1], aBufferType) ? 0 : 1;
+      di << "Dumping failed!\n";
+      return 1;
     }
+    return 0;
   }
-  else
+
+  Image_AlienPixMap aPixMap;
+  if (!view->ToPixMap (aPixMap, aWidth, aHeight, aBufferType))
+  {
+    di << "Dumping failed!\n";
+    return 1;
+  }
+
+  if (aPixMap.SizeX() != Standard_Size(aWidth)
+   || aPixMap.SizeY() != Standard_Size(aHeight))
+  {
+    std::cout << "Warning! Dumped dimensions " << aPixMap.SizeX() << "x" << aPixMap.SizeY()
+              << " are lesser than requested " << aWidth          << "x" << aHeight << "\n";
+  }
+  if (!aPixMap.Save (argv[1]))
   {
-    di << "Cannot find an active viewer/view" << "\n";
+    di << "Saving image failed!\n";
     return 1;
   }
+  return 0;
 }
 
 
@@ -1007,20 +1102,20 @@ static int VColor2 (Draw_Interpretor& di, Standard_Integer argc, const char** ar
           Handle(AIS_InteractiveObject)::DownCast (anObj);
 #ifdef DEB
           if (HaveToSet)
-            di  << "HaveToSet "<< "1" <<" Color Given "<< argv[2] << " Color returned "<< GetColorFromName(argv[2]) << "\n";
+            di  << "HaveToSet "<< "1" <<" Color Given "<< argv[2] << " Color returned "<< ViewerTest::GetColorFromName(argv[2]) << "\n";
           else
             di  << "HaveToSet 0\n";
 #endif
 
         if(HaveToSet)
-          TheAISContext()->SetColor(ashape,GetColorFromName(argv[2]) );
+          TheAISContext()->SetColor(ashape,ViewerTest::GetColorFromName(argv[2]) );
         else
           TheAISContext()->UnsetColor(ashape);
       } else if (anObj->IsKind(STANDARD_TYPE(NIS_InteractiveObject))) {
         Handle(NIS_Triangulated) ashape =
           Handle(NIS_Triangulated)::DownCast (anObj);
         if (!ashape.IsNull())
-          ashape->SetColor (GetColorFromName(argv[2]));
+          ashape->SetColor (ViewerTest::GetColorFromName(argv[2]));
       }
     }
 
@@ -1039,12 +1134,12 @@ static int VColor2 (Draw_Interpretor& di, Standard_Integer argc, const char** ar
           continue;
 #ifdef DEB
         if (HaveToSet)
-          di  << "HaveToSet "<< "1" <<" Color Given "<< argv[2] << " Color returned "<< GetColorFromName(argv[2]) << "\n";
+          di  << "HaveToSet "<< "1" <<" Color Given "<< argv[2] << " Color returned "<< ViewerTest::GetColorFromName(argv[2]) << "\n";
         else
           di  << "HaveToSet 0\n";
 #endif
         if(HaveToSet)
-          TheAISContext()->SetColor(ashape,GetColorFromName(argv[1]),Standard_False);
+          TheAISContext()->SetColor(ashape,ViewerTest::GetColorFromName(argv[1]),Standard_False);
         else
           TheAISContext()->UnsetColor(ashape,Standard_False);
       }
@@ -1063,7 +1158,7 @@ static int VColor2 (Draw_Interpretor& di, Standard_Integer argc, const char** ar
           Handle(AIS_InteractiveObject)::DownCast(it.Key1());
         if (!ashape.IsNull())
           if(HaveToSet)
-            TheAISContext()->SetColor(ashape,GetColorFromName(argv[1]),Standard_False);
+            TheAISContext()->SetColor(ashape,ViewerTest::GetColorFromName(argv[1]),Standard_False);
           else
             TheAISContext()->UnsetColor(ashape,Standard_False);
         it.Next();
@@ -1354,6 +1449,98 @@ static int VWidth (Draw_Interpretor& di, Standard_Integer argc, const char** arg
   }
   return 0;
 }
+
+//==============================================================================
+//function : VInteriorStyle
+//purpose  : sets interior style of the a selected or named or displayed shape
+//Draw arg : vsetinteriorstyle [shape] style
+//==============================================================================
+static void SetInteriorStyle (const Handle(AIS_InteractiveObject)& theIAO,
+                              const Standard_Integer theStyle,
+                              Draw_Interpretor& di)
+{
+  if (theStyle < Aspect_IS_EMPTY || theStyle > Aspect_IS_HIDDENLINE) {
+    di << "Style must be within a range [0 (Aspect_IS_EMPTY), " << Aspect_IS_HIDDENLINE <<
+      " (Aspect_IS_HIDDENLINE)]\n";
+    return;
+  }
+  const Handle(Prs3d_Drawer)& aDrawer = theIAO->Attributes();
+  Handle(Prs3d_ShadingAspect) aShadingAspect = aDrawer->ShadingAspect();
+  Handle(Graphic3d_AspectFillArea3d) aFillAspect = aShadingAspect->Aspect();
+  Aspect_InteriorStyle aStyle = (Aspect_InteriorStyle) (theStyle);
+  aFillAspect->SetInteriorStyle (aStyle);
+  TheAISContext()->RecomputePrsOnly (theIAO, Standard_False /*update*/, Standard_True /*all modes*/);
+}
+
+static int VInteriorStyle (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
+{
+  if (argc < 2 || argc > 3) {
+    di << argv[0] << " requires 2 or 3 arguments\n";
+    di << "Usage : " << argv[0] << " [shape] Style : Set interior style" << "\n";
+    di << "Style must match Aspect_InteriorStyle and be one of:\n";
+    di << "         0 = EMPTY, 1 = HOLLOW, 2 = HATCH, 3 = SOLID, 4 = HIDDENLINE\n";
+    return 1;
+  }
+
+  Standard_Boolean    ThereIsCurrent;
+  Standard_Boolean    ThereIsArgument;
+  Standard_Boolean    IsBound = Standard_False ;
+
+  ThereIsArgument = (argc > 2);
+  if ( !a3DView().IsNull() ) {
+    TCollection_AsciiString name;
+    if (ThereIsArgument) {
+      name = argv[1];
+      IsBound= GetMapOfAIS().IsBound2(name);
+    }
+    if (TheAISContext()->HasOpenedContext())
+      TheAISContext()->CloseLocalContext();
+
+    if (TheAISContext() -> NbCurrents() > 0  )
+      ThereIsCurrent =Standard_True;
+    else
+      ThereIsCurrent =Standard_False;
+
+    if ( ThereIsArgument && IsBound ) {
+      const Handle(Standard_Transient) anObj = GetMapOfAIS().Find2(name);
+      if (anObj->IsKind(STANDARD_TYPE(AIS_InteractiveObject))) {
+        const Handle(AIS_InteractiveObject) ashape =
+          Handle(AIS_InteractiveObject)::DownCast(GetMapOfAIS().Find2(name));
+        SetInteriorStyle (ashape, atoi (argv[2]), di);
+      }
+    }
+    //=======================================================================
+    // No arguments specified
+    // But there are one or more selected objects
+    //=======================================================================
+    if (ThereIsCurrent && !ThereIsArgument) {
+      for (TheAISContext() -> InitCurrent() ;
+           TheAISContext() -> MoreCurrent() ;
+           TheAISContext() ->NextCurrent() )
+      {
+        Handle(AIS_InteractiveObject) ashape =  TheAISContext() -> Current();
+        SetInteriorStyle (ashape, atoi (argv[1]), di);
+      }
+    }
+    //=======================================================================
+    // No arguments specified and there are no selected objects
+    //=======================================================================
+    else if (!ThereIsCurrent && !ThereIsArgument){
+      ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName
+       it(GetMapOfAIS());
+      while ( it.More() ) {
+        Handle(AIS_InteractiveObject) ashape =
+          Handle(AIS_InteractiveObject)::DownCast (it.Key1());
+        if (!ashape.IsNull())
+          SetInteriorStyle (ashape, atoi (argv[1]), di);
+        it.Next();
+      }
+    }
+    TheAISContext()->UpdateCurrentViewer();
+  }
+  return 0;
+}
+
 //==============================================================================
 //function : VDonly2
 //author   : ege
@@ -3359,6 +3546,7 @@ void ViewerTest::Commands(Draw_Interpretor& theCommands)
   ViewerTest::ObjectCommands(theCommands);
   ViewerTest::FilletCommands(theCommands);
   ViewerTest::VoxelCommands(theCommands);
+  ViewerTest::OpenGlCommands(theCommands);
 
   const char *group = "AIS_Display";
 
@@ -3451,6 +3639,10 @@ void ViewerTest::Commands(Draw_Interpretor& theCommands)
                  "vunsetwidth          : vwidth  [name of shape]",
                  __FILE__,VWidth,group);
 
+  theCommands.Add("vsetinteriorstyle",
+                 "vsetinteriorstyle    : vsetinteriorstyle [name of shape] style",
+                 __FILE__,VInteriorStyle,group);
+
   theCommands.Add("vardis",
                  "vardis          : display activeareas",
                  __FILE__,VDispAreas,group);
@@ -3466,6 +3658,10 @@ void ViewerTest::Commands(Draw_Interpretor& theCommands)
                  "vardisp           : erase  active entities",
                  __FILE__,VClearSensi,group);
 
+  theCommands.Add("vselprecision",
+                 "vselprecision : vselprecision [precision_mode [tolerance_value]]",
+                 __FILE__,VSelPrecision,group);
+
   theCommands.Add("vperf",
                  "vperf: vperf  ShapeName 1/0(Transfo/Location) 1/0(Primitives sensibles ON/OFF)",
                  __FILE__,VPerf,group);