0030579: Draw Harness, Draw_Interpretor - catch exceptions other than Standard_Failure
[occt.git] / src / Draw / Draw_View.cxx
1 // Copyright (c) 2015 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <Draw_View.hxx>
15 #include <Draw_Viewer.hxx>
16
17 #include <gp_Trsf.hxx>
18
19 //=======================================================================
20 //function : Draw_View
21 //purpose  : Constructor
22 //=======================================================================
23 Draw_View::Draw_View(Standard_Integer theId,
24                      Draw_Viewer*     theViewer,
25                      Standard_Integer theX,
26                      Standard_Integer theY,
27                      Standard_Integer theWidth,
28                      Standard_Integer theHeight)
29 : Draw_Window("Win", theX, theY, theWidth, theHeight),
30   myId       (theId),
31   myViewer   (theViewer),
32   myIsPers   (Standard_False),
33   myIs2D     (Standard_False),
34   myFocalDistance(0.0),
35   myZoom     (0.0),
36   myDx       (0),
37   myDy       (0),
38   myFrameX0  (0),
39   myFrameY0  (0),
40   myFrameX1  (0),
41   myFrameY1  (0)
42 {
43 }
44
45 //=======================================================================
46 //function : Draw_View
47 //purpose  : Constructor
48 //=======================================================================
49 #if defined(_WIN32) || defined(__WIN32__)
50 Draw_View::Draw_View(Standard_Integer theId,
51                      Draw_Viewer*     theViewer,
52                      Standard_Integer theX,
53                      Standard_Integer theY,
54                      Standard_Integer theWidth,
55                      Standard_Integer theHeight,
56                      HWND             theWindow)
57 : Draw_Window("Win", theX, theY, theWidth, theHeight, theWindow),
58   myId       (theId),
59   myViewer   (theViewer),
60   myIsPers   (Standard_False),
61   myIs2D     (Standard_False),
62   myFocalDistance(0.0),
63   myZoom     (0.0),
64   myDx       (0),
65   myDy       (0),
66   myFrameX0  (0),
67   myFrameY0  (0),
68   myFrameX1  (0),
69   myFrameY1  (0)
70 {
71 }
72
73 //=======================================================================
74 //function : Draw_View
75 //purpose  : Constructor
76 //=======================================================================
77 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
78 Draw_View::Draw_View(Standard_Integer theId,
79                      Draw_Viewer*     theViewer,
80                      Standard_Integer theX,
81                      Standard_Integer theY,
82                      Standard_Integer theWidth,
83                      Standard_Integer theHeight,
84                      NSWindow*        theWindow)
85 : Draw_Window(theWindow, "Win", theX, theY, theWidth, theHeight),
86   myId       (theId),
87   myViewer   (theViewer),
88   myIsPers   (Standard_False),
89   myIs2D     (Standard_False),
90   myFocalDistance(0.0),
91   myZoom     (0.0),
92   myDx       (0),
93   myDy       (0),
94   myFrameX0  (0),
95   myFrameY0  (0),
96   myFrameX1  (0),
97   myFrameY1  (0)
98 {
99 }
100 #endif
101
102
103 //=======================================================================
104 //function : Draw_View
105 //purpose  : Constructor
106 //=======================================================================
107 #if defined(_WIN32) || defined (__WIN32__) || (defined(__APPLE__) && !defined(MACOSX_USE_GLX))
108 Draw_View::Draw_View(Standard_Integer /*theId*/,
109                      Draw_Viewer*     /*theViewer*/,
110                      const char*      /*theTitle*/)
111 : Draw_Window  (),
112   myId         (-1),
113   myViewer     (NULL),
114 #else
115 Draw_View::Draw_View(Standard_Integer theId,
116                      Draw_Viewer*     theViewer,
117                      const char*      theTitle)
118 : Draw_Window  (theTitle),
119   myId         (theId),
120   myViewer     (theViewer),
121 #endif
122   myIsPers     (Standard_False),
123   myIs2D       (Standard_False),
124   myFocalDistance(0.0),
125   myZoom       (0.0),
126   myDx         (0),
127   myDy         (0),
128   myFrameX0    (0),
129   myFrameY0    (0),
130   myFrameX1    (0),
131   myFrameY1    (0)
132 {
133 }
134
135 //=======================================================================
136 //function : ~Draw_View
137 //purpose  : Destructor
138 //=======================================================================
139 Draw_View::~Draw_View()
140 {
141   Draw_Window::Destroy();
142 }
143
144 //=======================================================================
145 //function : Init
146 //purpose  :
147 //=======================================================================
148 Standard_Boolean Draw_View::Init(const char* theType)
149 {
150   { // default fields
151     myFocalDistance = 500.;
152     myIsPers        = Standard_False;
153     myIs2D          = strcmp("-2D-", theType) ? Standard_False : Standard_True;
154     myZoom          = 1;
155
156     ResetFrame();
157   }
158
159   gp_Trsf aRotation;
160   gp_Pnt  Pvise(0.0, 0.0, 0.0);
161
162   if (!strcmp("+X+Y", theType) || myIs2D)
163   {
164     myMatrix = aRotation;
165   }
166   else if (!strcmp("-Y+X", theType))
167   {
168     const gp_Dir aD(0., 0., 1.);
169     myMatrix.SetRotation(gp_Ax1(Pvise, aD), 0.5 * M_PI);
170   }
171   else if (!strcmp("-X-Y", theType))
172   {
173     const gp_Dir aD(0., 0., 1.);
174     myMatrix.SetRotation(gp_Ax1(Pvise, aD), M_PI);
175   }
176   else if (!strcmp("+Y-X", theType))
177   {
178     const gp_Dir aD(0., 0., 1.);
179     myMatrix.SetRotation(gp_Ax1(Pvise, aD), -0.5 * M_PI);
180   }
181   else if (!strcmp("+Y+X", theType))
182   {
183     const gp_Dir aD1(0., 0., 1.);
184     const gp_Dir aD2(0., 1., 0.);
185
186     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), -0.5 * M_PI);
187     aRotation.SetRotation(gp_Ax1(Pvise, aD2), M_PI);
188     myMatrix.Multiply(aRotation);
189   }
190   else if (!strcmp("-X+Y", theType))
191   {
192     const gp_Dir aD(0., 1., 0.);
193     myMatrix.SetRotation(gp_Ax1(Pvise, aD), M_PI);
194   }
195   else if (!strcmp("-Y-X", theType))
196   {
197     const gp_Dir aD1(0., 0., 1.);
198     const gp_Dir aD2(1., 0., 0.);
199
200     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), -0.5 * M_PI);
201     aRotation.SetRotation(gp_Ax1(Pvise, aD2), M_PI);
202     myMatrix.Multiply(aRotation);
203   }
204   else if (!strcmp("+X-Y", theType))
205   {
206     const gp_Dir aD(1., 0., 0.);
207     myMatrix.SetRotation(gp_Ax1(Pvise, aD), M_PI);
208   }
209   else if (!strcmp("+X+Z", theType))
210   {
211     const gp_Dir aD(1., 0., 0.);
212     myMatrix.SetRotation(gp_Ax1(Pvise, aD), -0.5 * M_PI);
213   }
214   else if (!strcmp("-Z+X", theType))
215   {
216     const gp_Dir aD1(1., 0., 0.);
217     const gp_Dir aD2(0., 1., 0.);
218
219     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), -0.5 * M_PI);
220     aRotation.SetRotation(gp_Ax1(Pvise, aD2), -0.5 * M_PI);
221     myMatrix.Multiply(aRotation);
222   }
223   else if (!strcmp("-X-Z", theType))
224   {
225     const gp_Dir aD1(1., 0., 0.);
226     const gp_Dir aD2(0., 1., 0.);
227
228     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), -0.5 * M_PI);
229     aRotation.SetRotation(gp_Ax1(Pvise, aD2), -M_PI);
230     myMatrix.Multiply(aRotation);
231   }
232   else if (!strcmp("+Z-X", theType))
233   {
234     const gp_Dir aD1(1., 0., 0.);
235     const gp_Dir aD2(0., 1., 0.);
236
237     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), -0.5 * M_PI);
238     aRotation.SetRotation(gp_Ax1(Pvise, aD2), 0.5 * M_PI);
239     myMatrix.Multiply(aRotation);
240   }
241   else if (!strcmp("+Z+X", theType))
242   {
243     const gp_Dir aD1(1., 0., 0.);
244     const gp_Dir aD2(0., 1., 0.);
245
246     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), 0.5 * M_PI);
247     aRotation.SetRotation(gp_Ax1(Pvise, aD2), 0.5 * M_PI);
248     myMatrix.Multiply(aRotation);
249   }
250   else if (!strcmp("-X+Z", theType))
251   {
252     const gp_Dir aD1(1., 0., 0.);
253     const gp_Dir aD2(0., 1., 0.);
254
255     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), 0.5 * M_PI);
256     aRotation.SetRotation(gp_Ax1(Pvise, aD2), M_PI);
257     myMatrix.Multiply(aRotation);
258   }
259   else if (!strcmp("-Z-X", theType))
260   {
261     const gp_Dir aD1(1., 0., 0.);
262     const gp_Dir aD2(0., 1., 0.);
263
264     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), 0.5 * M_PI);
265     aRotation.SetRotation(gp_Ax1(Pvise, aD2), -0.5 * M_PI);
266     myMatrix.Multiply(aRotation);
267   }
268   else if (!strcmp("+X-Z", theType))
269   {
270     const gp_Dir aD(1., 0., 0.);
271     myMatrix.SetRotation(gp_Ax1(Pvise, aD), 0.5 * M_PI);
272   }
273   else if (!strcmp("+Y+Z", theType))
274   {
275     const gp_Dir aD1(0., 1., 0.);
276     const gp_Dir aD2(1., 0., 0.);
277
278     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), -0.5 * M_PI);
279     aRotation.SetRotation(gp_Ax1(Pvise, aD2), -0.5 * M_PI);
280     myMatrix.Multiply(aRotation);
281   }
282   else if (!strcmp("-Z+Y", theType))
283   {
284     const gp_Dir aD(0., 1., 0.);
285     myMatrix.SetRotation(gp_Ax1(Pvise, aD), -0.5 * M_PI);
286   }
287   else if (!strcmp("-Y-Z", theType))
288   {
289     const gp_Dir aD1(0., 1., 0.);
290     const gp_Dir aD2(1., 0., 0.);
291
292     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), -0.5 * M_PI);
293     aRotation.SetRotation(gp_Ax1(Pvise, aD2), 0.5 * M_PI);
294     myMatrix.Multiply(aRotation);
295   }
296   else if (!strcmp("+Z-Y", theType))
297   {
298     const gp_Dir aD1(0., 1., 0.);
299     const gp_Dir aD2(1., 0., 0.);
300
301     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), -0.5 * M_PI);
302     aRotation.SetRotation(gp_Ax1(Pvise, aD2), M_PI);
303     myMatrix.Multiply(aRotation);
304   }
305   else if (!strcmp("+Z+Y", theType))
306   {
307     const gp_Dir aD(0., 1., 0.);
308     myMatrix.SetRotation(gp_Ax1(Pvise, aD), 0.5 * M_PI);
309   }
310   else if (!strcmp("-Y+Z", theType))
311   {
312     const gp_Dir aD1(0., 1., 0.);
313     const gp_Dir aD2(1., 0., 0.);
314
315     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), 0.5 * M_PI);
316     aRotation.SetRotation(gp_Ax1(Pvise, aD2), -0.5 * M_PI);
317     myMatrix.Multiply(aRotation);
318   }
319   else if (!strcmp("-Z-Y", theType))
320   {
321     const gp_Dir aD1(0., 1., 0.);
322     const gp_Dir aD2(1., 0., 0.);
323
324     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), 0.5 * M_PI);
325     aRotation.SetRotation(gp_Ax1(Pvise, aD2), M_PI);
326     myMatrix.Multiply(aRotation);
327   }
328   else if (!strcmp("+Y-Z", theType))
329   {
330     const gp_Dir aD1(0., 1., 0.);
331     const gp_Dir aD2(1., 0., 0.);
332
333     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), 0.5 * M_PI);
334     aRotation.SetRotation(gp_Ax1(Pvise, aD2), 0.5 * M_PI);
335     myMatrix.Multiply(aRotation);
336   }
337   else if (!strcmp("AXON", theType))
338   {
339     const gp_Dir aD1(1., 0., 0.);
340     const gp_Dir aD2(0., 0., 1.);
341
342     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), -0.25 * M_PI);
343     aRotation.SetRotation(gp_Ax1(Pvise, aD2), -0.25 * M_PI);
344     myMatrix.Multiply(aRotation);
345   }
346   else if (!strcmp("PERS", theType))
347   {
348     const gp_Dir aD1(1., 0., 0.);
349     const gp_Dir aD2(0., 0., 1.);
350
351     myMatrix.SetRotation (gp_Ax1(Pvise, aD1), -0.25 * M_PI);
352     aRotation.SetRotation(gp_Ax1(Pvise, aD2), -0.25 * M_PI);
353     myMatrix.Multiply(aRotation);
354
355     myIsPers = Standard_True;
356   }
357   else
358   {
359     return Standard_False;
360   }
361
362   strcpy(myType, theType);
363   return Standard_True;
364 }
365
366 //=======================================================================
367 //function : Transform
368 //purpose  :
369 //=======================================================================
370 void Draw_View::Transform(const gp_Trsf& theTransformation)
371 {
372   myMatrix.Multiply(theTransformation);
373 }
374
375 //=======================================================================
376 //function : ResetFrame
377 //purpose  :
378 //=======================================================================
379 void Draw_View::ResetFrame()
380 {
381   myFrameX0 = 0;
382   myFrameY0 = 0;
383   myFrameX1 = 0;
384   myFrameY1 = 0;
385 }
386
387 //=======================================================================
388 //function : GetFrame
389 //purpose  :
390 //=======================================================================
391 void Draw_View::GetFrame(Standard_Integer& theX0,Standard_Integer& theY0,
392                          Standard_Integer& theX1,Standard_Integer& theY1)
393 {
394   if ( myFrameX0 == myFrameX1 )
395   {
396     myViewer->GetFrame(myId, theX0, theY0, theX1, theY1);
397     myFrameX0 = theX0;
398     myFrameX1 = theX1;
399     myFrameY0 = theY0;
400     myFrameY1 = theY1;
401   }
402   else
403   {
404     theX0 = myFrameX0;
405     theX1 = myFrameX1;
406     theY0 = myFrameY0;
407     theY1 = myFrameY1;
408   }
409 }
410
411 //=======================================================================
412 //function : WExpose
413 //purpose  :
414 //=======================================================================
415 void Draw_View::WExpose()
416 {
417   ResetFrame();
418   myViewer->RepaintView(myId);
419 }