0024927: Getting rid of "Persistent" functionality -- Tests
[occt.git] / samples / CSharp / WPF_D3D / OCCViewer.cs
CommitLineData
15534713 1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows.Forms;
6using System.Windows.Input;
7using System.Drawing;
8
9namespace IE_WPF_D3D
10{
11 public enum CurrentAction3d
12 {
13 CurAction3d_Nothing,
14 CurAction3d_DynamicZooming,
15 CurAction3d_WindowZooming,
16 CurAction3d_DynamicPanning,
17 CurAction3d_GlobalPanning,
18 CurAction3d_DynamicRotation
19 }
20 public enum CurrentPressedKey
21 {
22 CurPressedKey_Nothing,
23 CurPressedKey_Ctrl,
24 CurPressedKey_Shift
25 }
26 public enum ModelFormat
27 {
28 BREP,
29 CSFDB,
30 STEP,
31 IGES,
32 VRML,
33 STL,
34 IMAGE
35 }
36
37 public enum DisplayMode
38 {
39 Wireframe,
40 Shading
41 }
42
43 public class OCCViewer
44 {
45 public event EventHandler ZoomingFinished;
46 protected void RaiseZoomingFinished ()
47 {
48 if (ZoomingFinished != null)
49 {
50 ZoomingFinished (this, EventArgs.Empty);
51 }
52 }
53
54 public event EventHandler AvaliabiltyOfOperationsChanged;
55 protected void RaiseAvaliabiltyOfOperationsChanged ()
56 {
57 if (AvaliabiltyOfOperationsChanged != null)
58 {
59 AvaliabiltyOfOperationsChanged (this, EventArgs.Empty);
60 }
61 }
62
63 public OCCTProxyD3D View { get; private set; }
64 public CurrentAction3d CurrentMode { get; private set; }
65 private bool IsRectVisible { get; set; }
66 public bool DegenerateMode { get; private set; }
67
68 public bool IsWireframeEnabled { get; private set; }
69 public bool IsShadingEnabled { get; private set; }
70 public bool IsTransparencyEnabled { get; private set; }
71 public bool IsColorEnabled { get; private set; }
72 public bool IsMaterialEnabled { get; private set; }
73 public bool IsDeleteEnabled { get; private set; }
74
75 private float myCurZoom;// ~ Quantity_Factor
76 private int myXmin;
77 private int myYmin;
78 private int myXmax;
79 private int myYmax;
80 private int myButtonDownX;
81 private int myButtonDownY;
82 public OCCViewer()
83 {
84 View = new OCCTProxyD3D ();
85 View.InitOCCTProxy ();
86 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
87 IsRectVisible = false;
88 DegenerateMode = true;
89 }
90
91 public bool InitInterop (IntPtr theHWND, IntPtr theD3DDevice)
92 {
93 return View.InitViewer (theHWND, theD3DDevice);
94 }
95
96 public void ImportModel (ModelFormat theFormat)
97 {
98 int aFormat = 10;
99 OpenFileDialog anOpenDialog = new OpenFileDialog ();
100 string aDataDir = ((Environment.GetEnvironmentVariable ("CASROOT")) + "\\..\\data");
101 string aFilter = "";
102
103 switch (theFormat)
104 {
105 case ModelFormat.BREP:
106 anOpenDialog.InitialDirectory = (aDataDir + "\\occ");
107 aFormat = 0;
108 aFilter = "BREP Files (*.brep *.rle)|*.brep; *.rle";
109 break;
110 case ModelFormat.CSFDB:
111 aFormat = 1;
112 aFilter = "CSFDB Files (*.csfdb)|*.csfdb";
113 break;
114 case ModelFormat.STEP:
115 anOpenDialog.InitialDirectory = (aDataDir + "\\step");
116 aFormat = 2;
117 aFilter = "STEP Files (*.stp *.step)|*.stp; *.step";
118 break;
119 case ModelFormat.IGES:
120 anOpenDialog.InitialDirectory = (aDataDir + "\\iges");
121 aFormat = 3;
122 aFilter = "IGES Files (*.igs *.iges)|*.igs; *.iges";
123 break;
124 default:
125 break;
126 }
127
128 anOpenDialog.Filter = aFilter + "|All files (*.*)|*.*";
129 if (anOpenDialog.ShowDialog () == DialogResult.OK)
130 {
131 string aFileName = anOpenDialog.FileName;
132 if (aFileName == "")
133 {
134 return;
135 }
136
137 if (!View.TranslateModel (aFileName, aFormat, true))
138 {
139 MessageBox.Show ("Cann't read this file", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
140 }
141 }
142 View.ZoomAllView ();
143 }
144
145 public void ExportModel (ModelFormat theFormat)
146 {
147 int aFormat = 10;
148 SaveFileDialog saveDialog = new SaveFileDialog ();
149 string aDataDir = ((Environment.GetEnvironmentVariable ("CASROOT")) + "\\..\\data");
150 string aFilter = "";
151
152 switch (theFormat)
153 {
154 case ModelFormat.BREP:
155 saveDialog.InitialDirectory = (aDataDir + "\\occ");
156 aFormat = 0;
157 aFilter = "BREP Files (*.brep *.rle)|*.brep; *.rle";
158 break;
159 case ModelFormat.CSFDB:
160 aFormat = 1;
161 aFilter = "CSFDB Files (*.csfdb)|*.csfdb";
162 break;
163 case ModelFormat.STEP:
164 saveDialog.InitialDirectory = (aDataDir + "\\step");
165 aFormat = 2;
166 aFilter = "STEP Files (*.stp *.step)|*.step; *.stp";
167 break;
168 case ModelFormat.IGES:
169 saveDialog.InitialDirectory = (aDataDir + "\\iges");
170 aFormat = 3;
171 aFilter = "IGES Files (*.igs *.iges)| *.iges; *.igs";
172 break;
173 case ModelFormat.VRML:
174 saveDialog.InitialDirectory = (aDataDir + "\\vrml");
175 aFormat = 4;
176 aFilter = "VRML Files (*.vrml)|*.vrml";
177 break;
178 case ModelFormat.STL:
179 saveDialog.InitialDirectory = (aDataDir + "\\stl");
180 aFormat = 5;
181 aFilter = "STL Files (*.stl)|*.stl";
182 break;
183 case ModelFormat.IMAGE:
184 saveDialog.InitialDirectory = (aDataDir + "\\images");
185 aFormat = 6;
186 aFilter = "Images Files (*.bmp)|*.bmp";
187 break;
188 default:
189 break;
190 }
191
192 saveDialog.Filter = aFilter;
193 if (saveDialog.ShowDialog () == DialogResult.OK)
194 {
195 string aFileName = saveDialog.FileName;
196 if (aFileName == "")
197 {
198 return;
199 }
200
201 if (!View.TranslateModel (aFileName, aFormat, false))
202 {
203 MessageBox.Show ("Can not write this file", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
204 }
205 }
206 }
207
208 public void FitAll ()
209 {
210 View.ZoomAllView ();
211 }
212
213 public void ZoomWindow ()
214 {
215 CurrentMode = CurrentAction3d.CurAction3d_WindowZooming;
216 }
217
218 public void DynamicZooming ()
219 {
220 CurrentMode = CurrentAction3d.CurAction3d_DynamicZooming;
221 }
222
223 public void DynamicPanning ()
224 {
225 CurrentMode = CurrentAction3d.CurAction3d_DynamicPanning;
226 }
227
228 public void GlobalPanning ()
229 {
230 myCurZoom = View.Scale ();
231 CurrentMode = CurrentAction3d.CurAction3d_GlobalPanning;
232 }
233
234 public void AxoView ()
235 {
236 View.AxoView ();
237 }
238
239 public void FrontView ()
240 {
241 View.FrontView ();
242 }
243
244 public void TopView ()
245 {
246 View.TopView ();
247 }
248
249 public void LeftView ()
250 {
251 View.LeftView ();
252 }
253
254 public void BackView ()
255 {
256 View.BackView ();
257 }
258
259 public void RightView ()
260 {
261 View.RightView ();
262 }
263
264 public void Reset ()
265 {
266 View.Reset ();
267 }
268
269 public void BottomView ()
270 {
271 View.BottomView ();
272 }
273
274 public void HiddenOff ()
275 {
276 View.SetDegenerateModeOff ();
277 DegenerateMode = false;
278 }
279
280 public void HiddenOn ()
281 {
282 View.SetDegenerateModeOn ();
283 DegenerateMode = true;
284 }
285
286 public void DynamicRotation ()
287 {
288 CurrentMode = CurrentAction3d.CurAction3d_DynamicRotation;
289 }
290
291 public void SelectionChanged ()
292 {
293 switch (View.DisplayMode ())
294 {
295 case -1:
296 IsShadingEnabled = false;
297 IsWireframeEnabled = false;
298 break;
299 case 0:
300 IsWireframeEnabled = false;
301 IsShadingEnabled = true;
302 IsTransparencyEnabled = false;
303 break;
304 case 1:
305 IsWireframeEnabled = true;
306 IsShadingEnabled = false;
307 IsTransparencyEnabled = true;
308 break;
309 case 10:
310 IsWireframeEnabled = true;
311 IsShadingEnabled = true;
312 IsTransparencyEnabled = true;
313 break;
314 default:
315 break;
316 }
317
318 if (View.IsObjectSelected ())
319 {
320 IsColorEnabled = true;
321 IsMaterialEnabled = true;
322 IsDeleteEnabled = true;
323 }
324 else
325 {
326 IsColorEnabled = false;
327 IsMaterialEnabled = false;
328 IsTransparencyEnabled = false;
329 IsDeleteEnabled = false;
330 }
331
332 RaiseAvaliabiltyOfOperationsChanged ();
333 }
334
335 public void ChangeColor (bool IsObjectColor)
336 {
337 int r, g, b;
338 if (IsObjectColor)
339 {
340 r = View.GetObjColR ();
341 g = View.GetObjColG ();
342 b = View.GetObjColB ();
343 }
344 else
345 {
346 r = View.GetBGColR ();
347 g = View.GetBGColG ();
348 b = View.GetBGColB ();
349 }
350 System.Windows.Forms.ColorDialog ColDlg = new System.Windows.Forms.ColorDialog ();
351 ColDlg.Color = System.Drawing.Color.FromArgb (r, g, b);
352 if (ColDlg.ShowDialog () == System.Windows.Forms.DialogResult.OK)
353 {
354 System.Drawing.Color c = ColDlg.Color;
355 r = c.R;
356 g = c.G;
357 b = c.B;
358 if (IsObjectColor)
359 {
360 View.SetColor (r, g, b);
361 }
362 else
363 {
364 View.SetBackgroundColor (r, g, b);
365 }
366 }
367 View.UpdateCurrentViewer ();
368 }
369
370 public void Wireframe ()
371 {
372 View.SetDisplayMode ((int)DisplayMode.Wireframe);
373 View.UpdateCurrentViewer ();
374
375 SelectionChanged ();
376 RaiseZoomingFinished ();
377 }
378
379 public void Shading ()
380 {
381 View.SetDisplayMode ((int)DisplayMode.Shading);
382 View.UpdateCurrentViewer ();
383
384 SelectionChanged ();
385 RaiseZoomingFinished ();
386 }
387
388 public void Color ()
389 {
390 ChangeColor (true);
391 }
392
393 public void Background ()
394 {
395 ChangeColor (false);
396 }
397
398 public void Material ()
399 {
400 MaterialDlg aDlg = new MaterialDlg (View);
401 aDlg.ShowDialog ();
402 }
403
404 public void Transparency ()
405 {
406 TransparencyDialog dlg = new TransparencyDialog ();
407 dlg.View = View;
408 dlg.ShowDialog ();
409 }
410
411 public void Delete ()
412 {
413 View.EraseObjects ();
414 }
415
416 protected void MultiDragEvent (int x, int y, int theState)
417 {
418 if (theState == -1) //mouse is down
419 {
420 myButtonDownX = x;
421 myButtonDownY = y;
422 }
423 else if (theState == 1) //mouse is up
424 {
425 View.ShiftSelect (Math.Min (myButtonDownX, x), Math.Min (myButtonDownY, y),
426 Math.Max (myButtonDownX, x), Math.Max (myButtonDownY, y));
427 }
428 }
429
430 protected void DragEvent (int x, int y, int theState)
431 {
432 if (theState == -1) //mouse is down
433 {
434 myButtonDownX = x;
435 myButtonDownY = y;
436 }
437 else if (theState == 1) //mouse is up
438 {
439 View.Select (Math.Min (myButtonDownX, x), Math.Min (myButtonDownY, y),
440 Math.Max (myButtonDownX, x), Math.Max (myButtonDownY, y));
441 }
442 }
443
444 public void OnMouseDown (System.Windows.IInputElement sender, MouseButtonEventArgs e)
445 {
446 System.Windows.Controls.TabControl aTabControl = sender as System.Windows.Controls.TabControl;
447 System.Windows.Controls.Grid aGrid = aTabControl.SelectedContent as System.Windows.Controls.Grid;
448
449 Point p = new Point((int)e.GetPosition(aGrid).X, (int)e.GetPosition(aGrid).Y);
450
451 // to avoid the context menu opening
452 aTabControl.ContextMenu.Visibility = System.Windows.Visibility.Collapsed;
453 aTabControl.ContextMenu.IsOpen = false;
454
455 if (e.LeftButton == MouseButtonState.Pressed)
456 {
457 myXmin = p.X;
458 myXmax = p.X;
459 myYmin = p.Y;
460 myYmax = p.Y;
461
462 if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl))
463 {
464 // start the dinamic zooming....
465 CurrentMode = CurrentAction3d.CurAction3d_DynamicZooming;
466 }
467 else
468 {
469 switch (CurrentMode)
470 {
471 case CurrentAction3d.CurAction3d_Nothing:
472 if (Keyboard.IsKeyDown (Key.LeftShift) || Keyboard.IsKeyDown (Key.RightShift))
473 {
474 MultiDragEvent (myXmax, myYmax, -1);
475 }
476 else
477 {
478 DragEvent (myXmax, myYmax, -1);
479 }
480 break;
481 case CurrentAction3d.CurAction3d_DynamicRotation:
482 if (!DegenerateMode)
483 {
484 View.SetDegenerateModeOn ();
485 }
486 View.StartRotation (p.X, p.Y);
487 break;
488 default:
489 break;
490 }
491 }
492 }
493 else if (e.RightButton == MouseButtonState.Pressed)
494 {
495 if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
496 {
497 if (!DegenerateMode)
498 {
499 View.SetDegenerateModeOn();
500 }
501 View.StartRotation(p.X, p.Y);
502 }
503 else
504 {
505 // show context menu only in this case
506 aTabControl.ContextMenu.Visibility = System.Windows.Visibility.Visible;
507 }
508 }
509 }
510
511 public void OnMouseUp(System.Windows.IInputElement sender, MouseButtonEventArgs e)
512 {
513 Point p = new Point((int)e.GetPosition(sender).X, (int)e.GetPosition(sender).Y);
514
515 if (e.ChangedButton == MouseButton.Left)
516 {
517 if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl))
518 {
519 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
520 return;
521 }
522 switch (CurrentMode)
523 {
524 case CurrentAction3d.CurAction3d_Nothing:
525 if (p.X == myXmin && p.Y == myYmin)
526 {
527 myXmax = p.X;
528 myYmax = p.Y;
529 if (Keyboard.IsKeyDown (Key.LeftShift) || Keyboard.IsKeyDown (Key.RightShift))
530 {
531 View.ShiftSelect ();
532 }
533 else
534 {
535 View.Select ();
536 }
537 }
538 else
539 {
540 myXmax = p.X;
541 myYmax = p.Y;
542 if (Keyboard.IsKeyDown (Key.LeftShift) || Keyboard.IsKeyDown (Key.RightShift))
543 {
544 MultiDragEvent (myXmax, myYmax, 1);
545 }
546 else
547 {
548 DragEvent (myXmax, myYmax, 1);
549 }
550 }
551 break;
552 case CurrentAction3d.CurAction3d_DynamicZooming:
553 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
554 break;
555 case CurrentAction3d.CurAction3d_WindowZooming:
556 myXmax = p.X;
557 myYmax = p.Y;
558 int ValZWMin = 1;
559 if (Math.Abs (myXmax - myXmin) > ValZWMin &&
560 Math.Abs (myXmax - myYmax) > ValZWMin)
561 {
562 View.WindowFitAll (myXmin, myYmin, myXmax, myYmax);
563 }
564 RaiseZoomingFinished ();
565 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
566 break;
567 case CurrentAction3d.CurAction3d_DynamicPanning:
568 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
569 break;
570 case CurrentAction3d.CurAction3d_GlobalPanning:
571 View.Place (p.X, p.Y, myCurZoom);
572 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
573 break;
574 case CurrentAction3d.CurAction3d_DynamicRotation:
575 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
576 if (!DegenerateMode)
577 {
578 View.SetDegenerateModeOff ();
579 }
580 else
581 {
582 View.SetDegenerateModeOn ();
583 }
584 break;
585 default:
586 break;
587 }
588 }
589 else if (e.ChangedButton == MouseButton.Right)
590 {
591 if (!DegenerateMode)
592 {
593 View.SetDegenerateModeOff ();
594 }
595 else
596 {
597 View.SetDegenerateModeOn ();
598 }
599 }
600
601 SelectionChanged ();
602 }
603
604 public void OnMouseMove (System.Windows.IInputElement sender, System.Windows.Input.MouseEventArgs e)
605 {
606 Point p = new Point ((int)e.GetPosition (sender).X, (int)e.GetPosition (sender).Y);
607
608 if (e.LeftButton == MouseButtonState.Pressed) //left button is pressed
609 {
610 if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl))
611 {
612 View.Zoom (myXmax, myYmax, p.X, p.Y);
613 myXmax = p.X;
614 myYmax = p.Y;
615 }
616 else
617 {
618 switch (CurrentMode)
619 {
620 case CurrentAction3d.CurAction3d_Nothing:
621 myXmax = p.X;
622 myYmax = p.Y;
623 break;
624 case CurrentAction3d.CurAction3d_DynamicZooming:
625 View.Zoom (myXmax, myYmax, p.X, p.Y);
626 myXmax = p.X;
627 myYmax = p.Y;
628 break;
629 case CurrentAction3d.CurAction3d_WindowZooming:
630 myXmax = p.X;
631 myYmax = p.Y;
632 break;
633 case CurrentAction3d.CurAction3d_DynamicPanning:
634 View.Pan (p.X - myXmax, myYmax - p.Y);
635 myXmax = p.X;
636 myYmax = p.Y;
637 break;
638 case CurrentAction3d.CurAction3d_GlobalPanning:
639 break;
640 case CurrentAction3d.CurAction3d_DynamicRotation:
641 View.Rotation (p.X, p.Y);
642 View.RedrawView ();
643 break;
644 default:
645 break;
646 }
647 }
648 }
649 else if (e.MiddleButton == MouseButtonState.Pressed) //middle button is pressed
650 {
651 if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl))
652 {
653 View.Pan (p.X - myXmax, myYmax - p.Y);
654 myXmax = p.X;
655 myYmax = p.Y;
656 }
657 }
658 else if (e.RightButton == MouseButtonState.Pressed) //right button is pressed
659 {
660 if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl))
661 {
662 View.Rotation (p.X, p.Y);
663 }
664 }
665 else // no buttons are pressed
666 {
667 myXmax = p.X;
668 myYmax = p.Y;
669 View.MoveTo (p.X, p.Y);
670 }
671 }
672 }
673}