1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
20 #define OCC198 // jfa 06/03/2002 // to prevent exception on bad font index
21 #define OCC199 // jfa 06/03/2002 // to prevent exception on bad color index
23 #include <QAViewer2dTest.hxx>
25 #include <QAViewer2dTest_EventManager.hxx>
26 #include <QAViewer2dTest_DataMapOfText.hxx>
28 #include <AIS2D_InteractiveContext.hxx>
30 #include <Draw_Interpretor.hxx>
37 #include <X11/Xutil.h>
38 #include <Graphic3d_GraphicDevice.hxx>
39 #include <Xw_Window.hxx>
40 #include <Xw_Driver.hxx>
44 #include <Graphic3d_WNTGraphicDevice.hxx>
45 #include <WNT_WClass.hxx>
46 #include <WNT_Window.hxx>
47 #include <WNT_WDriver.hxx>
53 #include <V2d_Viewer.hxx>
54 #include <V2d_View.hxx>
56 #include <Graphic2d_Text.hxx>
58 #include <PS_Driver.hxx>
60 //==============================================================================
62 //==============================================================================
63 Standard_EXPORT int ViewerMainLoop2d (Standard_Integer , char** argv);
66 static Handle(Graphic3d_WNTGraphicDevice)& GetG2dDevice()
68 static Handle(Graphic3d_WNTGraphicDevice) GD;
72 static Handle(WNT_Window)& VT_GetWindow2d()
74 static Handle(WNT_Window) WNTWin;
79 static Handle(Graphic3d_GraphicDevice)& GetG2dDevice()
81 static Handle(Graphic3d_GraphicDevice) GD;
85 static Handle(Xw_Window)& VT_GetWindow2d()
87 static Handle(Xw_Window) XWWin;
91 static Display* display2d;
93 static void VProcessEvents2d (ClientData, int);
98 static QAViewer2dTest_DataMapOfText myMapOfText;
100 static void OSWindowSetup2d();
102 //==============================================================================
103 // EVENT GLOBAL VARIABLES
104 //==============================================================================
105 static int Start_Rot = 0;
106 static int ZClipIsOn = 0;
107 static int X_Motion = 0, Y_Motion = 0; // Current cursor position
108 static int X_ButtonPress = 0, Y_ButtonPress = 0; // Last ButtonPress position
109 //==============================================================================
112 static LRESULT WINAPI Viewer2dWindowProc (HWND hwnd, UINT uMsg,
113 WPARAM wParam, LPARAM lParam);
116 //==============================================================================
117 //function : ViewerInit
118 //purpose : Create the window viewer and initialize all the global variable
119 //==============================================================================
120 void QAViewer2dTest::ViewerInit()
122 static int first = 1;
126 Handle(Aspect_WindowDriver) aWindowDriver;
129 // Create the Graphic device
130 if (GetG2dDevice().IsNull())
131 GetG2dDevice() = new Graphic3d_WNTGraphicDevice();
134 if (VT_GetWindow2d().IsNull())
136 Handle(WNT_GraphicDevice) g_Device = new WNT_GraphicDevice();
138 Handle(WNT_WClass) WC = new WNT_WClass
139 ("GW3D_Class", Viewer2dWindowProc, CS_VREDRAW | CS_HREDRAW,
140 0, 0, LoadCursor(NULL, IDC_ARROW));
141 VT_GetWindow2d() = new WNT_Window
142 (g_Device,"Test2d", WC, WS_OVERLAPPEDWINDOW,0.,0.20,0.40,0.40,Quantity_NOC_BLACK);
143 VT_GetWindow2d()->Map();
146 // Create window driver
147 aWindowDriver = new WNT_WDriver(VT_GetWindow2d());
149 // Create the Graphic device
150 if (GetG2dDevice().IsNull())
151 GetG2dDevice() = new Graphic3d_GraphicDevice(getenv("DISPLAY"),Xw_TOM_READONLY);
154 if (VT_GetWindow2d().IsNull())
156 VT_GetWindow2d() = new Xw_Window(GetG2dDevice(), "Test2d",
158 Xw_WQ_3DQUALITY, Quantity_NOC_BLACK);
161 // Create window driver
162 aWindowDriver = new Xw_Driver(VT_GetWindow2d());
165 Handle(V2d_Viewer) a3DViewer,a3DCollector;
166 // Create Viewer and View
167 a3DViewer = new V2d_Viewer(GetG2dDevice(),Standard_ExtString("Visu3D"));
168 a3DCollector = new V2d_Viewer(GetG2dDevice(),Standard_ExtString("Collector"));
169 // a3DViewer->SetDefaultBackgroundColor(Quantity_NOC_BLACK);
170 // a3DCollector->SetDefaultBackgroundColor(Quantity_NOC_STEELBLUE);
172 if (QAViewer2dTest::CurrentView().IsNull())
174 // VT_GetWindow2d()->SetBackground(Quantity_NOC_MATRAGRAY);
175 Handle(V2d_View) V = new V2d_View(aWindowDriver, a3DViewer);
176 QAViewer2dTest::CurrentView(V);
178 Handle(V2d_View) a3DViewCol = new V2d_View(aWindowDriver, a3DCollector);
181 if (QAViewer2dTest::GetAIS2DContext().IsNull())
183 Handle(AIS2D_InteractiveContext) C = new AIS2D_InteractiveContext(a3DViewer,a3DCollector);
184 QAViewer2dTest::SetAIS2DContext(C);
187 // Setup for X11 or NT
190 // Viewer and View creation
192 // a3DViewer->SetDefaultBackgroundColor(Quantity_NOC_BLACK);
194 Handle(V2d_View) V = QAViewer2dTest::CurrentView();
196 // V->SetDegenerateModeOn();
197 // V->SetWindow(VT_GetWindow2d());
199 // V->SetZClippingDepth(0.5);
200 // V->SetZClippingWidth(ZCLIPWIDTH/2.);
202 // a3DViewer->SetDefaultLights();
203 // a3DViewer->SetLightOn();
206 #if TCL_MAJOR_VERSION < 8 && !defined(DECOSF1)
207 Tk_CreateFileHandler((void*)ConnectionNumber(display2d),
208 TK_READABLE, VProcessEvents2d, (ClientData) VT_GetWindow2d()->XWindow());
210 Tk_CreateFileHandler(ConnectionNumber(display2d),
211 TK_READABLE, VProcessEvents2d, (ClientData) VT_GetWindow2d()->XWindow());
219 //==============================================================================
221 //purpose : Create the window viewer and initialize all the global variable
222 // Use Tk_CreateFileHandler on UNIX to cath the X11 Viewer event
223 //==============================================================================
224 static int V2dInit (Draw_Interpretor& , Standard_Integer , char** )
226 QAViewer2dTest::ViewerInit();
230 //==============================================================================
231 //function : ProcessKeyPress2d
232 //purpose : Handle KeyPress event from a CString
233 //==============================================================================
234 static void ProcessKeyPress2d (char *buf_ret)
237 if (!strcasecmp(buf_ret, "R"))
240 QAViewer2dTest::CurrentView()->Reset();
242 else if (!strcasecmp(buf_ret, "F"))
245 QAViewer2dTest::CurrentView()->Fitall();
247 else if (!strcasecmp(buf_ret, "H"))
250 cout << "HLR" << endl;
251 // QAViewer2dTest::CurrentView()->SetDegenerateModeOff();
253 else if (!strcasecmp(buf_ret, "S"))
256 cout << "passage en mode 1 (shading pour les shapes)" << endl;
257 // QAViewer2dTest::CurrentView()->SetDegenerateModeOn();
258 Handle(AIS2D_InteractiveContext) Ctx = QAViewer2dTest::GetAIS2DContext();
259 // if (Ctx->NbCurrents() == 0 || Ctx->NbSelected() == 0)
260 // Ctx->SetDisplayMode(AIS2D_Shaded);
263 if (Ctx->HasOpenedContext())
265 // for(Ctx->InitSelected();Ctx->MoreSelected();Ctx->NextSelected())
266 // Ctx->SetDisplayMode(Ctx->Interactive(), 1, Standard_False);
270 // for(Ctx->InitCurrent();Ctx->MoreCurrent();Ctx->NextCurrent())
271 // Ctx->SetDisplayMode(Ctx->Current(), 1, Standard_False);
273 Ctx->UpdateCurrentViewer();
276 else if (!strcasecmp(buf_ret, "U"))
278 // Unset display mode
279 cout << "passage au mode par defaut" << endl;
280 // QAViewer2dTest::CurrentView()->SetDegenerateModeOn();
281 Handle(AIS2D_InteractiveContext) Ctx = QAViewer2dTest::GetAIS2DContext();
282 // if (Ctx->NbCurrents() == 0 || Ctx->NbSelected() == 0)
283 // Ctx->SetDisplayMode(AIS2D_WireFrame);
286 if(Ctx->HasOpenedContext())
288 // for (Ctx->InitSelected(); Ctx->MoreSelected(); Ctx->NextSelected())
289 // Ctx->UnsetDisplayMode(Ctx->Interactive(), Standard_False);
293 // for (Ctx->InitCurrent(); Ctx->MoreCurrent(); Ctx->NextCurrent())
294 // Ctx->UnsetDisplayMode(Ctx->Current(), Standard_False);
296 Ctx->UpdateCurrentViewer();
299 else if (!strcasecmp(buf_ret, "W"))
302 cout << "passage en mode 0 (filaire pour les shapes)" << endl;
303 // QAViewer2dTest::CurrentView()->SetDegenerateModeOn();
304 Handle(AIS2D_InteractiveContext) Ctx = QAViewer2dTest::GetAIS2DContext();
305 // if (Ctx->NbCurrents() == 0 || Ctx->NbSelected() == 0)
306 // Ctx->SetDisplayMode(AIS2D_WireFrame);
309 if (Ctx->HasOpenedContext())
311 // for (Ctx->InitSelected(); Ctx->MoreSelected(); Ctx->NextSelected())
312 // Ctx->SetDisplayMode(Ctx->Interactive(), 0, Standard_False);
316 // for (Ctx->InitCurrent(); Ctx->MoreCurrent(); Ctx->NextCurrent())
317 // Ctx->SetDisplayMode(Ctx->Current(), 0, Standard_False);
319 Ctx->UpdateCurrentViewer();
322 else if (!strcasecmp(buf_ret, "D"))
325 cout << "Delete selected object - not yet implemented" << endl;
326 QAViewer2dTest::CurrentView()->Reset();
328 else if (!strcasecmp(buf_ret, "Z"))
333 cout << "ZClipping OFF" << endl;
336 // QAViewer2dTest::CurrentView()->SetZClippingType(V2d_OFF);
337 // QAViewer2dTest::CurrentView()->Redraw();
341 cout << "ZClipping ON" << endl;
344 // QAViewer2dTest::CurrentView()->SetZClippingType(V2d_FRONT);
345 // QAViewer2dTest::CurrentView()->Redraw();
348 else if (!strcasecmp(buf_ret, ","))
350 // QAViewer2dTest::GetAIS2DContext()->HilightNextDetected(QAViewer2dTest::CurrentView());
352 else if (!strcasecmp(buf_ret, "."))
354 // QAViewer2dTest::GetAIS2DContext()->HilightPreviousDetected(QAViewer2dTest::CurrentView());
358 Standard_Integer Num = atoi(buf_ret);
359 if (Num >= 0 && Num <= 7)
360 QAViewer2dTest::StandardModeActivation(Num);
364 //==============================================================================
365 //function : ProcessExpose2d
366 //purpose : Redraw the View on an Expose Event
367 //==============================================================================
368 static void ProcessExpose2d()
370 QAViewer2dTest::CurrentView()->Viewer()->Update();
371 // QAViewer2dTest::CurrentView()->Redraw();
374 //==============================================================================
375 //function : ProcessConfigure2d
376 //purpose : Resize the View on an Configure Event
377 //==============================================================================
378 static void ProcessConfigure2d()
380 Handle(V2d_View) V = QAViewer2dTest::CurrentView();
381 V->MustBeResized(V2d_TOWRE_ENLARGE_OBJECTS);
384 V->Viewer()->Update();
387 //==============================================================================
388 //function : V2dPickGrid
389 //purpose : v2dpickgrid : get coordinates of a grid point near to MB1 click
390 //==============================================================================
391 static int V2dHitGrid (int argc, Standard_Integer anX, Standard_Integer anY,
392 char* xxname = 0, char* yyname = 0)
394 if (argc != 1 && argc != 3 && argc != 5)
396 cout << "Usage : V2dHitGrid [mouse_X mouse_Y [grid_X grid_Y]]" << endl;
400 Handle(V2d_View) V = QAViewer2dTest::CurrentView();
401 if (V.IsNull()) return 1;
403 if (V->Viewer()->IsActive()) // about the grid
407 // cout << "Mouse cursor position: X = " << anX << " Y = " << anY << endl;
410 Quantity_Length xx, yy;
411 V->Hit(anX, anY, xx, yy);
413 Standard_Integer xw, yw;
414 V->Convert(xx, yy, xw, yw);
415 printf("Window coords of Grid Point: X = %d Y = %d\n", xw, yw);
419 printf("Grid Point: X = %.6g Y = %.6g\n", xx, yy);
424 sprintf(szbuf, "%.6g", xx);
425 Draw::Set(xxname, szbuf);
426 sprintf(szbuf, "%.6g", yy);
427 Draw::Set(yyname, szbuf);
433 int V2dPickGrid (Draw_Interpretor& , Standard_Integer argc, char** argv)
435 if (argc != 1 && argc != 3 && argc != 5)
437 cout << "Usage : v2dpickgrid (MB1 in the viewer)" << endl;
438 cout << "Usage : v2dgetgrid mouse_X mouse_Y [grid_X grid_Y]" << endl;
446 char *xxname, *yyname;
449 V2dHitGrid(argc, atoi(argv[1]), atoi(argv[2]), xxname, yyname);
453 V2dHitGrid(argc, atoi(argv[1]), atoi(argv[2]));
458 while (ViewerMainLoop2d (argc, argv)) {}
464 //==============================================================================
465 //function : ProcessButton1Press2d
467 //==============================================================================
468 static Standard_Boolean ProcessButton1Press2d
469 (Standard_Integer , char** argv, Standard_Boolean pick, Standard_Boolean shift)
473 cout << "Window X = " << X_ButtonPress << " Window Y = " << Y_ButtonPress << endl;
474 if (strlen(argv[0]) > 7) // v2dpickgrid
476 V2dHitGrid(1, X_ButtonPress, Y_ButtonPress);
480 Handle(V2d_View) V = QAViewer2dTest::CurrentView();
481 if (V.IsNull()) return 1;
482 Quantity_Length xx, yy;
483 V->Convert(X_ButtonPress, Y_ButtonPress, xx, yy);
484 printf("Coord system X = %.6g Y = %.6g\n", xx, yy);
488 Handle(QAViewer2dTest_EventManager) EM = QAViewer2dTest::CurrentEventManager();
489 if (shift) EM->ShiftSelect();
496 //==============================================================================
497 //function : ProcessButton3Press2d
498 //purpose : Start Rotation
499 //==============================================================================
500 static void ProcessButton3Press2d()
504 // QAViewer2dTest::CurrentView()->SetDegenerateModeOn();
505 // QAViewer2dTest::CurrentView()->StartRotation(X_ButtonPress, Y_ButtonPress);
508 //==============================================================================
509 //function : ProcessButtonRelease2d
510 //purpose : End Rotation
511 //==============================================================================
512 static void ProcessButtonRelease2d()
516 // QAViewer2dTest::CurrentView()->SetDegenerateModeOff();
519 //==============================================================================
520 //function : ProcessZClipMotion2d
522 //==============================================================================
523 void ProcessZClipMotion2d()
525 Handle(V2d_View) a3DView = QAViewer2dTest::CurrentView();
526 if (Abs(X_Motion - X_ButtonPress) > 2)
528 static Standard_Real CurZPos = 0.;
529 // Quantity_Length VDX, VDY;
531 // a3DView->Size(VDX,VDY);
534 // Standard_Real VDZ =
538 Quantity_Length dx = a3DView->Convert(X_Motion - X_ButtonPress);
540 // Front = Depth + width/2.
541 Standard_Real D = 0.5;
542 // Standard_Real W = 0.1;
548 // a3DView->SetZClippingType(V2d_OFF);
549 // a3DView->SetZClippingDepth(D);
550 // a3DView->SetZClippingWidth(W);
551 // a3DView->SetZClippingType(V2d_FRONT);
553 // a3DView->Redraw();
555 X_ButtonPress = X_Motion;
556 Y_ButtonPress = Y_Motion;
560 //==============================================================================
561 //function : ProcessControlButton1Motion2d
562 //purpose : Zoom on Ctrl+MB1
563 //==============================================================================
564 static void ProcessControlButton1Motion2d()
566 QAViewer2dTest::CurrentView()->Zoom(X_ButtonPress, Y_ButtonPress, X_Motion, Y_Motion);
568 X_ButtonPress = X_Motion;
569 Y_ButtonPress = Y_Motion;
572 //==============================================================================
573 //function : ProcessControlButton2Motion2d
574 //purpose : Pan on Ctrl+MB2
575 //==============================================================================
576 static void ProcessControlButton2Motion2d()
578 QAViewer2dTest::CurrentView()->Pan(X_Motion - X_ButtonPress, Y_ButtonPress - Y_Motion);
580 X_ButtonPress = X_Motion;
581 Y_ButtonPress = Y_Motion;
584 //==============================================================================
585 //function : ProcessControlButton3Motion2d
586 //purpose : Rotation on Ctrl+MB3
587 //==============================================================================
588 static void ProcessControlButton3Motion2d()
590 // if (Start_Rot) QAViewer2dTest::CurrentView()->Rotation(X_Motion, Y_Motion);
593 //==============================================================================
594 //function : ProcessMotion2d
595 //purpose : pre-hilights detected objects at mouse position
596 //==============================================================================
597 static void ProcessMotion2d()
599 Handle(QAViewer2dTest_EventManager) EM = QAViewer2dTest::CurrentEventManager();
600 Handle(V2d_View) V = QAViewer2dTest::CurrentView();
601 EM->MoveTo(X_Motion, Y_Motion, V);
604 //==============================================================================
605 //function : ProcessGridMotion2d
606 //purpose : pre-hilights grid point near mouse position
607 //==============================================================================
608 static void ProcessGridMotion2d()
610 Handle(V2d_View) V = QAViewer2dTest::CurrentView();
612 static Standard_Boolean LastIsGridActiveStatus = Standard_True;
613 if (V->Viewer()->IsActive()) // about the grid
615 V->ShowHit(X_Motion, Y_Motion);
616 LastIsGridActiveStatus = Standard_True;
618 if (!V->Viewer()->IsActive() && LastIsGridActiveStatus) // about the grid
621 LastIsGridActiveStatus = Standard_False;
625 //==============================================================================
626 //function : GetMousePosition
628 //==============================================================================
629 void QAViewer2dTest::GetMousePosition (Standard_Integer& Xpix, Standard_Integer& Ypix)
635 //==============================================================================
637 //purpose : Dsiplay help on viewer Keyboead and mouse commands
639 //==============================================================================
640 /*static int V2dHelp (Draw_Interpretor& , Standard_Integer , char** )
642 cout << "Q : Quit the application" << endl;
643 cout << "=========================" << endl;
644 cout << "F : FitAll" << endl;
645 cout << "R : ResetView" << endl;
646 cout << "=========================" << endl;
647 cout << "Selection mode " << endl;
648 cout << "0 : OBJECT" << endl;
649 cout << "1 : PRIMITIVE" << endl;
650 cout << "2 : ELEMENT" << endl;
651 cout << "3 : VERTEX" << endl;
652 cout << "4 : NONE" << endl;
653 cout << "==========================" << endl;
654 cout << "D : Remove Selected Object" << endl;
655 cout << "==========================" << endl;
662 static LRESULT WINAPI Viewer2dWindowProc (HWND hwnd, UINT Msg,
663 WPARAM wParam, LPARAM lParam )
665 static Standard_Boolean Ppick = 0;
666 static Standard_Integer Pargc = 0;
667 static char** Pargv = NULL;
671 if (!QAViewer2dTest::CurrentView().IsNull())
678 BeginPaint(hwnd, &ps);
684 ProcessConfigure2d();
689 c[0] = (char) wParam;
691 ProcessKeyPress2d(c);
698 ProcessButtonRelease2d();
705 WPARAM fwKeys = wParam;
709 X_ButtonPress = LOWORD(lParam);
710 Y_ButtonPress = HIWORD(lParam);
712 if (Msg == WM_LBUTTONDOWN && !(fwKeys & MK_CONTROL))
714 Ppick = ProcessButton1Press2d(Pargc, Pargv, Ppick, (fwKeys & MK_SHIFT));
716 else if (Msg == WM_RBUTTONDOWN)
719 ProcessButton3Press2d();
726 WPARAM fwKeys = wParam;
727 X_Motion = LOWORD(lParam);
728 Y_Motion = HIWORD(lParam);
730 if (Up && fwKeys & (MK_LBUTTON|MK_MBUTTON|MK_RBUTTON))
733 X_ButtonPress = LOWORD(lParam);
734 Y_ButtonPress = HIWORD(lParam);
736 if (fwKeys & MK_RBUTTON)
739 ProcessButton3Press2d();
743 if (fwKeys & MK_CONTROL)
745 if (fwKeys & MK_LBUTTON)
747 ProcessControlButton1Motion2d();
749 else if (fwKeys & MK_MBUTTON || ((fwKeys&MK_LBUTTON) && (fwKeys&MK_RBUTTON)))
751 ProcessControlButton2Motion2d();
753 else if (fwKeys & MK_RBUTTON)
755 ProcessControlButton3Motion2d();
758 else if (fwKeys & MK_SHIFT)
760 if (fwKeys & MK_MBUTTON || ((fwKeys&MK_LBUTTON) && (fwKeys&MK_RBUTTON)))
762 ProcessZClipMotion2d();
773 return(DefWindowProc(hwnd, Msg, wParam, lParam));
778 return DefWindowProc(hwnd, Msg, wParam, lParam);
781 //==============================================================================
782 //function : ViewerMainLoop2d
783 //purpose : Get a Event on the view and dispatch it
784 //==============================================================================
785 static int ViewerMainLoop2d (Standard_Integer argc, char** argv)
787 cout << "No yet implemented on WNT" << endl;
788 static Standard_Boolean Ppick = 0;
789 static Standard_Integer Pargc = 0;
790 static char** Pargv = NULL;
792 Ppick = (argc > 0)? -1 : 0;
801 cout << "Start picking" << endl;
805 // Wait for a ProcessButton1Press2d() to toggle pick to 1 or 0
806 if (GetMessage(&msg, NULL, 0, 0))
808 TranslateMessage(&msg);
809 DispatchMessage(&msg);
813 cout << "Picking done" << endl;
821 //==============================================================================
822 //function : ViewerMainLoop2d
823 //purpose : Get a Event on the view and dispatch it
824 //==============================================================================
825 int ViewerMainLoop2d (Standard_Integer argc, char** argv)
827 Standard_Boolean pick = argc > 0;
830 static XEvent report;
832 XNextEvent(display2d, &report);
839 case ConfigureNotify:
840 ProcessConfigure2d();
847 XComposeStatus status_in_out;
849 ret_len = XLookupString((XKeyEvent *)&report, (char *) buf_ret,
850 10, &ks_ret, &status_in_out);
852 buf_ret[ret_len] = '\0';
853 if (ret_len) ProcessKeyPress2d(buf_ret);
858 X_ButtonPress = report.xbutton.x;
859 Y_ButtonPress = report.xbutton.y;
861 if (report.xbutton.button == Button1 &&
862 !(report.xbutton.state & ControlMask))
864 pick = ProcessButton1Press2d(argc, argv, pick,
865 (report.xbutton.state & ShiftMask));
867 else if (report.xbutton.button == Button3)
870 ProcessButton3Press2d();
875 ProcessButtonRelease2d();
879 X_Motion = report.xmotion.x;
880 Y_Motion = report.xmotion.y;
882 // remove all the ButtonMotionMask
883 while (XCheckMaskEvent(display2d, ButtonMotionMask, &report)) {}
885 if (ZClipIsOn && report.xmotion.state & ShiftMask)
887 if (Abs(X_Motion - X_ButtonPress) > 2)
889 Quantity_Length VDX = 0.0, VDY = 0.0;
891 // QAViewer2dTest::CurrentView()->Size(VDX,VDY);
892 Standard_Real VDZ =0 ;
893 // VDZ = QAViewer2dTest::CurrentView()->ZSize();
895 printf("%f,%f,%f\n", VDX, VDY, VDZ);
897 Quantity_Length dx = 0 ;
898 dx = QAViewer2dTest::CurrentView()->Convert(X_Motion - X_ButtonPress);
906 // Front = Depth + width/2.
907 //QAViewer2dTest::CurrentView()->SetZClippingDepth(dx);
908 //QAViewer2dTest::CurrentView()->SetZClippingWidth(0.);
910 // QAViewer2dTest::CurrentView()->Redraw();
914 if (report.xmotion.state & ControlMask)
916 if (report.xmotion.state & Button1Mask)
918 ProcessControlButton1Motion2d();
920 else if (report.xmotion.state & Button2Mask)
922 ProcessControlButton2Motion2d();
924 else if (report.xmotion.state & Button3Mask)
926 ProcessControlButton3Motion2d();
931 if (pick && strlen(argv[0]) > 7) // v2dpickgrid
932 ProcessGridMotion2d();
943 //==============================================================================
944 //function : VProcessEvents2d
945 //purpose : call by Tk_CreateFileHandler() to be able to manage the
946 // event in the Viewer window
947 //==============================================================================
948 static void VProcessEvents2d (ClientData, int)
951 while (XPending(display2d))
953 ViewerMainLoop2d(0, NULL);
958 //==============================================================================
959 //function : OSWindowSetup2d
960 //purpose : Setup for the X11 window to be able to cath the event
961 //==============================================================================
962 static void OSWindowSetup2d()
966 Window window = VT_GetWindow2d()->XWindow();
968 Standard_Address theDisplay = GetG2dDevice()->XDisplay();
969 display2d = (Display * ) theDisplay;
971 XSynchronize(display2d, 1);
973 VT_GetWindow2d()->Map();
975 // X11 : For keyboard on SUN
977 wmhints.flags = InputHint;
980 XSetWMHints( display2d, window, &wmhints);
982 XSelectInput(display2d, window,
983 ExposureMask | StructureNotifyMask | KeyPressMask | ButtonPressMask |
985 Button1MotionMask | Button2MotionMask | Button3MotionMask);
987 XSynchronize(display2d, 0);
995 //==============================================================================
996 //function : InitQAViewer2dTest
997 //purpose : initialisation de toutes les variables static de QAViewer2dTest (dp)
998 //==============================================================================
999 void QAViewer2dTest_InitQAViewer2dTest (const Handle(AIS2D_InteractiveContext)& context)
1001 Handle(V2d_Viewer) viewer = context->CurrentViewer();
1002 QAViewer2dTest::SetAIS2DContext(context);
1003 viewer->InitActiveViews();
1004 Handle(V2d_View) view = viewer->ActiveView();
1005 if (viewer->MoreActiveViews()) QAViewer2dTest::CurrentView(view);
1006 QAViewer2dTest::ResetEventManager();
1007 Handle(Aspect_GraphicDevice) device = viewer->Device();
1008 Handle(Aspect_WindowDriver) aWDr = view->Driver();
1009 Handle(Aspect_Window) window = aWDr->Window();
1012 VT_GetWindow2d() = Handle(Xw_Window)::DownCast(window);
1013 GetG2dDevice() = Handle(Graphic3d_GraphicDevice)::DownCast(device);
1015 static int first = 1;
1018 #if TCL_MAJOR_VERSION < 8 && !defined(DECOSF1)
1019 Tk_CreateFileHandler((void*)ConnectionNumber(display2d),
1020 TK_READABLE, VProcessEvents2d, (ClientData) 0);
1022 Tk_CreateFileHandler(ConnectionNumber(display2d),
1023 TK_READABLE, VProcessEvents2d, (ClientData) 0);
1030 //==============================================================================
1032 //purpose : Fitall, no DRAW arguments
1033 //Draw arg : No args
1034 //==============================================================================
1035 static int V2dFit (Draw_Interpretor& , Standard_Integer , char** )
1037 Handle(V2d_View) V = QAViewer2dTest::CurrentView();
1038 if (!V.IsNull()) V->Fitall();
1042 //==============================================================================
1043 //function : V2dRepaint
1044 //purpose : refresh viewer
1045 //Draw arg : No args
1046 //==============================================================================
1047 static int V2dRepaint (Draw_Interpretor& , Standard_Integer , char** )
1049 Handle(V2d_View) V = QAViewer2dTest::CurrentView();
1052 V->Viewer()->Update();
1056 //==============================================================================
1057 //function : V2dClear
1058 //purpose : Remove all the object from the viewer
1059 //Draw arg : No args
1060 //==============================================================================
1061 static int V2dClear (Draw_Interpretor& , Standard_Integer , char** )
1063 Handle(V2d_View) V = QAViewer2dTest::CurrentView();
1064 if (V.IsNull()) return 1;
1066 QAViewer2dTest::Clear();
1067 V->Viewer()->Update();
1071 //==============================================================================
1072 //function : V2dPick
1073 //purpose : v2dpick - print mouse coords and color
1074 //==============================================================================
1075 static int V2dPick (Draw_Interpretor& , Standard_Integer argc, char** argv)
1079 cout << "Usage: v2dpick (MB1 in the viewer)" << endl;
1082 if (QAViewer2dTest::CurrentView().IsNull()) return 1;
1084 while (ViewerMainLoop2d (argc, argv)) {}
1089 //==============================================================================
1090 //function : V2dSetBG
1091 //purpose : v2dsetbg imagefile [filltype] : load image as background
1092 //==============================================================================
1093 static int V2dSetBG (Draw_Interpretor& , Standard_Integer argc, char** argv)
1095 if (argc < 2 || argc > 3)
1097 cout << "Usage : v2dsetbg imagefile [filltype]" << endl;
1100 if (QAViewer2dTest::CurrentView().IsNull()) return 1;
1102 Aspect_FillMethod aFillType = Aspect_FM_CENTERED;
1105 char* szType = argv[2];
1106 if (strcmp(szType, "NONE" ) == 0) aFillType = Aspect_FM_NONE;
1107 else if (strcmp(szType, "CENTERED") == 0) aFillType = Aspect_FM_CENTERED;
1108 else if (strcmp(szType, "TILED" ) == 0) aFillType = Aspect_FM_TILED;
1109 else if (strcmp(szType, "STRETCH" ) == 0) aFillType = Aspect_FM_STRETCH;
1112 cout << "Wrong fill type : " << szType << endl;
1113 cout << "Must be one of CENTERED, TILED, STRETCH, NONE" << endl;
1118 QAViewer2dTest::CurrentView()->SetBackground(argv[1], aFillType);
1119 QAViewer2dTest::CurrentView()->Viewer()->Update();
1124 //==============================================================================
1125 //function : V2dGrid
1126 //purpose : v2dgrid [type [Origin(X Y) XStep/RadiusStep YStep/DivisionNb RotationAngle] [drawmode]] : load grid
1127 // : v2drmgrid : unload grid
1128 //==============================================================================
1129 static int V2dGrid (Draw_Interpretor& , Standard_Integer argc, char** argv)
1131 if (QAViewer2dTest::CurrentView().IsNull()) return 1;
1132 Handle(V2d_Viewer) aViewer = QAViewer2dTest::CurrentView()->Viewer();
1134 char* szName = argv[0];
1135 if (strcmp(szName, "v2drmgrid") == 0)
1139 cout << "Usage : v2drmgrid" << endl;
1143 aViewer->DeactivateGrid();
1147 // v2dgrid. Args nb may be 1,2,3,7,8
1148 if (argc < 1 || argc > 8 || (argc < 7 && argc > 3))
1150 cout << "Usage : v2dgrid [type [Origin(X Y) XStep/RadiusStep YStep/DivisionNb RotationAngle] [drawmode]]" << endl;
1154 Aspect_GridType aGridT = Aspect_GT_Rectangular;
1155 Aspect_GridDrawMode aDrawM = Aspect_GDM_Lines;
1159 char* szType = argv[1];
1160 if (strcmp(szType, "Rect") == 0) aGridT = Aspect_GT_Rectangular;
1161 else if (strcmp(szType, "Circ") == 0) aGridT = Aspect_GT_Circular;
1164 cout << "Wrong grid type : " << szType << endl;
1165 cout << "Must be Rect or Circ" << endl;
1169 if (argc == 3 || argc == 8)
1171 char* szMode = (argc == 3) ? argv[2] : argv[7];
1172 if (strcmp(szMode, "Lines" ) == 0) aDrawM = Aspect_GDM_Lines;
1173 else if (strcmp(szMode, "Points") == 0) aDrawM = Aspect_GDM_Points;
1174 else if (strcmp(szMode, "None" ) == 0) aDrawM = Aspect_GDM_None;
1177 cout << "Wrong grid draw mode : " << szMode << endl;
1178 cout << "Must be Lines or Points, or None" << endl;
1184 aViewer->ActivateGrid(aGridT, aDrawM);
1188 Quantity_Length aXOrigin = atof(argv[2]);
1189 Quantity_Length aYOrigin = atof(argv[3]);
1190 Quantity_PlaneAngle aRotationAngle = atof(argv[6]);
1192 if (aGridT == Aspect_GT_Rectangular)
1194 Quantity_Length aXStep = atof(argv[4]);
1195 Quantity_Length aYStep = atof(argv[5]);
1196 aViewer->SetRectangularGridValues
1197 (aXOrigin, aYOrigin, aXStep, aYStep, aRotationAngle);
1201 Quantity_Length aRadiusStep = atof(argv[4]);
1202 Standard_Integer aDivisionNumber = atoi(argv[5]);
1203 aViewer->SetCircularGridValues
1204 (aXOrigin, aYOrigin, aRadiusStep, aDivisionNumber, aRotationAngle);
1211 //==============================================================================
1212 //function : V2dPSOut
1213 //purpose : v2dpsout imagefile [Scale TypeOfColorSpace] [Width Height XCenter YCenter] : PostScript Output
1214 //==============================================================================
1215 static int V2dPSOut (Draw_Interpretor& , Standard_Integer argc, char** argv)
1217 if (argc < 2 || argc > 8)
1219 cout << "Usage : v2dpsout imagefile [Scale TypeOfColorSpace] [Width Height XCenter YCenter]" << endl;
1223 Handle(V2d_View) V = QAViewer2dTest::CurrentView();
1224 if (V.IsNull()) return 1;
1226 Quantity_Factor aScale = 1.0;
1227 Aspect_TypeOfColorSpace aColorType = Aspect_TOCS_RGB;
1229 Quantity_Length aWidth = 100.0, aHeight = 100.0;
1231 Quantity_Length aXCenter, aYCenter;
1232 V->Center(aXCenter, aYCenter);
1236 aScale = atof(argv[2]);
1240 char* szType = argv[3];
1241 if (strcmp(szType, "RGB") == 0) aColorType = Aspect_TOCS_RGB;
1242 else if (strcmp(szType, "BlackAndWhite") == 0) aColorType = Aspect_TOCS_BlackAndWhite;
1243 else if (strcmp(szType, "GreyScale") == 0) aColorType = Aspect_TOCS_GreyScale;
1246 cout << "Wrong color space type : " << szType << endl;
1247 cout << "Must be one of RGB, BlackAndWhite, GreyScale" << endl;
1253 aWidth = atof(argv[4]);
1254 aHeight = atof(argv[5]);
1255 aXCenter = atof(argv[6]);
1256 aYCenter = atof(argv[7]);
1258 else // take current window size
1261 Handle(Aspect_Window) anAspWindow = V->Driver()->Window();
1262 Handle(Xw_Window) anXWindow = Handle(Xw_Window)::DownCast(anAspWindow);
1263 anXWindow->Size(aWidth, aHeight);
1269 QAViewer2dTest::CurrentView()->PostScriptOutput
1270 (argv[1], aWidth, aHeight, aXCenter, aYCenter, aScale, aColorType);
1275 //==============================================================================
1276 //function : V2dPlot
1277 //purpose : v2dplot format imagefile [Scale TypeOfColorSpace] [Width Height XCenter YCenter]
1278 //==============================================================================
1279 /*static int V2dPlot (Draw_Interpretor& , Standard_Integer argc, char** argv)
1281 if (argc < 3 || argc > 9)
1283 cout << "Args: format imagefile [Scale TypeOfColorSpace] [Width Height XCenter YCenter]" << endl;
1286 if (QAViewer2dTest::CurrentView().IsNull()) return 1;
1288 Quantity_Factor aScale = 1.0;
1289 Aspect_TypeOfColorSpace aColorType = Aspect_TOCS_RGB;
1291 Quantity_Length aWidth = 100.0, aHeight = 100.0;
1293 Quantity_Length aXCenter, aYCenter;
1294 QAViewer2dTest::CurrentView()->Center(aXCenter, aYCenter);
1298 aScale = atof(argv[3]);
1302 char* szType = argv[4];
1303 if (strcmp(szType, "RGB") == 0) aColorType = Aspect_TOCS_RGB;
1304 else if (strcmp(szType, "BlackAndWhite") == 0) aColorType = Aspect_TOCS_BlackAndWhite;
1305 else if (strcmp(szType, "GreyScale") == 0) aColorType = Aspect_TOCS_GreyScale;
1308 cout << "Wrong color space type : " << szType << endl;
1309 cout << "Must be one of RGB, BlackAndWhite, GreyScale" << endl;
1315 aWidth = atof(argv[5]);
1316 aHeight = atof(argv[6]);
1317 aXCenter = atof(argv[7]);
1318 aYCenter = atof(argv[8]);
1323 char* szFormat = argv[1];
1324 if (strcmp(szFormat, "PS") == 0)
1326 Handle(PS_Driver) aPSDriver = new PS_Driver(argv[2], aWidth, aHeight, aColorType);
1327 QAViewer2dTest::CurrentView()->Plot(aPSDriver, aXCenter, aYCenter, aScale);
1329 else if (strcmp(szFormat, "") == 0)
1331 Handle(PS_Driver) aPSDriver = new PS_Driver(argv[2], aWidth, aHeight, aColorType);
1332 QAViewer2dTest::CurrentView()->Plot(aPSDriver, aXCenter, aYCenter, aScale);
1334 else if (strcmp(szFormat, "") == 0)
1344 //=======================================================================
1345 //function : V2dTextColor
1346 //purpose : v2dsettextcolor text_name color_index
1347 //=======================================================================
1348 static int V2dTextColor (Draw_Interpretor& , int argc, char** argv)
1352 cerr << "Usage : v2dsettextcolor text_name color_index" << endl;
1356 //////////////////TCollection_AsciiString aName (argv[1]);
1357 TCollection_ExtendedString ExtendedString (argv[1]);
1358 Standard_ExtString aName = ExtendedString.ToExtString();
1360 if (!myMapOfText.IsBound(aName))
1362 cout << "No such text object: " << aName << endl;
1366 int aColInd = atoi(argv[2]);
1369 // check color index
1370 Standard_Integer aColMin, aColMax;
1371 QAViewer2dTest::CurrentView()->Driver()->ColorBoundIndexs(aColMin, aColMax);
1372 if (aColInd < aColMin || aColMax < aColInd)
1374 cout << "Color index must be between " << aColMin << " and " << aColMax << endl;
1379 myMapOfText(aName)->SetColorIndex(aColInd);
1380 QAViewer2dTest::CurrentView()->Viewer()->Update();
1384 //=======================================================================
1385 //function : V2dText
1386 //purpose : v2dtext text_str position(x y) [angle scale font]
1387 //=======================================================================
1388 static int V2dText (Draw_Interpretor& , int argc, char** argv)
1390 if (argc != 4 && argc != 7)
1392 cerr << "Usage : v2dtext text_str position(x y) [angle scale font]" << endl;
1396 Handle(AIS2D_InteractiveContext) myAIScontext = QAViewer2dTest::GetAIS2DContext();
1397 if (myAIScontext.IsNull())
1399 cerr << "use 'v2dinit' command before " << argv[0] << endl;
1402 myAIScontext->SetPickMode(Graphic2d_PM_INTERSECT);
1403 Handle(V2d_View) V = QAViewer2dTest::CurrentView();
1405 Handle(Graphic2d_GraphicObject) obj =
1406 new Graphic2d_GraphicObject(V->View());
1408 Quantity_PlaneAngle anAngle = 0.0;
1409 Quantity_Factor aScale = 1.0;
1410 Standard_Integer aFontIn = 0;
1414 anAngle = atof(argv[4]);
1415 aScale = atof(argv[5]);
1416 aFontIn = atoi(argv[6]);
1420 Standard_Integer aFontMin, aFontMax;
1421 QAViewer2dTest::CurrentView()->Driver()->FontBoundIndexs(aFontMin, aFontMax);
1422 if (aFontIn < aFontMin || aFontMax < aFontIn)
1424 cout << "Font index must be between " << aFontMin << " and " << aFontMax << endl;
1430 Handle(Graphic2d_Text) text =
1431 new Graphic2d_Text(obj, TCollection_ExtendedString(argv[1]),
1432 atof(argv[2]), atof(argv[3]), anAngle,
1433 Aspect_TOT_SOLID, aScale);
1434 text->SetFontIndex(aFontIn);
1439 int i = myMapOfText.Extent();
1441 TCollection_AsciiString aTxtName ("text_");
1442 aTxtName += TCollection_AsciiString(i);
1444 //////////////////myMapOfText.Bind(aTxtName, text);
1445 TCollection_ExtendedString ExtendedString (aTxtName);
1446 Standard_ExtString ExtString_aTxtName = ExtendedString.ToExtString();
1447 myMapOfText.Bind(ExtString_aTxtName, text);
1449 cout << "Text " << aTxtName << " created" << endl;
1453 //=======================================================================
1454 //function : ViewerCommands
1456 //=======================================================================
1457 void QAViewer2dTest::ViewerCommands(Draw_Interpretor& theCommands)
1459 char *group = "2D AIS Viewer - Viewer Commands";
1461 theCommands.Add("v2dinit",
1462 "v2dinit : Create the Viewer window",
1463 __FILE__, V2dInit, group);
1465 theCommands.Add("v2dsetbg",
1466 "v2dsetbg imagefile [filltype] : Load image as background",
1467 __FILE__, V2dSetBG, group);
1469 theCommands.Add("v2dgrid",
1470 "v2dgrid [type [Origin(X Y) XStep/RadiusStep YStep/DivisionNb RotationAngle(in radians)] [drawmode]] : Load grid",
1471 __FILE__, V2dGrid, group);
1473 theCommands.Add("v2drmgrid",
1474 "v2drmgrid : Unload grid",
1475 __FILE__, V2dGrid, group);
1477 // theCommands.Add("v2dplot",
1478 // "v2dplot format filename : Dump in file in certain format",
1479 // __FILE__, V2dPlot, group);
1481 theCommands.Add("v2dpsout",
1482 "v2dpsout imagefile [Scale TypeOfColorSpace] [Width Height XCenter YCenter] : PostScript Output",
1483 __FILE__, V2dPSOut, group);
1485 theCommands.Add("v2dpick",
1486 "v2dpick (MB1 in the viewer) : Print pixel coords and color",
1487 __FILE__, V2dPick, group);
1489 theCommands.Add("v2dpickgrid",
1490 "v2dpickgrid (MB1 in the viewer) : Print coordinates of a grid point near to MB1 click",
1491 __FILE__, V2dPickGrid, group);
1493 theCommands.Add("v2dfit",
1494 "v2dfit or <F> : Fit all shapes",
1495 __FILE__, V2dFit, group);
1497 theCommands.Add("v2drepaint",
1498 "v2drepaint : Force redraw",
1499 __FILE__, V2dRepaint, group);
1501 theCommands.Add("v2dclear",
1502 "v2dclear : Clear the viewer",
1503 __FILE__, V2dClear, group);
1505 theCommands.Add("v2dtext", "v2dtext text_str position(x y) [angle scale font]", V2dText, group);
1506 theCommands.Add("v2dsettextcolor", "v2dsettextcolor text_name color_index", V2dTextColor, group);
1508 // theCommands.Add("v2dhelp",
1509 // "v2dhelp : Display help on the viewer commands",
1510 // __FILE__, V2dHelp, group);