0020750: DrawAppliInit file from the current directory is used during Draw starting.
[occt.git] / src / ViewerTest / ViewerTest_ViewerCommands.cxx
CommitLineData
7fd59977 1// File: ViewerTest_ViewerCommands.cxx
2// Created: Tue Sep 1 10:28:35 1998
3// Author: Robert COUBLANC
4// <rob@robox.paris1.matra-dtv.fr>
5
6// Robert Boehne 30 May 2000 : Dec Osf
7
8#ifdef HAVE_CONFIG_H
9# include <config.h>
10#endif
11
12#ifdef WNT
13#include <windows.h>
14#endif
15
16#include <Graphic3d_ExportFormat.hxx>
17#include <ViewerTest.hxx>
18#include <ViewerTest_EventManager.hxx>
19#include <Visual3d_View.hxx>
20#include <NIS_View.hxx>
21#include <NIS_Triangulated.hxx>
22#include <NIS_InteractiveContext.hxx>
23#include <AIS_InteractiveContext.hxx>
24#include <Draw_Interpretor.hxx>
25#include <Draw.hxx>
26#include <Draw_Appli.hxx>
7edf74fd
A
27#include <Aspect_PrintAlgo.hxx>
28#include <Image_PixMap.hxx>
7fd59977 29
30#ifndef WNT
31#include <Graphic3d_GraphicDevice.hxx>
32#include <Xw_GraphicDevice.hxx>
33#include <Xw_WindowQuality.hxx>
34#include <Xw_Window.hxx>
35#include <X11/Xlib.h> /* contains some dangerous #defines such as Status, True etc. */
36#include <X11/Xutil.h>
37#include <tk.h>
38
39#else
40
41#include <Graphic3d_WNTGraphicDevice.hxx>
42#include <WNT_WClass.hxx>
43#include <WNT_Window.hxx>
44
45#define _CRT_SECURE_NO_DEPRECATE
46#pragma warning (disable:4996)
47
48#endif
49
50#define OCC120
51
52//==============================================================================
53
54//==============================================================================
55// VIEWER GLOBAL VARIABLES
56//==============================================================================
57
58Standard_IMPORT Standard_Boolean Draw_VirtualWindows;
59
60Standard_EXPORT int ViewerMainLoop(Standard_Integer , const char** argv);
61extern const Handle(NIS_InteractiveContext)& TheNISContext();
62
63#ifdef WNT
64static Handle(Graphic3d_WNTGraphicDevice)& GetG3dDevice(){
65 static Handle(Graphic3d_WNTGraphicDevice) GD;
66 return GD;
67}
68
69static Handle(WNT_Window)& VT_GetWindow() {
70 static Handle(WNT_Window) WNTWin;
71 return WNTWin;
72}
73
74#else
75static Handle(Graphic3d_GraphicDevice)& GetG3dDevice(){
76 static Handle(Graphic3d_GraphicDevice) GD;
77 return GD;
78}
79static Handle(Xw_Window)& VT_GetWindow(){
80 static Handle(Xw_Window) XWWin;
81 return XWWin;
82}
83static Display *display;
84
85static void VProcessEvents(ClientData,int);
86#endif
87
88#ifdef OCC120
89static Standard_Boolean DegenerateMode = Standard_True;
90#endif
91
92#define ZCLIPWIDTH 1.
93
94static void OSWindowSetup();
95
96//==============================================================================
97// EVENT GLOBAL VARIABLES
98//==============================================================================
99
100static int Start_Rot = 0;
101static int ZClipIsOn = 0;
102static int X_Motion= 0,Y_Motion=0; // Current cursor position
103static int X_ButtonPress = 0, Y_ButtonPress = 0; // Last ButtonPress position
104
105
106//==============================================================================
107
108#ifdef WNT
109static LRESULT WINAPI ViewerWindowProc(
110 HWND hwnd,
111 UINT uMsg,
112 WPARAM wParam,
113 LPARAM lParam );
114static LRESULT WINAPI AdvViewerWindowProc(
115 HWND hwnd,
116 UINT uMsg,
117 WPARAM wParam,
118 LPARAM lParam );
119#endif
120
121
122//==============================================================================
123//function : WClass
124//purpose :
125//==============================================================================
126
127const Handle(MMgt_TShared)& ViewerTest::WClass()
128{
129 static Handle(MMgt_TShared) theWClass;
130#ifdef WNT
131 if (theWClass.IsNull()) {
132 theWClass = new WNT_WClass ("GW3D_Class", AdvViewerWindowProc,
133 CS_VREDRAW | CS_HREDRAW, 0, 0,
134 ::LoadCursor (NULL, IDC_ARROW));
135 }
136#endif
137 return theWClass;
138}
139
140//==============================================================================
141//function : ViewerInit
142//purpose : Create the window viewer and initialize all the global variable
143//==============================================================================
144
145void ViewerTest::ViewerInit (const Standard_Integer thePxLeft, const Standard_Integer thePxTop,
146 const Standard_Integer thePxWidth, const Standard_Integer thePxHeight)
147{
148 static Standard_Boolean isFirst = Standard_True;
149
150 Standard_Integer aPxLeft = 0;
151 Standard_Integer aPxTop = 460;
152 Standard_Integer aPxWidth = 409;
153 Standard_Integer aPxHeight = 409;
154 if (thePxWidth != 0 && thePxHeight != 0)
155 {
156 aPxLeft = thePxLeft;
157 aPxTop = thePxTop;
158 aPxWidth = thePxWidth;
159 aPxHeight = thePxHeight;
160 }
161
162 if (isFirst)
163 {
164 // Create the Graphic device
165#ifdef WNT
166 if (GetG3dDevice().IsNull()) GetG3dDevice() = new Graphic3d_WNTGraphicDevice();
167 if (VT_GetWindow().IsNull())
168 {
169 // Create the Graphic device and the window
170 Handle(WNT_GraphicDevice) g_Device = new WNT_GraphicDevice();
171
172 VT_GetWindow() = new WNT_Window (g_Device, "Test3d",
173 Handle(WNT_WClass)::DownCast (WClass()),
174 WS_OVERLAPPEDWINDOW,
175 aPxLeft, aPxTop,
176 aPxWidth, aPxHeight,
177 Quantity_NOC_BLACK);
178 VT_GetWindow()->SetVirtual (Draw_VirtualWindows);
179 }
180#else
181 if (GetG3dDevice().IsNull()) GetG3dDevice() =
182 new Graphic3d_GraphicDevice (getenv ("DISPLAY"), Xw_TOM_READONLY);
183 if (VT_GetWindow().IsNull())
184 {
185 VT_GetWindow() = new Xw_Window (GetG3dDevice(),
186 "Test3d",
187 aPxLeft, aPxTop,
188 aPxWidth, aPxHeight,
189 Xw_WQ_3DQUALITY,
190 Quantity_NOC_BLACK);
191 VT_GetWindow()->SetVirtual (Draw_VirtualWindows);
192 }
193#endif
194
195 Handle(V3d_Viewer) a3DViewer, a3DCollector;
196 // Viewer and View creation
197
198 TCollection_ExtendedString NameOfWindow("Visu3D");
199
200 a3DViewer = new V3d_Viewer(GetG3dDevice(), NameOfWindow.ToExtString());
201 NameOfWindow = TCollection_ExtendedString("Collector");
202 a3DCollector = new V3d_Viewer(GetG3dDevice(), NameOfWindow.ToExtString());
203 a3DViewer->SetDefaultBackgroundColor(Quantity_NOC_BLACK);
204 a3DCollector->SetDefaultBackgroundColor(Quantity_NOC_STEELBLUE);
205 Handle(NIS_View) aView =
206 Handle(NIS_View)::DownCast(ViewerTest::CurrentView());
207 if ( aView.IsNull() ) {
208 // Handle (V3d_View) V = a3DViewer->CreateView();
209 aView = new NIS_View (a3DViewer, VT_GetWindow());
210 ViewerTest::CurrentView(aView);
211 TheNISContext()->AttachView (aView);
212 }
213 Handle(V3d_View) a3DViewCol;
214 if ( a3DViewCol.IsNull() ) a3DViewCol = a3DViewer->CreateView();
215
216 // AIS setup
217 if ( ViewerTest::GetAISContext().IsNull() ) {
218 Handle(AIS_InteractiveContext) C =
219 new AIS_InteractiveContext(a3DViewer,a3DCollector);
220 ViewerTest::SetAISContext(C);
221 }
222
223 // Setup for X11 or NT
224 OSWindowSetup();
225 // Viewer and View creation
226
227 a3DViewer->SetDefaultBackgroundColor(Quantity_NOC_BLACK);
228
229 Handle (V3d_View) V = ViewerTest::CurrentView();
230
231 V->SetDegenerateModeOn();
232#ifdef OCC120
233 DegenerateMode = V->DegenerateModeIsOn();
234#endif
235 // V->SetWindow(VT_GetWindow(), NULL, MyViewProc, NULL);
236
237 V->SetZClippingDepth(0.5);
238 V->SetZClippingWidth(ZCLIPWIDTH/2.);
239 a3DViewer->SetDefaultLights();
240 a3DViewer->SetLightOn();
241
242#ifndef WNT
243#if TCL_MAJOR_VERSION < 8
244 Tk_CreateFileHandler((void*)ConnectionNumber(display),
245 TK_READABLE, VProcessEvents, (ClientData) VT_GetWindow()->XWindow() );
246#else
247 Tk_CreateFileHandler(ConnectionNumber(display),
248 TK_READABLE, VProcessEvents, (ClientData) VT_GetWindow()->XWindow() );
249#endif
250#endif
251
252 isFirst = Standard_False;
253 }
254 VT_GetWindow()->Map();
255}
256
257//==============================================================================
258//function : Vinit
259//purpose : Create the window viewer and initialize all the global variable
260// Use Tk_CreateFileHandler on UNIX to cath the X11 Viewer event
261//==============================================================================
262
263static int VInit (Draw_Interpretor& , Standard_Integer argc, const char** argv)
264{
265 Standard_Integer aPxLeft = (argc > 1) ? atoi (argv[1]) : 0;
266 Standard_Integer aPxTop = (argc > 2) ? atoi (argv[2]) : 0;
267 Standard_Integer aPxWidth = (argc > 3) ? atoi (argv[3]) : 0;
268 Standard_Integer aPxHeight = (argc > 4) ? atoi (argv[4]) : 0;
269 ViewerTest::ViewerInit (aPxLeft, aPxTop, aPxWidth, aPxHeight);
270 return 0;
271}
272
273//==============================================================================
274//function : ProcessKeyPress
275//purpose : Handle KeyPress event from a CString
276//==============================================================================
277
278static void ProcessKeyPress( char *buf_ret )
279{
280 //cout << "KeyPress" << endl;
281 const Handle(V3d_View) aView = ViewerTest::CurrentView();
282 const Handle(NIS_View) aNisView = Handle(NIS_View)::DownCast (aView);
283 // Letter in alphabetic order
284
285 if ( !strcasecmp(buf_ret, "A") ) {
286 // AXO
287 aView->SetProj(V3d_XposYnegZpos);
288 }
289 else if ( !strcasecmp(buf_ret, "D") ) {
290 // Reset
291 aView->Reset();
292 }
293 else if ( !strcasecmp(buf_ret, "F") ) {
294 // FitAll
295 if (aNisView.IsNull())
296 aView->FitAll();
297 else
298 aNisView->FitAll3d();
299 }
300 else if ( !strcasecmp(buf_ret, "H") ) {
301 // HLR
302 cout << "HLR" << endl;
303#ifdef OCC120
304 if (aView->DegenerateModeIsOn()) ViewerTest::CurrentView()->SetDegenerateModeOff();
305 else aView->SetDegenerateModeOn();
306 DegenerateMode = aView->DegenerateModeIsOn();
307#else
308 ViewerTest::CurrentView()->SetDegenerateModeOff();
309#endif
310 }
311 else if ( !strcasecmp(buf_ret, "D") ) {
312 // Reset
313 aView->Reset();
314 }
315 else if ( !strcasecmp(buf_ret, "S") ) {
316 // SHADING
317 cout << "passage en mode 1 (shading pour les shapes)" << endl;
318#ifndef OCC120
319 ViewerTest::CurrentView()->SetDegenerateModeOn();
320#endif
321 Handle(AIS_InteractiveContext) Ctx = ViewerTest::GetAISContext();
322 if(Ctx->NbCurrents()==0 ||
323 Ctx->NbSelected()==0)
324 Ctx->SetDisplayMode(AIS_Shaded);
325 else{
326 if(Ctx->HasOpenedContext()){
327 for(Ctx->InitSelected();Ctx->MoreSelected();Ctx->NextSelected())
328 Ctx->SetDisplayMode(Ctx->Interactive(),1,Standard_False);
329 }
330 else{
331 for(Ctx->InitCurrent();Ctx->MoreCurrent();Ctx->NextCurrent())
332 Ctx->SetDisplayMode(Ctx->Current(),1,Standard_False);
333 }
334 Ctx->UpdateCurrentViewer();
335 }
336 }
337 else if ( !strcasecmp(buf_ret, "U") ) {
338 // SHADING
339 cout<<"passage au mode par defaut"<<endl;
340#ifndef OCC120
341 ViewerTest::CurrentView()->SetDegenerateModeOn();
342#endif
343 Handle(AIS_InteractiveContext) Ctx = ViewerTest::GetAISContext();
344 if(Ctx->NbCurrents()==0 ||
345 Ctx->NbSelected()==0)
346 Ctx->SetDisplayMode(AIS_WireFrame);
347 else{
348 if(Ctx->HasOpenedContext()){
349 for(Ctx->InitSelected();Ctx->MoreSelected();Ctx->NextSelected())
350 Ctx->UnsetDisplayMode(Ctx->Interactive(),Standard_False);
351 }
352 else{
353 for(Ctx->InitCurrent();Ctx->MoreCurrent();Ctx->NextCurrent())
354 Ctx->UnsetDisplayMode(Ctx->Current(),Standard_False);
355 }
356 Ctx->UpdateCurrentViewer();
357 }
358
359 }
360 else if ( !strcasecmp(buf_ret, "T") ) {
361 // Top
362 aView->SetProj(V3d_Zpos);
363 }
364 else if ( !strcasecmp(buf_ret, "B") ) {
365 // Top
366 aView->SetProj(V3d_Zneg);
367 }
368 else if ( !strcasecmp(buf_ret, "L") ) {
369 // Top
370 aView->SetProj(V3d_Xneg);
371 }
372 else if ( !strcasecmp(buf_ret, "R") ) {
373 // Top
374 aView->SetProj(V3d_Xpos);
375 }
376
377 else if ( !strcasecmp(buf_ret, "W") ) {
378 // WIREFRAME
379#ifndef OCC120
380 ViewerTest::CurrentView()->SetDegenerateModeOn();
381#endif
382 cout << "passage en mode 0 (filaire pour les shapes)" << endl;
383#ifndef OCC120
384 ViewerTest::CurrentView()->SetDegenerateModeOn();
385#endif
386 Handle(AIS_InteractiveContext) Ctx = ViewerTest::GetAISContext();
387 if(Ctx->NbCurrents()==0 ||
388 Ctx->NbSelected()==0)
389 Ctx->SetDisplayMode(AIS_WireFrame);
390 else{
391 if(Ctx->HasOpenedContext()){
392 for(Ctx->InitSelected();Ctx->MoreSelected();Ctx->NextSelected())
393 Ctx->SetDisplayMode(Ctx->Interactive(),0,Standard_False);
394 }
395 else{
396 for(Ctx->InitCurrent();Ctx->MoreCurrent();Ctx->NextCurrent())
397 Ctx->SetDisplayMode(Ctx->Current(),0,Standard_False);
398 }
399 Ctx->UpdateCurrentViewer();
400 }
401 }
402 else if ( !strcasecmp(buf_ret, "Z") ) {
403 // ZCLIP
404
405 if ( ZClipIsOn ) {
406 cout << "ZClipping OFF" << endl;
407 ZClipIsOn = 0;
408
409 aView->SetZClippingType(V3d_OFF);
410 aView->Redraw();
411 }
412 else {
413 cout << "ZClipping ON" << endl;
414 ZClipIsOn = 1;
415
416 aView->SetZClippingType(V3d_FRONT);
417 aView->Redraw();
418 }
419 }
420 else if ( !strcasecmp(buf_ret, ",") ) {
421 ViewerTest::GetAISContext()->HilightNextDetected(ViewerTest::CurrentView());
422
423
424 }
425 else if ( !strcasecmp(buf_ret, ".") ) {
426 ViewerTest::GetAISContext()->HilightPreviousDetected(ViewerTest::CurrentView());
427 }
428 // Number
429 else{
430 Standard_Integer Num = atoi(buf_ret);
431 if(Num>=0 && Num<=7)
432 ViewerTest::StandardModeActivation(Num);
433 }
434}
435
436//==============================================================================
437//function : ProcessExpose
438//purpose : Redraw the View on an Expose Event
439//==============================================================================
440
441static void ProcessExpose( )
442{ //cout << "Expose" << endl;
443 ViewerTest::CurrentView()->Redraw();
444}
445
446//==============================================================================
447//function : ProcessConfigure
448//purpose : Resize the View on an Configure Event
449//==============================================================================
450
451static void ProcessConfigure()
452{
453 Handle(V3d_View) V = ViewerTest::CurrentView();
454 V->MustBeResized();
455 V->Update();
456 V->Redraw();
457}
458
459//==============================================================================
460//function : ProcessButton1Press
461//purpose : Picking
462//==============================================================================
463
464static Standard_Boolean ProcessButton1Press(
465 Standard_Integer ,
466 const char** argv,
467 Standard_Boolean pick,
468 Standard_Boolean shift )
469{
470 Handle(ViewerTest_EventManager) EM = ViewerTest::CurrentEventManager();
471 if ( pick ) {
472 Standard_Real X, Y, Z;
473
474 ViewerTest::CurrentView()->Convert(X_Motion, Y_Motion, X, Y, Z);
475
476 Draw::Set(argv[1], X);
477 Draw::Set(argv[2], Y);
478 Draw::Set(argv[3], Z);}
479
480 if(shift)
481 EM->ShiftSelect();
482 else
483 EM->Select();
484
485 pick = 0;
486 return pick;
487}
488
489//==============================================================================
490//function : ProcessButton3Press
491//purpose : Start Rotation
492//==============================================================================
493
494static void ProcessButton3Press()
495
496{ // Start rotation
497 Start_Rot = 1;
498 ViewerTest::CurrentView()->SetDegenerateModeOn();
499 ViewerTest::CurrentView()->StartRotation( X_ButtonPress, Y_ButtonPress );
500
501}
502//==============================================================================
503//function : ProcessButtonRelease
504//purpose : Start Rotation
505//==============================================================================
506
507static void ProcessButtonRelease()
508
509{ // End rotation
510#ifdef OCC120
511 if (Start_Rot) {
512 Start_Rot = 0;
513 if (!DegenerateMode) ViewerTest::CurrentView()->SetDegenerateModeOff();
514 }
515#else
516 Start_Rot = 0;
517 ViewerTest::CurrentView()->SetDegenerateModeOff();
518#endif
519}
520
521//==============================================================================
522//function : ProcessZClipMotion
523//purpose : Zoom
524//==============================================================================
525
526void ProcessZClipMotion()
527{
528 Handle(V3d_View) a3DView = ViewerTest::CurrentView();
529 if ( Abs(X_Motion - X_ButtonPress) > 2 ) {
530 static Standard_Real CurZPos = 0.;
531
532 //Quantity_Length VDX, VDY;
533 //a3DView->Size(VDX,VDY);
534 //Standard_Real VDZ = a3DView->ZSize();
535 //printf("View size (%lf,%lf,%lf)\n", VDX, VDY, VDZ);
536
537 Quantity_Length dx = a3DView->Convert(X_Motion - X_ButtonPress);
538
539 // Front = Depth + width/2.
540 Standard_Real D = 0.5;
541 Standard_Real W = 0.1;
542
543 CurZPos += (dx);
544
545 D += CurZPos;
546
547 //printf("dx %lf Depth %lf Width %lf\n", dx, D, W);
548
549 a3DView->SetZClippingType(V3d_OFF);
550 a3DView->SetZClippingDepth(D);
551 a3DView->SetZClippingWidth(W);
552 a3DView->SetZClippingType(V3d_FRONT);
553
554 a3DView->Redraw();
555
556 X_ButtonPress = X_Motion;
557 Y_ButtonPress = Y_Motion;
558 }
559}
560
561//==============================================================================
562//function : ProcessControlButton1Motion
563//purpose : Zoom
564//==============================================================================
565
566static void ProcessControlButton1Motion()
567{
568 ViewerTest::CurrentView()->Zoom( X_ButtonPress, Y_ButtonPress, X_Motion, Y_Motion);
569
570 X_ButtonPress = X_Motion;
571 Y_ButtonPress = Y_Motion;
572}
573
574//==============================================================================
575//function : ProcessControlButton2Motion
576//purpose : Pann
577//==============================================================================
578
579static void ProcessControlButton2Motion()
580{
581 Quantity_Length dx = ViewerTest::CurrentView()->Convert(X_Motion - X_ButtonPress);
582 Quantity_Length dy = ViewerTest::CurrentView()->Convert(Y_Motion - Y_ButtonPress);
583
584 dy = -dy; // Xwindow Y axis is from top to Bottom
585
586 ViewerTest::CurrentView()->Panning( dx, dy );
587
588 X_ButtonPress = X_Motion;
589 Y_ButtonPress = Y_Motion;
590}
591
592//==============================================================================
593//function : ProcessControlButton3Motion
594//purpose : Rotation
595//==============================================================================
596
597static void ProcessControlButton3Motion()
598{
599 if ( Start_Rot ) ViewerTest::CurrentView()->Rotation( X_Motion, Y_Motion);
600}
601
602//==============================================================================
603//function : ProcessPointerMotion
604//purpose : Rotation
605//==============================================================================
606
607static void ProcessMotion()
608{
609 //pre-hilights detected objects at mouse position
610
611 Handle(ViewerTest_EventManager) EM = ViewerTest::CurrentEventManager();
612 EM->MoveTo(X_Motion, Y_Motion);
613}
614
615
616void ViewerTest::GetMousePosition(Standard_Integer& Xpix,Standard_Integer& Ypix)
617{
618 Xpix = X_Motion;Ypix=Y_Motion;
619}
620
621//==============================================================================
622//function : VAxo
623//purpose : Switch to an Axonometric view
624//Draw arg : No args
625//==============================================================================
626
627static int VAxo(Draw_Interpretor& di, Standard_Integer , const char** )
628{ if ( ViewerTest::CurrentView().IsNull() ) {
629 di<<"La commande vinit n'a pas ete appele avant"<<"\n";
630 // VInit(di, argc, argv);
631}
632
633ViewerTest::CurrentView()->SetProj(V3d_XposYnegZpos);
634
635return 0;
636}
637
638//==============================================================================
639//function : VTop
640//purpose : Switch to a Top View
641//Draw arg : No args
642//==============================================================================
643
644static int VTop(Draw_Interpretor& di, Standard_Integer , const char** )
645{
646
647 if ( ViewerTest::CurrentView().IsNull() ) {
648 di<<"La commande vinit n'a pas ete appele avant"<<"\n";
649
650 // VInit(di, , argv);
651 }
652
653 ViewerTest::CurrentView()->SetProj(V3d_Zpos);
654 return 0;
655
656}
657
658//==============================================================================
659//function : VHelp
660//purpose : Dsiplay help on viewer Keyboead and mouse commands
661//Draw arg : No args
662//==============================================================================
663
664static int VHelp(Draw_Interpretor& di, Standard_Integer , const char** )
665{
666
667 di << "Q : Quit the application" << "\n";
668
669 di << "========================="<<"\n";
670 di << "F : FitAll" << "\n";
671 di << "T : TopView" << "\n";
672 di << "A : AxonometricView" << "\n";
673 di << "R : ResetView" << "\n";
674
675 di << "========================="<<"\n";
676 di << "S : Shading" << "\n";
677 di << "W : Wireframe" << "\n";
678 di << "H : HidelLineRemoval" << "\n";
679
680 di << "========================="<<"\n";
681 di << "Selection mode "<<"\n";
682 di << "0 : Shape" <<"\n";
683 di << "1 : Vertex" <<"\n";
684 di << "2 : Edge" <<"\n";
685 di << "3 : Wire" <<"\n";
686 di << "4 : Face" <<"\n";
687 di << "5 : Shell" <<"\n";
688 di << "6 : Solid" <<"\n";
689 di << "7 : Compound" <<"\n";
690
691 di << "=========================="<<"\n";
692 di << "D : Remove Selected Object"<<"\n";
693 di << "=========================="<<"\n";
694
695 return 0;
696}
697
698Standard_Boolean IsDragged = Standard_False;
699
700Standard_Integer xx1, yy1, xx2, yy2;
701//the first and last point in viewer co-ordinates
702
703Standard_Boolean DragFirst;
704
705
706#ifdef WNT
707
708static Standard_Boolean Ppick = 0;
709static Standard_Integer Pargc = 0;
710static const char** Pargv = NULL;
711
712
713static LRESULT WINAPI AdvViewerWindowProc( HWND hwnd,
714 UINT Msg,
715 WPARAM wParam,
716 LPARAM lParam )
717{
718 if ( !ViewerTest::CurrentView().IsNull() ) {
719
720 WPARAM fwKeys = wParam;
721
722 switch( Msg ) {
723
724 case WM_LBUTTONUP:
725 IsDragged = Standard_False;
726 if( !DragFirst )
727 {
728 HDC hdc = GetDC( hwnd );
729 HGDIOBJ anObj = SelectObject( hdc, GetStockObject( WHITE_PEN ) );
730 SelectObject( hdc, GetStockObject( HOLLOW_BRUSH ) );
731 SetROP2( hdc, R2_NOT );
732 Rectangle( hdc, xx1, yy1, xx2, yy2 );
733 ReleaseDC( hwnd, hdc );
734
735 const Handle(ViewerTest_EventManager) EM =
736 ViewerTest::CurrentEventManager();
737 if ( fwKeys & MK_SHIFT )
738 EM->ShiftSelect( min( xx1, xx2 ), max( yy1, yy2 ),
739 max( xx1, xx2 ), min( yy1, yy2 ));
740 else
741 EM->Select( min( xx1, xx2 ), max( yy1, yy2 ),
742 max( xx1, xx2 ), min( yy1, yy2 ));
743 }
744 return ViewerWindowProc( hwnd, Msg, wParam, lParam );
745
746 case WM_LBUTTONDOWN:
747 if( fwKeys == MK_LBUTTON || fwKeys == ( MK_LBUTTON | MK_SHIFT ) )
748 {
749 IsDragged = Standard_True;
750 DragFirst = Standard_True;
751 xx1 = LOWORD(lParam);
752 yy1 = HIWORD(lParam);
753 }
754 return ViewerWindowProc( hwnd, Msg, wParam, lParam );
755
756 break;
757
758 case WM_MOUSEMOVE:
759 if( IsDragged )
760 {
761 HDC hdc = GetDC( hwnd );
762
763 HGDIOBJ anObj = SelectObject( hdc, GetStockObject( WHITE_PEN ) );
764 SelectObject( hdc, GetStockObject( HOLLOW_BRUSH ) );
765 SetROP2( hdc, R2_NOT );
766
767 if( !DragFirst )
768 Rectangle( hdc, xx1, yy1, xx2, yy2 );
769
770 DragFirst = Standard_False;
771 xx2 = LOWORD(lParam);
772 yy2 = HIWORD(lParam);
773
774 Rectangle( hdc, xx1, yy1, xx2, yy2 );
775
776 SelectObject( hdc, anObj );
777
778 ReleaseDC( hwnd, hdc );
779 }
780 else
781 return ViewerWindowProc( hwnd, Msg, wParam, lParam );
782 break;
783
784 default:
785 return ViewerWindowProc( hwnd, Msg, wParam, lParam );
786 }
787 return 0;
788 }
789 return ViewerWindowProc( hwnd, Msg, wParam, lParam );
790}
791
792
793static LRESULT WINAPI ViewerWindowProc( HWND hwnd,
794 UINT Msg,
795 WPARAM wParam,
796 LPARAM lParam )
797{
798 /*static Standard_Boolean Ppick = 0;
799 static Standard_Integer Pargc = 0;
800 static char** Pargv = NULL;*/
801
802 static int Up = 1;
803
804 if ( !ViewerTest::CurrentView().IsNull() ) {
805 PAINTSTRUCT ps;
806
807 switch( Msg ) {
808 case WM_CLOSE:
809 // do not destroy the window - just hide it!
810 VT_GetWindow()->Unmap();
811 return 0;
812 case WM_PAINT:
813 //cout << "\t WM_PAINT" << endl;
814 BeginPaint(hwnd, &ps);
815 EndPaint(hwnd, &ps);
816 ProcessExpose();
817 break;
818
819 case WM_SIZE:
820 //cout << "\t WM_SIZE" << endl;
821 ProcessConfigure();
822 break;
823
824 case WM_KEYDOWN:
825 //cout << "\t WM_KEYDOWN " << (int) wParam << endl;
826
827 if ( (wParam != VK_SHIFT) && (wParam != VK_CONTROL) ) {
828 char c[2];
829 c[0] = (char) wParam;
830 c[1] = '\0';
831 ProcessKeyPress( c);
832 }
833 break;
834
835 case WM_LBUTTONUP:
836 case WM_MBUTTONUP:
837 case WM_RBUTTONUP:
838 //cout << "\t WM_xBUTTONUP" << endl;
839 Up = 1;
840 ProcessButtonRelease();
841 break;
842
843 case WM_LBUTTONDOWN:
844 case WM_MBUTTONDOWN:
845 case WM_RBUTTONDOWN:
846 {
847 //cout << "\t WM_xBUTTONDOWN" << endl;
848 WPARAM fwKeys = wParam;
849
850 Up = 0;
851
852 X_ButtonPress = LOWORD(lParam);
853 Y_ButtonPress = HIWORD(lParam);
854
855 if ( Msg == WM_LBUTTONDOWN) {
856 if(fwKeys & MK_CONTROL) {
857 Ppick = ProcessButton1Press( Pargc, Pargv, Ppick, (fwKeys & MK_SHIFT) );
858 } else
859 ProcessButton1Press( Pargc, Pargv, Ppick, (fwKeys & MK_SHIFT) );
860 }
861 else if ( Msg == WM_RBUTTONDOWN ) {
862 // Start rotation
863 ProcessButton3Press( );
864 }
865 }
866 break;
867
868 case WM_MOUSEMOVE:
869 {
870 //cout << "\t WM_MOUSEMOVE" << endl;
871 WPARAM fwKeys = wParam;
872 X_Motion = LOWORD(lParam);
873 Y_Motion = HIWORD(lParam);
874
875 if ( Up &&
876 fwKeys & ( MK_LBUTTON|MK_MBUTTON|MK_RBUTTON ) ) {
877 Up = 0;
878 X_ButtonPress = LOWORD(lParam);
879 Y_ButtonPress = HIWORD(lParam);
880
881 if ( fwKeys & MK_RBUTTON ) {
882 // Start rotation
883 ProcessButton3Press();
884 }
885 }
886
887 if ( fwKeys & MK_CONTROL ) {
888 if ( fwKeys & MK_LBUTTON ) {
889 ProcessControlButton1Motion();
890 }
891 else if ( fwKeys & MK_MBUTTON ||
892 ((fwKeys&MK_LBUTTON) &&
893 (fwKeys&MK_RBUTTON) ) ){
894 ProcessControlButton2Motion();
895 }
896 else if ( fwKeys & MK_RBUTTON ) {
897 ProcessControlButton3Motion();
898 }
899 }
900#ifdef BUG
901 else if ( fwKeys & MK_SHIFT ) {
902 if ( fwKeys & MK_MBUTTON ||
903 ((fwKeys&MK_LBUTTON) &&
904 (fwKeys&MK_RBUTTON) ) ) {
905 cout << "ProcessZClipMotion()" << endl;
906 ProcessZClipMotion();
907 }
908 }
909#endif
910 else
911 if (( fwKeys & MK_MBUTTON || ((fwKeys&MK_LBUTTON) && (fwKeys&MK_RBUTTON) ) )){
912 ProcessZClipMotion();
913 }
914 else {
915 ProcessMotion();
916 }
917 }
918 break;
919
920 default:
921 return( DefWindowProc( hwnd, Msg, wParam, lParam ));
922 }
923 return 0L;
924 }
925
926 return DefWindowProc( hwnd, Msg, wParam, lParam );
927}
928
929
930
931
932//==============================================================================
933//function : ViewerMainLoop
934//purpose : Get a Event on the view and dispatch it
935//==============================================================================
936
937
938static int ViewerMainLoop(Standard_Integer argc, const char** argv)
939{
940
941 //cout << "No yet implemented on WNT" << endl;
942 /*static Standard_Boolean Ppick = 0;
943 static Standard_Integer Pargc = 0;
944 static char** Pargv = NULL;*/
945
946 //Ppick = (argc > 0)? -1 : 0;
947 Ppick = (argc > 0)? 1 : 0;
948 Pargc = argc;
949 Pargv = argv;
950
951 if ( Ppick ) {
952 MSG msg;
953 msg.wParam = 1;
954
955 cout << "Start picking" << endl;
956
957 //while ( Ppick == -1 ) {
958 while ( Ppick == 1 ) {
959 // Wait for a ProcessButton1Press() to toggle pick to 1 or 0
960 if (GetMessage(&msg, NULL, 0, 0) ) {
961 TranslateMessage(&msg);
962 DispatchMessage(&msg);
963 }
964 }
965
966 cout << "Picking done" << endl;
967 }
968
969 return Ppick;
970}
971
972
973#else
974
975int min( int a, int b )
976{
977 if( a<b )
978 return a;
979 else
980 return b;
981}
982
983int max( int a, int b )
984{
985 if( a>b )
986 return a;
987 else
988 return b;
989}
990
991int ViewerMainLoop(Standard_Integer argc, const char** argv)
992
993{ Standard_Boolean pick = argc > 0;
994
995// X11 Event loop
996
997static XEvent report;
998
999XNextEvent( display, &report );
1000// cout << "rep type = " << report.type << endl;
1001// cout << "rep button = " << report.xbutton.button << endl;
1002
1003switch ( report.type ) {
1004 case Expose:
1005 {
1006 ProcessExpose();
1007 }
1008 break;
1009 case ConfigureNotify:
1010 {
1011 ProcessConfigure();
1012 }
1013 break;
1014 case KeyPress:
1015 {
1016
1017 KeySym ks_ret ;
1018 char buf_ret[11] ;
1019 int ret_len ;
1020 XComposeStatus status_in_out;
1021
1022 ret_len = XLookupString( ( XKeyEvent *)&report ,
1023 (char *) buf_ret , 10 ,
1024 &ks_ret , &status_in_out ) ;
1025
1026
1027 buf_ret[ret_len] = '\0' ;
1028
1029 if ( ret_len ) {
1030 ProcessKeyPress( buf_ret);
1031 }
1032 }
1033 break;
1034 case ButtonPress:
1035 // cout << "ButtonPress" << endl;
1036 {
1037 X_ButtonPress = report.xbutton.x;
1038 Y_ButtonPress = report.xbutton.y;
1039
1040 if ( report.xbutton.button == Button1 )
1041 if( report.xbutton.state & ControlMask )
1042 pick = ProcessButton1Press( argc, argv, pick,
1043 ( report.xbutton.state & ShiftMask) );
1044 else
1045 {
1046 IsDragged = Standard_True;
1047 xx1 = X_ButtonPress;
1048 yy1 = Y_ButtonPress;
1049 DragFirst = Standard_True;
1050 }
1051 else if ( report.xbutton.button == Button3 )
1052 // Start rotation
1053 ProcessButton3Press();
1054 }
1055 break;
1056 case ButtonRelease:
1057 {
1058 // cout<<"relachement du bouton "<<(report.xbutton.button==3 ? "3": "on s'en fout") <<endl;
1059 // cout << IsDragged << endl;
1060 // cout << DragFirst << endl;
1061
1062 if( IsDragged )
1063 {
1064 if( !DragFirst )
1065 {
1066 Aspect_Handle aWindow = VT_GetWindow()->XWindow();
1067 GC gc = XCreateGC( display, aWindow, 0, 0 );
1068 // XSetFunction( display, gc, GXinvert );
1069 XDrawRectangle( display, aWindow, gc, min( xx1, xx2 ), min( yy1, yy2 ), abs( xx2-xx1 ), abs( yy2-yy1 ) );
1070 }
1071
1072 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
1073 if( aContext.IsNull() )
1074 {
1075 cout << "The context is null. Please use vinit before createmesh" << endl;
1076 return 0;
1077 }
1078
1079 Standard_Boolean ShiftPressed = ( report.xbutton.state & ShiftMask );
1080 if( report.xbutton.button==1 )
1081 if( DragFirst )
1082 if( ShiftPressed )
1083 {
1084 aContext->ShiftSelect();
1085 // cout << "shift select" << endl;
1086 }
1087 else
1088 {
1089 aContext->Select();
1090 // cout << "select" << endl;
1091 }
1092 else
1093 if( ShiftPressed )
1094 {
1095 aContext->ShiftSelect( min( xx1, xx2 ), min( yy1, yy2 ),
1096 max( xx1, xx2 ), max( yy1, yy2 ),
1097 ViewerTest::CurrentView());
1098 // cout << "shift select" << endl;
1099 }
1100 else
1101 {
1102 aContext->Select( min( xx1, xx2 ), min( yy1, yy2 ),
1103 max( xx1, xx2 ), max( yy1, yy2 ),
1104 ViewerTest::CurrentView() );
1105 // cout << "select" << endl;
1106 }
1107 else
1108 ProcessButtonRelease();
1109
1110 IsDragged = Standard_False;
1111 }
1112 else
1113 ProcessButtonRelease();
1114 }
1115 break;
1116 case MotionNotify:
1117 {
1118 // XEvent dummy;
1119
1120 X_Motion = report.xmotion.x;
1121 Y_Motion = report.xmotion.y;
1122
1123 if( IsDragged )
1124 {
1125 Aspect_Handle aWindow = VT_GetWindow()->XWindow();
1126 GC gc = XCreateGC( display, aWindow, 0, 0 );
1127 XSetFunction( display, gc, GXinvert );
1128
1129 if( !DragFirst )
1130 XDrawRectangle( display, aWindow, gc, min( xx1, xx2 ), min( yy1, yy2 ), abs( xx2-xx1 ), abs( yy2-yy1 ) );
1131
1132 xx2 = X_Motion;
1133 yy2 = Y_Motion;
1134 DragFirst = Standard_False;
1135
1136 //cout << "draw rect : " << xx2 << ", " << yy2 << endl;
1137 XDrawRectangle( display, aWindow, gc, min( xx1, xx2 ), min( yy1, yy2 ), abs( xx2-xx1 ), abs( yy2-yy1 ) );
1138 }
1139 else
1140 {
1141
1142 //cout << "MotionNotify " << X_Motion << "," << Y_Motion << endl;
1143
1144 // remove all the ButtonMotionMask
1145 while( XCheckMaskEvent( display, ButtonMotionMask, &report) ) ;
1146
1147 if ( ZClipIsOn && report.xmotion.state & ShiftMask ) {
1148 if ( Abs(X_Motion - X_ButtonPress) > 2 ) {
1149
1150 Quantity_Length VDX, VDY;
1151
1152 ViewerTest::CurrentView()->Size(VDX,VDY);
1153 Standard_Real VDZ =0 ;
1154 VDZ = ViewerTest::CurrentView()->ZSize();
1155
1156 // printf("%lf,%lf,%lf\n", VDX, VDY, VDZ);
1157 printf("%f,%f,%f\n", VDX, VDY, VDZ);
1158
1159 Quantity_Length dx = 0 ;
1160 dx = ViewerTest::CurrentView()->Convert(X_Motion - X_ButtonPress);
1161
1162 cout << dx << endl;
1163
1164 dx = dx / VDX * VDZ;
1165
1166 cout << dx << endl;
1167
1168 // Front = Depth + width/2.
1169 //ViewerTest::CurrentView()->SetZClippingDepth(dx);
1170 //ViewerTest::CurrentView()->SetZClippingWidth(0.);
1171
1172 ViewerTest::CurrentView()->Redraw();
1173 }
1174 }
1175
1176 if ( report.xmotion.state & ControlMask ) {
1177 if ( report.xmotion.state & Button1Mask ) {
1178 ProcessControlButton1Motion();
1179 }
1180 else if ( report.xmotion.state & Button2Mask ) {
1181 ProcessControlButton2Motion();
1182 }
1183 else if ( report.xmotion.state & Button3Mask ) {
1184 ProcessControlButton3Motion();
1185 }
1186 }
1187 else {
1188 ProcessMotion();
1189 }
1190 }
1191 }
1192 break;
1193}
1194
1195
1196return pick;
1197}
1198
1199//==============================================================================
1200//function : VProcessEvents
1201//purpose : call by Tk_CreateFileHandler() to be able to manage the
1202// event in the Viewer window
1203//==============================================================================
1204
1205static void VProcessEvents(ClientData,int)
1206{
1207 //cout << "VProcessEvents" << endl;
1208
1209 // test for X Event
1210 while (XPending(display)) {
1211 ViewerMainLoop( 0, NULL);
1212 }
1213}
1214#endif
1215
1216//==============================================================================
1217//function : OSWindowSetup
1218//purpose : Setup for the X11 window to be able to cath the event
1219//==============================================================================
1220
1221
1222static void OSWindowSetup()
1223{
1224#ifndef WNT
1225 // X11
1226
1227 Window window = VT_GetWindow()->XWindow();
1228
1229 Standard_Address theDisplay = GetG3dDevice()->XDisplay();
1230 display = (Display * ) theDisplay;
1231 // display = (Display *)GetG3dDevice()->XDisplay();
1232
1233 XSynchronize(display, 1);
1234
1235 VT_GetWindow()->Map();
1236
1237 // X11 : For keyboard on SUN
1238 XWMHints wmhints;
1239 wmhints.flags = InputHint;
1240 wmhints.input = 1;
1241
1242 XSetWMHints( display, window, &wmhints);
1243
1244 XSelectInput( display, window, ExposureMask | KeyPressMask |
1245 ButtonPressMask | ButtonReleaseMask |
1246 StructureNotifyMask |
1247 PointerMotionMask |
1248 Button1MotionMask | Button2MotionMask |
1249 Button3MotionMask
1250 );
1251
1252 XSynchronize(display, 0);
1253
1254#else
1255 // WNT
1256#endif
1257
1258}
1259
1260
1261//==============================================================================
1262//function : VFit
1263
1264//purpose : Fitall, no DRAW arguments
1265//Draw arg : No args
1266//==============================================================================
1267
1268static int VFit(Draw_Interpretor& , Standard_Integer , const char** )
1269{
1270 const Handle(V3d_View) aView = ViewerTest::CurrentView();
1271 Handle(NIS_View) V = Handle(NIS_View)::DownCast(aView);
1272 if (V.IsNull() == Standard_False) {
1273 V->FitAll3d();
1274 } else if (aView.IsNull() == Standard_False) {
1275 aView->FitAll();
1276 }
1277 return 0;
1278}
1279
1280//==============================================================================
1281//function : VZFit
1282//purpose : ZFitall, no DRAW arguments
1283//Draw arg : No args
1284//==============================================================================
1285
1286static int VZFit(Draw_Interpretor& , Standard_Integer , const char** )
1287{
1288 Handle(V3d_View) V = ViewerTest::CurrentView();
1289 if ( !V.IsNull() ) V->ZFitAll(); return 0; }
1290
1291
1292static int VRepaint(Draw_Interpretor& , Standard_Integer , const char** )
1293{
1294 Handle(V3d_View) V = ViewerTest::CurrentView();
1295 if ( !V.IsNull() ) V->Redraw(); return 0;
1296}
1297
1298
1299//==============================================================================
1300//function : VClear
1301//purpose : Remove all the object from the viewer
1302//Draw arg : No args
1303//==============================================================================
1304
1305static int VClear(Draw_Interpretor& , Standard_Integer , const char** )
1306{
1307 Handle(V3d_View) V = ViewerTest::CurrentView();
1308 if(!V.IsNull())
1309 ViewerTest::Clear();
1310 return 0;
1311}
1312
1313//==============================================================================
1314//function : VPick
1315//purpose :
1316//==============================================================================
1317
1318static int VPick(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1319{ if (ViewerTest::CurrentView().IsNull() ) return 1;
1320
1321if ( argc < 4 ) {
1322 di << argv[0] << "Invalid number of arguments" << "\n";
1323 return 1;
1324}
1325
1326while (ViewerMainLoop( argc, argv)) {
1327}
1328
1329return 0;
1330}
1331
1332
1333
1334//==============================================================================
1335//function : InitViewerTest
1336//purpose : initialisation de toutes les variables static de ViewerTest (dp)
1337//==============================================================================
1338
1339void ViewerTest_InitViewerTest (const Handle(AIS_InteractiveContext)& context)
1340{
1341 Handle(V3d_Viewer) viewer = context->CurrentViewer();
1342 ViewerTest::SetAISContext(context);
1343 viewer->InitActiveViews();
1344 Handle(V3d_View) view = viewer->ActiveView();
1345 if (viewer->MoreActiveViews()) ViewerTest::CurrentView(view);
1346 ViewerTest::ResetEventManager();
1347 Handle(Aspect_GraphicDevice) device = viewer->Device();
1348 Handle(Aspect_Window) window = view->Window();
1349#ifndef WNT
1350 // X11
1351 VT_GetWindow() = Handle(Xw_Window)::DownCast(window);
1352 GetG3dDevice() = Handle(Graphic3d_GraphicDevice)::DownCast(device);
1353 OSWindowSetup();
1354 static int first = 1;
1355 if ( first ) {
1356#if TCL_MAJOR_VERSION < 8
1357 Tk_CreateFileHandler((void*)ConnectionNumber(display),
1358 TK_READABLE, VProcessEvents, (ClientData) 0);
1359#else
1360 Tk_CreateFileHandler(ConnectionNumber(display),
1361 TK_READABLE, VProcessEvents, (ClientData) 0);
1362#endif
1363 first = 0;
1364 }
1365#endif
1366}
1367
1368
1369//==============================================================================
1370//function : VSetBg
1371//purpose : Load image as background
1372//==============================================================================
1373
1374static int VSetBg(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1375{
1376 if (argc < 2 || argc > 3)
1377 {
1378 di << "Usage : " << argv[0] << " imagefile [filltype] : Load image as background" << "\n";
1379 return 1;
1380 }
1381
1382 Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext();
1383 if(AISContext.IsNull())
1384 {
1385 di << "use 'vinit' command before " << argv[0] << "\n";
1386 return 1;
1387 }
1388
1389 Aspect_FillMethod aFillType = Aspect_FM_CENTERED;
1390 if (argc == 3)
1391 {
1392 const char* szType = argv[2];
1393 if (strcmp(szType, "NONE" ) == 0) aFillType = Aspect_FM_NONE;
1394 else if (strcmp(szType, "CENTERED") == 0) aFillType = Aspect_FM_CENTERED;
1395 else if (strcmp(szType, "TILED" ) == 0) aFillType = Aspect_FM_TILED;
1396 else if (strcmp(szType, "STRETCH" ) == 0) aFillType = Aspect_FM_STRETCH;
1397 else
1398 {
1399 di << "Wrong fill type : " << szType << "\n";
1400 di << "Must be one of CENTERED, TILED, STRETCH, NONE" << "\n";
1401 return 1;
1402 }
1403 }
1404
1405 Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1406 V3dView->SetBackgroundImage(argv[1], aFillType, Standard_True);
1407
1408 return 0;
1409}
1410
1411//==============================================================================
1412//function : VSetGradientBg
1413//purpose : Mount gradient background
1414//==============================================================================
1415static int VSetGradientBg(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1416{
1417 if (argc != 8 )
1418 {
1419 di << "Usage : " << argv[0] << " R1 G1 B1 R2 G2 B2 Type : Mount gradient background" << "\n";
1420 di << "R1,G1,B1,R2,G2,B2 = [0..255]" << "\n";
1421 di << "Type must be one of 0 = NONE, 1 = HOR, 2 = VER, 3 = DIAG1, 4 = DIAG2" << "\n";
1422 di << " 5 = CORNER1, 6 = CORNER2, 7 = CORNER3, 8 = CORNER4" << "\n";
1423 return 1;
1424 }
1425
1426 Handle(AIS_InteractiveContext) AISContext = ViewerTest::GetAISContext();
1427 if(AISContext.IsNull())
1428 {
1429 di << "use 'vinit' command before " << argv[0] << "\n";
1430 return 1;
1431 }
1432 if (argc == 8)
1433 {
1434
1435 Standard_Real R1 = atof(argv[1])/255.;
1436 Standard_Real G1 = atof(argv[2])/255.;
1437 Standard_Real B1 = atof(argv[3])/255.;
1438 Quantity_Color aColor1(R1,G1,B1,Quantity_TOC_RGB);
1439
1440 Standard_Real R2 = atof(argv[4])/255.;
1441 Standard_Real G2 = atof(argv[5])/255.;
1442 Standard_Real B2 = atof(argv[6])/255.;
1443
1444 Quantity_Color aColor2(R2,G2,B2,Quantity_TOC_RGB);
1445 int aType = atoi(argv[7]);
1446 if( aType < 0 || aType > 8 )
1447 {
1448 di << "Wrong fill type " << "\n";
1449 di << "Must be one of 0 = NONE, 1 = HOR, 2 = VER, 3 = DIAG1, 4 = DIAG2" << "\n";
1450 di << " 5 = CORNER1, 6 = CORNER2, 7 = CORNER3, 8 = CORNER4" << "\n";
1451 return 1;
1452 }
1453
1454 Aspect_GradientFillMethod aMethod = Aspect_GradientFillMethod(aType);
1455
1456 Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1457 V3dView->SetBgGradientColors( aColor1, aColor2, aMethod, 1);
1458 }
1459
1460 return 0;
1461}
1462
1463//==============================================================================
1464//function : VScale
1465//purpose : View Scaling
1466//==============================================================================
1467
1468static int VScale(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1469{
1470 Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1471 if ( V3dView.IsNull() ) return 1;
1472
1473 if ( argc != 4 ) {
1474 di << argv[0] << "Invalid number of arguments" << "\n";
1475 return 1;
1476 }
1477 V3dView->SetAxialScale( atof(argv[1]), atof(argv[2]), atof(argv[3]) );
1478 return 0;
1479}
1480//==============================================================================
1481//function : VTestZBuffTrihedron
1482//purpose : Displays a V3d_ZBUFFER'ed trihedron at the bottom left corner of the view
1483//==============================================================================
1484
1485static int VTestZBuffTrihedron(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1486{
1487 Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1488 if ( V3dView.IsNull() ) return 1;
1489
1490 // Set up default trihedron parameters
1491 V3dView->ZBufferTriedronSetup();
1492 V3dView->TriedronDisplay( Aspect_TOTP_LEFT_LOWER, Quantity_NOC_WHITE, 0.1, V3d_ZBUFFER );
1493 V3dView->ZFitAll();
1494 return 0;
1495}
1496
1497//==============================================================================
1498//function : VRotate
1499//purpose : Camera Rotating
1500//==============================================================================
1501
1502static int VRotate( Draw_Interpretor& di, Standard_Integer argc, const char** argv ) {
1503 Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1504 if ( V3dView.IsNull() ) {
1505 return 1;
1506 }
1507
1508 if ( argc == 4 ) {
1509 V3dView->Rotate( atof(argv[1]), atof(argv[2]), atof(argv[3]) );
1510 return 0;
1511 } else if ( argc == 7 ) {
1512 V3dView->Rotate( atof(argv[1]), atof(argv[2]), atof(argv[3]), atof(argv[4]), atof(argv[5]), atof(argv[6]) );
1513 return 0;
1514 } else {
1515 di << argv[0] << " Invalid number of arguments" << "\n";
1516 return 1;
1517 }
1518}
1519
1520//==============================================================================
1521//function : VZoom
1522//purpose : View zoom in / out (relative to current zoom)
1523//==============================================================================
1524
1525static int VZoom( Draw_Interpretor& di, Standard_Integer argc, const char** argv ) {
1526 Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1527 if ( V3dView.IsNull() ) {
1528 return 1;
1529 }
1530
1531 if ( argc == 2 ) {
1532 Standard_Real coef = atof(argv[1]);
1533 if ( coef <= 0.0 ) {
1534 di << argv[1] << "Invalid value" << "\n";
1535 return 1;
1536 }
1537 V3dView->SetZoom( atof(argv[1]) );
1538 return 0;
1539 } else {
1540 di << argv[0] << " Invalid number of arguments" << "\n";
1541 return 1;
1542 }
1543}
1544
1545//==============================================================================
1546//function : VPan
1547//purpose : View panning (in pixels)
1548//==============================================================================
1549
1550static int VPan( Draw_Interpretor& di, Standard_Integer argc, const char** argv ) {
1551 Handle(V3d_View) V3dView = ViewerTest::CurrentView();
1552 if ( V3dView.IsNull() ) return 1;
1553
1554 if ( argc == 3 ) {
1555 V3dView->Pan( atoi(argv[1]), atoi(argv[2]) );
1556 return 0;
1557 } else {
1558 di << argv[0] << " Invalid number of arguments" << "\n";
1559 return 1;
1560 }
1561}
1562
1563
1564//==============================================================================
1565//function : VExport
5cedc27f 1566//purpose : Export the view to a vector graphic format (PS, EMF, PDF)
7fd59977 1567//==============================================================================
1568
1569static int VExport(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1570{
1571 Handle(V3d_View) V3dView = ViewerTest::CurrentView();
5cedc27f
K
1572 if (V3dView.IsNull())
1573 return 1;
7fd59977 1574
5cedc27f
K
1575 if (argc == 1)
1576 {
1577 std::cout << "Usage: " << argv[0] << " Filename [Format]\n";
7fd59977 1578 return 1;
1579 }
1580
5cedc27f
K
1581 Graphic3d_ExportFormat anExpFormat = Graphic3d_EF_PDF;
1582 TCollection_AsciiString aFormatStr;
1583
1584 TCollection_AsciiString aFileName (argv[1]);
1585 Standard_Integer aLen = aFileName.Length();
1586
1587 if (argc > 2)
1588 {
1589 aFormatStr = TCollection_AsciiString (argv[2]);
1590 }
1591 else if (aLen >= 4)
1592 {
1593 if (aFileName.Value (aLen - 2) == '.')
1594 {
1595 aFormatStr = aFileName.SubString (aLen - 1, aLen);
1596 }
1597 else if (aFileName.Value (aLen - 3) == '.')
1598 {
1599 aFormatStr = aFileName.SubString (aLen - 2, aLen);
1600 }
1601 else
1602 {
1603 std::cout << "Export format couln't be detected from filename '" << argv[1] << "'\n";
1604 return 1;
1605 }
1606 }
1607 else
1608 {
1609 std::cout << "Export format couln't be detected from filename '" << argv[1] << "'\n";
1610 return 1;
1611 }
1612
1613 aFormatStr.UpperCase();
1614 if (aFormatStr == "PS")
1615 anExpFormat = Graphic3d_EF_PostScript;
1616 else if (aFormatStr == "EPS")
1617 anExpFormat = Graphic3d_EF_EnhPostScript;
1618 else if (aFormatStr == "TEX")
1619 anExpFormat = Graphic3d_EF_TEX;
1620 else if (aFormatStr == "PDF")
1621 anExpFormat = Graphic3d_EF_PDF;
1622 else if (aFormatStr == "SVG")
1623 anExpFormat = Graphic3d_EF_SVG;
1624 else if (aFormatStr == "PGF")
1625 anExpFormat = Graphic3d_EF_PGF;
1626 else if (aFormatStr == "EMF")
1627 anExpFormat = Graphic3d_EF_EMF;
1628 else
1629 {
1630 std::cout << "Invalid export format '" << aFormatStr << "'\n";
1631 return 1;
1632 }
1633
1634 if (!V3dView->View()->Export (argv[1], anExpFormat))
1635 {
1636 std::cout << "Export failed!\n";
1637 return 1;
1638 }
7fd59977 1639 return 0;
1640}
1641
1642//==============================================================================
1643//function : VColorScale
1644//purpose : representation color scale
1645//==============================================================================
1646#include <V3d_ColorScale.hxx>
1647
1648static int VColorScale (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
1649{
1650 if ( argc != 1 && argc != 4 && argc != 5 && argc != 6 && argc != 8 )
1651 {
1652 di << "Usage : " << argv[0] << " [RangeMin = 0 RangeMax = 100 Intervals = 10 HeightFont = 16 Position = Right X = 0 Y = 0] " << "\n";
1653 return 1;
1654 }
1655
1656 Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
1657 if(aContext.IsNull()) {
1658 di << argv[0] << " ERROR : use 'vinit' command before " << "\n";
1659 return -1;
1660 }
1661
1662 Standard_Real minRange = 0. , maxRange = 100. ;
1663
1664 Standard_Integer numIntervals = 10 ;
1665 Standard_Real textHeight = 16. ;
1666 Aspect_TypeOfColorScalePosition position = Aspect_TOCSP_RIGHT;
1667 Standard_Real X = 0., Y = 0. ;
1668
1669 if ( argc < 9 )
1670 {
1671 if( argc > 3 )
1672 {
1673 minRange = atof( argv[1] );
1674 maxRange = atof( argv[2] );
1675 numIntervals = atoi( argv[3] );
1676 }
1677 if ( argc > 4 )
1678 textHeight = atof( argv[4] );
1679 if ( argc > 5 )
1680 position = (Aspect_TypeOfColorScalePosition)atoi( argv[5] );
1681 if ( argc > 7 )
1682 {
1683 X = atof( argv[6] );
1684 Y = atof( argv[7] );
1685 }
1686 }
1687 Handle(V3d_View) curView = ViewerTest::CurrentView( );
1688 if ( curView.IsNull( ) )
1689 return 1;
1690 Handle(Aspect_ColorScale) aCSV = curView->ColorScale( );
1691 Handle(V3d_ColorScale) aCS = ( Handle( V3d_ColorScale )::DownCast( aCSV ) );
1692 if( ! aCS.IsNull( ) )
1693 {
1694 aCS->SetPosition( X , Y );
1695 aCS->SetHeight( 0.95) ;
1696 aCS->SetTextHeight( textHeight );
1697 aCS->SetRange( minRange , maxRange );
1698 aCS->SetNumberOfIntervals( numIntervals );
1699 aCS->SetLabelPosition( position );
1700 if( !curView->ColorScaleIsDisplayed() )
1701 curView->ColorScaleDisplay( );
1702 }
1703 return 0;
1704}
1705
1706//==============================================================================
1707//function : VGraduatedTrihedron
1708//purpose : Displays a graduated trihedron
1709//==============================================================================
13a22457
S
1710
1711static void AddMultibyteString (TCollection_ExtendedString &name, const char *arg)
1712{
1713 const char *str = arg;
1714 while (*str)
1715 {
1716 unsigned short c1 = *str++;
1717 unsigned short c2 = *str++;
1718 if (!c1 || !c2) break;
1719 name += (Standard_ExtCharacter)((c1 << 8) | c2);
1720 }
1721}
1722
7fd59977 1723static int VGraduatedTrihedron(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1724{
13a22457
S
1725 // Check arguments
1726 if (argc != 2 && argc < 5)
1727 {
1728 di<<"Error: "<<argv[0]<<" - invalid number of arguments\n";
1729 di<<"Usage: type help "<<argv[0]<<"\n";
1730 return 1; //TCL_ERROR
1731 }
7fd59977 1732
13a22457
S
1733 Handle(V3d_View) aV3dView = ViewerTest::CurrentView();
1734
1735 // Create 3D view if it doesn't exist
1736 if ( aV3dView.IsNull() )
7fd59977 1737 {
13a22457
S
1738 ViewerTest::ViewerInit();
1739 aV3dView = ViewerTest::CurrentView();
1740 if( aV3dView.IsNull() )
1741 {
1742 di << "Error: Cannot create a 3D view\n";
1743 return 1; //TCL_ERROR
1744 }
7fd59977 1745 }
1746
13a22457
S
1747 // Erase (==0) or display (!=0)
1748 const int display = atoi(argv[1]);
7fd59977 1749
13a22457 1750 if (display)
7fd59977 1751 {
13a22457
S
1752 // Text font
1753 TCollection_AsciiString font;
1754 if (argc < 6)
1755 font.AssignCat("Courier");
1756 else
1757 font.AssignCat(argv[5]);
1758
1759 // Text is multibyte
1760 const Standard_Boolean isMultibyte = (argc < 7)? Standard_False : (atoi(argv[6]) != 0);
1761
1762 // Set axis names
1763 TCollection_ExtendedString xname, yname, zname;
1764 if (argc >= 5)
1765 {
1766 if (isMultibyte)
7fd59977 1767 {
13a22457
S
1768 AddMultibyteString(xname, argv[2]);
1769 AddMultibyteString(yname, argv[3]);
1770 AddMultibyteString(zname, argv[4]);
7fd59977 1771 }
13a22457
S
1772 else
1773 {
1774 xname += argv[2];
1775 yname += argv[3];
1776 zname += argv[4];
1777 }
1778 }
1779 else
1780 {
1781 xname += "X (mm)";
1782 yname += "Y (mm)";
1783 zname += "Z (mm)";
1784 }
7fd59977 1785
13a22457
S
1786 aV3dView->GraduatedTrihedronDisplay(xname, yname, zname,
1787 Standard_True/*xdrawname*/, Standard_True/*ydrawname*/, Standard_True/*zdrawname*/,
1788 Standard_True/*xdrawvalues*/, Standard_True/*ydrawvalues*/, Standard_True/*zdrawvalues*/,
1789 Standard_True/*drawgrid*/,
1790 Standard_True/*drawaxes*/,
1791 5/*nbx*/, 5/*nby*/, 5/*nbz*/,
1792 10/*xoffset*/, 10/*yoffset*/, 10/*zoffset*/,
1793 30/*xaxisoffset*/, 30/*yaxisoffset*/, 30/*zaxisoffset*/,
1794 Standard_True/*xdrawtickmarks*/, Standard_True/*ydrawtickmarks*/, Standard_True/*zdrawtickmarks*/,
1795 10/*xtickmarklength*/, 10/*ytickmarklength*/, 10/*ztickmarklength*/,
1796 Quantity_NOC_WHITE/*gridcolor*/,
1797 Quantity_NOC_RED/*xnamecolor*/,Quantity_NOC_GREEN/*ynamecolor*/,Quantity_NOC_BLUE1/*znamecolor*/,
1798 Quantity_NOC_RED/*xcolor*/,Quantity_NOC_GREEN/*ycolor*/,Quantity_NOC_BLUE1/*zcolor*/,font);
1799 }
7fd59977 1800 else
13a22457 1801 aV3dView->GraduatedTrihedronErase();
7fd59977 1802
1803 ViewerTest::GetAISContext()->UpdateCurrentViewer();
13a22457
S
1804 aV3dView->Redraw();
1805
7fd59977 1806 return 0;
1807}
1808
7edf74fd
A
1809//==============================================================================
1810//function : VPrintView
1811//purpose : Test printing algorithm, print the view to image file with given
1812// width and height. Printing implemented only for WNT.
1813//==============================================================================
1814static int VPrintView (Draw_Interpretor& di, Standard_Integer argc,
1815 const char** argv)
1816{
1817#ifndef WNT
1818 di << "Printing implemented only for wnt!\n";
1819 return 1;
1820#else
1821
1822 Handle(AIS_InteractiveContext) aContextAIS = NULL;
1823 Handle(V3d_View) aView = NULL;
1824 aContextAIS = ViewerTest::GetAISContext();
1825 if (!aContextAIS.IsNull())
1826 {
1827 const Handle(V3d_Viewer)& Vwr = aContextAIS->CurrentViewer();
1828 Vwr->InitActiveViews();
1829 if(Vwr->MoreActiveViews())
1830 aView = Vwr->ActiveView();
1831 }
1832
1833 // check for errors
1834 if (aView.IsNull())
1835 {
1836 di << "Call vinit before!\n";
1837 return 1;
1838 }
1839 else if (argc < 4)
1840 {
1841 di << "Use: " << argv[0];
1842 di << " width height filename [print algo=0]\n";
1843 di << "width, height of the intermediate buffer for operation\n";
1844 di << "algo : {0|1}\n";
1845 di << " 0 - stretch algorithm\n";
1846 di << " 1 - tile algorithm\n";
1847 di << "test printing algorithms into an intermediate buffer\n";
1848 di << "with saving output to an image file\n";
1849 return 1;
1850 }
1851
1852 // get the input params
1853 Standard_Integer aWidth = atoi (argv[1]);
1854 Standard_Integer aHeight = atoi (argv[2]);
1855 Standard_Integer aMode = 0;
1856 TCollection_AsciiString aFileName = TCollection_AsciiString (argv[3]);
1857 if (argc==5)
1858 aMode = atoi (argv[4]);
1859
1860 // check the input parameters
1861 if (aWidth <= 0 || aHeight <= 0)
1862 {
1863 di << "Width and height must be positive values!\n";
1864 return 1;
1865 }
1866 if (aMode != 0 && aMode != 1)
1867 aMode = 0;
1868
1869 Image_CRawBufferData aRawBuffer;
1870 HDC anDC = CreateCompatibleDC(0);
1871
1872 // define compatible bitmap
1873 BITMAPINFO aBitmapData;
1874 memset (&aBitmapData, 0, sizeof (BITMAPINFOHEADER));
1875 aBitmapData.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
1876 aBitmapData.bmiHeader.biWidth = aWidth ;
1877 aBitmapData.bmiHeader.biHeight = aHeight;
1878 aBitmapData.bmiHeader.biPlanes = 1;
1879 aBitmapData.bmiHeader.biBitCount = 24;
1880 aBitmapData.bmiHeader.biXPelsPerMeter = 0;
1881 aBitmapData.bmiHeader.biYPelsPerMeter = 0;
1882 aBitmapData.bmiHeader.biClrUsed = 0;
1883 aBitmapData.bmiHeader.biClrImportant = 0;
1884 aBitmapData.bmiHeader.biCompression = BI_RGB;
1885 aBitmapData.bmiHeader.biSizeImage = 0;
1886
1887 // Create Device Independent Bitmap
1888 HBITMAP aMemoryBitmap = CreateDIBSection (anDC, &aBitmapData, DIB_RGB_COLORS,
1889 &aRawBuffer.dataPtr, NULL, 0);
1890 HGDIOBJ anOldBitmap = SelectObject(anDC, aMemoryBitmap);
1891
1892 Standard_Boolean isSaved = Standard_False, isPrinted = Standard_False;
1893 if (aRawBuffer.dataPtr != 0)
1894 {
1895 if (aMode == 0)
1896 isPrinted = aView->Print(anDC,1,1,0,Aspect_PA_STRETCH);
1897 else
1898 isPrinted = aView->Print(anDC,1,1,0,Aspect_PA_TILE);
1899
1900 // succesfully printed into an intermediate buffer
1901 if (isPrinted)
1902 {
1903 Handle(Image_PixMap) anImageBitmap =
1904 new Image_PixMap ((Standard_PByte)aRawBuffer.dataPtr,
1905 aWidth, aHeight,
1906 aWidth*3 + aWidth%4, 24, 0);
1907 isSaved = anImageBitmap->Dump(aFileName.ToCString());
1908 }
1909 else
1910 {
1911 di << "Print operation failed due to printing errors or\n";
1912 di << "insufficient memory available\n";
1913 di << "Please, try to use smaller dimensions for this test\n";
1914 di << "command, as it allocates intermediate buffer for storing\n";
1915 di << "the result\n";
1916 }
1917 }
1918 else
1919 {
1920 di << "Can't allocate memory for intermediate buffer\n";
1921 di << "Please use smaller dimensions\n";
1922 }
1923
1924 if (aMemoryBitmap)
1925 {
1926 SelectObject (anDC, anOldBitmap);
1927 DeleteObject (aMemoryBitmap);
1928 DeleteDC(anDC);
1929 }
1930
1931 if (!isSaved)
1932 {
1933 di << "Save to file operation failed. This operation may fail\n";
1934 di << "if you don't have enough available memory, then you can\n";
1935 di << "use smaller dimensions for the output file\n";
1936 return 1;
1937 }
1938
1939 return 0;
1940
1941#endif
1942}
1943
7fd59977 1944//=======================================================================
1945//function : ViewerCommands
1946//purpose :
1947//=======================================================================
1948
1949void ViewerTest::ViewerCommands(Draw_Interpretor& theCommands)
1950{
1951
1952 const char *group = "ZeViewer";
1953 theCommands.Add("vinit" ,
1954 "vinit : vinit [leftPx topPx widthPx heightPx] : Create the Viewer window",
1955 __FILE__,VInit,group);
1956 theCommands.Add("vhelp" ,
1957 "vhelp : display help on the viewer commands",
1958 __FILE__,VHelp,group);
1959 theCommands.Add("vtop" ,
1960 "vtop or <T> : Top view" ,
1961 __FILE__,VTop,group);
1962 theCommands.Add("vaxo" ,
1963 " vaxo or <A> : Axonometric view ",
1964 __FILE__,VAxo,group);
1965 theCommands.Add("vpick" ,
1966 "vpick : vpick X Y Z [shape subshape] ( all variables as string )",
1967 VPick,group);
1968 theCommands.Add("vfit" ,
1969 "vfit or <F> : vfit",
1970 __FILE__,VFit,group);
1971 theCommands.Add("vzfit" ,
1972 "vzfit",
1973 __FILE__,VZFit,group);
1974 theCommands.Add("vrepaint",
1975 "vrepaint : vrepaint, force redraw",
1976 __FILE__,VRepaint,group);
1977 theCommands.Add("vclear",
1978 "vclear : vclear",
1979 __FILE__,VClear,group);
1980 theCommands.Add("vsetbg",
1981 "vsetbg : vsetbg imagefile [filltype] : Load image as background",
1982 __FILE__,VSetBg,group);
1983 theCommands.Add("vsetgradientbg",
1984 "vsetgradientbg : vsetgradientbg r1 g1 b1 r2 g2 b2 filltype : Mount gradient background",
1985 __FILE__,VSetGradientBg,group);
1986 theCommands.Add("vscale",
1987 "vscale : vscale X Y Z",
1988 __FILE__,VScale,group);
1989 theCommands.Add("vzbufftrihedron",
1990 "vzbufftrihedron : Displays a V3d_ZBUFFER'ed trihedron at the bottom left corner of the view",
1991 __FILE__,VTestZBuffTrihedron,group);
1992 theCommands.Add("vrotate",
1993 "vrotate : vrotate AX AY AZ [X Y Z]",
1994 __FILE__,VRotate,group);
1995 theCommands.Add("vzoom",
1996 "vzoom : vzoom coef",
1997 __FILE__,VZoom,group);
1998 theCommands.Add("vpan",
1999 "vpan : vpan dx dy",
2000 __FILE__,VPan,group);
2001 theCommands.Add("vexport",
5cedc27f
K
2002 "vexport : vexport full_file_path {PS | EPS | TEX | PDF | SVG | PGF | EMF }"
2003 " : exports the view to a vector file of a given format"
2004 " : notice that EMF format requires patched gl2ps",
7fd59977 2005 __FILE__,VExport,group);
2006 theCommands.Add("vcolorscale",
2007 "vcolorscale : vcolorscale [RangeMin = 0 RangeMax = 100 Intervals = 10 HeightFont = 16 Position = 2 X = 0 Y = 0]: draw color scale",
2008 __FILE__,VColorScale,group);
2009 theCommands.Add("vgraduatedtrihedron",
13a22457 2010 "vgraduatedtrihedron : 1/0 (display/erase) [Xname Yname Zname [Font [isMultibyte]]]",
7fd59977 2011 __FILE__,VGraduatedTrihedron,group);
7edf74fd
A
2012 theCommands.Add("vprintview" ,
2013 "vprintview : width height filename [algo=0] : Test print algorithm: algo = 0 - stretch, algo = 1 - tile",
2014 __FILE__,VPrintView,group);
2015
7fd59977 2016}