2 using System.Collections.Generic;
5 using System.Windows.Forms;
6 using System.Windows.Input;
11 public enum CurrentAction3d
14 CurAction3d_DynamicZooming,
15 CurAction3d_WindowZooming,
16 CurAction3d_DynamicPanning,
17 CurAction3d_GlobalPanning,
18 CurAction3d_DynamicRotation
20 public enum CurrentPressedKey
22 CurPressedKey_Nothing,
26 public enum ModelFormat
36 public enum DisplayMode
42 public class OCCViewer
44 public event EventHandler ZoomingFinished;
45 protected void RaiseZoomingFinished ()
47 if (ZoomingFinished != null)
49 ZoomingFinished (this, EventArgs.Empty);
53 public event EventHandler AvaliabiltyOfOperationsChanged;
54 protected void RaiseAvaliabiltyOfOperationsChanged ()
56 if (AvaliabiltyOfOperationsChanged != null)
58 AvaliabiltyOfOperationsChanged (this, EventArgs.Empty);
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; }
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; }
74 private float myCurZoom;// ~ Quantity_Factor
79 private int myButtonDownX;
80 private int myButtonDownY;
83 View = new OCCTProxyD3D ();
84 View.InitOCCTProxy ();
85 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
86 IsRectVisible = false;
87 DegenerateMode = true;
90 public bool InitViewer()
92 return View.InitViewer();
95 public void ImportModel (ModelFormat theFormat)
98 OpenFileDialog anOpenDialog = new OpenFileDialog ();
99 string aDataDir = Environment.GetEnvironmentVariable ("CSF_OCCTDataPath");
104 case ModelFormat.BREP:
105 anOpenDialog.InitialDirectory = (aDataDir + "\\occ");
107 aFilter = "BREP Files (*.brep *.rle)|*.brep; *.rle";
109 case ModelFormat.STEP:
110 anOpenDialog.InitialDirectory = (aDataDir + "\\step");
112 aFilter = "STEP Files (*.stp *.step)|*.stp; *.step";
114 case ModelFormat.IGES:
115 anOpenDialog.InitialDirectory = (aDataDir + "\\iges");
117 aFilter = "IGES Files (*.igs *.iges)|*.igs; *.iges";
123 anOpenDialog.Filter = aFilter + "|All files (*.*)|*.*";
124 if (anOpenDialog.ShowDialog () == DialogResult.OK)
126 string aFileName = anOpenDialog.FileName;
132 if (!View.TranslateModel (aFileName, aFormat, true))
134 MessageBox.Show ("Cann't read this file", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
140 public void ExportModel (ModelFormat theFormat)
143 SaveFileDialog saveDialog = new SaveFileDialog ();
144 string aDataDir = Environment.GetEnvironmentVariable ("CSF_OCCTDataPath");
149 case ModelFormat.BREP:
150 saveDialog.InitialDirectory = (aDataDir + "\\occ");
152 aFilter = "BREP Files (*.brep *.rle)|*.brep; *.rle";
154 case ModelFormat.STEP:
155 saveDialog.InitialDirectory = (aDataDir + "\\step");
157 aFilter = "STEP Files (*.stp *.step)|*.step; *.stp";
159 case ModelFormat.IGES:
160 saveDialog.InitialDirectory = (aDataDir + "\\iges");
162 aFilter = "IGES Files (*.igs *.iges)| *.iges; *.igs";
164 case ModelFormat.VRML:
165 saveDialog.InitialDirectory = (aDataDir + "\\vrml");
167 aFilter = "VRML Files (*.vrml)|*.vrml";
169 case ModelFormat.STL:
170 saveDialog.InitialDirectory = (aDataDir + "\\stl");
172 aFilter = "STL Files (*.stl)|*.stl";
174 case ModelFormat.IMAGE:
175 saveDialog.InitialDirectory = (aDataDir + "\\images");
177 aFilter = "Images Files (*.bmp)|*.bmp";
183 saveDialog.Filter = aFilter;
184 if (saveDialog.ShowDialog () == DialogResult.OK)
186 string aFileName = saveDialog.FileName;
192 if (!View.TranslateModel (aFileName, aFormat, false))
194 MessageBox.Show ("Can not write this file", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
199 public void FitAll ()
204 public void ZoomWindow ()
206 CurrentMode = CurrentAction3d.CurAction3d_WindowZooming;
209 public void DynamicZooming ()
211 CurrentMode = CurrentAction3d.CurAction3d_DynamicZooming;
214 public void DynamicPanning ()
216 CurrentMode = CurrentAction3d.CurAction3d_DynamicPanning;
219 public void GlobalPanning ()
221 myCurZoom = View.Scale ();
222 CurrentMode = CurrentAction3d.CurAction3d_GlobalPanning;
225 public void AxoView ()
230 public void FrontView ()
235 public void TopView ()
240 public void LeftView ()
245 public void BackView ()
250 public void RightView ()
260 public void BottomView ()
265 public void HiddenOff ()
267 View.SetDegenerateModeOff ();
268 DegenerateMode = false;
271 public void HiddenOn ()
273 View.SetDegenerateModeOn ();
274 DegenerateMode = true;
277 public void DynamicRotation ()
279 CurrentMode = CurrentAction3d.CurAction3d_DynamicRotation;
282 public void SelectionChanged ()
284 switch (View.DisplayMode ())
287 IsShadingEnabled = false;
288 IsWireframeEnabled = false;
291 IsWireframeEnabled = false;
292 IsShadingEnabled = true;
293 IsTransparencyEnabled = false;
296 IsWireframeEnabled = true;
297 IsShadingEnabled = false;
298 IsTransparencyEnabled = true;
301 IsWireframeEnabled = true;
302 IsShadingEnabled = true;
303 IsTransparencyEnabled = true;
309 if (View.IsObjectSelected ())
311 IsColorEnabled = true;
312 IsMaterialEnabled = true;
313 IsDeleteEnabled = true;
317 IsColorEnabled = false;
318 IsMaterialEnabled = false;
319 IsTransparencyEnabled = false;
320 IsDeleteEnabled = false;
323 RaiseAvaliabiltyOfOperationsChanged ();
326 public void ChangeColor (bool IsObjectColor)
331 r = View.GetObjColR ();
332 g = View.GetObjColG ();
333 b = View.GetObjColB ();
337 r = View.GetBGColR ();
338 g = View.GetBGColG ();
339 b = View.GetBGColB ();
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)
345 System.Drawing.Color c = ColDlg.Color;
351 View.SetColor (r, g, b);
355 View.SetBackgroundColor (r, g, b);
358 View.UpdateCurrentViewer ();
361 public void Wireframe ()
363 View.SetDisplayMode ((int)DisplayMode.Wireframe);
364 View.UpdateCurrentViewer ();
367 RaiseZoomingFinished ();
370 public void Shading ()
372 View.SetDisplayMode ((int)DisplayMode.Shading);
373 View.UpdateCurrentViewer ();
376 RaiseZoomingFinished ();
384 public void Background ()
389 public void Material ()
391 MaterialDlg aDlg = new MaterialDlg (View);
395 public void Transparency ()
397 TransparencyDialog dlg = new TransparencyDialog ();
402 public void Delete ()
404 View.EraseObjects ();
408 protected void MultiDragEvent (int x, int y, int theState)
410 if (theState == -1) //mouse is down
415 else if (theState == 1) //mouse is up
417 View.ShiftSelect (Math.Min (myButtonDownX, x), Math.Min (myButtonDownY, y),
418 Math.Max (myButtonDownX, x), Math.Max (myButtonDownY, y));
422 protected void DragEvent (int x, int y, int theState)
424 if (theState == -1) //mouse is down
429 else if (theState == 1) //mouse is up
431 View.Select (Math.Min (myButtonDownX, x), Math.Min (myButtonDownY, y),
432 Math.Max (myButtonDownX, x), Math.Max (myButtonDownY, y));
436 public void OnMouseDown (System.Windows.IInputElement sender, MouseButtonEventArgs e)
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;
441 Point p = new Point((int)e.GetPosition(aGrid).X, (int)e.GetPosition(aGrid).Y);
443 // to avoid the context menu opening
444 aTabControl.ContextMenu.Visibility = System.Windows.Visibility.Collapsed;
445 aTabControl.ContextMenu.IsOpen = false;
447 if (e.LeftButton == MouseButtonState.Pressed)
454 if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl))
456 // start the dinamic zooming....
457 CurrentMode = CurrentAction3d.CurAction3d_DynamicZooming;
463 case CurrentAction3d.CurAction3d_Nothing:
464 if (Keyboard.IsKeyDown (Key.LeftShift) || Keyboard.IsKeyDown (Key.RightShift))
466 MultiDragEvent (myXmax, myYmax, -1);
470 DragEvent (myXmax, myYmax, -1);
473 case CurrentAction3d.CurAction3d_DynamicRotation:
476 View.SetDegenerateModeOn ();
478 View.StartRotation (p.X, p.Y);
485 else if (e.RightButton == MouseButtonState.Pressed)
487 if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
491 View.SetDegenerateModeOn();
493 View.StartRotation(p.X, p.Y);
497 // show context menu only in this case
498 aTabControl.ContextMenu.Visibility = System.Windows.Visibility.Visible;
503 public void OnMouseUp(System.Windows.IInputElement sender, MouseButtonEventArgs e)
505 Point p = new Point((int)e.GetPosition(sender).X, (int)e.GetPosition(sender).Y);
507 if (e.ChangedButton == MouseButton.Left)
509 if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl))
511 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
516 case CurrentAction3d.CurAction3d_Nothing:
517 if (p.X == myXmin && p.Y == myYmin)
521 if (Keyboard.IsKeyDown (Key.LeftShift) || Keyboard.IsKeyDown (Key.RightShift))
534 if (Keyboard.IsKeyDown (Key.LeftShift) || Keyboard.IsKeyDown (Key.RightShift))
536 MultiDragEvent (myXmax, myYmax, 1);
540 DragEvent (myXmax, myYmax, 1);
544 case CurrentAction3d.CurAction3d_DynamicZooming:
545 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
547 case CurrentAction3d.CurAction3d_WindowZooming:
551 if (Math.Abs (myXmax - myXmin) > ValZWMin &&
552 Math.Abs (myXmax - myYmax) > ValZWMin)
554 View.WindowFitAll (myXmin, myYmin, myXmax, myYmax);
556 RaiseZoomingFinished ();
557 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
559 case CurrentAction3d.CurAction3d_DynamicPanning:
560 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
562 case CurrentAction3d.CurAction3d_GlobalPanning:
563 View.Place (p.X, p.Y, myCurZoom);
564 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
566 case CurrentAction3d.CurAction3d_DynamicRotation:
567 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
570 View.SetDegenerateModeOff ();
574 View.SetDegenerateModeOn ();
581 else if (e.ChangedButton == MouseButton.Right)
585 View.SetDegenerateModeOff ();
589 View.SetDegenerateModeOn ();
596 public void OnMouseMove (System.Windows.IInputElement sender, System.Windows.Input.MouseEventArgs e)
598 Point p = new Point ((int)e.GetPosition (sender).X, (int)e.GetPosition (sender).Y);
600 if (e.LeftButton == MouseButtonState.Pressed) //left button is pressed
602 if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl))
604 View.Zoom (myXmax, myYmax, p.X, p.Y);
612 case CurrentAction3d.CurAction3d_Nothing:
616 case CurrentAction3d.CurAction3d_DynamicZooming:
617 View.Zoom (myXmax, myYmax, p.X, p.Y);
621 case CurrentAction3d.CurAction3d_WindowZooming:
625 case CurrentAction3d.CurAction3d_DynamicPanning:
626 View.Pan (p.X - myXmax, myYmax - p.Y);
630 case CurrentAction3d.CurAction3d_GlobalPanning:
632 case CurrentAction3d.CurAction3d_DynamicRotation:
633 View.Rotation (p.X, p.Y);
641 else if (e.MiddleButton == MouseButtonState.Pressed) //middle button is pressed
643 if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl))
645 View.Pan (p.X - myXmax, myYmax - p.Y);
650 else if (e.RightButton == MouseButtonState.Pressed) //right button is pressed
652 if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl))
654 View.Rotation (p.X, p.Y);
657 else // no buttons are pressed
661 View.MoveTo (p.X, p.Y);