f36ba2ca47225bd64fa652fc70dcb1004e44d810
[occt.git] / src / Draw / Draw_GraphicCommands.cxx
1 // Created on: 1995-02-23
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21 // **************************************************************
22
23
24 // Modif : DFO 05/11/96
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28
29 #include <Draw.ixx>
30 #include <Draw_Appli.hxx>
31
32 #include <Draw_Drawable3D.hxx>
33 #include <Draw_Grid.hxx>
34 #include <Draw_Display.hxx>
35 #include <Draw_Text3D.hxx>
36 #include <Draw_Text2D.hxx>
37
38 #include <Standard_Stream.hxx>
39
40 #include <stdio.h>
41 #ifdef WNT
42 extern Draw_Viewer dout;
43 extern Standard_Boolean Draw_Batch;
44 #endif
45 #ifdef HAVE_STRINGS_H
46 # include <strings.h>
47 #endif
48
49 extern Standard_Boolean Draw_BlackBackGround;
50
51
52 #define DEFROTATE (5 * M_PI/ 180.)
53 #define DEFMAGNIFY 1.1
54 #define DEFPANNING 0.1
55 #define DEFFOCAL 1.1
56 #define DEFFRAME 10
57 #define DEFGRIDSTEP 100.0
58 static Standard_Real steprot = DEFROTATE;
59 static Standard_Real steppan = DEFPANNING;
60 static Standard_Real stepmagnify = DEFMAGNIFY;
61 static Standard_Real stepfocal = DEFFOCAL;
62 static Standard_Real frame = DEFFRAME;
63 static Standard_Real DefaultGridStep = DEFGRIDSTEP ;
64
65 #define FONTLENGTH
66 static char Draw_fontname[FONTLENGTH]="Helvetica";
67 static char Draw_fontsize[FONTLENGTH]="150";
68 static char Draw_fontnamedefault[FONTLENGTH]="Helvetica";
69 static char Draw_fontsizedefault[FONTLENGTH]="150";
70
71 // *******************************************************************
72 // Graphic commands
73 // *******************************************************************
74
75 static Standard_Integer ViewId(const Standard_CString a)
76 {
77   Standard_Integer id = atoi(a);
78   if ((id < 0) || (id >= MAXVIEW)) {
79     cout << "Incorrect view-id, must be in 0.."<<MAXVIEW-1<<endl;
80     return -1;
81   }
82   if (!dout.HasView(id)) {
83     cout <<"View "<<id<<" does not exist."<<endl;
84     return -1;
85   }
86   return id;
87 }
88
89 static void SetTitle(const Standard_Integer id)
90 {
91   if (dout.HasView(id)) {
92     char title[255];
93     sprintf(title,"%d : %s - Zoom %f",id,dout.GetType(id),dout.Zoom(id));
94     dout.SetTitle(id,title);
95   }
96 }
97
98 //=======================================================================
99 //function : zoom
100 //purpose  :
101 //=======================================================================
102
103 static Standard_Integer zoom(Draw_Interpretor& , Standard_Integer n, const char** a)
104 {
105   // one argument -> All Views
106   // two argument -> First is the view
107   Standard_Boolean z2d = !strcasecmp(a[0],"2dzoom");
108   if (n == 2) {
109     Standard_Real z = atof(a[1]);
110     for (Standard_Integer id = 0; id < MAXVIEW; id++) {
111       if (dout.HasView(id)) {
112         if ((z2d && !dout.Is3D(id)) || (!z2d && dout.Is3D(id))) {
113           dout.SetZoom(id,z);
114           SetTitle(id);
115           dout.RepaintView(id);
116         }
117       }
118     }
119     return 0;
120   }
121   else if (n >= 3) {
122     Standard_Integer id = ViewId(a[1]);
123     if (id < 0) return 1;
124     Standard_Real z = atof(a[2]);
125     dout.SetZoom(id,z);
126     dout.RepaintView(id);
127     SetTitle(id);
128     return 0;
129   }
130   else
131     return 1;
132 }
133
134 //=======================================================================
135 //function : wzoom
136 //purpose  :
137 //=======================================================================
138
139 static Standard_Integer wzoom(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
140 {
141   Standard_Integer id,X,Y,W,H,X1,Y1,X2,Y2,b;
142   Standard_Real dX1,dY1,dX2,dY2,zx,zy;
143   if(argc != 1 && argc != 6)
144   {
145     di<<"Usage : " << argv[0] << " [view-id X1 Y1 X2 Y2]\n";
146     return 1;
147   }
148   if(argc == 1)
149   {
150     di << "Pick first corner"<<"\n";
151     dout.Select(id,X1,Y1,b);
152
153     gp_Trsf T;
154     gp_Pnt P0(0,0,0);
155     dout.GetTrsf(id,T);
156     T.Invert();
157     P0.Transform(T);
158     Standard_Real z = dout.Zoom(id);
159
160     dX1=X1;       dY1=Y1;
161     dX1-=P0.X();  dY1-=P0.Y();
162     dX1/=z;       dY1/=z;
163
164     if (b != 1) return 0;
165     if (id < 0) return 0;
166     Draw_Display d = dout.MakeDisplay(id);
167     d.SetColor(Draw_blanc);
168     d.SetMode(10);
169     Standard_Real dOX2 = dX1;
170     Standard_Real dOY2 = dY1;
171     d.Draw(gp_Pnt2d(dX1,dY1),gp_Pnt2d(dX1,dOY2));
172     d.Draw(gp_Pnt2d(dX1,dOY2),gp_Pnt2d(dOX2,dOY2));
173     d.Draw(gp_Pnt2d(dOX2,dOY2),gp_Pnt2d(dOX2,dY1));
174     d.Draw(gp_Pnt2d(dOX2,dY1),gp_Pnt2d(dX1,dY1));
175     d.Flush();
176     dout.GetPosSize(id,X,Y,W,H);
177     di << "Pick second corner"<<"\n";
178     b = 0;
179     while (b == 0) {
180       dout.Select(id,X2,Y2,b,Standard_False);
181       dX2=X2;          dY2=Y2;
182       dX2-=P0.X();     dY2-=P0.Y();
183       dX2/=z;          dY2/=z;
184
185       d.Draw(gp_Pnt2d(dX1,dY1),gp_Pnt2d(dX1,dOY2));
186       d.Draw(gp_Pnt2d(dX1,dOY2),gp_Pnt2d(dOX2,dOY2));
187       d.Draw(gp_Pnt2d(dOX2,dOY2),gp_Pnt2d(dOX2,dY1));
188       d.Draw(gp_Pnt2d(dOX2,dY1),gp_Pnt2d(dX1,dY1));
189       d.Draw(gp_Pnt2d(dX1,dY1),gp_Pnt2d(dX1,dY2));
190       d.Draw(gp_Pnt2d(dX1,dY2),gp_Pnt2d(dX2,dY2));
191       d.Draw(gp_Pnt2d(dX2,dY2),gp_Pnt2d(dX2,dY1));
192       d.Draw(gp_Pnt2d(dX2,dY1),gp_Pnt2d(dX1,dY1));
193       d.Flush();
194       dOX2 = dX2;
195       dOY2 = dY2;
196     }
197     d.Draw(gp_Pnt2d(dX1,dY1),gp_Pnt2d(dX1,dOY2));
198     d.Draw(gp_Pnt2d(dX1,dOY2),gp_Pnt2d(dOX2,dOY2));
199     d.Draw(gp_Pnt2d(dOX2,dOY2),gp_Pnt2d(dOX2,dY1));
200     d.Draw(gp_Pnt2d(dOX2,dY1),gp_Pnt2d(dX1,dY1));
201     d.Flush();
202     if (b != 1) return 0;
203     d.SetMode(0);
204   }
205   else
206   {
207     id = atoi(argv[1]); 
208     if ((id < 0) || (id >= MAXVIEW)) 
209     {
210       cout << "Incorrect view-id, must be in 0.."<<MAXVIEW-1<<endl;
211       return 1;
212     }
213     if (!dout.HasView(id))
214     {
215       cout <<"View "<<id<<" does not exist."<<endl;
216       return 1;
217     }
218     X1 = atoi (argv [2]);
219     Y1 = atoi (argv [3]);
220     X2 = atoi (argv [4]);
221     Y2 = atoi (argv [5]);
222
223     dout.GetPosSize(id,X,Y,W,H);
224   }
225
226   if ((X1 == X2) || (Y1 == Y2)) return 0;
227   zx = (Standard_Real) Abs(X2-X1) / (Standard_Real) W;
228   zy = (Standard_Real) Abs(Y2-Y1) / (Standard_Real) H;
229   if (zy > zx) zx = zy;
230   zx = 1/zx;
231   if (X2 < X1) X1 = X2;
232   if (Y2 > Y1) Y1 = Y2;
233   X1 = (Standard_Integer ) (X1*zx);
234   Y1 = (Standard_Integer ) (Y1*zx);
235   dout.SetZoom(id,zx*dout.Zoom(id));
236   dout.SetPan(id,-X1,-Y1);
237   dout.RepaintView(id);
238   SetTitle(id);
239   return 0;
240 }
241
242 //=======================================================================
243 //function : wclick
244 //purpose  :
245 //=======================================================================
246
247 static Standard_Integer wclick(Draw_Interpretor& di, Standard_Integer, const char**)
248 {
249   Standard_Integer id1,X1,Y1,b;
250   dout.Flush();
251   di << "Just click."<<"\n";
252   dout.Select(id1,X1,Y1,b);
253   return 0;
254 }
255
256 //=======================================================================
257 //function : view
258 //purpose  :
259 //=======================================================================
260
261 static Standard_Integer view(Draw_Interpretor& di, Standard_Integer n, const char** a)
262 {
263   if (Draw_Batch) return 1;
264
265   if ((n >= 3) && (n != 4)) {
266     Standard_Integer id = atoi(a[1]);
267     if ((id < 0) || (id >= MAXVIEW)) {
268       di <<"View-id must be in 0.."<<MAXVIEW-1<<"\n";
269       return 1;
270     }
271     Standard_Integer X = 0;
272     Standard_Integer Y = 0;
273     Standard_Integer W = 500;
274     Standard_Integer H = 500;
275     // if view exist, get old values
276     if (dout.HasView(id))
277       dout.GetPosSize(id,X,Y,W,H);
278     if (n >= 4)
279       X = atoi(a[3]);
280     if (n >= 5)
281       Y = atoi(a[4]);
282     if (n >= 6)
283       W = atoi(a[5]);
284     if (n >= 7)
285       H = atoi(a[6]);
286     dout.MakeView(id,a[2],X,Y,W,H);
287     if (!dout.HasView(id)) {
288       di << "View creation failed"<<"\n";
289       return 1;
290     }
291     SetTitle(id);
292     dout.DisplayView(id);
293     return 0;
294   }
295   else if (n == 4) {
296     // create a view on a given window
297     Standard_Integer id = atoi(a[1]);
298     if ((id < 0) || (id >= MAXVIEW)) {
299       di <<"View-id must be in 0.."<<MAXVIEW-1<<"\n";
300       return 1;
301     }
302     dout.MakeView(id,a[2],a[3]);
303     if (!dout.HasView(id)) {
304       di << "View creation failed"<<"\n";
305       return 1;
306     }
307     SetTitle(id);
308     dout.DisplayView(id);
309     return 0;
310   }
311   else
312     return 1;
313 }
314
315 //=======================================================================
316 //function : delview
317 //purpose  :
318 //=======================================================================
319
320 static Standard_Integer delview(Draw_Interpretor& , Standard_Integer n, const char** a)
321 {
322   if (n == 1) {
323     for (Standard_Integer id = 0; id < MAXVIEW; id++)
324       dout.DeleteView(id);
325     return 0;
326   }
327   else if (n >= 2) {
328     Standard_Integer id = ViewId(a[1]);
329     if (id < 0) return 1;
330     dout.DeleteView(id);
331     return 0;
332   }
333   else
334     return 1;
335 }
336
337 //=======================================================================
338 //function : fit
339 //purpose  :
340 //=======================================================================
341
342 static Standard_Integer fit(Draw_Interpretor& , Standard_Integer n, const char** a)
343 {
344   Standard_Boolean f2d = !strcasecmp(a[0],"2dfit");
345   if (n == 1) {
346     Standard_Real zoom = RealLast();
347     Standard_Integer id;
348     for ( id = 0; id < MAXVIEW; id++) {
349       if (dout.HasView(id)) {
350         if ((f2d && !dout.Is3D(id)) || (!f2d && dout.Is3D(id))) {
351 //        dout.FitView(id,frame);
352           dout.FitView(id,(Standard_Integer ) frame);
353           if (dout.Zoom(id) < zoom) zoom = dout.Zoom(id);
354         }
355       }
356     }
357     for (id = 0; id < MAXVIEW; id++) {
358       if (dout.HasView(id)) {
359         if ((f2d && !dout.Is3D(id)) || (!f2d && dout.Is3D(id))) {
360           dout.SetZoom(id,zoom);
361           dout.RepaintView(id);
362           SetTitle(id);
363         }
364       }
365     }
366     return 0;
367   }
368   else if (n >= 2) {
369     Standard_Integer id = ViewId(a[1]);
370     if (id < 0) return 1;
371 //    dout.FitView(id,frame);
372     dout.FitView(id,(Standard_Integer ) frame);
373     dout.RepaintView(id);
374     SetTitle(id);
375     return 0;
376   }
377   else
378     return 1;
379 }
380
381 //=======================================================================
382 //function : focal
383 //purpose  :
384 //=======================================================================
385
386 static Standard_Integer focal(Draw_Interpretor& , Standard_Integer n, const char** a)
387 {
388   Standard_Integer start = 0;
389   Standard_Integer end = MAXVIEW-1;
390   if (n >= 2) {
391     Standard_Integer anid = ViewId(a[1]);
392     if (anid < 0) return 1;
393     start = end = anid;
394   }
395   Standard_Real df = 1.;
396   if (!strcasecmp(a[0],"fu"))
397     df = stepfocal;
398   if (!strcasecmp(a[0],"fd"))
399     df = 1./stepfocal;
400
401   for (Standard_Integer id = start; id <= end; id++) {
402     if (!strcasecmp(dout.GetType(id),"PERS")) {
403       dout.SetFocal(id,dout.Focal(id) * df);
404       dout.RepaintView(id);
405     }
406   }
407   return 0;
408 }
409
410 //=======================================================================
411 //function : setfocal
412 //purpose  :
413 //=======================================================================
414
415 static Standard_Integer setfocal(Draw_Interpretor& di, Standard_Integer n, const char** a)
416 {
417   if (n == 1) {
418     for (Standard_Integer id = 0; id < MAXVIEW; id++) {
419       if (!strcasecmp(dout.GetType(id),"PERS"))
420         di << "Focal view "<<id<<" is "<<dout.Focal(id)<<"\n";
421     }
422   }
423   else {
424     Standard_Real f = atof(a[1]);
425     for (Standard_Integer id = 0; id < MAXVIEW; id++) {
426       if (!strcasecmp(dout.GetType(id),"PERS"))
427         dout.SetFocal(id,f);
428     }
429     dout.RepaintAll();
430   }
431   return 0;
432 }
433
434 //=======================================================================
435 //function : magnify
436 //purpose  :
437 //=======================================================================
438
439 //static Standard_Integer magnify(Draw_Interpretor& di, Standard_Integer n, const char** a)
440 static Standard_Integer magnify(Draw_Interpretor& , Standard_Integer n, const char** a)
441 {
442   Standard_Integer start = 0;
443   Standard_Integer end = MAXVIEW-1;
444   if (n >= 2) {
445     Standard_Integer anid = ViewId(a[1]);
446     if (anid < 0) return 1;
447     start = end = anid;
448   }
449   Standard_Boolean v2d = (a[0][0] == '2');   // 2dmu, 2dmd
450   const char* com = a[0];
451   if (v2d) com += 2;
452   Standard_Real dz = 1.;
453   if (!strcasecmp(com,"mu"))      // mu, 2dmu
454     dz = stepmagnify;
455   else                            // md, 2dmd
456     dz = 1/stepmagnify;
457
458   for (Standard_Integer id = start; id <= end; id++) {
459     if (dout.HasView(id)) {
460       if ((v2d && !dout.Is3D(id)) || (!v2d && dout.Is3D(id))) {
461         dout.SetZoom(id,dout.Zoom(id) * dz);
462         SetTitle(id);
463         dout.RepaintView(id);
464       }
465     }
466   }
467   return 0;
468 }
469
470 Standard_EXPORT Standard_Integer Draw_magnify(Draw_Interpretor& di, Standard_Integer n, const char** a)
471 {
472   return magnify(di,n,a);
473 }
474
475 //=======================================================================
476 //function : rotate
477 //purpose  :
478 //=======================================================================
479
480 static Standard_Integer rotate(Draw_Interpretor& , Standard_Integer n, const char** a)
481 {
482   Standard_Integer start = 0;
483   Standard_Integer end = MAXVIEW-1;
484   if (n >= 2) {
485     Standard_Integer anid = ViewId(a[1]);
486     if (anid < 0) return 1;
487     start = end = anid;
488   }
489
490   gp_Dir2d D;
491   Standard_Real ang=0;
492   if (!strcasecmp(a[0],"u")) {
493     D.SetCoord(1.,0.);
494     ang = -steprot;
495   }
496   if (!strcasecmp(a[0],"d")) {
497     D.SetCoord(1.,0.);
498     ang = steprot;
499   }
500   if (!strcasecmp(a[0],"l")) {
501     D.SetCoord(0.,1.);
502     ang = -steprot;
503   }
504   if (!strcasecmp(a[0],"r")) {
505     D.SetCoord(0.,1.);
506     ang = steprot;
507   }
508
509   for (Standard_Integer id = start; id <= end; id++) {
510     if ((!strcasecmp(dout.GetType(id),"AXON")) ||
511         (!strcasecmp(dout.GetType(id),"PERS"))) {
512       dout.RotateView(id,D,ang);
513       dout.RepaintView(id);
514     }
515   }
516   return 0;
517 }
518
519 //=======================================================================
520 //function : panning
521 //purpose  :
522 //=======================================================================
523
524 static Standard_Integer panning(Draw_Interpretor& , Standard_Integer n, const char** a)
525 {
526   Standard_Integer start = 0;
527   Standard_Integer end = MAXVIEW-1;
528   if (n >= 2) {
529     Standard_Integer anid = ViewId(a[1]);
530     if (anid < 0) return 1;
531     start = end = anid;
532   }
533   Standard_Integer DX = 0;
534   Standard_Integer DY = 0;
535   Standard_Integer X,Y,W,H;
536
537   Standard_Boolean v2d = (a[0][0] == '2');     // pu2d, pd2d, pr2d, pl2d
538   const char* com = a[0];
539   if (v2d) com += 2;
540
541   if (!strcasecmp(com,"pu"))    // pu , 2dpu
542     DY = 1;
543   if (!strcasecmp(com,"pd"))    // pd , 2dpd
544     DY = -1;
545   if (!strcasecmp(com,"pl"))    // pl , 2dpl
546     DX = -1;
547   if (!strcasecmp(com,"pr"))    // pr , 2dpr
548     DX = 1;
549
550   for (Standard_Integer id = start; id <= end; id++) {
551     if (dout.HasView(id)) {
552       if ((v2d && !dout.Is3D(id)) || (!v2d && dout.Is3D(id))) {
553         dout.GetPosSize(id,X,Y,W,H);
554 //      dout.PanView(id,W * DX * steppan, H * DY * steppan);
555         dout.PanView(id,(Standard_Integer )( W * DX * steppan),(Standard_Integer )( H * DY * steppan));
556         dout.RepaintView(id);
557       }
558     }
559   }
560   return 0;
561 }
562
563 //=======================================================================
564 //function : ptv
565 //purpose  :
566 //=======================================================================
567
568 static Standard_Integer ptv(Draw_Interpretor& , Standard_Integer n, const char** a)
569 {
570   Standard_Real X,Y,Z;
571   Standard_Integer start = 0;
572   Standard_Integer end = MAXVIEW-1;
573   if (n < 4) return 1;
574   if (n >= 5) {
575     Standard_Integer anid = ViewId(a[1]);
576     if (anid < 0) return 1;
577     start = end = anid;
578     X = atof(a[2]);
579     Y = atof(a[3]);
580     Z = atof(a[4]);
581   }
582   else {
583     X = atof(a[1]);
584     Y = atof(a[2]);
585     Z = atof(a[3]);
586   }
587
588   for (Standard_Integer id = start; id <= end; id++) {
589     gp_Trsf T;
590     dout.GetTrsf(id,T);
591     T.SetTranslationPart(gp_Vec(0,0,0));
592     gp_Trsf T1;
593     T1.SetTranslationPart(gp_Vec(-X,-Y,-Z));
594     gp_Trsf aLocalTrsf(T*T1);
595     dout.SetTrsf(id,aLocalTrsf);
596 //    dout.SetTrsf(id,T*T1);
597     dout.RepaintView(id);
598   }
599   return 0;
600 }
601
602 //=======================================================================
603 //function : dptv
604 //purpose  :
605 //=======================================================================
606
607 static Standard_Integer dptv(Draw_Interpretor& , Standard_Integer n, const char** a)
608 {
609   Standard_Real DX,DY,DZ;
610   Standard_Integer start = 0;
611   Standard_Integer end = MAXVIEW-1;
612   if (n < 4) return 1;
613   if (n >= 5) {
614     Standard_Integer anid = ViewId(a[1]);
615     if (anid < 0) return 1;
616     start = end = anid;
617     DX = atof(a[2]);
618     DY = atof(a[3]);
619     DZ = atof(a[4]);
620   }
621   else {
622     DX = atof(a[1]);
623     DY = atof(a[2]);
624     DZ = atof(a[3]);
625   }
626
627   for (Standard_Integer id = start; id <= end; id++) {
628     gp_Trsf T;
629     dout.GetTrsf(id,T);
630     gp_Trsf T1;
631     T1.SetTranslationPart(gp_Vec(-DX,-DY,-DZ));
632     gp_Trsf M = T*T1;
633     dout.SetTrsf(id,M);
634     dout.RepaintView(id);
635   }
636   return 0;
637 }
638
639
640 //=======================================================================
641 //function : color
642 //purpose  :
643 //=======================================================================
644
645 static Standard_Integer color(Draw_Interpretor& di, Standard_Integer n, const char** a)
646 {
647   if (n < 3) {
648     Draw_BlackBackGround = !Draw_BlackBackGround;
649   }
650   else if (!dout.DefineColor(atoi(a[1]),a[2])) {
651     di << "Could not allocate color "<<a[2]<<"\n";
652     return 1;
653   }
654   return 0;
655 }
656
657
658
659 //=======================================================================
660 //function : hardcopy
661 //purpose  : hardcopy                  --> hardcopy of view 1
662 //                                                  in file a4.ps
663 //                                                  in format a4
664 //           hardcopy name view        --> hardcopy of view <view>
665 //                                                  in file <name>
666 //                                                  in format a4
667 //           hardcopy name view format --> hardcopy of view <view>
668 //                                                  in file <name>
669 //                                                  in format <a4,a3,a2,a1,a0>
670 //=======================================================================
671
672 static Standard_Integer hardcopy(Draw_Interpretor& ,
673                                  Standard_Integer n, const char** a)
674 {
675   // Inch = 25.40001969 mm.
676   // 28.4 pixels / mm.
677   // format par default papier a4 210 297 mm avec marge de 3 mm.
678
679   Standard_Real rap = 28.4;
680   Standard_Real cad = 3;
681   Standard_Real dx  = 210;
682   Standard_Real dy  = 210 * Sqrt(2.);
683
684   Standard_Integer iview = 1;
685   const char* file = "a4.ps";
686   if (n >= 2) {
687     file = a[1];
688     if (n >= 3) {
689       iview = ViewId(a[2]);
690       if (iview < 0) return 1;
691       if (n >= 4) {
692         if      (!strcmp(a[3],"a7")) {
693           cad = cad / (2 * Sqrt(2));
694           dx  = dx  / (2 * Sqrt(2));
695           dy  = dy  / (2 * Sqrt(2));
696         }
697         else if (!strcmp(a[3],"a6")) {
698           cad = cad / 2;
699           dx  = dx  / 2;
700           dy  = dy  / 2;
701         }
702         else if (!strcmp(a[3],"a5")) {
703           cad = cad / Sqrt(2);
704           dx  = dx  / Sqrt(2);
705           dy  = dy  / Sqrt(2);
706         }
707         else if (!strcmp(a[3],"a4")) {
708           cad = cad;
709           dx  = dx;
710           dy  = dy;
711         }
712         else if (!strcmp(a[3],"a3")) {
713           cad = cad * Sqrt(2);
714           dx  = dx  * Sqrt(2);
715           dy  = dy  * Sqrt(2);
716         }
717         else if (!strcmp(a[3],"a2")) {
718           cad = cad * 2;
719           dx  = dx  * 2;
720           dy  = dy  * 2;
721         }
722         else if (!strcmp(a[3],"a1")) {
723           cad = cad * 2 * Sqrt(2);
724           dx  = dx  * 2 * Sqrt(2);
725           dy  = dy  * 2 * Sqrt(2);
726         }
727         else if (!strcmp(a[3],"a0")) {
728           cad = cad * 4;
729           dx  = dx  * 4;
730           dy  = dy  * 4;
731         }
732       }
733     }
734   }
735
736   Standard_Integer pxmin = (Standard_Integer)(cad * rap);
737   Standard_Integer pymin = (Standard_Integer)(cad * rap);
738   Standard_Integer pxmax = (Standard_Integer)((dx - cad) * rap);
739   Standard_Integer pymax = (Standard_Integer)((dy - cad) * rap);
740
741   ofstream os(file);
742
743   Standard_Integer vxmin,vymin,vxmax,vymax;
744   if (dout.HasView(iview)) {
745     dout.GetFrame(iview,vxmin,vymin,vxmax,vymax);
746     Standard_Real kx = (Standard_Real) (pxmax - pxmin) / (vxmax - vxmin);
747     Standard_Real ky = (Standard_Real) (pymax - pymin) / (vymax - vymin);
748     Standard_Real k = Min(Abs(kx),Abs(ky));
749     kx = (kx > 0) ? k : -k;
750     ky = (ky > 0) ? k : -k;
751     pxmax = (Standard_Integer )( pxmin + kx * (vxmax - vxmin));
752     pymax = (Standard_Integer )( pymin + ky * (vymax - vymin));
753
754     // si on veut choisir l'orientation : 90 rotate
755
756     // ecriture du header
757
758     os << "%!PS-Adobe-3.0 EPSF-3.0\n";
759     os << "%%BoundingBox: " << pxmin << " " << pymin << " "
760       << pxmax << " " << pymax << "\n";
761     os << "%%Pages: 1\n";
762     os << "%%DocumentFonts: "<<Draw_fontname<<"\n";
763     os << "%%EndComments\n";
764
765     os << "/"<<Draw_fontname<<" findfont\n"<<Draw_fontsize<<" scalefont\nsetfont\n";
766     os << "/m {moveto} bind def\n";
767     os << "/l {lineto} bind def\n";
768     os <<".1 .1 scale\n";
769
770     // draw the frame
771
772     os <<"3 setlinewidth\n0 setgray\nnewpath\n";
773     os << pxmin << " " << pymin << " m\n";
774     os << pxmax << " " << pymin << " l\n";
775     os << pxmax << " " << pymax << " l\n";
776     os << pxmin << " " << pymax << " l\n";
777     os << "closepath\nstroke\n";
778
779     // frame the view
780
781     os <<"newpath\n";
782     os << pxmin << " " << pymin << " m\n";
783     os << pxmax << " " << pymin << " l\n";
784     os << pxmax << " " << pymax << " l\n";
785     os << pxmin << " " << pymax << " l\n";
786     os << "closepath\nclip\n";
787
788     dout.PostScriptView(iview,
789                         vxmin,vymin,vxmax,vymax,
790                         pxmin,pymin,pxmax,pymax,os);
791     os <<"showpage\n";
792     os <<"%%EOF\n";
793   }
794
795   return 0;
796 }
797
798 static Standard_Integer dfont(Draw_Interpretor& di,
799                               Standard_Integer n, const char** a)
800 {
801   if        ( n == 1 ) {
802     strcpy(Draw_fontname, Draw_fontnamedefault);
803     strcpy(Draw_fontsize, Draw_fontsizedefault);
804   } else if ( n == 2 ) {
805     strcpy(Draw_fontname, a[1]);
806   } else if ( n == 3 ) {
807     strcpy(Draw_fontname, a[1]);
808     strcpy(Draw_fontsize, a[2]);
809   }
810   di<<Draw_fontname<<" "<<Draw_fontsize<<"\n";
811   return 0;
812 } // dfont
813
814 //=======================================================================
815 //function : hcolor
816 //purpose  :
817 //=======================================================================
818
819 static Standard_Integer hcolor(Draw_Interpretor& di, Standard_Integer n, const char** a)
820 {
821   if (n < 4) {
822     di << "code de couleur (Draw.cxx) : " << "\n" ;
823     di << "0 = White,\t 1 = Red,\t 2 = Green,\t 3 = Blue" << "\n" ;
824     di << "4 = Cyan,\t 5 = Gold,\t 6 = Magenta,\t 7 = Maroon"  << "\n" ;
825     di << "8 = Orange,\t 9 = Pink,\t 10 = Salmon,\t 11 = Violet" << "\n" ;
826     di << "12 = Yellow,\t 13 = Khaki,\t 14 = Coral" << "\n" ;
827     di << "1 <= width <= 11,  0 (noir)  <= gray <= 1 (blanc)" << "\n" ;
828   } else {
829     dout.PostColor(atoi(a[1]),atoi(a[2]),atof(a[3]));
830   }
831   return 0;
832 }
833
834 //=======================================================================
835 //function : xwd
836 //purpose  : xwd file from a view
837 //=======================================================================
838
839 extern void Draw_RepaintNowIfNecessary();
840
841 static Standard_Integer xwd(Draw_Interpretor& , Standard_Integer n, const char** a)
842 {
843   if (n < 2) return 1;
844
845   // enforce repaint if necessary
846   Draw_RepaintNowIfNecessary();
847
848   Standard_Integer id = 1;
849   const char* file = a[1];
850   if (n > 2) {
851     id  = atoi(a[1]);
852     file = a[2];
853   }
854   if (!dout.SaveView(id,file))
855     return 1;
856
857   return 0;
858 }
859
860 //=======================================================================
861 //function : grid
862 //purpose  : Creation/Suppression d'une grille.
863 //=======================================================================
864
865 static Standard_Integer grid (Draw_Interpretor& , Standard_Integer NbArg, const char **Arg)
866 {
867   Standard_Real StepX, StepY, StepZ ;
868
869   switch (NbArg) {
870     case 1 :
871       StepX = DefaultGridStep ;
872       StepY = DefaultGridStep ;
873       StepZ = DefaultGridStep ;
874       break ;
875     case 2 :
876       StepX = Abs (atof (Arg[1])) ;
877       StepY = Abs (atof (Arg[1])) ;
878       StepZ = Abs (atof (Arg[1])) ;
879       break ;
880     case 3 :
881       StepX = Abs (atof (Arg[1])) ;
882       StepY = Abs (atof (Arg[2])) ;
883       StepZ = Abs (atof (Arg[2])) ;
884       break ;
885     case 4 :
886       StepX = Abs (atof (Arg[1])) ;
887       StepY = Abs (atof (Arg[2])) ;
888       StepZ = Abs (atof (Arg[3])) ;
889       break ;
890     default :
891       return 1 ;
892   }
893
894 #ifdef HPUX
895   const char *temp = "grid";
896 #else
897   char temp1[] = "grid";
898   const char *temp = temp1;
899 #endif
900   Handle (Draw_Grid) Grille = Handle(Draw_Grid)::DownCast (Draw::Get(temp)) ;
901
902   Grille->Steps (StepX, StepY, StepZ) ;
903   dout.RepaintAll () ;
904
905   return 0 ;
906 }
907
908 //=======================================================================
909 //function : dflush
910 //purpose  :
911 //=======================================================================
912
913 static Standard_Integer dflush (Draw_Interpretor& , Standard_Integer, const char **)
914 {
915   dout.Flush();
916   return 0;
917 }
918
919 //=======================================================================
920 //function : dtext
921 //purpose  :
922 //=======================================================================
923
924 static Standard_Integer dtext(Draw_Interpretor& di, Standard_Integer n, const char** a)
925 {
926   gp_Pnt P;
927   Standard_Boolean is3d;
928   if (n == 2) {
929     Standard_Integer id,X,Y,b;
930     di << "Pick position with button 1, other button escape"<<"\n";
931     dout.Select(id,X,Y,b);
932     if (b != 1)
933       return 0;
934     Standard_Real z = dout.Zoom(id);
935     P.SetCoord((Standard_Real)X /z,(Standard_Real)Y /z,0);
936     gp_Trsf T;
937     dout.GetTrsf(id,T);
938     T.Invert();
939     P.Transform(T);
940     is3d = dout.Is3D(id);
941   }
942   else if (n >= 4) {
943     is3d = n > 4;
944     P.SetCoord(atof(a[1]),atof(a[2]),is3d ? atof(a[3]) : 0);
945   }
946   else
947     return 0;
948
949   if (is3d) {
950     Handle(Draw_Text3D) D = new Draw_Text3D(P,a[n-1],Draw_vert);
951     dout << D;
952   }
953   else {
954     Handle(Draw_Text2D) D = new Draw_Text2D(gp_Pnt2d(P.X(),P.Y()),
955                                             a[n-1],Draw_vert);
956     dout << D;
957   }
958   return 0;
959 }
960
961 void Draw::GraphicCommands(Draw_Interpretor& theCommands)
962 {
963   static Standard_Boolean Done = Standard_False;
964   if (Done) return;
965   Done = Standard_True;
966
967   const char* g =  "DRAW Graphic Commands";
968   theCommands.Add("wclick","wait for a mouse click",
969                   __FILE__,wclick,g);
970   theCommands.Add("zoom","zoom [view-id] z, or zoom z for all 3d views",
971                   __FILE__,zoom,g);
972   theCommands.Add("2dzoom","2dzoom [view-id] z, or zoom2d z for all 2d views",
973                   __FILE__,zoom,g);
974   theCommands.Add("wzoom","wzoom [view-id X1 Y1 X2 Y2]\n"
975       "- fits the contents of a given rectangle into a view window.\n"
976       "- The view window and rectangle corners are specified through the arguments\n"
977       "- or selected interactively by the user if no arguments are given",
978       __FILE__,wzoom,g);
979   theCommands.Add("view","view view-id type X(0) Y(0) W(500) H(500)",
980                   __FILE__,view,g);
981   theCommands.Add("delete","delete [view-id]",
982                   __FILE__,delview,g);
983   theCommands.Add("fit","fit [view-id]",
984                   __FILE__,fit,g);
985   theCommands.Add("2dfit","2dfit [view-id]",
986                   __FILE__,fit,g);
987   theCommands.Add("fu","fu [view-id], focal up",
988                   __FILE__,focal,g);
989   theCommands.Add("fd","fd [view-id], focal down",
990                   __FILE__,focal,g);
991   theCommands.Add("focal","focal [f]",
992                   __FILE__,setfocal,g);
993   theCommands.Add("mu","mu [view-id], magnify up",
994                   __FILE__,magnify,g);
995   theCommands.Add("2dmu","2dmu [view-id], magnify up",
996                   __FILE__,magnify,g);
997   theCommands.Add("md","md [view-id], magnify down",
998                   __FILE__,magnify,g);
999   theCommands.Add("2dmd","2dmd [view-id], magnify down",
1000                   __FILE__,magnify,g);
1001   theCommands.Add("u","u [view-id], rotate up",
1002                   __FILE__,rotate,g);
1003   theCommands.Add("d","d [view-id], rotate down",
1004                   __FILE__,rotate,g);
1005   theCommands.Add("l","l [view-id], rotate left",__FILE__,rotate,g);
1006   theCommands.Add("r","r [view-id], rotate right",__FILE__,rotate,g);
1007   theCommands.Add("pu","pu [view-id], panning up",__FILE__,panning,g);
1008   theCommands.Add("pd","pd [view-id], panning down",__FILE__,panning,g);
1009   theCommands.Add("pl","pl [view-id], panning left",__FILE__,panning,g);
1010   theCommands.Add("pr","pr [view-id], panning right",__FILE__,panning,g);
1011   theCommands.Add("2dpu","2dpu [view-id], panning up",__FILE__,panning,g);
1012   theCommands.Add("2dpd","2dpd [view-id], panning down",__FILE__,panning,g);
1013   theCommands.Add("2dpl","2dpl [view-id], panning left",__FILE__,panning,g);
1014   theCommands.Add("2dpr","2dpr [view-id], panning right",__FILE__,panning,g);
1015   theCommands.Add("ptv","ptv [view-id], X , Y , Z",
1016                   __FILE__,ptv,g);
1017   theCommands.Add("dptv","dptv [view-id], dX , dY , dZ",
1018                   __FILE__,dptv,g);
1019   theCommands.Add("color","color i colorname, define color i",
1020                   __FILE__,color,g);
1021   theCommands.Add("hardcopy","hardcopy [file = a4.ps] [view-id = 1] [format = a4]", __FILE__,hardcopy,g);
1022   theCommands.Add("xwd","xwd [id = 1] <filename>.{png|bmp|jpg|gif}\n\t\t: Dump contents of viewer window to PNG, BMP, JPEG or GIF file",
1023                   __FILE__,xwd,g);
1024   theCommands.Add("hcolor","hcolor icol width gray (< 1, 0 black)",
1025                   __FILE__,hcolor,g);
1026   theCommands.Add("grid", "grid [stepX(100) [stepY [stepZ]]] / 0",
1027                   __FILE__,grid, g);
1028   theCommands.Add("dflush","dflush, flush the viewer",
1029                   __FILE__,dflush,g);
1030   theCommands.Add("dtext","dtext [x y [z]] string",
1031                   __FILE__,dtext,g);
1032   theCommands.Add("dfont","dfont [name size] : set name and size of Draw font, or reset to default",
1033                   __FILE__,dfont,g);
1034 }
1035