Integration of OCCT 6.5.0 from SVN
[occt.git] / src / Graphic2d / Graphic2d_TransientManager.cxx
1 // K4           GG_161297
2 //              les methodes qui "tirent" GEOMLIGHT disparaissent
3 // PRO13311     GG_220498
4 //              Reinitialiser les parametres de vue dans tous les cas
5 // PRO14041     //GG_150698
6 //              Reinitialiser la taille de la zone visualisable dans tous
7 //              les cas.
8
9 #define G002    //GG 28/04/00 Add protection on MinMax() methods
10 //                            Add perf improvment when the used transient
11 //                   05/05/00 inherits from the current drawer. 
12 //                            Add DrawElement() DrawVertex() methods.
13
14 #define OCC402  // SAV 14/11/01 Added DrawPickedElements() - highlights picked elements.
15
16 #include <Graphic2d_TransientManager.ixx>
17 #include <Graphic2d_Drawer.hxx>
18 #include <Graphic2d_DisplayList.hxx>
19 #include <TShort_Array1OfShortReal.hxx>
20 #include <Aspect_Window.hxx>
21 #include <Graphic2d_Primitive.pxx>
22 #include <gp_Pnt2d.hxx>
23 #include <gp_Pnt.hxx>
24 #include <Graphic2d_View.hxx>
25
26 #define MAXPOINTS 1024
27 #define MAXSEGMENTS 1024
28
29 Graphic2d_TransientManager::Graphic2d_TransientManager (
30         const Handle(Graphic2d_View)& aView) : 
31                 myPView(aView.operator->()),
32                 myTrsf(),myCompositeTrsf(),
33                 myTypeOfComposition(Graphic2d_TOC_REPLACE),
34                 myDrawingIsStarted(Standard_False),
35                 myTrsfIsDefined(Standard_False),
36                 myMappingIsEnable(Standard_True),
37                 myTypeOfPrimitive(Aspect_TOP_UNKNOWN)
38 {
39 }
40
41 Graphic2d_TransientManager::Graphic2d_TransientManager (
42         const Graphic2d_ViewPtr& aView) : 
43                 myPView(aView),
44                 myTrsf(),myCompositeTrsf(),
45                 myTypeOfComposition(Graphic2d_TOC_REPLACE),
46                 myDrawingIsStarted(Standard_False),
47                 myTrsfIsDefined(Standard_False),
48                 myMappingIsEnable(Standard_True),
49                 myTypeOfPrimitive(Aspect_TOP_UNKNOWN)
50
51 }
52
53 //=======================================================================
54 Standard_Boolean Graphic2d_TransientManager::BeginDraw(
55         const Handle(Aspect_WindowDriver)& aDriver,
56         const Standard_Boolean ClearBefore) {
57 Standard_Boolean status = Standard_True;
58 Standard_Boolean reset_minmax = Standard_True;
59
60         if( myDrawingIsStarted ) return Standard_False;
61
62         if( myDriver == aDriver ) {
63           if( ClearBefore ) {
64             status = RestoreArea(aDriver);
65           } else reset_minmax = Standard_False;
66         }
67         
68 #ifdef G002
69     Handle(Graphic2d_Drawer) theDrawer = myPView->Drawer();
70     Handle(Graphic2d_Drawer) theDrawer1 = this;
71         if ( theDrawer1 != theDrawer )
72 #endif
73         {
74           Standard_ShortReal XF, YF, SF, XT, YT, ST, ZF;
75           theDrawer->Values(XF, YF, SF, XT, YT, ST, ZF);
76           Graphic2d_Drawer::SetValues(XF, YF, SF, XT, YT, ST, ZF);
77
78           Standard_ShortReal DP, DC;
79           Aspect_TypeOfDeflection TD;
80
81           theDrawer->DrawPrecision(DP,DC,TD);
82           Graphic2d_Drawer::SetDrawPrecision(DP,DC,TD);
83
84           Standard_ShortReal TP = theDrawer->TextPrecision();
85           Graphic2d_Drawer::SetTextPrecision(TP);
86         }
87
88         Graphic2d_Drawer::SetDriver(aDriver);
89
90                                         // Begin new graphics 
91         aDriver->BeginDraw(Standard_False,0);
92
93         this->EnableMinMax(Standard_True,reset_minmax);
94
95         myDrawingIsStarted = Standard_True; 
96         myTrsfIsDefined = Standard_False;
97         myTypeOfPrimitive = Aspect_TOP_UNKNOWN;
98
99         return status;
100 }
101
102 //=======================================================================
103 Standard_Boolean Graphic2d_TransientManager::BeginDraw(
104         const Handle(Aspect_WindowDriver)& aDriver,
105         const Handle(Graphic2d_ViewMapping)& aViewMapping,
106         const Standard_Real aXPosition,
107         const Standard_Real aYPosition,
108         const Standard_Real aScale,
109         const Standard_Boolean ClearBefore) {
110 Standard_Boolean status = Standard_True;
111 Standard_Boolean reset_minmax = Standard_True;
112
113         if( myDrawingIsStarted ) return Standard_False;
114
115         if( myDriver == aDriver ) {
116           if( ClearBefore ) {
117             status = RestoreArea(aDriver);
118           } else reset_minmax = Standard_False;
119         } else {
120           Graphic2d_Drawer::SetDriver(aDriver);
121         }
122                                         // Set new drawer attributes 
123 #ifdef G002
124     Handle(Graphic2d_Drawer) theDrawer = myPView->Drawer();
125     Handle(Graphic2d_Drawer) theDrawer1 = this;
126     if( theDrawer1 != theDrawer )
127 #endif
128         {
129           Standard_Real XCenter, YCenter, Size;
130  
131           aViewMapping->ViewMapping(XCenter, YCenter, Size);
132           Graphic2d_Drawer::SetValues (XCenter, YCenter, Size,
133                         aXPosition, aYPosition, aScale, aViewMapping->Zoom());
134           Standard_ShortReal DP, DC;
135           Aspect_TypeOfDeflection TD;
136
137           theDrawer->DrawPrecision(DP,DC,TD);
138           Graphic2d_Drawer::SetDrawPrecision(DP,DC,TD);
139
140           Standard_ShortReal TP = theDrawer->TextPrecision();
141           Graphic2d_Drawer::SetTextPrecision(TP);
142         }
143                                         // Begin new graphics 
144         aDriver->BeginDraw(Standard_False,0);
145
146         this->EnableMinMax(Standard_True,reset_minmax);
147
148         myDrawingIsStarted = Standard_True; 
149         myTrsfIsDefined = Standard_False;
150         myTypeOfPrimitive = Aspect_TOP_UNKNOWN;
151
152         return status;
153 }
154
155 //=======================================================================
156 void Graphic2d_TransientManager::EndDraw(const Standard_Boolean Synchronize) {
157
158         if( myDrawingIsStarted ) {
159           myDriver->EndDraw(Synchronize) ;
160           myDrawingIsStarted = Standard_False;
161           this->EnableMinMax(Standard_False,Standard_False);
162         }
163 }
164
165 //=======================================================================
166 Standard_Boolean Graphic2d_TransientManager::Restore(
167         const Handle(Aspect_WindowDriver)& aDriver) {
168 Standard_Boolean status = Standard_True;
169
170         if(( status = aDriver->Window()->BackingStore() )) {    
171                                 // Restore the front view from the back
172             aDriver->Window()->Restore();
173           } else {              // Redraw the View
174             Redraw(aDriver);
175         }
176
177         return status;
178 }
179
180 //=======================================================================
181 Standard_Boolean Graphic2d_TransientManager::RestoreArea(
182         const Handle(Aspect_WindowDriver)& aDriver) {
183 Standard_Integer pxmin,pymin,pxmax,pymax;
184 Standard_Boolean status = Standard_True;
185
186         if( this->MinMax(pxmin,pxmax,pymin,pymax) ) {
187           if(( status = aDriver->Window()->BackingStore() )) {  
188                                 // Restore the front view area from the back
189             aDriver->Window()->RestoreArea((pxmin+pxmax)/2,(pymin+pymax)/2,
190                                                 pxmax-pxmin+10,pymax-pymin+10);
191           } else {              // Redraw the View
192             Redraw(aDriver);
193           } 
194 #ifdef G002
195           this->EnableMinMax(Standard_True,Standard_True);
196 #endif
197         }
198
199         return status;
200 }
201
202 //=======================================================================
203 void Graphic2d_TransientManager::Redraw(
204         const Handle(Aspect_WindowDriver)& aDriver) {
205
206 Handle(Graphic2d_DisplayList) theDisplayList = myPView->DisplayList();
207 Handle(Graphic2d_GraphicObject) theGraphicObject;
208 Handle(Graphic2d_Drawer) theDrawer = myPView->Drawer();
209 Standard_Integer i;
210
211         aDriver->BeginDraw(Standard_True);
212         aDriver->Window()->Clear();
213         for (i=1; i<=theDisplayList->Length(); i++) {
214           theGraphicObject = theDisplayList->Value(i);
215           theGraphicObject->Redraw (theDrawer);
216         }
217         aDriver->EndDraw();
218 }
219
220 //=======================================================================
221 void Graphic2d_TransientManager::Destroy () {
222 }
223
224 //=======================================================================
225 void Graphic2d_TransientManager::Draw (
226                         const Handle(Graphic2d_Primitive) &aPrimitive) {
227
228         Graphic2d_TransientDefinitionError_Raise_if (
229                 !myDrawingIsStarted,"Drawing must be started before");
230
231         Graphic2d_TransientDefinitionError_Raise_if (
232                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN),"Bad primitive type");
233
234         if( myTrsfIsDefined ) {
235           Handle(Graphic2d_GraphicObject) theGO = 
236                                         aPrimitive->PGraphicObject();
237           gp_GTrsf2d theOldTrsf = theGO->Transform(); 
238           if( theGO->IsTransformed() && // Sets the new transformation
239                 (myTypeOfComposition != Graphic2d_TOC_REPLACE) ) {
240             gp_GTrsf2d theNewTrsf = myCompositeTrsf.Multiplied(theOldTrsf);
241             theGO->SetTransform(theNewTrsf);
242           } else {
243             theGO->SetTransform(myTrsf);
244           }
245           aPrimitive->Draw(this);
246           theGO->SetTransform(theOldTrsf);      // Restore the old
247         } else {
248           aPrimitive->Draw(this);
249         }
250
251
252 //=======================================================================
253 void Graphic2d_TransientManager::Draw (
254                         const Handle(Graphic2d_GraphicObject) &aGraphicObject) {
255
256         Graphic2d_TransientDefinitionError_Raise_if (
257                 !myDrawingIsStarted,"Drawing must be started before");
258
259         Graphic2d_TransientDefinitionError_Raise_if (
260                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN),"Bad primitive type");
261
262         if( myTrsfIsDefined ) {
263           gp_GTrsf2d theOldTrsf = aGraphicObject->Transform(); 
264           if( aGraphicObject->IsTransformed() && // Sets the new transformation
265                 (myTypeOfComposition != Graphic2d_TOC_REPLACE) ) {
266             gp_GTrsf2d theNewTrsf = myCompositeTrsf.Multiplied(theOldTrsf);
267             aGraphicObject->SetTransform(theNewTrsf);
268           } else {
269             aGraphicObject->SetTransform(myTrsf);
270           }
271           aGraphicObject->Redraw(this);
272           aGraphicObject->SetTransform(theOldTrsf);     // Restore the old
273         } else {
274           aGraphicObject->Redraw(this);
275         }
276 }
277
278 #ifdef G002
279 //=======================================================================
280 void Graphic2d_TransientManager::DrawElement (
281                         const Handle(Graphic2d_Primitive) &aPrimitive,
282                                 const Standard_Integer anIndex) {
283
284         Graphic2d_TransientDefinitionError_Raise_if (
285                 !myDrawingIsStarted,"Drawing must be started before");
286
287         Graphic2d_TransientDefinitionError_Raise_if (
288                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN),"Bad primitive type");
289
290         if( myTrsfIsDefined ) {
291           Handle(Graphic2d_GraphicObject) theGO = 
292                                         aPrimitive->PGraphicObject();
293           gp_GTrsf2d theOldTrsf = theGO->Transform(); 
294           if( theGO->IsTransformed() && // Sets the new transformation
295                 (myTypeOfComposition != Graphic2d_TOC_REPLACE) ) {
296             gp_GTrsf2d theNewTrsf = myCompositeTrsf.Multiplied(theOldTrsf);
297             theGO->SetTransform(theNewTrsf);
298           } else {
299             theGO->SetTransform(myTrsf);
300           }
301           aPrimitive->DrawElement(this,anIndex);
302           theGO->SetTransform(theOldTrsf);      // Restore the old
303         } else {
304           aPrimitive->DrawElement(this,anIndex);
305         }
306
307
308
309 // SAV
310 //=======================================================================
311 void Graphic2d_TransientManager
312 ::DrawPickedElements( const Handle(Graphic2d_Primitive) &aPrimitive )
313 {
314 #ifdef OCC402
315   Graphic2d_TransientDefinitionError_Raise_if(
316      !myDrawingIsStarted,"Drawing must be started before" );
317
318   Graphic2d_TransientDefinitionError_Raise_if(
319      (myTypeOfPrimitive != Aspect_TOP_UNKNOWN),"Bad primitive type");
320
321   if( myTrsfIsDefined ) {
322     Handle(Graphic2d_GraphicObject) theGO = 
323       aPrimitive->PGraphicObject();
324     gp_GTrsf2d theOldTrsf = theGO->Transform(); 
325     if( theGO->IsTransformed() && // Sets the new transformation
326        (myTypeOfComposition != Graphic2d_TOC_REPLACE) ) {
327       gp_GTrsf2d theNewTrsf = myCompositeTrsf.Multiplied(theOldTrsf);
328       theGO->SetTransform(theNewTrsf);
329     } else {
330       theGO->SetTransform(myTrsf);
331     }
332     aPrimitive->DrawPickedElements( this );
333     theGO->SetTransform(theOldTrsf);    // Restore the old
334   } else {
335     aPrimitive->DrawPickedElements( this );
336   }
337 #endif
338 }
339
340 //=======================================================================
341 void Graphic2d_TransientManager::DrawVertex (
342                         const Handle(Graphic2d_Primitive) &aPrimitive,
343                         const Standard_Integer anIndex) {
344
345         Graphic2d_TransientDefinitionError_Raise_if (
346                 !myDrawingIsStarted,"Drawing must be started before");
347
348         Graphic2d_TransientDefinitionError_Raise_if (
349                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN),"Bad primitive type");
350
351         if( myTrsfIsDefined ) {
352           Handle(Graphic2d_GraphicObject) theGO = 
353                                         aPrimitive->PGraphicObject();
354           gp_GTrsf2d theOldTrsf = theGO->Transform(); 
355           if( theGO->IsTransformed() && // Sets the new transformation
356                 (myTypeOfComposition != Graphic2d_TOC_REPLACE) ) {
357             gp_GTrsf2d theNewTrsf = myCompositeTrsf.Multiplied(theOldTrsf);
358             theGO->SetTransform(theNewTrsf);
359           } else {
360             theGO->SetTransform(myTrsf);
361           }
362           aPrimitive->DrawVertex(this,anIndex);
363           theGO->SetTransform(theOldTrsf);      // Restore the old
364         } else {
365           aPrimitive->DrawVertex(this,anIndex);
366         }
367
368 #endif
369
370 //=======================================================================
371 void Graphic2d_TransientManager::BeginPrimitive (
372                         const Aspect_TypeOfPrimitive aType,
373                         const Standard_Integer aSize) {
374
375         Graphic2d_TransientDefinitionError_Raise_if (
376                 !myDrawingIsStarted,"Drawing must be started before");
377
378         Graphic2d_TransientDefinitionError_Raise_if (
379                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN),"A primitive is already open");
380         myTypeOfPrimitive = aType;
381
382         switch (myTypeOfPrimitive) {
383           case Aspect_TOP_UNKNOWN:
384             break;
385           case Aspect_TOP_POLYLINE:
386             myDriver->BeginPolyline((aSize > 0) ? aSize : MAXPOINTS);
387             break;
388           case Aspect_TOP_POLYGON:
389             myDriver->BeginPolygon((aSize > 0) ? aSize : MAXPOINTS);
390             break;
391           case Aspect_TOP_SEGMENTS:
392             myDriver->BeginSegments();
393             break;
394           case Aspect_TOP_ARCS:
395             myDriver->BeginArcs();
396             break;
397           case Aspect_TOP_POLYARCS:
398             myDriver->BeginPolyArcs();
399             break;
400           case Aspect_TOP_POINTS:
401             myDriver->BeginPoints();
402             break;
403           case Aspect_TOP_MARKERS:
404             myDriver->BeginMarkers();
405             break;
406       }
407 }
408
409 //=======================================================================
410 void Graphic2d_TransientManager::ClosePrimitive () {
411
412         Graphic2d_TransientDefinitionError_Raise_if (
413                 !myDrawingIsStarted,"Drawing must be started before");
414
415         Graphic2d_TransientDefinitionError_Raise_if (
416                 (myTypeOfPrimitive == Aspect_TOP_UNKNOWN),"A primitive is not open");
417         myDriver->ClosePrimitive();
418
419         myTypeOfPrimitive = Aspect_TOP_UNKNOWN;
420 }
421
422 //=======================================================================
423 void Graphic2d_TransientManager::DrawSegment (
424                                const Standard_Real X1,
425                                const Standard_Real Y1,
426                                const Standard_Real X2,
427                                const Standard_Real Y2) {
428  
429         Graphic2d_TransientDefinitionError_Raise_if (
430                 !myDrawingIsStarted,"Drawing must be started before");
431
432         Graphic2d_TransientDefinitionError_Raise_if (
433                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN) && 
434                 (myTypeOfPrimitive != Aspect_TOP_SEGMENTS),"Bad primitive type");
435
436         Standard_ShortReal x1 = Standard_ShortReal(X1), 
437                        y1 = Standard_ShortReal(Y1), 
438                        x2 = Standard_ShortReal(X2), 
439                        y2 = Standard_ShortReal(Y2);
440         if( myTrsfIsDefined ) {
441           Standard_Real U = x1, V = y1;
442           myTrsf.Transforms(U,V);
443           x1 = Standard_ShortReal(U); y1 = Standard_ShortReal(V);
444           U = x2,V = y2;
445           myTrsf.Transforms(U,V);
446           x2 = Standard_ShortReal(U); y2 = Standard_ShortReal(V);
447         }
448         if( myMappingIsEnable )
449                 Graphic2d_Drawer::MapSegmentFromTo(x1,y1,x2,y2);
450         else
451                 Graphic2d_Drawer::DrawSegment(x1,y1,x2,y2);
452         
453 }
454
455 //=======================================================================
456 void Graphic2d_TransientManager::DrawInfiniteLine (
457                                const Standard_Real X,
458                                const Standard_Real Y,
459                                const Standard_Real Dx,
460                                const Standard_Real Dy) {
461  
462         Graphic2d_TransientDefinitionError_Raise_if (
463                 !myDrawingIsStarted,"Drawing must be started before");
464
465         Graphic2d_TransientDefinitionError_Raise_if (
466                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN),"Bad primitive type");
467
468         Standard_ShortReal x  = Standard_ShortReal(X), 
469                        y  = Standard_ShortReal(Y), 
470                        dx = Standard_ShortReal(Dx), 
471                        dy = Standard_ShortReal(Dy);
472         if( myTrsfIsDefined ) {
473           Standard_Real U = x,V = y;
474           myTrsf.Transforms(U,V);
475           x = Standard_ShortReal(U); y = Standard_ShortReal(V);
476           U = dx, V = dy;
477           myTrsf.Transforms(U,V);
478           dx = Standard_ShortReal(U) - x; dy = Standard_ShortReal(V) - y;
479         }
480         if( myMappingIsEnable )
481                 Graphic2d_Drawer::MapInfiniteLineFromTo(x,y,dx,dy);
482         else
483                 Graphic2d_Drawer::DrawInfiniteLine(x,y,dx,dy);
484 }
485
486 //=======================================================================
487 void Graphic2d_TransientManager::DrawArc(
488                         const Standard_Real Xc,
489                         const Standard_Real Yc,
490                         const Standard_Real aRadius,
491                         const Standard_Real Angle1,
492                         const Standard_Real Angle2) {
493  
494         Graphic2d_TransientDefinitionError_Raise_if (
495                 !myDrawingIsStarted,"Drawing must be started before");
496
497         Graphic2d_TransientDefinitionError_Raise_if (
498                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN) &&
499                 (myTypeOfPrimitive != Aspect_TOP_ARCS),"Bad primitive type");
500
501         Standard_ShortReal xc = Standard_ShortReal(Xc), 
502                        yc = Standard_ShortReal(Yc), 
503                        r  = Standard_ShortReal(aRadius), 
504                        a1 = Standard_ShortReal(Angle1), 
505                        a2 = Standard_ShortReal(Angle2);
506         if( myTrsfIsDefined ) {
507           Standard_Real U = xc,V = yc;
508           myTrsf.Transforms(U,V);
509           xc = Standard_ShortReal(U); yc = Standard_ShortReal(V);
510           U = r * Cos(a1); V = r * Sin(a1);
511           myTrsf.Transforms(U,V); U -= Standard_Real(xc); V -= Standard_Real(yc);
512           r = Standard_ShortReal(Sqrt( U*U + V*V )) ;
513           Standard_ShortReal a = Standard_ShortReal(atan2(U,V));
514           a2 += a - a1; a1 = a;
515         }
516         if( myMappingIsEnable )
517                 Graphic2d_Drawer::MapArcFromTo(xc,yc,r,a1,a2);
518         else
519                 Graphic2d_Drawer::DrawArc(xc,yc,r,a1,a2);
520 }
521
522 //=======================================================================
523 void Graphic2d_TransientManager::DrawPolyArc(
524                         const Standard_Real Xc,
525                         const Standard_Real Yc,
526                         const Standard_Real aRadius,
527                         const Standard_Real Angle1,
528                         const Standard_Real Angle2) {
529  
530         Graphic2d_TransientDefinitionError_Raise_if (
531                 !myDrawingIsStarted,"Drawing must be started before");
532
533         Graphic2d_TransientDefinitionError_Raise_if (
534                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN) &&
535                 (myTypeOfPrimitive != Aspect_TOP_POLYARCS),"Bad primitive type");
536
537         Standard_ShortReal xc = Standard_ShortReal(Xc), 
538                        yc = Standard_ShortReal(Yc), 
539                        r  = Standard_ShortReal(aRadius), 
540                        a1 = Standard_ShortReal(Angle1), 
541                        a2 = Standard_ShortReal(Angle2);
542         if( myTrsfIsDefined ) {
543           Standard_Real U = xc,V = yc;
544           myTrsf.Transforms(U,V);
545           xc = Standard_ShortReal(U); yc = Standard_ShortReal(V);
546           U = r * Cos(a1); V = r * Sin(a1);
547           myTrsf.Transforms(U,V); U -= Standard_Real(xc); V -= Standard_Real(yc);
548           r = Standard_ShortReal(Sqrt( U*U + V*V )) ;
549           Standard_ShortReal a = Standard_ShortReal(atan2(U,V));
550           a2 += a - a1; a1 = a;
551         }
552         if( myMappingIsEnable )
553                 Graphic2d_Drawer::MapPolyArcFromTo(xc,yc,r,a1,a2);
554         else
555                 Graphic2d_Drawer::DrawPolyArc(xc,yc,r,a1,a2);
556 }
557
558 //=======================================================================
559 void Graphic2d_TransientManager::DrawPoint (
560                 const Standard_Real X, const Standard_Real Y) {
561  
562         Graphic2d_TransientDefinitionError_Raise_if (
563                 !myDrawingIsStarted,"Drawing must be started before");
564
565         Graphic2d_TransientDefinitionError_Raise_if (
566                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN) &&
567                 (myTypeOfPrimitive != Aspect_TOP_POLYLINE) &&
568                 (myTypeOfPrimitive != Aspect_TOP_POLYGON) &&
569                 (myTypeOfPrimitive != Aspect_TOP_MARKERS),"Bad primitive type");
570
571         Standard_ShortReal x = Standard_ShortReal(X), y = Standard_ShortReal(Y);
572         if( myTrsfIsDefined ) {
573           Standard_Real U = x,V = y;
574           myTrsf.Transforms(U,V);
575           x = Standard_ShortReal(U); y = Standard_ShortReal(V);
576         }
577         if( myMappingIsEnable )
578                 Graphic2d_Drawer::MapMarkerFromTo(0,x,y,0.,0.,0.);
579         else
580                 Graphic2d_Drawer::DrawMarker(0,x,y,0.,0.,0.);
581 }
582
583 //=======================================================================
584 void Graphic2d_TransientManager::DrawMarker(
585                         const Standard_Integer anIndex,
586                         const Standard_Real X,
587                         const Standard_Real Y,
588                         const Standard_Real aWidth,
589                         const Standard_Real anHeight,
590                         const Standard_Real anAngle) {
591
592         Graphic2d_TransientDefinitionError_Raise_if (
593                 !myDrawingIsStarted,"Drawing must be started before");
594
595         Graphic2d_TransientDefinitionError_Raise_if (
596                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN) &&
597                 (myTypeOfPrimitive != Aspect_TOP_MARKERS),"Bad primitive type");
598
599         Standard_ShortReal x = Standard_ShortReal(X), 
600                        y = Standard_ShortReal(Y), 
601                        w = Standard_ShortReal(aWidth), 
602                        h = Standard_ShortReal(anHeight), 
603                        a = Standard_ShortReal(anAngle);
604         if( myTrsfIsDefined ) {
605           Standard_Real U = x,V = y;
606           myTrsf.Transforms(U,V);
607           x = Standard_ShortReal(U); y = Standard_ShortReal(V);
608         }
609         if( myMappingIsEnable )
610                 Graphic2d_Drawer::MapMarkerFromTo(anIndex,x,y,w,h,a);
611         else
612                 Graphic2d_Drawer::DrawMarker(anIndex,x,y,w,h,a);
613 }
614
615 //=======================================================================
616 void Graphic2d_TransientManager::DrawText(
617                                      const TCollection_ExtendedString& aText,
618                                      const Standard_Real X,
619                                      const Standard_Real Y,
620                                      const Standard_Real anAngle,
621                                      const Standard_Real aDeltax,
622                                      const Standard_Real aDeltay,
623                                      const Aspect_TypeOfText aType,
624                                      const Graphic2d_TypeOfAlignment anAlignment) {
625
626         Graphic2d_TransientDefinitionError_Raise_if (
627                 !myDrawingIsStarted,"Drawing must be started before");
628
629         Graphic2d_TransientDefinitionError_Raise_if (
630                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN),"Bad primitive type");
631
632         Standard_ShortReal x = Standard_ShortReal(X), 
633                        y = Standard_ShortReal(Y), 
634                        a = Standard_ShortReal(anAngle);
635         Standard_ShortReal dx = Standard_ShortReal(0.), 
636                        dy = Standard_ShortReal(0.), 
637                        ox = Standard_ShortReal(aDeltax), 
638                        oy = Standard_ShortReal(aDeltay);
639
640         if( anAlignment != Graphic2d_TOA_LEFT ) {
641           Standard_ShortReal tw,th,txo,tyo;
642           if( Graphic2d_Drawer::GetTextSize(aText,tw,th,txo,tyo) ) {
643             switch( anAlignment ) {
644               case Graphic2d_TOA_LEFT:
645               dx = Standard_ShortReal(0.); dy = Standard_ShortReal(0.);
646               break;
647               case Graphic2d_TOA_RIGHT:
648               dx = -tw; dy = 0.;
649               break;
650               case Graphic2d_TOA_CENTER:
651               dx = Standard_ShortReal(-tw/2.); dy = 0.;
652               break;
653               case Graphic2d_TOA_TOPLEFT:
654               dx = 0.; dy = tyo-th;
655               break;
656               case Graphic2d_TOA_TOPRIGHT:
657               dx = -tw; dy = tyo-th;
658               break;
659               case Graphic2d_TOA_TOPCENTER:
660               dx = Standard_ShortReal(-tw/2.); dy = Standard_ShortReal(tyo-th);
661               break;
662               case Graphic2d_TOA_MEDIUMLEFT:
663               dx = 0.; dy = Standard_ShortReal((tyo-th)/2.);
664               break;
665               case Graphic2d_TOA_MEDIUMRIGHT:
666               dx = Standard_ShortReal(-tw); dy = Standard_ShortReal((tyo-th)/2.);
667               break;
668               case Graphic2d_TOA_MEDIUMCENTER:
669               dx = Standard_ShortReal(-tw/2.); dy = Standard_ShortReal((tyo-th)/2.);
670               break;
671               case Graphic2d_TOA_BOTTOMLEFT:
672               dx = 0.; dy = tyo;
673               break;
674               case Graphic2d_TOA_BOTTOMRIGHT:
675               dx = -tw; dy = tyo;
676               break;
677               case Graphic2d_TOA_BOTTOMCENTER:
678               dx = Standard_ShortReal(-tw/2.); dy = tyo;
679               break;
680             }
681           }
682         }
683
684         if( myTrsfIsDefined ) {
685           Standard_Real U = x,V = y;
686           myTrsf.Transforms(U,V);
687           x = Standard_ShortReal(U); y = Standard_ShortReal(V);
688           U = Cos(a); V = Sin(a);
689           myTrsf.Transforms(U,V); U -= Standard_Real(x); V -= Standard_Real(y);
690           a = Standard_ShortReal(atan2(U,V));
691         }
692         if( a != 0. ) {
693           Standard_ShortReal ax = dx, ay = dy;
694           Standard_Real cosa = Cos (a);
695           Standard_Real sina = Sin (a);
696               dx = Standard_ShortReal(XROTATE(ax,ay));
697           dy = Standard_ShortReal(YROTATE(ax,ay));
698         }
699         if( myMappingIsEnable )
700                 Graphic2d_Drawer::MapTextFromTo(aText,x+dx,y+dy,a,ox,oy,aType);
701         else
702                 Graphic2d_Drawer::DrawText(aText,x+ox+dx,y+oy+dy,a,aType);
703 }
704
705 //=======================================================================
706 void Graphic2d_TransientManager::DrawPolyText(
707                                      const TCollection_ExtendedString& aText,
708                                      const Standard_Real X,
709                                      const Standard_Real Y,
710                                      const Standard_Real anAngle,
711                                                      const Standard_Real aMargin,
712                                      const Standard_Real aDeltax,
713                                      const Standard_Real aDeltay,
714                                      const Aspect_TypeOfText aType,
715                                                      const Graphic2d_TypeOfAlignment anAlignment) {
716
717         Graphic2d_TransientDefinitionError_Raise_if (
718                 !myDrawingIsStarted,"Drawing must be started before");
719
720         Graphic2d_TransientDefinitionError_Raise_if (
721                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN),"Bad primitive type");
722
723         Standard_ShortReal x = Standard_ShortReal(X), y = Standard_ShortReal(Y), 
724                        a = Standard_ShortReal(anAngle), m = Standard_ShortReal(aMargin);
725         Standard_ShortReal dx = 0., dy = 0., ox = Standard_ShortReal(aDeltax), oy = Standard_ShortReal(aDeltay);
726
727         if( anAlignment != Graphic2d_TOA_LEFT ) {
728           Standard_ShortReal tw,th,txo,tyo;
729           if( Graphic2d_Drawer::GetTextSize(aText,tw,th,txo,tyo) ) {
730             switch( anAlignment ) {
731               case Graphic2d_TOA_LEFT:
732               dx = 0.; dy = 0.;
733               break;
734               case Graphic2d_TOA_RIGHT:
735               dx = -tw; dy = 0.;
736               break;
737               case Graphic2d_TOA_CENTER:
738               dx = Standard_ShortReal(-tw/2.); dy = 0.;
739               break;
740               case Graphic2d_TOA_TOPLEFT:
741               dx = 0.; dy = tyo-th;
742               break;
743               case Graphic2d_TOA_TOPRIGHT:
744               dx = -tw; dy = tyo-th;
745               break;
746               case Graphic2d_TOA_TOPCENTER:
747               dx = Standard_ShortReal(-tw/2.); dy = tyo-th;
748               break;
749               case Graphic2d_TOA_MEDIUMLEFT:
750               dx = 0.; dy = Standard_ShortReal((tyo-th)/2.);
751               break;
752               case Graphic2d_TOA_MEDIUMRIGHT:
753               dx = -tw; dy = Standard_ShortReal((tyo-th)/2.);
754               break;
755               case Graphic2d_TOA_MEDIUMCENTER:
756               dx = Standard_ShortReal(-tw/2.); dy = Standard_ShortReal((tyo-th)/2.);
757               break;
758               case Graphic2d_TOA_BOTTOMLEFT:
759               dx = 0.; dy = tyo;
760               break;
761               case Graphic2d_TOA_BOTTOMRIGHT:
762               dx = -tw; dy = tyo;
763               break;
764               case Graphic2d_TOA_BOTTOMCENTER:
765               dx = Standard_ShortReal(-tw/2.); dy = tyo;
766               break;
767             }
768           }
769         }
770
771         if( myTrsfIsDefined ) {
772           Standard_Real U = x,V = y;
773           myTrsf.Transforms(U,V);
774           x = Standard_ShortReal(U); y = Standard_ShortReal(V);
775           U = Cos(a); V = Sin(a);
776           myTrsf.Transforms(U,V); U -= Standard_Real(x); V -= Standard_Real(y);
777           a = Standard_ShortReal(atan2(U,V));
778         }
779         if( a != 0. ) {
780           Standard_ShortReal ax = dx, ay = dy;
781       Standard_Real cosa = Cos (a);
782       Standard_Real sina = Sin (a);
783           dx = Standard_ShortReal(XROTATE(ax,ay));
784       dy = Standard_ShortReal(YROTATE(ax,ay));
785         }
786         if( myMappingIsEnable )
787                 Graphic2d_Drawer::MapPolyTextFromTo(aText,x+dx,y+dy,a,m,ox,oy,aType);
788         else
789                 Graphic2d_Drawer::DrawPolyText(aText,x+ox+dx,y+oy+dy,a,m,aType);
790 }
791
792 //=======================================================================
793 void Graphic2d_TransientManager::DrawFramedText(
794                                      const TCollection_ExtendedString& aText,
795                                      const Standard_Real X,
796                                      const Standard_Real Y,
797                                      const Standard_Real anAngle,
798                                                      const Standard_Real aMargin,
799                                      const Standard_Real aDeltax,
800                                      const Standard_Real aDeltay,
801                                      const Aspect_TypeOfText aType,
802                                                      const Graphic2d_TypeOfAlignment anAlignment) {
803
804         Graphic2d_TransientDefinitionError_Raise_if (
805                 !myDrawingIsStarted,"Drawing must be started before");
806
807         Graphic2d_TransientDefinitionError_Raise_if (
808                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN),"Bad primitive type");
809
810         Standard_ShortReal x = Standard_ShortReal(X), 
811                        y = Standard_ShortReal(Y), 
812                        a = Standard_ShortReal(anAngle), 
813                        m = Standard_ShortReal(aMargin);
814         Standard_ShortReal dx = 0., dy = 0., 
815         ox = Standard_ShortReal(aDeltax), oy = Standard_ShortReal(aDeltay);
816
817         if( anAlignment != Graphic2d_TOA_LEFT ) {
818           Standard_ShortReal tw,th,txo,tyo;
819           if( Graphic2d_Drawer::GetTextSize(aText,tw,th,txo,tyo) ) {
820             switch( anAlignment ) {
821               case Graphic2d_TOA_LEFT:
822               dx = 0.; dy = 0.;
823               break;
824               case Graphic2d_TOA_RIGHT:
825               dx = -tw; dy = 0.;
826               break;
827               case Graphic2d_TOA_CENTER:
828               dx = Standard_ShortReal(-tw/2.); dy = 0.;
829               break;
830               case Graphic2d_TOA_TOPLEFT:
831               dx = 0.; dy = tyo-th;
832               break;
833               case Graphic2d_TOA_TOPRIGHT:
834               dx = -tw; dy = tyo-th;
835               break;
836               case Graphic2d_TOA_TOPCENTER:
837               dx = Standard_ShortReal(-tw/2.); dy = tyo-th;
838               break;
839               case Graphic2d_TOA_MEDIUMLEFT:
840               dx = 0.; dy = Standard_ShortReal((tyo-th)/2.);
841               break;
842               case Graphic2d_TOA_MEDIUMRIGHT:
843               dx = -tw; dy = Standard_ShortReal((tyo-th)/2.);
844               break;
845               case Graphic2d_TOA_MEDIUMCENTER:
846               dx = Standard_ShortReal(-tw/2.); dy = Standard_ShortReal((tyo-th)/2.);
847               break;
848               case Graphic2d_TOA_BOTTOMLEFT:
849               dx = 0.; dy = tyo;
850               break;
851               case Graphic2d_TOA_BOTTOMRIGHT:
852               dx = -tw; dy = tyo;
853               break;
854               case Graphic2d_TOA_BOTTOMCENTER:
855               dx = Standard_ShortReal(-tw/2.); dy = tyo;
856               break;
857             }
858           }
859         }
860
861         if( myTrsfIsDefined ) {
862           Standard_Real U = x,V = y;
863           myTrsf.Transforms(U,V);
864           x = Standard_ShortReal(U); y = Standard_ShortReal(V);
865           U = Cos(a); V = Sin(a);
866           myTrsf.Transforms(U,V); U -= Standard_Real(x); V -= Standard_Real(y);
867           a = Standard_ShortReal(atan2(U,V));
868         }
869         if( a != 0. ) {
870           Standard_ShortReal ax = dx, ay = dy;
871           Standard_Real cosa = Cos (a);
872           Standard_Real sina = Sin (a);
873               dx = Standard_ShortReal(XROTATE(ax,ay));
874           dy = Standard_ShortReal(YROTATE(ax,ay));
875         }
876         if( myMappingIsEnable )
877                 Graphic2d_Drawer::MapFramedTextFromTo(aText,x+dx,y+dy,a,m,ox,oy,aType);
878         else
879                 Graphic2d_Drawer::DrawFramedText(aText,x+ox+dx,y+oy+dy,a,m,aType);
880 }
881
882 //=======================================================================
883 void Graphic2d_TransientManager::SetTextAttrib(
884                                      const Standard_Integer ColorIndex,
885                      const Standard_Integer FontIndex,
886                      const Standard_Real aSlant,
887                      const Standard_Real aHScale,
888                      const Standard_Real aWScale,
889                      const Standard_Boolean isUnderlined,
890                      const Standard_Boolean isZoomable) {
891                      
892     Graphic2d_TransientDefinitionError_Raise_if (
893                 !myDrawingIsStarted,"Drawing must be started before");
894
895     Graphic2d_TransientDefinitionError_Raise_if (
896                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN),"Bad primitive type");
897
898     Standard_ShortReal sl = Standard_ShortReal(aSlant), 
899                        hsc = (aHScale > 0.) ? Standard_ShortReal(aHScale) : Standard_ShortReal(1.0);
900     Standard_ShortReal wsc = (aWScale > 0.) ? Standard_ShortReal(aWScale) : hsc;
901     
902     if( myTrsfIsDefined ) {
903       hsc *= Standard_ShortReal((myTrsf(1,1) + myTrsf(2,2))/2.);
904       wsc *= Standard_ShortReal((myTrsf(1,1) + myTrsf(2,2))/2.);
905     }
906     if( isZoomable ) {
907       hsc *= Graphic2d_Drawer::Scale(); wsc *= Graphic2d_Drawer::Scale();
908     }
909     Graphic2d_Drawer::SetTextAttrib(ColorIndex,FontIndex,sl,hsc,wsc,isUnderlined);
910
911 }
912
913 //=======================================================================
914 void Graphic2d_TransientManager::SetHidingTextAttrib(
915                                      const Standard_Integer ColorIndex,
916                                      const Standard_Integer HidingColorIndex,
917                                      const Standard_Integer FrameColorIndex,
918                                      const Standard_Integer FrameWidthIndex,
919                                      const Standard_Integer FontIndex,
920                                      const Standard_Real aSlant,
921                                      const Standard_Real aHScale,
922                                      const Standard_Real aWScale,
923                                      const Standard_Boolean isUnderlined,
924                                      const Standard_Boolean isZoomable) {
925
926     Graphic2d_TransientDefinitionError_Raise_if (
927                 !myDrawingIsStarted,"Drawing must be started before");
928
929     Graphic2d_TransientDefinitionError_Raise_if (
930                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN),"Bad primitive type");
931
932     Standard_ShortReal sl = Standard_ShortReal(aSlant), 
933         hsc = (aHScale > 0.) ? Standard_ShortReal(aHScale) : Standard_ShortReal(1.0);
934     Standard_ShortReal wsc = (aWScale > 0.) ? Standard_ShortReal(aWScale) : hsc;
935     if( myTrsfIsDefined ) {
936       hsc *= Standard_ShortReal((myTrsf(1,1) + myTrsf(2,2))/2.);
937       wsc *= Standard_ShortReal((myTrsf(1,1) + myTrsf(2,2))/2.);
938     }
939     if( isZoomable ) {
940       hsc *= Graphic2d_Drawer::Scale(); wsc *= Graphic2d_Drawer::Scale();
941     }
942     Graphic2d_Drawer::SetHidingTextAttrib(ColorIndex,HidingColorIndex,
943         FrameColorIndex,FrameWidthIndex,FontIndex,sl,hsc,wsc,isUnderlined);
944 }
945
946 //=======================================================================
947 void Graphic2d_TransientManager::SetFramedTextAttrib(
948                                      const Standard_Integer ColorIndex,
949                                      const Standard_Integer FrameColorIndex,
950                                      const Standard_Integer FrameWidthIndex,
951                                      const Standard_Integer FontIndex,
952                                      const Standard_Real aSlant,
953                                      const Standard_Real aHScale,
954                                      const Standard_Real aWScale,
955                                      const Standard_Boolean isUnderlined,
956                                      const Standard_Boolean isZoomable) {
957
958     Graphic2d_TransientDefinitionError_Raise_if (
959                 !myDrawingIsStarted,"Drawing must be started before");
960
961     Graphic2d_TransientDefinitionError_Raise_if (
962                 (myTypeOfPrimitive != Aspect_TOP_UNKNOWN),"Bad primitive type");
963
964     Standard_ShortReal sl = Standard_ShortReal(aSlant), hsc = (aHScale > 0.) ? Standard_ShortReal(aHScale) : Standard_ShortReal(1.0);
965     Standard_ShortReal wsc = (aWScale > 0.) ? Standard_ShortReal(aWScale) : hsc;
966     if( myTrsfIsDefined ) {
967       hsc *= Standard_ShortReal((myTrsf(1,1) + myTrsf(2,2))/2.);
968       wsc *= Standard_ShortReal((myTrsf(1,1) + myTrsf(2,2))/2.);
969     }
970     if( isZoomable ) {
971       hsc *= Graphic2d_Drawer::Scale(); wsc *= Graphic2d_Drawer::Scale();
972     }
973     Graphic2d_Drawer::SetFramedTextAttrib(ColorIndex,FrameColorIndex,
974                         FrameWidthIndex,FontIndex,sl,hsc,wsc,isUnderlined);
975
976
977 //=======================================================================
978 void Graphic2d_TransientManager::SetTransform (
979                                 const gp_GTrsf2d& aTrsf,
980                                 const Graphic2d_TypeOfComposition aType) {
981  
982         if( myDrawingIsStarted ) {
983           Standard_Real re = RealEpsilon ();
984           if( !myTrsfIsDefined || (aType == Graphic2d_TOC_REPLACE) ) {
985             myTrsf  = aTrsf;
986           } else {
987             myTrsf = aTrsf.Multiplied(myTrsf);
988           }
989           myTypeOfComposition = aType;
990           myCompositeTrsf = aTrsf;
991           myTrsfIsDefined = 
992                 (Abs (myCompositeTrsf.Value (1, 1) - 1.0) > re) ||
993                 (Abs (myCompositeTrsf.Value (1, 2) - 0.0) > re) ||
994                 (Abs (myCompositeTrsf.Value (1, 3) - 0.0) > re) ||
995                 (Abs (myCompositeTrsf.Value (2, 1) - 0.0) > re) ||
996                 (Abs (myCompositeTrsf.Value (2, 2) - 1.0) > re) ||
997                 (Abs (myCompositeTrsf.Value (2, 3) - 0.0) > re);
998         } else {
999           Graphic2d_TransientDefinitionError::Raise ("Drawing must be started");
1000         }
1001 }
1002
1003 //=======================================================================
1004 void Graphic2d_TransientManager::SetMapping(const Standard_Boolean aStatus) {
1005
1006         myMappingIsEnable = aStatus;
1007 }
1008
1009 //=======================================================================
1010 void Graphic2d_TransientManager::EnableMinMax(const Standard_Boolean aStatus,
1011                                        const Standard_Boolean Reset) {
1012         myMinMaxIsActivated = aStatus;
1013         if( Reset ) {
1014           myMinX = myMinY = ShortRealLast();
1015           myMaxX = myMaxY = ShortRealFirst();
1016         }
1017 }
1018
1019 //=======================================================================
1020 Standard_Boolean Graphic2d_TransientManager::MinMax (
1021                 Standard_Real &XMin, Standard_Real &YMin,
1022                 Standard_Real &XMax, Standard_Real &YMax) const {
1023
1024 #ifdef G002
1025         if( myMaxX < myMinX || myMaxY < myMinY ) return Standard_False;
1026 #endif
1027         Standard_ShortReal xmin = Max(myMinX,Standard_ShortReal(0.));
1028         Standard_ShortReal ymin = Max(myMinY,Standard_ShortReal(0.));
1029         Standard_ShortReal xmax = Min(myMaxX,mySpaceWidth); 
1030         Standard_ShortReal ymax = Min(myMaxY,mySpaceHeight);
1031
1032         if( xmax > xmin && ymax > ymin ) {
1033           Standard_ShortReal uxmin,uymin,uxmax,uymax;
1034           Graphic2d_Drawer::UnMapFromTo(xmin,ymin,uxmin,uymin);
1035           Graphic2d_Drawer::UnMapFromTo(xmax,ymax,uxmax,uymax);
1036           XMin = uxmin; YMin = uymin; XMax = uxmax; YMax = uymax;
1037           return Standard_True;
1038         } else {
1039           XMin = YMin = RealLast();
1040           XMax = YMax = RealFirst();
1041           return Standard_False;
1042         }
1043
1044
1045 //=======================================================================
1046 Standard_Boolean Graphic2d_TransientManager::MinMax(
1047                                         Standard_Integer &aMinX,
1048                                         Standard_Integer &aMaxX,
1049                                         Standard_Integer &aMinY,
1050                                         Standard_Integer &aMaxY)const
1051 {
1052         if (! myDriverIsDefined)
1053                 Graphic2d_TransientDefinitionError::Raise ("No defined driver");
1054 #ifdef G002
1055         if( myMaxX < myMinX || myMaxY < myMinY ) return Standard_False;
1056 #endif
1057
1058         Standard_ShortReal xmin = Max(myMinX,Standard_ShortReal(0.));
1059         Standard_ShortReal ymin = Max(myMinY,Standard_ShortReal(0.));
1060         Standard_ShortReal xmax = Min(myMaxX,mySpaceWidth); 
1061         Standard_ShortReal ymax = Min(myMaxY,mySpaceHeight);
1062
1063         myDriver->Convert(xmin,ymin,aMinX,aMaxY);
1064         myDriver->Convert(xmax,ymax,aMaxX,aMinY);
1065         if( aMaxX > aMinX && aMaxY > aMinY ) return Standard_True;
1066         else return Standard_False;
1067 }