0024337: Draw Harness - relax vinit syntax
[occt.git] / src / ViewerTest / ViewerTest_ViewerCommands.cxx
1 // Created on: 1998-09-01
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21 #ifdef HAVE_CONFIG_H
22   #include <config.h>
23 #endif
24
25 #include <OpenGl_GlCore20.hxx>
26 #include <AIS_Shape.hxx>
27 #include <AIS_Drawer.hxx>
28 #include <AIS_InteractiveObject.hxx>
29 #include <AIS_ListOfInteractive.hxx>
30 #include <AIS_ListIteratorOfListOfInteractive.hxx>
31 #include <DBRep.hxx>
32 #include <Graphic3d_AspectMarker3d.hxx>
33 #include <Graphic3d_ExportFormat.hxx>
34 #include <Graphic3d_NameOfTextureEnv.hxx>
35 #include <Graphic3d_TextureEnv.hxx>
36 #include <Graphic3d_TextureParams.hxx>
37 #include <Graphic3d_TypeOfTextureFilter.hxx>
38 #include <Graphic3d_AspectFillArea3d.hxx>
39 #include <ViewerTest.hxx>
40 #include <ViewerTest_EventManager.hxx>
41 #include <ViewerTest_DoubleMapOfInteractiveAndName.hxx>
42 #include <ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName.hxx>
43 #include <Visual3d_View.hxx>
44 #include <Visual3d_ViewManager.hxx>
45 #include <V3d_AmbientLight.hxx>
46 #include <V3d_DirectionalLight.hxx>
47 #include <V3d_LayerMgr.hxx>
48 #include <V3d_LayerMgrPointer.hxx>
49 #include <V3d_PositionalLight.hxx>
50 #include <V3d_SpotLight.hxx>
51 #include <NCollection_DoubleMap.hxx>
52 #include <NCollection_List.hxx>
53 #include <NCollection_Vector.hxx>
54 #include <NIS_View.hxx>
55 #include <NIS_Triangulated.hxx>
56 #include <NIS_InteractiveContext.hxx>
57 #include <AIS_InteractiveContext.hxx>
58 #include <Draw_Interpretor.hxx>
59 #include <Draw.hxx>
60 #include <Draw_Appli.hxx>
61 #include <Aspect_PrintAlgo.hxx>
62 #include <Image_AlienPixMap.hxx>
63 #include <OpenGl_GraphicDriver.hxx>
64 #include <OSD_Timer.hxx>
65 #include <TColStd_SequenceOfInteger.hxx>
66 #include <TColStd_HSequenceOfReal.hxx>
67 #include <TColgp_Array1OfPnt2d.hxx>
68 #include <Visual3d_LayerItem.hxx>
69 #include <Aspect_TypeOfLine.hxx>
70 #include <Image_Diff.hxx>
71 #include <Aspect_DisplayConnection.hxx>
72 #include <Graphic3d.hxx>
73 #include <gp_Pnt.hxx>
74 #include <gp_Dir.hxx>
75 #include <gp_Pln.hxx>
76 #include <PrsMgr_PresentableObject.hxx>
77 #include <Graphic3d_ClipPlane.hxx>
78 #include <NCollection_DataMap.hxx>
79 #include <Graphic3d_Texture2Dmanual.hxx>
80 #include <Prs3d_ShadingAspect.hxx>
81
82 #ifdef WNT
83 #undef DrawText
84 #endif
85
86 #include <Visual3d_Layer.hxx>
87 #include <cstdlib>
88
89 #if defined(_WIN32)
90   #include <WNT_WClass.hxx>
91   #include <WNT_Window.hxx>
92
93   #if defined(_MSC_VER)
94     #define _CRT_SECURE_NO_DEPRECATE
95     #pragma warning (disable:4996)
96   #endif
97 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
98   #include <Cocoa_Window.hxx>
99 #else
100   #include <Xw_Window.hxx>
101   #include <X11/Xlib.h> /* contains some dangerous #defines such as Status, True etc. */
102   #include <X11/Xutil.h>
103   #include <tk.h>
104 #endif
105
106 // Auxiliary definitions
107 static const char THE_KEY_DELETE = 127;
108
109 //==============================================================================
110 //  VIEWER GLOBAL VARIABLES
111 //==============================================================================
112
113 Standard_IMPORT Standard_Boolean Draw_VirtualWindows;
114 Standard_IMPORT Standard_Boolean Draw_Interprete (const char* theCommand);
115
116 Standard_EXPORT int ViewerMainLoop(Standard_Integer , const char** argv);
117 extern const Handle(NIS_InteractiveContext)& TheNISContext();
118 extern ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
119
120 extern int VErase (Draw_Interpretor& theDI,
121                    Standard_Integer  theArgNb,
122                    const char**      theArgVec);
123
124 #if defined(_WIN32)
125 static Handle(WNT_Window)& VT_GetWindow() {
126   static Handle(WNT_Window) WNTWin;
127   return WNTWin;
128 }
129 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
130 static Handle(Cocoa_Window)& VT_GetWindow()
131 {
132   static Handle(Cocoa_Window) aWindow;
133   return aWindow;
134 }
135 extern void ViewerTest_SetCocoaEventManagerView (const Handle(Cocoa_Window)& theWindow);
136 extern void SetCocoaWindowTitle (const Handle(Cocoa_Window)& theWindow, Standard_CString theTitle);
137 extern void GetCocoaScreenResolution (Standard_Integer& theWidth, Standard_Integer& theHeight);
138
139 #else
140 static Handle(Xw_Window)& VT_GetWindow(){
141   static Handle(Xw_Window) XWWin;
142   return XWWin;
143 }
144
145 static void VProcessEvents(ClientData,int);
146 #endif
147
148 static Handle(Aspect_DisplayConnection)& GetDisplayConnection()
149 {
150   static Handle(Aspect_DisplayConnection) aDisplayConnection;
151   return aDisplayConnection;
152 }
153
154 static void SetDisplayConnection (const Handle(Aspect_DisplayConnection)& theDisplayConnection)
155 {
156   GetDisplayConnection() = theDisplayConnection;
157 }
158
159 #if defined(_WIN32) || (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
160 Aspect_Handle GetWindowHandle(const Handle(Aspect_Window)& theWindow)
161 {
162   Aspect_Handle aWindowHandle = (Aspect_Handle)NULL;
163 #if defined(_WIN32)
164   const Handle (WNT_Window) aWindow = Handle(WNT_Window)::DownCast (theWindow);
165   if (!aWindow.IsNull())
166     return aWindow->HWindow();
167 #elif (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
168   const Handle (Xw_Window) aWindow = Handle(Xw_Window)::DownCast (theWindow);
169   if (!aWindow.IsNull())
170   return aWindow->XWindow();
171 #endif
172   return aWindowHandle;
173 }
174 #endif
175
176 static Standard_Boolean MyHLRIsOn = Standard_False;
177
178 NCollection_DoubleMap <TCollection_AsciiString, Handle(V3d_View)> ViewerTest_myViews;
179 static NCollection_DoubleMap <TCollection_AsciiString, Handle(AIS_InteractiveContext)>  ViewerTest_myContexts;
180 static NCollection_DoubleMap <TCollection_AsciiString, Handle(Graphic3d_GraphicDriver)> ViewerTest_myDrivers;
181 static OpenGl_Caps ViewerTest_myDefaultCaps;
182
183 #define ZCLIPWIDTH 1.
184
185 static void OSWindowSetup();
186
187 //==============================================================================
188 //  EVENT GLOBAL VARIABLES
189 //==============================================================================
190
191 static int Start_Rot = 0;
192 static int ZClipIsOn = 0;
193 int X_Motion = 0; // Current cursor position
194 int Y_Motion = 0;
195 int X_ButtonPress = 0; // Last ButtonPress position
196 int Y_ButtonPress = 0;
197 Standard_Boolean IsDragged = Standard_False;
198 Standard_Boolean DragFirst;
199
200 //==============================================================================
201
202 #ifdef WNT
203 static LRESULT WINAPI ViewerWindowProc(
204                                        HWND hwnd,
205                                        UINT uMsg,
206                                        WPARAM wParam,
207                                        LPARAM lParam );
208 static LRESULT WINAPI AdvViewerWindowProc(
209   HWND hwnd,
210   UINT uMsg,
211   WPARAM wParam,
212   LPARAM lParam );
213 #endif
214
215
216 //==============================================================================
217 //function : WClass
218 //purpose  :
219 //==============================================================================
220
221 const Handle(MMgt_TShared)& ViewerTest::WClass()
222 {
223   static Handle(MMgt_TShared) theWClass;
224 #if defined(_WIN32)
225   if (theWClass.IsNull())
226   {
227     theWClass = new WNT_WClass ("GW3D_Class", AdvViewerWindowProc,
228       CS_VREDRAW | CS_HREDRAW, 0, 0,
229       ::LoadCursor (NULL, IDC_ARROW));
230   }
231 #endif
232   return theWClass;
233 }
234
235 //==============================================================================
236 //function : CreateName
237 //purpose  : Create numerical name for new object in theMap
238 //==============================================================================
239 template <typename ObjectType>
240 TCollection_AsciiString CreateName (const NCollection_DoubleMap <TCollection_AsciiString, ObjectType>& theObjectMap,
241                                     const TCollection_AsciiString& theDefaultString)
242 {
243   if (theObjectMap.IsEmpty())
244     return theDefaultString + TCollection_AsciiString(1);
245
246   Standard_Integer aNextKey = 1;
247   Standard_Boolean isFound = Standard_False;
248   while (!isFound)
249   {
250     TCollection_AsciiString aStringKey = theDefaultString + TCollection_AsciiString(aNextKey);
251     // Look for objects with default names
252     if (theObjectMap.IsBound1(aStringKey))
253     {
254       aNextKey++;
255     }
256     else
257       isFound = Standard_True;
258   }
259
260   return theDefaultString + TCollection_AsciiString(aNextKey);
261 }
262
263 //==============================================================================
264 //structure : ViewerTest_Names
265 //purpose   : Allow to operate with full view name: driverName/viewerName/viewName
266 //==============================================================================
267 struct ViewerTest_Names
268 {
269 private:
270   TCollection_AsciiString myDriverName;
271   TCollection_AsciiString myViewerName;
272   TCollection_AsciiString myViewName;
273
274 public:
275
276   const TCollection_AsciiString& GetDriverName () const
277   {
278     return myDriverName;
279   }
280   void SetDriverName (const TCollection_AsciiString& theDriverName)
281   {
282     myDriverName = theDriverName;
283   }
284   const TCollection_AsciiString& GetViewerName () const
285   {
286     return myViewerName;
287   }
288   void SetViewerName (const TCollection_AsciiString& theViewerName)
289   {
290     myViewerName = theViewerName;
291   }
292   const TCollection_AsciiString& GetViewName () const
293   {
294     return myViewName;
295   }
296   void SetViewName (const TCollection_AsciiString& theViewName)
297   {
298     myViewName = theViewName;
299   }
300
301   //===========================================================================
302   //function : Constructor for ViewerTest_Names
303   //purpose  : Get view, viewer, driver names from custom string
304   //===========================================================================
305
306   ViewerTest_Names (const TCollection_AsciiString& theInputString)
307   {
308     TCollection_AsciiString aName(theInputString);
309     if (theInputString.IsEmpty())
310     {
311       // Get current configuration
312       if (ViewerTest_myDrivers.IsEmpty())
313         myDriverName = CreateName<Handle(Graphic3d_GraphicDriver)>
314           (ViewerTest_myDrivers, TCollection_AsciiString("Driver"));
315       else
316         myDriverName = ViewerTest_myDrivers.Find2
317         (ViewerTest::GetAISContext()->CurrentViewer()->Driver());
318
319       if(ViewerTest_myContexts.IsEmpty())
320       {
321         myViewerName = CreateName <Handle(AIS_InteractiveContext)>
322           (ViewerTest_myContexts, TCollection_AsciiString (myDriverName + "/Viewer"));
323       }
324       else
325         myViewerName = ViewerTest_myContexts.Find2 (ViewerTest::GetAISContext());
326
327         myViewName = CreateName <Handle(V3d_View)>
328           (ViewerTest_myViews, TCollection_AsciiString(myViewerName + "/View"));
329     }
330     else
331     {
332       // There is at least view name
333       Standard_Integer aParserNumber = 0;
334       for (Standard_Integer i = 0; i < 3; ++i)
335       {
336         Standard_Integer aParserPos = aName.SearchFromEnd("/");
337         if(aParserPos != -1)
338         {
339           aParserNumber++;
340           aName.Split(aParserPos-1);
341         }
342         else
343           break;
344       }
345       if (aParserNumber == 0)
346       {
347         // Only view name
348         if (!ViewerTest::GetAISContext().IsNull())
349         {
350           myDriverName = ViewerTest_myDrivers.Find2
351           (ViewerTest::GetAISContext()->CurrentViewer()->Driver());
352           myViewerName = ViewerTest_myContexts.Find2
353           (ViewerTest::GetAISContext());
354         }
355         else
356         {
357           // There is no opened contexts here, need to create names for viewer and driver
358           myDriverName = CreateName<Handle(Graphic3d_GraphicDriver)>
359             (ViewerTest_myDrivers, TCollection_AsciiString("Driver"));
360
361           myViewerName = CreateName <Handle(AIS_InteractiveContext)>
362             (ViewerTest_myContexts, TCollection_AsciiString (myDriverName + "/Viewer"));
363         }
364         myViewName = TCollection_AsciiString(myViewerName + "/" + theInputString);
365       }
366       else if (aParserNumber == 1)
367       {
368         // Here is viewerName/viewName
369         if (!ViewerTest::GetAISContext().IsNull())
370           myDriverName = ViewerTest_myDrivers.Find2
371           (ViewerTest::GetAISContext()->CurrentViewer()->Driver());
372         else
373         {
374           // There is no opened contexts here, need to create name for driver
375           myDriverName = CreateName<Handle(Graphic3d_GraphicDriver)>
376             (ViewerTest_myDrivers, TCollection_AsciiString("Driver"));
377         }
378         myViewerName = TCollection_AsciiString(myDriverName + "/" + aName);
379
380         myViewName = TCollection_AsciiString(myDriverName + "/" + theInputString);
381       }
382       else
383       {
384         //Here is driverName/viewerName/viewName
385         myDriverName = TCollection_AsciiString(aName);
386
387         TCollection_AsciiString aViewerName(theInputString);
388         aViewerName.Split(aViewerName.SearchFromEnd("/") - 1);
389         myViewerName = TCollection_AsciiString(aViewerName);
390
391         myViewName = TCollection_AsciiString(theInputString);
392       }
393     }
394   }
395 };
396
397 //==============================================================================
398 //function : FindContextByView
399 //purpose  : Find AIS_InteractiveContext by View
400 //==============================================================================
401
402 Handle(AIS_InteractiveContext) FindContextByView (const Handle(V3d_View)& theView)
403 {
404   Handle(AIS_InteractiveContext) anAISContext;
405
406   for (NCollection_DoubleMap<TCollection_AsciiString, Handle(AIS_InteractiveContext)>::Iterator
407        anIter (ViewerTest_myContexts); anIter.More(); anIter.Next())
408   {
409     if (anIter.Value()->CurrentViewer() == theView->Viewer())
410        return anIter.Key2();
411   }
412   return anAISContext;
413 }
414
415
416 //==============================================================================
417 //function : SetWindowTitle
418 //purpose  : Set window title
419 //==============================================================================
420
421 void SetWindowTitle (const Handle(Aspect_Window)& theWindow,
422                      Standard_CString theTitle)
423 {
424 #if defined(_WIN32)
425   SetWindowText ((HWND)Handle(WNT_Window)::DownCast(theWindow)->HWindow(),
426     theTitle);
427 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
428   SetCocoaWindowTitle (Handle(Cocoa_Window)::DownCast(theWindow), theTitle);
429 #else
430   if(GetDisplayConnection()->GetDisplay())
431   {
432     Window aWindow =
433       Handle(Xw_Window)::DownCast(theWindow)->XWindow();
434     XStoreName (GetDisplayConnection()->GetDisplay(), aWindow , theTitle);
435   }
436 #endif
437 }
438
439 //==============================================================================
440 //function : IsWindowOverlapped
441 //purpose  : Check if theWindow overlapp another view
442 //==============================================================================
443
444 Standard_Boolean IsWindowOverlapped (const Standard_Integer thePxLeft,
445                                      const Standard_Integer thePxTop,
446                                      const Standard_Integer thePxRight,
447                                      const Standard_Integer thePxBottom,
448                                      TCollection_AsciiString& theViewId)
449 {
450   for(NCollection_DoubleMap <TCollection_AsciiString, Handle(V3d_View)>::Iterator
451       anIter(ViewerTest_myViews); anIter.More(); anIter.Next())
452   {
453     Standard_Integer aTop = 0,
454       aLeft = 0,
455       aRight = 0,
456       aBottom = 0;
457     anIter.Value()->Window()->Position(aLeft, aTop, aRight, aBottom);
458     if ((thePxLeft >= aLeft && thePxLeft <= aRight && thePxTop >= aTop && thePxTop <= aBottom) ||
459         (thePxLeft >= aLeft && thePxLeft <= aRight && thePxBottom >= aTop && thePxBottom <= aBottom) ||
460         (thePxRight >= aLeft && thePxRight <= aRight && thePxTop >= aTop && thePxTop <= aBottom) ||
461         (thePxRight >= aLeft && thePxRight <= aRight && thePxBottom >= aTop && thePxBottom <= aBottom))
462     {
463       theViewId = anIter.Key1();
464       return Standard_True;
465     }
466   }
467   return Standard_False;
468 }
469
470 // Workaround: to create and delete non-orthographic views outside ViewerTest
471 void ViewerTest::RemoveViewName (const TCollection_AsciiString& theName)
472 {
473   ViewerTest_myViews.UnBind1 (theName);
474 }
475
476 void ViewerTest::InitViewName (const TCollection_AsciiString& theName,
477                                const Handle(V3d_View)& theView)
478 {
479   ViewerTest_myViews.Bind (theName, theView);
480 }
481
482 TCollection_AsciiString ViewerTest::GetCurrentViewName ()
483 {
484   return ViewerTest_myViews.Find2( ViewerTest::CurrentView());
485 }
486 //==============================================================================
487 //function : ViewerInit
488 //purpose  : Create the window viewer and initialize all the global variable
489 //==============================================================================
490
491 TCollection_AsciiString ViewerTest::ViewerInit (const Standard_Integer thePxLeft,
492                                                 const Standard_Integer thePxTop,
493                                                 const Standard_Integer thePxWidth,
494                                                 const Standard_Integer thePxHeight,
495                                                 Standard_CString theViewName,
496                                                 Standard_CString theDisplayName)
497 {
498   // Default position and dimension of the viewer window.
499   // Note that left top corner is set to be sufficiently small to have
500   // window fit in the small screens (actual for remote desktops, see #23003).
501   // The position corresponds to the window's client area, thus some
502   // gap is added for window frame to be visible.
503   Standard_Integer aPxLeft   = 20;
504   Standard_Integer aPxTop    = 40;
505   Standard_Integer aPxWidth  = 409;
506   Standard_Integer aPxHeight = 409;
507   Standard_Boolean toCreateViewer = Standard_False;
508
509   Handle(OpenGl_GraphicDriver) aGraphicDriver;
510   ViewerTest_Names aViewNames(theViewName);
511   if (ViewerTest_myViews.IsBound1 (aViewNames.GetViewName ()))
512     aViewNames.SetViewName (aViewNames.GetViewerName() + "/" + CreateName<Handle(V3d_View)>(ViewerTest_myViews, "View"));
513
514   if (thePxLeft != 0)
515     aPxLeft = thePxLeft;
516   if (thePxTop != 0)
517     aPxTop = thePxTop;
518   if (thePxWidth != 0)
519     aPxWidth = thePxWidth;
520   if (thePxHeight != 0)
521     aPxHeight = thePxHeight;
522
523   // Get graphic driver (create it or get from another view)
524   if (!ViewerTest_myDrivers.IsBound1 (aViewNames.GetDriverName()))
525   {
526     // Get connection string
527   #if !defined(_WIN32) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
528     TCollection_AsciiString aDisplayName(theDisplayName);
529     if (!aDisplayName.IsEmpty())
530       SetDisplayConnection (new Aspect_DisplayConnection ());
531     else
532       SetDisplayConnection (new Aspect_DisplayConnection (aDisplayName));
533   #else
534     (void)theDisplayName; // avoid warning on unused argument
535     SetDisplayConnection (new Aspect_DisplayConnection ());
536   #endif
537     aGraphicDriver = new OpenGl_GraphicDriver (GetDisplayConnection());
538     aGraphicDriver->ChangeOptions() = ViewerTest_myDefaultCaps;
539     ViewerTest_myDrivers.Bind (aViewNames.GetDriverName(), aGraphicDriver);
540     toCreateViewer = Standard_True;
541   }
542   else
543   {
544     aGraphicDriver = Handle(OpenGl_GraphicDriver)::DownCast (ViewerTest_myDrivers.Find1 (aViewNames.GetDriverName()));
545   }
546
547   //Dispose the window if input parameters are default
548   if (!ViewerTest_myViews.IsEmpty() && thePxLeft == 0 && thePxTop == 0)
549   {
550     Standard_Integer aTop = 0,
551                      aLeft = 0,
552                      aRight = 0,
553                      aBottom = 0,
554                      aScreenWidth = 0,
555                      aScreenHeight = 0;
556
557     // Get screen resolution
558 #if defined(_WIN32) || defined(__WIN32__)
559     RECT aWindowSize;
560     GetClientRect(GetDesktopWindow(), &aWindowSize);
561     aScreenHeight = aWindowSize.bottom;
562     aScreenWidth = aWindowSize.right;
563 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
564     GetCocoaScreenResolution (aScreenWidth, aScreenHeight);
565 #else
566     Screen *aScreen = DefaultScreenOfDisplay(GetDisplayConnection()->GetDisplay());
567     aScreenWidth = WidthOfScreen(aScreen);
568     aScreenHeight = HeightOfScreen(aScreen);
569 #endif
570
571     TCollection_AsciiString anOverlappedViewId("");
572
573     while (IsWindowOverlapped (aPxLeft, aPxTop, aPxLeft + aPxWidth, aPxTop + aPxHeight, anOverlappedViewId))
574     {
575       ViewerTest_myViews.Find1(anOverlappedViewId)->Window()->Position (aLeft, aTop, aRight, aBottom);
576
577       if (IsWindowOverlapped (aRight + 20, aPxTop, aRight + 20 + aPxWidth, aPxTop + aPxHeight, anOverlappedViewId)
578         && aRight + 2*aPxWidth + 40 > aScreenWidth)
579       {
580         if (aBottom + aPxHeight + 40 > aScreenHeight)
581         {
582           aPxLeft = 20;
583           aPxTop = 40;
584           break;
585         }
586         aPxLeft = 20;
587         aPxTop = aBottom + 40;
588       }
589       else
590         aPxLeft = aRight + 20;
591     }
592   }
593
594   // Get viewer name
595   TCollection_AsciiString aTitle("3D View - ");
596   aTitle = aTitle + aViewNames.GetViewName() + "(*)";
597
598   // Change name of current active window
599   if (!ViewerTest::CurrentView().IsNull())
600   {
601     TCollection_AsciiString aTitle("3D View - ");
602     aTitle = aTitle
603       + ViewerTest_myViews.Find2 (ViewerTest::CurrentView());
604     SetWindowTitle (ViewerTest::CurrentView()->Window(), aTitle.ToCString());
605   }
606
607   // Create viewer
608   Handle(V3d_Viewer) a3DViewer;
609   // If it's the single view, we first look for empty context
610   if (ViewerTest_myViews.IsEmpty() && !ViewerTest_myContexts.IsEmpty())
611   {
612     NCollection_DoubleMap <TCollection_AsciiString, Handle(AIS_InteractiveContext)>::Iterator
613       anIter(ViewerTest_myContexts);
614     if (anIter.More())
615       ViewerTest::SetAISContext (anIter.Value());
616     a3DViewer = ViewerTest::GetAISContext()->CurrentViewer();
617   }
618   else if (ViewerTest_myContexts.IsBound1(aViewNames.GetViewerName()))
619   {
620     ViewerTest::SetAISContext(ViewerTest_myContexts.Find1(aViewNames.GetViewerName()));
621     a3DViewer = ViewerTest::GetAISContext()->CurrentViewer();
622   }
623   else if (a3DViewer.IsNull())
624   {
625     toCreateViewer = Standard_True;
626     TCollection_ExtendedString NameOfWindow("Viewer3D");
627     a3DViewer = new V3d_Viewer(aGraphicDriver, NameOfWindow.ToExtString());
628
629     NameOfWindow = TCollection_ExtendedString("Collector");
630
631     a3DViewer->SetDefaultBackgroundColor(Quantity_NOC_BLACK);
632   }
633
634   // AIS context setup
635   if (ViewerTest::GetAISContext().IsNull() ||
636       !(ViewerTest_myContexts.IsBound1(aViewNames.GetViewerName())))
637   {
638     Handle(AIS_InteractiveContext) aContext = new AIS_InteractiveContext (a3DViewer);
639     ViewerTest::SetAISContext (aContext);
640     ViewerTest_myContexts.Bind (aViewNames.GetViewerName(), ViewerTest::GetAISContext());
641   }
642   else
643   {
644     ViewerTest::ResetEventManager();
645   }
646
647   // Create window
648 #if defined(_WIN32)
649   VT_GetWindow() = new WNT_Window (aTitle.ToCString(),
650                                     Handle(WNT_WClass)::DownCast (WClass()),
651                                     Draw_VirtualWindows ? WS_POPUPWINDOW : WS_OVERLAPPEDWINDOW,
652                                     aPxLeft, aPxTop,
653                                     aPxWidth, aPxHeight,
654                                     Quantity_NOC_BLACK);
655 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
656   VT_GetWindow() = new Cocoa_Window (aTitle.ToCString(),
657                                      aPxLeft, aPxTop,
658                                      aPxWidth, aPxHeight);
659   ViewerTest_SetCocoaEventManagerView (VT_GetWindow());
660 #else
661   VT_GetWindow() = new Xw_Window (aGraphicDriver->GetDisplayConnection(),
662                                   aTitle.ToCString(),
663                                   aPxLeft, aPxTop,
664                                   aPxWidth, aPxHeight);
665 #endif
666   VT_GetWindow()->SetVirtual (Draw_VirtualWindows);
667
668   // NIS setup
669   Handle(NIS_View) aView = new NIS_View (a3DViewer, VT_GetWindow());
670
671   ViewerTest::CurrentView(aView);
672   ViewerTest_myViews.Bind (aViewNames.GetViewName(), aView);
673   TheNISContext()->AttachView (aView);
674
675   // Setup for X11 or NT
676   OSWindowSetup();
677
678   // Set parameters for V3d_View and V3d_Viewer
679   const Handle (V3d_View) aV3dView = ViewerTest::CurrentView();
680   aV3dView->SetComputedMode(Standard_False);
681   MyHLRIsOn = aV3dView->ComputedMode();
682   aV3dView->SetZClippingDepth(0.5);
683   aV3dView->SetZClippingWidth(ZCLIPWIDTH/2.);
684
685   a3DViewer->SetDefaultBackgroundColor(Quantity_NOC_BLACK);
686   if (toCreateViewer)
687   {
688     a3DViewer->SetDefaultLights();
689     a3DViewer->SetLightOn();
690   }
691
692   #if !defined(_WIN32) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
693   #if TCL_MAJOR_VERSION  < 8
694   Tk_CreateFileHandler((void*)XConnectionNumber(GetDisplayConnection()->GetDisplay()),
695       TK_READABLE, VProcessEvents, (ClientData) VT_GetWindow()->XWindow() );
696   #else
697   Tk_CreateFileHandler(XConnectionNumber(GetDisplayConnection()->GetDisplay()),
698       TK_READABLE, VProcessEvents, (ClientData) VT_GetWindow()->XWindow() );
699   #endif
700   #endif
701
702   VT_GetWindow()->Map();
703
704   // Set the handle of created view in the event manager
705   ViewerTest::ResetEventManager();
706
707   ViewerTest::CurrentView()->Redraw();
708
709   aView.Nullify();
710   a3DViewer.Nullify();
711
712   return aViewNames.GetViewName();
713 }
714
715 //==============================================================================
716 //function : RedrawAllViews
717 //purpose  : Redraw all created views
718 //==============================================================================
719 void ViewerTest::RedrawAllViews()
720 {
721   NCollection_DoubleMap<TCollection_AsciiString, Handle(V3d_View)>::Iterator aViewIt(ViewerTest_myViews);
722   for (; aViewIt.More(); aViewIt.Next())
723   {
724     const Handle(V3d_View)& aView = aViewIt.Key2();
725     aView->Redraw();
726   }
727 }
728
729 //==============================================================================
730 //function : splitParameter
731 //purpose  : Split parameter string to parameter name an parameter value
732 //==============================================================================
733 Standard_Boolean splitParameter (const TCollection_AsciiString& theString,
734                                  TCollection_AsciiString&       theName,
735                                  TCollection_AsciiString&       theValue)
736 {
737   Standard_Integer aParamNameEnd = theString.FirstLocationInSet("=",1, theString.Length());
738   if (aParamNameEnd == 0)
739     return Standard_False;
740   TCollection_AsciiString aString(theString);
741   if (aParamNameEnd != 0)
742   {
743     theValue = aString.Split(aParamNameEnd);
744     aString.Split(aString.Length()-1);
745     theName = aString;
746   }
747   return Standard_True;
748 }
749
750 //==============================================================================
751 //function : Vinit
752 //purpose  : Create the window viewer and initialize all the global variable
753 //    Use Tk_CreateFileHandler on UNIX to catch the X11 Viewer event
754 //==============================================================================
755
756 static int VInit (Draw_Interpretor& theDi, Standard_Integer theArgsNb, const char** theArgVec)
757 {
758   if (theArgsNb > 9)
759   {
760     std::cerr << theArgVec[0] << ": incorrect number of command arguments.\n"
761               << "Type help for more information.\n";
762     return 1;
763   }
764
765   TCollection_AsciiString aViewName, aDisplayName;
766   Standard_Integer aPxLeft = 0, aPxTop = 0, aPxWidth = 0, aPxHeight = 0;
767   TCollection_AsciiString aName, aValue;
768   for (Standard_Integer anArgIt = 1; anArgIt < theArgsNb; ++anArgIt)
769   {
770     const TCollection_AsciiString anArg = theArgVec[anArgIt];
771     TCollection_AsciiString anArgCase = anArg;
772     anArgCase.UpperCase();
773     if (splitParameter (anArg, aName, aValue))
774     {
775       aName.UpperCase();
776       if (aName.IsEqual ("NAME"))
777       {
778         aViewName = aValue;
779       }
780       else if (aName.IsEqual ("L")
781             || aName.IsEqual ("LEFT"))
782       {
783         aPxLeft = aValue.IntegerValue();
784       }
785       else if (aName.IsEqual ("T")
786             || aName.IsEqual ("TOP"))
787       {
788         aPxTop = aValue.IntegerValue();
789       }
790     #if !defined(_WIN32) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
791       else if (aName.IsEqual ("DISP")
792             || aName.IsEqual ("DISPLAY"))
793       {
794         aDisplayName = aValue;
795       }
796     #endif
797       else if (aName.IsEqual ("W")
798             || aName.IsEqual ("WIDTH"))
799       {
800         aPxWidth = aValue.IntegerValue();
801       }
802       else if (aName.IsEqual ("H")
803             || aName.IsEqual ("HEIGHT"))
804       {
805         aPxHeight = aValue.IntegerValue();
806       }
807       else
808       {
809         std::cerr << theArgVec[0] << ": Warning: unknown argument " << anArg << ".\n";
810       }
811     }
812     else if (aViewName.IsEmpty())
813     {
814       aViewName = anArg;
815     }
816     else
817     {
818       std::cerr << theArgVec[0] << ": Warning: unknown argument " << anArg << ".\n";
819     }
820   }
821
822   ViewerTest_Names aViewNames (aViewName);
823   if (ViewerTest_myViews.IsBound1 (aViewNames.GetViewName()))
824   {
825     TCollection_AsciiString aCommand = TCollection_AsciiString ("vactivate ") + aViewNames.GetViewName();
826     theDi.Eval (aCommand.ToCString());
827     return 0;
828   }
829
830   TCollection_AsciiString aViewId = ViewerTest::ViewerInit (aPxLeft, aPxTop, aPxWidth, aPxHeight,
831                                                             aViewName.ToCString(),
832                                                             aDisplayName.ToCString());
833   theDi << aViewId;
834   return 0;
835 }
836
837 //==============================================================================
838 //function : VHLR
839 //purpose  : hidden lines removal algorithm
840 //==============================================================================
841
842 static int VHLR (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
843 {
844   if (ViewerTest::CurrentView().IsNull())
845   {
846     di << argv[0] << ": Call vinit before this command, please.\n";
847     return 1;
848   }
849
850   if (argc != 2)
851   {
852     di << argv[0] << ": Wrong number of command arguments.\n"
853       << "Type help " << argv[0] << " for more information.\n";
854     return 1;
855   }
856
857   Standard_Boolean isHLROn =
858     (!strcasecmp (argv[1], "on")) ? Standard_True : Standard_False;
859
860   if (isHLROn == MyHLRIsOn)
861   {
862     return 0;
863   }
864
865   MyHLRIsOn = isHLROn;
866   ViewerTest::CurrentView()->SetComputedMode (MyHLRIsOn);
867
868   return 0;
869 }
870
871 //==============================================================================
872 //function : VHLRType
873 //purpose  : change type of using HLR algorithm
874 //==============================================================================
875
876 static int VHLRType (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
877 {
878   if (ViewerTest::CurrentView().IsNull())
879   {
880     di << argv[0] << ": Call vinit before this command, please.\n";
881     return 1;
882   }
883
884   if (argc < 2)
885   {
886     di << argv[0] << ": Wrong number of command arguments.\n"
887       << "Type help " << argv[0] << " for more information.\n";
888     return 1;
889   }
890
891   Prs3d_TypeOfHLR aTypeOfHLR =
892     (!strcasecmp (argv[1], "algo")) ? Prs3d_TOH_Algo : Prs3d_TOH_PolyAlgo;
893
894   if (argc == 2)
895   {
896     AIS_ListOfInteractive aListOfShapes;
897     ViewerTest::GetAISContext()->DisplayedObjects (aListOfShapes);
898     ViewerTest::GetAISContext()->DefaultDrawer()->SetTypeOfHLR(aTypeOfHLR);
899     for (AIS_ListIteratorOfListOfInteractive anIter(aListOfShapes);
900       anIter.More(); anIter.Next())
901     {
902       Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anIter.Value());
903       if (aShape.IsNull())
904         continue;
905       if (aShape->TypeOfHLR() != aTypeOfHLR)
906         aShape->SetTypeOfHLR (aTypeOfHLR);
907       if (MyHLRIsOn)
908         aShape->Redisplay();
909     }
910     ViewerTest::CurrentView()->Update();
911     return 0;
912   }
913   else
914   {
915     for (Standard_Integer i = 2; i < argc; ++i)
916     {
917       ViewerTest_DoubleMapOfInteractiveAndName& aMap = GetMapOfAIS();
918       TCollection_AsciiString aName (argv[i]);
919
920       if (!aMap.IsBound2 (aName))
921       {
922         di << argv[0] << ":" << " Wrong shape name:" << aName.ToCString() << ".\n";
923         continue;
924       }
925       Handle(AIS_Shape) anAISObject =
926         Handle(AIS_Shape)::DownCast (aMap.Find2(aName));
927       if (anAISObject.IsNull())
928         continue;
929       anAISObject->SetTypeOfHLR (aTypeOfHLR);
930       if (MyHLRIsOn)
931         anAISObject->Redisplay();
932     }
933     ViewerTest::CurrentView()->Update();
934   }
935
936   return 0;
937 }
938
939 //==============================================================================
940 //function : FindViewIdByWindowHandle
941 //purpose  : Find theView Id in the map of views by window handle
942 //==============================================================================
943 #if defined(_WIN32) || defined(__WIN32__) || (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
944 TCollection_AsciiString FindViewIdByWindowHandle(const Aspect_Handle theWindowHandle)
945 {
946   for (NCollection_DoubleMap<TCollection_AsciiString, Handle(V3d_View)>::Iterator
947        anIter(ViewerTest_myViews); anIter.More(); anIter.Next())
948   {
949     Aspect_Handle aWindowHandle = GetWindowHandle(anIter.Value()->Window());
950     if (aWindowHandle == theWindowHandle)
951       return anIter.Key1();
952   }
953   return TCollection_AsciiString("");
954 }
955 #endif
956
957 //==============================================================================
958 //function : ActivateView
959 //purpose  : Make the view active
960 //==============================================================================
961
962 void ActivateView (const TCollection_AsciiString& theViewName)
963 {
964   const Handle(V3d_View) aView = ViewerTest_myViews.Find1(theViewName);
965   if (!aView.IsNull())
966   {
967     Handle(AIS_InteractiveContext) anAISContext = FindContextByView(aView);
968     if (!anAISContext.IsNull())
969     {
970       if (!ViewerTest::CurrentView().IsNull())
971       {
972         TCollection_AsciiString aTitle("3D View - ");
973         aTitle = aTitle + ViewerTest_myViews.Find2 (ViewerTest::CurrentView());
974         SetWindowTitle (ViewerTest::CurrentView()->Window(), aTitle.ToCString());
975       }
976
977       ViewerTest::CurrentView (aView);
978       // Update degenerate mode
979       MyHLRIsOn = ViewerTest::CurrentView()->ComputedMode();
980       ViewerTest::SetAISContext (anAISContext);
981       TCollection_AsciiString aTitle = TCollection_AsciiString("3D View - ");
982       aTitle = aTitle + theViewName + "(*)";
983       SetWindowTitle (ViewerTest::CurrentView()->Window(), aTitle.ToCString());
984 #if defined(_WIN32) || defined(__WIN32__)
985       VT_GetWindow() = Handle(WNT_Window)::DownCast(ViewerTest::CurrentView()->Window());
986 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
987       VT_GetWindow() = Handle(Cocoa_Window)::DownCast(ViewerTest::CurrentView()->Window());
988 #else
989       VT_GetWindow() = Handle(Xw_Window)::DownCast(ViewerTest::CurrentView()->Window());
990 #endif
991       SetDisplayConnection(ViewerTest::CurrentView()->Viewer()->Driver()->GetDisplayConnection());
992       ViewerTest::CurrentView()->Redraw();
993     }
994   }
995 }
996
997 //==============================================================================
998 //function : RemoveView
999 //purpose  : Close and remove view from display, clear maps if neccessary
1000 //==============================================================================
1001 void ViewerTest::RemoveView (const TCollection_AsciiString& theViewName, const Standard_Boolean isContextRemoved)
1002 {
1003   if (!ViewerTest_myViews.IsBound1(theViewName))
1004   {
1005     cout << "Wrong view name\n";
1006     return;
1007   }
1008
1009   // Activate another view if it's active now
1010   if (ViewerTest_myViews.Find1(theViewName) == ViewerTest::CurrentView())
1011   {
1012     if (ViewerTest_myViews.Extent() > 1)
1013     {
1014       TCollection_AsciiString aNewViewName;
1015       for (NCollection_DoubleMap <TCollection_AsciiString, Handle(V3d_View)> :: Iterator
1016            anIter(ViewerTest_myViews); anIter.More(); anIter.Next())
1017         if (anIter.Key1() != theViewName)
1018         {
1019           aNewViewName = anIter.Key1();
1020           break;
1021         }
1022         ActivateView (aNewViewName);
1023     }
1024     else
1025     {
1026       Handle(V3d_View) anEmptyView;
1027 #if defined(_WIN32) || defined(__WIN32__)
1028       Handle(WNT_Window) anEmptyWindow;
1029 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
1030       Handle(Cocoa_Window) anEmptyWindow;
1031 #else
1032       Handle(Xw_Window) anEmptyWindow;
1033 #endif
1034       VT_GetWindow() = anEmptyWindow;
1035       ViewerTest::CurrentView (anEmptyView);
1036       if (isContextRemoved)
1037       {
1038         Handle(AIS_InteractiveContext) anEmptyContext;
1039         ViewerTest::SetAISContext(anEmptyContext);
1040       }
1041     }
1042   }
1043
1044   // Delete view
1045   Handle(V3d_View) aView = ViewerTest_myViews.Find1(theViewName);
1046   Handle(AIS_InteractiveContext) aCurrentContext = FindContextByView(aView);
1047
1048   // Remove view resources
1049   TheNISContext()->DetachView(Handle(NIS_View)::DownCast(aView));
1050   ViewerTest_myViews.UnBind1(theViewName);
1051   aView->Remove();
1052
1053 #if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
1054   XFlush (GetDisplayConnection()->GetDisplay());
1055 #endif
1056
1057   // Keep context opened only if the closed view is last to avoid
1058   // unused empty contexts
1059   if (!aCurrentContext.IsNull())
1060   {
1061     // Check if there are more difined views in the viewer
1062     aCurrentContext->CurrentViewer()->InitDefinedViews();
1063     if ((isContextRemoved || ViewerTest_myContexts.Size() != 1) && !aCurrentContext->CurrentViewer()->MoreDefinedViews())
1064     {
1065       // Remove driver if there is no viewers that use it
1066       Standard_Boolean isRemoveDriver = Standard_True;
1067       for(NCollection_DoubleMap<TCollection_AsciiString, Handle(AIS_InteractiveContext)>::Iterator
1068           anIter(ViewerTest_myContexts); anIter.More(); anIter.Next())
1069       {
1070         if (aCurrentContext != anIter.Key2() &&
1071           aCurrentContext->CurrentViewer()->Driver() == anIter.Value()->CurrentViewer()->Driver())
1072         {
1073           isRemoveDriver = Standard_False;
1074           break;
1075         }
1076       }
1077       if(isRemoveDriver)
1078       {
1079         ViewerTest_myDrivers.UnBind2 (aCurrentContext->CurrentViewer()->Driver());
1080       #if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
1081         #if TCL_MAJOR_VERSION  < 8
1082         Tk_DeleteFileHandler((void*)XConnectionNumber(aCurrentContext->CurrentViewer()->Driver()->GetDisplayConnection()->GetDisplay()));
1083         #else
1084         Tk_DeleteFileHandler(XConnectionNumber(aCurrentContext->CurrentViewer()->Driver()->GetDisplayConnection()->GetDisplay()));
1085         #endif
1086       #endif
1087       }
1088
1089       ViewerTest_myContexts.UnBind2(aCurrentContext);
1090     }
1091   }
1092   cout << "3D View - " << theViewName << " was deleted.\n";
1093
1094 }
1095
1096 //==============================================================================
1097 //function : VClose
1098 //purpose  : Remove the view defined by its name
1099 //==============================================================================
1100
1101 static int VClose (Draw_Interpretor& /*theDi*/,
1102                    Standard_Integer  theArgsNb,
1103                    const char**      theArgVec)
1104 {
1105   NCollection_List<TCollection_AsciiString> aViewList;
1106   if (theArgsNb > 1)
1107   {
1108     TCollection_AsciiString anArg (theArgVec[1]);
1109     anArg.UpperCase();
1110     if (anArg.IsEqual ("ALL")
1111      || anArg.IsEqual ("*"))
1112     {
1113       for (NCollection_DoubleMap<TCollection_AsciiString, Handle(V3d_View)>::Iterator anIter (ViewerTest_myViews);
1114            anIter.More(); anIter.Next())
1115       {
1116         aViewList.Append (anIter.Key1());
1117       }
1118       if (aViewList.IsEmpty())
1119       {
1120         std::cout << "No view to close\n";
1121         return 0;
1122       }
1123     }
1124     else
1125     {
1126       ViewerTest_Names aViewName (theArgVec[1]);
1127       if (!ViewerTest_myViews.IsBound1 (aViewName.GetViewName()))
1128       {
1129         std::cerr << "The view with name '" << theArgVec[1] << "' does not exist\n";
1130         return 1;
1131       }
1132       aViewList.Append (aViewName.GetViewName());
1133     }
1134   }
1135   else
1136   {
1137     // close active view
1138     if (ViewerTest::CurrentView().IsNull())
1139     {
1140       std::cerr << "No active view!\n";
1141       return 1;
1142     }
1143     aViewList.Append (ViewerTest_myViews.Find2 (ViewerTest::CurrentView()));
1144   }
1145
1146   Standard_Boolean toRemoveContext = (theArgsNb != 3 || Draw::Atoi (theArgVec[2]) != 1);
1147   for (NCollection_List<TCollection_AsciiString>::Iterator anIter(aViewList);
1148        anIter.More(); anIter.Next())
1149   {
1150     ViewerTest::RemoveView (anIter.Value(), toRemoveContext);
1151   }
1152
1153   return 0;
1154 }
1155
1156 //==============================================================================
1157 //function : VActivate
1158 //purpose  : Activate the view defined by its ID
1159 //==============================================================================
1160
1161 static int VActivate (Draw_Interpretor& theDi, Standard_Integer theArgsNb, const char** theArgVec)
1162 {
1163   if (theArgsNb > 2)
1164   {
1165     theDi << theArgVec[0] << ": wrong number of command arguments.\n"
1166     << "Usage: " << theArgVec[0] << " ViewID\n";
1167     return 1;
1168   }
1169   if(theArgsNb == 1)
1170   {
1171     theDi.Eval("vviewlist");
1172     return 0;
1173   }
1174
1175   TCollection_AsciiString aNameString(theArgVec[1]);
1176   if ( strcasecmp( aNameString.ToCString(), "NONE" ) == 0 )
1177   {
1178     TCollection_AsciiString aTitle("3D View - ");
1179     aTitle = aTitle + ViewerTest_myViews.Find2(ViewerTest::CurrentView());
1180     SetWindowTitle (ViewerTest::CurrentView()->Window(), aTitle.ToCString());
1181     Handle(V3d_View) anEmptyView;
1182 #if defined(_WIN32) || defined(__WIN32__)
1183     Handle(WNT_Window) anEmptyWindow;
1184 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
1185     Handle(Cocoa_Window) anEmptyWindow;
1186 #else
1187     Handle(Xw_Window) anEmptyWindow;
1188 #endif
1189     VT_GetWindow() = anEmptyWindow;
1190     ViewerTest::CurrentView (anEmptyView);
1191     ViewerTest::ResetEventManager();
1192     theDi << theArgVec[0] << ": all views are inactive\n";
1193     return 0;
1194   }
1195
1196   ViewerTest_Names aViewNames(aNameString);
1197
1198   // Check if this view exists in the viewer with the driver
1199   if (!ViewerTest_myViews.IsBound1(aViewNames.GetViewName()))
1200   {
1201     theDi << "Wrong view name\n";
1202     return 1;
1203   }
1204
1205   // Check if it is active already
1206   if (ViewerTest::CurrentView() == ViewerTest_myViews.Find1(aViewNames.GetViewName()))
1207   {
1208     theDi << theArgVec[0] << ": the view is active already\n";
1209     return 0;
1210   }
1211
1212   ActivateView (aViewNames.GetViewName());
1213   return 0;
1214 }
1215
1216 //==============================================================================
1217 //function : VViewList
1218 //purpose  : Print current list of views per viewer and graphic driver ID
1219 //           shared between viewers
1220 //==============================================================================
1221
1222 static int VViewList (Draw_Interpretor& theDi, Standard_Integer theArgsNb, const char** theArgVec)
1223 {
1224   if (theArgsNb > 2)
1225   {
1226     theDi << theArgVec[0] << ": Wrong number of command arguments\n"
1227           << "Usage: " << theArgVec[0] << " name";
1228     return 1;
1229   }
1230   if (ViewerTest_myContexts.Size() < 1)
1231     return 0;
1232
1233   Standard_Boolean isTreeView =
1234     (( theArgsNb==1 ) || ( strcasecmp( theArgVec[1], "long" ) != 0 ));
1235
1236   if (isTreeView)
1237     theDi << theArgVec[0] <<":\n";
1238
1239     for (NCollection_DoubleMap <TCollection_AsciiString, Handle(Graphic3d_GraphicDriver)>::Iterator
1240       aDriverIter(ViewerTest_myDrivers); aDriverIter.More(); aDriverIter.Next())
1241     {
1242       if (isTreeView)
1243         theDi << aDriverIter.Key1() << ":\n";
1244
1245       for (NCollection_DoubleMap <TCollection_AsciiString, Handle(AIS_InteractiveContext)>::Iterator
1246         aContextIter(ViewerTest_myContexts); aContextIter.More(); aContextIter.Next())
1247       {
1248         if (aContextIter.Key1().Search(aDriverIter.Key1()) != -1)
1249         {
1250           if (isTreeView)
1251           {
1252             TCollection_AsciiString aContextName(aContextIter.Key1());
1253             theDi << " " << aContextName.Split(aDriverIter.Key1().Length() + 1) << ":" << "\n";
1254           }
1255
1256           for (NCollection_DoubleMap <TCollection_AsciiString, Handle(V3d_View)>::Iterator
1257             aViewIter(ViewerTest_myViews); aViewIter.More(); aViewIter.Next())
1258           {
1259             if (aViewIter.Key1().Search(aContextIter.Key1()) != -1)
1260             {
1261               TCollection_AsciiString aViewName(aViewIter.Key1());
1262               if (isTreeView)
1263               {
1264                 if (aViewIter.Value() == ViewerTest::CurrentView())
1265                   theDi << "  " << aViewName.Split(aContextIter.Key1().Length() + 1) << "(*)" << "\n";
1266                 else
1267                   theDi << "  " << aViewName.Split(aContextIter.Key1().Length() + 1) << "\n";
1268               }
1269               else
1270               {
1271                 theDi << aViewName << " ";
1272               }
1273             }
1274           }
1275         }
1276       }
1277     }
1278   return 0;
1279 }
1280
1281 //==============================================================================
1282 //function : VT_ProcessKeyPress
1283 //purpose  : Handle KeyPress event from a CString
1284 //==============================================================================
1285 void VT_ProcessKeyPress (const char* buf_ret)
1286 {
1287   //cout << "KeyPress" << endl;
1288   const Handle(V3d_View) aView = ViewerTest::CurrentView();
1289   const Handle(NIS_View) aNisView = Handle(NIS_View)::DownCast (aView);
1290   // Letter in alphabetic order
1291
1292   if (!strcasecmp (buf_ret, "A"))
1293   {
1294     // AXO
1295     aView->SetProj(V3d_XposYnegZpos);
1296   }
1297   else if (!strcasecmp (buf_ret, "D"))
1298   {
1299     // Reset
1300     aView->Reset();
1301   }
1302   else if (!strcasecmp (buf_ret, "F"))
1303   {
1304     // FitAll
1305     if (aNisView.IsNull())
1306       aView->FitAll();
1307     else
1308       aNisView->FitAll3d();
1309   }
1310   else if (!strcasecmp (buf_ret, "H"))
1311   {
1312     // HLR
1313     cout << "HLR" << endl;
1314     aView->SetComputedMode (!aView->ComputedMode());
1315     MyHLRIsOn = aView->ComputedMode();
1316   }
1317   else if (!strcasecmp (buf_ret, "P"))
1318   {
1319     // Type of HLR
1320     Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
1321     if (aContext->DefaultDrawer()->TypeOfHLR() == Prs3d_TOH_Algo)
1322       aContext->DefaultDrawer()->SetTypeOfHLR(Prs3d_TOH_PolyAlgo);
1323     else
1324       aContext->DefaultDrawer()->SetTypeOfHLR(Prs3d_TOH_Algo);
1325     if (aContext->NbCurrents()==0 || aContext->NbSelected() == 0)
1326     {
1327       AIS_ListOfInteractive aListOfShapes;
1328       aContext->DisplayedObjects(aListOfShapes);
1329       for (AIS_ListIteratorOfListOfInteractive anIter(aListOfShapes);
1330         anIter.More(); anIter.Next())
1331       {
1332         Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anIter.Value());
1333         if (aShape.IsNull())
1334           continue;
1335         if (aShape->TypeOfHLR() == Prs3d_TOH_PolyAlgo)
1336           aShape->SetTypeOfHLR (Prs3d_TOH_Algo);
1337         else
1338           aShape->SetTypeOfHLR (Prs3d_TOH_PolyAlgo);
1339         aShape->Redisplay();
1340       }
1341     }
1342     else
1343     {
1344       for (aContext->InitCurrent();aContext->MoreCurrent();aContext->NextCurrent())
1345       {
1346         Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(aContext->Current());
1347         if (aShape.IsNull())
1348           continue;
1349         if(aShape->TypeOfHLR() == Prs3d_TOH_PolyAlgo)
1350           aShape->SetTypeOfHLR (Prs3d_TOH_Algo);
1351         else
1352           aShape->SetTypeOfHLR (Prs3d_TOH_PolyAlgo);
1353         aShape->Redisplay();
1354       }
1355     }
1356
1357     aContext->UpdateCurrentViewer();
1358
1359   }
1360   else if (!strcasecmp (buf_ret, "S"))
1361   {
1362     std::cout << "setup Shaded display mode" << std::endl;
1363
1364     Handle(AIS_InteractiveContext) Ctx = ViewerTest::GetAISContext();
1365     if(Ctx->NbCurrents()==0 ||
1366       Ctx->NbSelected()==0)
1367       Ctx->SetDisplayMode(AIS_Shaded);
1368     else{
1369       if(Ctx->HasOpenedContext()){
1370         for(Ctx->InitSelected();Ctx->MoreSelected();Ctx->NextSelected())
1371           Ctx->SetDisplayMode(Ctx->Interactive(),1,Standard_False);
1372       }
1373       else{
1374         for(Ctx->InitCurrent();Ctx->MoreCurrent();Ctx->NextCurrent())
1375           Ctx->SetDisplayMode(Ctx->Current(),1,Standard_False);
1376       }
1377       Ctx->UpdateCurrentViewer();
1378     }
1379   }
1380   else if (!strcasecmp (buf_ret, "U"))
1381   {
1382     // Unset display mode
1383     std::cout << "reset display mode to defaults" << std::endl;
1384
1385     Handle(AIS_InteractiveContext) Ctx = ViewerTest::GetAISContext();
1386     if(Ctx->NbCurrents()==0 ||
1387       Ctx->NbSelected()==0)
1388       Ctx->SetDisplayMode(AIS_WireFrame);
1389     else{
1390       if(Ctx->HasOpenedContext()){
1391         for(Ctx->InitSelected();Ctx->MoreSelected();Ctx->NextSelected())
1392           Ctx->UnsetDisplayMode(Ctx->Interactive(),Standard_False);
1393       }
1394       else{
1395         for(Ctx->InitCurrent();Ctx->MoreCurrent();Ctx->NextCurrent())
1396           Ctx->UnsetDisplayMode(Ctx->Current(),Standard_False);
1397       }
1398       Ctx->UpdateCurrentViewer();
1399     }
1400
1401   }
1402   else if (!strcasecmp (buf_ret, "T"))
1403   {
1404     // Top
1405     aView->SetProj(V3d_Zpos);
1406   }
1407   else if (!strcasecmp (buf_ret, "B"))
1408   {
1409     // Bottom
1410     aView->SetProj(V3d_Zneg);
1411   }
1412   else if (!strcasecmp (buf_ret, "L"))
1413   {
1414     // Left
1415     aView->SetProj(V3d_Xneg);
1416   }
1417   else if (!strcasecmp (buf_ret, "R"))
1418   {
1419     // Right
1420     aView->SetProj(V3d_Xpos);
1421   }
1422   else if (!strcasecmp (buf_ret, "W"))
1423   {
1424     std::cout << "setup WireFrame display mode" << std::endl;
1425     Handle(AIS_InteractiveContext) Ctx = ViewerTest::GetAISContext();
1426     if(Ctx->NbCurrents()==0 ||
1427       Ctx->NbSelected()==0)
1428       Ctx->SetDisplayMode(AIS_WireFrame);
1429     else{
1430       if(Ctx->HasOpenedContext()){
1431         for(Ctx->InitSelected();Ctx->MoreSelected();Ctx->NextSelected())
1432           Ctx->SetDisplayMode(Ctx->Interactive(),0,Standard_False);
1433       }
1434       else{
1435         for(Ctx->InitCurrent();Ctx->MoreCurrent();Ctx->NextCurrent())
1436           Ctx->SetDisplayMode(Ctx->Current(),0,Standard_False);
1437       }
1438       Ctx->UpdateCurrentViewer();
1439     }
1440   }
1441   else if (!strcasecmp (buf_ret, "Z"))
1442   {
1443     // ZCLIP
1444     if ( ZClipIsOn ) {
1445       cout << "ZClipping OFF" << endl;
1446       ZClipIsOn = 0;
1447
1448       aView->SetZClippingType(V3d_OFF);
1449       aView->Redraw();
1450     }
1451     else {
1452       cout << "ZClipping ON" << endl;
1453       ZClipIsOn = 1;
1454
1455       aView->SetZClippingType(V3d_FRONT);
1456       aView->Redraw();
1457     }
1458   }
1459   else if (!strcasecmp (buf_ret, ","))
1460   {
1461     ViewerTest::GetAISContext()->HilightNextDetected(ViewerTest::CurrentView());
1462   }
1463   else if (!strcasecmp (buf_ret, "."))
1464   {
1465     ViewerTest::GetAISContext()->HilightPreviousDetected(ViewerTest::CurrentView());
1466   }
1467   else if (*buf_ret == THE_KEY_DELETE)
1468   {
1469     Handle(AIS_InteractiveContext) aCtx = ViewerTest::GetAISContext();
1470     if (!aCtx.IsNull()
1471      && aCtx->NbCurrents() > 0
1472      && aCtx->NbSelected() > 0)
1473     {
1474       Draw_Interprete ("verase");
1475     }
1476   }
1477   else
1478   {
1479     // Number
1480     Standard_Integer Num = Draw::Atoi(buf_ret);
1481     if(Num>=0 && Num<=7)
1482       ViewerTest::StandardModeActivation(Num);
1483   }
1484 }
1485
1486 //==============================================================================
1487 //function : VT_ProcessExpose
1488 //purpose  : Redraw the View on an Expose Event
1489 //==============================================================================
1490 void VT_ProcessExpose()
1491 {
1492   Handle(V3d_View) aView3d = ViewerTest::CurrentView();
1493   if (!aView3d.IsNull())
1494   {
1495     aView3d->Redraw();
1496   }
1497 }
1498
1499 //==============================================================================
1500 //function : VT_ProcessConfigure
1501 //purpose  : Resize the View on an Configure Event
1502 //==============================================================================
1503 void VT_ProcessConfigure()
1504 {
1505   Handle(V3d_View) aView3d = ViewerTest::CurrentView();
1506   if (aView3d.IsNull())
1507   {
1508     return;
1509   }
1510
1511   aView3d->MustBeResized();
1512   aView3d->Update();
1513   aView3d->Redraw();
1514 }
1515
1516 //==============================================================================
1517 //function : VT_ProcessButton1Press
1518 //purpose  : Picking
1519 //==============================================================================
1520 Standard_Boolean VT_ProcessButton1Press (Standard_Integer ,
1521                                          const char**     theArgVec,
1522                                          Standard_Boolean theToPick,
1523                                          Standard_Boolean theIsShift)
1524 {
1525   if (theToPick)
1526   {
1527     Standard_Real X, Y, Z;
1528     ViewerTest::CurrentView()->Convert (X_Motion, Y_Motion, X, Y, Z);
1529
1530     Draw::Set (theArgVec[1], X);
1531     Draw::Set (theArgVec[2], Y);
1532     Draw::Set (theArgVec[3], Z);
1533   }
1534
1535   if (theIsShift)
1536   {
1537     ViewerTest::CurrentEventManager()->ShiftSelect();
1538   }
1539   else
1540   {
1541     ViewerTest::CurrentEventManager()->Select();
1542   }
1543
1544   return Standard_False;
1545 }
1546
1547 //==============================================================================
1548 //function : VT_ProcessButton1Release
1549 //purpose  : End selecting
1550 //==============================================================================
1551 void VT_ProcessButton1Release (Standard_Boolean theIsShift)
1552 {
1553   if (IsDragged)
1554   {
1555     IsDragged = Standard_False;
1556     Handle(ViewerTest_EventManager) EM = ViewerTest::CurrentEventManager();
1557     if (theIsShift)
1558     {
1559       EM->ShiftSelect (Min (X_ButtonPress, X_Motion), Max (Y_ButtonPress, Y_Motion),
1560                        Max (X_ButtonPress, X_Motion), Min (Y_ButtonPress, Y_Motion));
1561     }
1562     else
1563     {
1564       EM->Select (Min (X_ButtonPress, X_Motion), Max (Y_ButtonPress, Y_Motion),
1565                   Max (X_ButtonPress, X_Motion), Min (Y_ButtonPress, Y_Motion));
1566     }
1567   }
1568 }
1569
1570 //==============================================================================
1571 //function : VT_ProcessButton3Press
1572 //purpose  : Start Rotation
1573 //==============================================================================
1574 void VT_ProcessButton3Press()
1575 {
1576   Start_Rot = 1;
1577   if (MyHLRIsOn)
1578   {
1579     ViewerTest::CurrentView()->SetComputedMode (Standard_False);
1580   }
1581   ViewerTest::CurrentView()->StartRotation( X_ButtonPress, Y_ButtonPress );
1582 }
1583
1584 //==============================================================================
1585 //function : VT_ProcessButton3Release
1586 //purpose  : End rotation
1587 //==============================================================================
1588 void VT_ProcessButton3Release()
1589 {
1590   if (Start_Rot)
1591   {
1592     Start_Rot = 0;
1593     if (MyHLRIsOn)
1594     {
1595       ViewerTest::CurrentView()->SetComputedMode (Standard_True);
1596     }
1597   }
1598 }
1599
1600 //==============================================================================
1601 //function : ProcessZClipMotion
1602 //purpose  : Zoom
1603 //==============================================================================
1604
1605 void ProcessZClipMotion()
1606 {
1607   Handle(V3d_View)  a3DView = ViewerTest::CurrentView();
1608   if ( Abs(X_Motion - X_ButtonPress) > 2 ) {
1609     static Standard_Real CurZPos = 0.;
1610
1611     //Quantity_Length VDX, VDY;
1612     //a3DView->Size(VDX,VDY);
1613     //Standard_Real VDZ = a3DView->ZSize();
1614     //printf("View size (%lf,%lf,%lf)\n", VDX, VDY, VDZ);
1615
1616     Quantity_Length dx = a3DView->Convert(X_Motion - X_ButtonPress);
1617
1618     // Front = Depth + width/2.
1619     Standard_Real D = 0.5;
1620     Standard_Real W = 0.1;
1621
1622     CurZPos += (dx);
1623
1624     D += CurZPos;
1625
1626     //printf("dx %lf Depth %lf Width %lf\n", dx, D, W);
1627
1628     a3DView->SetZClippingType(V3d_OFF);
1629     a3DView->SetZClippingDepth(D);
1630     a3DView->SetZClippingWidth(W);
1631     a3DView->SetZClippingType(V3d_FRONT);
1632
1633     a3DView->Redraw();
1634
1635     X_ButtonPress = X_Motion;
1636     Y_ButtonPress = Y_Motion;
1637   }
1638 }
1639
1640 //==============================================================================
1641 //function : ProcessControlButton1Motion
1642 //purpose  : Zoom
1643 //==============================================================================
1644
1645 static void ProcessControlButton1Motion()
1646 {
1647   ViewerTest::CurrentView()->Zoom( X_ButtonPress, Y_ButtonPress, X_Motion, Y_Motion);
1648
1649   X_ButtonPress = X_Motion;
1650   Y_ButtonPress = Y_Motion;
1651 }
1652
1653 //==============================================================================
1654 //function : VT_ProcessControlButton2Motion
1655 //purpose  : Panning
1656 //==============================================================================
1657 void VT_ProcessControlButton2Motion()
1658 {
1659   Quantity_Length dx = ViewerTest::CurrentView()->Convert(X_Motion - X_ButtonPress);
1660   Quantity_Length dy = ViewerTest::CurrentView()->Convert(Y_Motion - Y_ButtonPress);
1661
1662   dy = -dy; // Xwindow Y axis is from top to Bottom
1663
1664   ViewerTest::CurrentView()->Panning( dx, dy );
1665
1666   X_ButtonPress = X_Motion;
1667   Y_ButtonPress = Y_Motion;
1668 }
1669
1670 //==============================================================================
1671 //function : VT_ProcessControlButton3Motion
1672 //purpose  : Rotation
1673 //==============================================================================
1674 void VT_ProcessControlButton3Motion()
1675 {
1676   if (Start_Rot)
1677   {
1678     ViewerTest::CurrentView()->Rotation (X_Motion, Y_Motion);
1679   }
1680 }
1681
1682 //==============================================================================
1683 //function : VT_ProcessMotion
1684 //purpose  :
1685 //==============================================================================
1686 void VT_ProcessMotion()
1687 {
1688   //pre-hilights detected objects at mouse position
1689
1690   Handle(ViewerTest_EventManager) EM = ViewerTest::CurrentEventManager();
1691   EM->MoveTo(X_Motion, Y_Motion);
1692 }
1693
1694
1695 void ViewerTest::GetMousePosition(Standard_Integer& Xpix,Standard_Integer& Ypix)
1696 {
1697   Xpix = X_Motion;Ypix=Y_Motion;
1698 }
1699
1700 //==============================================================================
1701 //function : ViewProject: implements VAxo, VTop, VLeft, ...
1702 //purpose  : Switches to an axonometric, top, left and other views
1703 //==============================================================================
1704
1705 static int ViewProject(Draw_Interpretor& di, const V3d_TypeOfOrientation ori)
1706 {
1707   if ( ViewerTest::CurrentView().IsNull() )
1708   {
1709     di<<"Call vinit before this command, please"<<"\n";
1710     return 1;
1711   }
1712
1713   ViewerTest::CurrentView()->SetProj(ori);
1714   return 0;
1715 }
1716
1717 //==============================================================================
1718 //function : VAxo
1719 //purpose  : Switch to an Axonometric view
1720 //Draw arg : No args
1721 //==============================================================================
1722
1723 static int VAxo(Draw_Interpretor& di, Standard_Integer , const char** )
1724 {
1725   return ViewProject(di, V3d_XposYnegZpos);
1726 }
1727
1728 //==============================================================================
1729 //function : VTop
1730 //purpose  : Switch to a Top View
1731 //Draw arg : No args
1732 //==============================================================================
1733
1734 static int VTop(Draw_Interpretor& di, Standard_Integer , const char** )
1735 {
1736   return ViewProject(di, V3d_Zpos);
1737 }
1738
1739 //==============================================================================
1740 //function : VBottom
1741 //purpose  : Switch to a Bottom View
1742 //Draw arg : No args
1743 //==============================================================================
1744
1745 static int VBottom(Draw_Interpretor& di, Standard_Integer , const char** )
1746 {
1747   return ViewProject(di, V3d_Zneg);
1748 }
1749
1750 //==============================================================================
1751 //function : VLeft
1752 //purpose  : Switch to a Left View
1753 //Draw arg : No args
1754 //==============================================================================
1755
1756 static int VLeft(Draw_Interpretor& di, Standard_Integer , const char** )
1757 {
1758   return ViewProject(di, V3d_Ypos);
1759 }
1760
1761 //==============================================================================
1762 //function : VRight
1763 //purpose  : Switch to a Right View
1764 //Draw arg : No args
1765 //==============================================================================
1766
1767 static int VRight(Draw_Interpretor& di, Standard_Integer , const char** )
1768 {
1769   return ViewProject(di, V3d_Yneg);
1770 }
1771
1772 //==============================================================================
1773 //function : VFront
1774 //purpose  : Switch to a Front View
1775 //Draw arg : No args
1776 //==============================================================================
1777
1778 static int VFront(Draw_Interpretor& di, Standard_Integer , const char** )
1779 {
1780   return ViewProject(di, V3d_Xpos);
1781 }
1782
1783 //==============================================================================
1784 //function : VBack
1785 //purpose  : Switch to a Back View
1786 //Draw arg : No args
1787 //==============================================================================
1788
1789 static int VBack(Draw_Interpretor& di, Standard_Integer , const char** )
1790 {
1791   return ViewProject(di, V3d_Xneg);
1792 }
1793
1794 //==============================================================================
1795 //function : VHelp
1796 //purpose  : Dsiplay help on viewer Keyboead and mouse commands
1797 //Draw arg : No args
1798 //==============================================================================
1799
1800 static int VHelp(Draw_Interpretor& di, Standard_Integer , const char** )
1801 {
1802
1803   di << "Q : Quit the application" << "\n";
1804
1805   di << "========================="<<"\n";
1806   di << "F : FitAll" << "\n";
1807   di << "T : TopView" << "\n";
1808   di << "B : BottomView" << "\n";
1809   di << "R : RightView" << "\n";
1810   di << "L : LeftView" << "\n";
1811   di << "A : AxonometricView" << "\n";
1812   di << "D : ResetView" << "\n";
1813
1814   di << "========================="<<"\n";
1815   di << "S : Shading" << "\n";
1816   di << "W : Wireframe" << "\n";
1817   di << "H : HidelLineRemoval" << "\n";
1818   di << "U : Unset display mode" << "\n";
1819   di << "Delete : Remove selection from viewer" << "\n";
1820
1821   di << "========================="<<"\n";
1822   di << "Selection mode "<<"\n";
1823   di << "0 : Shape" <<"\n";
1824   di << "1 : Vertex" <<"\n";
1825   di << "2 : Edge" <<"\n";
1826   di << "3 : Wire" <<"\n";
1827   di << "4 : Face" <<"\n";
1828   di << "5 : Shell" <<"\n";
1829   di << "6 : Solid" <<"\n";
1830   di << "7 : Compound" <<"\n";
1831
1832   di << "========================="<<"\n";
1833   di << "Z : Switch Z clipping On/Off" << "\n";
1834   di << ", : Hilight next detected" << "\n";
1835   di << ". : Hilight previous detected" << "\n";
1836
1837   return 0;
1838 }
1839
1840 #ifdef WNT
1841
1842 static Standard_Boolean Ppick = 0;
1843 static Standard_Integer Pargc = 0;
1844 static const char**           Pargv = NULL;
1845
1846
1847 static LRESULT WINAPI AdvViewerWindowProc( HWND hwnd,
1848                                           UINT Msg,
1849                                           WPARAM wParam,
1850                                           LPARAM lParam )
1851 {
1852   if (!ViewerTest_myViews.IsEmpty()) {
1853
1854     WPARAM fwKeys = wParam;
1855
1856     switch( Msg ) {
1857     case WM_CLOSE:
1858        {
1859          // Delete view from map of views
1860          ViewerTest::RemoveView(FindViewIdByWindowHandle(hwnd));
1861          return 0;
1862        }
1863        break;
1864     case WM_ACTIVATE:
1865       if(LOWORD(wParam) == WA_CLICKACTIVE || LOWORD(wParam) == WA_ACTIVE
1866         || ViewerTest::CurrentView().IsNull())
1867       {
1868         // Activate inactive window
1869         if(GetWindowHandle(VT_GetWindow()) != hwnd)
1870         {
1871           ActivateView (FindViewIdByWindowHandle(hwnd));
1872         }
1873       }
1874       break;
1875     case WM_LBUTTONUP:
1876       IsDragged = Standard_False;
1877       if( !DragFirst )
1878       {
1879         HDC hdc = GetDC( hwnd );
1880         SelectObject( hdc, GetStockObject( HOLLOW_BRUSH ) );
1881         SetROP2( hdc, R2_NOT );
1882         Rectangle( hdc, X_ButtonPress, Y_ButtonPress, X_Motion, Y_Motion );
1883         ReleaseDC( hwnd, hdc );
1884
1885         const Handle(ViewerTest_EventManager) EM =
1886           ViewerTest::CurrentEventManager();
1887         if ( fwKeys & MK_SHIFT )
1888           EM->ShiftSelect( min( X_ButtonPress, X_Motion ), max( Y_ButtonPress, Y_Motion ),
1889           max( X_ButtonPress, X_Motion ), min( Y_ButtonPress, Y_Motion ));
1890         else
1891           EM->Select( min( X_ButtonPress, X_Motion ), max( Y_ButtonPress, Y_Motion ),
1892           max( X_ButtonPress, X_Motion ), min( Y_ButtonPress, Y_Motion ));
1893       }
1894       return ViewerWindowProc( hwnd, Msg, wParam, lParam );
1895
1896     case WM_LBUTTONDOWN:
1897       if( fwKeys == MK_LBUTTON || fwKeys == ( MK_LBUTTON | MK_SHIFT ) )
1898       {
1899         IsDragged = Standard_True;
1900         DragFirst = Standard_True;
1901         X_ButtonPress = LOWORD(lParam);
1902         Y_ButtonPress = HIWORD(lParam);
1903       }
1904       return ViewerWindowProc( hwnd, Msg, wParam, lParam );
1905
1906       break;
1907
1908     case WM_MOUSEMOVE:
1909       if( IsDragged )
1910       {
1911         HDC hdc = GetDC( hwnd );
1912
1913         HGDIOBJ anObj = SelectObject( hdc, GetStockObject( WHITE_PEN ) );
1914         SelectObject( hdc, GetStockObject( HOLLOW_BRUSH ) );
1915         SetROP2( hdc, R2_NOT );
1916
1917         if( !DragFirst )
1918           Rectangle( hdc, X_ButtonPress, Y_ButtonPress, X_Motion, Y_Motion );
1919
1920         DragFirst = Standard_False;
1921         X_Motion = LOWORD(lParam);
1922         Y_Motion = HIWORD(lParam);
1923
1924         Rectangle( hdc, X_ButtonPress, Y_ButtonPress, X_Motion, Y_Motion );
1925
1926         SelectObject( hdc, anObj );
1927
1928         ReleaseDC( hwnd, hdc );
1929       }
1930       else
1931         return ViewerWindowProc( hwnd, Msg, wParam, lParam );
1932       break;
1933
1934     default:
1935       return ViewerWindowProc( hwnd, Msg, wParam, lParam );
1936     }
1937     return 0;
1938   }
1939   return ViewerWindowProc( hwnd, Msg, wParam, lParam );
1940 }
1941
1942
1943 static LRESULT WINAPI ViewerWindowProc( HWND hwnd,
1944                                        UINT Msg,
1945                                        WPARAM wParam,
1946                                        LPARAM lParam )
1947 {
1948   static int Up = 1;
1949
1950   if ( !ViewerTest::CurrentView().IsNull() ) {
1951     PAINTSTRUCT    ps;
1952
1953     switch( Msg ) {
1954     case WM_PAINT:
1955       BeginPaint(hwnd, &ps);
1956       EndPaint(hwnd, &ps);
1957       VT_ProcessExpose();
1958       break;
1959
1960     case WM_SIZE:
1961       VT_ProcessConfigure();
1962       break;
1963
1964     case WM_KEYDOWN:
1965       if ((wParam != VK_SHIFT) && (wParam != VK_CONTROL))
1966       {
1967         char c[2];
1968         c[0] = (char) wParam;
1969         c[1] = '\0';
1970         if (wParam == VK_DELETE)
1971         {
1972           c[0] = THE_KEY_DELETE;
1973         }
1974         VT_ProcessKeyPress (c);
1975       }
1976       break;
1977
1978     case WM_LBUTTONUP:
1979     case WM_MBUTTONUP:
1980     case WM_RBUTTONUP:
1981       Up = 1;
1982       VT_ProcessButton3Release();
1983       break;
1984
1985     case WM_LBUTTONDOWN:
1986     case WM_MBUTTONDOWN:
1987     case WM_RBUTTONDOWN:
1988       {
1989         WPARAM fwKeys = wParam;
1990
1991         Up = 0;
1992
1993         X_ButtonPress = LOWORD(lParam);
1994         Y_ButtonPress = HIWORD(lParam);
1995
1996         if (Msg == WM_LBUTTONDOWN)
1997         {
1998           if (fwKeys & MK_CONTROL)
1999           {
2000             Ppick = VT_ProcessButton1Press (Pargc, Pargv, Ppick, (fwKeys & MK_SHIFT));
2001           }
2002           else
2003           {
2004             VT_ProcessButton1Press (Pargc, Pargv, Ppick, (fwKeys & MK_SHIFT));
2005           }
2006         }
2007         else if (Msg == WM_RBUTTONDOWN)
2008         {
2009           // Start rotation
2010           VT_ProcessButton3Press();
2011         }
2012       }
2013       break;
2014
2015     case WM_MOUSEMOVE:
2016       {
2017         //cout << "\t WM_MOUSEMOVE" << endl;
2018         WPARAM fwKeys = wParam;
2019         X_Motion = LOWORD(lParam);
2020         Y_Motion = HIWORD(lParam);
2021
2022         if ( Up &&
2023           fwKeys & ( MK_LBUTTON|MK_MBUTTON|MK_RBUTTON ) ) {
2024             Up = 0;
2025             X_ButtonPress = LOWORD(lParam);
2026             Y_ButtonPress = HIWORD(lParam);
2027
2028             if ( fwKeys & MK_RBUTTON ) {
2029               // Start rotation
2030               VT_ProcessButton3Press();
2031             }
2032           }
2033
2034           if ( fwKeys & MK_CONTROL ) {
2035             if ( fwKeys & MK_LBUTTON ) {
2036               ProcessControlButton1Motion();
2037             }
2038             else if ( fwKeys & MK_MBUTTON ||
2039               ((fwKeys&MK_LBUTTON) &&
2040               (fwKeys&MK_RBUTTON) ) ){
2041                 VT_ProcessControlButton2Motion();
2042               }
2043             else if ( fwKeys & MK_RBUTTON ) {
2044               VT_ProcessControlButton3Motion();
2045             }
2046           }
2047 #ifdef BUG
2048           else if ( fwKeys & MK_SHIFT ) {
2049             if ( fwKeys & MK_MBUTTON ||
2050               ((fwKeys&MK_LBUTTON) &&
2051               (fwKeys&MK_RBUTTON) ) ) {
2052                 cout << "ProcessZClipMotion()" << endl;
2053                 ProcessZClipMotion();
2054               }
2055           }
2056 #endif
2057           else if (GetWindowHandle (VT_GetWindow()) == hwnd)
2058           {
2059             if ((fwKeys & MK_MBUTTON
2060             || ((fwKeys & MK_LBUTTON) && (fwKeys & MK_RBUTTON))))
2061             {
2062               ProcessZClipMotion();
2063             }
2064             else
2065             {
2066               VT_ProcessMotion();
2067             }
2068           }
2069       }
2070       break;
2071
2072     default:
2073       return( DefWindowProc( hwnd, Msg, wParam, lParam ));
2074     }
2075     return 0L;
2076   }
2077
2078   return DefWindowProc( hwnd, Msg, wParam, lParam );
2079 }
2080
2081
2082
2083
2084 //==============================================================================
2085 //function : ViewerMainLoop
2086 //purpose  : Get a Event on the view and dispatch it
2087 //==============================================================================
2088
2089
2090 int ViewerMainLoop(Standard_Integer argc, const char** argv)
2091 {
2092   Ppick = (argc > 0)? 1 : 0;
2093   Pargc = argc;
2094   Pargv = argv;
2095
2096   if ( Ppick ) {
2097     MSG msg;
2098     msg.wParam = 1;
2099
2100     cout << "Start picking" << endl;
2101
2102     while ( Ppick == 1 ) {
2103       // Wait for a VT_ProcessButton1Press() to toggle pick to 1 or 0
2104       if (GetMessage(&msg, NULL, 0, 0) ) {
2105         TranslateMessage(&msg);
2106         DispatchMessage(&msg);
2107       }
2108     }
2109
2110     cout << "Picking done" << endl;
2111   }
2112
2113   return Ppick;
2114 }
2115
2116 #elif !defined(__APPLE__) || defined(MACOSX_USE_GLX)
2117
2118 int min( int a, int b )
2119 {
2120   if( a<b )
2121     return a;
2122   else
2123     return b;
2124 }
2125
2126 int max( int a, int b )
2127 {
2128   if( a>b )
2129     return a;
2130   else
2131     return b;
2132 }
2133
2134 int ViewerMainLoop(Standard_Integer argc, const char** argv)
2135
2136 {
2137   static XEvent aReport;
2138   Standard_Boolean pick = argc > 0;
2139   Display *aDisplay = GetDisplayConnection()->GetDisplay();
2140   XNextEvent (aDisplay, &aReport);
2141
2142   // Handle event for the chosen display connection
2143   switch (aReport.type) {
2144       case ClientMessage:
2145         {
2146           if(aReport.xclient.data.l[0] == GetDisplayConnection()->GetAtom(Aspect_XA_DELETE_WINDOW))
2147           {
2148             // Close the window
2149             ViewerTest::RemoveView(FindViewIdByWindowHandle (aReport.xclient.window));
2150           }
2151         }
2152         return 0;
2153      case FocusIn:
2154       {
2155          // Activate inactive view
2156          Window aWindow = GetWindowHandle(VT_GetWindow());
2157          if(aWindow != aReport.xfocus.window)
2158          {
2159            ActivateView (FindViewIdByWindowHandle (aReport.xfocus.window));
2160          }
2161       }
2162       break;
2163       case Expose:
2164         {
2165           VT_ProcessExpose();
2166         }
2167         break;
2168       case ConfigureNotify:
2169         {
2170           VT_ProcessConfigure();
2171         }
2172         break;
2173       case KeyPress:
2174         {
2175
2176           KeySym ks_ret ;
2177           char buf_ret[11] ;
2178           int ret_len ;
2179           XComposeStatus status_in_out;
2180
2181           ret_len = XLookupString( ( XKeyEvent *)&aReport ,
2182             (char *) buf_ret , 10 ,
2183             &ks_ret , &status_in_out ) ;
2184
2185
2186           buf_ret[ret_len] = '\0' ;
2187
2188           if (ret_len)
2189           {
2190             VT_ProcessKeyPress (buf_ret);
2191           }
2192         }
2193         break;
2194       case ButtonPress:
2195         {
2196           X_ButtonPress = aReport.xbutton.x;
2197           Y_ButtonPress = aReport.xbutton.y;
2198
2199           if (aReport.xbutton.button == Button1)
2200           {
2201             if (aReport.xbutton.state & ControlMask)
2202             {
2203               pick = VT_ProcessButton1Press (argc, argv, pick, (aReport.xbutton.state & ShiftMask));
2204             }
2205             else
2206             {
2207               IsDragged = Standard_True;
2208               DragFirst = Standard_True;
2209             }
2210           }
2211           else if (aReport.xbutton.button == Button3)
2212           {
2213             // Start rotation
2214             VT_ProcessButton3Press();
2215           }
2216         }
2217         break;
2218       case ButtonRelease:
2219         {
2220           if( IsDragged )
2221           {
2222             if( !DragFirst )
2223             {
2224               Aspect_Handle aWindow = VT_GetWindow()->XWindow();
2225               GC gc = XCreateGC( aDisplay, aWindow, 0, 0 );
2226               XDrawRectangle( aDisplay, aWindow, gc, min( X_ButtonPress, X_Motion ), min( Y_ButtonPress, Y_Motion ), abs( X_Motion-X_ButtonPress ), abs( Y_Motion-Y_ButtonPress ) );
2227             }
2228
2229             Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
2230             if( aContext.IsNull() )
2231             {
2232               cout << "The context is null. Please use vinit before createmesh" << endl;
2233               return 0;
2234             }
2235
2236             Standard_Boolean ShiftPressed = ( aReport.xbutton.state & ShiftMask );
2237             if( aReport.xbutton.button==1 )
2238               if( DragFirst )
2239                 if( ShiftPressed )
2240                 {
2241                   aContext->ShiftSelect();
2242                 }
2243                 else
2244                 {
2245                   aContext->Select();
2246                 }
2247               else
2248                 if( ShiftPressed )
2249                 {
2250                   aContext->ShiftSelect( min( X_ButtonPress, X_Motion ), min( Y_ButtonPress, Y_Motion ),
2251                     max( X_ButtonPress, X_Motion ), max( Y_ButtonPress, Y_Motion ),
2252                     ViewerTest::CurrentView());
2253                 }
2254                 else
2255                 {
2256                   aContext->Select( min( X_ButtonPress, X_Motion ), min( Y_ButtonPress, Y_Motion ),
2257                     max( X_ButtonPress, X_Motion ), max( Y_ButtonPress, Y_Motion ),
2258                     ViewerTest::CurrentView() );
2259                 }
2260             else
2261               VT_ProcessButton3Release();
2262
2263             IsDragged = Standard_False;
2264           }
2265           else
2266             VT_ProcessButton3Release();
2267         }
2268         break;
2269       case MotionNotify:
2270         {
2271           if (GetWindowHandle (VT_GetWindow()) != aReport.xmotion.window)
2272           {
2273             break;
2274           }
2275           if( IsDragged )
2276           {
2277             Aspect_Handle aWindow = VT_GetWindow()->XWindow();
2278             GC gc = XCreateGC( aDisplay, aWindow, 0, 0 );
2279             XSetFunction( aDisplay, gc, GXinvert );
2280
2281             if( !DragFirst )
2282               XDrawRectangle(aDisplay, aWindow, gc, min( X_ButtonPress, X_Motion ), min( Y_ButtonPress, Y_Motion ), abs( X_Motion-X_ButtonPress ), abs( Y_Motion-Y_ButtonPress ) );
2283
2284             X_Motion = aReport.xmotion.x;
2285             Y_Motion = aReport.xmotion.y;
2286             DragFirst = Standard_False;
2287
2288             XDrawRectangle( aDisplay, aWindow, gc, min( X_ButtonPress, X_Motion ), min( Y_ButtonPress, Y_Motion ), abs( X_Motion-X_ButtonPress ), abs( Y_Motion-Y_ButtonPress ) );
2289           }
2290           else
2291           {
2292             X_Motion = aReport.xmotion.x;
2293             Y_Motion = aReport.xmotion.y;
2294
2295             // remove all the ButtonMotionMaskr
2296             while( XCheckMaskEvent( aDisplay, ButtonMotionMask, &aReport) ) ;
2297
2298             if ( ZClipIsOn && aReport.xmotion.state & ShiftMask ) {
2299               if ( Abs(X_Motion - X_ButtonPress) > 2 ) {
2300
2301                 Quantity_Length VDX, VDY;
2302
2303                 ViewerTest::CurrentView()->Size(VDX,VDY);
2304                 Standard_Real VDZ =0 ;
2305                 VDZ = ViewerTest::CurrentView()->ZSize();
2306
2307                 printf("%f,%f,%f\n", VDX, VDY, VDZ);
2308
2309                 Quantity_Length dx = 0 ;
2310                 dx = ViewerTest::CurrentView()->Convert(X_Motion - X_ButtonPress);
2311
2312                 cout << dx << endl;
2313
2314                 dx = dx / VDX * VDZ;
2315
2316                 cout << dx << endl;
2317
2318                 ViewerTest::CurrentView()->Redraw();
2319               }
2320             }
2321
2322             if ( aReport.xmotion.state & ControlMask ) {
2323               if ( aReport.xmotion.state & Button1Mask ) {
2324                 ProcessControlButton1Motion();
2325               }
2326               else if ( aReport.xmotion.state & Button2Mask ) {
2327                 VT_ProcessControlButton2Motion();
2328               }
2329               else if ( aReport.xmotion.state & Button3Mask ) {
2330                 VT_ProcessControlButton3Motion();
2331               }
2332             }
2333             else
2334             {
2335               VT_ProcessMotion();
2336             }
2337           }
2338         }
2339         break;
2340 }
2341 return pick;
2342 }
2343
2344 //==============================================================================
2345 //function : VProcessEvents
2346 //purpose  : call by Tk_CreateFileHandler() to be able to manage the
2347 //       event in the Viewer window
2348 //==============================================================================
2349
2350 static void VProcessEvents(ClientData,int)
2351 {
2352   NCollection_Vector<int> anEventNumbers;
2353   // Get number of messages from every display
2354   for (NCollection_DoubleMap <TCollection_AsciiString, Handle(Graphic3d_GraphicDriver)>::Iterator
2355        anIter (ViewerTest_myDrivers); anIter.More(); anIter.Next())
2356   {
2357     anEventNumbers.Append(XPending (anIter.Key2()->GetDisplayConnection()->GetDisplay()));
2358   }
2359     // Handle events for every display
2360   int anEventIter = 0;
2361   for (NCollection_DoubleMap <TCollection_AsciiString, Handle(Graphic3d_GraphicDriver)>::Iterator
2362        anIter (ViewerTest_myDrivers); anIter.More(); anIter.Next(), anEventIter++)
2363   {
2364     for (int i = 0; i < anEventNumbers.Value(anEventIter) &&
2365          XPending (anIter.Key2()->GetDisplayConnection()->GetDisplay()) > 0; ++i)
2366     {
2367       SetDisplayConnection (anIter.Key2()->GetDisplayConnection());
2368       int anEventResult = ViewerMainLoop( 0, NULL);
2369       // If window is closed or context was not found finish current event processing loop
2370       if (!anEventResult)
2371         return;
2372     }
2373   }
2374
2375   SetDisplayConnection (ViewerTest::GetAISContext()->CurrentViewer()->Driver()->GetDisplayConnection());
2376
2377 }
2378 #endif
2379
2380 //==============================================================================
2381 //function : OSWindowSetup
2382 //purpose  : Setup for the X11 window to be able to cath the event
2383 //==============================================================================
2384
2385
2386 static void OSWindowSetup()
2387 {
2388 #if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
2389   // X11
2390
2391   Window  window   = VT_GetWindow()->XWindow();
2392   SetDisplayConnection (ViewerTest::CurrentView()->Viewer()->Driver()->GetDisplayConnection());
2393   Display *aDisplay = GetDisplayConnection()->GetDisplay();
2394   XSynchronize(aDisplay, 1);
2395
2396   // X11 : For keyboard on SUN
2397   XWMHints wmhints;
2398   wmhints.flags = InputHint;
2399   wmhints.input = 1;
2400
2401   XSetWMHints( aDisplay, window, &wmhints);
2402
2403   XSelectInput( aDisplay, window,  ExposureMask | KeyPressMask |
2404     ButtonPressMask | ButtonReleaseMask |
2405     StructureNotifyMask |
2406     PointerMotionMask |
2407     Button1MotionMask | Button2MotionMask |
2408     Button3MotionMask | FocusChangeMask
2409     );
2410   Atom aDeleteWindowAtom = GetDisplayConnection()->GetAtom(Aspect_XA_DELETE_WINDOW);
2411   XSetWMProtocols(aDisplay, window, &aDeleteWindowAtom, 1);
2412
2413   XSynchronize(aDisplay, 0);
2414
2415 #else
2416   // WNT
2417 #endif
2418
2419 }
2420
2421
2422 //==============================================================================
2423 //function : VFit
2424
2425 //purpose  : Fitall, no DRAW arguments
2426 //Draw arg : No args
2427 //==============================================================================
2428
2429 static int VFit(Draw_Interpretor& , Standard_Integer , const char** )
2430 {
2431   const Handle(V3d_View) aView = ViewerTest::CurrentView();
2432   Handle(NIS_View) V = Handle(NIS_View)::DownCast(aView);
2433   if (V.IsNull() == Standard_False) {
2434     V->FitAll3d();
2435   } else if (aView.IsNull() == Standard_False) {
2436     aView->FitAll();
2437   }
2438   return 0;
2439 }
2440
2441 //==============================================================================
2442 //function : VZFit
2443 //purpose  : ZFitall, no DRAW arguments
2444 //Draw arg : No args
2445 //==============================================================================
2446
2447 static int VZFit(Draw_Interpretor& , Standard_Integer , const char** )
2448 {
2449   Handle(V3d_View) V = ViewerTest::CurrentView();
2450   if ( !V.IsNull() ) V->ZFitAll(); return 0; }
2451
2452
2453 static int VRepaint(Draw_Interpretor& , Standard_Integer , const char** )
2454 {
2455   Handle(V3d_View) V = ViewerTest::CurrentView();
2456   if ( !V.IsNull() ) V->Redraw(); return 0;
2457 }
2458
2459
2460 //==============================================================================
2461 //function : VClear
2462 //purpose  : Remove all the object from the viewer
2463 //Draw arg : No args
2464 //==============================================================================
2465
2466 static int VClear(Draw_Interpretor& , Standard_Integer , const char** )
2467 {
2468   Handle(V3d_View) V = ViewerTest::CurrentView();
2469   if(!V.IsNull())
2470     ViewerTest::Clear();
2471   return 0;
2472 }
2473
2474 //==============================================================================
2475 //function : VPick
2476 //purpose  :
2477 //==============================================================================
2478
2479 static int VPick(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
2480 { if (ViewerTest::CurrentView().IsNull() ) return 1;
2481
2482 if ( argc < 4 ) {
2483   di << argv[0] << "Invalid number of arguments" << "\n";
2484   return 1;
2485 }
2486
2487 while (ViewerMainLoop( argc, argv)) {
2488 }
2489
2490 return 0;
2491 }
2492
2493 //==============================================================================
2494 //function : InitViewerTest
2495 //purpose  : initialisation de toutes les variables static de  ViewerTest (dp)
2496 //==============================================================================
2497
2498 void ViewerTest_InitViewerTest (const Handle(AIS_InteractiveContext)& theContext)
2499 {
2500   Handle(V3d_Viewer) aViewer = theContext->CurrentViewer();
2501   ViewerTest::SetAISContext(theContext);
2502   aViewer->InitActiveViews();
2503   Handle(V3d_View) aView = aViewer->ActiveView();
2504   if (aViewer->MoreActiveViews()) ViewerTest::CurrentView(aView);
2505   ViewerTest::ResetEventManager();
2506   Handle(Aspect_Window) aWindow = aView->Window();
2507 #if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
2508   // X11
2509   VT_GetWindow() = Handle(Xw_Window)::DownCast(aWindow);
2510   OSWindowSetup();
2511   static int aFirst = 1;
2512   if ( aFirst ) {
2513 #if TCL_MAJOR_VERSION  < 8
2514     Tk_CreateFileHandler((void*)XConnectionNumber(GetDisplayConnection()->GetDisplay()),
2515       TK_READABLE, VProcessEvents, (ClientData) 0);
2516 #else
2517     Tk_CreateFileHandler(XConnectionNumber(GetDisplayConnection()->GetDisplay()),
2518       TK_READABLE, VProcessEvents, (ClientData) 0);
2519 #endif
2520     aFirst = 0;
2521   }
2522 #endif
2523 }
2524
2525 //==============================================================================
2526 //function : VSetBg
2527 //purpose  : Load image as background
2528 //==============================================================================
2529
2530 static int VSetBg(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
2531 {
2532   if (argc < 2 || argc > 3)
2533   {
2534     di << "Usage : " << argv[0] << " imagefile [filltype] : Load image as background" << "\n";
2535     di << "filltype can be one of CENTERED, TILED, STRETCH, NONE" << "\n";
2536     return 1;
2537   }
2538
2539   Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext();
2540   if(AISContext.IsNull())
2541   {
2542     di << "use 'vinit' command before " << argv[0] << "\n";
2543     return 1;
2544   }
2545
2546   Aspect_FillMethod aFillType = Aspect_FM_CENTERED;
2547   if (argc == 3)
2548   {
2549     const char* szType = argv[2];
2550     if      (strcmp(szType, "NONE"    ) == 0) aFillType = Aspect_FM_NONE;
2551     else if (strcmp(szType, "CENTERED") == 0) aFillType = Aspect_FM_CENTERED;
2552     else if (strcmp(szType, "TILED"   ) == 0) aFillType = Aspect_FM_TILED;
2553     else if (strcmp(szType, "STRETCH" ) == 0) aFillType = Aspect_FM_STRETCH;
2554     else
2555     {
2556       di << "Wrong fill type : " << szType << "\n";
2557       di << "Must be one of CENTERED, TILED, STRETCH, NONE" << "\n";
2558       return 1;
2559     }
2560   }
2561
2562   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
2563   V3dView->SetBackgroundImage(argv[1], aFillType, Standard_True);
2564
2565   return 0;
2566 }
2567
2568 //==============================================================================
2569 //function : VSetBgMode
2570 //purpose  : Change background image fill type
2571 //==============================================================================
2572
2573 static int VSetBgMode(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
2574 {
2575   if (argc != 2)
2576   {
2577     di << "Usage : " << argv[0] << " filltype : Change background image mode" << "\n";
2578     di << "filltype must be one of CENTERED, TILED, STRETCH, NONE" << "\n";
2579     return 1;
2580   }
2581
2582   Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext();
2583   if(AISContext.IsNull())
2584   {
2585     di << "use 'vinit' command before " << argv[0] << "\n";
2586     return 1;
2587   }
2588   Aspect_FillMethod aFillType = Aspect_FM_NONE;
2589   const char* szType = argv[1];
2590   if      (strcmp(szType, "NONE"    ) == 0) aFillType = Aspect_FM_NONE;
2591   else if (strcmp(szType, "CENTERED") == 0) aFillType = Aspect_FM_CENTERED;
2592   else if (strcmp(szType, "TILED"   ) == 0) aFillType = Aspect_FM_TILED;
2593   else if (strcmp(szType, "STRETCH" ) == 0) aFillType = Aspect_FM_STRETCH;
2594   else
2595   {
2596     di << "Wrong fill type : " << szType << "\n";
2597     di << "Must be one of CENTERED, TILED, STRETCH, NONE" << "\n";
2598     return 1;
2599   }
2600   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
2601   V3dView->SetBgImageStyle(aFillType, Standard_True);
2602   return 0;
2603 }
2604
2605 //==============================================================================
2606 //function : VSetGradientBg
2607 //purpose  : Mount gradient background
2608 //==============================================================================
2609 static int VSetGradientBg(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
2610 {
2611   if (argc != 8 )
2612   {
2613     di << "Usage : " << argv[0] << " R1 G1 B1 R2 G2 B2 Type : Mount gradient background" << "\n";
2614     di << "R1,G1,B1,R2,G2,B2 = [0..255]" << "\n";
2615     di << "Type must be one of 0 = NONE, 1 = HOR, 2 = VER, 3 = DIAG1, 4 = DIAG2" << "\n";
2616     di << "                    5 = CORNER1, 6 = CORNER2, 7 = CORNER3, 8 = CORNER4" << "\n";
2617     return 1;
2618   }
2619
2620   Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext();
2621   if(AISContext.IsNull())
2622   {
2623     di << "use 'vinit' command before " << argv[0] << "\n";
2624     return 1;
2625   }
2626   if (argc == 8)
2627   {
2628
2629     Standard_Real R1 = Draw::Atof(argv[1])/255.;
2630     Standard_Real G1 = Draw::Atof(argv[2])/255.;
2631     Standard_Real B1 = Draw::Atof(argv[3])/255.;
2632     Quantity_Color aColor1(R1,G1,B1,Quantity_TOC_RGB);
2633
2634     Standard_Real R2 = Draw::Atof(argv[4])/255.;
2635     Standard_Real G2 = Draw::Atof(argv[5])/255.;
2636     Standard_Real B2 = Draw::Atof(argv[6])/255.;
2637
2638     Quantity_Color aColor2(R2,G2,B2,Quantity_TOC_RGB);
2639     int aType = Draw::Atoi(argv[7]);
2640     if( aType < 0 || aType > 8 )
2641     {
2642       di << "Wrong fill type " << "\n";
2643       di << "Must be one of 0 = NONE, 1 = HOR, 2 = VER, 3 = DIAG1, 4 = DIAG2" << "\n";
2644       di << "               5 = CORNER1, 6 = CORNER2, 7 = CORNER3, 8 = CORNER4" << "\n";
2645       return 1;
2646     }
2647
2648     Aspect_GradientFillMethod aMethod = Aspect_GradientFillMethod(aType);
2649
2650     Handle(V3d_View) V3dView = ViewerTest::CurrentView();
2651     V3dView->SetBgGradientColors( aColor1, aColor2, aMethod, 1);
2652   }
2653
2654   return 0;
2655 }
2656
2657 //==============================================================================
2658 //function : VSetGradientBgMode
2659 //purpose  : Change gradient background fill style
2660 //==============================================================================
2661 static int VSetGradientBgMode(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
2662 {
2663   if (argc != 2 )
2664   {
2665     di << "Usage : " << argv[0] << " Type : Change gradient background fill type" << "\n";
2666     di << "Type must be one of 0 = NONE, 1 = HOR, 2 = VER, 3 = DIAG1, 4 = DIAG2" << "\n";
2667     di << "                    5 = CORNER1, 6 = CORNER2, 7 = CORNER3, 8 = CORNER4" << "\n";
2668     return 1;
2669   }
2670
2671   Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext();
2672   if(AISContext.IsNull())
2673   {
2674     di << "use 'vinit' command before " << argv[0] << "\n";
2675     return 1;
2676   }
2677   if (argc == 2)
2678   {
2679     int aType = Draw::Atoi(argv[1]);
2680     if( aType < 0 || aType > 8 )
2681     {
2682       di << "Wrong fill type " << "\n";
2683       di << "Must be one of 0 = NONE, 1 = HOR, 2 = VER, 3 = DIAG1, 4 = DIAG2" << "\n";
2684       di << "               5 = CORNER1, 6 = CORNER2, 7 = CORNER3, 8 = CORNER4" << "\n";
2685       return 1;
2686     }
2687
2688     Aspect_GradientFillMethod aMethod = Aspect_GradientFillMethod(aType);
2689
2690     Handle(V3d_View) V3dView = ViewerTest::CurrentView();
2691     V3dView->SetBgGradientStyle( aMethod, 1 );
2692   }
2693
2694   return 0;
2695 }
2696
2697 //==============================================================================
2698 //function : VSetColorBg
2699 //purpose  : Set color background
2700 //==============================================================================
2701 static int VSetColorBg(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
2702 {
2703   if (argc != 4 )
2704   {
2705     di << "Usage : " << argv[0] << " R G B : Set color background" << "\n";
2706     di << "R,G,B = [0..255]" << "\n";
2707     return 1;
2708   }
2709
2710   Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext();
2711   if(AISContext.IsNull())
2712   {
2713     di << "use 'vinit' command before " << argv[0] << "\n";
2714     return 1;
2715   }
2716   if (argc == 4)
2717   {
2718
2719     Standard_Real R = Draw::Atof(argv[1])/255.;
2720     Standard_Real G = Draw::Atof(argv[2])/255.;
2721     Standard_Real B = Draw::Atof(argv[3])/255.;
2722     Quantity_Color aColor(R,G,B,Quantity_TOC_RGB);
2723
2724     Handle(V3d_View) V3dView = ViewerTest::CurrentView();
2725     V3dView->SetBackgroundColor( aColor );
2726     V3dView->Update();
2727   }
2728
2729   return 0;
2730 }
2731
2732 //==============================================================================
2733 //function : VScale
2734 //purpose  : View Scaling
2735 //==============================================================================
2736
2737 static int VScale(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
2738 {
2739   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
2740   if ( V3dView.IsNull() ) return 1;
2741
2742   if ( argc != 4 ) {
2743     di << argv[0] << "Invalid number of arguments" << "\n";
2744     return 1;
2745   }
2746   V3dView->SetAxialScale( Draw::Atof(argv[1]),  Draw::Atof(argv[2]),  Draw::Atof(argv[3]) );
2747   return 0;
2748 }
2749 //==============================================================================
2750 //function : VTestZBuffTrihedron
2751 //purpose  : Displays a V3d_ZBUFFER'ed or V3d_WIREFRAME'd trihedron
2752 //==============================================================================
2753
2754 static int VTestZBuffTrihedron(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
2755 {
2756   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
2757   if ( V3dView.IsNull() ) return 1;
2758
2759   V3dView->ZBufferTriedronSetup();
2760
2761   if ( argc == 1 ) {
2762     // Set up default trihedron parameters
2763     V3dView->TriedronDisplay( Aspect_TOTP_LEFT_LOWER, Quantity_NOC_WHITE, 0.1, V3d_ZBUFFER );
2764   } else
2765   if ( argc == 7 )
2766   {
2767     Aspect_TypeOfTriedronPosition aPosition = Aspect_TOTP_LEFT_LOWER;
2768     const char* aPosType = argv[1];
2769
2770     if ( strcmp(aPosType, "center") == 0 )
2771     {
2772       aPosition = Aspect_TOTP_CENTER;
2773     } else
2774     if (strcmp(aPosType, "left_lower") == 0)
2775     {
2776       aPosition = Aspect_TOTP_LEFT_LOWER;
2777     } else
2778     if (strcmp(aPosType, "left_upper") == 0)
2779     {
2780       aPosition = Aspect_TOTP_LEFT_UPPER;
2781     } else
2782     if (strcmp(aPosType, "right_lower") == 0)
2783     {
2784       aPosition = Aspect_TOTP_RIGHT_LOWER;
2785     } else
2786     if (strcmp(aPosType, "right_upper") == 0)
2787     {
2788       aPosition = Aspect_TOTP_RIGHT_UPPER;
2789     } else
2790     {
2791       di << argv[1] << " Invalid type of alignment"  << "\n";
2792       di << "Must be one of [ center, left_lower,"   << "\n";
2793       di << "left_upper, right_lower, right_upper ]" << "\n";
2794       return 1;
2795     }
2796
2797     Standard_Real R = Draw::Atof(argv[2])/255.;
2798     Standard_Real G = Draw::Atof(argv[3])/255.;
2799     Standard_Real B = Draw::Atof(argv[4])/255.;
2800     Quantity_Color aColor(R, G, B, Quantity_TOC_RGB);
2801
2802     Standard_Real aScale = Draw::Atof(argv[5]);
2803
2804     if( aScale <= 0.0 )
2805     {
2806       di << argv[5] << " Invalid value. Must be > 0" << "\n";
2807       return 1;
2808     }
2809
2810     V3d_TypeOfVisualization aPresentation = V3d_ZBUFFER;
2811     const char* aPresType = argv[6];
2812
2813     if ( strcmp(aPresType, "wireframe") == 0 )
2814     {
2815       aPresentation = V3d_WIREFRAME;
2816     } else
2817     if (strcmp(aPresType, "zbuffer") == 0)
2818     {
2819       aPresentation = V3d_ZBUFFER;
2820     } else
2821     {
2822       di << argv[6] << " Invalid type of visualization" << "\n";
2823       di << "Must be one of [ wireframe, zbuffer ]"     << "\n";
2824       return 1;
2825     }
2826
2827     V3dView->TriedronDisplay( aPosition, aColor.Name(), aScale, aPresentation );
2828
2829   } else
2830   {
2831     di << argv[0] << " Invalid number of arguments" << "\n";
2832     return 1;
2833   }
2834
2835   V3dView->ZFitAll();
2836
2837   return 0;
2838 }
2839
2840 //==============================================================================
2841 //function : VRotate
2842 //purpose  : Camera Rotating
2843 //==============================================================================
2844
2845 static int VRotate( Draw_Interpretor& di, Standard_Integer argc, const char** argv ) {
2846   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
2847   if ( V3dView.IsNull() ) {
2848     return 1;
2849   }
2850
2851   if ( argc == 4 ) {
2852     V3dView->Rotate( Draw::Atof(argv[1]), Draw::Atof(argv[2]), Draw::Atof(argv[3]) );
2853     return 0;
2854   } else if ( argc == 7 ) {
2855     V3dView->Rotate( Draw::Atof(argv[1]), Draw::Atof(argv[2]), Draw::Atof(argv[3]), Draw::Atof(argv[4]), Draw::Atof(argv[5]), Draw::Atof(argv[6]) );
2856     return 0;
2857   } else {
2858     di << argv[0] << " Invalid number of arguments" << "\n";
2859     return 1;
2860   }
2861 }
2862
2863 //==============================================================================
2864 //function : VZoom
2865 //purpose  : View zoom in / out (relative to current zoom)
2866 //==============================================================================
2867
2868 static int VZoom( Draw_Interpretor& di, Standard_Integer argc, const char** argv ) {
2869   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
2870   if ( V3dView.IsNull() ) {
2871     return 1;
2872   }
2873
2874   if ( argc == 2 ) {
2875     Standard_Real coef = Draw::Atof(argv[1]);
2876     if ( coef <= 0.0 ) {
2877       di << argv[1] << "Invalid value" << "\n";
2878       return 1;
2879     }
2880     V3dView->SetZoom( Draw::Atof(argv[1]) );
2881     return 0;
2882   } else {
2883     di << argv[0] << " Invalid number of arguments" << "\n";
2884     return 1;
2885   }
2886 }
2887
2888 //==============================================================================
2889 //function : VPan
2890 //purpose  : View panning (in pixels)
2891 //==============================================================================
2892
2893 static int VPan( Draw_Interpretor& di, Standard_Integer argc, const char** argv ) {
2894   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
2895   if ( V3dView.IsNull() ) return 1;
2896
2897   if ( argc == 3 ) {
2898     V3dView->Pan( Draw::Atoi(argv[1]), Draw::Atoi(argv[2]) );
2899     return 0;
2900   } else {
2901     di << argv[0] << " Invalid number of arguments" << "\n";
2902     return 1;
2903   }
2904 }
2905
2906
2907 //==============================================================================
2908 //function : VExport
2909 //purpose  : Export the view to a vector graphic format (PS, EMF, PDF)
2910 //==============================================================================
2911
2912 static int VExport(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
2913 {
2914   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
2915   if (V3dView.IsNull())
2916     return 1;
2917
2918   if (argc == 1)
2919   {
2920     std::cout << "Usage: " << argv[0] << " Filename [Format]\n";
2921     return 1;
2922   }
2923
2924   Graphic3d_ExportFormat anExpFormat = Graphic3d_EF_PDF;
2925   TCollection_AsciiString aFormatStr;
2926
2927   TCollection_AsciiString aFileName (argv[1]);
2928   Standard_Integer aLen = aFileName.Length();
2929
2930   if (argc > 2)
2931   {
2932     aFormatStr = TCollection_AsciiString (argv[2]);
2933   }
2934   else if (aLen >= 4)
2935   {
2936     if (aFileName.Value (aLen - 2) == '.')
2937     {
2938       aFormatStr = aFileName.SubString (aLen - 1, aLen);
2939     }
2940     else if (aFileName.Value (aLen - 3) == '.')
2941     {
2942       aFormatStr = aFileName.SubString (aLen - 2, aLen);
2943     }
2944     else
2945     {
2946       std::cout << "Export format couln't be detected from filename '" << argv[1] << "'\n";
2947       return 1;
2948     }
2949   }
2950   else
2951   {
2952     std::cout << "Export format couln't be detected from filename '" << argv[1] << "'\n";
2953     return 1;
2954   }
2955
2956   aFormatStr.UpperCase();
2957   if (aFormatStr == "PS")
2958     anExpFormat = Graphic3d_EF_PostScript;
2959   else if (aFormatStr == "EPS")
2960     anExpFormat = Graphic3d_EF_EnhPostScript;
2961   else if (aFormatStr == "TEX")
2962     anExpFormat = Graphic3d_EF_TEX;
2963   else if (aFormatStr == "PDF")
2964     anExpFormat = Graphic3d_EF_PDF;
2965   else if (aFormatStr == "SVG")
2966     anExpFormat = Graphic3d_EF_SVG;
2967   else if (aFormatStr == "PGF")
2968     anExpFormat = Graphic3d_EF_PGF;
2969   else if (aFormatStr == "EMF")
2970     anExpFormat = Graphic3d_EF_EMF;
2971   else
2972   {
2973     std::cout << "Invalid export format '" << aFormatStr << "'\n";
2974     return 1;
2975   }
2976
2977   try {
2978     if (!V3dView->View()->Export (argv[1], anExpFormat))
2979     {
2980       di << "Error: export of image to " << aFormatStr << " failed!\n";
2981     }
2982   }
2983   catch (Standard_Failure)
2984   {
2985     di << "Error: export of image to " << aFormatStr << " failed";
2986     di << " (exception: " << Standard_Failure::Caught()->GetMessageString() << ")";
2987   }
2988   return 0;
2989 }
2990
2991 //==============================================================================
2992 //function : VColorScale
2993 //purpose  : representation color scale
2994 //==============================================================================
2995 #include <V3d_ColorScale.hxx>
2996
2997 static int VColorScale (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
2998 {
2999   if ( argc != 1 && argc != 4 && argc != 5 && argc != 6 && argc != 8 )
3000   {
3001     di << "Usage : " << argv[0] << " [RangeMin = 0 RangeMax = 100 Intervals = 10 HeightFont = 16 Position = Right X = 0 Y = 0]  " << "\n";
3002     return 1;
3003   }
3004
3005   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
3006   if(aContext.IsNull()) {
3007     di << argv[0] << " ERROR : use 'vinit' command before " << "\n";
3008     return -1;
3009   }
3010
3011   Standard_Real minRange = 0. , maxRange = 100. ;
3012
3013   Standard_Integer numIntervals = 10 ;
3014   Standard_Integer textHeight = 16;
3015   Aspect_TypeOfColorScalePosition position = Aspect_TOCSP_RIGHT;
3016   Standard_Real X = 0., Y = 0. ;
3017
3018   if ( argc < 9 )
3019   {
3020      if( argc > 3 )
3021      {
3022        minRange = Draw::Atof( argv[1] );
3023        maxRange = Draw::Atof( argv[2] );
3024        numIntervals = Draw::Atoi( argv[3] );
3025      }
3026      if ( argc > 4 )
3027        textHeight = Draw::Atoi( argv[4] );
3028      if ( argc > 5 )
3029        position = (Aspect_TypeOfColorScalePosition)Draw::Atoi( argv[5] );
3030      if ( argc > 7 )
3031      {
3032        X = Draw::Atof( argv[6] );
3033        Y = Draw::Atof( argv[7] );
3034      }
3035   }
3036   Handle(V3d_View) curView = ViewerTest::CurrentView( );
3037   if ( curView.IsNull( ) )
3038     return 1;
3039   Handle(Aspect_ColorScale) aCSV = curView->ColorScale( );
3040   Handle(V3d_ColorScale) aCS = ( Handle( V3d_ColorScale )::DownCast( aCSV ) );
3041   if( ! aCS.IsNull( ) )
3042   {
3043     aCS->SetPosition( X , Y );
3044     aCS->SetHeight( 0.95) ;
3045     aCS->SetTextHeight( textHeight );
3046     aCS->SetRange( minRange , maxRange );
3047     aCS->SetNumberOfIntervals( numIntervals );
3048     aCS->SetLabelPosition( position );
3049     if( !curView->ColorScaleIsDisplayed() )
3050       curView->ColorScaleDisplay( );
3051   }
3052   return 0;
3053 }
3054
3055 //==============================================================================
3056 //function : VGraduatedTrihedron
3057 //purpose  : Displays a graduated trihedron
3058 //==============================================================================
3059
3060 static void AddMultibyteString (TCollection_ExtendedString &name, const char *arg)
3061 {
3062   const char *str = arg;
3063   while (*str)
3064   {
3065     unsigned short c1 = *str++;
3066     unsigned short c2 = *str++;
3067     if (!c1 || !c2) break;
3068     name += (Standard_ExtCharacter)((c1 << 8) | c2);
3069   }
3070 }
3071
3072 static int VGraduatedTrihedron(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
3073 {
3074   // Check arguments
3075   if (argc != 2 && argc < 5)
3076   {
3077     di<<"Error: "<<argv[0]<<" - invalid number of arguments\n";
3078     di<<"Usage: type help "<<argv[0]<<"\n";
3079     return 1; //TCL_ERROR
3080   }
3081
3082   Handle(V3d_View) aV3dView = ViewerTest::CurrentView();
3083
3084   // Create 3D view if it doesn't exist
3085   if ( aV3dView.IsNull() )
3086   {
3087     ViewerTest::ViewerInit();
3088     aV3dView = ViewerTest::CurrentView();
3089     if( aV3dView.IsNull() )
3090     {
3091       di << "Error: Cannot create a 3D view\n";
3092       return 1; //TCL_ERROR
3093     }
3094   }
3095
3096   // Erase (==0) or display (!=0)
3097   const int display = Draw::Atoi(argv[1]);
3098
3099   if (display)
3100   {
3101     // Text font
3102     TCollection_AsciiString font;
3103     if (argc < 6)
3104       font.AssignCat("Courier");
3105     else
3106       font.AssignCat(argv[5]);
3107
3108     // Text is multibyte
3109     const Standard_Boolean isMultibyte = (argc < 7)? Standard_False : (Draw::Atoi(argv[6]) != 0);
3110
3111     // Set axis names
3112     TCollection_ExtendedString xname, yname, zname;
3113     if (argc >= 5)
3114     {
3115       if (isMultibyte)
3116       {
3117         AddMultibyteString(xname, argv[2]);
3118         AddMultibyteString(yname, argv[3]);
3119         AddMultibyteString(zname, argv[4]);
3120       }
3121       else
3122       {
3123         xname += argv[2];
3124         yname += argv[3];
3125         zname += argv[4];
3126       }
3127     }
3128     else
3129     {
3130       xname += "X (mm)";
3131       yname += "Y (mm)";
3132       zname += "Z (mm)";
3133     }
3134
3135     aV3dView->GraduatedTrihedronDisplay(xname, yname, zname,
3136                                         Standard_True/*xdrawname*/, Standard_True/*ydrawname*/, Standard_True/*zdrawname*/,
3137                                         Standard_True/*xdrawvalues*/, Standard_True/*ydrawvalues*/, Standard_True/*zdrawvalues*/,
3138                                         Standard_True/*drawgrid*/,
3139                                         Standard_True/*drawaxes*/,
3140                                         5/*nbx*/, 5/*nby*/, 5/*nbz*/,
3141                                         10/*xoffset*/, 10/*yoffset*/, 10/*zoffset*/,
3142                                         30/*xaxisoffset*/, 30/*yaxisoffset*/, 30/*zaxisoffset*/,
3143                                         Standard_True/*xdrawtickmarks*/, Standard_True/*ydrawtickmarks*/, Standard_True/*zdrawtickmarks*/,
3144                                         10/*xtickmarklength*/, 10/*ytickmarklength*/, 10/*ztickmarklength*/,
3145                                         Quantity_NOC_WHITE/*gridcolor*/,
3146                                         Quantity_NOC_RED/*xnamecolor*/,Quantity_NOC_GREEN/*ynamecolor*/,Quantity_NOC_BLUE1/*znamecolor*/,
3147                                         Quantity_NOC_RED/*xcolor*/,Quantity_NOC_GREEN/*ycolor*/,Quantity_NOC_BLUE1/*zcolor*/,font);
3148   }
3149   else
3150     aV3dView->GraduatedTrihedronErase();
3151
3152   ViewerTest::GetAISContext()->UpdateCurrentViewer();
3153   aV3dView->Redraw();
3154
3155   return 0;
3156 }
3157
3158 //==============================================================================
3159 //function : VPrintView
3160 //purpose  : Test printing algorithm, print the view to image file with given
3161 //           width and height. Printing implemented only for WNT.
3162 //==============================================================================
3163 static int VPrintView (Draw_Interpretor& di, Standard_Integer argc,
3164                        const char** argv)
3165 {
3166 #ifndef WNT
3167   di << "Printing implemented only for wnt!\n";
3168   return 1;
3169 #else
3170
3171   Handle(AIS_InteractiveContext) aContextAIS = NULL;
3172   Handle(V3d_View) aView = NULL;
3173   aContextAIS = ViewerTest::GetAISContext();
3174   if (!aContextAIS.IsNull())
3175   {
3176     const Handle(V3d_Viewer)& Vwr = aContextAIS->CurrentViewer();
3177     Vwr->InitActiveViews();
3178     if(Vwr->MoreActiveViews())
3179       aView = Vwr->ActiveView();
3180   }
3181
3182   // check for errors
3183   if (aView.IsNull())
3184   {
3185     di << "Call vinit before!\n";
3186     return 1;
3187   }
3188   else if (argc < 4)
3189   {
3190     di << "Use: " << argv[0];
3191     di << " width height filename [print algo=0]\n";
3192     di << "width, height of the intermediate buffer for operation\n";
3193     di << "algo : {0|1}\n";
3194     di << "        0 - stretch algorithm\n";
3195     di << "        1 - tile algorithm\n";
3196     di << "test printing algorithms into an intermediate buffer\n";
3197     di << "with saving output to an image file\n";
3198     return 1;
3199   }
3200
3201   // get the input params
3202   Standard_Integer aWidth  = Draw::Atoi (argv[1]);
3203   Standard_Integer aHeight = Draw::Atoi (argv[2]);
3204   Standard_Integer aMode   = 0;
3205   TCollection_AsciiString aFileName = TCollection_AsciiString (argv[3]);
3206   if (argc==5)
3207     aMode = Draw::Atoi (argv[4]);
3208
3209   // check the input parameters
3210   if (aWidth <= 0 || aHeight <= 0)
3211   {
3212     di << "Width and height must be positive values!\n";
3213     return 1;
3214   }
3215   if (aMode != 0 && aMode != 1)
3216     aMode = 0;
3217
3218   // define compatible bitmap
3219   HDC anDC = CreateCompatibleDC(0);
3220   BITMAPINFO aBitmapData;
3221   memset (&aBitmapData, 0, sizeof (BITMAPINFOHEADER));
3222   aBitmapData.bmiHeader.biSize          = sizeof (BITMAPINFOHEADER);
3223   aBitmapData.bmiHeader.biWidth         = aWidth ;
3224   aBitmapData.bmiHeader.biHeight        = aHeight;
3225   aBitmapData.bmiHeader.biPlanes        = 1;
3226   aBitmapData.bmiHeader.biBitCount      = 24;
3227   aBitmapData.bmiHeader.biXPelsPerMeter = 0;
3228   aBitmapData.bmiHeader.biYPelsPerMeter = 0;
3229   aBitmapData.bmiHeader.biClrUsed       = 0;
3230   aBitmapData.bmiHeader.biClrImportant  = 0;
3231   aBitmapData.bmiHeader.biCompression   = BI_RGB;
3232   aBitmapData.bmiHeader.biSizeImage     = 0;
3233
3234   // Create Device Independent Bitmap
3235   void* aBitsOut = NULL;
3236   HBITMAP aMemoryBitmap = CreateDIBSection (anDC, &aBitmapData, DIB_RGB_COLORS,
3237                                             &aBitsOut, NULL, 0);
3238   HGDIOBJ anOldBitmap   = SelectObject(anDC, aMemoryBitmap);
3239
3240   Standard_Boolean isSaved = Standard_False, isPrinted = Standard_False;
3241   if (aBitsOut != NULL)
3242   {
3243     if (aMode == 0)
3244       isPrinted = aView->Print(anDC,1,1,0,Aspect_PA_STRETCH);
3245     else
3246       isPrinted = aView->Print(anDC,1,1,0,Aspect_PA_TILE);
3247
3248     // succesfully printed into an intermediate buffer
3249     if (isPrinted)
3250     {
3251       Image_PixMap aWrapper;
3252       aWrapper.InitWrapper (Image_PixMap::ImgBGR, (Standard_Byte* )aBitsOut, aWidth, aHeight, aWidth * 3 + aWidth % 4);
3253       aWrapper.SetTopDown (false);
3254
3255       Image_AlienPixMap anImageBitmap;
3256       anImageBitmap.InitCopy (aWrapper);
3257       isSaved = anImageBitmap.Save (aFileName);
3258     }
3259     else
3260     {
3261       di << "Print operation failed due to printing errors or\n";
3262       di << "insufficient memory available\n";
3263       di << "Please, try to use smaller dimensions for this test\n";
3264       di << "command, as it allocates intermediate buffer for storing\n";
3265       di << "the result\n";
3266     }
3267   }
3268   else
3269   {
3270     di << "Can't allocate memory for intermediate buffer\n";
3271     di << "Please use smaller dimensions\n";
3272   }
3273
3274   if (aMemoryBitmap)
3275   {
3276     SelectObject (anDC, anOldBitmap);
3277     DeleteObject (aMemoryBitmap);
3278     DeleteDC(anDC);
3279   }
3280
3281   if (!isSaved)
3282   {
3283     di << "Save to file operation failed. This operation may fail\n";
3284     di << "if you don't have enough available memory, then you can\n";
3285     di << "use smaller dimensions for the output file\n";
3286     return 1;
3287   }
3288
3289   return 0;
3290
3291 #endif
3292 }
3293
3294 //==============================================================================
3295 //function : VZLayer
3296 //purpose  : Test z layer operations for v3d viewer
3297 //==============================================================================
3298 static int VZLayer (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
3299 {
3300   Handle(AIS_InteractiveContext) aContextAIS = ViewerTest::GetAISContext ();
3301   if (aContextAIS.IsNull())
3302   {
3303     di << "Call vinit before!\n";
3304     return 1;
3305   }
3306   else if (argc < 2)
3307   {
3308     di << "Use: vzlayer " << argv[0];
3309     di << " add/del/get [id]\n";
3310     di << " add - add new z layer to viewer and print its id\n";
3311     di << " del - del z layer by its id\n";
3312     di << " get - print sequence of z layers in increasing order of their overlay level\n";
3313     di << "id - the layer identificator value defined when removing z layer\n";
3314     return 1;
3315   }
3316
3317   const Handle(V3d_Viewer)& aViewer = aContextAIS->CurrentViewer();
3318   if (aViewer.IsNull())
3319   {
3320     di << "No active viewer!\n";
3321     return 1;
3322   }
3323
3324   // perform operation
3325   TCollection_AsciiString anOp = TCollection_AsciiString (argv[1]);
3326   if (anOp == "add")
3327   {
3328     Standard_Integer aNewId;
3329     if (!aViewer->AddZLayer (aNewId))
3330     {
3331       di << "Impossible to add new z layer!\n";
3332       return 1;
3333     }
3334
3335     di << "New z layer added with index: " << aNewId << "\n";
3336   }
3337   else if (anOp == "del")
3338   {
3339     if (argc < 3)
3340     {
3341       di << "Please also provide as argument id of z layer to remove\n";
3342       return 1;
3343     }
3344
3345     Standard_Integer aDelId = Draw::Atoi (argv[2]);
3346     if (!aViewer->RemoveZLayer (aDelId))
3347     {
3348       di << "Impossible to remove the z layer or invalid id!\n";
3349       return 1;
3350     }
3351
3352     di << "Z layer " << aDelId << " has been removed\n";
3353   }
3354   else if (anOp == "get")
3355   {
3356     TColStd_SequenceOfInteger anIds;
3357     aViewer->GetAllZLayers (anIds);
3358     for (Standard_Integer aSeqIdx = 1; aSeqIdx <= anIds.Length(); aSeqIdx++)
3359     {
3360       di << anIds.Value (aSeqIdx) << " ";
3361     }
3362
3363     di << "\n";
3364   }
3365   else
3366   {
3367     di << "Invalid operation, please use { add / del / get }\n";
3368     return 1;
3369   }
3370
3371   return 0;
3372 }
3373
3374 DEFINE_STANDARD_HANDLE(V3d_TextItem, Visual3d_LayerItem)
3375
3376 // this class provides a presentation of text item in v3d view under-/overlayer
3377 class V3d_TextItem : public Visual3d_LayerItem
3378 {
3379 public:
3380
3381   // CASCADE RTTI
3382   DEFINE_STANDARD_RTTI(V3d_TextItem)
3383
3384   // constructor
3385   Standard_EXPORT V3d_TextItem(const TCollection_AsciiString& theText,
3386                                const Standard_Real theX1,
3387                                const Standard_Real theY1,
3388                                const Standard_Real theHeight,
3389                                const TCollection_AsciiString& theFontName,
3390                                const Quantity_Color& theColor,
3391                                const Quantity_Color& theSubtitleColor,
3392                                const Aspect_TypeOfDisplayText& theTypeOfDisplay,
3393                                const Handle(Visual3d_Layer)& theLayer);
3394
3395   // redraw method
3396   Standard_EXPORT void RedrawLayerPrs();
3397
3398 private:
3399
3400   Standard_Real            myX1;
3401   Standard_Real            myY1;
3402   TCollection_AsciiString  myText;
3403   Standard_Real            myHeight;
3404   Handle(Visual3d_Layer)   myLayer;
3405   Quantity_Color           myColor;
3406   Quantity_Color           mySubtitleColor;
3407   Aspect_TypeOfDisplayText myType;
3408   TCollection_AsciiString  myFontName;
3409 };
3410
3411 IMPLEMENT_STANDARD_HANDLE(V3d_TextItem, Visual3d_LayerItem)
3412 IMPLEMENT_STANDARD_RTTIEXT(V3d_TextItem, Visual3d_LayerItem)
3413
3414 // create and add to display the text item
3415 V3d_TextItem::V3d_TextItem (const TCollection_AsciiString& theText,
3416                             const Standard_Real theX1,
3417                             const Standard_Real theY1,
3418                             const Standard_Real theHeight,
3419                             const TCollection_AsciiString& theFontName,
3420                             const Quantity_Color& theColor,
3421                             const Quantity_Color& theSubtitleColor,
3422                             const Aspect_TypeOfDisplayText& theTypeOfDisplay,
3423                             const Handle(Visual3d_Layer)& theLayer)
3424  : myX1 (theX1), myY1 (theY1),
3425    myText (theText),
3426    myHeight (theHeight),
3427    myLayer (theLayer),
3428    myColor (theColor),
3429    mySubtitleColor (theSubtitleColor),
3430    myType (theTypeOfDisplay),
3431    myFontName (theFontName)
3432 {
3433   if (!myLayer.IsNull ())
3434     myLayer->AddLayerItem (this);
3435 }
3436
3437 // render item
3438 void V3d_TextItem::RedrawLayerPrs ()
3439 {
3440   if (myLayer.IsNull ())
3441     return;
3442
3443   myLayer->SetColor (myColor);
3444   myLayer->SetTextAttributes (myFontName.ToCString (), myType, mySubtitleColor);
3445   myLayer->DrawText (myText.ToCString (), myX1, myY1, myHeight);
3446 }
3447
3448 DEFINE_STANDARD_HANDLE(V3d_LineItem, Visual3d_LayerItem)
3449
3450 // The Visual3d_LayerItem line item for "vlayerline" command
3451 // it provides a presentation of line with user-defined
3452 // linewidth, linetype and transparency.
3453 class V3d_LineItem : public Visual3d_LayerItem
3454 {
3455 public:
3456   // CASCADE RTTI
3457   DEFINE_STANDARD_RTTI(V3d_LineItem)
3458
3459   // constructor
3460   Standard_EXPORT V3d_LineItem(Standard_Real X1, Standard_Real Y1,
3461                                Standard_Real X2, Standard_Real Y2,
3462                                V3d_LayerMgrPointer theLayerMgr,
3463                                Aspect_TypeOfLine theType = Aspect_TOL_SOLID,
3464                                Standard_Real theWidth    = 0.5,
3465                                Standard_Real theTransp   = 1.0);
3466
3467   // redraw method
3468   Standard_EXPORT   void RedrawLayerPrs();
3469
3470 private:
3471
3472   Standard_Real       myX1, myY1, myX2, myY2;
3473   V3d_LayerMgrPointer myLayerMgr;
3474   Aspect_TypeOfLine   myType;
3475   Standard_Real       myWidth;
3476   Standard_Real       myTransparency;
3477 };
3478
3479 IMPLEMENT_STANDARD_HANDLE(V3d_LineItem, Visual3d_LayerItem)
3480 IMPLEMENT_STANDARD_RTTIEXT(V3d_LineItem, Visual3d_LayerItem)
3481
3482 // default constructor for line item
3483 V3d_LineItem::V3d_LineItem(Standard_Real X1, Standard_Real Y1,
3484                            Standard_Real X2, Standard_Real Y2,
3485                            V3d_LayerMgrPointer theLayerMgr,
3486                            Aspect_TypeOfLine theType,
3487                            Standard_Real theWidth,
3488                            Standard_Real theTransp) :
3489   myX1(X1), myY1(Y1), myX2(X2), myY2(Y2), myLayerMgr(theLayerMgr),
3490   myType(theType), myWidth(theWidth), myTransparency(theTransp)
3491 {
3492   if (myLayerMgr && !myLayerMgr->Overlay().IsNull())
3493     myLayerMgr->Overlay()->AddLayerItem (this);
3494 }
3495
3496 // render line
3497 void V3d_LineItem::RedrawLayerPrs ()
3498 {
3499   Handle (Visual3d_Layer) aOverlay;
3500
3501   if (myLayerMgr)
3502     aOverlay = myLayerMgr->Overlay();
3503
3504   if (!aOverlay.IsNull())
3505   {
3506     Quantity_Color aColor(1.0, 0, 0, Quantity_TOC_RGB);
3507     aOverlay->SetColor(aColor);
3508     aOverlay->SetTransparency((Standard_ShortReal)myTransparency);
3509     aOverlay->SetLineAttributes((Aspect_TypeOfLine)myType, myWidth);
3510     aOverlay->BeginPolyline();
3511     aOverlay->AddVertex(myX1, myY1);
3512     aOverlay->AddVertex(myX2, myY2);
3513     aOverlay->ClosePrimitive();
3514   }
3515 }
3516
3517 //=============================================================================
3518 //function : VLayerLine
3519 //purpose  : Draws line in the v3d view layer with given attributes: linetype,
3520 //         : linewidth, transparency coefficient
3521 //============================================================================
3522 static int VLayerLine(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
3523 {
3524   // get the active view
3525   Handle(V3d_View) aView = ViewerTest::CurrentView();
3526   if (aView.IsNull())
3527   {
3528     di << "Call vinit before!\n";
3529     return 1;
3530   }
3531   else if (argc < 5)
3532   {
3533     di << "Use: " << argv[0];
3534     di << " x1 y1 x2 y2 [linewidth = 0.5] [linetype = 0] [transparency = 1]\n";
3535     di << " linetype : { 0 | 1 | 2 | 3 } \n";
3536     di << "              0 - solid  \n";
3537     di << "              1 - dashed \n";
3538     di << "              2 - dot    \n";
3539     di << "              3 - dashdot\n";
3540     di << " transparency : { 0.0 - 1.0 } \n";
3541     di << "                  0.0 - transparent\n";
3542     di << "                  1.0 - visible    \n";
3543     return 1;
3544   }
3545
3546   // get the input params
3547   Standard_Real X1 = Draw::Atof(argv[1]);
3548   Standard_Real Y1 = Draw::Atof(argv[2]);
3549   Standard_Real X2 = Draw::Atof(argv[3]);
3550   Standard_Real Y2 = Draw::Atof(argv[4]);
3551
3552   Standard_Real    aWidth = 0.5;
3553   Standard_Integer aType  = 0;
3554   Standard_Real    aTransparency = 1.0;
3555
3556   // has width
3557   if (argc > 5)
3558     aWidth = Draw::Atof(argv[5]);
3559
3560   // has type
3561   if (argc > 6)
3562      aType = (Standard_Integer) Draw::Atoi(argv[6]);
3563
3564   // has transparency
3565   if (argc > 7)
3566   {
3567     aTransparency = Draw::Atof(argv[7]);
3568     if (aTransparency < 0 || aTransparency > 1.0)
3569       aTransparency = 1.0;
3570   }
3571
3572   // select appropriate line type
3573   Aspect_TypeOfLine aLineType;
3574   switch (aType)
3575   {
3576     case 1:
3577       aLineType = Aspect_TOL_DASH;
3578     break;
3579
3580     case 2:
3581       aLineType = Aspect_TOL_DOT;
3582     break;
3583
3584     case 3:
3585       aLineType = Aspect_TOL_DOTDASH;
3586     break;
3587
3588     default:
3589       aLineType = Aspect_TOL_SOLID;
3590   }
3591
3592   // replace layer manager
3593   Handle(V3d_LayerMgr) aMgr = new V3d_LayerMgr(aView);
3594   aView->SetLayerMgr(aMgr);
3595
3596   // add line item
3597   Handle (V3d_LineItem) anItem = new V3d_LineItem(X1, Y1, X2, Y2,
3598                                                   aMgr.operator->(),
3599                                                   aLineType, aWidth,
3600                                                   aTransparency);
3601
3602   // update view
3603   aView->MustBeResized();
3604   aView->Redraw();
3605
3606   return 0;
3607 }
3608
3609 //=======================================================================
3610 //function : VOverlayText
3611 //purpose  : Test text displaying in view overlay
3612 //=======================================================================
3613 static int VOverlayText (Draw_Interpretor& di, Standard_Integer argc, const char**argv)
3614 {
3615   // get the active view
3616   Handle(V3d_View) aView = ViewerTest::CurrentView();
3617   if (aView.IsNull())
3618   {
3619     di << "No active view. Please call vinit.\n";
3620     return 1;
3621   }
3622   else if (argc < 4 || argc > 13)
3623   {
3624     di << "Use: " << argv[0];
3625     di << " text x y [height] [font_name] [text_color: R G B] [displayType]\n";
3626     di << "[background_color: R G B]\n";
3627     di << "  height - pixel height of the text (default=10.0)\n";
3628     di << "  font_name - name of font (default=courier)\n";
3629     di << "  text_color - R G B values of text color (default=255.0 255.0 255.0)\n";
3630     di << "  display_type = {normal/subtitle/decal/blend/dimension}, (default=normal)\n";
3631     di << "  background_color- R G B values used for subtitle and decal text\n";
3632     di << "(default=255.0 255.0 255.0)\n";
3633     return 1;
3634   }
3635
3636   TCollection_AsciiString aText (argv[1]);
3637   Standard_Real aPosX = Draw::Atof(argv[2]);
3638   Standard_Real aPosY = Draw::Atof(argv[3]);
3639   Standard_Real aHeight = (argc >= 5) ? Draw::Atof (argv[4]) : 10.0;
3640
3641   // font name
3642   TCollection_AsciiString aFontName = "Courier";
3643   if (argc >= 6)
3644     aFontName = TCollection_AsciiString (argv[5]);
3645
3646   // text colors
3647   Quantity_Parameter aColorRed   = 1.0;
3648   Quantity_Parameter aColorGreen = 1.0;
3649   Quantity_Parameter aColorBlue  = 1.0;
3650   if (argc >= 9)
3651   {
3652     aColorRed   = Draw::Atof (argv[6])/255.;
3653     aColorGreen = Draw::Atof (argv[7])/255.;
3654     aColorBlue  = Draw::Atof (argv[8])/255.;
3655   }
3656
3657   // display type
3658   TCollection_AsciiString aDispStr;
3659   if (argc >= 10)
3660     aDispStr = TCollection_AsciiString (argv[9]);
3661
3662   Aspect_TypeOfDisplayText aTextType = Aspect_TODT_NORMAL;
3663   if (aDispStr.IsEqual ("subtitle"))
3664     aTextType = Aspect_TODT_SUBTITLE;
3665   else if (aDispStr.IsEqual ("decal"))
3666     aTextType = Aspect_TODT_DEKALE;
3667   else if (aDispStr.IsEqual ("blend"))
3668     aTextType = Aspect_TODT_BLEND;
3669   else if (aDispStr.IsEqual ("dimension"))
3670     aTextType = Aspect_TODT_DIMENSION;
3671
3672   // subtitle color
3673   Quantity_Parameter aSubRed   = 1.0;
3674   Quantity_Parameter aSubGreen = 1.0;
3675   Quantity_Parameter aSubBlue  = 1.0;
3676   if (argc == 13)
3677   {
3678     aSubRed   = Draw::Atof (argv[10])/255.;
3679     aSubGreen = Draw::Atof (argv[11])/255.;
3680     aSubBlue  = Draw::Atof (argv[12])/255.;
3681   }
3682
3683   // check fo current overlay
3684   Handle(Visual3d_Layer) anOverlay = aView->Viewer()->Viewer()->OverLayer ();
3685   if (anOverlay.IsNull ())
3686   {
3687     Handle(V3d_LayerMgr) aMgr = new V3d_LayerMgr (aView);
3688     anOverlay = aMgr->Overlay ();
3689     aView->SetLayerMgr (aMgr);
3690   }
3691
3692   Quantity_Color aTextColor (aColorRed, aColorGreen,
3693     aColorBlue, Quantity_TOC_RGB);
3694   Quantity_Color aSubtColor (aSubRed, aSubGreen,
3695     aSubBlue, Quantity_TOC_RGB);
3696
3697   // add text item
3698   Handle(V3d_TextItem) anItem = new V3d_TextItem (aText, aPosX, aPosY,
3699     aHeight, aFontName, aTextColor, aSubtColor, aTextType, anOverlay);
3700
3701   // update view
3702   aView->MustBeResized();
3703   aView->Redraw();
3704
3705   return 0;
3706 }
3707
3708 //==============================================================================
3709 //function : VGrid
3710 //purpose  :
3711 //==============================================================================
3712
3713 static int VGrid (Draw_Interpretor& /*theDI*/,
3714                   Standard_Integer  theArgNb,
3715                   const char**      theArgVec)
3716 {
3717   // get the active view
3718   Handle(V3d_View)   aView   = ViewerTest::CurrentView();
3719   Handle(V3d_Viewer) aViewer = ViewerTest::GetViewerFromContext();
3720   if (aView.IsNull() || aViewer.IsNull())
3721   {
3722     std::cerr << "No active view. Please call vinit.\n";
3723     return 1;
3724   }
3725
3726   Aspect_GridType     aType = aViewer->GridType();
3727   Aspect_GridDrawMode aMode = aViewer->GridDrawMode();
3728
3729   Standard_Integer anIter = 1;
3730   for (; anIter < theArgNb; ++anIter)
3731   {
3732     const char* aValue = theArgVec[anIter];
3733     if (*aValue == 'r')
3734     {
3735       aType = Aspect_GT_Rectangular;
3736     }
3737     else if (*aValue == 'c')
3738     {
3739       aType = Aspect_GT_Circular;
3740     }
3741     else if (*aValue == 'l')
3742     {
3743       aMode = Aspect_GDM_Lines;
3744     }
3745     else if (*aValue == 'p')
3746     {
3747       aMode = Aspect_GDM_Points;
3748     }
3749     else if (strcmp (aValue, "off" ) == 0)
3750     {
3751       aViewer->DeactivateGrid();
3752       return 0;
3753     }
3754     else
3755     {
3756       break;
3757     }
3758   }
3759
3760   Standard_Integer aTail = (theArgNb - anIter);
3761   if (aTail == 0)
3762   {
3763     aViewer->ActivateGrid (aType, aMode);
3764     return 0;
3765   }
3766   else if (aTail != 2 && aTail != 5)
3767   {
3768     std::cerr << "Incorrect arguments number! Usage:\n"
3769               << "vgrid [off] [Mode={r|c}] [Type={l|p}] [OriginX OriginY [StepX/StepRadius StepY/DivNb RotAngle]]\n";
3770     return 1;
3771   }
3772
3773   Quantity_Length anOriginX, anOriginY;
3774   Quantity_PlaneAngle aRotAngle;
3775   if (aType == Aspect_GT_Rectangular)
3776   {
3777     Quantity_Length aRStepX, aRStepY;
3778     aViewer->RectangularGridValues (anOriginX, anOriginY, aRStepX, aRStepY, aRotAngle);
3779
3780     anOriginX = Draw::Atof (theArgVec[anIter++]);
3781     anOriginY = Draw::Atof (theArgVec[anIter++]);
3782     if (aTail == 5)
3783     {
3784       aRStepX   = Draw::Atof (theArgVec[anIter++]);
3785       aRStepY   = Draw::Atof (theArgVec[anIter++]);
3786       aRotAngle = Draw::Atof (theArgVec[anIter++]);
3787     }
3788     aViewer->SetRectangularGridValues (anOriginX, anOriginY, aRStepX, aRStepY, aRotAngle);
3789     aViewer->ActivateGrid (aType, aMode);
3790   }
3791   else if (aType == Aspect_GT_Circular)
3792   {
3793     Quantity_Length aRadiusStep;
3794     Standard_Integer aDivisionNumber;
3795     aViewer->CircularGridValues (anOriginX, anOriginY, aRadiusStep, aDivisionNumber, aRotAngle);
3796
3797     anOriginX = Draw::Atof (theArgVec[anIter++]);
3798     anOriginY = Draw::Atof (theArgVec[anIter++]);
3799     if (aTail == 5)
3800     {
3801       aRadiusStep     = Draw::Atof (theArgVec[anIter++]);
3802       aDivisionNumber = Draw::Atoi (theArgVec[anIter++]);
3803       aRotAngle       = Draw::Atof (theArgVec[anIter++]);
3804     }
3805
3806     aViewer->SetCircularGridValues (anOriginX, anOriginY, aRadiusStep, aDivisionNumber, aRotAngle);
3807     aViewer->ActivateGrid (aType, aMode);
3808   }
3809
3810   return 0;
3811 }
3812
3813 //==============================================================================
3814 //function : VFps
3815 //purpose  :
3816 //==============================================================================
3817
3818 static int VFps (Draw_Interpretor& theDI,
3819                  Standard_Integer  theArgNb,
3820                  const char**      theArgVec)
3821 {
3822   // get the active view
3823   Handle(V3d_View) aView = ViewerTest::CurrentView();
3824   if (aView.IsNull())
3825   {
3826     std::cerr << "No active view. Please call vinit.\n";
3827     return 1;
3828   }
3829
3830   Standard_Integer aFramesNb = (theArgNb > 1) ? Draw::Atoi(theArgVec[1]) : 100;
3831   if (aFramesNb <= 0)
3832   {
3833     std::cerr << "Incorrect arguments!\n";
3834     return 1;
3835   }
3836
3837   // the time is meaningless for first call
3838   // due to async OpenGl rendering
3839   aView->Redraw();
3840
3841   // redraw view in loop to estimate average values
3842   OSD_Timer aTimer;
3843   aTimer.Start();
3844   for (Standard_Integer anInter = 0; anInter < aFramesNb; ++anInter)
3845   {
3846     aView->Redraw();
3847   }
3848   aTimer.Stop();
3849   Standard_Real aCpu;
3850   const Standard_Real aTime = aTimer.ElapsedTime();
3851   aTimer.OSD_Chronometer::Show (aCpu);
3852
3853   const Standard_Real aFpsAver = Standard_Real(aFramesNb) / aTime;
3854   const Standard_Real aCpuAver = aCpu / Standard_Real(aFramesNb);
3855
3856   // return statistics
3857   theDI << "FPS: " << aFpsAver << "\n"
3858         << "CPU: " << (1000.0 * aCpuAver) << " msec\n";
3859
3860   return 0;
3861 }
3862
3863 //==============================================================================
3864 //function : VGlDebug
3865 //purpose  :
3866 //==============================================================================
3867
3868 static int VGlDebug (Draw_Interpretor& theDI,
3869                      Standard_Integer  theArgNb,
3870                      const char**      theArgVec)
3871 {
3872   if (theArgNb < 2)
3873   {
3874     Handle(V3d_View) aView = ViewerTest::CurrentView();
3875     if (aView.IsNull())
3876     {
3877       std::cerr << "No active view. Please call vinit.\n";
3878       return 0;
3879     }
3880
3881     Standard_Boolean isActive = OpenGl_Context::CheckExtension ((const char* )glGetString (GL_EXTENSIONS),
3882                                                                 "GL_ARB_debug_output");
3883     std::cout << "Active graphic driver: debug " << (isActive ? "ON" : "OFF") << "\n";
3884     theDI << (isActive ? "1" : "0");
3885     return 0;
3886   }
3887
3888   ViewerTest_myDefaultCaps.contextDebug = Draw::Atoi (theArgVec[1]) != 0;
3889   return 0;
3890 }
3891
3892 //==============================================================================
3893 //function : VVbo
3894 //purpose  :
3895 //==============================================================================
3896
3897 static int VVbo (Draw_Interpretor& theDI,
3898                  Standard_Integer  theArgNb,
3899                  const char**      theArgVec)
3900 {
3901   const Standard_Boolean toSet    = (theArgNb > 1);
3902   const Standard_Boolean toUseVbo = toSet ? (Draw::Atoi (theArgVec[1]) == 0) : 1;
3903   if (toSet)
3904   {
3905     ViewerTest_myDefaultCaps.vboDisable = toUseVbo;
3906   }
3907
3908   // get the context
3909   Handle(AIS_InteractiveContext) aContextAIS = ViewerTest::GetAISContext();
3910   if (aContextAIS.IsNull())
3911   {
3912     if (!toSet)
3913     {
3914       std::cerr << "No active view!\n";
3915     }
3916     return 1;
3917   }
3918   Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast (aContextAIS->CurrentViewer()->Driver());
3919   if (!aDriver.IsNull())
3920   {
3921     if (!toSet)
3922     {
3923       theDI << (aDriver->Options().vboDisable ? "0" : "1") << "\n";
3924     }
3925     else
3926     {
3927       aDriver->ChangeOptions().vboDisable = toUseVbo;
3928     }
3929   }
3930
3931   return 0;
3932 }
3933
3934 //==============================================================================
3935 //function : VCaps
3936 //purpose  :
3937 //==============================================================================
3938
3939 static int VCaps (Draw_Interpretor& theDI,
3940                   Standard_Integer  theArgNb,
3941                   const char**      theArgVec)
3942 {
3943   OpenGl_Caps* aCaps = &ViewerTest_myDefaultCaps;
3944   Handle(OpenGl_GraphicDriver)   aDriver;
3945   Handle(AIS_InteractiveContext) aContextAIS = ViewerTest::GetAISContext();
3946   if (!aContextAIS.IsNull())
3947   {
3948     aDriver = Handle(OpenGl_GraphicDriver)::DownCast (aContextAIS->CurrentViewer()->Driver());
3949     aCaps   = &aDriver->ChangeOptions();
3950   }
3951
3952   if (theArgNb < 2)
3953   {
3954     theDI << "VBO:     " << (aCaps->vboDisable        ? "0" : "1") << "\n";
3955     theDI << "Sprites: " << (aCaps->pntSpritesDisable ? "0" : "1") << "\n";
3956     theDI << "SoftMode:" << (aCaps->contextNoAccel    ? "1" : "0") << "\n";
3957     return 0;
3958   }
3959
3960   for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
3961   {
3962     const TCollection_AsciiString anArg (theArgVec[anArgIter]);
3963     if (anArg.Search ("vbo=") > -1)
3964     {
3965       aCaps->vboDisable        = anArg.Token ("=", 2).IntegerValue() == 0;
3966     }
3967     else if (anArg.Search ("sprites=") > -1)
3968     {
3969       aCaps->pntSpritesDisable = anArg.Token ("=", 2).IntegerValue() == 0;
3970     }
3971     else if (anArg.Search ("soft=") > -1)
3972     {
3973       aCaps->contextNoAccel = anArg.Token ("=", 2).IntegerValue() != 0;
3974     }
3975     else
3976     {
3977       std::cerr << "Unknown argument: " << anArg << "\n";
3978     }
3979   }
3980   if (aCaps != &ViewerTest_myDefaultCaps)
3981   {
3982     ViewerTest_myDefaultCaps = *aCaps;
3983   }
3984   return 0;
3985 }
3986
3987 //==============================================================================
3988 //function : VMemGpu
3989 //purpose  :
3990 //==============================================================================
3991
3992 static int VMemGpu (Draw_Interpretor& theDI,
3993                     Standard_Integer  theArgNb,
3994                     const char**      theArgVec)
3995 {
3996   // get the context
3997   Handle(AIS_InteractiveContext) aContextAIS = ViewerTest::GetAISContext();
3998   if (aContextAIS.IsNull())
3999   {
4000     std::cerr << "No active view. Please call vinit.\n";
4001     return 1;
4002   }
4003
4004   Handle(Graphic3d_GraphicDriver) aDriver = aContextAIS->CurrentViewer()->Driver();
4005   if (aDriver.IsNull())
4006   {
4007     std::cerr << "Graphic driver not available.\n";
4008     return 1;
4009   }
4010
4011   Standard_Size aFreeBytes = 0;
4012   TCollection_AsciiString anInfo;
4013   if (!aDriver->MemoryInfo (aFreeBytes, anInfo))
4014   {
4015     std::cerr << "Information not available.\n";
4016     return 1;
4017   }
4018
4019   if (theArgNb > 1 && *theArgVec[1] == 'f')
4020   {
4021     theDI << Standard_Real (aFreeBytes);
4022   }
4023   else
4024   {
4025     theDI << anInfo;
4026   }
4027
4028   return 0;
4029 }
4030
4031 // ==============================================================================
4032 // function : VReadPixel
4033 // purpose  :
4034 // ==============================================================================
4035 static int VReadPixel (Draw_Interpretor& theDI,
4036                        Standard_Integer  theArgNb,
4037                        const char**      theArgVec)
4038 {
4039   // get the active view
4040   Handle(V3d_View) aView = ViewerTest::CurrentView();
4041   if (aView.IsNull())
4042   {
4043     std::cerr << "No active view. Please call vinit.\n";
4044     return 1;
4045   }
4046   else if (theArgNb < 3)
4047   {
4048     std::cerr << "Usage : " << theArgVec[0] << " xPixel yPixel [{rgb|rgba|depth|hls|rgbf|rgbaf}=rgba] [name]\n";
4049     return 1;
4050   }
4051
4052   Image_PixMap::ImgFormat aFormat     = Image_PixMap::IsBigEndianHost() ? Image_PixMap::ImgRGBA : Image_PixMap::ImgBGRA;
4053   Graphic3d_BufferType    aBufferType = Graphic3d_BT_RGBA;
4054
4055   Standard_Integer aWidth, aHeight;
4056   aView->Window()->Size (aWidth, aHeight);
4057   const Standard_Integer anX = Draw::Atoi (theArgVec[1]);
4058   const Standard_Integer anY = Draw::Atoi (theArgVec[2]);
4059   if (anX < 0 || anX >= aWidth || anY < 0 || anY > aHeight)
4060   {
4061     std::cerr << "Pixel coordinates (" << anX << "; " << anY << ") are out of view (" << aWidth << " x " << aHeight << ")\n";
4062     return 1;
4063   }
4064
4065   Standard_Boolean toShowName = Standard_False;
4066   Standard_Boolean toShowHls  = Standard_False;
4067   for (Standard_Integer anIter = 3; anIter < theArgNb; ++anIter)
4068   {
4069     const char* aParam = theArgVec[anIter];
4070     if ( strcasecmp( aParam, "rgb" ) == 0 )
4071     {
4072       aFormat     = Image_PixMap::IsBigEndianHost() ? Image_PixMap::ImgRGB : Image_PixMap::ImgBGR;
4073       aBufferType = Graphic3d_BT_RGB;
4074     }
4075     else if ( strcasecmp( aParam, "hls" ) == 0 )
4076     {
4077       aFormat     = Image_PixMap::IsBigEndianHost() ? Image_PixMap::ImgRGB : Image_PixMap::ImgBGR;
4078       aBufferType = Graphic3d_BT_RGB;
4079       toShowHls   = Standard_True;
4080     }
4081     else if ( strcasecmp( aParam, "rgbf" ) == 0 )
4082     {
4083       aFormat     = Image_PixMap::ImgRGBF;
4084       aBufferType = Graphic3d_BT_RGB;
4085     }
4086     else if ( strcasecmp( aParam, "rgba" ) == 0 )
4087     {
4088       aFormat     = Image_PixMap::IsBigEndianHost() ? Image_PixMap::ImgRGBA : Image_PixMap::ImgBGRA;
4089       aBufferType = Graphic3d_BT_RGBA;
4090     }
4091     else if ( strcasecmp( aParam, "rgbaf" ) == 0 )
4092     {
4093       aFormat     = Image_PixMap::ImgRGBAF;
4094       aBufferType = Graphic3d_BT_RGBA;
4095     }
4096     else if ( strcasecmp( aParam, "depth" ) == 0 )
4097     {
4098       aFormat     = Image_PixMap::ImgGrayF;
4099       aBufferType = Graphic3d_BT_Depth;
4100     }
4101     else if ( strcasecmp( aParam, "name" ) == 0 )
4102     {
4103       toShowName = Standard_True;
4104     }
4105   }
4106
4107   Image_PixMap anImage;
4108   if (!anImage.InitTrash (aFormat, aWidth, aHeight))
4109   {
4110     std::cerr << "Image allocation failed\n";
4111     return 1;
4112   }
4113   else if (!aView->ToPixMap (anImage, aWidth, aHeight, aBufferType))
4114   {
4115     std::cerr << "Image dump failed\n";
4116     return 1;
4117   }
4118
4119   Quantity_Parameter anAlpha;
4120   Quantity_Color aColor = anImage.PixelColor (anX, anY, anAlpha);
4121   if (toShowName)
4122   {
4123     if (aBufferType == Graphic3d_BT_RGBA)
4124     {
4125       theDI << Quantity_Color::StringName (aColor.Name()) << " " << anAlpha << "\n";
4126     }
4127     else
4128     {
4129       theDI << Quantity_Color::StringName (aColor.Name()) << "\n";
4130     }
4131   }
4132   else
4133   {
4134     switch (aBufferType)
4135     {
4136       default:
4137       case Graphic3d_BT_RGB:
4138       {
4139         if (toShowHls)
4140         {
4141           theDI << aColor.Hue() << " " << aColor.Light() << " " << aColor.Saturation() << "\n";
4142         }
4143         else
4144         {
4145           theDI << aColor.Red() << " " << aColor.Green() << " " << aColor.Blue() << "\n";
4146         }
4147         break;
4148       }
4149       case Graphic3d_BT_RGBA:
4150       {
4151         theDI << aColor.Red() << " " << aColor.Green() << " " << aColor.Blue() << " " << anAlpha << "\n";
4152         break;
4153       }
4154       case Graphic3d_BT_Depth:
4155       {
4156         theDI << aColor.Red() << "\n";
4157         break;
4158       }
4159     }
4160   }
4161
4162   return 0;
4163 }
4164
4165 //==============================================================================
4166 //function : VDiffImage
4167 //purpose  : The draw-command compares two images.
4168 //==============================================================================
4169
4170 static int VDiffImage (Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
4171 {
4172   if (theArgNb < 6)
4173   {
4174     theDI << "Not enough arguments.\n";
4175     return 1;
4176   }
4177
4178   // image file names
4179   const char* anImgPathRef = theArgVec[1];
4180   const char* anImgPathNew = theArgVec[2];
4181
4182   // get string tolerance and check its validity
4183   Standard_Real aTolColor = Draw::Atof (theArgVec[3]);
4184   if (aTolColor < 0.0)
4185     aTolColor = 0.0;
4186   if (aTolColor > 1.0)
4187     aTolColor = 1.0;
4188
4189   Standard_Boolean toBlackWhite     = (Draw::Atoi (theArgVec[4]) == 1);
4190   Standard_Boolean isBorderFilterOn = (Draw::Atoi (theArgVec[5]) == 1);
4191
4192   // image file of difference
4193   const char* aDiffImagePath = (theArgNb >= 7) ? theArgVec[6] : NULL;
4194
4195   // compare the images
4196   Image_Diff aComparer;
4197   if (!aComparer.Init (anImgPathRef, anImgPathNew, toBlackWhite))
4198   {
4199     return 1;
4200   }
4201
4202   aComparer.SetColorTolerance (aTolColor);
4203   aComparer.SetBorderFilterOn (isBorderFilterOn);
4204   Standard_Integer aDiffColorsNb = aComparer.Compare();
4205   theDI << aDiffColorsNb << "\n";
4206
4207   // save image of difference
4208   if (aDiffImagePath != NULL)
4209   {
4210     aComparer.SaveDiffImage (aDiffImagePath);
4211   }
4212
4213   return 0;
4214 }
4215
4216 //=======================================================================
4217 //function : VSelect
4218 //purpose  : Emulates different types of selection by mouse:
4219 //           1) single click selection
4220 //           2) selection with rectangle having corners at pixel positions (x1,y1) and (x2,y2)
4221 //           3) selection with polygon having corners at
4222 //           pixel positions (x1,y1),...,(xn,yn)
4223 //           4) any of these selections with shift button pressed
4224 //=======================================================================
4225 static Standard_Integer VSelect (Draw_Interpretor& di,
4226                                  Standard_Integer argc,
4227                                  const char ** argv)
4228 {
4229   if(argc < 3)
4230   {
4231     di << "Usage : " << argv[0] << " x1 y1 [x2 y2 [... xn yn]] [shift_selection = 1|0]" << "\n";
4232     return 1;
4233   }
4234
4235   Handle(AIS_InteractiveContext) myAIScontext = ViewerTest::GetAISContext();
4236   if(myAIScontext.IsNull())
4237   {
4238     di << "use 'vinit' command before " << argv[0] << "\n";
4239     return 1;
4240   }
4241   const Standard_Boolean isShiftSelection = (argc>3 && !(argc%2) && (atoi(argv[argc-1])==1));
4242   Handle(ViewerTest_EventManager) aCurrentEventManager = ViewerTest::CurrentEventManager();
4243   aCurrentEventManager->MoveTo(atoi(argv[1]),atoi(argv[2]));
4244   if(argc <= 4)
4245   {
4246     if(isShiftSelection)
4247       aCurrentEventManager->ShiftSelect();
4248     else
4249       aCurrentEventManager->Select();
4250   }
4251   else if(argc <= 6)
4252   {
4253     if(isShiftSelection)
4254       aCurrentEventManager->ShiftSelect(atoi(argv[1]),atoi(argv[2]),atoi(argv[3]),atoi(argv[4]));
4255     else
4256       aCurrentEventManager->Select(atoi(argv[1]),atoi(argv[2]),atoi(argv[3]),atoi(argv[4]));
4257   }
4258   else
4259   {
4260     Standard_Integer anUpper = 0;
4261
4262     if(isShiftSelection)
4263       anUpper = (argc-1)/2;
4264     else
4265       anUpper = argc/2;
4266     TColgp_Array1OfPnt2d aPolyline(1,anUpper);
4267
4268     for(Standard_Integer i=1;i<=anUpper;++i)
4269       aPolyline.SetValue(i,gp_Pnt2d(atoi(argv[2*i-1]),atoi(argv[2*i])));
4270
4271     if(isShiftSelection)
4272       aCurrentEventManager->ShiftSelect(aPolyline);
4273     else
4274       aCurrentEventManager->Select(aPolyline);
4275   }
4276   return 0;
4277 }
4278
4279 //=======================================================================
4280 //function : VMoveTo
4281 //purpose  : Emulates cursor movement to defined pixel position
4282 //=======================================================================
4283 static Standard_Integer VMoveTo (Draw_Interpretor& di,
4284                                 Standard_Integer argc,
4285                                 const char ** argv)
4286 {
4287   if(argc != 3)
4288   {
4289     di << "Usage : " << argv[0] << " x y" << "\n";
4290     return 1;
4291   }
4292
4293   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
4294   if(aContext.IsNull())
4295   {
4296     di << "use 'vinit' command before " << argv[0] << "\n";
4297     return 1;
4298   }
4299   ViewerTest::CurrentEventManager()->MoveTo(atoi(argv[1]),atoi(argv[2]));
4300   return 0;
4301 }
4302
4303 //=======================================================================
4304 //function : VViewParams
4305 //purpose  : Gets or sets AIS View characteristics
4306 //=======================================================================
4307 static Standard_Integer VViewParams (Draw_Interpretor& di,
4308                                 Standard_Integer argc,
4309                                 const char ** argv)
4310 {
4311   if ( argc != 1 && argc != 13)
4312   {
4313     di << "Usage : " << argv[0] << "\n";
4314     return 1;
4315   }
4316   Handle (V3d_View) anAISView = ViewerTest::CurrentView ();
4317   if ( anAISView.IsNull () )
4318   {
4319     di << "use 'vinit' command before " << argv[0] << "\n";
4320     return 1;
4321   }
4322   if(argc==1){
4323     Quantity_Factor anAISViewScale = anAISView -> V3d_View::Scale ();
4324     Standard_Real anAISViewCenterCoordinateX = 0.0;
4325     Standard_Real anAISViewCenterCoordinateY = 0.0;
4326     anAISView -> V3d_View::Center (anAISViewCenterCoordinateX, anAISViewCenterCoordinateY);
4327     Standard_Real anAISViewProjX = 0.0;
4328     Standard_Real anAISViewProjY = 0.0;
4329     Standard_Real anAISViewProjZ = 0.0;
4330     anAISView -> V3d_View::Proj (anAISViewProjX, anAISViewProjY, anAISViewProjZ);
4331     Standard_Real anAISViewUpX = 0.0;
4332     Standard_Real anAISViewUpY = 0.0;
4333     Standard_Real anAISViewUpZ = 0.0;
4334     anAISView -> V3d_View::Up (anAISViewUpX, anAISViewUpY, anAISViewUpZ);
4335     Standard_Real anAISViewAtX = 0.0;
4336     Standard_Real anAISViewAtY = 0.0;
4337     Standard_Real anAISViewAtZ = 0.0;
4338     anAISView -> V3d_View::At (anAISViewAtX, anAISViewAtY, anAISViewAtZ);
4339     di << "Scale of current view: " << anAISViewScale << "\n";
4340     di << "Center on X : "<< anAISViewCenterCoordinateX << "; on Y: " << anAISViewCenterCoordinateY << "\n";
4341     di << "Proj on X : " << anAISViewProjX << "; on Y: " << anAISViewProjY << "; on Z: " << anAISViewProjZ << "\n";
4342     di << "Up on X : " << anAISViewUpX << "; on Y: " << anAISViewUpY << "; on Z: " << anAISViewUpZ << "\n";
4343     di << "At on X : " << anAISViewAtX << "; on Y: " << anAISViewAtY << "; on Z: " << anAISViewAtZ << "\n";
4344   }
4345   else
4346   {
4347     Quantity_Factor anAISViewScale = atof (argv [1]);
4348     Standard_Real anAISViewCenterCoordinateX = atof (argv [2]);
4349     Standard_Real anAISViewCenterCoordinateY = atof (argv [3]);
4350     Standard_Real anAISViewProjX = atof (argv [4]);
4351     Standard_Real anAISViewProjY = atof (argv [5]);
4352     Standard_Real anAISViewProjZ = atof (argv [6]);
4353     Standard_Real anAISViewUpX = atof (argv [7]);
4354     Standard_Real anAISViewUpY = atof (argv [8]);
4355     Standard_Real anAISViewUpZ = atof (argv [9]);
4356     Standard_Real anAISViewAtX = atof (argv [10]);
4357     Standard_Real anAISViewAtY = atof (argv [11]);
4358     Standard_Real anAISViewAtZ = atof (argv [12]);
4359     anAISView -> V3d_View::SetScale (anAISViewScale);
4360     anAISView -> V3d_View::SetCenter (anAISViewCenterCoordinateX, anAISViewCenterCoordinateY);
4361     anAISView -> V3d_View::SetAt (anAISViewAtX, anAISViewAtY, anAISViewAtZ);
4362     anAISView -> V3d_View::SetProj (anAISViewProjX, anAISViewProjY, anAISViewProjZ);
4363     anAISView -> V3d_View::SetUp (anAISViewUpX, anAISViewUpY, anAISViewUpZ);
4364   }
4365   return 0;
4366 }
4367
4368 //=======================================================================
4369 //function : VChangeSelected
4370 //purpose  : Adds the shape to selection or remove one from it
4371 //=======================================================================
4372 static Standard_Integer VChangeSelected (Draw_Interpretor& di,
4373                                 Standard_Integer argc,
4374                                 const char ** argv)
4375 {
4376   if(argc != 2)
4377   {
4378     di<<"Usage : " << argv[0] << " shape \n";
4379     return 1;
4380   }
4381   //get AIS_Shape:
4382   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
4383   ViewerTest_DoubleMapOfInteractiveAndName& aMap = GetMapOfAIS();
4384   TCollection_AsciiString aName(argv[1]);
4385   Handle(AIS_InteractiveObject) anAISObject;
4386
4387   if(!aMap.IsBound2(aName))
4388   {
4389     di<<"Use 'vdisplay' before";
4390     return 1;
4391   }
4392   else
4393   {
4394     anAISObject = Handle(AIS_InteractiveObject)::DownCast(aMap.Find2(aName));
4395     if(anAISObject.IsNull()){
4396       di<<"No interactive object \n";
4397       return 1;
4398     }
4399
4400     if(aContext->HasOpenedContext())
4401     {
4402       aContext->AddOrRemoveSelected(anAISObject);
4403     }
4404     else
4405     {
4406       aContext->AddOrRemoveCurrentObject(anAISObject);
4407     }
4408   }
4409   return 0;
4410 }
4411
4412 //=======================================================================
4413 //function : VZClipping
4414 //purpose  : Gets or sets ZClipping mode, width and depth
4415 //=======================================================================
4416 static Standard_Integer VZClipping (Draw_Interpretor& di,
4417                                 Standard_Integer argc,
4418                                 const char ** argv)
4419 {
4420   if(argc>4)
4421   {
4422     di << "Usage : " << argv[0] << " [mode] [depth  width]" << "\n"
4423       <<"mode = OFF|BACK|FRONT|SLICE depth = [0..1] width = [0..1]" << "\n";
4424     return -1;
4425   }
4426   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
4427   if(aContext.IsNull())
4428   {
4429     di << "use 'vinit' command before " << argv[0] << "\n";
4430     return 1;
4431   }
4432   Handle(V3d_View) aView = ViewerTest::CurrentView();
4433   V3d_TypeOfZclipping aZClippingMode = V3d_OFF;
4434   if(argc==1)
4435   {
4436     TCollection_AsciiString aZClippingModeString;
4437     Quantity_Length aDepth, aWidth;
4438     aZClippingMode = aView->ZClipping(aDepth, aWidth);
4439     switch (aZClippingMode)
4440     {
4441     case V3d_OFF:
4442       aZClippingModeString.Copy("OFF");
4443       break;
4444     case V3d_BACK:
4445       aZClippingModeString.Copy("BACK");
4446       break;
4447     case V3d_FRONT:
4448       aZClippingModeString.Copy("FRONT");
4449       break;
4450     case V3d_SLICE:
4451       aZClippingModeString.Copy("SLICE");
4452       break;
4453     default:
4454       aZClippingModeString.Copy(TCollection_AsciiString(aZClippingMode));
4455       break;
4456     }
4457     di << "ZClippingMode = " << aZClippingModeString.ToCString() << "\n"
4458       << "ZClipping depth = " << aDepth << "\n"
4459       << "ZClipping width = " << aWidth << "\n";
4460   }
4461   else
4462   {
4463     if(argc !=3)
4464     {
4465       Standard_Integer aStatus = 0;
4466       if ( strcmp (argv [1], "OFF") == 0 ) {
4467         aStatus = 1;
4468         aZClippingMode = V3d_OFF;
4469       }
4470       if ( strcmp (argv [1], "BACK") == 0 ) {
4471         aStatus = 1;
4472         aZClippingMode = V3d_BACK;
4473       }
4474       if ( strcmp (argv [1], "FRONT") == 0 ) {
4475         aStatus = 1;
4476         aZClippingMode = V3d_FRONT;
4477       }
4478       if ( strcmp (argv [1], "SLICE") == 0 ) {
4479         aStatus = 1;
4480         aZClippingMode = V3d_SLICE;
4481       }
4482       if (aStatus != 1)
4483       {
4484         di << "Bad mode; Usage : " << argv[0] << " [mode] [depth width]" << "\n"
4485           << "mode = OFF|BACK|FRONT|SLICE depth = [0..1] width = [0..1]" << "\n";
4486         return 1;
4487       }
4488       aView->SetZClippingType(aZClippingMode);
4489     }
4490     if(argc >2)
4491     {
4492       Quantity_Length aDepth = 0., aWidth = 1.;
4493       if(argc == 3)
4494       {
4495         aDepth = atof(argv[1]);
4496         aWidth = atof(argv[2]);
4497       }
4498       else if(argc == 4)
4499       {
4500         aDepth = atof(argv[2]);
4501         aWidth = atof(argv[3]);
4502       }
4503
4504       if(aDepth<0. || aDepth>1.)
4505       {
4506         di << "Bad depth; Usage : " << argv[0] << " [mode] [depth width]" << "\n"
4507         << "mode = OFF|BACK|FRONT|SLICE depth = [0..1] width = [0..1]" << "\n";
4508         return 1;
4509       }
4510       if(aWidth<0. || aWidth>1.)
4511       {
4512         di << "Bad width; Usage : " << argv[0] << " [mode] [depth width]" << "\n"
4513         << "mode = OFF|BACK|FRONT|SLICE depth = [0..1] width = [0..1]" << "\n";
4514         return 1;
4515       }
4516
4517       aView->SetZClippingDepth(aDepth);
4518       aView->SetZClippingWidth(aWidth);
4519     }
4520     aView->Redraw();
4521   }
4522   return 0;
4523 }
4524
4525 //=======================================================================
4526 //function : VNbSelected
4527 //purpose  : Returns number of selected objects
4528 //=======================================================================
4529 static Standard_Integer VNbSelected (Draw_Interpretor& di,
4530                                 Standard_Integer argc,
4531                                 const char ** argv)
4532 {
4533   if(argc != 1)
4534   {
4535     di << "Usage : " << argv[0] << "\n";
4536     return 1;
4537   }
4538   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
4539   if(aContext.IsNull())
4540   {
4541     di << "use 'vinit' command before " << argv[0] << "\n";
4542     return 1;
4543   }
4544   di << aContext->NbSelected() << "\n";
4545   return 0;
4546 }
4547
4548 //=======================================================================
4549 //function : VAntialiasing
4550 //purpose  : Switches altialiasing on or off
4551 //=======================================================================
4552 static Standard_Integer VAntialiasing (Draw_Interpretor& di,
4553                                 Standard_Integer argc,
4554                                 const char ** argv)
4555 {
4556   if(argc > 2)
4557   {
4558     di << "Usage : " << argv[0] << " [1|0]" << "\n";
4559     return 1;
4560   }
4561
4562   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
4563   if(aContext.IsNull())
4564   {
4565     di << "use 'vinit' command before " << argv[0] << "\n";
4566     return 1;
4567   }
4568
4569   Handle(V3d_View) aView = ViewerTest::CurrentView();
4570
4571   if((argc == 2) && (atof(argv[1]) == 0))
4572     aView->SetAntialiasingOff();
4573   else
4574     aView->SetAntialiasingOn();
4575   aView->Update();
4576   return 0;
4577 }
4578
4579 //=======================================================================
4580 //function : VPurgeDisplay
4581 //purpose  : Switches altialiasing on or off
4582 //=======================================================================
4583 static Standard_Integer VPurgeDisplay (Draw_Interpretor& di,
4584                                 Standard_Integer argc,
4585                                 const char ** argv)
4586 {
4587   if (argc > 1)
4588   {
4589     di << "Usage : " << argv[0] << "\n";
4590     return 1;
4591   }
4592   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
4593   if (aContext.IsNull())
4594   {
4595     di << "use 'vinit' command before " << argv[0] << "\n";
4596     return 1;
4597   }
4598   aContext->CloseAllContexts(Standard_False);
4599   di << aContext->PurgeDisplay() << "\n";
4600   return 0;
4601 }
4602
4603 //=======================================================================
4604 //function : VSetViewSize
4605 //purpose  :
4606 //=======================================================================
4607 static Standard_Integer VSetViewSize (Draw_Interpretor& di,
4608                                 Standard_Integer argc,
4609                                 const char ** argv)
4610 {
4611   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
4612   if(aContext.IsNull())
4613   {
4614     di << "use 'vinit' command before " << argv[0] << "\n";
4615     return 1;
4616   }
4617   if(argc != 2)
4618   {
4619     di<<"Usage : " << argv[0] << " Size\n";
4620     return 1;
4621   }
4622   Standard_Real aSize = atof(argv[1]);
4623   if (aSize <= 0.)
4624   {
4625     di<<"Bad Size value  : " << aSize << "\n";
4626     return 1;
4627   }
4628
4629   Handle(V3d_View) aView = ViewerTest::CurrentView();
4630   aView->SetSize(aSize);
4631   return 0;
4632 }
4633
4634 //=======================================================================
4635 //function : VMoveView
4636 //purpose  :
4637 //=======================================================================
4638 static Standard_Integer VMoveView (Draw_Interpretor& di,
4639                                 Standard_Integer argc,
4640                                 const char ** argv)
4641 {
4642   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
4643   if(aContext.IsNull())
4644   {
4645     di << "use 'vinit' command before " << argv[0] << "\n";
4646     return 1;
4647   }
4648   if(argc < 4 || argc > 5)
4649   {
4650     di<<"Usage : " << argv[0] << " Dx Dy Dz [Start = 1|0]\n";
4651     return 1;
4652   }
4653   Standard_Real Dx = atof(argv[1]);
4654   Standard_Real Dy = atof(argv[2]);
4655   Standard_Real Dz = atof(argv[3]);
4656   Standard_Boolean aStart = Standard_True;
4657   if (argc == 5)
4658   {
4659       aStart = (atoi(argv[4]) > 0);
4660   }
4661
4662   Handle(V3d_View) aView = ViewerTest::CurrentView();
4663   aView->Move(Dx,Dy,Dz,aStart);
4664   return 0;
4665 }
4666
4667 //=======================================================================
4668 //function : VTranslateView
4669 //purpose  :
4670 //=======================================================================
4671 static Standard_Integer VTranslateView (Draw_Interpretor& di,
4672                                 Standard_Integer argc,
4673                                 const char ** argv)
4674 {
4675   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
4676   if(aContext.IsNull())
4677   {
4678     di << "use 'vinit' command before " << argv[0] << "\n";
4679     return 1;
4680   }
4681   if(argc < 4 || argc > 5)
4682   {
4683     di<<"Usage : " << argv[0] << " Dx Dy Dz [Start = 1|0]\n";
4684     return 1;
4685   }
4686   Standard_Real Dx = atof(argv[1]);
4687   Standard_Real Dy = atof(argv[2]);
4688   Standard_Real Dz = atof(argv[3]);
4689   Standard_Boolean aStart = Standard_True;
4690   if (argc == 5)
4691   {
4692       aStart = (atoi(argv[4]) > 0);
4693   }
4694
4695   Handle(V3d_View) aView = ViewerTest::CurrentView();
4696   aView->Translate(Dx,Dy,Dz,aStart);
4697   return 0;
4698 }
4699
4700 //=======================================================================
4701 //function : VTurnView
4702 //purpose  :
4703 //=======================================================================
4704 static Standard_Integer VTurnView (Draw_Interpretor& di,
4705                                 Standard_Integer argc,
4706                                 const char ** argv)
4707 {
4708   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
4709   if(aContext.IsNull()) {
4710     di << "use 'vinit' command before " << argv[0] << "\n";
4711     return 1;
4712   }
4713   if(argc < 4 || argc > 5){
4714     di<<"Usage : " << argv[0] << " Ax Ay Az [Start = 1|0]\n";
4715     return 1;
4716   }
4717   Standard_Real Ax = atof(argv[1]);
4718   Standard_Real Ay = atof(argv[2]);
4719   Standard_Real Az = atof(argv[3]);
4720   Standard_Boolean aStart = Standard_True;
4721   if (argc == 5)
4722   {
4723       aStart = (atoi(argv[4]) > 0);
4724   }
4725
4726   Handle(V3d_View) aView = ViewerTest::CurrentView();
4727   aView->Turn(Ax,Ay,Az,aStart);
4728   return 0;
4729 }
4730
4731 //==============================================================================
4732 //function : VTextureEnv
4733 //purpose  : ENables or disables environment mapping
4734 //==============================================================================
4735 class OCC_TextureEnv : public Graphic3d_TextureEnv
4736 {
4737 public:
4738   OCC_TextureEnv(const Standard_CString FileName);
4739   OCC_TextureEnv(const Graphic3d_NameOfTextureEnv aName);
4740   void SetTextureParameters(const Standard_Boolean theRepeatFlag,
4741                             const Standard_Boolean theModulateFlag,
4742                             const Graphic3d_TypeOfTextureFilter theFilter,
4743                             const Standard_ShortReal theXScale,
4744                             const Standard_ShortReal theYScale,
4745                             const Standard_ShortReal theXShift,
4746                             const Standard_ShortReal theYShift,
4747                             const Standard_ShortReal theAngle);
4748   DEFINE_STANDARD_RTTI(OCC_TextureEnv);
4749 };
4750 DEFINE_STANDARD_HANDLE(OCC_TextureEnv, Graphic3d_TextureEnv);
4751 IMPLEMENT_STANDARD_HANDLE(OCC_TextureEnv, Graphic3d_TextureEnv);
4752 IMPLEMENT_STANDARD_RTTIEXT(OCC_TextureEnv, Graphic3d_TextureEnv);
4753
4754 OCC_TextureEnv::OCC_TextureEnv(const Standard_CString theFileName)
4755   : Graphic3d_TextureEnv(theFileName)
4756 {
4757 }
4758
4759 OCC_TextureEnv::OCC_TextureEnv(const Graphic3d_NameOfTextureEnv theTexId)
4760   : Graphic3d_TextureEnv(theTexId)
4761 {
4762 }
4763
4764 void OCC_TextureEnv::SetTextureParameters(const Standard_Boolean theRepeatFlag,
4765                                           const Standard_Boolean theModulateFlag,
4766                                           const Graphic3d_TypeOfTextureFilter theFilter,
4767                                           const Standard_ShortReal theXScale,
4768                                           const Standard_ShortReal theYScale,
4769                                           const Standard_ShortReal theXShift,
4770                                           const Standard_ShortReal theYShift,
4771                                           const Standard_ShortReal theAngle)
4772 {
4773   myParams->SetRepeat     (theRepeatFlag);
4774   myParams->SetModulate   (theModulateFlag);
4775   myParams->SetFilter     (theFilter);
4776   myParams->SetScale      (Graphic3d_Vec2(theXScale, theYScale));
4777   myParams->SetTranslation(Graphic3d_Vec2(theXShift, theYShift));
4778   myParams->SetRotation   (theAngle);
4779 }
4780
4781 static int VTextureEnv (Draw_Interpretor& /*theDI*/, Standard_Integer theArgNb, const char** theArgVec)
4782 {
4783   // get the active view
4784   Handle(V3d_View) aView = ViewerTest::CurrentView();
4785   if (aView.IsNull())
4786   {
4787     std::cerr << "No active view. Please call vinit.\n";
4788     return 1;
4789   }
4790
4791   // Checking the input arguments
4792   Standard_Boolean anEnableFlag = Standard_False;
4793   Standard_Boolean isOk         = theArgNb >= 2;
4794   if (isOk)
4795   {
4796     TCollection_AsciiString anEnableOpt(theArgVec[1]);
4797     anEnableFlag = anEnableOpt.IsEqual("on");
4798     isOk         = anEnableFlag || anEnableOpt.IsEqual("off");
4799   }
4800   if (anEnableFlag)
4801   {
4802     isOk = (theArgNb == 3 || theArgNb == 11);
4803     if (isOk)
4804     {
4805       TCollection_AsciiString aTextureOpt(theArgVec[2]);
4806       isOk = (!aTextureOpt.IsIntegerValue() ||
4807              (aTextureOpt.IntegerValue() >= 0 && aTextureOpt.IntegerValue() < Graphic3d_NOT_ENV_UNKNOWN));
4808
4809       if (isOk && theArgNb == 11)
4810       {
4811         TCollection_AsciiString aRepeatOpt  (theArgVec[3]),
4812                                 aModulateOpt(theArgVec[4]),
4813                                 aFilterOpt  (theArgVec[5]),
4814                                 aSScaleOpt  (theArgVec[6]),
4815                                 aTScaleOpt  (theArgVec[7]),
4816                                 aSTransOpt  (theArgVec[8]),
4817                                 aTTransOpt  (theArgVec[9]),
4818                                 anAngleOpt  (theArgVec[10]);
4819         isOk = ((aRepeatOpt.  IsEqual("repeat")   || aRepeatOpt.  IsEqual("clamp")) &&
4820                 (aModulateOpt.IsEqual("modulate") || aModulateOpt.IsEqual("decal")) &&
4821                 (aFilterOpt.  IsEqual("nearest")  || aFilterOpt.  IsEqual("bilinear") || aFilterOpt.IsEqual("trilinear")) &&
4822                 aSScaleOpt.IsRealValue() && aTScaleOpt.IsRealValue() &&
4823                 aSTransOpt.IsRealValue() && aTTransOpt.IsRealValue() &&
4824                 anAngleOpt.IsRealValue());
4825       }
4826     }
4827   }
4828
4829   if (!isOk)
4830   {
4831     std::cerr << "Usage :" << std::endl;
4832     std::cerr << theArgVec[0] << " off" << std::endl;
4833     std::cerr << theArgVec[0] << " on {index_of_std_texture(0..7)|texture_file_name} [{clamp|repeat} {decal|modulate} {nearest|bilinear|trilinear} scale_s scale_t translation_s translation_t rotation_degrees]" << std::endl;
4834     return 1;
4835   }
4836
4837   if (anEnableFlag)
4838   {
4839     TCollection_AsciiString aTextureOpt(theArgVec[2]);
4840     Handle(OCC_TextureEnv) aTexEnv = aTextureOpt.IsIntegerValue() ?
4841                                      new OCC_TextureEnv((Graphic3d_NameOfTextureEnv)aTextureOpt.IntegerValue()) :
4842                                      new OCC_TextureEnv(theArgVec[2]);
4843
4844     if (theArgNb == 11)
4845     {
4846       TCollection_AsciiString aRepeatOpt(theArgVec[3]), aModulateOpt(theArgVec[4]), aFilterOpt(theArgVec[5]);
4847       aTexEnv->SetTextureParameters(
4848         aRepeatOpt.  IsEqual("repeat"),
4849         aModulateOpt.IsEqual("modulate"),
4850         aFilterOpt.  IsEqual("nearest") ? Graphic3d_TOTF_NEAREST :
4851                                           aFilterOpt.IsEqual("bilinear") ? Graphic3d_TOTF_BILINEAR :
4852                                                                            Graphic3d_TOTF_TRILINEAR,
4853         (Standard_ShortReal)Draw::Atof(theArgVec[6]),
4854         (Standard_ShortReal)Draw::Atof(theArgVec[7]),
4855         (Standard_ShortReal)Draw::Atof(theArgVec[8]),
4856         (Standard_ShortReal)Draw::Atof(theArgVec[9]),
4857         (Standard_ShortReal)Draw::Atof(theArgVec[10])
4858         );
4859     }
4860     aView->SetTextureEnv(aTexEnv);
4861     aView->SetSurfaceDetail(V3d_TEX_ENVIRONMENT);
4862   }
4863   else // Disabling environment mapping
4864   {
4865     aView->SetSurfaceDetail(V3d_TEX_NONE);
4866     Handle(Graphic3d_TextureEnv) aTexture;
4867     aView->SetTextureEnv(aTexture); // Passing null handle to clear the texture data
4868   }
4869
4870   aView->Redraw();
4871   return 0;
4872 }
4873
4874 //===============================================================================================
4875 //function : VClipPlane
4876 //purpose  :
4877 //===============================================================================================
4878 static int VClipPlane (Draw_Interpretor& theDi, Standard_Integer theArgsNb, const char** theArgVec)
4879 {
4880   // use short-cut for created clip planes map of created (or "registered by name") clip planes
4881   typedef NCollection_DataMap<TCollection_AsciiString, Handle(Graphic3d_ClipPlane)> MapOfPlanes;
4882   static MapOfPlanes aRegPlanes;
4883
4884   if (theArgsNb < 2)
4885   {
4886     theDi << theArgVec[0] << ": command argument is required. Type help for more information.\n";
4887     return 1;
4888   }
4889
4890   TCollection_AsciiString aCommand (theArgVec[1]);
4891
4892   // print maximum number of planes for current viewer
4893   if (aCommand == "maxplanes")
4894   {
4895     if (theArgsNb < 3)
4896     {
4897       theDi << theArgVec[0] << ": view name is required. Type help for more information.\n";
4898       return 1;
4899     }
4900
4901     TCollection_AsciiString aViewName (theArgVec[2]);
4902
4903     if (!ViewerTest_myViews.IsBound1 (aViewName))
4904     {
4905       theDi << theArgVec[0] << ": view is not found.\n";
4906       return 1;
4907     }
4908
4909     const Handle(V3d_View)& aView = ViewerTest_myViews.Find1 (aViewName);
4910
4911     theDi << theArgVec[0] << ": "
4912                           << aView->Viewer()->Driver()->InquirePlaneLimit()
4913                           << " plane slots provided by driver."
4914                           << " Note that 2 more planes might be used (reserved for z-clipping).\n";
4915
4916     return 0;
4917   }
4918
4919   // create / delete plane instance
4920   if (aCommand == "create" || aCommand == "delete" || aCommand == "clone")
4921   {
4922     if (theArgsNb < 3)
4923     {
4924       theDi << theArgVec[0] << ": plane name is required. Type help for more information.\n";
4925       return 1;
4926     }
4927
4928     Standard_Boolean toCreate = (aCommand == "create");
4929     Standard_Boolean toClone  = (aCommand == "clone");
4930     TCollection_AsciiString aPlane (theArgVec[2]);
4931
4932     if (toCreate)
4933     {
4934       if (aRegPlanes.IsBound (aPlane))
4935       {
4936         theDi << theArgVec[0] << ": plane name is in use.\n";
4937         return 1;
4938       }
4939
4940       aRegPlanes.Bind (aPlane, new Graphic3d_ClipPlane());
4941     }
4942     else if (toClone) // toClone
4943     {
4944       if (!aRegPlanes.IsBound (aPlane))
4945       {
4946         theDi << theArgVec[0] << ": no such plane.\n";
4947         return 1;
4948       }
4949
4950       if (theArgsNb < 4)
4951       {
4952         theDi << theArgVec[0] << ": enter name for new plane. Type help for more information.\n";
4953         return 1;
4954       }
4955
4956       TCollection_AsciiString aClone (theArgVec[3]);
4957       if (aRegPlanes.IsBound (aClone))
4958       {
4959         theDi << theArgVec[0] << ": plane name is in use.\n";
4960         return 1;
4961       }
4962
4963       const Handle(Graphic3d_ClipPlane)& aClipPlane = aRegPlanes.Find (aPlane);
4964
4965       aRegPlanes.Bind (aClone, aClipPlane->Clone());
4966     }
4967     else// toDelete
4968     {
4969       if (!aRegPlanes.IsBound (aPlane))
4970       {
4971         theDi << theArgVec[0] << ": no such plane.\n";
4972         return 1;
4973       }
4974
4975       Handle(Graphic3d_ClipPlane) aClipPlane = aRegPlanes.Find (aPlane);
4976       aRegPlanes.UnBind (aPlane);
4977
4978       ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName anIObjIt (GetMapOfAIS());
4979       for (; anIObjIt.More(); anIObjIt.Next())
4980       {
4981         Handle(PrsMgr_PresentableObject) aPrs = Handle(PrsMgr_PresentableObject)::DownCast (anIObjIt.Key1());
4982         aPrs->RemoveClipPlane(aClipPlane);
4983       }
4984
4985       NCollection_DoubleMap<TCollection_AsciiString, Handle(V3d_View)>::Iterator aViewIt(ViewerTest_myViews);
4986       for (; aViewIt.More(); aViewIt.Next())
4987       {
4988         const Handle(V3d_View)& aView = aViewIt.Key2();
4989         aView->RemoveClipPlane(aClipPlane);
4990       }
4991
4992       ViewerTest::RedrawAllViews();
4993     }
4994
4995     return 0;
4996   }
4997
4998   // set / unset plane command
4999   if (aCommand == "set" || aCommand == "unset")
5000   {
5001     if (theArgsNb < 4)
5002     {
5003       theDi << theArgVec[0] << ": need more arguments. Type help for more information.\n";
5004       return 1;
5005     }
5006
5007     Standard_Boolean toSet = (aCommand == "set");
5008     TCollection_AsciiString aPlane (theArgVec [2]);
5009     if (!aRegPlanes.IsBound (aPlane))
5010     {
5011       theDi << theArgVec[0] << ": no such plane.\n";
5012       return 1;
5013     }
5014
5015     const Handle(Graphic3d_ClipPlane)& aClipPlane = aRegPlanes.Find (aPlane);
5016
5017     TCollection_AsciiString aTarget (theArgVec [3]);
5018     if (aTarget != "object" && aTarget != "view")
5019     {
5020       theDi << theArgVec[0] << ": invalid target.\n";
5021       return 1;
5022     }
5023
5024     if (aTarget == "object" || aTarget == "view")
5025     {
5026       if (theArgsNb < 5)
5027       {
5028         theDi << theArgVec[0] << ": need more arguments. Type help for more information.\n";
5029         return 1;
5030       }
5031
5032       Standard_Boolean isObject = (aTarget == "object");
5033
5034       for (Standard_Integer anIt = 4; anIt < theArgsNb; ++anIt)
5035       {
5036         TCollection_AsciiString anEntityName (theArgVec[anIt]);
5037         if (isObject) // to object
5038         {
5039           if (!GetMapOfAIS().IsBound2 (anEntityName))
5040           {
5041             theDi << theArgVec[0] << ": can not find IO with name " << anEntityName << ".\n";
5042             continue;
5043           }
5044
5045           Handle(AIS_InteractiveObject) aIObj =
5046             Handle(AIS_InteractiveObject)::DownCast (GetMapOfAIS().Find2 (anEntityName));
5047
5048           if (toSet)
5049             aIObj->AddClipPlane (aClipPlane);
5050           else
5051             aIObj->RemoveClipPlane (aClipPlane);
5052         }
5053         else // to view
5054         {
5055           if (!ViewerTest_myViews.IsBound1 (anEntityName))
5056           {
5057             theDi << theArgVec[0] << ": can not find View with name " << anEntityName << ".\n";
5058             continue;
5059           }
5060
5061           Handle(V3d_View) aView = ViewerTest_myViews.Find1(anEntityName);
5062           if (toSet)
5063             aView->AddClipPlane (aClipPlane);
5064           else
5065             aView->RemoveClipPlane (aClipPlane);
5066         }
5067       }
5068
5069       ViewerTest::RedrawAllViews();
5070     }
5071
5072     return 0;
5073   }
5074
5075   // change plane command
5076   if (aCommand == "change")
5077   {
5078     if (theArgsNb < 4)
5079     {
5080       theDi << theArgVec[0] << ": need more arguments. Type help for more information.\n";
5081       return 1;
5082     }
5083
5084     TCollection_AsciiString aPlane (theArgVec [2]);
5085     if (!aRegPlanes.IsBound (aPlane))
5086     {
5087       theDi << theArgVec[0] << ": no such plane.\n";
5088       return 1;
5089     }
5090
5091     const Handle(Graphic3d_ClipPlane)& aClipPlane = aRegPlanes.Find (aPlane);
5092
5093     TCollection_AsciiString aChangeArg (theArgVec [3]);
5094     if (aChangeArg != "on" && aChangeArg != "off" && aChangeArg != "capping" && aChangeArg != "equation")
5095     {
5096       theDi << theArgVec[0] << ": invalid arguments. Type help for more information.\n";
5097       return 1;
5098     }
5099
5100     if (aChangeArg == "on" || aChangeArg == "off") // on / off
5101     {
5102       aClipPlane->SetOn (aChangeArg == "on");
5103     }
5104     else if (aChangeArg == "equation") // change equation
5105     {
5106       if (theArgsNb < 8)
5107       {
5108         theDi << theArgVec[0] << ": need more arguments. Type help for more information.\n";
5109         return 1;
5110       }
5111
5112       Standard_Real aCoeffA = atof (theArgVec [4]);
5113       Standard_Real aCoeffB = atof (theArgVec [5]);
5114       Standard_Real aCoeffC = atof (theArgVec [6]);
5115       Standard_Real aCoeffD = atof (theArgVec [7]);
5116       aClipPlane->SetEquation (gp_Pln (aCoeffA, aCoeffB, aCoeffC, aCoeffD));
5117     }
5118     else if (aChangeArg == "capping") // change capping aspects
5119     {
5120       if (theArgsNb < 5)
5121       {
5122         theDi << theArgVec[0] << ": need more arguments. Type help for more information.\n";
5123         return 1;
5124       }
5125
5126       TCollection_AsciiString aCappingArg (theArgVec [4]);
5127       if (aCappingArg != "on" && aCappingArg != "off" &&
5128           aCappingArg != "color" && aCappingArg != "texname" &&
5129           aCappingArg != "texscale" && aCappingArg != "texorigin" &&
5130           aCappingArg != "texrotate" && aCappingArg != "hatch")
5131       {
5132         theDi << theArgVec[0] << ": invalid arguments. Type help for more information.\n";
5133         return 1;
5134       }
5135
5136       if (aCappingArg == "on" || aCappingArg == "off") // on / off capping
5137       {
5138         aClipPlane->SetCapping (aCappingArg == "on");
5139       }
5140       else if (aCappingArg == "color") // color aspect for capping
5141       {
5142         if (theArgsNb < 8)
5143         {
5144           theDi << theArgVec[0] << ": need more arguments. Type help for more information.\n";
5145           return 1;
5146         }
5147
5148         Standard_Real aRed = atof (theArgVec [5]);
5149         Standard_Real aGrn = atof (theArgVec [6]);
5150         Standard_Real aBlu = atof (theArgVec [7]);
5151
5152         Graphic3d_MaterialAspect aMat = aClipPlane->CappingMaterial();
5153         aMat.SetColor (Quantity_Color (aRed, aGrn, aBlu, Quantity_TOC_RGB));
5154         aClipPlane->SetCappingMaterial (aMat);
5155       }
5156       else if (aCappingArg == "texname") // texture name
5157       {
5158         if (theArgsNb < 6)
5159         {
5160           theDi << theArgVec[0] << ": need more arguments. Type help for more information.\n";
5161           return 1;
5162         }
5163
5164         TCollection_AsciiString aTextureName (theArgVec [5]);
5165
5166         Handle(Graphic3d_Texture2Dmanual) aTexture = new Graphic3d_Texture2Dmanual(aTextureName);
5167         if (!aTexture->IsDone ())
5168         {
5169           aClipPlane->SetCappingTexture (NULL);
5170         }
5171         else
5172         {
5173           aTexture->EnableModulate();
5174           aTexture->EnableRepeat();
5175           aClipPlane->SetCappingTexture (aTexture);
5176         }
5177       }
5178       else if (aCappingArg == "texscale") // texture scale
5179       {
5180         if (aClipPlane->CappingTexture().IsNull())
5181         {
5182           theDi << theArgVec[0] << ": no texture is set.\n";
5183           return 1;
5184         }
5185
5186         if (theArgsNb < 7)
5187         {
5188           theDi << theArgVec[0] << ": need more arguments. Type help for more information.\n";
5189           return 1;
5190         }
5191
5192         Standard_ShortReal aSx = (Standard_ShortReal)atof (theArgVec [5]);
5193         Standard_ShortReal aSy = (Standard_ShortReal)atof (theArgVec [6]);
5194
5195         aClipPlane->CappingTexture()->GetParams()->SetScale (Graphic3d_Vec2 (aSx, aSy));
5196       }
5197       else if (aCappingArg == "texorigin") // texture origin
5198       {
5199         if (aClipPlane->CappingTexture().IsNull())
5200         {
5201           theDi << theArgVec[0] << ": no texture is set.\n";
5202           return 1;
5203         }
5204
5205         if (theArgsNb < 7)
5206         {
5207           theDi << theArgVec[0] << ": need more arguments. Type help for more information.\n";
5208           return 1;
5209         }
5210
5211         Standard_ShortReal aTx = (Standard_ShortReal)atof (theArgVec [5]);
5212         Standard_ShortReal aTy = (Standard_ShortReal)atof (theArgVec [6]);
5213
5214         aClipPlane->CappingTexture()->GetParams()->SetTranslation (Graphic3d_Vec2 (aTx, aTy));
5215       }
5216       else if (aCappingArg == "texrotate") // texture rotation
5217       {
5218         if (aClipPlane->CappingTexture().IsNull())
5219         {
5220           theDi << theArgVec[0] << ": no texture is set.\n";
5221           return 1;
5222         }
5223
5224         if (theArgsNb < 6)
5225         {
5226           theDi << theArgVec[0] << ": need more arguments. Type help for more information.\n";
5227           return 1;
5228         }
5229
5230         Standard_ShortReal aRot = (Standard_ShortReal)atof (theArgVec[5]);
5231
5232         aClipPlane->CappingTexture()->GetParams()->SetRotation (aRot);
5233       }
5234       else if (aCappingArg == "hatch") // hatch style
5235       {
5236         if (theArgsNb < 6)
5237         {
5238           theDi << theArgVec[0] << ": need more arguments. Type help for more information.\n";
5239           return 1;
5240         }
5241
5242         TCollection_AsciiString aHatchStr (theArgVec [5]);
5243         if (aHatchStr == "on")
5244         {
5245           aClipPlane->SetCappingHatchOn();
5246         }
5247         else if (aHatchStr == "off")
5248         {
5249           aClipPlane->SetCappingHatchOff();
5250         }
5251         else
5252         {
5253           aClipPlane->SetCappingHatch ((Aspect_HatchStyle)atoi (theArgVec[5]));
5254         }
5255       }
5256     }
5257
5258     ViewerTest::RedrawAllViews();
5259
5260     return 0;
5261   }
5262
5263   theDi << theArgVec[0] << ": invalid command. Type help for more information.\n";
5264   return 1;
5265 }
5266
5267 //===============================================================================================
5268 //function : VSetTextureMode
5269 //purpose  :
5270 //===============================================================================================
5271 static int VSetTextureMode (Draw_Interpretor& theDi, Standard_Integer theArgsNb, const char** theArgVec)
5272 {
5273   if (theArgsNb < 3)
5274   {
5275     theDi << theArgVec[0] << ": insufficient command arguments. Type help for more information.\n";
5276     return 1;
5277   }
5278
5279   TCollection_AsciiString aViewName (theArgVec[1]);
5280   if (!ViewerTest_myViews.IsBound1 (aViewName))
5281   {
5282     theDi << theArgVec[0] << ": view is not found.\n";
5283     return 1;
5284   }
5285
5286   const Handle(V3d_View)& aView = ViewerTest_myViews.Find1 (aViewName);
5287   switch (atoi (theArgVec[2]))
5288   {
5289     case 0: aView->SetSurfaceDetail (V3d_TEX_NONE); break;
5290     case 1: aView->SetSurfaceDetail (V3d_TEX_ENVIRONMENT); break;
5291     case 2: aView->SetSurfaceDetail (V3d_TEX_ALL); break;
5292     default:
5293       theDi << theArgVec[0] << ": invalid mode.\n";
5294       return 1;
5295   }
5296
5297   aView->Redraw();
5298   return 0;
5299 }
5300
5301 //===============================================================================================
5302 //function : VDefaults
5303 //purpose  :
5304 //===============================================================================================
5305 static int VDefaults (Draw_Interpretor& theDi,
5306                       Standard_Integer  theArgsNb,
5307                       const char**      theArgVec)
5308 {
5309   const Handle(AIS_InteractiveContext)& aCtx = ViewerTest::GetAISContext();
5310   if (aCtx.IsNull())
5311   {
5312     std::cerr << "No active viewer!\n";
5313     return 1;
5314   }
5315
5316   Handle(Prs3d_Drawer) aDefParams = aCtx->DefaultDrawer();
5317   if (theArgsNb < 2)
5318   {
5319     if (aDefParams->TypeOfDeflection() == Aspect_TOD_RELATIVE)
5320     {
5321       theDi << "DeflType:           relative\n"
5322             << "DeviationCoeff:     " << aDefParams->DeviationCoefficient() << "\n";
5323     }
5324     else
5325     {
5326       theDi << "DeflType:           absolute\n"
5327             << "AbsoluteDeflection: " << aDefParams->MaximalChordialDeviation() << "\n";
5328     }
5329     theDi << "AngularDeflection:  " << (180.0 * aDefParams->HLRAngle() / M_PI) << "\n";
5330     return 0;
5331   }
5332
5333   for (Standard_Integer anArgIter = 1; anArgIter < theArgsNb; ++anArgIter)
5334   {
5335     TCollection_AsciiString anArg (theArgVec[anArgIter]);
5336     TCollection_AsciiString aKey, aValue;
5337     if (!splitParameter (anArg, aKey, aValue)
5338      || aValue.IsEmpty())
5339     {
5340       std::cerr << "Error, wrong syntax at: '" << anArg.ToCString() << "'!\n";
5341       return 1;
5342     }
5343
5344     aKey.UpperCase();
5345     if (aKey == "ABSDEFL"
5346      || aKey == "ABSOLUTEDEFLECTION"
5347      || aKey == "DEFL"
5348      || aKey == "DEFLECTION")
5349     {
5350       aDefParams->SetTypeOfDeflection         (Aspect_TOD_ABSOLUTE);
5351       aDefParams->SetMaximalChordialDeviation (aValue.RealValue());
5352     }
5353     else if (aKey == "RELDEFL"
5354           || aKey == "RELATIVEDEFLECTION"
5355           || aKey == "DEVCOEFF"
5356           || aKey == "DEVIATIONCOEFF"
5357           || aKey == "DEVIATIONCOEFFICIENT")
5358     {
5359       aDefParams->SetTypeOfDeflection     (Aspect_TOD_RELATIVE);
5360       aDefParams->SetDeviationCoefficient (aValue.RealValue());
5361     }
5362     else if (aKey == "ANGDEFL"
5363           || aKey == "ANGULARDEFL"
5364           || aKey == "ANGULARDEFLECTION")
5365     {
5366       // currently HLRDeviationAngle is used instead of DeviationAngle in most places
5367       aDefParams->SetHLRAngle (M_PI * aValue.RealValue() / 180.0);
5368     }
5369     else
5370     {
5371       std::cerr << "Warning, unknown argument '" << anArg.ToCString() << "'\n";
5372     }
5373   }
5374
5375   return 0;
5376 }
5377
5378 //! Auxiliary method
5379 inline void addLight (const Handle(V3d_Light)& theLightNew,
5380                       const Standard_Boolean   theIsGlobal)
5381 {
5382   if (theLightNew.IsNull())
5383   {
5384     return;
5385   }
5386
5387   if (theIsGlobal)
5388   {
5389     ViewerTest::GetViewerFromContext()->SetLightOn (theLightNew);
5390   }
5391   else
5392   {
5393     ViewerTest::CurrentView()->SetLightOn (theLightNew);
5394   }
5395 }
5396
5397 //! Auxiliary method
5398 inline Standard_Integer getLightId (const TCollection_AsciiString& theArgNext)
5399 {
5400   TCollection_AsciiString anArgNextCase (theArgNext);
5401   anArgNextCase.UpperCase();
5402   if (anArgNextCase.Length() > 5
5403    && anArgNextCase.SubString (1, 5).IsEqual ("LIGHT"))
5404   {
5405     return theArgNext.SubString (6, theArgNext.Length()).IntegerValue();
5406   }
5407   else
5408   {
5409     return theArgNext.IntegerValue();
5410   }
5411 }
5412
5413 //===============================================================================================
5414 //function : VLight
5415 //purpose  :
5416 //===============================================================================================
5417 static int VLight (Draw_Interpretor& theDi,
5418                    Standard_Integer  theArgsNb,
5419                    const char**      theArgVec)
5420 {
5421   Handle(V3d_View)   aView   = ViewerTest::CurrentView();
5422   Handle(V3d_Viewer) aViewer = ViewerTest::GetViewerFromContext();
5423   if (aView.IsNull()
5424    || aViewer.IsNull())
5425   {
5426     std::cerr << "No active viewer!\n";
5427     return 1;
5428   }
5429
5430   Standard_Real        anXYZ[3];
5431   Quantity_Coefficient anAtten[2];
5432   if (theArgsNb < 2)
5433   {
5434     // print lights info
5435     Standard_Integer aLightId = 0;
5436     for (aView->InitActiveLights(); aView->MoreActiveLights(); aView->NextActiveLights(), ++aLightId)
5437     {
5438       Handle(V3d_Light) aLight = aView->ActiveLight();
5439       const Quantity_Color aColor = aLight->Color();
5440       theDi << "Light" << aLightId << "\n";
5441       switch (aLight->Type())
5442       {
5443         case V3d_AMBIENT:
5444         {
5445           theDi << "  Type:      Ambient\n";
5446           break;
5447         }
5448         case V3d_DIRECTIONAL:
5449         {
5450           Handle(V3d_DirectionalLight) aLightDir = Handle(V3d_DirectionalLight)::DownCast (aLight);
5451           theDi << "  Type:      Directional\n";
5452           theDi << "  Headlight: " << (aLight->Headlight() ? "TRUE" : "FALSE") << "\n";
5453           if (!aLightDir.IsNull())
5454           {
5455             aLightDir->Position  (anXYZ[0], anXYZ[1], anXYZ[2]);
5456             theDi << "  Position:  " << anXYZ[0] << ", " << anXYZ[1] << ", " << anXYZ[2] << "\n";
5457             aLightDir->Direction (anXYZ[0], anXYZ[1], anXYZ[2]);
5458             theDi << "  Direction: " << anXYZ[0] << ", " << anXYZ[1] << ", " << anXYZ[2] << "\n";
5459           }
5460           break;
5461         }
5462         case V3d_POSITIONAL:
5463         {
5464           Handle(V3d_PositionalLight) aLightPos = Handle(V3d_PositionalLight)::DownCast (aLight);
5465           theDi << "  Type:      Positional\n";
5466           theDi << "  Headlight: " << (aLight->Headlight() ? "TRUE" : "FALSE") << "\n";
5467           if (!aLightPos.IsNull())
5468           {
5469             aLightPos->Position  (anXYZ[0], anXYZ[1], anXYZ[2]);
5470             theDi << "  Position:  " << anXYZ[0] << ", " << anXYZ[1] << ", " << anXYZ[2] << "\n";
5471             aLightPos->Attenuation (anAtten[0], anAtten[1]);
5472             theDi << "  Atten.:    " << anAtten[0] << " " << anAtten[1] << "\n";
5473           }
5474           break;
5475         }
5476         case V3d_SPOT:
5477         {
5478           Handle(V3d_SpotLight) aLightSpot = Handle(V3d_SpotLight)::DownCast (aLight);
5479           theDi << "  Type:      Spot\n";
5480           theDi << "  Headlight: " << (aLight->Headlight() ? "TRUE" : "FALSE") << "\n";
5481           if (!aLightSpot.IsNull())
5482           {
5483             aLightSpot->Position  (anXYZ[0], anXYZ[1], anXYZ[2]);
5484             theDi << "  Position:  " << anXYZ[0] << ", " << anXYZ[1] << ", " << anXYZ[2] << "\n";
5485             aLightSpot->Direction (anXYZ[0], anXYZ[1], anXYZ[2]);
5486             theDi << "  Direction: " << anXYZ[0] << ", " << anXYZ[1] << ", " << anXYZ[2] << "\n";
5487             aLightSpot->Attenuation (anAtten[0], anAtten[1]);
5488             theDi << "  Atten.:    " << anAtten[0] << " " << anAtten[1] << "\n";
5489             theDi << "  Angle:     " << (aLightSpot->Angle() * 180.0 / M_PI) << "\n";
5490             theDi << "  Exponent:  " << aLightSpot->Concentration() << "\n";
5491           }
5492           break;
5493         }
5494         default:
5495         {
5496           theDi << "  Type:      UNKNOWN\n";
5497           break;
5498         }
5499       }
5500       theDi << "  Color:     " << aColor.Red() << ", " << aColor.Green() << ", " << aColor.Blue() << "\n";
5501     }
5502   }
5503
5504   Handle(V3d_Light) aLightNew;
5505   Handle(V3d_Light) aLightOld;
5506   Standard_Boolean  isGlobal = Standard_True;
5507   Standard_Boolean  toCreate = Standard_False;
5508   for (Standard_Integer anArgIt = 1; anArgIt < theArgsNb; ++anArgIt)
5509   {
5510     Handle(V3d_Light)            aLightCurr = aLightNew.IsNull() ? aLightOld : aLightNew;
5511     Handle(V3d_AmbientLight)     aLightAmb  = Handle(V3d_AmbientLight)    ::DownCast (aLightCurr);
5512     Handle(V3d_DirectionalLight) aLightDir  = Handle(V3d_DirectionalLight)::DownCast (aLightCurr);
5513     Handle(V3d_PositionalLight)  aLightPos  = Handle(V3d_PositionalLight) ::DownCast (aLightCurr);
5514     Handle(V3d_SpotLight)        aLightSpot = Handle(V3d_SpotLight)       ::DownCast (aLightCurr);
5515
5516     TCollection_AsciiString aName, aValue;
5517     const TCollection_AsciiString anArg (theArgVec[anArgIt]);
5518     TCollection_AsciiString anArgCase (anArg);
5519     anArgCase.UpperCase();
5520     if (anArgCase.IsEqual ("NEW")
5521      || anArgCase.IsEqual ("ADD")
5522      || anArgCase.IsEqual ("CREATE"))
5523     {
5524       toCreate = Standard_True;
5525     }
5526     else if (anArgCase.IsEqual ("GLOB")
5527           || anArgCase.IsEqual ("GLOBAL"))
5528     {
5529       isGlobal = Standard_True;
5530     }
5531     else if (anArgCase.IsEqual ("LOC")
5532           || anArgCase.IsEqual ("LOCAL"))
5533     {
5534       isGlobal = Standard_False;
5535     }
5536     else if (anArgCase.IsEqual ("AMB")
5537           || anArgCase.IsEqual ("AMBIENT")
5538           || anArgCase.IsEqual ("AMBLIGHT"))
5539     {
5540       addLight (aLightNew, isGlobal);
5541       if (!toCreate)
5542       {
5543         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5544         return 1;
5545       }
5546       toCreate  = Standard_False;
5547       aLightNew = new V3d_AmbientLight (aViewer);
5548     }
5549     else if (anArgCase.IsEqual ("DIRECTIONAL")
5550           || anArgCase.IsEqual ("DIRLIGHT"))
5551     {
5552       addLight (aLightNew, isGlobal);
5553       if (!toCreate)
5554       {
5555         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5556         return 1;
5557       }
5558       toCreate  = Standard_False;
5559       aLightNew = new V3d_DirectionalLight (aViewer);
5560     }
5561     else if (anArgCase.IsEqual ("SPOT")
5562           || anArgCase.IsEqual ("SPOTLIGHT"))
5563     {
5564       addLight (aLightNew, isGlobal);
5565       if (!toCreate)
5566       {
5567         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5568         return 1;
5569       }
5570       toCreate  = Standard_False;
5571       aLightNew = new V3d_SpotLight (aViewer, 0.0, 0.0, 0.0);
5572     }
5573     else if (anArgCase.IsEqual ("POSLIGHT")
5574           || anArgCase.IsEqual ("POSITIONAL"))
5575     {
5576       addLight (aLightNew, isGlobal);
5577       if (!toCreate)
5578       {
5579         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5580         return 1;
5581       }
5582       toCreate  = Standard_False;
5583       aLightNew = new V3d_PositionalLight (aViewer, 0.0, 0.0, 0.0);
5584     }
5585     else if (anArgCase.IsEqual ("CHANGE"))
5586     {
5587       addLight (aLightNew, isGlobal);
5588       aLightNew.Nullify();
5589       if (++anArgIt >= theArgsNb)
5590       {
5591         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5592         return 1;
5593       }
5594
5595       const Standard_Integer aLightId = getLightId (theArgVec[anArgIt]);
5596       Standard_Integer aLightIt = 0;
5597       for (aView->InitActiveLights(); aView->MoreActiveLights(); aView->NextActiveLights(), ++aLightIt)
5598       {
5599         if (aLightIt == aLightId)
5600         {
5601           aLightOld = aView->ActiveLight();
5602           break;
5603         }
5604       }
5605
5606       if (aLightOld.IsNull())
5607       {
5608         std::cerr << "Light " << theArgVec[anArgIt] << " is undefined!\n";
5609         return 1;
5610       }
5611     }
5612     else if (anArgCase.IsEqual ("DEL")
5613           || anArgCase.IsEqual ("DELETE"))
5614     {
5615       Handle(V3d_Light) aLightDel;
5616       if (++anArgIt >= theArgsNb)
5617       {
5618         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5619         return 1;
5620       }
5621
5622       const TCollection_AsciiString anArgNext (theArgVec[anArgIt]);
5623       const Standard_Integer aLightDelId = getLightId (theArgVec[anArgIt]);
5624       Standard_Integer aLightIt = 0;
5625       for (aView->InitActiveLights(); aView->MoreActiveLights(); aView->NextActiveLights(), ++aLightIt)
5626       {
5627         aLightDel = aView->ActiveLight();
5628         if (aLightIt == aLightDelId)
5629         {
5630           break;
5631         }
5632       }
5633       if (!aLightDel.IsNull())
5634       {
5635         aViewer->DelLight (aLightDel);
5636       }
5637     }
5638     else if (anArgCase.IsEqual ("COLOR")
5639           || anArgCase.IsEqual ("COLOUR"))
5640     {
5641       if (++anArgIt >= theArgsNb)
5642       {
5643         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5644         return 1;
5645       }
5646
5647       TCollection_AsciiString anArgNext (theArgVec[anArgIt]);
5648       anArgNext.UpperCase();
5649       const Quantity_Color aColor = ViewerTest::GetColorFromName (anArgNext.ToCString());
5650       if (!aLightCurr.IsNull())
5651       {
5652         aLightCurr->SetColor (aColor);
5653       }
5654     }
5655     else if (anArgCase.IsEqual ("POS")
5656           || anArgCase.IsEqual ("POSITION"))
5657     {
5658       if ((anArgIt + 3) >= theArgsNb)
5659       {
5660         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5661         return 1;
5662       }
5663
5664       anXYZ[0] = Atof (theArgVec[++anArgIt]);
5665       anXYZ[1] = Atof (theArgVec[++anArgIt]);
5666       anXYZ[2] = Atof (theArgVec[++anArgIt]);
5667       if (!aLightDir.IsNull())
5668       {
5669         aLightDir->SetPosition (anXYZ[0], anXYZ[1], anXYZ[2]);
5670       }
5671       else if (!aLightPos.IsNull())
5672       {
5673         aLightPos->SetPosition (anXYZ[0], anXYZ[1], anXYZ[2]);
5674       }
5675       else if (!aLightSpot.IsNull())
5676       {
5677         aLightSpot->SetPosition (anXYZ[0], anXYZ[1], anXYZ[2]);
5678       }
5679       else
5680       {
5681         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5682         return 1;
5683       }
5684     }
5685     else if (anArgCase.IsEqual ("DIR")
5686           || anArgCase.IsEqual ("DIRECTION"))
5687     {
5688       if ((anArgIt + 3) >= theArgsNb)
5689       {
5690         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5691         return 1;
5692       }
5693
5694       anXYZ[0] = Atof (theArgVec[++anArgIt]);
5695       anXYZ[1] = Atof (theArgVec[++anArgIt]);
5696       anXYZ[2] = Atof (theArgVec[++anArgIt]);
5697       if (!aLightDir.IsNull())
5698       {
5699         aLightDir->SetDirection (anXYZ[0], anXYZ[1], anXYZ[2]);
5700       }
5701       else if (!aLightSpot.IsNull())
5702       {
5703         aLightSpot->SetDirection (anXYZ[0], anXYZ[1], anXYZ[2]);
5704       }
5705       else
5706       {
5707         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5708         return 1;
5709       }
5710     }
5711     else if (anArgCase.IsEqual ("CONSTATTEN")
5712           || anArgCase.IsEqual ("CONSTATTENUATION"))
5713     {
5714       if (++anArgIt >= theArgsNb)
5715       {
5716         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5717         return 1;
5718       }
5719
5720       if (!aLightPos.IsNull())
5721       {
5722         aLightPos->Attenuation (anAtten[0], anAtten[1]);
5723         anAtten[0] = Atof (theArgVec[anArgIt]);
5724         aLightPos->SetAttenuation (anAtten[0], anAtten[1]);
5725       }
5726       else if (!aLightSpot.IsNull())
5727       {
5728         aLightSpot->Attenuation (anAtten[0], anAtten[1]);
5729         anAtten[0] = Atof (theArgVec[anArgIt]);
5730         aLightSpot->SetAttenuation (anAtten[0], anAtten[1]);
5731       }
5732       else
5733       {
5734         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5735         return 1;
5736       }
5737     }
5738     else if (anArgCase.IsEqual ("LINATTEN")
5739           || anArgCase.IsEqual ("LINEARATTEN")
5740           || anArgCase.IsEqual ("LINEARATTENUATION"))
5741     {
5742       if (++anArgIt >= theArgsNb)
5743       {
5744         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5745         return 1;
5746       }
5747
5748       if (!aLightPos.IsNull())
5749       {
5750         aLightPos->Attenuation (anAtten[0], anAtten[1]);
5751         anAtten[1] = Atof (theArgVec[anArgIt]);
5752         aLightPos->SetAttenuation (anAtten[0], anAtten[1]);
5753       }
5754       else if (!aLightSpot.IsNull())
5755       {
5756         aLightSpot->Attenuation (anAtten[0], anAtten[1]);
5757         anAtten[1] = Atof (theArgVec[anArgIt]);
5758         aLightSpot->SetAttenuation (anAtten[0], anAtten[1]);
5759       }
5760       else
5761       {
5762         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5763         return 1;
5764       }
5765     }
5766     else if (anArgCase.IsEqual ("EXP")
5767           || anArgCase.IsEqual ("EXPONENT")
5768           || anArgCase.IsEqual ("SPOTEXP")
5769           || anArgCase.IsEqual ("SPOTEXPONENT"))
5770     {
5771       if (++anArgIt >= theArgsNb)
5772       {
5773         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5774         return 1;
5775       }
5776
5777       if (!aLightSpot.IsNull())
5778       {
5779         aLightSpot->SetConcentration (Atof (theArgVec[anArgIt]));
5780       }
5781       else
5782       {
5783         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5784         return 1;
5785       }
5786     }
5787     else if (anArgCase.IsEqual ("HEAD")
5788           || anArgCase.IsEqual ("HEADLIGHT"))
5789     {
5790       if (++anArgIt >= theArgsNb)
5791       {
5792         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5793         return 1;
5794       }
5795
5796       if (aLightAmb.IsNull()
5797        && !aLightCurr.IsNull())
5798       {
5799         aLightCurr->SetHeadlight (Draw::Atoi (theArgVec[anArgIt]) != 0);
5800       }
5801       else
5802       {
5803         std::cerr << "Wrong syntax at argument '" << anArg << "'!\n";
5804         return 1;
5805       }
5806     }
5807     else
5808     {
5809       std::cerr << "Warning: unknown argument '" << anArg << "'\n";
5810     }
5811   }
5812
5813   addLight (aLightNew, isGlobal);
5814   aViewer->UpdateLights();
5815
5816   return 0;
5817 }
5818
5819
5820
5821 //==============================================================================
5822 //function : VClInfo
5823 //purpose  : Prints info about active OpenCL device
5824 //==============================================================================
5825
5826 static Standard_Integer VClInfo (Draw_Interpretor& theDi,
5827                                  Standard_Integer,
5828                                  const char**)
5829 {
5830   Handle(AIS_InteractiveContext) aContextAIS = ViewerTest::GetAISContext();
5831   if (aContextAIS.IsNull())
5832   {
5833     std::cerr << "Call vinit before!\n";
5834     return 1;
5835   }
5836
5837   Handle(OpenGl_GraphicDriver) aDrv = Handle(OpenGl_GraphicDriver)::DownCast (aContextAIS->CurrentViewer()->Driver());
5838   Graphic3d_CView* aCView = static_cast<Graphic3d_CView*> (ViewerTest::CurrentView()->View()->CView());
5839   NCollection_DataMap<TCollection_AsciiString, TCollection_AsciiString> anInfo;
5840   if (aDrv.IsNull()
5841    || aCView == NULL
5842    || !aDrv->GetOpenClDeviceInfo (*aCView, anInfo))
5843   {
5844     theDi << "OpenCL device info is unavailable!\n";
5845     return 0;
5846   }
5847
5848   theDi << "OpenCL device info:\n";
5849   for (NCollection_DataMap<TCollection_AsciiString, TCollection_AsciiString>::Iterator anIter (anInfo);
5850        anIter.More(); anIter.Next())
5851   {
5852     theDi << anIter.Key() << ": \t" << anIter.Value() << "\n";
5853   }
5854   return 0;
5855 }
5856
5857 //=======================================================================
5858 //function : VRaytrace
5859 //purpose  : Enables/disables OpenCL-based ray-tracing
5860 //=======================================================================
5861
5862 static Standard_Integer VRaytrace (Draw_Interpretor& ,
5863                                    Standard_Integer  theArgNb,
5864                                    const char**      theArgVec)
5865 {
5866   Handle(V3d_View) aView = ViewerTest::CurrentView();
5867   if (aView.IsNull())
5868   {
5869     std::cerr << "Use 'vinit' command before " << theArgVec[0] << "\n";
5870     return 1;
5871   }
5872
5873   if (theArgNb < 2
5874    || Draw::Atoi (theArgVec[1]))
5875   {
5876     aView->SetRaytracingMode();
5877   }
5878   else
5879   {
5880     aView->SetRasterizationMode();
5881   }
5882   aView->Redraw();
5883   return 0;
5884 }
5885
5886 //=======================================================================
5887 //function : VSetRaytraceMode
5888 //purpose  : Enables/disables features of OpenCL-based ray-tracing
5889 //=======================================================================
5890
5891 static Standard_Integer VSetRaytraceMode (Draw_Interpretor&,
5892                                           Standard_Integer theArgNb,
5893                                           const char ** theArgVec)
5894 {
5895   Handle(V3d_View) aView = ViewerTest::CurrentView();
5896   if (aView.IsNull())
5897   {
5898     std::cerr << "Use 'vinit' command before " << theArgVec[0] << "\n";
5899     return 1;
5900   }
5901   else if (theArgNb < 2)
5902   {
5903     std::cerr << "Usage : " << theArgVec[0] << " [shad=0|1] [refl=0|1] [aa=0|1]\n";
5904     return 1;
5905   }
5906
5907   for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
5908   {
5909     const TCollection_AsciiString anArg (theArgVec[anArgIter]);
5910
5911     if (anArg.Search ("shad=") > -1)
5912     {
5913       if (anArg.Token ("=", 2).IntegerValue() != 0)
5914         aView->EnableRaytracedShadows();
5915       else
5916         aView->DisableRaytracedShadows();
5917     }
5918     else if (anArg.Search ("refl=") > -1)
5919     {
5920       if (anArg.Token ("=", 2).IntegerValue() != 0)
5921         aView->EnableRaytracedReflections();
5922       else
5923         aView->DisableRaytracedReflections();
5924     }
5925     else if (anArg.Search ("aa=") > -1)
5926     {
5927       if (anArg.Token ("=", 2).IntegerValue() != 0)
5928         aView->EnableRaytracedAntialiasing();
5929       else
5930         aView->DisableRaytracedAntialiasing();
5931     }
5932     else
5933     {
5934       std::cerr << "Unknown argument: " << anArg << "\n";
5935     }
5936   }
5937
5938   aView->Redraw();
5939   return 0;
5940 }
5941
5942 //=======================================================================
5943 //function : ViewerCommands
5944 //purpose  :
5945 //=======================================================================
5946
5947 void ViewerTest::ViewerCommands(Draw_Interpretor& theCommands)
5948 {
5949
5950   const char *group = "ZeViewer";
5951   theCommands.Add("vinit",
5952 #if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
5953     "[name=view_name] [display=display_name] [l=leftPx t=topPx] [w=widthPx h=heightPx]\n"
5954 #else
5955     "[name=view_name] [l=leftPx t=topPx] [w=widthPx h=heightPx]\n"
5956 #endif
5957     " - Creates new View window with specified name view_name.\n"
5958     "By default the new view is created in the viewer and in"
5959     " graphic driver shared with active view.\n"
5960     " - name = {driverName/viewerName/viewName | viewerName/viewName | viewName}.\n"
5961     "If driverName isn't specified the driver will be shared with active view.\n"
5962     "If viewerName isn't specified the viewer will be shared with active view.\n"
5963 #if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
5964     " - display = HostName.DisplayNumber[:ScreenNumber] : if specified"
5965     "is used in creation of graphic driver\n"
5966 #endif
5967     " - l, t: pixel position of left top corner of the window\n"
5968     " - w,h: width and heigth of window respectively.\n"
5969     "Additional commands for operations with views: vclose, vactivate, vviewlist.\n",
5970     __FILE__,VInit,group);
5971   theCommands.Add("vclose" ,
5972     "[view_id [keep_context=0|1]]\n"
5973     "or vclose ALL - to remove all created views\n"
5974     " - removes view(viewer window) defined by its view_id.\n"
5975     " - keep_context: by default 0; if 1 and the last view is deleted"
5976     " the current context is not removed.",
5977     __FILE__,VClose,group);
5978   theCommands.Add("vactivate" ,
5979     "view_id"
5980     " - activates view(viewer window) defined by its view_id",
5981     __FILE__,VActivate,group);
5982   theCommands.Add("vviewlist",
5983     "vviewlist [format={tree, long}]"
5984     " - prints current list of views per viewer and graphic_driver ID shared between viewers"
5985     " - format: format of result output, if tree the output is a tree view;"
5986     "otherwise it's a list of full view names. By default format = tree",
5987     __FILE__,VViewList,group);
5988   theCommands.Add("vhelp" ,
5989     "vhelp            : display help on the viewer commands",
5990     __FILE__,VHelp,group);
5991   theCommands.Add("vtop" ,
5992     "vtop or <T>      : Top view" ,
5993     __FILE__,VTop,group);
5994   theCommands.Add("vbottom" ,
5995     "vbottom          : Bottom view" ,
5996     __FILE__,VBottom,group);
5997   theCommands.Add("vleft" ,
5998     "vleft            : Left view" ,
5999     __FILE__,VLeft,group);
6000   theCommands.Add("vright" ,
6001     "vright           : Right view" ,
6002     __FILE__,VRight,group);
6003   theCommands.Add("vaxo" ,
6004     " vaxo or <A>     : Axonometric view ",
6005     __FILE__,VAxo,group);
6006   theCommands.Add("vfront" ,
6007     "vfront           : Front view" ,
6008     __FILE__,VFront,group);
6009   theCommands.Add("vback" ,
6010     "vback            : Back view" ,
6011     __FILE__,VBack,group);
6012   theCommands.Add("vpick" ,
6013     "vpick           : vpick X Y Z [shape subshape] ( all variables as string )",
6014     VPick,group);
6015   theCommands.Add("vfit"    ,
6016     "vfit or <F>         : vfit",
6017     __FILE__,VFit,group);
6018   theCommands.Add("vzfit"    ,
6019     "vzfit",
6020     __FILE__,VZFit,group);
6021   theCommands.Add("vrepaint",
6022     "vrepaint        : vrepaint, force redraw",
6023     __FILE__,VRepaint,group);
6024   theCommands.Add("vclear",
6025     "vclear          : vclear",
6026     __FILE__,VClear,group);
6027   theCommands.Add("vsetbg",
6028     "vsetbg          : vsetbg imagefile [filltype] : Load image as background",
6029     __FILE__,VSetBg,group);
6030   theCommands.Add("vsetbgmode",
6031     "vsetbgmode      : vsetbgmode filltype : Change background image fill type",
6032     __FILE__,VSetBgMode,group);
6033   theCommands.Add("vsetgradientbg",
6034     "vsetgradientbg  : vsetgradientbg r1 g1 b1 r2 g2 b2 filltype : Mount gradient background",
6035     __FILE__,VSetGradientBg,group);
6036   theCommands.Add("vsetgrbgmode",
6037     "vsetgrbgmode    : vsetgrbgmode filltype : Change gradient background fill type",
6038     __FILE__,VSetGradientBgMode,group);
6039   theCommands.Add("vsetcolorbg",
6040     "vsetcolorbg     : vsetcolorbg r g b : Set background color",
6041     __FILE__,VSetColorBg,group);
6042   theCommands.Add("vscale",
6043     "vscale          : vscale X Y Z",
6044     __FILE__,VScale,group);
6045   theCommands.Add("vzbufftrihedron",
6046     "vzbufftrihedron [center|left_lower|left_upper|right_lower|right_upper"
6047     " textR=255 textG=255 textB=255 scale=0.1 wireframe|zbuffer]"
6048     " : Displays a V3d_ZBUFFER'ed or V3d_WIREFRAME'd trihedron",
6049     __FILE__,VTestZBuffTrihedron,group);
6050   theCommands.Add("vrotate",
6051     "vrotate         : vrotate AX AY AZ [X Y Z]",
6052     __FILE__,VRotate,group);
6053   theCommands.Add("vzoom",
6054     "vzoom           : vzoom coef",
6055     __FILE__,VZoom,group);
6056   theCommands.Add("vpan",
6057     "vpan            : vpan dx dy",
6058     __FILE__,VPan,group);
6059   theCommands.Add("vexport",
6060     "vexport         : vexport full_file_path {PS | EPS | TEX | PDF | SVG | PGF | EMF }"
6061     " : exports the view to a vector file of a given format"
6062     " : notice that EMF format requires patched gl2ps",
6063     __FILE__,VExport,group);
6064   theCommands.Add("vcolorscale",
6065     "vcolorscale     : vcolorscale [RangeMin = 0 RangeMax = 100 Intervals = 10 HeightFont = 16 Position = 2 X = 0 Y = 0]: draw color scale",
6066     __FILE__,VColorScale,group);
6067   theCommands.Add("vgraduatedtrihedron",
6068     "vgraduatedtrihedron : 1/0 (display/erase) [Xname Yname Zname [Font [isMultibyte]]]",
6069     __FILE__,VGraduatedTrihedron,group);
6070   theCommands.Add("vprintview" ,
6071     "vprintview : width height filename [algo=0] : Test print algorithm: algo = 0 - stretch, algo = 1 - tile",
6072     __FILE__,VPrintView,group);
6073   theCommands.Add("vzlayer",
6074     "vzlayer : add/del/get [id] : Z layer operations in v3d viewer: add new z layer, delete z layer, get z layer ids",
6075     __FILE__,VZLayer,group);
6076   theCommands.Add("voverlaytext",
6077     "voverlaytext : text x y [height] [font_name] [text_color: R G B] [display_type] [background_color: R G B]"
6078     " : height - pixel height of the text (default=10.0)"
6079     " : font_name - name of font (default=courier)"
6080     " : text_color - three values: RedColor GreenColor BlueColor (default = 255.0 255.0 255.0) "
6081     " : display_type = {normal/subtitle/decal/blend}, (default=normal) "
6082     " : background_color - three values: RedColor GreenColor BlueColor (default = 255.0 255.0 255.0), the parameter is defined for subtitle and decal display types ",
6083     __FILE__,VOverlayText,group);
6084   theCommands.Add("vlayerline",
6085     "vlayerline : vlayerline x1 y1 x2 y2 [linewidth=0.5] [linetype=0] [transparency=1.0]",
6086     __FILE__,VLayerLine,group);
6087   theCommands.Add ("vgrid",
6088     "vgrid [off] [Mode={r|c}] [Type={l|p}] [OriginX OriginY [StepX/StepRadius StepY/DivNb RotAngle]]"
6089     " : Mode - rectangular or circular"
6090     " : Type - lines or points",
6091     __FILE__, VGrid, group);
6092   theCommands.Add ("vfps",
6093     "vfps [framesNb=100] : estimate average frame rate for active view",
6094     __FILE__, VFps, group);
6095   theCommands.Add ("vgldebug",
6096     "vgldebug [{0|1}] : request debug GL context, should be called before vinit\n"
6097     "                : this function is implemented only for Windows\n"
6098     "                : GL_ARB_debug_output extension should be exported by OpenGL driver!",
6099     __FILE__, VGlDebug, group);
6100   theCommands.Add ("vvbo",
6101     "vvbo [{0|1}] : turn VBO usage On/Off; affects only newly displayed objects",
6102     __FILE__, VVbo, group);
6103   theCommands.Add ("vcaps",
6104     "vcaps [vbo={0|1}] [sprites={0|1}] [soft={0|1}] : modify particular graphic driver options",
6105     __FILE__, VCaps, group);
6106   theCommands.Add ("vmemgpu",
6107     "vmemgpu [f]: print system-dependent GPU memory information if available;"
6108     " with f option returns free memory in bytes",
6109     __FILE__, VMemGpu, group);
6110   theCommands.Add ("vreadpixel",
6111     "vreadpixel xPixel yPixel [{rgb|rgba|depth|hls|rgbf|rgbaf}=rgba] [name]"
6112     " : Read pixel value for active view",
6113     __FILE__, VReadPixel, group);
6114   theCommands.Add("diffimage",
6115     "diffimage     : diffimage imageFile1 imageFile2 toleranceOfColor(0..1) blackWhite(1|0) borderFilter(1|0) [diffImageFile]",
6116     __FILE__, VDiffImage, group);
6117   theCommands.Add ("vselect",
6118     "vselect x1 y1 [x2 y2 [x3 y3 ... xn yn]] [shift_selection = 0|1]\n"
6119     "- emulates different types of selection:\n"
6120     "- 1) single click selection\n"
6121     "- 2) selection with rectangle having corners at pixel positions (x1,y1) and (x2,y2)\n"
6122     "- 3) selection with polygon having corners in pixel positions (x1,y1), (x2,y2),...,(xn,yn)\n"
6123     "- 4) any of these selections with shift button pressed",
6124     __FILE__, VSelect, group);
6125   theCommands.Add ("vmoveto",
6126     "vmoveto x y"
6127     "- emulates cursor movement to pixel postion (x,y)",
6128     __FILE__, VMoveTo, group);
6129   theCommands.Add("vviewparams",
6130     "vviewparams [scale center_X center_Y proj_X proj_Y proj_Z up_X up_Y up_Z at_X at_Y at_Z]"
6131     "- gets or sets current view characteristics",
6132     __FILE__,VViewParams, group);
6133   theCommands.Add("vchangeselected",
6134     "vchangeselected shape"
6135     "- adds to shape to selection or remove one from it",
6136                 __FILE__, VChangeSelected, group);
6137   theCommands.Add("vzclipping",
6138     "vzclipping [mode] [depth width]\n"
6139     "- mode = OFF|BACK|FRONT|SLICE depth = [0..1] width = [0..1]\n"
6140     "- gets or sets ZClipping mode, width and depth",
6141     __FILE__,VZClipping,group);
6142   theCommands.Add ("vnbselected",
6143     "vnbselected", __FILE__, VNbSelected, group);
6144   theCommands.Add("vantialiasing",
6145     "vantialiasing 1|0",
6146     __FILE__,VAntialiasing,group);
6147   theCommands.Add ("vpurgedisplay",
6148     "vpurgedisplay"
6149     "- removes structures which don't belong to objects displayed in neutral point",
6150     __FILE__, VPurgeDisplay, group);
6151   theCommands.Add("vsetviewsize",
6152     "vsetviewsize size",
6153     __FILE__,VSetViewSize,group);
6154   theCommands.Add("vmoveview",
6155     "vmoveview Dx Dy Dz [Start = 1|0]",
6156     __FILE__,VMoveView,group);
6157   theCommands.Add("vtranslateview",
6158     "vtranslateview Dx Dy Dz [Start = 1|0)]",
6159     __FILE__,VTranslateView,group);
6160   theCommands.Add("vturnview",
6161     "vturnview Ax Ay Az [Start = 1|0]",
6162     __FILE__,VTurnView,group);
6163   theCommands.Add("vtextureenv",
6164     "Enables or disables environment mapping in the 3D view, loading the texture from the given standard "
6165     "or user-defined file and optionally applying texture mapping parameters\n"
6166     "                  Usage:\n"
6167     "                  vtextureenv off - disables environment mapping\n"
6168     "                  vtextureenv on {std_texture|texture_file_name} [rep mod flt ss st ts tt rot] - enables environment mapping\n"
6169     "                              std_texture = (0..7)\n"
6170     "                              rep         = {clamp|repeat}\n"
6171     "                              mod         = {decal|modulate}\n"
6172     "                              flt         = {nearest|bilinear|trilinear}\n"
6173     "                              ss, st      - scale factors for s and t texture coordinates\n"
6174     "                              ts, tt      - translation for s and t texture coordinates\n"
6175     "                              rot         - texture rotation angle in degrees",
6176     __FILE__, VTextureEnv, group);
6177   theCommands.Add("vhlr" ,
6178     "is_enabled={on|off}"
6179     " - Hidden line removal algorithm:"
6180     " - is_enabled: if is on HLR algorithm is applied\n",
6181     __FILE__,VHLR,group);
6182   theCommands.Add("vhlrtype" ,
6183     "algo_type={algo|polyalgo} [shape_1 ... shape_n]"
6184     " - Changes the type of HLR algorithm using for shapes."
6185     " - algo_type: if equals to algo, exact HLR algorithm is applied;\n"
6186     "   if equals to polyalgo, polygonal HLR algorithm is applied."
6187     "If shapes are not given HLR algoithm of given type is applied"
6188     " to all shapes in the view\n",
6189     __FILE__,VHLRType,group);
6190   theCommands.Add("vclipplane", "vclipplane usage: \n"
6191     "  maxplanes <view_name> - get plane limit for view.\n"
6192     "  create <plane_name> - create new plane.\n"
6193     "  delete <plane_name> - delete plane.\n"
6194     "  clone <source_plane> <plane_name> - clone the plane definition.\n"
6195     "  set/unset <plane_name> object <object list> - set/unset plane for IO.\n"
6196     "  set/unset <plane_name> view <view list> - set/unset plane for view.\n"
6197     "  change <plane_name> on/off - turn clipping on/off.\n"
6198     "  change <plane_name> equation <a> <b> <c> <d> - change plane equation.\n"
6199     "  change <plane_name> capping on/off - turn capping on/off.\n"
6200     "  change <plane_name> capping color <r> <g> <b> - set color.\n"
6201     "  change <plane name> capping texname <texture> - set texture.\n"
6202     "  change <plane_name> capping texscale <sx> <sy> - set tex scale.\n"
6203     "  change <plane_name> capping texorigin <tx> <ty> - set tex origin.\n"
6204     "  change <plane_name> capping texrotate <angle> - set tex rotation.\n"
6205     "  change <plane_name> capping hatch on/off/<id> - set hatching mask.\n"
6206     "  please use VSetTextureMode command to enable texture rendering in view.\n"
6207     , __FILE__, VClipPlane, group);
6208   theCommands.Add("vsettexturemode", "vsettexturemode view_name mode \n"
6209     "  mode can be:\n"
6210     "  0 - no textures enabled in view.\n"
6211     "  1 - only environment textures enabled.\n"
6212     "  2 - all textures enabled.\n"
6213     "  this command sets texture details mode for the specified view.\n"
6214     , __FILE__, VSetTextureMode, group);
6215   theCommands.Add("vdefaults",
6216     "vdefaults [absDefl=value] [devCoeff=value] [angDefl=value]",
6217     __FILE__, VDefaults, group);
6218   theCommands.Add("vlight",
6219             "vlight [add|new {amb}ient|directional|{spot}light|positional]"
6220     "\n\t\t:        [{del}ete|change lightId] [local|global]"
6221     "\n\t\t:        [{pos}ition X Y Z] [color colorName] [{head}light 0|1]"
6222     "\n\t\t:        [{constAtten}uation value] [{linearAtten}uation value]"
6223     "\n\t\t:        [angle angleDeg] [{spotexp}onent value]",
6224     __FILE__, VLight, group);
6225   theCommands.Add("vraytrace",
6226     "vraytrace 0|1",
6227     __FILE__,VRaytrace,group);
6228   theCommands.Add("vclinfo",
6229     "vclinfo",
6230     __FILE__,VClInfo,group);
6231   theCommands.Add("vsetraytracemode",
6232     "vsetraytracemode [shad=0|1] [refl=0|1] [aa=0|1]",
6233     __FILE__,VSetRaytraceMode,group);
6234 }