8a97930a806b441e43cb1d3999bce13ec15e16a9
[occt.git] / src / QADraw / QADraw.cxx
1 // Created on: 2002-02-04
2 // Created by: QA Admin
3 // Copyright (c) 2002-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21
22 #if defined(WNT)
23 #include <windows.h>
24 #endif
25 #include <QADraw.hxx>
26 #include <QADraw_DataMapOfAsciiStringOfAddress.hxx>
27 #include <Draw_Interpretor.hxx>
28 #include <Image_AlienPixMap.hxx>
29 #include <V3d_View.hxx>
30 #include <ViewerTest.hxx>
31 #include <ViewerTest_EventManager.hxx>
32 #include <TColStd_StackOfInteger.hxx>
33 #include <TColStd_ListOfInteger.hxx>
34 #include <TColStd_ListIteratorOfListOfInteger.hxx>
35 #include <TColStd_HSequenceOfReal.hxx>
36 #include <AIS_InteractiveContext.hxx>
37 #include <Graphic3d_GraphicDevice.hxx>
38 #include <Draw.hxx>
39 #include <Draw_Window.hxx>
40 #include <Draw_Viewer.hxx>
41 #include <Aspect_WindowDriver.hxx>
42 #include <stdio.h>
43
44 #if ! defined(WNT)
45 #include <Xw_Window.hxx>
46 //#include <Xm/Xm.h>
47 #include <X11/Xlib.h>
48 #include <X11/Xutil.h>
49 #include <Xw_GraphicDevice.hxx>
50 #include <Xw_Cextern.hxx>
51 #include <unistd.h>
52 #else
53 #include <WNT.h>
54 #include <WNT_GraphicDevice.hxx>
55 #include <WNT_Window.hxx>
56 #include <io.h>
57 #endif
58
59 #include <tcl.h>
60
61 #if ! defined(STDOUT_FILENO)
62 #define STDOUT_FILENO fileno(stdout)
63 #endif
64
65 // mkv 15.07.03
66 #if ! defined(STDERR_FILENO)
67 #define STDERR_FILENO fileno(stderr)
68 #endif
69
70 Draw_Interpretor *thePCommands = NULL;
71
72 #if ! defined(WNT)
73 //extern Draw_Interpretor theCommands;
74
75 extern void (*Draw_BeforeCommand)();
76 extern void (*Draw_AfterCommand)(Standard_Integer);
77 #else
78 //Standard_EXPORT Draw_Interpretor theCommands;
79
80 Standard_EXPORT void (*Draw_BeforeCommand)();
81 Standard_EXPORT void (*Draw_AfterCommand)(Standard_Integer);
82 #endif
83
84
85 #include <Draw_PluginMacro.hxx>
86
87 // avoid warnings on 'extern "C"' functions returning C++ classes
88 #ifdef WNT
89 #pragma warning(4:4190)
90 #endif
91
92
93 int st_err = 0;
94
95 void (*Draw_BeforeCommand_old)();
96 void (*Draw_AfterCommand_old)(Standard_Integer);
97
98 static Standard_Boolean should_be_printed = Standard_True;
99
100 static Standard_Boolean shouldDUP() {
101   // MKV 30.03.05
102 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4))) && !defined(USE_NON_CONST)
103   const Standard_Character * adup = Tcl_GetVar(thePCommands->Interp(),
104                                          "QA_DUP",TCL_GLOBAL_ONLY);
105 #else
106   Standard_Character * adup = Tcl_GetVar(thePCommands->Interp(),
107                                          "QA_DUP",TCL_GLOBAL_ONLY);
108 #endif
109   Standard_Integer aDUP=1;
110   if((adup != NULL) && (Draw::Atof(adup) == 0)) {
111     aDUP = 0;
112   }
113
114   return aDUP;
115 }
116
117 static void before() {
118   should_be_printed = Standard_True;
119   if(Draw_BeforeCommand_old) (*Draw_BeforeCommand_old) ();
120 }
121
122 static void  after(Standard_Integer a)
123 {
124   if(Draw_AfterCommand_old) (*Draw_AfterCommand_old) (a);
125   if(!should_be_printed) {
126 //    Tcl_ResetResult(theCommands.Interp());
127   }
128 }
129
130 static QADraw_DataMapOfAsciiStringOfAddress stFuncMap;
131
132   // MKV 30.03.05
133 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4))) && !defined(USE_NON_CONST)
134 static Standard_Integer (*CommandCmd_Old)
135 (ClientData clientData, Tcl_Interp *interp,
136  Standard_Integer argc, const char* argv[]) = NULL;
137 #else
138 static Standard_Integer (*CommandCmd_Old)
139 (ClientData clientData, Tcl_Interp *interp,
140  Standard_Integer argc, char* argv[]) = NULL;
141 #endif
142
143   // MKV 30.03.05
144 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4))) && !defined(USE_NON_CONST)
145 static Standard_Integer CommandCmd
146 (ClientData clientData, Tcl_Interp *interp,
147  Standard_Integer argc, const char* argv[])
148 #else
149 static Standard_Integer CommandCmd
150 (ClientData clientData, Tcl_Interp *interp,
151  Standard_Integer argc, char* argv[])
152 #endif
153 {
154   Standard_Integer old_out = 0;
155   Standard_Integer old_err = 0;
156   Standard_Integer fd_out = 0;
157   Standard_Integer fd_err = 0;
158
159   FILE * aFile_out = NULL;
160   FILE * aFile_err = NULL;
161
162   char *nameo = NULL;
163   char *namee = NULL;
164 #ifdef WNT
165   if(strlen(getenv("TEMP")) == 0) {
166     cerr << "The TEMP variable is not set." << endl;
167     aFile_out = tmpfile();
168     aFile_err = tmpfile();
169   } else {
170     nameo = _tempnam(getenv("TEMP"),"tmpo");
171     namee = _tempnam(getenv("TEMP"),"tmpe");
172     aFile_out=fopen(nameo, "w+");
173     aFile_err=fopen(namee, "w+");
174   }
175 #else
176     aFile_out = tmpfile();
177     aFile_err = tmpfile();
178 #endif
179   fd_out = fileno(aFile_out);
180   fd_err = fileno(aFile_err);
181   if(fd_out !=-1 && fd_err !=-1) {
182     old_err = dup(STDERR_FILENO);
183     old_out = dup(STDOUT_FILENO);
184     dup2(fd_err,STDERR_FILENO);
185     dup2(fd_out,STDOUT_FILENO);
186   } else
187     cout << "Faulty : Can not create temporary file."   << endl;
188
189   Standard_Integer clen = sizeof(Standard_Character);
190
191   Standard_Character * QA = getenv("QA_print_command");
192   if((QA != NULL) && (!strcmp(QA,"1"))) {
193     for(Standard_Integer i=0;i<argc;i++) {
194       write(st_err,argv[i],clen*strlen(argv[i]));
195       write(st_err," ",clen);
196     }
197     write(st_err,"\n",clen);
198   }
199
200   // MKV 30.03.05
201 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4))) && !defined(USE_NON_CONST)
202   TCollection_AsciiString aName((char *) argv[0]);
203 #else
204   TCollection_AsciiString aName(argv[0]);
205 #endif
206
207   Standard_Integer res = 0;
208
209   if(stFuncMap.IsBound(aName)) {
210     // MKV 30.03.05
211 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4))) && !defined(USE_NON_CONST)
212     CommandCmd_Old  = (int(*)(void*, Tcl_Interp*, int, const char**))  stFuncMap((char *) argv[0]);
213 #else
214     CommandCmd_Old  = (int(*)(void*, Tcl_Interp*, int, char**))  stFuncMap(argv[0]);
215 #endif
216     res = (*CommandCmd_Old) (clientData,interp,argc,argv);
217   }
218
219   fflush(stderr);
220   fflush(stdout);
221   close(STDERR_FILENO);
222   close(STDOUT_FILENO);
223   dup2(old_err,STDERR_FILENO);
224   dup2(old_out,STDOUT_FILENO);
225   close(old_err);
226   close(old_out);
227
228   Standard_Character buf[256];
229   Standard_Integer len = 0;
230
231   rewind(aFile_err);
232   while((len = read(fd_err,buf,clen*255)/clen) > 0) {
233     buf[len]='\0';
234     if(shouldDUP()) {
235       (*thePCommands) << buf;
236     } else {
237       write(st_err,buf,clen*len);
238     }
239   }
240   close(fd_err);
241
242   rewind(aFile_out);
243   buf[0] = '\0';
244   len = 0;
245   while((len = read(fd_out,buf,clen*255)/clen) > 0) {
246     buf[len]='\0';
247     if(shouldDUP()) {
248       (*thePCommands) << buf;
249     } else {
250       write(st_err,buf,clen*len);
251     }
252   }
253   close(fd_out);
254
255   if(shouldDUP()) {
256     Standard_Character *Result = (Standard_Character *)thePCommands->Result();
257     if(Result[0] != '\0') {
258       Standard_Integer pos = 0;
259       Standard_Integer rlen = strlen(Result);
260       while(pos < rlen) {
261         Standard_Integer nb = 256;
262         if((rlen - pos) <= 256) {
263           nb = rlen - pos;
264         }
265         write(st_err,Result+pos,nb*clen);
266         pos += nb;
267       }
268       write(st_err,"\n",clen);
269       should_be_printed = Standard_False  ;
270     }
271   } //else {
272   if(nameo)
273     free(nameo);
274   if(namee)
275     free(namee);
276   return res;
277 }
278
279 static Standard_Integer QARebuild (Draw_Interpretor& di, Standard_Integer /*n*/, const char ** a)
280 {
281
282   Tcl_CmdInfo *infoPtr = new Tcl_CmdInfo;
283
284   if(!strcmp(a[0],a[1]))
285     return 0;
286
287   char* var_a = (char*)a[1];
288   Standard_Integer res = Tcl_GetCommandInfo(di.Interp(),var_a,infoPtr);
289   if(res && (infoPtr->proc != &CommandCmd)) {
290     TCollection_AsciiString aName(var_a);
291     if (!stFuncMap.IsBound(aName)) {
292       stFuncMap.Bind(aName, (Standard_Address) infoPtr->proc);
293
294       infoPtr->proc = &CommandCmd;
295 #if TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION == 3
296       infoPtr->isNativeObjectProc = 0;
297       infoPtr->objProc = NULL;
298 #endif
299       Tcl_SetCommandInfo(di.Interp(),var_a,infoPtr);
300     }
301   }
302
303   return 0;
304 }
305
306 Handle(TColStd_HSequenceOfReal) GetColorOfPixel (const Image_PixMap&    theImage,
307                                                  const Standard_Integer theCoordinateX,
308                                                  const Standard_Integer theCoordinateY,
309                                                  const Standard_Integer theRadius)
310 {
311   Handle(TColStd_HSequenceOfReal) aSeq = new TColStd_HSequenceOfReal();
312   if (theImage.IsEmpty()) {
313     std::cerr << "The image is null.\n";
314     return aSeq;
315   }
316   Standard_Integer aWidth   = (Standard_Integer )theImage.SizeX();
317   Standard_Integer anHeight = (Standard_Integer )theImage.SizeY();
318
319   Quantity_Color aColorTmp;
320   for (Standard_Integer anXIter = theCoordinateX - theRadius;
321        anXIter <= theCoordinateX + theRadius; ++anXIter)
322   {
323     if (anXIter < 0 || anXIter >= aWidth)
324     {
325       continue;
326     }
327     for (Standard_Integer anYIter = theCoordinateY - theRadius;
328          anYIter <= theCoordinateY + theRadius; ++anYIter)
329     {
330       if (anYIter < 0 || anYIter >= anHeight)
331       {
332         continue;
333       }
334       // Image_PixMap stores image upside-down in memory!
335       aColorTmp = theImage.PixelColor (anXIter, anYIter);
336       aSeq->Append (aColorTmp.Red());
337       aSeq->Append (aColorTmp.Green());
338       aSeq->Append (aColorTmp.Blue());
339     }
340   }
341   return aSeq;
342 }
343
344 static Standard_Integer QAAISGetPixelColor (Draw_Interpretor& theDi,
345                                             Standard_Integer  theArgsNb,
346                                             const char**      theArgs)
347 {
348   if (theArgsNb != 3 && theArgsNb != 6)
349   {
350     theDi << "Usage : " << theArgs[0] << " coordinate_X coordinate_Y [color_R color_G color_B]" << "\n";
351     return 1; // TCL_ERROR
352   }
353
354   Handle(V3d_View) aView3d = ViewerTest::CurrentView();
355   if (aView3d.IsNull())
356   {
357     theDi << "You must initialize AISViewer before this command.\n";
358     return 1; // TCL_ERROR
359   }
360
361   const Handle(Aspect_Window) anAISWindow = aView3d->Window();
362   Standard_Integer aWindowSizeX = 0;
363   Standard_Integer aWindowSizeY = 0;
364   anAISWindow->Size (aWindowSizeX, aWindowSizeY);
365
366   Standard_Integer anArgIter = 1;
367   const Standard_Integer aPickCoordX = Draw::Atoi (theArgs[anArgIter++]);
368   const Standard_Integer aPickCoordY = Draw::Atoi (theArgs[anArgIter++]);
369   const Standard_Integer aRadius = (theArgsNb == 3) ? 0 : 1;
370
371   Image_ColorRGBF aColorInput = {{ 0.0f, 0.0f, 0.0f }};
372   if (theArgsNb == 6)
373   {
374     aColorInput.r() = (Standard_ShortReal )Draw::Atof (theArgs[anArgIter++]);
375     aColorInput.g() = (Standard_ShortReal )Draw::Atof (theArgs[anArgIter++]);
376     aColorInput.b() = (Standard_ShortReal )Draw::Atof (theArgs[anArgIter++]);
377   }
378
379   Image_PixMap anImage;
380   aView3d->ToPixMap (anImage, aWindowSizeX, aWindowSizeY);
381   const Handle(TColStd_HSequenceOfReal) aSeq = GetColorOfPixel (anImage, aPickCoordX, aPickCoordY, aRadius);
382   cout << "Length = " << aSeq->Length() << endl;
383
384   Image_ColorRGBF aColorPicked = {{ 0.0f, 0.0f, 0.0f }};
385   Standard_Boolean isNotEqual = Standard_True;
386   for (Standard_Integer i = 1; i <= aSeq->Length(); i += 3)
387   {
388     aColorPicked.r() = (Standard_ShortReal )aSeq->Value (i + 0);
389     aColorPicked.g() = (Standard_ShortReal )aSeq->Value (i + 1);
390     aColorPicked.b() = (Standard_ShortReal )aSeq->Value (i + 2);
391
392     if (theArgsNb == 3 ||
393         ((Abs (aColorPicked.r() - aColorInput.r()) <= Precision::Confusion())
394       && (Abs (aColorPicked.g() - aColorInput.g()) <= Precision::Confusion())
395       && (Abs (aColorPicked.b() - aColorInput.b()) <= Precision::Confusion())))
396     {
397       isNotEqual = Standard_False;
398       break;
399     }
400   }
401
402   theDi << "RED :   " << aColorPicked.r() << " "
403         << "GREEN : " << aColorPicked.g() << " "
404         << "BLUE :  " << aColorPicked.b() << "\n";
405
406   if (theArgsNb == 6)
407   {
408     theDi << "User color: \n"
409           << "RED :   " << aColorInput.r() << " "
410           << "GREEN : " << aColorInput.g() << " "
411           << "BLUE :  " << aColorInput.b() << "\n";
412   }
413
414   if (isNotEqual)
415   {
416     theDi << "Faulty : colors are not equal.\n";
417     return 1; // TCL_ERROR
418   }
419   return 0;
420 }
421
422 #if ! defined(WNT)
423 extern int ViewerMainLoop (Standard_Integer argc, const char ** argv);
424 #else
425 Standard_EXPORT int ViewerMainLoop (Standard_Integer argc, const char ** argv);
426 #endif
427 #if ! defined(WNT)
428 extern int ViewerMainLoop2d (Standard_Integer argc, const char ** argv);
429 #else
430 Standard_EXPORT int ViewerMainLoop2d (Standard_Integer argc, const char ** argv);
431 #endif
432
433 static Standard_Integer QAAISGetMousePoint (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
434 {
435   if ( argc != 1 ) {
436     di << "Usage : " << argv[0] << "\n";
437     return 1;
438   }
439   Handle (V3d_View) QAAISView = ViewerTest::CurrentView ();
440   if ( QAAISView.IsNull () ) {
441     di << "You must initialize AISViewer before this command." << "\n";
442     return 1;
443   }
444   Standard_Integer QAAISMouseCoordinateX = 0;
445   Standard_Integer QAAISMouseCoordinateY = 0;
446   Standard_Integer argccc = 5;
447   const char *bufff[] = { "A", "B", "C", "D", "E" };
448   const char **argvvv = (const char **) bufff;
449   while ( ViewerMainLoop (argccc, argvvv) ) {
450     ViewerTest::GetMousePosition (QAAISMouseCoordinateX, QAAISMouseCoordinateY);
451   }
452   ViewerTest::GetMousePosition (QAAISMouseCoordinateX, QAAISMouseCoordinateY);
453   di << "X-coordinate: " << QAAISMouseCoordinateX << "; Y-coordinate: " << QAAISMouseCoordinateY << "\n";
454   return 0;
455 }
456
457 static Standard_Integer QAAISGetColorCoord (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
458 {
459 #if ! defined(WNT)
460   if ( argc > 2 ) {
461     di << "Usage : " << argv[0] << " [3d|2d]" << "\n";
462     return 1;
463   }
464   Handle(Xw_Window) QAAISWindow;
465
466   Standard_Boolean is3d = 1;
467
468   if(argc == 1 || !strcmp(argv[1],"3d")) {
469
470     Handle (V3d_View) QAAIS_MainView = ViewerTest::CurrentView ();
471     if ( QAAIS_MainView.IsNull () ) {
472       di << "You must initialize AISViewer before this command." << "\n";
473       return 1;
474     }
475     QAAISWindow = Handle(Xw_Window)::DownCast (QAAIS_MainView->V3d_View::Window());
476     is3d = 1;
477   }
478
479   Standard_Integer QAAIS_WindowSize_X = 0;
480   Standard_Integer QAAIS_WindowSize_Y = 0;
481   QAAISWindow->Size(QAAIS_WindowSize_X, QAAIS_WindowSize_Y);
482   Handle (Graphic3d_GraphicDevice) QAAIS_GraphicDevice = new Graphic3d_GraphicDevice (getenv ("DISPLAY"));
483
484   Draw_Window QAAIS_CoordWindow ("coordinate", 421, 205, 200, 60);
485   QAAIS_CoordWindow.DisplayWindow ();
486   QAAIS_CoordWindow.SetColor (12);
487
488   Handle (Xw_Window) QAAIS_ColorWindow = new Xw_Window (QAAIS_GraphicDevice, "color", 0.4074, 0.678, 0.1962, 0.06, Xw_WQ_3DQUALITY, Quantity_NOC_BLACK);
489   Handle (V3d_Viewer) QAAIS_ColorViewer = new V3d_Viewer (QAAIS_GraphicDevice, Standard_ExtString ("COLOR"));
490   Handle (V3d_View) QAAIS_ColorView = QAAIS_ColorViewer -> CreateView ();
491   QAAIS_ColorWindow -> Map ();
492   QAAIS_ColorView -> SetWindow (QAAIS_ColorWindow);
493   QAAIS_ColorView -> SetBackgroundColor (Quantity_NOC_BLACK);
494   QAAIS_ColorView -> Redraw ();
495
496   Standard_Integer QAAIS_MousePoint_X = 0;
497   Standard_Integer QAAIS_MousePoint_Y = 0;
498   Standard_Character QAAIS_MousePointX[32];
499   Sprintf (QAAIS_MousePointX, "X : %d", QAAIS_MousePoint_X);
500   Standard_Character QAAIS_MousePointY[32];
501   Sprintf (QAAIS_MousePointY, "Y : %d", QAAIS_MousePoint_Y);
502   Standard_ShortReal QAAIS_ColorRED = 0;
503   Standard_ShortReal QAAIS_ColorGRN = 0;
504   Standard_ShortReal QAAIS_ColorBLU = 0;
505   Quantity_Color QAAIS_ShowingColor;
506   QAAIS_ShowingColor.SetValues (QAAIS_ColorRED, QAAIS_ColorGRN, QAAIS_ColorBLU, Quantity_TOC_RGB);
507   Standard_Integer argccc = 5;
508   const char *bufff[] = { "A", "B", "C", "D", "E" };
509   const char **argvvv = (const char **) bufff;
510   while ( is3d ? ViewerMainLoop (argccc, argvvv) : ViewerMainLoop2d (argccc, argvvv)) {
511     Handle(TColStd_HSequenceOfReal) aSeq;
512     Image_PixMap anImage;
513     if(is3d)
514     {
515       ViewerTest::GetMousePosition (QAAIS_MousePoint_X, QAAIS_MousePoint_Y);
516       Handle (V3d_View) QAAIS_MainView = ViewerTest::CurrentView();
517       QAAIS_MainView->ToPixMap (anImage, QAAIS_WindowSize_X, QAAIS_WindowSize_Y);
518     }
519     aSeq = GetColorOfPixel (anImage, QAAIS_MousePoint_X, QAAIS_MousePoint_Y, 0);
520
521     QAAIS_ColorRED = aSeq->Value(1);
522     QAAIS_ColorGRN = aSeq->Value(2);
523     QAAIS_ColorBLU = aSeq->Value(3);
524     QAAIS_ShowingColor.SetValues (QAAIS_ColorRED, QAAIS_ColorGRN, QAAIS_ColorBLU, Quantity_TOC_RGB);
525     QAAIS_ColorView -> SetBackgroundColor (QAAIS_ShowingColor);
526     QAAIS_ColorView -> Redraw ();
527     QAAIS_CoordWindow.Clear();
528     Sprintf (QAAIS_MousePointX, "X : %d", QAAIS_MousePoint_X);
529     Sprintf (QAAIS_MousePointY, "Y : %d", QAAIS_MousePoint_Y);
530     QAAIS_CoordWindow.DrawString (30, 35, QAAIS_MousePointX);
531     QAAIS_CoordWindow.DrawString (125, 35, QAAIS_MousePointY);
532   }
533   QAAIS_CoordWindow.Destroy ();
534 #endif //WNT
535     return 0;
536 }
537
538 //==============================================================================
539 //  VIEWER GLOBALs
540 //==============================================================================
541 #ifndef WNT
542 extern Draw_Viewer dout;
543 extern Display*         Draw_WindowDisplay;
544 extern Colormap         Draw_WindowColorMap;
545 #else
546 Standard_IMPORT Draw_Viewer dout;
547 #endif
548
549 //=======================================================================
550 #if ! defined(WNT)
551 extern ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
552 extern Handle(AIS_InteractiveContext)& TheAISContext();
553 #else
554 Standard_EXPORT ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
555 Standard_EXPORT Handle(AIS_InteractiveContext)& TheAISContext();
556 #endif
557 #include <ViewerTest_DoubleMapOfInteractiveAndName.hxx>
558 #include <AIS_Trihedron.hxx>
559 #include <AIS_Axis.hxx>
560 #include <Geom_Line.hxx>
561 #include <AIS_Line.hxx>
562
563 //==============================================================================
564 // function : VTrihedronOrigins
565 // author   : ota
566 // purpose  : draws triheron axis origin lines.
567 // Draw arg : vtri_orig trihedron_name
568 //==============================================================================
569 static int VTrihedronOrigins(Draw_Interpretor& di,
570                               Standard_Integer argc,
571                               const char ** argv)
572 {
573   if(argc != 2){
574     di <<"Usage : vtri_orig tri_name"<<"\n";
575     return 1;
576   }
577
578   if(TheAISContext().IsNull()){
579     di<<"Make 'vinit' before this method call"<<"\n";
580     return 1;
581   }
582
583   //get trihedron from AIS map.
584   TCollection_AsciiString aName(argv[1]);
585   if(!GetMapOfAIS().IsBound2(aName)){
586     di<<"No object named '"<<argv[1]<<"'"<<"\n";
587     return 1;
588   }
589
590   Handle(AIS_Trihedron) aTrih =
591     Handle(AIS_Trihedron)::DownCast(GetMapOfAIS().Find2(aName));
592   if(aTrih.IsNull()){
593     di<<"Trihedron is not found, try another name"<<"\n";
594     return 1;
595   }
596
597   //get axis
598   Handle(AIS_Axis) XAxis = aTrih->XAxis();
599   Handle(AIS_Axis) YAxis = aTrih->YAxis();
600   Handle(AIS_Axis) ZAxis = aTrih->Axis();
601
602   //get geometrical lines
603   Handle(Geom_Line) XGLine = XAxis->Component();
604   Handle(Geom_Line) YGLine = YAxis->Component();
605   Handle(Geom_Line) ZGLine = ZAxis->Component();
606
607   //make AIS_Lines
608   Handle(AIS_Line) XLine = new AIS_Line(XGLine);
609   Handle(AIS_Line) YLine = new AIS_Line(YGLine);
610   Handle(AIS_Line) ZLine = new AIS_Line(ZGLine);
611
612   //put them into AIS map:
613   GetMapOfAIS().Bind(XLine,aName+"_X");
614   GetMapOfAIS().Bind(YLine,aName+"_Y");
615   GetMapOfAIS().Bind(ZLine,aName+"_Z");
616   //print names of created objects:
617   di<<argv[1]<<"_X  "<<argv[1]<<"_Y  "<<argv[1]<<"_Z"<<"\n";
618
619   //try to draw them:
620   TheAISContext()->Display(XLine);
621   TheAISContext()->Display(YLine);
622   TheAISContext()->Display(ZLine);
623
624   return 0;
625 }
626
627 #include <Draw_Viewer.hxx>
628 #include <Draw.hxx>
629
630 static Standard_Integer ViewId(const Standard_CString a)
631 {
632   Standard_Integer id = Draw::Atoi(a);
633   if ((id < 0) || (id >= MAXVIEW)) {
634     cout << "Incorrect view-id, must be in 0.."<<MAXVIEW-1<<endl;
635     return -1;
636   }
637   if (!dout.HasView(id)) {
638     cout <<"View "<<id<<" does not exist."<<endl;
639     return -1;
640   }
641   return id;
642 }
643
644 #include <Draw_Display.hxx>
645
646 //=======================================================================
647 // QArename
648 //=======================================================================
649
650 static Standard_Integer QArename(Draw_Interpretor& di, Standard_Integer n, const char** a)
651 {
652   if (n < 3) return 1;
653   Standard_Boolean cop = !strcasecmp(a[0],"copy");
654
655   Handle(Draw_Drawable3D) D;
656   for (Standard_Integer i = 1; i < n; i += 2) {
657     if (i+1 >= n) return 0;
658     D = Draw::Get(a[i]);
659     if (!D.IsNull()) {
660       if (cop)
661         D = D->Copy();
662       else
663         // clear old name
664         Draw::Set(a[i],Handle(Draw_Drawable3D()));
665
666       Draw::Set(a[i+1],D);
667     }
668   }
669   return 0;
670 }
671
672 //#if defined(V2D)
673 //#include <AIS2D_InteractiveContext.hxx>
674 //static Standard_Integer QANbSelected2d (Draw_Interpretor& /*di*/, Standard_Integer argc, char** argv)
675 //{
676 //  if(argc != 1) {
677 //    cout << "Usage : " << argv[0] << endl;
678 //    return 1;
679 //  }
680 //  Handle(AIS2D_InteractiveContext) aContext = Viewer2dTest::GetAIS2DContext();
681 //  if(aContext.IsNull()) {
682 //    cerr << "use 'v2dinit' command before " << argv[0] << endl;
683 //    return 1;
684 //  }
685 //  cout << aContext->NbSelected() << endl;
686 //  return 0;
687 //}
688 //#endif
689
690 void QADraw::CommonCommands(Draw_Interpretor& theCommands)
691 {
692   ios::sync_with_stdio();
693
694   st_err = dup(STDERR_FILENO);
695
696   Draw_BeforeCommand_old = Draw_BeforeCommand;
697   Draw_AfterCommand_old  = Draw_AfterCommand;
698
699   Draw_BeforeCommand = &before;
700   Draw_AfterCommand  = &after;
701
702   const char* group = "QA_Commands";
703
704   theCommands.Add("QARebuild","QARebuild command_name",__FILE__,QARebuild,group);
705   theCommands.Add("QAGetPixelColor", "QAGetPixelColor coordinate_X coordinate_Y [color_R color_G color_B]", __FILE__,QAAISGetPixelColor, group);
706   theCommands.Add("QAGetMousePoint", "QAGetMousePoint", __FILE__,QAAISGetMousePoint, group);
707   theCommands.Add("QAGetColorCoord", "QAGetColorCoord [3d|2d]", __FILE__,QAAISGetColorCoord, group);
708   theCommands.Add("vtri_orig",
709                   "vtri_orig         : vtri_orig trihedron_name  -  draws axis origin lines",
710                   __FILE__,VTrihedronOrigins,group);
711
712 // adding commands "rename" leads to the fact that QA commands doesn't work properly OCC23410, use function "renamevar"
713 // theCommands.Add("rename","rename name1 toname1 name2 toname2 ...",__FILE__,QArename,group);
714 }
715 /*
716 extern "C" int Tkqadraw_Init(Tcl_Interp *);
717
718 int Tkqadraw_Init(Tcl_Interp * ) {
719
720   ios::sync_with_stdio();
721
722   QADraw::CommonCommands(theCommands);
723   QADraw::AdditionalCommands(theCommands);
724
725   ViewerTest::Commands (theCommands);
726   ViewerTest::ViewerCommands (theCommands);
727
728   Viewer2dTest::Commands(theCommands);
729 //   if (Tcl_PkgProvide(theCommands.Interp(), "Qa", "1.0") != TCL_OK) {
730 //     return TCL_ERROR;
731 //   }
732
733   return TCL_OK;
734 }
735 */
736 //==============================================================================
737 // QADraw::Factory
738 //==============================================================================
739 void QADraw::Factory(Draw_Interpretor& theCommands)
740 {
741   thePCommands = &theCommands;
742
743   // definition of QA Command
744   QADraw::CommonCommands(theCommands);
745   QADraw::AdditionalCommands(theCommands);
746
747   //ViewerTest::Commands (theCommands);
748   //ViewerTest::ViewerCommands (theCommands);
749
750 //#if defined(V2D)
751 //    Viewer2dTest::Commands(theCommands);
752 //#endif
753
754 //#ifdef DEB
755 //      cout << "Draw Plugin : QA commands are loaded" << endl;
756 //#endif
757 }
758
759 // Declare entry point PLUGINFACTORY
760 DPLUGIN(QADraw)