0023663: Removing 2D viewer library
[occt.git] / src / V2d / V2d.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18 // Modified     10/04/98 : FMN ; Ajout include stdio.h pour WNT
19 #define PRO12324  // 03/07/98 : SYL ; bascule echo/pas echo pour test affichage et plotter
20
21 #define TEST_IMAGE
22 #define TEST_PARAGRAPH
23
24 #include <V2d.hxx>
25 #include <Graphic2d_GraphicObject.hxx>
26 #include <Graphic2d_Circle.hxx>
27 #include <Graphic2d_InfiniteLine.hxx>
28 #include <V2d_Viewer.hxx>
29 #include <TCollection_ExtendedString.hxx>
30 #include <Graphic2d_Text.hxx>
31 #include <Graphic2d_View.hxx>
32 #include <Graphic2d_FramedText.hxx>
33 #include <Graphic2d_HidingText.hxx>
34 #include <V2d_View.hxx>
35 #include <Graphic2d_InfiniteLine.hxx>
36 #include <Graphic2d_Segment.hxx>
37 #include <Graphic2d_CircleMarker.hxx>
38 #include <Graphic2d_Marker.hxx>
39 #include <Graphic2d_ViewMapping.hxx>
40 #include <Aspect_Units.hxx>
41 #include <Aspect_FontMap.hxx>
42 #include <Aspect_FontMapEntry.hxx>
43 #include <Aspect_FontStyle.hxx>
44 #include <stdio.h>
45
46 #ifdef TEST_IMAGE
47 # include <OSD_Path.hxx>
48 # include <OSD_File.hxx>
49 # include <OSD_Protection.hxx>
50 # include <OSD_Environment.hxx>
51 # include <Graphic2d_ImageFile.hxx>
52 #endif // TEST_IMAGE
53
54 #ifdef TEST_PARAGRAPH
55 # include <Graphic2d_Paragraph.hxx>
56 # include <OSD_Environment.hxx>
57 #endif // TEST_PARAGRAPH
58
59 #define TRACE
60
61 static char alphabet[]="abcdefghijklmnopqrstuvwzyz {}[]|`~!@#$%^&*()-_=+;:',<.>/?ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789";
62 static Standard_Boolean jeveuxecho = Standard_False;
63 static Standard_Boolean TestImage  = Standard_False;
64 static Standard_Boolean TestPara   = Standard_False;
65
66 void V2d::Draw(const Handle(V2d_Viewer)& aViewer)
67 {
68   Standard_Character txt[255];
69   Handle_Graphic2d_ViewMapping aViewMapping;
70   Handle(Graphic2d_GraphicObject) go = new Graphic2d_GraphicObject(aViewer->View());
71
72 #ifdef TEST_IMAGE
73   OSD_Environment envImg ("CSF_V2D_TESTIMAGE");
74   OSD_File File;
75   if (!envImg.Value().IsEmpty ()) {
76     OSD_Protection Protection (OSD_R, OSD_R, OSD_R, OSD_R);
77     OSD_Path Path = OSD_Path (TCollection_AsciiString (envImg.Value ()), OSD_Default);
78     File.SetPath (Path);
79     File.Open (OSD_ReadOnly, Protection);
80     if (File.IsOpen ()) {
81       File.Close ();
82       TestImage = Standard_True;
83     }
84   }
85
86   if (TestImage) {
87     Handle(Graphic2d_ImageFile) myImageFile;
88     // Add non-zoomable image to GO
89     myImageFile = new Graphic2d_ImageFile (go, File, -0.1 METER, -0.1 METER);
90     myImageFile -> SetZoomable (Standard_False);
91     myImageFile -> SetColorIndex (3);
92
93     // Add zoomable image to GO
94     myImageFile = new Graphic2d_ImageFile (go, File, 0.1 METER, 0.1 METER);
95     myImageFile -> SetZoomable (Standard_True);
96
97     // Add some circles
98     Standard_Integer col = 1;
99 //    Standard_Integer i,j;
100     Standard_Integer i;
101     Standard_Real ray = .10 METER;
102     for (i = 1; i<=20; i++) {
103       Handle(Graphic2d_Circle) c = new Graphic2d_Circle(go,0. METER,0. METER,ray);
104       c->SetColorIndex(col);
105       if (i>5)
106         c->SetTypeOfPolygonFilling(Graphic2d_TOPF_EMPTY);
107       else if (i>10)
108         c->SetTypeOfPolygonFilling(Graphic2d_TOPF_FILLED);
109       else if (i>15)
110         c->SetTypeOfPolygonFilling(Graphic2d_TOPF_PATTERNED);
111       ray -= 0.005 METER  ;
112       col = col < 10 ? col + 1 : 1;
113     }
114
115   }
116 #endif // TEST_IMAGE
117
118 #ifdef TEST_PARAGRAPH
119   OSD_Environment envPara ("CSF_V2D_PZOOM");
120   TestPara = (!envPara.Value ().IsEmpty());
121   if (!TestImage && TestPara) {
122     Handle(Graphic2d_Paragraph)  myParagraph;
123     Handle(Graphic2d_HidingText) myHText;
124     // Texts to add
125     TCollection_ExtendedString zText   ("Zoomable Hiding Text");
126     TCollection_ExtendedString nzText  ("Non-Zoomable Hiding Text");
127     TCollection_ExtendedString pstr1z  ("Zoom Caracteres");
128     TCollection_ExtendedString pstr1nz ("Non-Zoom Caracteres");
129     TCollection_ExtendedString pstr21  ("Si vous ne goutez point ces");
130     TCollection_ExtendedString pstr22  ("je m'en etonne");
131     TCollection_ExtendedString pstr3   ("Mais si vous les goutez,je m'en etonne de meme");
132     TCollection_ExtendedString pstr4   ("La BRUYERE");
133
134     // Add non-zoomable paragraph in GO
135     myParagraph =
136       new Graphic2d_Paragraph (go, 0. METER, 0. METER, 0.0, Aspect_CP_NorthEast, 0.7);
137     myParagraph -> SetFrameColorIndex   (3);
138     myParagraph -> SetFrameWidthIndex   (8);
139     myParagraph -> SetCurrentColorIndex (5);
140     myParagraph -> SetHidingColorIndex  (9);
141     myParagraph -> SetZoomable (Standard_False);
142
143     myParagraph -> SetCurrentFontIndex (13);
144     myParagraph -> SetCurrentAlignment (Graphic2d_TOA_CENTER);
145     myParagraph -> SetCurrentUnderline (Standard_True);
146     myParagraph -> AddText(pstr1nz,  1);
147
148     myParagraph -> SetCurrentFontIndex (18);
149     myParagraph -> SetCurrentAlignment (Graphic2d_TOA_LEFT);
150     myParagraph -> SetCurrentUnderline (Standard_False);
151     myParagraph -> AddText(pstr21, 2);
152
153     myParagraph -> SetCurrentFontIndex (23);
154     myParagraph -> SetCurrentAlignment (Graphic2d_TOA_RIGHT);
155     myParagraph -> SetCurrentUnderline (Standard_True);
156     myParagraph -> AddText(pstr22, 3);
157
158     myParagraph -> SetCurrentFontIndex (27);
159     myParagraph -> SetCurrentAlignment (Graphic2d_TOA_LEFT);
160     myParagraph -> SetCurrentUnderline (Standard_False);
161     myParagraph -> AddText(pstr3,  4);
162
163     myParagraph -> SetCurrentFontIndex (2);
164     myParagraph -> SetCurrentAlignment (Graphic2d_TOA_RIGHT);
165     myParagraph -> SetCurrentUnderline (Standard_True);
166     myParagraph -> AddText(pstr4,  5);
167
168     // Add zoomable paragraph in GO
169     myParagraph =
170       new Graphic2d_Paragraph (go, 0. METER, 0. METER, 0.0, Aspect_CP_SouthWest, 3.5);
171     myParagraph -> SetFrameColorIndex   (3);
172     myParagraph -> SetFrameWidthIndex   (8);
173     myParagraph -> SetCurrentColorIndex (5);
174     myParagraph -> SetZoomable (Standard_True);
175
176     myParagraph -> SetCurrentFontIndex (13);
177     myParagraph -> SetCurrentAlignment (Graphic2d_TOA_CENTER);
178     myParagraph -> SetCurrentUnderline (Standard_True);
179     myParagraph -> AddText(pstr1z,  1);
180
181     myParagraph -> SetCurrentFontIndex (18);
182     myParagraph -> SetCurrentAlignment (Graphic2d_TOA_LEFT);
183     myParagraph -> SetCurrentUnderline (Standard_False);
184     myParagraph -> AddText(pstr21, 2);
185
186     myParagraph -> SetCurrentFontIndex (23);
187     myParagraph -> SetCurrentAlignment (Graphic2d_TOA_RIGHT);
188     myParagraph -> SetCurrentUnderline (Standard_True);
189     myParagraph -> AddText(pstr22, 3);
190
191     myParagraph -> SetCurrentFontIndex (27);
192     myParagraph -> SetCurrentAlignment (Graphic2d_TOA_LEFT);
193     myParagraph -> SetCurrentUnderline (Standard_False);
194     myParagraph -> AddText(pstr3,  4);
195
196     myParagraph -> SetCurrentFontIndex (2);
197     myParagraph -> SetCurrentAlignment (Graphic2d_TOA_RIGHT);
198     myParagraph -> SetCurrentUnderline (Standard_True);
199     myParagraph -> AddText(pstr4,  5);
200
201     // Add zoomable hiding text to GO
202     myHText = new Graphic2d_HidingText (go, zText,
203                   -0.25 METER, 0.25 METER, 0.0, 0.5, Aspect_TOT_SOLID, 10.);
204     myHText -> SetFrameWidthIndex  (8);
205     myHText -> SetFrameColorIndex  (1);
206     myHText -> SetHidingColorIndex (8);
207     myHText -> SetColorIndex       (5);
208     myHText -> SetZoomable (Standard_True);
209
210     // Add non-zoomable hiding text to GO
211     myHText = new Graphic2d_HidingText (go, nzText,
212                   0.25 METER, -0.25 METER, 0.0, 0.5, Aspect_TOT_SOLID, 2.);
213     myHText -> SetFrameWidthIndex  (8);
214     myHText -> SetFrameColorIndex  (1);
215     myHText -> SetHidingColorIndex (10);
216     myHText -> SetColorIndex       (5);
217     myHText -> SetZoomable (Standard_False);
218
219     // Add some circles
220     Standard_Integer col = 1;
221 //    Standard_Integer i,j;
222     Standard_Integer i;
223     Standard_Real ray = .10 METER;
224     for (i = 1; i<=20; i++) {
225       Handle(Graphic2d_Circle) c = new Graphic2d_Circle (go, 0. METER, 0. METER, ray);
226       c -> SetColorIndex(col);
227       ray -= 0.005 METER;
228       col = col < 12 ? col + 1 : 1;
229     }
230
231     // Add some markers
232     Handle(Graphic2d_Marker) M;
233     Standard_ShortReal pos = -0.15 METER;
234     for (i = 1; i <= 12; i++) {
235       M = new Graphic2d_Marker(go, i, pos, pos, 0.005 METER,0.005 METER);
236       M -> SetColorIndex (i);
237       pos -= 0.02 METER;
238     }
239   }
240 #endif // TEST_PARAGRAPH
241
242   if (!TestImage && !TestPara) {
243 #ifdef PRO12324 
244     // couleur de l' echo                              
245     Handle(Graphic2d_View) myGraphicView = aViewer->View();
246     myGraphicView->SetDefaultOverrideColor((Standard_Integer) 1); 
247 #endif
248     Handle(Aspect_FontMap) theFontMap = aViewer->FontMap();
249     Standard_Real ray;
250     Standard_Real post;
251     Standard_Real slant = 0.5;
252     Standard_Integer col = 1;
253     Standard_Integer typ ;
254     Standard_Integer wid ;
255     Standard_Integer i,j;
256     Handle(Graphic2d_Text) T;
257     Handle(Graphic2d_FramedText) TF;
258     Handle(Graphic2d_HidingText) TH;
259     Handle(Graphic2d_Segment) s; 
260      // Circles 
261 #ifdef TRACE
262     cout << "Circle " << endl;
263 #endif
264     ray = .10 METER;
265     for ( i = 1; i<=20; i++) {
266       // cout << "ray=" << ray << " col=" << col << endl;
267       Handle(Graphic2d_Circle) c = new Graphic2d_Circle(go,0. METER,0. METER,ray);
268       c->SetColorIndex(col);
269       if(i>5) 
270         c->SetTypeOfPolygonFilling(Graphic2d_TOPF_EMPTY);
271       else if (i>10)
272         c->SetTypeOfPolygonFilling(Graphic2d_TOPF_FILLED);
273       else if (i>15)
274         c->SetTypeOfPolygonFilling(Graphic2d_TOPF_PATTERNED);
275       ray -= 0.005 METER  ;
276       col = col < 10 ? col + 1 : 1;
277     }
278
279     // Lines
280     Standard_Real pos;
281     col = 1;
282     typ = 1;
283     wid = 1;
284     pos = 0.1 METER ;
285 #ifdef TRACE
286     cout << "Lines " << endl;
287 #endif
288     for ( i = 1; i<=8; i++) {
289     //   Handle(Graphic2d_InfiniteLine) l = new Graphic2d_InfiniteLine(go, 0.0 METER, pos , pos , pos);
290     //   l->SetColorIndex(col);   
291
292      s = new Graphic2d_Segment(go, -0.6 METER, pos , -0.25 METER , pos);
293      s->SetColorIndex(col);   
294      s->SetTypeIndex(typ);
295      s->SetWidthIndex(wid);
296      post = pos;
297      for ( j = 0; j<10; j++) {
298        s = new Graphic2d_Segment(go, -0.25 METER, post , -0.20 METER , post);
299        s->SetColorIndex(col);  
300        s->SetWidthIndex(1);
301        post -= 0.002 METER;
302      }
303      pos -=  0.02 METER ;
304      col = col < 8 ? col + 1 : 1;
305      typ  = typ < 4 ? typ + 1 : 1;
306      wid = wid < 8 ? wid + 1 : 1;
307     }  
308
309     // Markers
310 #ifdef TRACE
311     cout << "Markers " << endl;
312 #endif
313     Handle(Graphic2d_Marker) M ; 
314     pos = 0.2 METER;
315     for ( i = 1; i<=13; i++) {  
316      M = new Graphic2d_Marker(go,i,
317                               -.25 METER ,pos,
318                               0.005 METER,0.005 METER);
319      M->SetColorIndex(2); 
320      pos -= 0.02 METER;
321     }
322
323     //Handle(TCollection_ExtendedString) txt = new TCollection_ExtendedString(alphabet);
324
325     // Texts
326 #ifdef TRACE
327     cout << "Texts " << endl;
328 #endif
329     Standard_Integer nb = 30;
330     col = 1;
331     pos = 0.60 METER;
332     post = (0.05 + nb * 0.02) METER;
333     // traits verticaux tous les 10 mm
334     Standard_Real posh;
335     for ( i = 1; i<=40; i++) {
336      posh = (-0.15 + (i-1) * 0.01) METER;
337      s = new Graphic2d_Segment(go, posh , pos , posh , pos-post);
338      s->SetColorIndex(2);   
339      s->SetTypeIndex(1);
340      s->SetWidthIndex(1); 
341     }
342
343     Aspect_FontMapEntry entry;
344     TCollection_AsciiString aname;
345     Aspect_FontStyle style;
346     Standard_Integer iindex;
347     nb = theFontMap->Size();
348     for ( i = 1; i<=nb; i++) {  
349      // FontName
350      entry = theFontMap->Entry(i) ;   
351      iindex = entry.Index() ;
352      style = entry.Type();
353      aname = style.AliasName();   
354      sprintf(txt,"Text Pol %d:%s:%s",iindex,aname.ToCString(),alphabet);
355      T = new Graphic2d_Text(go,
356                             TCollection_ExtendedString(txt),
357                             -0.15 METER, pos, 0.);
358      T->SetFontIndex(iindex);
359      T->SetColorIndex(col);
360      // Trait horizontal 
361      s = new Graphic2d_Segment(go, -0.2 METER, pos , 0.5 METER , pos);
362      s->SetColorIndex(2);   
363      s->SetTypeIndex(1);
364      s->SetWidthIndex(1);
365
366      sprintf(txt,"Hiding Text Pol %d:%s",iindex,alphabet);
367      TH = new Graphic2d_HidingText(go,
368                                    TCollection_ExtendedString(txt),
369                                    -0.15 METER, pos - post, 
370                                    0. , 0.1 ,Aspect_TOT_SOLID);
371      TH->SetFontIndex(iindex);
372      TH->SetColorIndex(col);     
373      if( i>8 && i<15 ){
374        TH->SetFrameColorIndex(col);
375      }
376      // Frame + Underline + Slant 
377      sprintf(txt,"FramedText Pol %d",iindex);
378      TF = new Graphic2d_FramedText(go,
379                                    TCollection_ExtendedString(txt),
380                                    -0.6 METER, pos , 
381                                    slant , 0.1 ,Aspect_TOT_SOLID);
382      TF->SetFontIndex(iindex);
383      TF->SetUnderline(Standard_True);
384      TF->SetColorIndex(col);    
385
386      M = new Graphic2d_Marker(go,2,
387                               -0.6 METER ,pos,
388                               0.005 METER,0.005 METER);
389      M->SetColorIndex(2);
390
391      pos -= 0.02 METER;
392      col = col < 8 ? col + 1 : 1;
393     }
394     i=1;
395     sprintf(txt,"Texte non zoom Pol %d",i);
396     T = new Graphic2d_Text(go,
397                           TCollection_ExtendedString(txt),
398                           -0.3 METER, -0.22 METER , 
399                           0. METER);
400     T->SetColorIndex(2);
401     T->SetFontIndex(i);
402     T->SetZoomable(Standard_False);
403   }
404
405 #ifdef PRO12324 
406  // bascule echo/pas echo
407  if(jeveuxecho){
408    cout << "---- V2d ------ Highlight " << endl ;
409    go->Highlight( (Standard_Integer) 1);
410    jeveuxecho = Standard_False;  
411  }else{
412    cout << "---- V2d ------ Display" << endl ;
413    go->Display();  
414    jeveuxecho = Standard_True;  
415  }
416 #else
417  go->Display();  
418 #endif
419
420  aViewer->Update();
421 }