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