2 using System.Collections.Generic;
5 using System.Windows.Forms;
7 namespace IE_WPF_WinForms
9 public enum CurrentAction3d
12 CurAction3d_DynamicZooming,
13 CurAction3d_WindowZooming,
14 CurAction3d_DynamicPanning,
15 CurAction3d_GlobalPanning,
16 CurAction3d_DynamicRotation
18 public enum CurrentPressedKey
20 CurPressedKey_Nothing,
24 public enum ModelFormat
35 public enum DisplayMode
41 public class OCCViewer : System.Windows.Forms.Form
43 public event EventHandler ZoomingFinished;
44 protected void RaiseZoomingFinished()
46 if ( ZoomingFinished != null )
48 ZoomingFinished( this, EventArgs.Empty );
52 public event EventHandler AvaliabiltyOfOperationsChanged;
53 protected void RaiseAvaliabiltyOfOperationsChanged()
55 if ( AvaliabiltyOfOperationsChanged != null )
57 AvaliabiltyOfOperationsChanged( this, EventArgs.Empty );
61 public OCCTProxy View { get; private set; }
62 public CurrentAction3d CurrentMode { get; private set; }
63 private CurrentPressedKey CurrentPressedKey { get; 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 myRectDownX;
80 private int myRectDownY;
81 private int myButtonDownX;
82 private int myButtonDownY;
84 private ContextMenu Popup { get; set; }
85 private MenuItem ContextWireframe;
86 private MenuItem ContextShading;
87 private MenuItem ContextColor;
88 private MenuItem ContextMaterial;
89 private MenuItem ContextDelete;
90 private MenuItem ContextBackground;
91 private MenuItem ContextTransparency;
96 InitializeComponent();
98 View = new OCCTProxy();
100 if ( !View.InitViewer( this.Handle ) )
102 MessageBox.Show( "Fatal Error during the graphic initialisation", "Error!" );
105 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
106 CurrentPressedKey = CurrentPressedKey.CurPressedKey_Nothing;
107 IsRectVisible = false;
108 DegenerateMode = true;
111 private void InitializeComponent()
116 this.ImeMode = System.Windows.Forms.ImeMode.NoControl;
118 SizeChanged += new System.EventHandler( OnSizeChanged );
119 Paint += new System.Windows.Forms.PaintEventHandler( OnPaint );
121 MouseDown += new System.Windows.Forms.MouseEventHandler( OnMouseDown );
122 MouseUp += new System.Windows.Forms.MouseEventHandler( OnMouseUp );
123 MouseMove += new System.Windows.Forms.MouseEventHandler( OnMouseMove );
125 Popup = new ContextMenu();
126 ContextWireframe = new MenuItem();
127 ContextShading = new MenuItem();
128 ContextColor = new MenuItem();
129 ContextMaterial = new MenuItem();
130 ContextTransparency = new MenuItem();
131 ContextDelete = new MenuItem();
132 ContextBackground = new MenuItem();
134 ContextWireframe.Text = "Wireframe";
135 ContextShading.Text = "Shading";
136 ContextColor.Text = "Color";
137 ContextMaterial.Text = "Material";
138 ContextTransparency.Text = "Transparency";
139 ContextDelete.Text = "Delete";
140 ContextBackground.Text = "Background";
142 ContextWireframe.Click += new System.EventHandler( ContextWireframe_Click );
143 ContextShading.Click += new System.EventHandler( ContextShading_Click );
144 ContextColor.Click += new System.EventHandler( ContextColor_Click );
145 ContextMaterial.Click += new System.EventHandler( ContextMaterial_Click );
146 ContextTransparency.Click += new System.EventHandler( ContextTransparency_Click );
147 ContextDelete.Click += new System.EventHandler( ContextDelete_Click );
148 ContextBackground.Click += new System.EventHandler( ContextBackground_Click );
150 Popup.MenuItems.AddRange( new MenuItem[] { ContextWireframe,
156 ContextBackground } );
157 Popup.Popup += new System.EventHandler( OnPopup );
160 private void OnPaint(object sender, System.Windows.Forms.PaintEventArgs e)
166 private void OnSizeChanged(object sender, System.EventArgs e)
171 public void ImportModel( ModelFormat theFormat )
174 OpenFileDialog anOpenDialog = new OpenFileDialog();
175 string aDataDir = ( (Environment.GetEnvironmentVariable("CASROOT")) + "\\..\\data" );
180 case ModelFormat.BREP:
181 anOpenDialog.InitialDirectory = (aDataDir + "\\occ");
183 aFilter = "BREP Files (*.brep *.rle)|*.brep; *.rle";
185 case ModelFormat.CSFDB:
187 aFilter = "CSFDB Files (*.csfdb)|*.csfdb";
189 case ModelFormat.STEP:
190 anOpenDialog.InitialDirectory = (aDataDir + "\\step");
192 aFilter = "STEP Files (*.stp *.step)|*.stp; *.step";
194 case ModelFormat.IGES:
195 anOpenDialog.InitialDirectory = (aDataDir + "\\iges");
197 aFilter = "IGES Files (*.igs *.iges)|*.igs; *.iges";
203 anOpenDialog.Filter = aFilter + "|All files (*.*)|*.*";
204 if (anOpenDialog.ShowDialog() == DialogResult.OK)
206 string aFileName = anOpenDialog.FileName;
212 Cursor = System.Windows.Forms.Cursors.WaitCursor;
213 if ( !View.TranslateModel( aFileName, aFormat, true ) )
215 MessageBox.Show( "Cann't read this file", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning );
217 Cursor = System.Windows.Forms.Cursors.Default;
222 public void ExportModel( ModelFormat theFormat )
225 SaveFileDialog saveDialog = new SaveFileDialog();
226 string aDataDir = ( (Environment.GetEnvironmentVariable("CASROOT") ) + "\\..\\data" );
231 case ModelFormat.BREP:
232 saveDialog.InitialDirectory = ( aDataDir + "\\occ" );
234 aFilter = "BREP Files (*.brep *.rle)|*.brep; *.rle";
236 case ModelFormat.CSFDB:
238 aFilter = "CSFDB Files (*.csfdb)|*.csfdb";
240 case ModelFormat.STEP:
241 saveDialog.InitialDirectory = ( aDataDir + "\\step" );
243 aFilter = "STEP Files (*.stp *.step)|*.step; *.stp";
245 case ModelFormat.IGES:
246 saveDialog.InitialDirectory = ( aDataDir + "\\iges" );
248 aFilter = "IGES Files (*.igs *.iges)| *.iges; *.igs";
250 case ModelFormat.VRML:
251 saveDialog.InitialDirectory = ( aDataDir + "\\vrml" );
253 aFilter = "VRML Files (*.vrml)|*.vrml";
255 case ModelFormat.STL:
256 saveDialog.InitialDirectory = ( aDataDir + "\\stl" );
258 aFilter = "STL Files (*.stl)|*.stl";
260 case ModelFormat.IMAGE:
261 saveDialog.InitialDirectory = ( aDataDir + "\\images" );
263 aFilter = "Images Files (*.bmp)|*.bmp";
269 saveDialog.Filter = aFilter;
270 if ( saveDialog.ShowDialog() == DialogResult.OK )
272 string aFileName = saveDialog.FileName;
273 if ( aFileName == "" )
278 Cursor = System.Windows.Forms.Cursors.WaitCursor;
279 if ( !View.TranslateModel( aFileName, aFormat, false ) )
281 MessageBox.Show( "Can not write this file", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning );
283 Cursor = System.Windows.Forms.Cursors.Default;
292 public void ZoomWindow()
294 CurrentMode = CurrentAction3d.CurAction3d_WindowZooming;
297 public void DynamicZooming()
299 CurrentMode = CurrentAction3d.CurAction3d_DynamicZooming;
302 public void DynamicPanning()
304 CurrentMode = CurrentAction3d.CurAction3d_DynamicPanning;
307 public void GlobalPanning()
309 myCurZoom = View.Scale();
310 CurrentMode = CurrentAction3d.CurAction3d_GlobalPanning;
313 public void AxoView()
318 public void FrontView()
323 public void TopView()
328 public void LeftView()
333 public void BackView()
338 public void RightView()
348 public void BottomView()
353 public void HiddenOff()
355 View.SetDegenerateModeOff();
356 DegenerateMode = false;
359 public void HiddenOn()
361 View.SetDegenerateModeOn();
362 DegenerateMode = true;
365 public void DynamicRotation()
367 CurrentMode = CurrentAction3d.CurAction3d_DynamicRotation;
370 public void SelectionChanged()
372 switch ( View.DisplayMode() )
375 IsShadingEnabled = false;
376 IsWireframeEnabled = false;
379 IsWireframeEnabled = false;
380 IsShadingEnabled = true;
381 IsTransparencyEnabled = false;
384 IsWireframeEnabled = true;
385 IsShadingEnabled = false;
386 IsTransparencyEnabled = true;
389 IsWireframeEnabled = true;
390 IsShadingEnabled = true;
391 IsTransparencyEnabled = true;
397 if ( View.IsObjectSelected() )
399 IsColorEnabled = true;
400 IsMaterialEnabled = true;
401 IsDeleteEnabled = true;
405 IsColorEnabled = false;
406 IsMaterialEnabled = false;
407 IsTransparencyEnabled = false;
408 IsDeleteEnabled = false;
411 RaiseAvaliabiltyOfOperationsChanged();
414 public void ChangeColor( bool IsObjectColor )
419 r = View.GetObjColR();
420 g = View.GetObjColG();
421 b = View.GetObjColB();
425 r = View.GetBGColR();
426 g = View.GetBGColG();
427 b = View.GetBGColB();
429 System.Windows.Forms.ColorDialog ColDlg = new System.Windows.Forms.ColorDialog();
430 ColDlg.Color = System.Drawing.Color.FromArgb( r, g, b );
431 if ( ColDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK )
433 System.Drawing.Color c = ColDlg.Color;
439 View.SetColor( r, g, b );
443 View.SetBackgroundColor( r, g, b );
446 View.UpdateCurrentViewer();
449 public void Wireframe()
451 View.SetDisplayMode( (int)DisplayMode.Wireframe );
452 View.UpdateCurrentViewer();
455 RaiseZoomingFinished();
458 public void Shading()
460 View.SetDisplayMode( (int)DisplayMode.Shading );
461 View.UpdateCurrentViewer();
464 RaiseZoomingFinished();
472 public void Background()
474 ChangeColor( false );
477 public void Material()
479 MaterialDlg aDlg = new MaterialDlg( View );
483 public void Transparency()
485 TransparencyDialog dlg = new TransparencyDialog();
487 dlg.ShowDialog( this );
495 public void OnKeyDown( System.Windows.Input.Key theKey )
497 if ( theKey == System.Windows.Input.Key.LeftShift ||
498 theKey == System.Windows.Input.Key.RightShift )
500 CurrentPressedKey = CurrentPressedKey.CurPressedKey_Shift;
502 else if (theKey == System.Windows.Input.Key.LeftCtrl ||
503 theKey == System.Windows.Input.Key.RightCtrl )
505 CurrentPressedKey = CurrentPressedKey.CurPressedKey_Ctrl;
509 public void OnKeyUp()
511 CurrentPressedKey = CurrentPressedKey.CurPressedKey_Nothing;
514 protected void MultiDragEvent( int x, int y, int theState )
516 if ( theState == -1 ) //mouse is down
521 else if ( theState == 1) //mouse is up
523 View.ShiftSelect( Math.Min( myButtonDownX, x ), Math.Min( myButtonDownY, y ),
524 Math.Max( myButtonDownX, x ), Math.Max( myButtonDownY, y ) );
528 protected void DragEvent( int x, int y, int theState )
530 if ( theState == -1 ) //mouse is down
535 else if ( theState == 1 ) //mouse is up
537 View.Select( Math.Min( myButtonDownX, x ), Math.Min( myButtonDownY, y ),
538 Math.Max( myButtonDownX, x ), Math.Max( myButtonDownY, y ) );
542 private void DrawRectangle( bool draw )
544 System.Drawing.Graphics gr = System.Drawing.Graphics.FromHwnd(Handle);
545 System.Drawing.Pen p = null;
546 if ( IsRectVisible || !draw )//erase the rect
548 int r = View.GetBGColR();
549 int g = View.GetBGColG();
550 int b = View.GetBGColB();
551 p = new System.Drawing.Pen( System.Drawing.Color.FromArgb(r, g, b) );
552 IsRectVisible = false;
557 p = new System.Drawing.Pen( System.Drawing.Color.White );
558 IsRectVisible = true;
564 int x = Math.Min( myXmin, myXmax );
565 int y = Math.Min( myYmin, myYmax );
566 gr.DrawRectangle( p, x, y, Math.Abs(myXmax - myXmin), Math.Abs(myYmax - myYmin) );
567 myRectDownX = Math.Max( myXmin, myXmax );
568 myRectDownY = Math.Max( myYmin, myYmax );
571 private void OnMouseDown( object sender, System.Windows.Forms.MouseEventArgs e )
573 if ( e.Button == MouseButtons.Left )
579 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl )
581 // start the dinamic zooming....
582 CurrentMode = CurrentAction3d.CurAction3d_DynamicZooming;
586 switch ( CurrentMode )
588 case CurrentAction3d.CurAction3d_Nothing:
589 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Shift )
591 MultiDragEvent( myXmax, myYmax, -1 );
595 DragEvent( myXmax, myYmax, -1 );
598 case CurrentAction3d.CurAction3d_DynamicRotation:
599 if ( !DegenerateMode )
601 View.SetDegenerateModeOn();
603 View.StartRotation( e.X, e.Y );
605 case CurrentAction3d.CurAction3d_WindowZooming:
606 Cursor = Cursors.Hand;
613 else if ( e.Button == MouseButtons.Right )
615 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl )
617 if ( !DegenerateMode )
619 View.SetDegenerateModeOn();
621 View.StartRotation( e.X, e.Y );
625 Popup.Show( this, new System.Drawing.Point( e.X, e.Y ) );
630 private void OnMouseUp( object sender, System.Windows.Forms.MouseEventArgs e )
632 if ( e.Button == MouseButtons.Left )
634 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl )
638 switch ( CurrentMode )
640 case CurrentAction3d.CurAction3d_Nothing:
641 if ( e.X == myXmin && e.Y == myYmin )
645 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Shift )
658 DrawRectangle( false );
659 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Shift )
661 MultiDragEvent( myXmax, myYmax, 1 );
665 DragEvent( myXmax, myYmax, 1 );
669 case CurrentAction3d.CurAction3d_DynamicZooming:
670 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
672 case CurrentAction3d.CurAction3d_WindowZooming:
675 DrawRectangle( false );
677 if ( Math.Abs(myXmax - myXmin) > ValZWMin &&
678 Math.Abs(myXmax - myYmax) > ValZWMin )
680 View.WindowFitAll( myXmin, myYmin, myXmax, myYmax );
682 Cursor = Cursors.Arrow;
683 RaiseZoomingFinished();
684 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
686 case CurrentAction3d.CurAction3d_DynamicPanning:
687 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
689 case CurrentAction3d.CurAction3d_GlobalPanning:
690 View.Place( e.X, e.Y, myCurZoom );
691 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
693 case CurrentAction3d.CurAction3d_DynamicRotation:
694 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
695 if ( !DegenerateMode )
697 View.SetDegenerateModeOff();
701 View.SetDegenerateModeOn();
708 else if ( e.Button == MouseButtons.Right )
710 if ( !DegenerateMode )
712 View.SetDegenerateModeOff();
716 View.SetDegenerateModeOn();
723 private void OnMouseMove( object sender, System.Windows.Forms.MouseEventArgs e )
725 if ( e.Button == MouseButtons.Left ) //left button is pressed
727 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl )
729 View.Zoom(myXmax, myYmax, e.X, e.Y);
735 switch ( CurrentMode )
737 case CurrentAction3d.CurAction3d_Nothing:
738 DrawRectangle( false );
741 DrawRectangle( true );
743 case CurrentAction3d.CurAction3d_DynamicZooming:
744 View.Zoom( myXmax, myYmax, e.X, e.Y );
748 case CurrentAction3d.CurAction3d_WindowZooming:
749 DrawRectangle( false );
752 DrawRectangle( true );//add brush here
754 case CurrentAction3d.CurAction3d_DynamicPanning:
755 View.Pan( e.X - myXmax, myYmax - e.Y );
759 case CurrentAction3d.CurAction3d_GlobalPanning:
761 case CurrentAction3d.CurAction3d_DynamicRotation:
762 View.Rotation( e.X, e.Y );
770 else if ( e.Button == MouseButtons.Middle ) //middle button is pressed
772 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl )
774 View.Pan( e.X - myXmax, myYmax - e.Y );
779 else if ( e.Button == MouseButtons.Right ) //right button is pressed
781 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl)
783 View.Rotation( e.X, e.Y );
786 else // no buttons are pressed
790 View.MoveTo( e.X, e.Y );
794 private void OnPopup( object sender, System.EventArgs e )
796 ContextWireframe.Enabled = IsWireframeEnabled;
797 ContextShading.Enabled = IsShadingEnabled;
798 ContextColor.Enabled = IsColorEnabled;
799 ContextMaterial.Enabled = IsMaterialEnabled;
800 ContextDelete.Enabled = IsDeleteEnabled;
801 ContextTransparency.Enabled = IsTransparencyEnabled;
802 ContextBackground.Enabled = true;
805 private void ContextWireframe_Click( object sender, System.EventArgs e )
810 private void ContextShading_Click( object sender, System.EventArgs e )
815 private void ContextColor_Click( object sender, System.EventArgs e )
820 private void ContextMaterial_Click( object sender, System.EventArgs e )
825 private void ContextTransparency_Click( object sender, System.EventArgs e )
830 private void ContextDelete_Click( object sender, System.EventArgs e )
835 private void ContextBackground_Click( object sender, System.EventArgs e )