0023905: Clean obsolete test commands
[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
22 // Robert Boehne 30 May 2000 : Dec Osf
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #ifdef WNT
29 #include <windows.h>
30 #endif
31
32 #include <AIS_Shape.hxx>
33 #include <AIS_Drawer.hxx>
34 #include <AIS_InteractiveObject.hxx>
35 #include <AIS_ListOfInteractive.hxx>
36 #include <AIS_ListIteratorOfListOfInteractive.hxx>
37 #include <DBRep.hxx>
38 #include <Graphic3d_AspectMarker3d.hxx>
39 #include <Graphic3d_GraphicDriver.hxx>
40 #include <Graphic3d_ExportFormat.hxx>
41 #include <Graphic3d_NameOfTextureEnv.hxx>
42 #include <Graphic3d_TextureEnv.hxx>
43 #include <Graphic3d_TextureParams.hxx>
44 #include <Graphic3d_TypeOfTextureFilter.hxx>
45 #include <ViewerTest.hxx>
46 #include <ViewerTest_EventManager.hxx>
47 #include <ViewerTest_DoubleMapOfInteractiveAndName.hxx>
48 #include <Visual3d_View.hxx>
49 #include <Visual3d_ViewManager.hxx>
50 #include <V3d_LayerMgr.hxx>
51 #include <NIS_View.hxx>
52 #include <NIS_Triangulated.hxx>
53 #include <NIS_InteractiveContext.hxx>
54 #include <AIS_InteractiveContext.hxx>
55 #include <Draw_Interpretor.hxx>
56 #include <Draw.hxx>
57 #include <Draw_Appli.hxx>
58 #include <Aspect_PrintAlgo.hxx>
59 #include <Image_AlienPixMap.hxx>
60 #include <OSD_Timer.hxx>
61 #include <TColStd_SequenceOfInteger.hxx>
62 #include <TColStd_HSequenceOfReal.hxx>
63 #include <TColgp_Array1OfPnt2d.hxx>
64 #include <Visual3d_LayerItem.hxx>
65 #include <V3d_LayerMgr.hxx>
66 #include <V3d_LayerMgrPointer.hxx>
67 #include <Aspect_TypeOfLine.hxx>
68 #include <Image_Diff.hxx>
69 #include <Aspect_DisplayConnection.hxx>
70 #include <Graphic3d.hxx>
71
72 #ifdef WNT
73 #undef DrawText
74 #endif
75
76 #include <Visual3d_Layer.hxx>
77 #include <cstdlib>
78
79 #if defined(_WIN32) || defined(__WIN32__)
80   #include <WNT_WClass.hxx>
81   #include <WNT_Window.hxx>
82
83   #if defined(_MSC_VER)
84     #define _CRT_SECURE_NO_DEPRECATE
85     #pragma warning (disable:4996)
86   #endif
87 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
88   #include <Cocoa_Window.hxx>
89   #include <tk.h>
90 #else
91   #include <Xw_Window.hxx>
92   #include <X11/Xlib.h> /* contains some dangerous #defines such as Status, True etc. */
93   #include <X11/Xutil.h>
94   #include <tk.h>
95 #endif
96
97 //==============================================================================
98
99 //==============================================================================
100 //  VIEWER GLOBAL VARIABLES
101 //==============================================================================
102
103 Standard_IMPORT Standard_Boolean Draw_VirtualWindows;
104
105 Standard_EXPORT int ViewerMainLoop(Standard_Integer , const char** argv);
106 extern const Handle(NIS_InteractiveContext)& TheNISContext();
107 extern ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
108
109 #if defined(_WIN32) || defined(__WIN32__)
110 static Handle(WNT_Window)& VT_GetWindow() {
111   static Handle(WNT_Window) WNTWin;
112   return WNTWin;
113 }
114 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
115 static Handle(Cocoa_Window)& VT_GetWindow()
116 {
117   static Handle(Cocoa_Window) aWindow;
118   return aWindow;
119 }
120 extern void ViewerTest_SetCocoaEventManagerView (const Handle(Cocoa_Window)& theWindow);
121 #else
122 static Handle(Xw_Window)& VT_GetWindow(){
123   static Handle(Xw_Window) XWWin;
124   return XWWin;
125 }
126 static Display *display;
127
128 static void VProcessEvents(ClientData,int);
129 #endif
130
131 static Handle(Graphic3d_GraphicDriver)& GetGraphicDriver()
132 {
133   static Handle(Graphic3d_GraphicDriver) aGraphicDriver;
134   return aGraphicDriver;
135 }
136
137 static Standard_Boolean MyHLRIsOn = Standard_False;
138
139 #define ZCLIPWIDTH 1.
140
141 static void OSWindowSetup();
142
143 //==============================================================================
144 //  EVENT GLOBAL VARIABLES
145 //==============================================================================
146
147 static int Start_Rot = 0;
148 static int ZClipIsOn = 0;
149 int X_Motion = 0; // Current cursor position
150 int Y_Motion = 0;
151 int X_ButtonPress = 0; // Last ButtonPress position
152 int Y_ButtonPress = 0;
153 Standard_Boolean IsDragged = Standard_False;
154 Standard_Boolean DragFirst;
155
156 //==============================================================================
157
158 #ifdef WNT
159 static LRESULT WINAPI ViewerWindowProc(
160                                        HWND hwnd,
161                                        UINT uMsg,
162                                        WPARAM wParam,
163                                        LPARAM lParam );
164 static LRESULT WINAPI AdvViewerWindowProc(
165   HWND hwnd,
166   UINT uMsg,
167   WPARAM wParam,
168   LPARAM lParam );
169 #endif
170
171
172 //==============================================================================
173 //function : WClass
174 //purpose  :
175 //==============================================================================
176
177 const Handle(MMgt_TShared)& ViewerTest::WClass()
178 {
179   static Handle(MMgt_TShared) theWClass;
180 #if defined(_WIN32) || defined(__WIN32__)
181   if (theWClass.IsNull())
182   {
183     theWClass = new WNT_WClass ("GW3D_Class", AdvViewerWindowProc,
184       CS_VREDRAW | CS_HREDRAW, 0, 0,
185       ::LoadCursor (NULL, IDC_ARROW));
186   }
187 #endif
188   return theWClass;
189 }
190
191 //==============================================================================
192 //function : ViewerInit
193 //purpose  : Create the window viewer and initialize all the global variable
194 //==============================================================================
195
196 void ViewerTest::ViewerInit (const Standard_Integer thePxLeft,  const Standard_Integer thePxTop,
197                              const Standard_Integer thePxWidth, const Standard_Integer thePxHeight)
198 {
199   static Standard_Boolean isFirst = Standard_True;
200
201   // Default position and dimension of the viewer window.
202   // Note that left top corner is set to be sufficiently small to have
203   // window fit in the small screens (actual for remote desktops, see #23003).
204   // The position corresponds to the window's client area, thus some
205   // gap is added for window frame to be visible.
206   Standard_Integer aPxLeft   = 20;
207   Standard_Integer aPxTop    = 40;
208   Standard_Integer aPxWidth  = 409;
209   Standard_Integer aPxHeight = 409;
210   if (thePxWidth != 0 && thePxHeight != 0)
211   {
212     aPxLeft   = thePxLeft;
213     aPxTop    = thePxTop;
214     aPxWidth  = thePxWidth;
215     aPxHeight = thePxHeight;
216   }
217
218   if (isFirst)
219   {
220     Handle(Aspect_DisplayConnection) aDisplayConnection = new Aspect_DisplayConnection();
221     if (GetGraphicDriver().IsNull())
222     {
223       GetGraphicDriver() = Graphic3d::InitGraphicDriver (aDisplayConnection);
224     }
225 #if defined(_WIN32) || defined(__WIN32__)
226     if (VT_GetWindow().IsNull())
227     {
228       VT_GetWindow() = new WNT_Window ("Test3d",
229                                        Handle(WNT_WClass)::DownCast (WClass()),
230                                        WS_OVERLAPPEDWINDOW,
231                                        aPxLeft, aPxTop,
232                                        aPxWidth, aPxHeight,
233                                        Quantity_NOC_BLACK);
234     }
235 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
236     if (VT_GetWindow().IsNull())
237     {
238       VT_GetWindow() = new Cocoa_Window ("Test3d",
239                                          aPxLeft, aPxTop,
240                                          aPxWidth, aPxHeight);
241       ViewerTest_SetCocoaEventManagerView (VT_GetWindow());
242     }
243 #else
244     if (VT_GetWindow().IsNull())
245     {
246       VT_GetWindow() = new Xw_Window (aDisplayConnection,
247                                       "Test3d",
248                                       aPxLeft, aPxTop,
249                                       aPxWidth, aPxHeight);
250     }
251 #endif
252     VT_GetWindow()->SetVirtual (Draw_VirtualWindows);
253
254     Handle(V3d_Viewer) a3DViewer, a3DCollector;
255     // Viewer and View creation
256
257     TCollection_ExtendedString NameOfWindow("Visu3D");
258
259     a3DViewer = new V3d_Viewer(GetGraphicDriver(), NameOfWindow.ToExtString());
260     NameOfWindow = TCollection_ExtendedString("Collector");
261     a3DCollector = new V3d_Viewer(GetGraphicDriver(), NameOfWindow.ToExtString());
262     a3DViewer->SetDefaultBackgroundColor(Quantity_NOC_BLACK);
263     a3DCollector->SetDefaultBackgroundColor(Quantity_NOC_STEELBLUE);
264     Handle(NIS_View) aView = Handle(NIS_View)::DownCast(ViewerTest::CurrentView());
265     if (aView.IsNull())
266     {
267       //Handle(V3d_View) a3DViewCol = a3DViewer->CreateView();
268       aView = new NIS_View (a3DViewer, VT_GetWindow());
269       ViewerTest::CurrentView(aView);
270       TheNISContext()->AttachView (aView);
271     }
272
273     // AIS setup
274     if ( ViewerTest::GetAISContext().IsNull() ) {
275       Handle(AIS_InteractiveContext) C =
276         new AIS_InteractiveContext(a3DViewer,a3DCollector);
277       ViewerTest::SetAISContext(C);
278     }
279
280     // Setup for X11 or NT
281     OSWindowSetup();
282     // Viewer and View creation
283
284     a3DViewer->SetDefaultBackgroundColor(Quantity_NOC_BLACK);
285
286     Handle (V3d_View) V = ViewerTest::CurrentView();
287     //    V->SetWindow(VT_GetWindow(), NULL, MyViewProc, NULL);
288
289     V->SetZClippingDepth(0.5);
290     V->SetZClippingWidth(ZCLIPWIDTH/2.);
291     a3DViewer->SetDefaultLights();
292     a3DViewer->SetLightOn();
293
294   #if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
295   #if TCL_MAJOR_VERSION  < 8
296     Tk_CreateFileHandler((void*)ConnectionNumber(display),
297       TK_READABLE, VProcessEvents, (ClientData) VT_GetWindow()->XWindow() );
298   #else
299     Tk_CreateFileHandler(ConnectionNumber(display),
300       TK_READABLE, VProcessEvents, (ClientData) VT_GetWindow()->XWindow() );
301   #endif
302   #endif
303
304     isFirst = Standard_False;
305   }
306
307   VT_GetWindow()->Map();
308   ViewerTest::CurrentView()->Redraw();
309 }
310
311 //==============================================================================
312 //function : Vinit
313 //purpose  : Create the window viewer and initialize all the global variable
314 //    Use Tk_CreateFileHandler on UNIX to cath the X11 Viewer event
315 //==============================================================================
316
317 static int VInit (Draw_Interpretor& , Standard_Integer argc, const char** argv)
318 {
319   Standard_Integer aPxLeft   = (argc > 1) ? Draw::Atoi (argv[1]) : 0;
320   Standard_Integer aPxTop    = (argc > 2) ? Draw::Atoi (argv[2]) : 0;
321   Standard_Integer aPxWidth  = (argc > 3) ? Draw::Atoi (argv[3]) : 0;
322   Standard_Integer aPxHeight = (argc > 4) ? Draw::Atoi (argv[4]) : 0;
323   ViewerTest::ViewerInit (aPxLeft, aPxTop, aPxWidth, aPxHeight);
324   return 0;
325 }
326
327 //==============================================================================
328 //function : VHLR
329 //purpose  : hidden lines removal algorithm
330 //==============================================================================
331
332 static int VHLR (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
333 {
334   if (ViewerTest::CurrentView().IsNull())
335   {
336     di << argv[0] << ": Call vinit before this command, please.\n";
337     return 1;
338   }
339
340   if (argc != 2)
341   {
342     di << argv[0] << ": Wrong number of command arguments.\n"
343       << "Type help " << argv[0] << " for more information.\n";
344     return 1;
345   }
346
347   Standard_Boolean isHLROn =
348     (!strcasecmp (argv[1], "on")) ? Standard_True : Standard_False;
349
350   if (isHLROn == MyHLRIsOn)
351   {
352     return 0;
353   }
354
355   MyHLRIsOn = isHLROn;
356   ViewerTest::CurrentView()->SetComputedMode (MyHLRIsOn);
357
358   return 0;
359 }
360
361 //==============================================================================
362 //function : VHLRType
363 //purpose  : change type of using HLR algorithm
364 //==============================================================================
365
366 static int VHLRType (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
367 {
368   if (ViewerTest::CurrentView().IsNull())
369   {
370     di << argv[0] << ": Call vinit before this command, please.\n";
371     return 1;
372   }
373
374   if (argc < 2)
375   {
376     di << argv[0] << ": Wrong number of command arguments.\n"
377       << "Type help " << argv[0] << " for more information.\n";
378     return 1;
379   }
380
381   Prs3d_TypeOfHLR aTypeOfHLR =
382     (!strcasecmp (argv[1], "algo")) ? Prs3d_TOH_Algo : Prs3d_TOH_PolyAlgo;
383
384   if (argc == 2)
385   {
386     AIS_ListOfInteractive aListOfShapes;
387     ViewerTest::GetAISContext()->DisplayedObjects (aListOfShapes);
388     ViewerTest::GetAISContext()->DefaultDrawer()->SetTypeOfHLR(aTypeOfHLR);
389     for (AIS_ListIteratorOfListOfInteractive anIter(aListOfShapes);
390       anIter.More(); anIter.Next())
391     {
392       Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anIter.Value());
393       if (aShape.IsNull())
394         continue;
395       if (aShape->TypeOfHLR() != aTypeOfHLR)
396         aShape->SetTypeOfHLR (aTypeOfHLR);
397       if (MyHLRIsOn)
398         aShape->Redisplay();
399     }
400     ViewerTest::CurrentView()->Update();
401     return 0;
402   }
403   else
404   {
405     for (Standard_Integer i = 2; i < argc; ++i)
406     {
407       ViewerTest_DoubleMapOfInteractiveAndName& aMap = GetMapOfAIS();
408       TCollection_AsciiString aName (argv[i]);
409
410       if (!aMap.IsBound2 (aName))
411       {
412         di << argv[0] << ":" << " Wrong shape name:" << aName.ToCString() << ".\n";
413         continue;
414       }
415       Handle(AIS_Shape) anAISObject =
416         Handle(AIS_Shape)::DownCast (aMap.Find2(aName));
417       if (anAISObject.IsNull())
418         continue;
419       anAISObject->SetTypeOfHLR (aTypeOfHLR);
420       if (MyHLRIsOn)
421         anAISObject->Redisplay();
422     }
423     ViewerTest::CurrentView()->Update();
424   }
425
426   return 0;
427 }
428
429 //==============================================================================
430 //function : VT_ProcessKeyPress
431 //purpose  : Handle KeyPress event from a CString
432 //==============================================================================
433 void VT_ProcessKeyPress (const char* buf_ret)
434 {
435   //cout << "KeyPress" << endl;
436   const Handle(V3d_View) aView = ViewerTest::CurrentView();
437   const Handle(NIS_View) aNisView = Handle(NIS_View)::DownCast (aView);
438   // Letter in alphabetic order
439
440   if ( !strcasecmp(buf_ret, "A") ) {
441     // AXO
442     aView->SetProj(V3d_XposYnegZpos);
443   }
444   else if ( !strcasecmp(buf_ret, "D") ) {
445     // Reset
446     aView->Reset();
447   }
448   else if ( !strcasecmp(buf_ret, "F") ) {
449     // FitAll
450     if (aNisView.IsNull())
451       aView->FitAll();
452     else
453       aNisView->FitAll3d();
454   }
455   else if ( !strcasecmp(buf_ret, "H") ) {
456     // HLR
457     cout << "HLR" << endl;
458     aView->SetComputedMode (!aView->ComputedMode());
459     MyHLRIsOn = aView->ComputedMode();
460   }
461   else if ( !strcasecmp(buf_ret, "P") ) {
462     // Type of HLR
463     Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
464     if (aContext->DefaultDrawer()->TypeOfHLR() == Prs3d_TOH_Algo)
465       aContext->DefaultDrawer()->SetTypeOfHLR(Prs3d_TOH_PolyAlgo);
466     else
467       aContext->DefaultDrawer()->SetTypeOfHLR(Prs3d_TOH_Algo);
468     if (aContext->NbCurrents()==0 || aContext->NbSelected() == 0)
469     {
470       AIS_ListOfInteractive aListOfShapes;
471       aContext->DisplayedObjects(aListOfShapes);
472       for (AIS_ListIteratorOfListOfInteractive anIter(aListOfShapes);
473         anIter.More(); anIter.Next())
474       {
475         Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anIter.Value());
476         if (aShape.IsNull())
477           continue;
478         if (aShape->TypeOfHLR() == Prs3d_TOH_PolyAlgo)
479           aShape->SetTypeOfHLR (Prs3d_TOH_Algo);
480         else
481           aShape->SetTypeOfHLR (Prs3d_TOH_PolyAlgo);
482         aShape->Redisplay();
483       }
484     }
485     else
486     {
487       for (aContext->InitCurrent();aContext->MoreCurrent();aContext->NextCurrent())
488       {
489         Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(aContext->Current());
490         if (aShape.IsNull())
491           continue;
492         if(aShape->TypeOfHLR() == Prs3d_TOH_PolyAlgo)
493           aShape->SetTypeOfHLR (Prs3d_TOH_Algo);
494         else
495           aShape->SetTypeOfHLR (Prs3d_TOH_PolyAlgo);
496         aShape->Redisplay();
497       }
498     }
499
500     aContext->UpdateCurrentViewer();
501     
502   }
503   else if ( !strcasecmp(buf_ret, "S") ) {
504     // SHADING
505     cout << "passage en mode 1 (shading pour les shapes)" << endl;
506
507     Handle(AIS_InteractiveContext) Ctx = ViewerTest::GetAISContext();
508     if(Ctx->NbCurrents()==0 ||
509       Ctx->NbSelected()==0)
510       Ctx->SetDisplayMode(AIS_Shaded);
511     else{
512       if(Ctx->HasOpenedContext()){
513         for(Ctx->InitSelected();Ctx->MoreSelected();Ctx->NextSelected())
514           Ctx->SetDisplayMode(Ctx->Interactive(),1,Standard_False);
515       }
516       else{
517         for(Ctx->InitCurrent();Ctx->MoreCurrent();Ctx->NextCurrent())
518           Ctx->SetDisplayMode(Ctx->Current(),1,Standard_False);
519       }
520       Ctx->UpdateCurrentViewer();
521     }
522   }
523   else if ( !strcasecmp(buf_ret, "U") ) {
524     // Unset display mode
525     cout<<"passage au mode par defaut"<<endl;
526
527     Handle(AIS_InteractiveContext) Ctx = ViewerTest::GetAISContext();
528     if(Ctx->NbCurrents()==0 ||
529       Ctx->NbSelected()==0)
530       Ctx->SetDisplayMode(AIS_WireFrame);
531     else{
532       if(Ctx->HasOpenedContext()){
533         for(Ctx->InitSelected();Ctx->MoreSelected();Ctx->NextSelected())
534           Ctx->UnsetDisplayMode(Ctx->Interactive(),Standard_False);
535       }
536       else{
537         for(Ctx->InitCurrent();Ctx->MoreCurrent();Ctx->NextCurrent())
538           Ctx->UnsetDisplayMode(Ctx->Current(),Standard_False);
539       }
540       Ctx->UpdateCurrentViewer();
541     }
542
543   }
544   else if ( !strcasecmp(buf_ret, "T") ) {
545     // Top
546     aView->SetProj(V3d_Zpos);
547   }
548   else if ( !strcasecmp(buf_ret, "B") ) {
549     // Bottom
550     aView->SetProj(V3d_Zneg);
551   }
552   else if ( !strcasecmp(buf_ret, "L") ) {
553     // Left
554     aView->SetProj(V3d_Xneg);
555   }
556   else if ( !strcasecmp(buf_ret, "R") ) {
557     // Right
558     aView->SetProj(V3d_Xpos);
559   }
560
561   else if ( !strcasecmp(buf_ret, "W") ) {
562     // WIREFRAME
563     cout << "passage en mode 0 (filaire pour les shapes)" << endl;
564     Handle(AIS_InteractiveContext) Ctx = ViewerTest::GetAISContext();
565     if(Ctx->NbCurrents()==0 ||
566       Ctx->NbSelected()==0)
567       Ctx->SetDisplayMode(AIS_WireFrame);
568     else{
569       if(Ctx->HasOpenedContext()){
570         for(Ctx->InitSelected();Ctx->MoreSelected();Ctx->NextSelected())
571           Ctx->SetDisplayMode(Ctx->Interactive(),0,Standard_False);
572       }
573       else{
574         for(Ctx->InitCurrent();Ctx->MoreCurrent();Ctx->NextCurrent())
575           Ctx->SetDisplayMode(Ctx->Current(),0,Standard_False);
576       }
577       Ctx->UpdateCurrentViewer();
578     }
579   }
580   else if ( !strcasecmp(buf_ret, "Z") ) {
581     // ZCLIP
582
583     if ( ZClipIsOn ) {
584       cout << "ZClipping OFF" << endl;
585       ZClipIsOn = 0;
586
587       aView->SetZClippingType(V3d_OFF);
588       aView->Redraw();
589     }
590     else {
591       cout << "ZClipping ON" << endl;
592       ZClipIsOn = 1;
593
594       aView->SetZClippingType(V3d_FRONT);
595       aView->Redraw();
596     }
597   }
598   else if ( !strcasecmp(buf_ret, ",") ) {
599     ViewerTest::GetAISContext()->HilightNextDetected(ViewerTest::CurrentView());
600
601
602   }
603   else if ( !strcasecmp(buf_ret, ".") ) {
604     ViewerTest::GetAISContext()->HilightPreviousDetected(ViewerTest::CurrentView());
605   }
606   // Number
607   else{
608     Standard_Integer Num = Draw::Atoi(buf_ret);
609     if(Num>=0 && Num<=7)
610       ViewerTest::StandardModeActivation(Num);
611   }
612 }
613
614 //==============================================================================
615 //function : VT_ProcessExpose
616 //purpose  : Redraw the View on an Expose Event
617 //==============================================================================
618 void VT_ProcessExpose()
619 {
620   Handle(V3d_View) aView3d = ViewerTest::CurrentView();
621   if (!aView3d.IsNull())
622   {
623     aView3d->Redraw();
624   }
625 }
626
627 //==============================================================================
628 //function : VT_ProcessConfigure
629 //purpose  : Resize the View on an Configure Event
630 //==============================================================================
631 void VT_ProcessConfigure()
632 {
633   Handle(V3d_View) aView3d = ViewerTest::CurrentView();
634   if (aView3d.IsNull())
635   {
636     return;
637   }
638
639   aView3d->MustBeResized();
640   aView3d->Update();
641   aView3d->Redraw();
642 }
643
644 //==============================================================================
645 //function : VT_ProcessButton1Press
646 //purpose  : Picking
647 //==============================================================================
648 Standard_Boolean VT_ProcessButton1Press(
649   Standard_Integer ,
650   const char**     argv,
651   Standard_Boolean pick,
652   Standard_Boolean shift)
653 {
654   Handle(ViewerTest_EventManager) EM = ViewerTest::CurrentEventManager();
655   if ( pick ) {
656     Standard_Real X, Y, Z;
657
658     ViewerTest::CurrentView()->Convert(X_Motion, Y_Motion, X, Y, Z);
659
660     Draw::Set(argv[1], X);
661     Draw::Set(argv[2], Y);
662     Draw::Set(argv[3], Z);}
663
664   if(shift)
665     EM->ShiftSelect();
666   else
667     EM->Select();
668
669   pick = 0;
670   return pick;
671 }
672
673 //==============================================================================
674 //function : VT_ProcessButton1Release
675 //purpose  : End selecting
676 //==============================================================================
677 void VT_ProcessButton1Release (Standard_Boolean theIsShift)
678 {
679   if (IsDragged)
680   {
681     IsDragged = Standard_False;
682     Handle(ViewerTest_EventManager) EM = ViewerTest::CurrentEventManager();
683     if (theIsShift)
684     {
685       EM->ShiftSelect (Min (X_ButtonPress, X_Motion), Max (Y_ButtonPress, Y_Motion),
686                        Max (X_ButtonPress, X_Motion), Min (Y_ButtonPress, Y_Motion));
687     }
688     else
689     {
690       EM->Select (Min (X_ButtonPress, X_Motion), Max (Y_ButtonPress, Y_Motion),
691                   Max (X_ButtonPress, X_Motion), Min (Y_ButtonPress, Y_Motion));
692     }
693   }
694 }
695
696 //==============================================================================
697 //function : VT_ProcessButton3Press
698 //purpose  : Start Rotation
699 //==============================================================================
700 void VT_ProcessButton3Press()
701 {
702   Start_Rot = 1;
703   if (MyHLRIsOn)
704   {
705     ViewerTest::CurrentView()->SetComputedMode (Standard_False);
706   }
707   ViewerTest::CurrentView()->StartRotation( X_ButtonPress, Y_ButtonPress );
708 }
709
710 //==============================================================================
711 //function : VT_ProcessButton3Release
712 //purpose  : End rotation
713 //==============================================================================
714 void VT_ProcessButton3Release()
715 {
716   if (Start_Rot)
717   {
718     Start_Rot = 0;
719     if (MyHLRIsOn)
720     {
721       ViewerTest::CurrentView()->SetComputedMode (Standard_True);
722     }
723   }
724 }
725
726 //==============================================================================
727 //function : ProcessZClipMotion
728 //purpose  : Zoom
729 //==============================================================================
730
731 void ProcessZClipMotion()
732 {
733   Handle(V3d_View)  a3DView = ViewerTest::CurrentView();
734   if ( Abs(X_Motion - X_ButtonPress) > 2 ) {
735     static Standard_Real CurZPos = 0.;
736
737     //Quantity_Length VDX, VDY;
738     //a3DView->Size(VDX,VDY);
739     //Standard_Real VDZ = a3DView->ZSize();
740     //printf("View size (%lf,%lf,%lf)\n", VDX, VDY, VDZ);
741
742     Quantity_Length dx = a3DView->Convert(X_Motion - X_ButtonPress);
743
744     // Front = Depth + width/2.
745     Standard_Real D = 0.5;
746     Standard_Real W = 0.1;
747
748     CurZPos += (dx);
749
750     D += CurZPos;
751
752     //printf("dx %lf Depth %lf Width %lf\n", dx, D, W);
753
754     a3DView->SetZClippingType(V3d_OFF);
755     a3DView->SetZClippingDepth(D);
756     a3DView->SetZClippingWidth(W);
757     a3DView->SetZClippingType(V3d_FRONT);
758
759     a3DView->Redraw();
760
761     X_ButtonPress = X_Motion;
762     Y_ButtonPress = Y_Motion;
763   }
764 }
765
766 //==============================================================================
767 //function : ProcessControlButton1Motion
768 //purpose  : Zoom
769 //==============================================================================
770
771 static void ProcessControlButton1Motion()
772 {
773   ViewerTest::CurrentView()->Zoom( X_ButtonPress, Y_ButtonPress, X_Motion, Y_Motion);
774
775   X_ButtonPress = X_Motion;
776   Y_ButtonPress = Y_Motion;
777 }
778
779 //==============================================================================
780 //function : VT_ProcessControlButton2Motion
781 //purpose  : Panning
782 //==============================================================================
783 void VT_ProcessControlButton2Motion()
784 {
785   Quantity_Length dx = ViewerTest::CurrentView()->Convert(X_Motion - X_ButtonPress);
786   Quantity_Length dy = ViewerTest::CurrentView()->Convert(Y_Motion - Y_ButtonPress);
787
788   dy = -dy; // Xwindow Y axis is from top to Bottom
789
790   ViewerTest::CurrentView()->Panning( dx, dy );
791
792   X_ButtonPress = X_Motion;
793   Y_ButtonPress = Y_Motion;
794 }
795
796 //==============================================================================
797 //function : VT_ProcessControlButton3Motion
798 //purpose  : Rotation
799 //==============================================================================
800 void VT_ProcessControlButton3Motion()
801 {
802   if (Start_Rot)
803   {
804     ViewerTest::CurrentView()->Rotation (X_Motion, Y_Motion);
805   }
806 }
807
808 //==============================================================================
809 //function : VT_ProcessMotion
810 //purpose  :
811 //==============================================================================
812 void VT_ProcessMotion()
813 {
814   //pre-hilights detected objects at mouse position
815
816   Handle(ViewerTest_EventManager) EM = ViewerTest::CurrentEventManager();
817   EM->MoveTo(X_Motion, Y_Motion);
818 }
819
820
821 void ViewerTest::GetMousePosition(Standard_Integer& Xpix,Standard_Integer& Ypix)
822 {
823   Xpix = X_Motion;Ypix=Y_Motion;
824 }
825
826 //==============================================================================
827 //function : ViewProject: implements VAxo, VTop, VLeft, ...
828 //purpose  : Switches to an axonometric, top, left and other views
829 //==============================================================================
830
831 static int ViewProject(Draw_Interpretor& di, const V3d_TypeOfOrientation ori)
832 {
833   if ( ViewerTest::CurrentView().IsNull() )
834   {
835     di<<"Call vinit before this command, please"<<"\n";
836     return 1;
837   }
838
839   ViewerTest::CurrentView()->SetProj(ori);
840   return 0;
841 }
842
843 //==============================================================================
844 //function : VAxo
845 //purpose  : Switch to an Axonometric view
846 //Draw arg : No args
847 //==============================================================================
848
849 static int VAxo(Draw_Interpretor& di, Standard_Integer , const char** )
850 {
851   return ViewProject(di, V3d_XposYnegZpos);
852 }
853
854 //==============================================================================
855 //function : VTop
856 //purpose  : Switch to a Top View
857 //Draw arg : No args
858 //==============================================================================
859
860 static int VTop(Draw_Interpretor& di, Standard_Integer , const char** )
861 {
862   return ViewProject(di, V3d_Zpos);
863 }
864
865 //==============================================================================
866 //function : VBottom
867 //purpose  : Switch to a Bottom View
868 //Draw arg : No args
869 //==============================================================================
870
871 static int VBottom(Draw_Interpretor& di, Standard_Integer , const char** )
872 {
873   return ViewProject(di, V3d_Zneg);
874 }
875
876 //==============================================================================
877 //function : VLeft
878 //purpose  : Switch to a Left View
879 //Draw arg : No args
880 //==============================================================================
881
882 static int VLeft(Draw_Interpretor& di, Standard_Integer , const char** )
883 {
884   return ViewProject(di, V3d_Ypos);
885 }
886
887 //==============================================================================
888 //function : VRight
889 //purpose  : Switch to a Right View
890 //Draw arg : No args
891 //==============================================================================
892
893 static int VRight(Draw_Interpretor& di, Standard_Integer , const char** )
894 {
895   return ViewProject(di, V3d_Yneg);
896 }
897
898 //==============================================================================
899 //function : VFront
900 //purpose  : Switch to a Front View
901 //Draw arg : No args
902 //==============================================================================
903
904 static int VFront(Draw_Interpretor& di, Standard_Integer , const char** )
905 {
906   return ViewProject(di, V3d_Xpos);
907 }
908
909 //==============================================================================
910 //function : VBack
911 //purpose  : Switch to a Back View
912 //Draw arg : No args
913 //==============================================================================
914
915 static int VBack(Draw_Interpretor& di, Standard_Integer , const char** )
916 {
917   return ViewProject(di, V3d_Xneg);
918 }
919
920 //==============================================================================
921 //function : VHelp
922 //purpose  : Dsiplay help on viewer Keyboead and mouse commands
923 //Draw arg : No args
924 //==============================================================================
925
926 static int VHelp(Draw_Interpretor& di, Standard_Integer , const char** )
927 {
928
929   di << "Q : Quit the application" << "\n";
930
931   di << "========================="<<"\n";
932   di << "F : FitAll" << "\n";
933   di << "T : TopView" << "\n";
934   di << "B : BottomView" << "\n";
935   di << "R : RightView" << "\n";
936   di << "L : LeftView" << "\n";
937   di << "A : AxonometricView" << "\n";
938   di << "D : ResetView" << "\n";
939
940   di << "========================="<<"\n";
941   di << "S : Shading" << "\n";
942   di << "W : Wireframe" << "\n";
943   di << "H : HidelLineRemoval" << "\n";
944   di << "U : Unset display mode" << "\n";
945
946   di << "========================="<<"\n";
947   di << "Selection mode "<<"\n";
948   di << "0 : Shape" <<"\n";
949   di << "1 : Vertex" <<"\n";
950   di << "2 : Edge" <<"\n";
951   di << "3 : Wire" <<"\n";
952   di << "4 : Face" <<"\n";
953   di << "5 : Shell" <<"\n";
954   di << "6 : Solid" <<"\n";
955   di << "7 : Compound" <<"\n";
956
957   di << "========================="<<"\n";
958   di << "Z : Switch Z clipping On/Off" << "\n";
959   di << ", : Hilight next detected" << "\n";
960   di << ". : Hilight previous detected" << "\n";
961
962   return 0;
963 }
964
965 #ifdef WNT
966
967 static Standard_Boolean Ppick = 0;
968 static Standard_Integer Pargc = 0;
969 static const char**           Pargv = NULL;
970
971
972 static LRESULT WINAPI AdvViewerWindowProc( HWND hwnd,
973                                           UINT Msg,
974                                           WPARAM wParam,
975                                           LPARAM lParam )
976 {
977   if ( !ViewerTest::CurrentView().IsNull() ) {
978
979     WPARAM fwKeys = wParam;
980
981     switch( Msg ) {
982
983     case WM_LBUTTONUP:
984       IsDragged = Standard_False;
985       if( !DragFirst )
986       {
987         HDC hdc = GetDC( hwnd );
988         HGDIOBJ anObj = SelectObject( hdc, GetStockObject( WHITE_PEN ) );
989         SelectObject( hdc, GetStockObject( HOLLOW_BRUSH ) );
990         SetROP2( hdc, R2_NOT );
991         Rectangle( hdc, X_ButtonPress, Y_ButtonPress, X_Motion, Y_Motion );
992         ReleaseDC( hwnd, hdc );
993
994         const Handle(ViewerTest_EventManager) EM =
995           ViewerTest::CurrentEventManager();
996         if ( fwKeys & MK_SHIFT )
997           EM->ShiftSelect( min( X_ButtonPress, X_Motion ), max( Y_ButtonPress, Y_Motion ),
998           max( X_ButtonPress, X_Motion ), min( Y_ButtonPress, Y_Motion ));
999         else
1000           EM->Select( min( X_ButtonPress, X_Motion ), max( Y_ButtonPress, Y_Motion ),
1001           max( X_ButtonPress, X_Motion ), min( Y_ButtonPress, Y_Motion ));
1002       }
1003       return ViewerWindowProc( hwnd, Msg, wParam, lParam );
1004
1005     case WM_LBUTTONDOWN:
1006       if( fwKeys == MK_LBUTTON || fwKeys == ( MK_LBUTTON | MK_SHIFT ) )
1007       {
1008         IsDragged = Standard_True;
1009         DragFirst = Standard_True;
1010         X_ButtonPress = LOWORD(lParam);
1011         Y_ButtonPress = HIWORD(lParam);
1012       }
1013       return ViewerWindowProc( hwnd, Msg, wParam, lParam );
1014
1015       break;
1016
1017     case WM_MOUSEMOVE:
1018       if( IsDragged )
1019       {
1020         HDC hdc = GetDC( hwnd );
1021
1022         HGDIOBJ anObj = SelectObject( hdc, GetStockObject( WHITE_PEN ) );
1023         SelectObject( hdc, GetStockObject( HOLLOW_BRUSH ) );
1024         SetROP2( hdc, R2_NOT );
1025
1026         if( !DragFirst )
1027           Rectangle( hdc, X_ButtonPress, Y_ButtonPress, X_Motion, Y_Motion );
1028
1029         DragFirst = Standard_False;
1030         X_Motion = LOWORD(lParam);
1031         Y_Motion = HIWORD(lParam);
1032
1033         Rectangle( hdc, X_ButtonPress, Y_ButtonPress, X_Motion, Y_Motion );
1034
1035         SelectObject( hdc, anObj );
1036
1037         ReleaseDC( hwnd, hdc );
1038       }
1039       else
1040         return ViewerWindowProc( hwnd, Msg, wParam, lParam );
1041       break;
1042
1043     default:
1044       return ViewerWindowProc( hwnd, Msg, wParam, lParam );
1045     }
1046     return 0;
1047   }
1048   return ViewerWindowProc( hwnd, Msg, wParam, lParam );
1049 }
1050
1051
1052 static LRESULT WINAPI ViewerWindowProc( HWND hwnd,
1053                                        UINT Msg,
1054                                        WPARAM wParam,
1055                                        LPARAM lParam )
1056 {
1057   /*static Standard_Boolean Ppick = 0;
1058   static Standard_Integer Pargc = 0;
1059   static char**           Pargv = NULL;*/
1060
1061   static int Up = 1;
1062
1063   if ( !ViewerTest::CurrentView().IsNull() ) {
1064     PAINTSTRUCT    ps;
1065
1066     switch( Msg ) {
1067     case WM_CLOSE:
1068       // do not destroy the window - just hide it!
1069       VT_GetWindow()->Unmap();
1070       return 0;
1071     case WM_PAINT:
1072       BeginPaint(hwnd, &ps);
1073       EndPaint(hwnd, &ps);
1074       VT_ProcessExpose();
1075       break;
1076
1077     case WM_SIZE:
1078       VT_ProcessConfigure();
1079       break;
1080
1081     case WM_KEYDOWN:
1082       if ((wParam != VK_SHIFT) && (wParam != VK_CONTROL))
1083       {
1084         char c[2];
1085         c[0] = (char) wParam;
1086         c[1] = '\0';
1087         VT_ProcessKeyPress (c);
1088       }
1089       break;
1090
1091     case WM_LBUTTONUP:
1092     case WM_MBUTTONUP:
1093     case WM_RBUTTONUP:
1094       Up = 1;
1095       VT_ProcessButton3Release();
1096       break;
1097
1098     case WM_LBUTTONDOWN:
1099     case WM_MBUTTONDOWN:
1100     case WM_RBUTTONDOWN:
1101       {
1102         WPARAM fwKeys = wParam;
1103
1104         Up = 0;
1105
1106         X_ButtonPress = LOWORD(lParam);
1107         Y_ButtonPress = HIWORD(lParam);
1108
1109         if (Msg == WM_LBUTTONDOWN)
1110         {
1111           if (fwKeys & MK_CONTROL)
1112           {
1113             Ppick = VT_ProcessButton1Press (Pargc, Pargv, Ppick, (fwKeys & MK_SHIFT));
1114           }
1115           else
1116           {
1117             VT_ProcessButton1Press (Pargc, Pargv, Ppick, (fwKeys & MK_SHIFT));
1118           }
1119         }
1120         else if (Msg == WM_RBUTTONDOWN)
1121         {
1122           // Start rotation
1123           VT_ProcessButton3Press();
1124         }
1125       }
1126       break;
1127
1128     case WM_MOUSEMOVE:
1129       {
1130         //cout << "\t WM_MOUSEMOVE" << endl;
1131         WPARAM fwKeys = wParam;
1132         X_Motion = LOWORD(lParam);
1133         Y_Motion = HIWORD(lParam);
1134
1135         if ( Up &&
1136           fwKeys & ( MK_LBUTTON|MK_MBUTTON|MK_RBUTTON ) ) {
1137             Up = 0;
1138             X_ButtonPress = LOWORD(lParam);
1139             Y_ButtonPress = HIWORD(lParam);
1140
1141             if ( fwKeys & MK_RBUTTON ) {
1142               // Start rotation
1143               VT_ProcessButton3Press();
1144             }
1145           }
1146
1147           if ( fwKeys & MK_CONTROL ) {
1148             if ( fwKeys & MK_LBUTTON ) {
1149               ProcessControlButton1Motion();
1150             }
1151             else if ( fwKeys & MK_MBUTTON ||
1152               ((fwKeys&MK_LBUTTON) &&
1153               (fwKeys&MK_RBUTTON) ) ){
1154                 VT_ProcessControlButton2Motion();
1155               }
1156             else if ( fwKeys & MK_RBUTTON ) {
1157               VT_ProcessControlButton3Motion();
1158             }
1159           }
1160 #ifdef BUG
1161           else if ( fwKeys & MK_SHIFT ) {
1162             if ( fwKeys & MK_MBUTTON ||
1163               ((fwKeys&MK_LBUTTON) &&
1164               (fwKeys&MK_RBUTTON) ) ) {
1165                 cout << "ProcessZClipMotion()" << endl;
1166                 ProcessZClipMotion();
1167               }
1168           }
1169 #endif
1170           else
1171             if ((fwKeys & MK_MBUTTON
1172             || ((fwKeys & MK_LBUTTON) && (fwKeys & MK_RBUTTON))))
1173             {
1174               ProcessZClipMotion();
1175             }
1176             else
1177             {
1178               VT_ProcessMotion();
1179             }
1180       }
1181       break;
1182
1183     default:
1184       return( DefWindowProc( hwnd, Msg, wParam, lParam ));
1185     }
1186     return 0L;
1187   }
1188
1189   return DefWindowProc( hwnd, Msg, wParam, lParam );
1190 }
1191
1192
1193
1194
1195 //==============================================================================
1196 //function : ViewerMainLoop
1197 //purpose  : Get a Event on the view and dispatch it
1198 //==============================================================================
1199
1200
1201 static int ViewerMainLoop(Standard_Integer argc, const char** argv)
1202 {
1203
1204   //cout << "No yet implemented on WNT" << endl;
1205   /*static Standard_Boolean Ppick = 0;
1206   static Standard_Integer Pargc = 0;
1207   static char**           Pargv = NULL;*/
1208
1209   //Ppick = (argc > 0)? -1 : 0;
1210   Ppick = (argc > 0)? 1 : 0;
1211   Pargc = argc;
1212   Pargv = argv;
1213
1214   if ( Ppick ) {
1215     MSG msg;
1216     msg.wParam = 1;
1217
1218     cout << "Start picking" << endl;
1219
1220     //while ( Ppick == -1 ) {
1221     while ( Ppick == 1 ) {
1222       // Wait for a VT_ProcessButton1Press() to toggle pick to 1 or 0
1223       if (GetMessage(&msg, NULL, 0, 0) ) {
1224         TranslateMessage(&msg);
1225         DispatchMessage(&msg);
1226       }
1227     }
1228
1229     cout << "Picking done" << endl;
1230   }
1231
1232   return Ppick;
1233 }
1234
1235 #elif !defined(__APPLE__) || defined(MACOSX_USE_GLX)
1236
1237 int min( int a, int b )
1238 {
1239   if( a<b )
1240     return a;
1241   else
1242     return b;
1243 }
1244
1245 int max( int a, int b )
1246 {
1247   if( a>b )
1248     return a;
1249   else
1250     return b;
1251 }
1252
1253 int ViewerMainLoop(Standard_Integer argc, const char** argv)
1254
1255 { Standard_Boolean pick = argc > 0;
1256
1257 // X11 Event loop
1258
1259 static XEvent report;
1260
1261 XNextEvent( display, &report );
1262 //    cout << "rep type = " << report.type << endl;
1263 //    cout << "rep button = " << report.xbutton.button << endl;
1264
1265 switch ( report.type ) {
1266       case Expose:
1267         {
1268           VT_ProcessExpose();
1269         }
1270         break;
1271       case ConfigureNotify:
1272         {
1273           VT_ProcessConfigure();
1274         }
1275         break;
1276       case KeyPress:
1277         {
1278
1279           KeySym ks_ret ;
1280           char buf_ret[11] ;
1281           int ret_len ;
1282           XComposeStatus status_in_out;
1283
1284           ret_len = XLookupString( ( XKeyEvent *)&report ,
1285             (char *) buf_ret , 10 ,
1286             &ks_ret , &status_in_out ) ;
1287
1288
1289           buf_ret[ret_len] = '\0' ;
1290
1291           if (ret_len)
1292           {
1293             VT_ProcessKeyPress (buf_ret);
1294           }
1295         }
1296         break;
1297       case ButtonPress:
1298         //  cout << "ButtonPress" << endl;
1299         {
1300           X_ButtonPress = report.xbutton.x;
1301           Y_ButtonPress = report.xbutton.y;
1302
1303           if (report.xbutton.button == Button1)
1304           {
1305             if (report.xbutton.state & ControlMask)
1306             {
1307               pick = VT_ProcessButton1Press (argc, argv, pick, (report.xbutton.state & ShiftMask));
1308             }
1309             else
1310             {
1311               IsDragged = Standard_True;
1312               DragFirst = Standard_True;
1313             }
1314           }
1315           else if (report.xbutton.button == Button3)
1316           {
1317             // Start rotation
1318             VT_ProcessButton3Press();
1319           }
1320         }
1321         break;
1322       case ButtonRelease:
1323         {
1324           //    cout<<"relachement du bouton "<<(report.xbutton.button==3 ? "3": "on s'en fout") <<endl;
1325           //    cout << IsDragged << endl;
1326           //    cout << DragFirst << endl;
1327
1328           if( IsDragged )
1329           {
1330             if( !DragFirst )
1331             {
1332               Aspect_Handle aWindow = VT_GetWindow()->XWindow();
1333               GC gc = XCreateGC( display, aWindow, 0, 0 );
1334               //  XSetFunction( display, gc, GXinvert );
1335               XDrawRectangle( display, aWindow, gc, min( X_ButtonPress, X_Motion ), min( Y_ButtonPress, Y_Motion ), abs( X_Motion-X_ButtonPress ), abs( Y_Motion-Y_ButtonPress ) );
1336             }
1337
1338             Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
1339             if( aContext.IsNull() )
1340             {
1341               cout << "The context is null. Please use vinit before createmesh" << endl;
1342               return 0;
1343             }
1344
1345             Standard_Boolean ShiftPressed = ( report.xbutton.state & ShiftMask );
1346             if( report.xbutton.button==1 )
1347               if( DragFirst )
1348                 if( ShiftPressed )
1349                 {
1350                   aContext->ShiftSelect();
1351                   //                   cout << "shift select" << endl;
1352                 }
1353                 else
1354                 {
1355                   aContext->Select();
1356                   //                   cout << "select" << endl;
1357                 }
1358               else
1359                 if( ShiftPressed )
1360                 {
1361                   aContext->ShiftSelect( min( X_ButtonPress, X_Motion ), min( Y_ButtonPress, Y_Motion ),
1362                     max( X_ButtonPress, X_Motion ), max( Y_ButtonPress, Y_Motion ),
1363                     ViewerTest::CurrentView());
1364                   //                   cout << "shift select" << endl;
1365                 }
1366                 else
1367                 {
1368                   aContext->Select( min( X_ButtonPress, X_Motion ), min( Y_ButtonPress, Y_Motion ),
1369                     max( X_ButtonPress, X_Motion ), max( Y_ButtonPress, Y_Motion ),
1370                     ViewerTest::CurrentView() );
1371                   //                   cout << "select" << endl;
1372                 }
1373             else
1374               VT_ProcessButton3Release();
1375
1376             IsDragged = Standard_False;
1377           }
1378           else
1379             VT_ProcessButton3Release();
1380         }
1381         break;
1382       case MotionNotify:
1383         {
1384           if( IsDragged )
1385           {
1386             Aspect_Handle aWindow = VT_GetWindow()->XWindow();
1387             GC gc = XCreateGC( display, aWindow, 0, 0 );
1388             XSetFunction( display, gc, GXinvert );
1389
1390             if( !DragFirst )
1391               XDrawRectangle( display, aWindow, gc, min( X_ButtonPress, X_Motion ), min( Y_ButtonPress, Y_Motion ), abs( X_Motion-X_ButtonPress ), abs( Y_Motion-Y_ButtonPress ) );
1392
1393             X_Motion = report.xmotion.x;
1394             Y_Motion = report.xmotion.y;
1395             DragFirst = Standard_False;
1396
1397             XDrawRectangle( display, aWindow, gc, min( X_ButtonPress, X_Motion ), min( Y_ButtonPress, Y_Motion ), abs( X_Motion-X_ButtonPress ), abs( Y_Motion-Y_ButtonPress ) );
1398           }
1399           else
1400           {
1401             X_Motion = report.xmotion.x;
1402             Y_Motion = report.xmotion.y;
1403
1404             // remove all the ButtonMotionMask
1405             while( XCheckMaskEvent( display, ButtonMotionMask, &report) ) ;
1406
1407             if ( ZClipIsOn && report.xmotion.state & ShiftMask ) {
1408               if ( Abs(X_Motion - X_ButtonPress) > 2 ) {
1409
1410                 Quantity_Length VDX, VDY;
1411
1412                 ViewerTest::CurrentView()->Size(VDX,VDY);
1413                 Standard_Real VDZ =0 ;
1414                 VDZ = ViewerTest::CurrentView()->ZSize();
1415
1416                 //          printf("%lf,%lf,%lf\n", VDX, VDY, VDZ);
1417                 printf("%f,%f,%f\n", VDX, VDY, VDZ);
1418
1419                 Quantity_Length dx = 0 ;
1420                 dx = ViewerTest::CurrentView()->Convert(X_Motion - X_ButtonPress);
1421
1422                 cout << dx << endl;
1423
1424                 dx = dx / VDX * VDZ;
1425
1426                 cout << dx << endl;
1427
1428                 // Front = Depth + width/2.
1429                 //ViewerTest::CurrentView()->SetZClippingDepth(dx);
1430                 //ViewerTest::CurrentView()->SetZClippingWidth(0.);
1431
1432                 ViewerTest::CurrentView()->Redraw();
1433               }
1434             }
1435
1436             if ( report.xmotion.state & ControlMask ) {
1437               if ( report.xmotion.state & Button1Mask ) {
1438                 ProcessControlButton1Motion();
1439               }
1440               else if ( report.xmotion.state & Button2Mask ) {
1441                 VT_ProcessControlButton2Motion();
1442               }
1443               else if ( report.xmotion.state & Button3Mask ) {
1444                 VT_ProcessControlButton3Motion();
1445               }
1446             }
1447             else
1448             {
1449               VT_ProcessMotion();
1450             }
1451           }
1452         }
1453         break;
1454 }
1455
1456
1457 return pick;
1458 }
1459
1460 //==============================================================================
1461 //function : VProcessEvents
1462 //purpose  : call by Tk_CreateFileHandler() to be able to manage the
1463 //       event in the Viewer window
1464 //==============================================================================
1465
1466 static void VProcessEvents(ClientData,int)
1467 {
1468   //cout << "VProcessEvents" << endl;
1469
1470   // test for X Event
1471   while (XPending(display)) {
1472     ViewerMainLoop( 0, NULL);
1473   }
1474 }
1475 #endif
1476
1477 //==============================================================================
1478 //function : OSWindowSetup
1479 //purpose  : Setup for the X11 window to be able to cath the event
1480 //==============================================================================
1481
1482
1483 static void OSWindowSetup()
1484 {
1485 #if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
1486   // X11
1487
1488   Window  window   = VT_GetWindow()->XWindow();
1489
1490   display = GetGraphicDriver()->GetDisplayConnection()->GetDisplay();
1491   //  display = (Display *)GetG3dDevice()->XDisplay();
1492
1493   XSynchronize(display, 1);
1494
1495   VT_GetWindow()->Map();
1496
1497   // X11 : For keyboard on SUN
1498   XWMHints wmhints;
1499   wmhints.flags = InputHint;
1500   wmhints.input = 1;
1501
1502   XSetWMHints( display, window, &wmhints);
1503
1504   XSelectInput( display, window,  ExposureMask | KeyPressMask |
1505     ButtonPressMask | ButtonReleaseMask |
1506     StructureNotifyMask |
1507     PointerMotionMask |
1508     Button1MotionMask | Button2MotionMask |
1509     Button3MotionMask
1510     );
1511
1512   XSynchronize(display, 0);
1513
1514 #else
1515   // WNT
1516 #endif
1517
1518 }
1519
1520
1521 //==============================================================================
1522 //function : VFit
1523
1524 //purpose  : Fitall, no DRAW arguments
1525 //Draw arg : No args
1526 //==============================================================================
1527
1528 static int VFit(Draw_Interpretor& , Standard_Integer , const char** )
1529 {
1530   const Handle(V3d_View) aView = ViewerTest::CurrentView();
1531   Handle(NIS_View) V = Handle(NIS_View)::DownCast(aView);
1532   if (V.IsNull() == Standard_False) {
1533     V->FitAll3d();
1534   } else if (aView.IsNull() == Standard_False) {
1535     aView->FitAll();
1536   }
1537   return 0;
1538 }
1539
1540 //==============================================================================
1541 //function : VZFit
1542 //purpose  : ZFitall, no DRAW arguments
1543 //Draw arg : No args
1544 //==============================================================================
1545
1546 static int VZFit(Draw_Interpretor& , Standard_Integer , const char** )
1547 {
1548   Handle(V3d_View) V = ViewerTest::CurrentView();
1549   if ( !V.IsNull() ) V->ZFitAll(); return 0; }
1550
1551
1552 static int VRepaint(Draw_Interpretor& , Standard_Integer , const char** )
1553 {
1554   Handle(V3d_View) V = ViewerTest::CurrentView();
1555   if ( !V.IsNull() ) V->Redraw(); return 0;
1556 }
1557
1558
1559 //==============================================================================
1560 //function : VClear
1561 //purpose  : Remove all the object from the viewer
1562 //Draw arg : No args
1563 //==============================================================================
1564
1565 static int VClear(Draw_Interpretor& , Standard_Integer , const char** )
1566 {
1567   Handle(V3d_View) V = ViewerTest::CurrentView();
1568   if(!V.IsNull())
1569     ViewerTest::Clear();
1570   return 0;
1571 }
1572
1573 //==============================================================================
1574 //function : VPick
1575 //purpose  :
1576 //==============================================================================
1577
1578 static int VPick(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1579 { if (ViewerTest::CurrentView().IsNull() ) return 1;
1580
1581 if ( argc < 4 ) {
1582   di << argv[0] << "Invalid number of arguments" << "\n";
1583   return 1;
1584 }
1585
1586 while (ViewerMainLoop( argc, argv)) {
1587 }
1588
1589 return 0;
1590 }
1591
1592 //==============================================================================
1593 //function : InitViewerTest
1594 //purpose  : initialisation de toutes les variables static de  ViewerTest (dp)
1595 //==============================================================================
1596
1597 void ViewerTest_InitViewerTest (const Handle(AIS_InteractiveContext)& context)
1598 {
1599   Handle(V3d_Viewer) viewer = context->CurrentViewer();
1600   ViewerTest::SetAISContext(context);
1601   viewer->InitActiveViews();
1602   Handle(V3d_View) view = viewer->ActiveView();
1603   if (viewer->MoreActiveViews()) ViewerTest::CurrentView(view);
1604   ViewerTest::ResetEventManager();
1605   Handle(Aspect_Window) window = view->Window();
1606 #if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
1607   // X11
1608   VT_GetWindow() = Handle(Xw_Window)::DownCast(window);
1609   GetGraphicDriver() = viewer->Driver();
1610   OSWindowSetup();
1611   static int first = 1;
1612   if ( first ) {
1613 #if TCL_MAJOR_VERSION  < 8
1614     Tk_CreateFileHandler((void*)ConnectionNumber(display),
1615       TK_READABLE, VProcessEvents, (ClientData) 0);
1616 #else
1617     Tk_CreateFileHandler(ConnectionNumber(display),
1618       TK_READABLE, VProcessEvents, (ClientData) 0);
1619 #endif
1620     first = 0;
1621   }
1622 #endif
1623 }
1624
1625 //==============================================================================
1626 //function : VSetBg
1627 //purpose  : Load image as background
1628 //==============================================================================
1629
1630 static int VSetBg(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1631 {
1632   if (argc < 2 || argc > 3)
1633   {
1634     di << "Usage : " << argv[0] << " imagefile [filltype] : Load image as background" << "\n";
1635     di << "filltype can be one of CENTERED, TILED, STRETCH, NONE" << "\n";
1636     return 1;
1637   }
1638
1639   Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext();
1640   if(AISContext.IsNull())
1641   {
1642     di << "use 'vinit' command before " << argv[0] << "\n";
1643     return 1;
1644   }
1645
1646   Aspect_FillMethod aFillType = Aspect_FM_CENTERED;
1647   if (argc == 3)
1648   {
1649     const char* szType = argv[2];
1650     if      (strcmp(szType, "NONE"    ) == 0) aFillType = Aspect_FM_NONE;
1651     else if (strcmp(szType, "CENTERED") == 0) aFillType = Aspect_FM_CENTERED;
1652     else if (strcmp(szType, "TILED"   ) == 0) aFillType = Aspect_FM_TILED;
1653     else if (strcmp(szType, "STRETCH" ) == 0) aFillType = Aspect_FM_STRETCH;
1654     else
1655     {
1656       di << "Wrong fill type : " << szType << "\n";
1657       di << "Must be one of CENTERED, TILED, STRETCH, NONE" << "\n";
1658       return 1;
1659     }
1660   }
1661
1662   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1663   V3dView->SetBackgroundImage(argv[1], aFillType, Standard_True);
1664
1665   return 0;
1666 }
1667
1668 //==============================================================================
1669 //function : VSetBgMode
1670 //purpose  : Change background image fill type
1671 //==============================================================================
1672
1673 static int VSetBgMode(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1674 {
1675   if (argc != 2)
1676   {
1677     di << "Usage : " << argv[0] << " filltype : Change background image mode" << "\n";
1678     di << "filltype must be one of CENTERED, TILED, STRETCH, NONE" << "\n";
1679     return 1;
1680   }
1681
1682   Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext();
1683   if(AISContext.IsNull())
1684   {
1685     di << "use 'vinit' command before " << argv[0] << "\n";
1686     return 1;
1687   }
1688
1689   Aspect_FillMethod aFillType;
1690   if (argc == 2)
1691   {
1692     const char* szType = argv[1];
1693     if      (strcmp(szType, "NONE"    ) == 0) aFillType = Aspect_FM_NONE;
1694     else if (strcmp(szType, "CENTERED") == 0) aFillType = Aspect_FM_CENTERED;
1695     else if (strcmp(szType, "TILED"   ) == 0) aFillType = Aspect_FM_TILED;
1696     else if (strcmp(szType, "STRETCH" ) == 0) aFillType = Aspect_FM_STRETCH;
1697     else
1698     {
1699       di << "Wrong fill type : " << szType << "\n";
1700       di << "Must be one of CENTERED, TILED, STRETCH, NONE" << "\n";
1701       return 1;
1702     }
1703   }
1704
1705   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1706   V3dView->SetBgImageStyle(aFillType, Standard_True);
1707
1708   return 0;
1709 }
1710
1711 //==============================================================================
1712 //function : VSetGradientBg
1713 //purpose  : Mount gradient background
1714 //==============================================================================
1715 static int VSetGradientBg(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1716 {
1717   if (argc != 8 )
1718   {
1719     di << "Usage : " << argv[0] << " R1 G1 B1 R2 G2 B2 Type : Mount gradient background" << "\n";
1720     di << "R1,G1,B1,R2,G2,B2 = [0..255]" << "\n";
1721     di << "Type must be one of 0 = NONE, 1 = HOR, 2 = VER, 3 = DIAG1, 4 = DIAG2" << "\n";
1722     di << "                    5 = CORNER1, 6 = CORNER2, 7 = CORNER3, 8 = CORNER4" << "\n";
1723     return 1;
1724   }
1725
1726   Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext();
1727   if(AISContext.IsNull())
1728   {
1729     di << "use 'vinit' command before " << argv[0] << "\n";
1730     return 1;
1731   }
1732   if (argc == 8)
1733   {
1734
1735     Standard_Real R1 = Draw::Atof(argv[1])/255.;
1736     Standard_Real G1 = Draw::Atof(argv[2])/255.;
1737     Standard_Real B1 = Draw::Atof(argv[3])/255.;
1738     Quantity_Color aColor1(R1,G1,B1,Quantity_TOC_RGB);
1739
1740     Standard_Real R2 = Draw::Atof(argv[4])/255.;
1741     Standard_Real G2 = Draw::Atof(argv[5])/255.;
1742     Standard_Real B2 = Draw::Atof(argv[6])/255.;
1743
1744     Quantity_Color aColor2(R2,G2,B2,Quantity_TOC_RGB);
1745     int aType = Draw::Atoi(argv[7]);
1746     if( aType < 0 || aType > 8 )
1747     {
1748       di << "Wrong fill type " << "\n";
1749       di << "Must be one of 0 = NONE, 1 = HOR, 2 = VER, 3 = DIAG1, 4 = DIAG2" << "\n";
1750       di << "               5 = CORNER1, 6 = CORNER2, 7 = CORNER3, 8 = CORNER4" << "\n";
1751       return 1;
1752     }
1753
1754     Aspect_GradientFillMethod aMethod = Aspect_GradientFillMethod(aType);
1755
1756     Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1757     V3dView->SetBgGradientColors( aColor1, aColor2, aMethod, 1);
1758   }
1759
1760   return 0;
1761 }
1762
1763 //==============================================================================
1764 //function : VSetGradientBgMode
1765 //purpose  : Change gradient background fill style
1766 //==============================================================================
1767 static int VSetGradientBgMode(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1768 {
1769   if (argc != 2 )
1770   {
1771     di << "Usage : " << argv[0] << " Type : Change gradient background fill type" << "\n";
1772     di << "Type must be one of 0 = NONE, 1 = HOR, 2 = VER, 3 = DIAG1, 4 = DIAG2" << "\n";
1773     di << "                    5 = CORNER1, 6 = CORNER2, 7 = CORNER3, 8 = CORNER4" << "\n";
1774     return 1;
1775   }
1776
1777   Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext();
1778   if(AISContext.IsNull())
1779   {
1780     di << "use 'vinit' command before " << argv[0] << "\n";
1781     return 1;
1782   }
1783   if (argc == 2)
1784   {
1785     int aType = Draw::Atoi(argv[1]);
1786     if( aType < 0 || aType > 8 )
1787     {
1788       di << "Wrong fill type " << "\n";
1789       di << "Must be one of 0 = NONE, 1 = HOR, 2 = VER, 3 = DIAG1, 4 = DIAG2" << "\n";
1790       di << "               5 = CORNER1, 6 = CORNER2, 7 = CORNER3, 8 = CORNER4" << "\n";
1791       return 1;
1792     }
1793
1794     Aspect_GradientFillMethod aMethod = Aspect_GradientFillMethod(aType);
1795
1796     Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1797     V3dView->SetBgGradientStyle( aMethod, 1 );
1798   }
1799
1800   return 0;
1801 }
1802
1803 //==============================================================================
1804 //function : VSetColorBg
1805 //purpose  : Set color background
1806 //==============================================================================
1807 static int VSetColorBg(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1808 {
1809   if (argc != 4 )
1810   {
1811     di << "Usage : " << argv[0] << " R G B : Set color background" << "\n";
1812     di << "R,G,B = [0..255]" << "\n";
1813     return 1;
1814   }
1815
1816   Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext();
1817   if(AISContext.IsNull())
1818   {
1819     di << "use 'vinit' command before " << argv[0] << "\n";
1820     return 1;
1821   }
1822   if (argc == 4)
1823   {
1824
1825     Standard_Real R = Draw::Atof(argv[1])/255.;
1826     Standard_Real G = Draw::Atof(argv[2])/255.;
1827     Standard_Real B = Draw::Atof(argv[3])/255.;
1828     Quantity_Color aColor(R,G,B,Quantity_TOC_RGB);
1829
1830     Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1831     V3dView->SetBackgroundColor( aColor );
1832     V3dView->Update();
1833   }
1834
1835   return 0;
1836 }
1837
1838 //==============================================================================
1839 //function : VScale
1840 //purpose  : View Scaling
1841 //==============================================================================
1842
1843 static int VScale(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1844 {
1845   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1846   if ( V3dView.IsNull() ) return 1;
1847
1848   if ( argc != 4 ) {
1849     di << argv[0] << "Invalid number of arguments" << "\n";
1850     return 1;
1851   }
1852   V3dView->SetAxialScale( Draw::Atof(argv[1]),  Draw::Atof(argv[2]),  Draw::Atof(argv[3]) );
1853   return 0;
1854 }
1855 //==============================================================================
1856 //function : VTestZBuffTrihedron
1857 //purpose  : Displays a V3d_ZBUFFER'ed or V3d_WIREFRAME'd trihedron
1858 //==============================================================================
1859
1860 static int VTestZBuffTrihedron(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1861 {
1862   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1863   if ( V3dView.IsNull() ) return 1;
1864
1865   V3dView->ZBufferTriedronSetup();
1866
1867   if ( argc == 1 ) {
1868     // Set up default trihedron parameters
1869     V3dView->TriedronDisplay( Aspect_TOTP_LEFT_LOWER, Quantity_NOC_WHITE, 0.1, V3d_ZBUFFER );
1870   } else
1871   if ( argc == 7 )
1872   {
1873     Aspect_TypeOfTriedronPosition aPosition = Aspect_TOTP_LEFT_LOWER;
1874     const char* aPosType = argv[1];
1875
1876     if ( strcmp(aPosType, "center") == 0 )
1877     {
1878       aPosition = Aspect_TOTP_CENTER;
1879     } else
1880     if (strcmp(aPosType, "left_lower") == 0)
1881     {
1882       aPosition = Aspect_TOTP_LEFT_LOWER;
1883     } else
1884     if (strcmp(aPosType, "left_upper") == 0)
1885     {
1886       aPosition = Aspect_TOTP_LEFT_UPPER;
1887     } else
1888     if (strcmp(aPosType, "right_lower") == 0)
1889     {
1890       aPosition = Aspect_TOTP_RIGHT_LOWER;
1891     } else
1892     if (strcmp(aPosType, "right_upper") == 0)
1893     {
1894       aPosition = Aspect_TOTP_RIGHT_UPPER;
1895     } else
1896     {
1897       di << argv[1] << " Invalid type of alignment"  << "\n";
1898       di << "Must be one of [ center, left_lower,"   << "\n";
1899       di << "left_upper, right_lower, right_upper ]" << "\n";
1900       return 1;
1901     }
1902
1903     Standard_Real R = Draw::Atof(argv[2])/255.;
1904     Standard_Real G = Draw::Atof(argv[3])/255.;
1905     Standard_Real B = Draw::Atof(argv[4])/255.;
1906     Quantity_Color aColor(R, G, B, Quantity_TOC_RGB);
1907
1908     Standard_Real aScale = Draw::Atof(argv[5]);
1909
1910     if( aScale <= 0.0 )
1911     {
1912       di << argv[5] << " Invalid value. Must be > 0" << "\n";
1913       return 1;
1914     }
1915
1916     V3d_TypeOfVisualization aPresentation = V3d_ZBUFFER;
1917     const char* aPresType = argv[6];
1918
1919     if ( strcmp(aPresType, "wireframe") == 0 )
1920     {
1921       aPresentation = V3d_WIREFRAME;
1922     } else
1923     if (strcmp(aPresType, "zbuffer") == 0)
1924     {
1925       aPresentation = V3d_ZBUFFER;
1926     } else
1927     {
1928       di << argv[6] << " Invalid type of visualization" << "\n";
1929       di << "Must be one of [ wireframe, zbuffer ]"     << "\n";
1930       return 1;
1931     }
1932
1933     V3dView->TriedronDisplay( aPosition, aColor.Name(), aScale, aPresentation );
1934
1935   } else
1936   {
1937     di << argv[0] << " Invalid number of arguments" << "\n";
1938     return 1;
1939   }
1940
1941   V3dView->ZFitAll();
1942
1943   return 0;
1944 }
1945
1946 //==============================================================================
1947 //function : VRotate
1948 //purpose  : Camera Rotating
1949 //==============================================================================
1950
1951 static int VRotate( Draw_Interpretor& di, Standard_Integer argc, const char** argv ) {
1952   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1953   if ( V3dView.IsNull() ) {
1954     return 1;
1955   }
1956
1957   if ( argc == 4 ) {
1958     V3dView->Rotate( Draw::Atof(argv[1]), Draw::Atof(argv[2]), Draw::Atof(argv[3]) );
1959     return 0;
1960   } else if ( argc == 7 ) {
1961     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]) );
1962     return 0;
1963   } else {
1964     di << argv[0] << " Invalid number of arguments" << "\n";
1965     return 1;
1966   }
1967 }
1968
1969 //==============================================================================
1970 //function : VZoom
1971 //purpose  : View zoom in / out (relative to current zoom)
1972 //==============================================================================
1973
1974 static int VZoom( Draw_Interpretor& di, Standard_Integer argc, const char** argv ) {
1975   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1976   if ( V3dView.IsNull() ) {
1977     return 1;
1978   }
1979
1980   if ( argc == 2 ) {
1981     Standard_Real coef = Draw::Atof(argv[1]);
1982     if ( coef <= 0.0 ) {
1983       di << argv[1] << "Invalid value" << "\n";
1984       return 1;
1985     }
1986     V3dView->SetZoom( Draw::Atof(argv[1]) );
1987     return 0;
1988   } else {
1989     di << argv[0] << " Invalid number of arguments" << "\n";
1990     return 1;
1991   }
1992 }
1993
1994 //==============================================================================
1995 //function : VPan
1996 //purpose  : View panning (in pixels)
1997 //==============================================================================
1998
1999 static int VPan( Draw_Interpretor& di, Standard_Integer argc, const char** argv ) {
2000   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
2001   if ( V3dView.IsNull() ) return 1;
2002
2003   if ( argc == 3 ) {
2004     V3dView->Pan( Draw::Atoi(argv[1]), Draw::Atoi(argv[2]) );
2005     return 0;
2006   } else {
2007     di << argv[0] << " Invalid number of arguments" << "\n";
2008     return 1;
2009   }
2010 }
2011
2012
2013 //==============================================================================
2014 //function : VExport
2015 //purpose  : Export the view to a vector graphic format (PS, EMF, PDF)
2016 //==============================================================================
2017
2018 static int VExport(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
2019 {
2020   Handle(V3d_View) V3dView = ViewerTest::CurrentView();
2021   if (V3dView.IsNull())
2022     return 1;
2023
2024   if (argc == 1)
2025   {
2026     std::cout << "Usage: " << argv[0] << " Filename [Format]\n";
2027     return 1;
2028   }
2029
2030   Graphic3d_ExportFormat anExpFormat = Graphic3d_EF_PDF;
2031   TCollection_AsciiString aFormatStr;
2032
2033   TCollection_AsciiString aFileName (argv[1]);
2034   Standard_Integer aLen = aFileName.Length();
2035
2036   if (argc > 2)
2037   {
2038     aFormatStr = TCollection_AsciiString (argv[2]);
2039   }
2040   else if (aLen >= 4)
2041   {
2042     if (aFileName.Value (aLen - 2) == '.')
2043     {
2044       aFormatStr = aFileName.SubString (aLen - 1, aLen);
2045     }
2046     else if (aFileName.Value (aLen - 3) == '.')
2047     {
2048       aFormatStr = aFileName.SubString (aLen - 2, aLen);
2049     }
2050     else
2051     {
2052       std::cout << "Export format couln't be detected from filename '" << argv[1] << "'\n";
2053       return 1;
2054     }
2055   }
2056   else
2057   {
2058     std::cout << "Export format couln't be detected from filename '" << argv[1] << "'\n";
2059     return 1;
2060   }
2061
2062   aFormatStr.UpperCase();
2063   if (aFormatStr == "PS")
2064     anExpFormat = Graphic3d_EF_PostScript;
2065   else if (aFormatStr == "EPS")
2066     anExpFormat = Graphic3d_EF_EnhPostScript;
2067   else if (aFormatStr == "TEX")
2068     anExpFormat = Graphic3d_EF_TEX;
2069   else if (aFormatStr == "PDF")
2070     anExpFormat = Graphic3d_EF_PDF;
2071   else if (aFormatStr == "SVG")
2072     anExpFormat = Graphic3d_EF_SVG;
2073   else if (aFormatStr == "PGF")
2074     anExpFormat = Graphic3d_EF_PGF;
2075   else if (aFormatStr == "EMF")
2076     anExpFormat = Graphic3d_EF_EMF;
2077   else
2078   {
2079     std::cout << "Invalid export format '" << aFormatStr << "'\n";
2080     return 1;
2081   }
2082
2083   try {
2084     if (!V3dView->View()->Export (argv[1], anExpFormat))
2085     {
2086       di << "Error: export of image to " << aFormatStr << " failed!\n";
2087     }
2088   }
2089   catch (Standard_Failure)
2090   {
2091     di << "Error: export of image to " << aFormatStr << " failed";
2092     di << " (exception: " << Standard_Failure::Caught()->GetMessageString() << ")";
2093   }
2094   return 0;
2095 }
2096
2097 //==============================================================================
2098 //function : VColorScale
2099 //purpose  : representation color scale
2100 //==============================================================================
2101 #include <V3d_ColorScale.hxx>
2102
2103 static int VColorScale (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
2104 {
2105   if ( argc != 1 && argc != 4 && argc != 5 && argc != 6 && argc != 8 )
2106   {
2107     di << "Usage : " << argv[0] << " [RangeMin = 0 RangeMax = 100 Intervals = 10 HeightFont = 16 Position = Right X = 0 Y = 0]  " << "\n";
2108     return 1;
2109   }
2110
2111   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
2112   if(aContext.IsNull()) {
2113     di << argv[0] << " ERROR : use 'vinit' command before " << "\n";
2114     return -1;
2115   }
2116
2117   Standard_Real minRange = 0. , maxRange = 100. ;
2118
2119   Standard_Integer numIntervals = 10 ;
2120   Standard_Integer textHeight = 16;
2121   Aspect_TypeOfColorScalePosition position = Aspect_TOCSP_RIGHT;
2122   Standard_Real X = 0., Y = 0. ;
2123
2124   if ( argc < 9 )
2125   {
2126      if( argc > 3 )
2127      {
2128        minRange = Draw::Atof( argv[1] );
2129        maxRange = Draw::Atof( argv[2] );
2130        numIntervals = Draw::Atoi( argv[3] );
2131      }
2132      if ( argc > 4 )
2133        textHeight = Draw::Atoi( argv[4] );
2134      if ( argc > 5 )
2135        position = (Aspect_TypeOfColorScalePosition)Draw::Atoi( argv[5] );
2136      if ( argc > 7 )
2137      {
2138        X = Draw::Atof( argv[6] );
2139        Y = Draw::Atof( argv[7] );
2140      }
2141   }
2142   Handle(V3d_View) curView = ViewerTest::CurrentView( );
2143   if ( curView.IsNull( ) )
2144     return 1;
2145   Handle(Aspect_ColorScale) aCSV = curView->ColorScale( );
2146   Handle(V3d_ColorScale) aCS = ( Handle( V3d_ColorScale )::DownCast( aCSV ) );
2147   if( ! aCS.IsNull( ) )
2148   {
2149     aCS->SetPosition( X , Y );
2150     aCS->SetHeight( 0.95) ;
2151     aCS->SetTextHeight( textHeight );
2152     aCS->SetRange( minRange , maxRange );
2153     aCS->SetNumberOfIntervals( numIntervals );
2154     aCS->SetLabelPosition( position );
2155     if( !curView->ColorScaleIsDisplayed() )
2156       curView->ColorScaleDisplay( );
2157   }
2158   return 0;
2159 }
2160
2161 //==============================================================================
2162 //function : VGraduatedTrihedron
2163 //purpose  : Displays a graduated trihedron
2164 //==============================================================================
2165
2166 static void AddMultibyteString (TCollection_ExtendedString &name, const char *arg)
2167 {
2168   const char *str = arg;
2169   while (*str)
2170   {
2171     unsigned short c1 = *str++;
2172     unsigned short c2 = *str++;
2173     if (!c1 || !c2) break;
2174     name += (Standard_ExtCharacter)((c1 << 8) | c2);
2175   }
2176 }
2177
2178 static int VGraduatedTrihedron(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
2179 {
2180   // Check arguments
2181   if (argc != 2 && argc < 5)
2182   {
2183     di<<"Error: "<<argv[0]<<" - invalid number of arguments\n";
2184     di<<"Usage: type help "<<argv[0]<<"\n";
2185     return 1; //TCL_ERROR
2186   }
2187
2188   Handle(V3d_View) aV3dView = ViewerTest::CurrentView();
2189
2190   // Create 3D view if it doesn't exist
2191   if ( aV3dView.IsNull() )
2192   {
2193     ViewerTest::ViewerInit();
2194     aV3dView = ViewerTest::CurrentView();
2195     if( aV3dView.IsNull() )
2196     {
2197       di << "Error: Cannot create a 3D view\n";
2198       return 1; //TCL_ERROR
2199     }
2200   }
2201
2202   // Erase (==0) or display (!=0)
2203   const int display = Draw::Atoi(argv[1]);
2204
2205   if (display)
2206   {
2207     // Text font
2208     TCollection_AsciiString font;
2209     if (argc < 6)
2210       font.AssignCat("Courier");
2211     else
2212       font.AssignCat(argv[5]);
2213
2214     // Text is multibyte
2215     const Standard_Boolean isMultibyte = (argc < 7)? Standard_False : (Draw::Atoi(argv[6]) != 0);
2216
2217     // Set axis names
2218     TCollection_ExtendedString xname, yname, zname;
2219     if (argc >= 5)
2220     {
2221       if (isMultibyte)
2222       {
2223         AddMultibyteString(xname, argv[2]);
2224         AddMultibyteString(yname, argv[3]);
2225         AddMultibyteString(zname, argv[4]);
2226       }
2227       else
2228       {
2229         xname += argv[2];
2230         yname += argv[3];
2231         zname += argv[4];
2232       }
2233     }
2234     else
2235     {
2236       xname += "X (mm)";
2237       yname += "Y (mm)";
2238       zname += "Z (mm)";
2239     }
2240
2241     aV3dView->GraduatedTrihedronDisplay(xname, yname, zname,
2242                                         Standard_True/*xdrawname*/, Standard_True/*ydrawname*/, Standard_True/*zdrawname*/,
2243                                         Standard_True/*xdrawvalues*/, Standard_True/*ydrawvalues*/, Standard_True/*zdrawvalues*/,
2244                                         Standard_True/*drawgrid*/,
2245                                         Standard_True/*drawaxes*/,
2246                                         5/*nbx*/, 5/*nby*/, 5/*nbz*/,
2247                                         10/*xoffset*/, 10/*yoffset*/, 10/*zoffset*/,
2248                                         30/*xaxisoffset*/, 30/*yaxisoffset*/, 30/*zaxisoffset*/,
2249                                         Standard_True/*xdrawtickmarks*/, Standard_True/*ydrawtickmarks*/, Standard_True/*zdrawtickmarks*/,
2250                                         10/*xtickmarklength*/, 10/*ytickmarklength*/, 10/*ztickmarklength*/,
2251                                         Quantity_NOC_WHITE/*gridcolor*/,
2252                                         Quantity_NOC_RED/*xnamecolor*/,Quantity_NOC_GREEN/*ynamecolor*/,Quantity_NOC_BLUE1/*znamecolor*/,
2253                                         Quantity_NOC_RED/*xcolor*/,Quantity_NOC_GREEN/*ycolor*/,Quantity_NOC_BLUE1/*zcolor*/,font);
2254   }
2255   else
2256     aV3dView->GraduatedTrihedronErase();
2257
2258   ViewerTest::GetAISContext()->UpdateCurrentViewer();
2259   aV3dView->Redraw();
2260
2261   return 0;
2262 }
2263
2264 //==============================================================================
2265 //function : VPrintView
2266 //purpose  : Test printing algorithm, print the view to image file with given
2267 //           width and height. Printing implemented only for WNT.
2268 //==============================================================================
2269 static int VPrintView (Draw_Interpretor& di, Standard_Integer argc,
2270                        const char** argv)
2271 {
2272 #ifndef WNT
2273   di << "Printing implemented only for wnt!\n";
2274   return 1;
2275 #else
2276
2277   Handle(AIS_InteractiveContext) aContextAIS = NULL;
2278   Handle(V3d_View) aView = NULL;
2279   aContextAIS = ViewerTest::GetAISContext();
2280   if (!aContextAIS.IsNull())
2281   {
2282     const Handle(V3d_Viewer)& Vwr = aContextAIS->CurrentViewer();
2283     Vwr->InitActiveViews();
2284     if(Vwr->MoreActiveViews())
2285       aView = Vwr->ActiveView();
2286   }
2287
2288   // check for errors
2289   if (aView.IsNull())
2290   {
2291     di << "Call vinit before!\n";
2292     return 1;
2293   }
2294   else if (argc < 4)
2295   {
2296     di << "Use: " << argv[0];
2297     di << " width height filename [print algo=0]\n";
2298     di << "width, height of the intermediate buffer for operation\n";
2299     di << "algo : {0|1}\n";
2300     di << "        0 - stretch algorithm\n";
2301     di << "        1 - tile algorithm\n";
2302     di << "test printing algorithms into an intermediate buffer\n";
2303     di << "with saving output to an image file\n";
2304     return 1;
2305   }
2306
2307   // get the input params
2308   Standard_Integer aWidth  = Draw::Atoi (argv[1]);
2309   Standard_Integer aHeight = Draw::Atoi (argv[2]);
2310   Standard_Integer aMode   = 0;
2311   TCollection_AsciiString aFileName = TCollection_AsciiString (argv[3]);
2312   if (argc==5)
2313     aMode = Draw::Atoi (argv[4]);
2314
2315   // check the input parameters
2316   if (aWidth <= 0 || aHeight <= 0)
2317   {
2318     di << "Width and height must be positive values!\n";
2319     return 1;
2320   }
2321   if (aMode != 0 && aMode != 1)
2322     aMode = 0;
2323
2324   // define compatible bitmap
2325   HDC anDC = CreateCompatibleDC(0);
2326   BITMAPINFO aBitmapData;
2327   memset (&aBitmapData, 0, sizeof (BITMAPINFOHEADER));
2328   aBitmapData.bmiHeader.biSize          = sizeof (BITMAPINFOHEADER);
2329   aBitmapData.bmiHeader.biWidth         = aWidth ;
2330   aBitmapData.bmiHeader.biHeight        = aHeight;
2331   aBitmapData.bmiHeader.biPlanes        = 1;
2332   aBitmapData.bmiHeader.biBitCount      = 24;
2333   aBitmapData.bmiHeader.biXPelsPerMeter = 0;
2334   aBitmapData.bmiHeader.biYPelsPerMeter = 0;
2335   aBitmapData.bmiHeader.biClrUsed       = 0;
2336   aBitmapData.bmiHeader.biClrImportant  = 0;
2337   aBitmapData.bmiHeader.biCompression   = BI_RGB;
2338   aBitmapData.bmiHeader.biSizeImage     = 0;
2339
2340   // Create Device Independent Bitmap
2341   void* aBitsOut = NULL;
2342   HBITMAP aMemoryBitmap = CreateDIBSection (anDC, &aBitmapData, DIB_RGB_COLORS,
2343                                             &aBitsOut, NULL, 0);
2344   HGDIOBJ anOldBitmap   = SelectObject(anDC, aMemoryBitmap);
2345
2346   Standard_Boolean isSaved = Standard_False, isPrinted = Standard_False;
2347   if (aBitsOut != NULL)
2348   {
2349     if (aMode == 0)
2350       isPrinted = aView->Print(anDC,1,1,0,Aspect_PA_STRETCH);
2351     else
2352       isPrinted = aView->Print(anDC,1,1,0,Aspect_PA_TILE);
2353
2354     // succesfully printed into an intermediate buffer
2355     if (isPrinted)
2356     {
2357       Image_PixMap aWrapper;
2358       aWrapper.InitWrapper (Image_PixMap::ImgBGR, (Standard_Byte* )aBitsOut, aWidth, aHeight, aWidth * 3 + aWidth % 4);
2359       aWrapper.SetTopDown (false);
2360
2361       Image_AlienPixMap anImageBitmap;
2362       anImageBitmap.InitCopy (aWrapper);
2363       isSaved = anImageBitmap.Save (aFileName);
2364     }
2365     else
2366     {
2367       di << "Print operation failed due to printing errors or\n";
2368       di << "insufficient memory available\n";
2369       di << "Please, try to use smaller dimensions for this test\n";
2370       di << "command, as it allocates intermediate buffer for storing\n";
2371       di << "the result\n";
2372     }
2373   }
2374   else
2375   {
2376     di << "Can't allocate memory for intermediate buffer\n";
2377     di << "Please use smaller dimensions\n";
2378   }
2379
2380   if (aMemoryBitmap)
2381   {
2382     SelectObject (anDC, anOldBitmap);
2383     DeleteObject (aMemoryBitmap);
2384     DeleteDC(anDC);
2385   }
2386
2387   if (!isSaved)
2388   {
2389     di << "Save to file operation failed. This operation may fail\n";
2390     di << "if you don't have enough available memory, then you can\n";
2391     di << "use smaller dimensions for the output file\n";
2392     return 1;
2393   }
2394
2395   return 0;
2396
2397 #endif
2398 }
2399
2400 //==============================================================================
2401 //function : VZLayer
2402 //purpose  : Test z layer operations for v3d viewer
2403 //==============================================================================
2404 static int VZLayer (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
2405 {
2406   Handle(AIS_InteractiveContext) aContextAIS = ViewerTest::GetAISContext ();
2407   if (aContextAIS.IsNull())
2408   {
2409     di << "Call vinit before!\n";
2410     return 1;
2411   }
2412   else if (argc < 2)
2413   {
2414     di << "Use: vzlayer " << argv[0];
2415     di << " add/del/get [id]\n";
2416     di << " add - add new z layer to viewer and print its id\n";
2417     di << " del - del z layer by its id\n";
2418     di << " get - print sequence of z layers in increasing order of their overlay level\n";
2419     di << "id - the layer identificator value defined when removing z layer\n";
2420     return 1;
2421   }
2422
2423   const Handle(V3d_Viewer)& aViewer = aContextAIS->CurrentViewer();
2424   if (aViewer.IsNull())
2425   {
2426     di << "No active viewer!\n";
2427     return 1;
2428   }
2429
2430   // perform operation
2431   TCollection_AsciiString anOp = TCollection_AsciiString (argv[1]);
2432   if (anOp == "add")
2433   {
2434     Standard_Integer aNewId;
2435     if (!aViewer->AddZLayer (aNewId))
2436     {
2437       di << "Impossible to add new z layer!\n";
2438       return 1;
2439     }
2440
2441     di << "New z layer added with index: " << aNewId << "\n";
2442   }
2443   else if (anOp == "del")
2444   {
2445     if (argc < 3)
2446     {
2447       di << "Please also provide as argument id of z layer to remove\n";
2448       return 1;
2449     }
2450
2451     Standard_Integer aDelId = Draw::Atoi (argv[2]);
2452     if (!aViewer->RemoveZLayer (aDelId))
2453     {
2454       di << "Impossible to remove the z layer or invalid id!\n";
2455       return 1;
2456     }
2457
2458     di << "Z layer " << aDelId << " has been removed\n";
2459   }
2460   else if (anOp == "get")
2461   {
2462     TColStd_SequenceOfInteger anIds;
2463     aViewer->GetAllZLayers (anIds);
2464     for (Standard_Integer aSeqIdx = 1; aSeqIdx <= anIds.Length(); aSeqIdx++)
2465     {
2466       di << anIds.Value (aSeqIdx) << " ";
2467     }
2468
2469     di << "\n";
2470   }
2471   else
2472   {
2473     di << "Invalid operation, please use { add / del / get }\n";
2474     return 1;
2475   }
2476
2477   return 0;
2478 }
2479
2480 DEFINE_STANDARD_HANDLE(V3d_TextItem, Visual3d_LayerItem)
2481
2482 // this class provides a presentation of text item in v3d view under-/overlayer
2483 class V3d_TextItem : public Visual3d_LayerItem
2484 {
2485 public:
2486
2487   // CASCADE RTTI
2488   DEFINE_STANDARD_RTTI(V3d_TextItem)
2489
2490   // constructor
2491   Standard_EXPORT V3d_TextItem(const TCollection_AsciiString& theText,
2492                                const Standard_Real theX1,
2493                                const Standard_Real theY1,
2494                                const Standard_Real theHeight,
2495                                const TCollection_AsciiString& theFontName,
2496                                const Quantity_Color& theColor,
2497                                const Quantity_Color& theSubtitleColor,
2498                                const Aspect_TypeOfDisplayText& theTypeOfDisplay,
2499                                const Handle(Visual3d_Layer)& theLayer);
2500
2501   // redraw method
2502   Standard_EXPORT void RedrawLayerPrs();
2503
2504 private:
2505
2506   Standard_Real            myX1;
2507   Standard_Real            myY1;
2508   Standard_Real            myHeight;
2509   TCollection_AsciiString  myText;
2510   TCollection_AsciiString  myFontName;
2511   Quantity_Color           myColor;
2512   Quantity_Color           mySubtitleColor;
2513   Aspect_TypeOfDisplayText myType;
2514   Handle(Visual3d_Layer)   myLayer;
2515
2516 };
2517
2518 IMPLEMENT_STANDARD_HANDLE(V3d_TextItem, Visual3d_LayerItem)
2519 IMPLEMENT_STANDARD_RTTIEXT(V3d_TextItem, Visual3d_LayerItem)
2520
2521 // create and add to display the text item
2522 V3d_TextItem::V3d_TextItem (const TCollection_AsciiString& theText,
2523                             const Standard_Real theX1,
2524                             const Standard_Real theY1,
2525                             const Standard_Real theHeight,
2526                             const TCollection_AsciiString& theFontName,
2527                             const Quantity_Color& theColor,
2528                             const Quantity_Color& theSubtitleColor,
2529                             const Aspect_TypeOfDisplayText& theTypeOfDisplay,
2530                             const Handle(Visual3d_Layer)& theLayer)
2531  : myX1 (theX1), myY1 (theY1),
2532    myText (theText),
2533    myHeight (theHeight),
2534    myLayer (theLayer),
2535    myColor (theColor),
2536    mySubtitleColor (theSubtitleColor),
2537    myType (theTypeOfDisplay),
2538    myFontName (theFontName)
2539 {
2540   if (!myLayer.IsNull ())
2541     myLayer->AddLayerItem (this);
2542 }
2543
2544 // render item
2545 void V3d_TextItem::RedrawLayerPrs ()
2546 {
2547   if (myLayer.IsNull ())
2548     return;
2549
2550   myLayer->SetColor (myColor);
2551   myLayer->SetTextAttributes (myFontName.ToCString (), myType, mySubtitleColor);
2552   myLayer->DrawText (myText.ToCString (), myX1, myY1, myHeight);
2553 }
2554
2555 DEFINE_STANDARD_HANDLE(V3d_LineItem, Visual3d_LayerItem)
2556
2557 // The Visual3d_LayerItem line item for "vlayerline" command
2558 // it provides a presentation of line with user-defined
2559 // linewidth, linetype and transparency.
2560 class V3d_LineItem : public Visual3d_LayerItem
2561 {
2562 public:
2563   // CASCADE RTTI
2564   DEFINE_STANDARD_RTTI(V3d_LineItem)
2565
2566   // constructor
2567   Standard_EXPORT V3d_LineItem(Standard_Real X1, Standard_Real Y1,
2568                                Standard_Real X2, Standard_Real Y2,
2569                                V3d_LayerMgrPointer theLayerMgr,
2570                                Aspect_TypeOfLine theType = Aspect_TOL_SOLID,
2571                                Standard_Real theWidth    = 0.5,
2572                                Standard_Real theTransp   = 1.0);
2573
2574   // redraw method
2575   Standard_EXPORT   void RedrawLayerPrs();
2576
2577 private:
2578
2579   Standard_Real       myX1, myY1, myX2, myY2;
2580   Standard_Real       myWidth;
2581   Standard_Real       myTransparency;
2582   Aspect_TypeOfLine   myType;
2583   V3d_LayerMgrPointer myLayerMgr;
2584 };
2585
2586 IMPLEMENT_STANDARD_HANDLE(V3d_LineItem, Visual3d_LayerItem)
2587 IMPLEMENT_STANDARD_RTTIEXT(V3d_LineItem, Visual3d_LayerItem)
2588
2589 // default constructor for line item
2590 V3d_LineItem::V3d_LineItem(Standard_Real X1, Standard_Real Y1,
2591                            Standard_Real X2, Standard_Real Y2,
2592                            V3d_LayerMgrPointer theLayerMgr,
2593                            Aspect_TypeOfLine theType,
2594                            Standard_Real theWidth,
2595                            Standard_Real theTransp) :
2596   myX1(X1), myY1(Y1), myX2(X2), myY2(Y2), myLayerMgr(theLayerMgr),
2597   myType(theType), myWidth(theWidth), myTransparency(theTransp)
2598 {
2599   if (myLayerMgr && !myLayerMgr->Overlay().IsNull())
2600     myLayerMgr->Overlay()->AddLayerItem (this);
2601 }
2602
2603 // render line
2604 void V3d_LineItem::RedrawLayerPrs ()
2605 {
2606   Handle (Visual3d_Layer) aOverlay;
2607
2608   if (myLayerMgr)
2609     aOverlay = myLayerMgr->Overlay();
2610
2611   if (!aOverlay.IsNull())
2612   {
2613     Quantity_Color aColor(1.0, 0, 0, Quantity_TOC_RGB);
2614     aOverlay->SetColor(aColor);
2615     aOverlay->SetTransparency((Standard_ShortReal)myTransparency);
2616     aOverlay->SetLineAttributes((Aspect_TypeOfLine)myType, myWidth);
2617     aOverlay->BeginPolyline();
2618     aOverlay->AddVertex(myX1, myY1);
2619     aOverlay->AddVertex(myX2, myY2);
2620     aOverlay->ClosePrimitive();
2621   }
2622 }
2623
2624 //=============================================================================
2625 //function : VLayerLine
2626 //purpose  : Draws line in the v3d view layer with given attributes: linetype,
2627 //         : linewidth, transparency coefficient
2628 //============================================================================
2629 static int VLayerLine(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
2630 {
2631   // get the active view
2632   Handle(V3d_View) aView = ViewerTest::CurrentView();
2633   if (aView.IsNull())
2634   {
2635     di << "Call vinit before!\n";
2636     return 1;
2637   }
2638   else if (argc < 5)
2639   {
2640     di << "Use: " << argv[0];
2641     di << " x1 y1 x2 y2 [linewidth = 0.5] [linetype = 0] [transparency = 1]\n";
2642     di << " linetype : { 0 | 1 | 2 | 3 } \n";
2643     di << "              0 - solid  \n";
2644     di << "              1 - dashed \n";
2645     di << "              2 - dot    \n";
2646     di << "              3 - dashdot\n";
2647     di << " transparency : { 0.0 - 1.0 } \n";
2648     di << "                  0.0 - transparent\n";
2649     di << "                  1.0 - visible    \n";
2650     return 1;
2651   }
2652
2653   // get the input params
2654   Standard_Real X1 = Draw::Atof(argv[1]);
2655   Standard_Real Y1 = Draw::Atof(argv[2]);
2656   Standard_Real X2 = Draw::Atof(argv[3]);
2657   Standard_Real Y2 = Draw::Atof(argv[4]);
2658
2659   Standard_Real    aWidth = 0.5;
2660   Standard_Integer aType  = 0;
2661   Standard_Real    aTransparency = 1.0;
2662
2663   // has width
2664   if (argc > 5)
2665     aWidth = Draw::Atof(argv[5]);
2666
2667   // has type
2668   if (argc > 6)
2669      aType = (Standard_Integer) Draw::Atoi(argv[6]);
2670
2671   // has transparency
2672   if (argc > 7)
2673   {
2674     aTransparency = Draw::Atof(argv[7]);
2675     if (aTransparency < 0 || aTransparency > 1.0)
2676       aTransparency = 1.0;
2677   }
2678
2679   // select appropriate line type
2680   Aspect_TypeOfLine aLineType;
2681   switch (aType)
2682   {
2683     case 1:
2684       aLineType = Aspect_TOL_DASH;
2685     break;
2686
2687     case 2:
2688       aLineType = Aspect_TOL_DOT;
2689     break;
2690
2691     case 3:
2692       aLineType = Aspect_TOL_DOTDASH;
2693     break;
2694
2695     default:
2696       aLineType = Aspect_TOL_SOLID;
2697   }
2698
2699   // replace layer manager
2700   Handle(V3d_LayerMgr) aMgr = new V3d_LayerMgr(aView);
2701   aView->SetLayerMgr(aMgr);
2702
2703   // add line item
2704   Handle (V3d_LineItem) anItem = new V3d_LineItem(X1, Y1, X2, Y2,
2705                                                   aMgr.operator->(),
2706                                                   aLineType, aWidth,
2707                                                   aTransparency);
2708
2709   // update view
2710   aView->MustBeResized();
2711   aView->Redraw();
2712
2713   return 0;
2714 }
2715
2716 //=======================================================================
2717 //function : VOverlayText
2718 //purpose  : Test text displaying in view overlay
2719 //=======================================================================
2720 static int VOverlayText (Draw_Interpretor& di, Standard_Integer argc, const char**argv)
2721 {
2722   // get the active view
2723   Handle(V3d_View) aView = ViewerTest::CurrentView();
2724   if (aView.IsNull())
2725   {
2726     di << "No active view. Please call vinit.\n";
2727     return 1;
2728   }
2729   else if (argc < 4 || argc > 13)
2730   {
2731     di << "Use: " << argv[0];
2732     di << " text x y [height] [font_name] [text_color: R G B] [displayType]\n";
2733     di << "[background_color: R G B]\n";
2734     di << "  height - pixel height of the text (default=10.0)\n";
2735     di << "  font_name - name of font (default=courier)\n";
2736     di << "  text_color - R G B values of text color (default=255.0 255.0 255.0)\n";
2737     di << "  display_type = {normal/subtitle/decal/blend}, (default=normal)\n";
2738     di << "  background_color- R G B values used for subtitle and decal text\n";
2739     di << "(default=255.0 255.0 255.0)\n";
2740     return 1;
2741   }
2742
2743   TCollection_AsciiString aText (argv[1]);
2744   Standard_Real aPosX = Draw::Atof(argv[2]);
2745   Standard_Real aPosY = Draw::Atof(argv[3]);
2746   Standard_Real aHeight = (argc >= 5) ? Draw::Atof (argv[4]) : 10.0;
2747
2748   // font name
2749   TCollection_AsciiString aFontName = "Courier";
2750   if (argc >= 6)
2751     aFontName = TCollection_AsciiString (argv[5]);
2752
2753   // text colors
2754   Quantity_Parameter aColorRed   = 1.0;
2755   Quantity_Parameter aColorGreen = 1.0;
2756   Quantity_Parameter aColorBlue  = 1.0;
2757   if (argc >= 9)
2758   {
2759     aColorRed   = Draw::Atof (argv[6])/255.;
2760     aColorGreen = Draw::Atof (argv[7])/255.;
2761     aColorBlue  = Draw::Atof (argv[8])/255.;
2762   }
2763
2764   // display type
2765   TCollection_AsciiString aDispStr;
2766   if (argc >= 10)
2767     aDispStr = TCollection_AsciiString (argv[9]);
2768
2769   Aspect_TypeOfDisplayText aTextType = Aspect_TODT_NORMAL;
2770   if (aDispStr.IsEqual ("subtitle"))
2771     aTextType = Aspect_TODT_SUBTITLE;
2772   else if (aDispStr.IsEqual ("decal"))
2773     aTextType = Aspect_TODT_DEKALE;
2774   else if (aDispStr.IsEqual ("blend"))
2775     aTextType = Aspect_TODT_BLEND;
2776
2777   // subtitle color
2778   Quantity_Parameter aSubRed   = 1.0;
2779   Quantity_Parameter aSubGreen = 1.0;
2780   Quantity_Parameter aSubBlue  = 1.0;
2781   if (argc == 13)
2782   {
2783     aSubRed   = Draw::Atof (argv[10])/255.;
2784     aSubGreen = Draw::Atof (argv[11])/255.;
2785     aSubBlue  = Draw::Atof (argv[12])/255.;
2786   }
2787
2788   // check fo current overlay
2789   Handle(Visual3d_Layer) anOverlay = aView->Viewer()->Viewer()->OverLayer ();
2790   if (anOverlay.IsNull ())
2791   {
2792     Handle(V3d_LayerMgr) aMgr = new V3d_LayerMgr (aView);
2793     anOverlay = aMgr->Overlay ();
2794     aView->SetLayerMgr (aMgr);
2795   }
2796
2797   Quantity_Color aTextColor (aColorRed, aColorGreen,
2798     aColorBlue, Quantity_TOC_RGB);
2799   Quantity_Color aSubtColor (aSubRed, aSubGreen,
2800     aSubBlue, Quantity_TOC_RGB);
2801
2802   // add text item
2803   Handle(V3d_TextItem) anItem = new V3d_TextItem (aText, aPosX, aPosY,
2804     aHeight, aFontName, aTextColor, aSubtColor, aTextType, anOverlay);
2805
2806   // update view
2807   aView->MustBeResized();
2808   aView->Redraw();
2809
2810   return 0;
2811 }
2812
2813 //==============================================================================
2814 //function : VGrid
2815 //purpose  :
2816 //==============================================================================
2817
2818 static int VGrid (Draw_Interpretor& theDI,
2819                   Standard_Integer  theArgNb,
2820                   const char**      theArgVec)
2821 {
2822   // get the active view
2823   Handle(V3d_View)   aView   = ViewerTest::CurrentView();
2824   Handle(V3d_Viewer) aViewer = ViewerTest::GetViewerFromContext();
2825   if (aView.IsNull() || aViewer.IsNull())
2826   {
2827     std::cerr << "No active view. Please call vinit.\n";
2828     return 1;
2829   }
2830
2831   Aspect_GridType     aType = aViewer->GridType();
2832   Aspect_GridDrawMode aMode = aViewer->GridDrawMode();
2833
2834   Standard_Integer anIter = 1;
2835   for (; anIter < theArgNb; ++anIter)
2836   {
2837     const char* aValue = theArgVec[anIter];
2838     if (*aValue == 'r')
2839     {
2840       aType = Aspect_GT_Rectangular;
2841     }
2842     else if (*aValue == 'c')
2843     {
2844       aType = Aspect_GT_Circular;
2845     }
2846     else if (*aValue == 'l')
2847     {
2848       aMode = Aspect_GDM_Lines;
2849     }
2850     else if (*aValue == 'p')
2851     {
2852       aMode = Aspect_GDM_Points;
2853     }
2854     else if (strcmp (aValue, "off" ) == 0)
2855     {
2856       aViewer->DeactivateGrid();
2857       return 0;
2858     }
2859     else
2860     {
2861       break;
2862     }
2863   }
2864
2865   Standard_Integer aTail = (theArgNb - anIter);
2866   if (aTail == 0)
2867   {
2868     aViewer->ActivateGrid (aType, aMode);
2869     return 0;
2870   }
2871   else if (aTail != 2 && aTail != 5)
2872   {
2873     std::cerr << "Incorrect arguments number! Usage:\n"
2874               << "vgrid [off] [Mode={r|c}] [Type={l|p}] [OriginX OriginY [StepX/StepRadius StepY/DivNb RotAngle]]\n";
2875     return 1;
2876   }
2877
2878   Quantity_Length anOriginX, anOriginY;
2879   Quantity_PlaneAngle aRotAngle;
2880   if (aType == Aspect_GT_Rectangular)
2881   {
2882     Quantity_Length aRStepX, aRStepY;
2883     aViewer->RectangularGridValues (anOriginX, anOriginY, aRStepX, aRStepY, aRotAngle);
2884
2885     anOriginX = Draw::Atof (theArgVec[anIter++]);
2886     anOriginY = Draw::Atof (theArgVec[anIter++]);
2887     if (aTail == 5)
2888     {
2889       aRStepX   = Draw::Atof (theArgVec[anIter++]);
2890       aRStepY   = Draw::Atof (theArgVec[anIter++]);
2891       aRotAngle = Draw::Atof (theArgVec[anIter++]);
2892     }
2893     aViewer->SetRectangularGridValues (anOriginX, anOriginY, aRStepX, aRStepY, aRotAngle);
2894     aViewer->ActivateGrid (aType, aMode);
2895   }
2896   else if (aType == Aspect_GT_Circular)
2897   {
2898     Quantity_Length aRadiusStep;
2899     Standard_Integer aDivisionNumber;
2900     aViewer->CircularGridValues (anOriginX, anOriginY, aRadiusStep, aDivisionNumber, aRotAngle);
2901
2902     anOriginX = Draw::Atof (theArgVec[anIter++]);
2903     anOriginY = Draw::Atof (theArgVec[anIter++]);
2904     if (aTail == 5)
2905     {
2906       aRadiusStep     = Draw::Atof (theArgVec[anIter++]);
2907       aDivisionNumber = Draw::Atoi (theArgVec[anIter++]);
2908       aRotAngle       = Draw::Atof (theArgVec[anIter++]);
2909     }
2910
2911     aViewer->SetCircularGridValues (anOriginX, anOriginY, aRadiusStep, aDivisionNumber, aRotAngle);
2912     aViewer->ActivateGrid (aType, aMode);
2913   }
2914
2915   return 0;
2916 }
2917
2918 //==============================================================================
2919 //function : VFps
2920 //purpose  :
2921 //==============================================================================
2922
2923 static int VFps (Draw_Interpretor& theDI,
2924                  Standard_Integer  theArgNb,
2925                  const char**      theArgVec)
2926 {
2927   // get the active view
2928   Handle(V3d_View) aView = ViewerTest::CurrentView();
2929   if (aView.IsNull())
2930   {
2931     std::cerr << "No active view. Please call vinit.\n";
2932     return 1;
2933   }
2934
2935   Standard_Integer aFramesNb = (theArgNb > 1) ? Draw::Atoi(theArgVec[1]) : 100;
2936   if (aFramesNb <= 0)
2937   {
2938     std::cerr << "Incorrect arguments!\n";
2939     return 1;
2940   }
2941
2942   // the time is meaningless for first call
2943   // due to async OpenGl rendering
2944   aView->Redraw();
2945
2946   // redraw view in loop to estimate average values
2947   OSD_Timer aTimer;
2948   aTimer.Start();
2949   for (Standard_Integer anInter = 0; anInter < aFramesNb; ++anInter)
2950   {
2951     aView->Redraw();
2952   }
2953   aTimer.Stop();
2954   Standard_Real aCpu;
2955   const Standard_Real aTime = aTimer.ElapsedTime();
2956   aTimer.OSD_Chronometer::Show (aCpu);
2957
2958   const Standard_Real aFpsAver = Standard_Real(aFramesNb) / aTime;
2959   const Standard_Real aCpuAver = aCpu / Standard_Real(aFramesNb);
2960
2961   // return statistics
2962   theDI << "FPS: " << aFpsAver << "\n"
2963         << "CPU: " << (1000.0 * aCpuAver) << " msec\n";
2964
2965   return 0;
2966 }
2967
2968
2969 //==============================================================================
2970 //function : VVbo
2971 //purpose  :
2972 //==============================================================================
2973
2974 static int VVbo (Draw_Interpretor& theDI,
2975                  Standard_Integer  theArgNb,
2976                  const char**      theArgVec)
2977 {
2978   // get the context
2979   Handle(AIS_InteractiveContext) aContextAIS = ViewerTest::GetAISContext();
2980   if (aContextAIS.IsNull())
2981   {
2982     std::cerr << "No active view. Please call vinit.\n";
2983     return 1;
2984   }
2985
2986   Handle(Graphic3d_GraphicDriver) aDriver = aContextAIS->CurrentViewer()->Driver();
2987
2988   if (aDriver.IsNull())
2989   {
2990     std::cerr << "Graphic driver not available.\n";
2991     return 1;
2992   }
2993
2994   if (theArgNb < 2)
2995   {
2996     //theDI << "VBO: " << aDriver->ToUseVBO() << "\n";
2997     //return 0;
2998     std::cerr << "Wrong number of arguments.\n";
2999     return 1;
3000   }
3001
3002   aDriver->EnableVBO (Draw::Atoi(theArgVec[1]) != 0);
3003   return 0;
3004 }
3005
3006 //==============================================================================
3007 //function : VMemGpu
3008 //purpose  :
3009 //==============================================================================
3010
3011 static int VMemGpu (Draw_Interpretor& theDI,
3012                     Standard_Integer  theArgNb,
3013                     const char**      theArgVec)
3014 {
3015   // get the context
3016   Handle(AIS_InteractiveContext) aContextAIS = ViewerTest::GetAISContext();
3017   if (aContextAIS.IsNull())
3018   {
3019     std::cerr << "No active view. Please call vinit.\n";
3020     return 1;
3021   }
3022
3023   Handle(Graphic3d_GraphicDriver) aDriver = aContextAIS->CurrentViewer()->Driver();
3024
3025   if (aDriver.IsNull())
3026   {
3027     std::cerr << "Graphic driver not available.\n";
3028     return 1;
3029   }
3030
3031   Standard_Size aFreeBytes = 0;
3032   TCollection_AsciiString anInfo;
3033   if (!aDriver->MemoryInfo (aFreeBytes, anInfo))
3034   {
3035     std::cerr << "Information not available.\n";
3036     return 1;
3037   }
3038
3039   if (theArgNb > 1 && *theArgVec[1] == 'f')
3040   {
3041     theDI << Standard_Real (aFreeBytes);
3042   }
3043   else
3044   {
3045     theDI << anInfo;
3046   }
3047
3048   return 0;
3049 }
3050
3051 // ==============================================================================
3052 // function : VReadPixel
3053 // purpose  :
3054 // ==============================================================================
3055 static int VReadPixel (Draw_Interpretor& theDI,
3056                        Standard_Integer  theArgNb,
3057                        const char**      theArgVec)
3058 {
3059   // get the active view
3060   Handle(V3d_View) aView = ViewerTest::CurrentView();
3061   if (aView.IsNull())
3062   {
3063     std::cerr << "No active view. Please call vinit.\n";
3064     return 1;
3065   }
3066   else if (theArgNb < 3)
3067   {
3068     std::cerr << "Usage : " << theArgVec[0] << " xPixel yPixel [{rgb|rgba|depth|hls|rgbf|rgbaf}=rgba] [name]\n";
3069     return 1;
3070   }
3071
3072   Image_PixMap::ImgFormat aFormat     = Image_PixMap::IsBigEndianHost() ? Image_PixMap::ImgRGBA : Image_PixMap::ImgBGRA;
3073   Graphic3d_BufferType    aBufferType = Graphic3d_BT_RGBA;
3074
3075   Standard_Integer aWidth, aHeight;
3076   aView->Window()->Size (aWidth, aHeight);
3077   const Standard_Integer anX = Draw::Atoi (theArgVec[1]);
3078   const Standard_Integer anY = Draw::Atoi (theArgVec[2]);
3079   if (anX < 0 || anX >= aWidth || anY < 0 || anY > aHeight)
3080   {
3081     std::cerr << "Pixel coordinates (" << anX << "; " << anY << ") are out of view (" << aWidth << " x " << aHeight << ")\n";
3082     return 1;
3083   }
3084
3085   Standard_Boolean toShowName = Standard_False;
3086   Standard_Boolean toShowHls  = Standard_False;
3087   for (Standard_Integer anIter = 3; anIter < theArgNb; ++anIter)
3088   {
3089     TCollection_AsciiString aParam (theArgVec[anIter]);
3090     if (TCollection_AsciiString::ISSIMILAR      (aParam, TCollection_AsciiString ("rgb")))
3091     {
3092       aFormat     = Image_PixMap::IsBigEndianHost() ? Image_PixMap::ImgRGB : Image_PixMap::ImgBGR;
3093       aBufferType = Graphic3d_BT_RGB;
3094     }
3095     else if (TCollection_AsciiString::ISSIMILAR (aParam, TCollection_AsciiString ("hls")))
3096     {
3097       aFormat     = Image_PixMap::IsBigEndianHost() ? Image_PixMap::ImgRGB : Image_PixMap::ImgBGR;
3098       aBufferType = Graphic3d_BT_RGB;
3099       toShowHls   = Standard_True;
3100     }
3101     else if (TCollection_AsciiString::ISSIMILAR (aParam, TCollection_AsciiString ("rgbf")))
3102     {
3103       aFormat     = Image_PixMap::ImgRGBF;
3104       aBufferType = Graphic3d_BT_RGB;
3105     }
3106     else if (TCollection_AsciiString::ISSIMILAR (aParam, TCollection_AsciiString ("rgba")))
3107     {
3108       aFormat     = Image_PixMap::IsBigEndianHost() ? Image_PixMap::ImgRGBA : Image_PixMap::ImgBGRA;
3109       aBufferType = Graphic3d_BT_RGBA;
3110     }
3111     else if (TCollection_AsciiString::ISSIMILAR (aParam, TCollection_AsciiString ("rgbaf")))
3112     {
3113       aFormat     = Image_PixMap::ImgRGBAF;
3114       aBufferType = Graphic3d_BT_RGBA;
3115     }
3116     else if (TCollection_AsciiString::ISSIMILAR (aParam, TCollection_AsciiString ("depth")))
3117     {
3118       aFormat     = Image_PixMap::ImgGrayF;
3119       aBufferType = Graphic3d_BT_Depth;
3120     }
3121     else if (TCollection_AsciiString::ISSIMILAR (aParam, TCollection_AsciiString ("name")))
3122     {
3123       toShowName = Standard_True;
3124     }
3125   }
3126
3127   Image_PixMap anImage;
3128   if (!anImage.InitTrash (aFormat, aWidth, aHeight))
3129   {
3130     std::cerr << "Image allocation failed\n";
3131     return 1;
3132   }
3133   else if (!aView->ToPixMap (anImage, aWidth, aHeight, aBufferType))
3134   {
3135     std::cerr << "Image dump failed\n";
3136     return 1;
3137   }
3138
3139   Quantity_Parameter anAlpha;
3140   Quantity_Color aColor = anImage.PixelColor (anX, anY, anAlpha);
3141   if (toShowName)
3142   {
3143     if (aBufferType == Graphic3d_BT_RGBA)
3144     {
3145       theDI << Quantity_Color::StringName (aColor.Name()) << " " << anAlpha << "\n";
3146     }
3147     else
3148     {
3149       theDI << Quantity_Color::StringName (aColor.Name()) << "\n";
3150     }
3151   }
3152   else
3153   {
3154     switch (aBufferType)
3155     {
3156       default:
3157       case Graphic3d_BT_RGB:
3158       {
3159         if (toShowHls)
3160         {
3161           theDI << aColor.Hue() << " " << aColor.Light() << " " << aColor.Saturation() << "\n";
3162         }
3163         else
3164         {
3165           theDI << aColor.Red() << " " << aColor.Green() << " " << aColor.Blue() << "\n";
3166         }
3167         break;
3168       }
3169       case Graphic3d_BT_RGBA:
3170       {
3171         theDI << aColor.Red() << " " << aColor.Green() << " " << aColor.Blue() << " " << anAlpha << "\n";
3172         break;
3173       }
3174       case Graphic3d_BT_Depth:
3175       {
3176         theDI << aColor.Red() << "\n";
3177         break;
3178       }
3179     }
3180   }
3181
3182   return 0;
3183 }
3184
3185 //==============================================================================
3186 //function : VDiffImage
3187 //purpose  : The draw-command compares two images.
3188 //==============================================================================
3189
3190 static int VDiffImage (Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
3191 {
3192   if (theArgNb < 6)
3193   {
3194     theDI << "Not enough arguments.\n";
3195     return 1;
3196   }
3197
3198   // image file names
3199   const char* anImgPathRef = theArgVec[1];
3200   const char* anImgPathNew = theArgVec[2];
3201
3202   // get string tolerance and check its validity
3203   Standard_Real aTolColor = Draw::Atof (theArgVec[3]);
3204   if (aTolColor < 0.0)
3205     aTolColor = 0.0;
3206   if (aTolColor > 1.0)
3207     aTolColor = 1.0;
3208
3209   Standard_Boolean toBlackWhite     = (Draw::Atoi (theArgVec[4]) == 1);
3210   Standard_Boolean isBorderFilterOn = (Draw::Atoi (theArgVec[5]) == 1);
3211
3212   // image file of difference
3213   const char* aDiffImagePath = (theArgNb >= 7) ? theArgVec[6] : NULL;
3214
3215   // compare the images
3216   Image_Diff aComparer;
3217   if (!aComparer.Init (anImgPathRef, anImgPathNew, toBlackWhite))
3218   {
3219     return 1;
3220   }
3221
3222   aComparer.SetColorTolerance (aTolColor);
3223   aComparer.SetBorderFilterOn (isBorderFilterOn);
3224   Standard_Integer aDiffColorsNb = aComparer.Compare();
3225   theDI << aDiffColorsNb << "\n";
3226
3227   // save image of difference
3228   if (aDiffImagePath != NULL)
3229   {
3230     aComparer.SaveDiffImage (aDiffImagePath);
3231   }
3232
3233   return 0;
3234 }
3235
3236 //=======================================================================
3237 //function : VSelect
3238 //purpose  : Emulates different types of selection by mouse:
3239 //           1) single click selection
3240 //           2) selection with rectangle having corners at pixel positions (x1,y1) and (x2,y2)
3241 //           3) selection with polygon having corners at
3242 //           pixel positions (x1,y1),...,(xn,yn)
3243 //           4) any of these selections with shift button pressed
3244 //=======================================================================
3245 static Standard_Integer VSelect (Draw_Interpretor& di,
3246                                  Standard_Integer argc,
3247                                  const char ** argv)
3248 {
3249   if(argc < 3)
3250   {
3251     di << "Usage : " << argv[0] << " x1 y1 [x2 y2 [... xn yn]] [shift_selection = 1|0]" << "\n";
3252     return 1;
3253   }
3254
3255   Handle(AIS_InteractiveContext) myAIScontext = ViewerTest::GetAISContext();
3256   if(myAIScontext.IsNull())
3257   {
3258     di << "use 'vinit' command before " << argv[0] << "\n";
3259     return 1;
3260   }
3261   const Standard_Boolean isShiftSelection = (argc>3 && !(argc%2) && (atoi(argv[argc-1])==1));
3262   Handle(ViewerTest_EventManager) aCurrentEventManager = ViewerTest::CurrentEventManager();
3263   aCurrentEventManager->MoveTo(atoi(argv[1]),atoi(argv[2]));
3264   if(argc <= 4)
3265   {
3266     if(isShiftSelection)
3267       aCurrentEventManager->ShiftSelect();
3268     else
3269       aCurrentEventManager->Select();
3270   }
3271   else if(argc <= 6)
3272   {
3273     if(isShiftSelection)
3274       aCurrentEventManager->ShiftSelect(atoi(argv[1]),atoi(argv[2]),atoi(argv[3]),atoi(argv[4]));
3275     else
3276       aCurrentEventManager->Select(atoi(argv[1]),atoi(argv[2]),atoi(argv[3]),atoi(argv[4]));
3277   }
3278   else
3279   {
3280     Standard_Integer anUpper = 0;
3281
3282     if(isShiftSelection)
3283       anUpper = (argc-1)/2;
3284     else
3285       anUpper = argc/2;
3286     TColgp_Array1OfPnt2d aPolyline(1,anUpper);
3287
3288     for(Standard_Integer i=1;i<=anUpper;++i)
3289       aPolyline.SetValue(i,gp_Pnt2d(atoi(argv[2*i-1]),atoi(argv[2*i])));
3290
3291     if(isShiftSelection)
3292       aCurrentEventManager->ShiftSelect(aPolyline);
3293     else
3294       aCurrentEventManager->Select(aPolyline);
3295   }
3296   return 0;
3297 }
3298
3299 //=======================================================================
3300 //function : VMoveTo
3301 //purpose  : Emulates cursor movement to defined pixel position
3302 //=======================================================================
3303 static Standard_Integer VMoveTo (Draw_Interpretor& di,
3304                                 Standard_Integer argc,
3305                                 const char ** argv)
3306 {
3307   if(argc != 3)
3308   {
3309     di << "Usage : " << argv[0] << " x y" << "\n";
3310     return 1;
3311   }
3312
3313   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
3314   if(aContext.IsNull())
3315   {
3316     di << "use 'vinit' command before " << argv[0] << "\n";
3317     return 1;
3318   }
3319   ViewerTest::CurrentEventManager()->MoveTo(atoi(argv[1]),atoi(argv[2]));
3320   return 0;
3321 }
3322
3323 //=======================================================================
3324 //function : VViewParams
3325 //purpose  : Gets or sets AIS View characteristics
3326 //=======================================================================
3327 static Standard_Integer VViewParams (Draw_Interpretor& di,
3328                                 Standard_Integer argc,
3329                                 const char ** argv)
3330 {
3331   if ( argc != 1 && argc != 13)
3332   {
3333     di << "Usage : " << argv[0] << "\n";
3334     return 1;
3335   }
3336   Handle (V3d_View) anAISView = ViewerTest::CurrentView ();
3337   if ( anAISView.IsNull () )
3338   {
3339     di << "use 'vinit' command before " << argv[0] << "\n";
3340     return 1;
3341   }
3342   if(argc==1){
3343     Quantity_Factor anAISViewScale = anAISView -> V3d_View::Scale ();
3344     Standard_Real anAISViewCenterCoordinateX = 0.0;
3345     Standard_Real anAISViewCenterCoordinateY = 0.0;
3346     anAISView -> V3d_View::Center (anAISViewCenterCoordinateX, anAISViewCenterCoordinateY);
3347     Standard_Real anAISViewProjX = 0.0;
3348     Standard_Real anAISViewProjY = 0.0;
3349     Standard_Real anAISViewProjZ = 0.0;
3350     anAISView -> V3d_View::Proj (anAISViewProjX, anAISViewProjY, anAISViewProjZ);
3351     Standard_Real anAISViewUpX = 0.0;
3352     Standard_Real anAISViewUpY = 0.0;
3353     Standard_Real anAISViewUpZ = 0.0;
3354     anAISView -> V3d_View::Up (anAISViewUpX, anAISViewUpY, anAISViewUpZ);
3355     Standard_Real anAISViewAtX = 0.0;
3356     Standard_Real anAISViewAtY = 0.0;
3357     Standard_Real anAISViewAtZ = 0.0;
3358     anAISView -> V3d_View::At (anAISViewAtX, anAISViewAtY, anAISViewAtZ);
3359     di << "Scale of current view: " << anAISViewScale << "\n";
3360     di << "Center on X : "<< anAISViewCenterCoordinateX << "; on Y: " << anAISViewCenterCoordinateY << "\n";
3361     di << "Proj on X : " << anAISViewProjX << "; on Y: " << anAISViewProjY << "; on Z: " << anAISViewProjZ << "\n";
3362     di << "Up on X : " << anAISViewUpX << "; on Y: " << anAISViewUpY << "; on Z: " << anAISViewUpZ << "\n";
3363     di << "At on X : " << anAISViewAtX << "; on Y: " << anAISViewAtY << "; on Z: " << anAISViewAtZ << "\n";
3364   }
3365   else
3366   {
3367     Quantity_Factor anAISViewScale = atof (argv [1]);
3368     Standard_Real anAISViewCenterCoordinateX = atof (argv [2]);
3369     Standard_Real anAISViewCenterCoordinateY = atof (argv [3]);
3370     Standard_Real anAISViewProjX = atof (argv [4]);
3371     Standard_Real anAISViewProjY = atof (argv [5]);
3372     Standard_Real anAISViewProjZ = atof (argv [6]);
3373     Standard_Real anAISViewUpX = atof (argv [7]);
3374     Standard_Real anAISViewUpY = atof (argv [8]);
3375     Standard_Real anAISViewUpZ = atof (argv [9]);
3376     Standard_Real anAISViewAtX = atof (argv [10]);
3377     Standard_Real anAISViewAtY = atof (argv [11]);
3378     Standard_Real anAISViewAtZ = atof (argv [12]);
3379     anAISView -> V3d_View::SetScale (anAISViewScale);
3380     anAISView -> V3d_View::SetCenter (anAISViewCenterCoordinateX, anAISViewCenterCoordinateY);
3381     anAISView -> V3d_View::SetAt (anAISViewAtX, anAISViewAtY, anAISViewAtZ);
3382     anAISView -> V3d_View::SetProj (anAISViewProjX, anAISViewProjY, anAISViewProjZ);
3383     anAISView -> V3d_View::SetUp (anAISViewUpX, anAISViewUpY, anAISViewUpZ);
3384   }
3385   return 0;
3386 }
3387
3388 //=======================================================================
3389 //function : VChangeSelected
3390 //purpose  : Adds the shape to selection or remove one from it
3391 //=======================================================================
3392 static Standard_Integer VChangeSelected (Draw_Interpretor& di,
3393                                 Standard_Integer argc,
3394                                 const char ** argv)
3395 {
3396   if(argc != 2)
3397   {
3398     di<<"Usage : " << argv[0] << " shape \n";
3399     return 1;
3400   }
3401   //get AIS_Shape:
3402   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
3403   ViewerTest_DoubleMapOfInteractiveAndName& aMap = GetMapOfAIS();
3404   TCollection_AsciiString aName(argv[1]);
3405   Handle(AIS_InteractiveObject) anAISObject;
3406
3407   if(!aMap.IsBound2(aName))
3408   {
3409     di<<"Use 'vdisplay' before";
3410     return 1;
3411   }
3412   else
3413   {
3414     anAISObject = Handle(AIS_InteractiveObject)::DownCast(aMap.Find2(aName));
3415     if(anAISObject.IsNull()){
3416       di<<"No interactive object \n";
3417       return 1;
3418     }
3419
3420     if(aContext->HasOpenedContext())
3421     {
3422       aContext->AddOrRemoveSelected(anAISObject);
3423     }
3424     else
3425     {
3426       aContext->AddOrRemoveCurrentObject(anAISObject);
3427     }
3428   }
3429   return 0;
3430 }
3431
3432 //=======================================================================
3433 //function : VZClipping
3434 //purpose  : Gets or sets ZClipping mode, width and depth
3435 //=======================================================================
3436 static Standard_Integer VZClipping (Draw_Interpretor& di,
3437                                 Standard_Integer argc,
3438                                 const char ** argv)
3439 {
3440   if(argc>4)
3441   {
3442     di << "Usage : " << argv[0] << " [mode] [depth  width]" << "\n"
3443       <<"mode = OFF|BACK|FRONT|SLICE depth = [0..1] width = [0..1]" << "\n";
3444     return -1;
3445   }
3446   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
3447   if(aContext.IsNull())
3448   {
3449     di << "use 'vinit' command before " << argv[0] << "\n";
3450     return 1;
3451   }
3452   Handle(V3d_View) aView = ViewerTest::CurrentView();
3453   V3d_TypeOfZclipping aZClippingMode;
3454   if(argc==1)
3455   {
3456     TCollection_AsciiString aZClippingModeString;
3457     Quantity_Length aDepth, aWidth;
3458     aZClippingMode = aView->ZClipping(aDepth, aWidth);
3459     switch (aZClippingMode)
3460     {
3461     case V3d_OFF:
3462       aZClippingModeString.Copy("OFF");
3463       break;
3464     case V3d_BACK:
3465       aZClippingModeString.Copy("BACK");
3466       break;
3467     case V3d_FRONT:
3468       aZClippingModeString.Copy("FRONT");
3469       break;
3470     case V3d_SLICE:
3471       aZClippingModeString.Copy("SLICE");
3472       break;
3473     default:
3474       aZClippingModeString.Copy(TCollection_AsciiString(aZClippingMode));
3475       break;
3476     }
3477     di << "ZClippingMode = " << aZClippingModeString.ToCString() << "\n"
3478       << "ZClipping depth = " << aDepth << "\n"
3479       << "ZClipping width = " << aWidth << "\n";
3480   }
3481   else
3482   {
3483     if(argc !=3)
3484     {
3485       Standard_Integer aStatus = 0;
3486       if ( strcmp (argv [1], "OFF") == 0 ) {
3487         aStatus = 1;
3488         aZClippingMode = V3d_OFF;
3489       }
3490       if ( strcmp (argv [1], "BACK") == 0 ) {
3491         aStatus = 1;
3492         aZClippingMode = V3d_BACK;
3493       }
3494       if ( strcmp (argv [1], "FRONT") == 0 ) {
3495         aStatus = 1;
3496         aZClippingMode = V3d_FRONT;
3497       }
3498       if ( strcmp (argv [1], "SLICE") == 0 ) {
3499         aStatus = 1;
3500         aZClippingMode = V3d_SLICE;
3501       }
3502       if (aStatus != 1)
3503       {
3504         di << "Bad mode; Usage : " << argv[0] << " [mode] [depth width]" << "\n"
3505           << "mode = OFF|BACK|FRONT|SLICE depth = [0..1] width = [0..1]" << "\n";
3506         return 1;
3507       }
3508       aView->SetZClippingType(aZClippingMode);
3509     }
3510     if(argc >2)
3511     {
3512       Quantity_Length aDepth = 0., aWidth = 1.;
3513       if(argc == 3)
3514       {
3515         aDepth = atof(argv[1]);
3516         aWidth = atof(argv[2]);
3517       }
3518       else if(argc == 4)
3519       {
3520         aDepth = atof(argv[2]);
3521         aWidth = atof(argv[3]);
3522       }
3523
3524       if(aDepth<0. || aDepth>1.)
3525       {
3526         di << "Bad depth; Usage : " << argv[0] << " [mode] [depth width]" << "\n"
3527         << "mode = OFF|BACK|FRONT|SLICE depth = [0..1] width = [0..1]" << "\n";
3528         return 1;
3529       }
3530       if(aWidth<0. || aWidth>1.)
3531       {
3532         di << "Bad width; Usage : " << argv[0] << " [mode] [depth width]" << "\n"
3533         << "mode = OFF|BACK|FRONT|SLICE depth = [0..1] width = [0..1]" << "\n";
3534         return 1;
3535       }
3536
3537       aView->SetZClippingDepth(aDepth);
3538       aView->SetZClippingWidth(aWidth);
3539     }
3540     aView->Redraw();
3541   }
3542   return 0;
3543 }
3544
3545 //=======================================================================
3546 //function : VNbSelected
3547 //purpose  : Returns number of selected objects
3548 //=======================================================================
3549 static Standard_Integer VNbSelected (Draw_Interpretor& di,
3550                                 Standard_Integer argc,
3551                                 const char ** argv)
3552 {
3553   if(argc != 1)
3554   {
3555     di << "Usage : " << argv[0] << "\n";
3556     return 1;
3557   }
3558   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
3559   if(aContext.IsNull())
3560   {
3561     di << "use 'vinit' command before " << argv[0] << "\n";
3562     return 1;
3563   }
3564   di << aContext->NbSelected() << "\n";
3565   return 0;
3566 }
3567
3568 //=======================================================================
3569 //function : VAntialiasing
3570 //purpose  : Switches altialiasing on or off
3571 //=======================================================================
3572 static Standard_Integer VAntialiasing (Draw_Interpretor& di,
3573                                 Standard_Integer argc,
3574                                 const char ** argv)
3575 {
3576   if(argc > 2)
3577   {
3578     di << "Usage : " << argv[0] << " [1|0]" << "\n";
3579     return 1;
3580   }
3581
3582   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
3583   if(aContext.IsNull())
3584   {
3585     di << "use 'vinit' command before " << argv[0] << "\n";
3586     return 1;
3587   }
3588
3589   Handle(V3d_View) aView = ViewerTest::CurrentView();
3590
3591   if((argc == 2) && (atof(argv[1]) == 0))
3592     aView->SetAntialiasingOff();
3593   else
3594     aView->SetAntialiasingOn();
3595   aView->Update();
3596   return 0;
3597 }
3598
3599 //=======================================================================
3600 //function : VPurgeDisplay
3601 //purpose  : Switches altialiasing on or off
3602 //=======================================================================
3603 static Standard_Integer VPurgeDisplay (Draw_Interpretor& di,
3604                                 Standard_Integer argc,
3605                                 const char ** argv)
3606 {
3607   if (argc > 2)
3608   {
3609     di << "Usage : " << argv[0] << " [CollectorToo = 0|1]" << "\n";
3610     return 1;
3611   }
3612   Standard_Boolean isCollectorToo = Standard_False;
3613   if (argc == 2)
3614   {
3615       isCollectorToo = (atoi(argv [1]) != 0);
3616   }
3617   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
3618   if (aContext.IsNull())
3619   {
3620     di << "use 'vinit' command before " << argv[0] << "\n";
3621     return 1;
3622   }
3623   aContext->CloseAllContexts(Standard_False);
3624   di << aContext->PurgeDisplay(isCollectorToo) << "\n";
3625   return 0;
3626 }
3627
3628 //=======================================================================
3629 //function : VSetViewSize
3630 //purpose  :
3631 //=======================================================================
3632 static Standard_Integer VSetViewSize (Draw_Interpretor& di,
3633                                 Standard_Integer argc,
3634                                 const char ** argv)
3635 {
3636   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
3637   if(aContext.IsNull())
3638   {
3639     di << "use 'vinit' command before " << argv[0] << "\n";
3640     return 1;
3641   }
3642   if(argc != 2)
3643   {
3644     di<<"Usage : " << argv[0] << " Size\n";
3645     return 1;
3646   }
3647   Standard_Real aSize = atof(argv[1]);
3648   if (aSize <= 0.)
3649   {
3650     di<<"Bad Size value  : " << aSize << "\n";
3651     return 1;
3652   }
3653
3654   Handle(V3d_View) aView = ViewerTest::CurrentView();
3655   aView->SetSize(aSize);
3656   return 0;
3657 }
3658
3659 //=======================================================================
3660 //function : VMoveView
3661 //purpose  :
3662 //=======================================================================
3663 static Standard_Integer VMoveView (Draw_Interpretor& di,
3664                                 Standard_Integer argc,
3665                                 const char ** argv)
3666 {
3667   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
3668   if(aContext.IsNull())
3669   {
3670     di << "use 'vinit' command before " << argv[0] << "\n";
3671     return 1;
3672   }
3673   if(argc < 4 || argc > 5)
3674   {
3675     di<<"Usage : " << argv[0] << " Dx Dy Dz [Start = 1|0]\n";
3676     return 1;
3677   }
3678   Standard_Real Dx = atof(argv[1]);
3679   Standard_Real Dy = atof(argv[2]);
3680   Standard_Real Dz = atof(argv[3]);
3681   Standard_Boolean aStart = Standard_True;
3682   if (argc == 5)
3683   {
3684       aStart = (atoi(argv[4]) > 0);
3685   }
3686
3687   Handle(V3d_View) aView = ViewerTest::CurrentView();
3688   aView->Move(Dx,Dy,Dz,aStart);
3689   return 0;
3690 }
3691
3692 //=======================================================================
3693 //function : VTranslateView
3694 //purpose  :
3695 //=======================================================================
3696 static Standard_Integer VTranslateView (Draw_Interpretor& di,
3697                                 Standard_Integer argc,
3698                                 const char ** argv)
3699 {
3700   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
3701   if(aContext.IsNull())
3702   {
3703     di << "use 'vinit' command before " << argv[0] << "\n";
3704     return 1;
3705   }
3706   if(argc < 4 || argc > 5)
3707   {
3708     di<<"Usage : " << argv[0] << " Dx Dy Dz [Start = 1|0]\n";
3709     return 1;
3710   }
3711   Standard_Real Dx = atof(argv[1]);
3712   Standard_Real Dy = atof(argv[2]);
3713   Standard_Real Dz = atof(argv[3]);
3714   Standard_Boolean aStart = Standard_True;
3715   if (argc == 5)
3716   {
3717       aStart = (atoi(argv[4]) > 0);
3718   }
3719
3720   Handle(V3d_View) aView = ViewerTest::CurrentView();
3721   aView->Translate(Dx,Dy,Dz,aStart);
3722   return 0;
3723 }
3724
3725 //=======================================================================
3726 //function : VTurnView
3727 //purpose  :
3728 //=======================================================================
3729 static Standard_Integer VTurnView (Draw_Interpretor& di,
3730                                 Standard_Integer argc,
3731                                 const char ** argv)
3732 {
3733   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
3734   if(aContext.IsNull()) {
3735     di << "use 'vinit' command before " << argv[0] << "\n";
3736     return 1;
3737   }
3738   if(argc < 4 || argc > 5){
3739     di<<"Usage : " << argv[0] << " Ax Ay Az [Start = 1|0]\n";
3740     return 1;
3741   }
3742   Standard_Real Ax = atof(argv[1]);
3743   Standard_Real Ay = atof(argv[2]);
3744   Standard_Real Az = atof(argv[3]);
3745   Standard_Boolean aStart = Standard_True;
3746   if (argc == 5)
3747   {
3748       aStart = (atoi(argv[4]) > 0);
3749   }
3750
3751   Handle(V3d_View) aView = ViewerTest::CurrentView();
3752   aView->Turn(Ax,Ay,Az,aStart);
3753   return 0;
3754 }
3755
3756 //==============================================================================
3757 //function : VTextureEnv
3758 //purpose  : ENables or disables environment mapping
3759 //==============================================================================
3760 class OCC_TextureEnv : public Graphic3d_TextureEnv
3761 {
3762 public:
3763   OCC_TextureEnv(const Standard_CString FileName);
3764   OCC_TextureEnv(const Graphic3d_NameOfTextureEnv aName);
3765   void SetTextureParameters(const Standard_Boolean theRepeatFlag,
3766                             const Standard_Boolean theModulateFlag,
3767                             const Graphic3d_TypeOfTextureFilter theFilter,
3768                             const Standard_ShortReal theXScale,
3769                             const Standard_ShortReal theYScale,
3770                             const Standard_ShortReal theXShift,
3771                             const Standard_ShortReal theYShift,
3772                             const Standard_ShortReal theAngle);
3773   DEFINE_STANDARD_RTTI(OCC_TextureEnv);
3774 };
3775 DEFINE_STANDARD_HANDLE(OCC_TextureEnv, Graphic3d_TextureEnv);
3776 IMPLEMENT_STANDARD_HANDLE(OCC_TextureEnv, Graphic3d_TextureEnv);
3777 IMPLEMENT_STANDARD_RTTIEXT(OCC_TextureEnv, Graphic3d_TextureEnv);
3778
3779 OCC_TextureEnv::OCC_TextureEnv(const Standard_CString theFileName)
3780   : Graphic3d_TextureEnv(theFileName)
3781 {
3782 }
3783
3784 OCC_TextureEnv::OCC_TextureEnv(const Graphic3d_NameOfTextureEnv theTexId)
3785   : Graphic3d_TextureEnv(theTexId)
3786 {
3787 }
3788
3789 void OCC_TextureEnv::SetTextureParameters(const Standard_Boolean theRepeatFlag,
3790                                           const Standard_Boolean theModulateFlag,
3791                                           const Graphic3d_TypeOfTextureFilter theFilter,
3792                                           const Standard_ShortReal theXScale,
3793                                           const Standard_ShortReal theYScale,
3794                                           const Standard_ShortReal theXShift,
3795                                           const Standard_ShortReal theYShift,
3796                                           const Standard_ShortReal theAngle)
3797 {
3798   myParams->SetRepeat     (theRepeatFlag);
3799   myParams->SetModulate   (theModulateFlag);
3800   myParams->SetFilter     (theFilter);
3801   myParams->SetScale      (Graphic3d_Vec2(theXScale, theYScale));
3802   myParams->SetTranslation(Graphic3d_Vec2(theXShift, theYShift));
3803   myParams->SetRotation   (theAngle);
3804 }
3805
3806 static int VTextureEnv (Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
3807 {
3808   // get the active view
3809   Handle(V3d_View) aView = ViewerTest::CurrentView();
3810   if (aView.IsNull())
3811   {
3812     std::cerr << "No active view. Please call vinit.\n";
3813     return 1;
3814   }
3815
3816   // Checking the input arguments
3817   Standard_Boolean anEnableFlag = Standard_False;
3818   Standard_Boolean isOk         = theArgNb >= 2;
3819   if (isOk)
3820   {
3821     TCollection_AsciiString anEnableOpt(theArgVec[1]);
3822     anEnableFlag = anEnableOpt.IsEqual("on");
3823     isOk         = anEnableFlag || anEnableOpt.IsEqual("off");
3824   }
3825   if (anEnableFlag)
3826   {
3827     isOk = (theArgNb == 3 || theArgNb == 11);
3828     if (isOk)
3829     {
3830       TCollection_AsciiString aTextureOpt(theArgVec[2]);
3831       isOk = (!aTextureOpt.IsIntegerValue() ||
3832              (aTextureOpt.IntegerValue() >= 0 && aTextureOpt.IntegerValue() < Graphic3d_NOT_ENV_UNKNOWN));
3833
3834       if (isOk && theArgNb == 11)
3835       {
3836         TCollection_AsciiString aRepeatOpt  (theArgVec[3]),
3837                                 aModulateOpt(theArgVec[4]),
3838                                 aFilterOpt  (theArgVec[5]),
3839                                 aSScaleOpt  (theArgVec[6]),
3840                                 aTScaleOpt  (theArgVec[7]),
3841                                 aSTransOpt  (theArgVec[8]),
3842                                 aTTransOpt  (theArgVec[9]),
3843                                 anAngleOpt  (theArgVec[10]);
3844         isOk = ((aRepeatOpt.  IsEqual("repeat")   || aRepeatOpt.  IsEqual("clamp")) &&
3845                 (aModulateOpt.IsEqual("modulate") || aModulateOpt.IsEqual("decal")) &&
3846                 (aFilterOpt.  IsEqual("nearest")  || aFilterOpt.  IsEqual("bilinear") || aFilterOpt.IsEqual("trilinear")) &&
3847                 aSScaleOpt.IsRealValue() && aTScaleOpt.IsRealValue() &&
3848                 aSTransOpt.IsRealValue() && aTTransOpt.IsRealValue() &&
3849                 anAngleOpt.IsRealValue());
3850       }
3851     }
3852   }
3853
3854   if (!isOk)
3855   {
3856     std::cerr << "Usage :" << std::endl;
3857     std::cerr << theArgVec[0] << " off" << std::endl;
3858     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;
3859     return 1;
3860   }
3861
3862   if (anEnableFlag)
3863   {
3864     TCollection_AsciiString aTextureOpt(theArgVec[2]);
3865     Handle(OCC_TextureEnv) aTexEnv = aTextureOpt.IsIntegerValue() ?
3866                                      new OCC_TextureEnv((Graphic3d_NameOfTextureEnv)aTextureOpt.IntegerValue()) :
3867                                      new OCC_TextureEnv(theArgVec[2]);
3868
3869     if (theArgNb == 11)
3870     {
3871       TCollection_AsciiString aRepeatOpt(theArgVec[3]), aModulateOpt(theArgVec[4]), aFilterOpt(theArgVec[5]);
3872       aTexEnv->SetTextureParameters(
3873         aRepeatOpt.  IsEqual("repeat"),
3874         aModulateOpt.IsEqual("modulate"),
3875         aFilterOpt.  IsEqual("nearest") ? Graphic3d_TOTF_NEAREST :
3876                                           aFilterOpt.IsEqual("bilinear") ? Graphic3d_TOTF_BILINEAR :
3877                                                                            Graphic3d_TOTF_TRILINEAR,
3878         (Standard_ShortReal)Draw::Atof(theArgVec[6]),
3879         (Standard_ShortReal)Draw::Atof(theArgVec[7]),
3880         (Standard_ShortReal)Draw::Atof(theArgVec[8]),
3881         (Standard_ShortReal)Draw::Atof(theArgVec[9]),
3882         (Standard_ShortReal)Draw::Atof(theArgVec[10])
3883         );
3884     }
3885     aView->SetTextureEnv(aTexEnv);
3886     aView->SetSurfaceDetail(V3d_TEX_ENVIRONMENT);
3887   }
3888   else // Disabling environment mapping
3889   {
3890     aView->SetSurfaceDetail(V3d_TEX_NONE);
3891     Handle(Graphic3d_TextureEnv) aTexture;
3892     aView->SetTextureEnv(aTexture); // Passing null handle to clear the texture data
3893   }
3894
3895   aView->Redraw();
3896   return 0;
3897 }
3898
3899 //=======================================================================
3900 //function : ViewerCommands
3901 //purpose  :
3902 //=======================================================================
3903
3904 void ViewerTest::ViewerCommands(Draw_Interpretor& theCommands)
3905 {
3906
3907   const char *group = "ZeViewer";
3908   theCommands.Add("vinit" ,
3909     "vinit            : vinit [leftPx topPx widthPx heightPx] : Create the Viewer window",
3910     __FILE__,VInit,group);
3911   theCommands.Add("vhelp" ,
3912     "vhelp            : display help on the viewer commands",
3913     __FILE__,VHelp,group);
3914   theCommands.Add("vtop" ,
3915     "vtop or <T>      : Top view" ,
3916     __FILE__,VTop,group);
3917   theCommands.Add("vbottom" ,
3918     "vbottom          : Bottom view" ,
3919     __FILE__,VBottom,group);
3920   theCommands.Add("vleft" ,
3921     "vleft            : Left view" ,
3922     __FILE__,VLeft,group);
3923   theCommands.Add("vright" ,
3924     "vright           : Right view" ,
3925     __FILE__,VRight,group);
3926   theCommands.Add("vaxo" ,
3927     " vaxo or <A>     : Axonometric view ",
3928     __FILE__,VAxo,group);
3929   theCommands.Add("vfront" ,
3930     "vfront           : Front view" ,
3931     __FILE__,VFront,group);
3932   theCommands.Add("vback" ,
3933     "vback            : Back view" ,
3934     __FILE__,VBack,group);
3935   theCommands.Add("vpick" ,
3936     "vpick           : vpick X Y Z [shape subshape] ( all variables as string )",
3937     VPick,group);
3938   theCommands.Add("vfit"    ,
3939     "vfit or <F>         : vfit",
3940     __FILE__,VFit,group);
3941   theCommands.Add("vzfit"    ,
3942     "vzfit",
3943     __FILE__,VZFit,group);
3944   theCommands.Add("vrepaint",
3945     "vrepaint        : vrepaint, force redraw",
3946     __FILE__,VRepaint,group);
3947   theCommands.Add("vclear",
3948     "vclear          : vclear",
3949     __FILE__,VClear,group);
3950   theCommands.Add("vsetbg",
3951     "vsetbg          : vsetbg imagefile [filltype] : Load image as background",
3952     __FILE__,VSetBg,group);
3953   theCommands.Add("vsetbgmode",
3954     "vsetbgmode      : vsetbgmode filltype : Change background image fill type",
3955     __FILE__,VSetBgMode,group);
3956   theCommands.Add("vsetgradientbg",
3957     "vsetgradientbg  : vsetgradientbg r1 g1 b1 r2 g2 b2 filltype : Mount gradient background",
3958     __FILE__,VSetGradientBg,group);
3959   theCommands.Add("vsetgrbgmode",
3960     "vsetgrbgmode    : vsetgrbgmode filltype : Change gradient background fill type",
3961     __FILE__,VSetGradientBgMode,group);
3962   theCommands.Add("vsetcolorbg",
3963     "vsetcolorbg     : vsetcolorbg r g b : Set background color",
3964     __FILE__,VSetColorBg,group);
3965   theCommands.Add("vscale",
3966     "vscale          : vscale X Y Z",
3967     __FILE__,VScale,group);
3968   theCommands.Add("vzbufftrihedron",
3969     "vzbufftrihedron [center|left_lower|left_upper|right_lower|right_upper"
3970     " textR=255 textG=255 textB=255 scale=0.1 wireframe|zbuffer]"
3971     " : Displays a V3d_ZBUFFER'ed or V3d_WIREFRAME'd trihedron",
3972     __FILE__,VTestZBuffTrihedron,group);
3973   theCommands.Add("vrotate",
3974     "vrotate         : vrotate AX AY AZ [X Y Z]",
3975     __FILE__,VRotate,group);
3976   theCommands.Add("vzoom",
3977     "vzoom           : vzoom coef",
3978     __FILE__,VZoom,group);
3979   theCommands.Add("vpan",
3980     "vpan            : vpan dx dy",
3981     __FILE__,VPan,group);
3982   theCommands.Add("vexport",
3983     "vexport         : vexport full_file_path {PS | EPS | TEX | PDF | SVG | PGF | EMF }"
3984     " : exports the view to a vector file of a given format"
3985     " : notice that EMF format requires patched gl2ps",
3986     __FILE__,VExport,group);
3987   theCommands.Add("vcolorscale",
3988     "vcolorscale     : vcolorscale [RangeMin = 0 RangeMax = 100 Intervals = 10 HeightFont = 16 Position = 2 X = 0 Y = 0]: draw color scale",
3989     __FILE__,VColorScale,group);
3990   theCommands.Add("vgraduatedtrihedron",
3991     "vgraduatedtrihedron : 1/0 (display/erase) [Xname Yname Zname [Font [isMultibyte]]]",
3992     __FILE__,VGraduatedTrihedron,group);
3993   theCommands.Add("vprintview" ,
3994     "vprintview : width height filename [algo=0] : Test print algorithm: algo = 0 - stretch, algo = 1 - tile",
3995     __FILE__,VPrintView,group);
3996   theCommands.Add("vzlayer",
3997     "vzlayer : add/del/get [id] : Z layer operations in v3d viewer: add new z layer, delete z layer, get z layer ids",
3998     __FILE__,VZLayer,group);
3999   theCommands.Add("voverlaytext",
4000     "voverlaytext : text x y [height] [font_name] [text_color: R G B] [display_type] [background_color: R G B]"
4001     " : height - pixel height of the text (default=10.0)"
4002     " : font_name - name of font (default=courier)"
4003     " : text_color - three values: RedColor GreenColor BlueColor (default = 255.0 255.0 255.0) "
4004     " : display_type = {normal/subtitle/decal/blend}, (default=normal) "
4005     " : background_color - three values: RedColor GreenColor BlueColor (default = 255.0 255.0 255.0), the parameter is defined for subtitle and decal display types ",
4006     __FILE__,VOverlayText,group);
4007   theCommands.Add("vlayerline",
4008     "vlayerline : vlayerline x1 y1 x2 y2 [linewidth=0.5] [linetype=0] [transparency=1.0]",
4009     __FILE__,VLayerLine,group);
4010   theCommands.Add ("vgrid",
4011     "vgrid [off] [Mode={r|c}] [Type={l|p}] [OriginX OriginY [StepX/StepRadius StepY/DivNb RotAngle]]"
4012     " : Mode - rectangular or circular"
4013     " : Type - lines or points",
4014     __FILE__, VGrid, group);
4015   theCommands.Add ("vfps",
4016     "vfps [framesNb=100] : estimate average frame rate for active view",
4017     __FILE__, VFps, group);
4018   theCommands.Add ("vvbo",
4019     "vvbo {0|1} : turn VBO usage On/Off; affects only newly displayed objects",
4020     __FILE__, VVbo, group);
4021   theCommands.Add ("vmemgpu",
4022     "vmemgpu [f]: print system-dependent GPU memory information if available;"
4023     " with f option returns free memory in bytes",
4024     __FILE__, VMemGpu, group);
4025   theCommands.Add ("vreadpixel",
4026     "vreadpixel xPixel yPixel [{rgb|rgba|depth|hls|rgbf|rgbaf}=rgba] [name]"
4027     " : Read pixel value for active view",
4028     __FILE__, VReadPixel, group);
4029   theCommands.Add("diffimage",
4030     "diffimage     : diffimage imageFile1 imageFile2 toleranceOfColor(0..1) blackWhite(1|0) borderFilter(1|0) [diffImageFile]",
4031     __FILE__, VDiffImage, group);
4032   theCommands.Add ("vselect",
4033     "vselect x1 y1 [x2 y2 [x3 y3 ... xn yn]] [shift_selection = 0|1]\n"
4034     "- emulates different types of selection:\n"
4035     "- 1) single click selection\n"
4036     "- 2) selection with rectangle having corners at pixel positions (x1,y1) and (x2,y2)\n"
4037     "- 3) selection with polygon having corners in pixel positions (x1,y1), (x2,y2),...,(xn,yn)\n"
4038     "- 4) any of these selections with shift button pressed",
4039     __FILE__, VSelect, group);
4040   theCommands.Add ("vmoveto",
4041     "vmoveto x y"
4042     "- emulates cursor movement to pixel postion (x,y)",
4043     __FILE__, VMoveTo, group);
4044   theCommands.Add("vviewparams",
4045     "vviewparams [scale center_X center_Y proj_X proj_Y proj_Z up_X up_Y up_Z at_X at_Y at_Z]"
4046     "- gets or sets current view characteristics",
4047     __FILE__,VViewParams, group);
4048   theCommands.Add("vchangeselected",
4049     "vchangeselected shape"
4050     "- adds to shape to selection or remove one from it",
4051                 __FILE__, VChangeSelected, group);
4052   theCommands.Add("vzclipping",
4053     "vzclipping [mode] [depth width]\n"
4054     "- mode = OFF|BACK|FRONT|SLICE depth = [0..1] width = [0..1]\n"
4055     "- gets or sets ZClipping mode, width and depth",
4056     __FILE__,VZClipping,group);
4057   theCommands.Add ("vnbselected",
4058     "vnbselected", __FILE__, VNbSelected, group);
4059   theCommands.Add("vantialiasing",
4060     "vantialiasing 1|0",
4061     __FILE__,VAntialiasing,group);
4062   theCommands.Add ("vpurgedisplay",
4063     "vpurgedisplay [CollectorToo = 0|1]"
4064     "- removes structures which don't belong to objects displayed in neutral point",
4065     __FILE__, VPurgeDisplay, group);
4066   theCommands.Add("vsetviewsize",
4067     "vsetviewsize size",
4068     __FILE__,VSetViewSize,group);
4069   theCommands.Add("vmoveview",
4070     "vmoveview Dx Dy Dz [Start = 1|0]",
4071     __FILE__,VMoveView,group);
4072   theCommands.Add("vtranslateview",
4073     "vtranslateview Dx Dy Dz [Start = 1|0)]",
4074     __FILE__,VTranslateView,group);
4075   theCommands.Add("vturnview",
4076     "vturnview Ax Ay Az [Start = 1|0]",
4077     __FILE__,VTurnView,group);
4078   theCommands.Add("vtextureenv",
4079     "Enables or disables environment mapping in the 3D view, loading the texture from the given standard "
4080     "or user-defined file and optionally applying texture mapping parameters\n"
4081     "                  Usage:\n"
4082     "                  vtextureenv off - disables environment mapping\n"
4083     "                  vtextureenv on {std_texture|texture_file_name} [rep mod flt ss st ts tt rot] - enables environment mapping\n"
4084     "                              std_texture = (0..7)\n"
4085     "                              rep         = {clamp|repeat}\n"
4086     "                              mod         = {decal|modulate}\n"
4087     "                              flt         = {nearest|bilinear|trilinear}\n"
4088     "                              ss, st      - scale factors for s and t texture coordinates\n"
4089     "                              ts, tt      - translation for s and t texture coordinates\n"
4090     "                              rot         - texture rotation angle in degrees",
4091     __FILE__, VTextureEnv, group);
4092   theCommands.Add("vhlr" ,
4093     "is_enabled={on|off}"
4094     " - Hidden line removal algorithm:"
4095     " - is_enabled: if is on HLR algorithm is applied\n",
4096     __FILE__,VHLR,group);
4097   theCommands.Add("vhlrtype" ,
4098     "algo_type={algo|polyalgo} [shape_1 ... shape_n]"
4099     " - Changes the type of HLR algorithm using for shapes."
4100     " - algo_type: if equals to algo, exact HLR algorithm is applied;\n"
4101     "   if equals to polyalgo, polygonal HLR algorithm is applied."
4102     "If shapes are not given HLR algoithm of given type is applied"
4103     " to all shapes in the view\n",
4104     __FILE__,VHLRType,group);
4105 }