0028782: Shape sewing behavior not consistent for the same CAD file
[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 ();
4b3541c6 99 string aDataDir = Environment.GetEnvironmentVariable ("CSF_OCCTDataPath");
15534713 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 ();
4b3541c6 144 string aDataDir = Environment.GetEnvironmentVariable ("CSF_OCCTDataPath");
15534713 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 ();
4d6554d1 405 SelectionChanged ();
15534713 406 }
407
408 protected void MultiDragEvent (int x, int y, int theState)
409 {
410 if (theState == -1) //mouse is down
411 {
412 myButtonDownX = x;
413 myButtonDownY = y;
414 }
415 else if (theState == 1) //mouse is up
416 {
417 View.ShiftSelect (Math.Min (myButtonDownX, x), Math.Min (myButtonDownY, y),
418 Math.Max (myButtonDownX, x), Math.Max (myButtonDownY, y));
419 }
420 }
421
422 protected void DragEvent (int x, int y, int theState)
423 {
424 if (theState == -1) //mouse is down
425 {
426 myButtonDownX = x;
427 myButtonDownY = y;
428 }
429 else if (theState == 1) //mouse is up
430 {
431 View.Select (Math.Min (myButtonDownX, x), Math.Min (myButtonDownY, y),
432 Math.Max (myButtonDownX, x), Math.Max (myButtonDownY, y));
433 }
434 }
435
436 public void OnMouseDown (System.Windows.IInputElement sender, MouseButtonEventArgs e)
437 {
438 System.Windows.Controls.TabControl aTabControl = sender as System.Windows.Controls.TabControl;
439 System.Windows.Controls.Grid aGrid = aTabControl.SelectedContent as System.Windows.Controls.Grid;
440
441 Point p = new Point((int)e.GetPosition(aGrid).X, (int)e.GetPosition(aGrid).Y);
442
443 // to avoid the context menu opening
444 aTabControl.ContextMenu.Visibility = System.Windows.Visibility.Collapsed;
445 aTabControl.ContextMenu.IsOpen = false;
446
447 if (e.LeftButton == MouseButtonState.Pressed)
448 {
449 myXmin = p.X;
450 myXmax = p.X;
451 myYmin = p.Y;
452 myYmax = p.Y;
453
454 if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl))
455 {
456 // start the dinamic zooming....
457 CurrentMode = CurrentAction3d.CurAction3d_DynamicZooming;
458 }
459 else
460 {
461 switch (CurrentMode)
462 {
463 case CurrentAction3d.CurAction3d_Nothing:
464 if (Keyboard.IsKeyDown (Key.LeftShift) || Keyboard.IsKeyDown (Key.RightShift))
465 {
466 MultiDragEvent (myXmax, myYmax, -1);
467 }
468 else
469 {
470 DragEvent (myXmax, myYmax, -1);
471 }
472 break;
473 case CurrentAction3d.CurAction3d_DynamicRotation:
474 if (!DegenerateMode)
475 {
476 View.SetDegenerateModeOn ();
477 }
478 View.StartRotation (p.X, p.Y);
479 break;
480 default:
481 break;
482 }
483 }
484 }
485 else if (e.RightButton == MouseButtonState.Pressed)
486 {
487 if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
488 {
489 if (!DegenerateMode)
490 {
491 View.SetDegenerateModeOn();
492 }
493 View.StartRotation(p.X, p.Y);
494 }
495 else
496 {
497 // show context menu only in this case
498 aTabControl.ContextMenu.Visibility = System.Windows.Visibility.Visible;
499 }
500 }
501 }
502
503 public void OnMouseUp(System.Windows.IInputElement sender, MouseButtonEventArgs e)
504 {
505 Point p = new Point((int)e.GetPosition(sender).X, (int)e.GetPosition(sender).Y);
506
507 if (e.ChangedButton == MouseButton.Left)
508 {
509 if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl))
510 {
511 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
512 return;
513 }
514 switch (CurrentMode)
515 {
516 case CurrentAction3d.CurAction3d_Nothing:
517 if (p.X == myXmin && p.Y == myYmin)
518 {
519 myXmax = p.X;
520 myYmax = p.Y;
521 if (Keyboard.IsKeyDown (Key.LeftShift) || Keyboard.IsKeyDown (Key.RightShift))
522 {
523 View.ShiftSelect ();
524 }
525 else
526 {
527 View.Select ();
528 }
529 }
530 else
531 {
532 myXmax = p.X;
533 myYmax = p.Y;
534 if (Keyboard.IsKeyDown (Key.LeftShift) || Keyboard.IsKeyDown (Key.RightShift))
535 {
536 MultiDragEvent (myXmax, myYmax, 1);
537 }
538 else
539 {
540 DragEvent (myXmax, myYmax, 1);
541 }
542 }
543 break;
544 case CurrentAction3d.CurAction3d_DynamicZooming:
545 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
546 break;
547 case CurrentAction3d.CurAction3d_WindowZooming:
548 myXmax = p.X;
549 myYmax = p.Y;
550 int ValZWMin = 1;
551 if (Math.Abs (myXmax - myXmin) > ValZWMin &&
552 Math.Abs (myXmax - myYmax) > ValZWMin)
553 {
554 View.WindowFitAll (myXmin, myYmin, myXmax, myYmax);
555 }
556 RaiseZoomingFinished ();
557 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
558 break;
559 case CurrentAction3d.CurAction3d_DynamicPanning:
560 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
561 break;
562 case CurrentAction3d.CurAction3d_GlobalPanning:
563 View.Place (p.X, p.Y, myCurZoom);
564 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
565 break;
566 case CurrentAction3d.CurAction3d_DynamicRotation:
567 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
568 if (!DegenerateMode)
569 {
570 View.SetDegenerateModeOff ();
571 }
572 else
573 {
574 View.SetDegenerateModeOn ();
575 }
576 break;
577 default:
578 break;
579 }
580 }
581 else if (e.ChangedButton == MouseButton.Right)
582 {
583 if (!DegenerateMode)
584 {
585 View.SetDegenerateModeOff ();
586 }
587 else
588 {
589 View.SetDegenerateModeOn ();
590 }
591 }
592
593 SelectionChanged ();
594 }
595
596 public void OnMouseMove (System.Windows.IInputElement sender, System.Windows.Input.MouseEventArgs e)
597 {
598 Point p = new Point ((int)e.GetPosition (sender).X, (int)e.GetPosition (sender).Y);
599
600 if (e.LeftButton == MouseButtonState.Pressed) //left button is pressed
601 {
602 if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl))
603 {
604 View.Zoom (myXmax, myYmax, p.X, p.Y);
605 myXmax = p.X;
606 myYmax = p.Y;
607 }
608 else
609 {
610 switch (CurrentMode)
611 {
612 case CurrentAction3d.CurAction3d_Nothing:
613 myXmax = p.X;
614 myYmax = p.Y;
615 break;
616 case CurrentAction3d.CurAction3d_DynamicZooming:
617 View.Zoom (myXmax, myYmax, p.X, p.Y);
618 myXmax = p.X;
619 myYmax = p.Y;
620 break;
621 case CurrentAction3d.CurAction3d_WindowZooming:
622 myXmax = p.X;
623 myYmax = p.Y;
624 break;
625 case CurrentAction3d.CurAction3d_DynamicPanning:
626 View.Pan (p.X - myXmax, myYmax - p.Y);
627 myXmax = p.X;
628 myYmax = p.Y;
629 break;
630 case CurrentAction3d.CurAction3d_GlobalPanning:
631 break;
632 case CurrentAction3d.CurAction3d_DynamicRotation:
633 View.Rotation (p.X, p.Y);
634 View.RedrawView ();
635 break;
636 default:
637 break;
638 }
639 }
640 }
641 else if (e.MiddleButton == MouseButtonState.Pressed) //middle button is pressed
642 {
643 if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl))
644 {
645 View.Pan (p.X - myXmax, myYmax - p.Y);
646 myXmax = p.X;
647 myYmax = p.Y;
648 }
649 }
650 else if (e.RightButton == MouseButtonState.Pressed) //right button is pressed
651 {
652 if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl))
653 {
654 View.Rotation (p.X, p.Y);
655 }
656 }
657 else // no buttons are pressed
658 {
659 myXmax = p.X;
660 myYmax = p.Y;
661 View.MoveTo (p.X, p.Y);
662 }
663 }
664 }
665}