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
34 public enum DisplayMode
40 public class OCCViewer : System.Windows.Forms.Form
42 public event EventHandler ZoomingFinished;
43 protected void RaiseZoomingFinished()
45 if ( ZoomingFinished != null )
47 ZoomingFinished( this, EventArgs.Empty );
51 public event EventHandler AvaliabiltyOfOperationsChanged;
52 protected void RaiseAvaliabiltyOfOperationsChanged()
54 if ( AvaliabiltyOfOperationsChanged != null )
56 AvaliabiltyOfOperationsChanged( this, EventArgs.Empty );
60 public OCCTProxy View { get; private set; }
61 public CurrentAction3d CurrentMode { get; private set; }
62 private CurrentPressedKey CurrentPressedKey { get; set; }
63 private bool IsRectVisible { get; set; }
64 public bool DegenerateMode { get; private set; }
66 public bool IsWireframeEnabled { get; private set; }
67 public bool IsShadingEnabled { get; private set; }
68 public bool IsTransparencyEnabled { get; private set; }
69 public bool IsColorEnabled { get; private set; }
70 public bool IsMaterialEnabled { get; private set; }
71 public bool IsDeleteEnabled { get; private set; }
73 private float myCurZoom;// ~ Quantity_Factor
78 private int myRectDownX;
79 private int myRectDownY;
80 private int myButtonDownX;
81 private int myButtonDownY;
83 private ContextMenu Popup { get; set; }
84 private MenuItem ContextWireframe;
85 private MenuItem ContextShading;
86 private MenuItem ContextColor;
87 private MenuItem ContextMaterial;
88 private MenuItem ContextDelete;
89 private MenuItem ContextBackground;
90 private MenuItem ContextTransparency;
95 InitializeComponent();
97 View = new OCCTProxy();
99 if ( !View.InitViewer( this.Handle ) )
101 MessageBox.Show( "Fatal Error during the graphic initialisation", "Error!" );
104 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
105 CurrentPressedKey = CurrentPressedKey.CurPressedKey_Nothing;
106 IsRectVisible = false;
107 DegenerateMode = true;
110 private void InitializeComponent()
115 this.ImeMode = System.Windows.Forms.ImeMode.NoControl;
117 SizeChanged += new System.EventHandler( OnSizeChanged );
118 Paint += new System.Windows.Forms.PaintEventHandler( OnPaint );
120 MouseDown += new System.Windows.Forms.MouseEventHandler( OnMouseDown );
121 MouseUp += new System.Windows.Forms.MouseEventHandler( OnMouseUp );
122 MouseMove += new System.Windows.Forms.MouseEventHandler( OnMouseMove );
124 Popup = new ContextMenu();
125 ContextWireframe = new MenuItem();
126 ContextShading = new MenuItem();
127 ContextColor = new MenuItem();
128 ContextMaterial = new MenuItem();
129 ContextTransparency = new MenuItem();
130 ContextDelete = new MenuItem();
131 ContextBackground = new MenuItem();
133 ContextWireframe.Text = "Wireframe";
134 ContextShading.Text = "Shading";
135 ContextColor.Text = "Color";
136 ContextMaterial.Text = "Material";
137 ContextTransparency.Text = "Transparency";
138 ContextDelete.Text = "Delete";
139 ContextBackground.Text = "Background";
141 ContextWireframe.Click += new System.EventHandler( ContextWireframe_Click );
142 ContextShading.Click += new System.EventHandler( ContextShading_Click );
143 ContextColor.Click += new System.EventHandler( ContextColor_Click );
144 ContextMaterial.Click += new System.EventHandler( ContextMaterial_Click );
145 ContextTransparency.Click += new System.EventHandler( ContextTransparency_Click );
146 ContextDelete.Click += new System.EventHandler( ContextDelete_Click );
147 ContextBackground.Click += new System.EventHandler( ContextBackground_Click );
149 Popup.MenuItems.AddRange( new MenuItem[] { ContextWireframe,
155 ContextBackground } );
156 Popup.Popup += new System.EventHandler( OnPopup );
159 private void OnPaint(object sender, System.Windows.Forms.PaintEventArgs e)
165 private void OnSizeChanged(object sender, System.EventArgs e)
170 public void ImportModel( ModelFormat theFormat )
173 OpenFileDialog anOpenDialog = new OpenFileDialog();
174 string aDataDir = ( (Environment.GetEnvironmentVariable("CASROOT")) + "\\..\\data" );
179 case ModelFormat.BREP:
180 anOpenDialog.InitialDirectory = (aDataDir + "\\occ");
182 aFilter = "BREP Files (*.brep *.rle)|*.brep; *.rle";
184 case ModelFormat.STEP:
185 anOpenDialog.InitialDirectory = (aDataDir + "\\step");
187 aFilter = "STEP Files (*.stp *.step)|*.stp; *.step";
189 case ModelFormat.IGES:
190 anOpenDialog.InitialDirectory = (aDataDir + "\\iges");
192 aFilter = "IGES Files (*.igs *.iges)|*.igs; *.iges";
198 anOpenDialog.Filter = aFilter + "|All files (*.*)|*.*";
199 if (anOpenDialog.ShowDialog() == DialogResult.OK)
201 string aFileName = anOpenDialog.FileName;
207 Cursor = System.Windows.Forms.Cursors.WaitCursor;
208 if ( !View.TranslateModel( aFileName, aFormat, true ) )
210 MessageBox.Show( "Cann't read this file", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning );
212 Cursor = System.Windows.Forms.Cursors.Default;
217 public void ExportModel( ModelFormat theFormat )
220 SaveFileDialog saveDialog = new SaveFileDialog();
221 string aDataDir = ( (Environment.GetEnvironmentVariable("CASROOT") ) + "\\..\\data" );
226 case ModelFormat.BREP:
227 saveDialog.InitialDirectory = ( aDataDir + "\\occ" );
229 aFilter = "BREP Files (*.brep *.rle)|*.brep; *.rle";
231 case ModelFormat.STEP:
232 saveDialog.InitialDirectory = ( aDataDir + "\\step" );
234 aFilter = "STEP Files (*.stp *.step)|*.step; *.stp";
236 case ModelFormat.IGES:
237 saveDialog.InitialDirectory = ( aDataDir + "\\iges" );
239 aFilter = "IGES Files (*.igs *.iges)| *.iges; *.igs";
241 case ModelFormat.VRML:
242 saveDialog.InitialDirectory = ( aDataDir + "\\vrml" );
244 aFilter = "VRML Files (*.vrml)|*.vrml";
246 case ModelFormat.STL:
247 saveDialog.InitialDirectory = ( aDataDir + "\\stl" );
249 aFilter = "STL Files (*.stl)|*.stl";
251 case ModelFormat.IMAGE:
252 saveDialog.InitialDirectory = ( aDataDir + "\\images" );
254 aFilter = "Images Files (*.bmp)|*.bmp";
260 saveDialog.Filter = aFilter;
261 if ( saveDialog.ShowDialog() == DialogResult.OK )
263 string aFileName = saveDialog.FileName;
264 if ( aFileName == "" )
269 Cursor = System.Windows.Forms.Cursors.WaitCursor;
270 if ( !View.TranslateModel( aFileName, aFormat, false ) )
272 MessageBox.Show( "Can not write this file", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning );
274 Cursor = System.Windows.Forms.Cursors.Default;
283 public void ZoomWindow()
285 CurrentMode = CurrentAction3d.CurAction3d_WindowZooming;
288 public void DynamicZooming()
290 CurrentMode = CurrentAction3d.CurAction3d_DynamicZooming;
293 public void DynamicPanning()
295 CurrentMode = CurrentAction3d.CurAction3d_DynamicPanning;
298 public void GlobalPanning()
300 myCurZoom = View.Scale();
301 CurrentMode = CurrentAction3d.CurAction3d_GlobalPanning;
304 public void AxoView()
309 public void FrontView()
314 public void TopView()
319 public void LeftView()
324 public void BackView()
329 public void RightView()
339 public void BottomView()
344 public void HiddenOff()
346 View.SetDegenerateModeOff();
347 DegenerateMode = false;
350 public void HiddenOn()
352 View.SetDegenerateModeOn();
353 DegenerateMode = true;
356 public void DynamicRotation()
358 CurrentMode = CurrentAction3d.CurAction3d_DynamicRotation;
361 public void SelectionChanged()
363 switch ( View.DisplayMode() )
366 IsShadingEnabled = false;
367 IsWireframeEnabled = false;
370 IsWireframeEnabled = false;
371 IsShadingEnabled = true;
372 IsTransparencyEnabled = false;
375 IsWireframeEnabled = true;
376 IsShadingEnabled = false;
377 IsTransparencyEnabled = true;
380 IsWireframeEnabled = true;
381 IsShadingEnabled = true;
382 IsTransparencyEnabled = true;
388 if ( View.IsObjectSelected() )
390 IsColorEnabled = true;
391 IsMaterialEnabled = true;
392 IsDeleteEnabled = true;
396 IsColorEnabled = false;
397 IsMaterialEnabled = false;
398 IsTransparencyEnabled = false;
399 IsDeleteEnabled = false;
402 RaiseAvaliabiltyOfOperationsChanged();
405 public void ChangeColor( bool IsObjectColor )
410 r = View.GetObjColR();
411 g = View.GetObjColG();
412 b = View.GetObjColB();
416 r = View.GetBGColR();
417 g = View.GetBGColG();
418 b = View.GetBGColB();
420 System.Windows.Forms.ColorDialog ColDlg = new System.Windows.Forms.ColorDialog();
421 ColDlg.Color = System.Drawing.Color.FromArgb( r, g, b );
422 if ( ColDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK )
424 System.Drawing.Color c = ColDlg.Color;
430 View.SetColor( r, g, b );
434 View.SetBackgroundColor( r, g, b );
437 View.UpdateCurrentViewer();
440 public void Wireframe()
442 View.SetDisplayMode( (int)DisplayMode.Wireframe );
443 View.UpdateCurrentViewer();
446 RaiseZoomingFinished();
449 public void Shading()
451 View.SetDisplayMode( (int)DisplayMode.Shading );
452 View.UpdateCurrentViewer();
455 RaiseZoomingFinished();
463 public void Background()
465 ChangeColor( false );
468 public void Material()
470 MaterialDlg aDlg = new MaterialDlg( View );
474 public void Transparency()
476 TransparencyDialog dlg = new TransparencyDialog();
478 dlg.ShowDialog( this );
486 public void OnKeyDown( System.Windows.Input.Key theKey )
488 if ( theKey == System.Windows.Input.Key.LeftShift ||
489 theKey == System.Windows.Input.Key.RightShift )
491 CurrentPressedKey = CurrentPressedKey.CurPressedKey_Shift;
493 else if (theKey == System.Windows.Input.Key.LeftCtrl ||
494 theKey == System.Windows.Input.Key.RightCtrl )
496 CurrentPressedKey = CurrentPressedKey.CurPressedKey_Ctrl;
500 public void OnKeyUp()
502 CurrentPressedKey = CurrentPressedKey.CurPressedKey_Nothing;
505 protected void MultiDragEvent( int x, int y, int theState )
507 if ( theState == -1 ) //mouse is down
512 else if ( theState == 1) //mouse is up
514 View.ShiftSelect( Math.Min( myButtonDownX, x ), Math.Min( myButtonDownY, y ),
515 Math.Max( myButtonDownX, x ), Math.Max( myButtonDownY, y ) );
519 protected void DragEvent( int x, int y, int theState )
521 if ( theState == -1 ) //mouse is down
526 else if ( theState == 1 ) //mouse is up
528 View.Select( Math.Min( myButtonDownX, x ), Math.Min( myButtonDownY, y ),
529 Math.Max( myButtonDownX, x ), Math.Max( myButtonDownY, y ) );
533 private void DrawRectangle( bool draw )
535 System.Drawing.Graphics gr = System.Drawing.Graphics.FromHwnd(Handle);
536 System.Drawing.Pen p = null;
537 if ( IsRectVisible || !draw )//erase the rect
539 int r = View.GetBGColR();
540 int g = View.GetBGColG();
541 int b = View.GetBGColB();
542 p = new System.Drawing.Pen( System.Drawing.Color.FromArgb(r, g, b) );
543 IsRectVisible = false;
548 p = new System.Drawing.Pen( System.Drawing.Color.White );
549 IsRectVisible = true;
555 int x = Math.Min( myXmin, myXmax );
556 int y = Math.Min( myYmin, myYmax );
557 gr.DrawRectangle( p, x, y, Math.Abs(myXmax - myXmin), Math.Abs(myYmax - myYmin) );
558 myRectDownX = Math.Max( myXmin, myXmax );
559 myRectDownY = Math.Max( myYmin, myYmax );
562 private void OnMouseDown( object sender, System.Windows.Forms.MouseEventArgs e )
564 if ( e.Button == MouseButtons.Left )
570 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl )
572 // start the dinamic zooming....
573 CurrentMode = CurrentAction3d.CurAction3d_DynamicZooming;
577 switch ( CurrentMode )
579 case CurrentAction3d.CurAction3d_Nothing:
580 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Shift )
582 MultiDragEvent( myXmax, myYmax, -1 );
586 DragEvent( myXmax, myYmax, -1 );
589 case CurrentAction3d.CurAction3d_DynamicRotation:
590 if ( !DegenerateMode )
592 View.SetDegenerateModeOn();
594 View.StartRotation( e.X, e.Y );
596 case CurrentAction3d.CurAction3d_WindowZooming:
597 Cursor = Cursors.Hand;
604 else if ( e.Button == MouseButtons.Right )
606 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl )
608 if ( !DegenerateMode )
610 View.SetDegenerateModeOn();
612 View.StartRotation( e.X, e.Y );
616 Popup.Show( this, new System.Drawing.Point( e.X, e.Y ) );
621 private void OnMouseUp( object sender, System.Windows.Forms.MouseEventArgs e )
623 if ( e.Button == MouseButtons.Left )
625 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl )
629 switch ( CurrentMode )
631 case CurrentAction3d.CurAction3d_Nothing:
632 if ( e.X == myXmin && e.Y == myYmin )
636 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Shift )
649 DrawRectangle( false );
650 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Shift )
652 MultiDragEvent( myXmax, myYmax, 1 );
656 DragEvent( myXmax, myYmax, 1 );
660 case CurrentAction3d.CurAction3d_DynamicZooming:
661 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
663 case CurrentAction3d.CurAction3d_WindowZooming:
666 DrawRectangle( false );
668 if ( Math.Abs(myXmax - myXmin) > ValZWMin &&
669 Math.Abs(myXmax - myYmax) > ValZWMin )
671 View.WindowFitAll( myXmin, myYmin, myXmax, myYmax );
673 Cursor = Cursors.Arrow;
674 RaiseZoomingFinished();
675 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
677 case CurrentAction3d.CurAction3d_DynamicPanning:
678 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
680 case CurrentAction3d.CurAction3d_GlobalPanning:
681 View.Place( e.X, e.Y, myCurZoom );
682 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
684 case CurrentAction3d.CurAction3d_DynamicRotation:
685 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
686 if ( !DegenerateMode )
688 View.SetDegenerateModeOff();
692 View.SetDegenerateModeOn();
699 else if ( e.Button == MouseButtons.Right )
701 if ( !DegenerateMode )
703 View.SetDegenerateModeOff();
707 View.SetDegenerateModeOn();
714 private void OnMouseMove( object sender, System.Windows.Forms.MouseEventArgs e )
716 if ( e.Button == MouseButtons.Left ) //left button is pressed
718 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl )
720 View.Zoom(myXmax, myYmax, e.X, e.Y);
726 switch ( CurrentMode )
728 case CurrentAction3d.CurAction3d_Nothing:
729 DrawRectangle( false );
732 DrawRectangle( true );
734 case CurrentAction3d.CurAction3d_DynamicZooming:
735 View.Zoom( myXmax, myYmax, e.X, e.Y );
739 case CurrentAction3d.CurAction3d_WindowZooming:
740 DrawRectangle( false );
743 DrawRectangle( true );//add brush here
745 case CurrentAction3d.CurAction3d_DynamicPanning:
746 View.Pan( e.X - myXmax, myYmax - e.Y );
750 case CurrentAction3d.CurAction3d_GlobalPanning:
752 case CurrentAction3d.CurAction3d_DynamicRotation:
753 View.Rotation( e.X, e.Y );
761 else if ( e.Button == MouseButtons.Middle ) //middle button is pressed
763 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl )
765 View.Pan( e.X - myXmax, myYmax - e.Y );
770 else if ( e.Button == MouseButtons.Right ) //right button is pressed
772 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl)
774 View.Rotation( e.X, e.Y );
777 else // no buttons are pressed
781 View.MoveTo( e.X, e.Y );
785 private void OnPopup( object sender, System.EventArgs e )
787 ContextWireframe.Enabled = IsWireframeEnabled;
788 ContextShading.Enabled = IsShadingEnabled;
789 ContextColor.Enabled = IsColorEnabled;
790 ContextMaterial.Enabled = IsMaterialEnabled;
791 ContextDelete.Enabled = IsDeleteEnabled;
792 ContextTransparency.Enabled = IsTransparencyEnabled;
793 ContextBackground.Enabled = true;
796 private void ContextWireframe_Click( object sender, System.EventArgs e )
801 private void ContextShading_Click( object sender, System.EventArgs e )
806 private void ContextColor_Click( object sender, System.EventArgs e )
811 private void ContextMaterial_Click( object sender, System.EventArgs e )
816 private void ContextTransparency_Click( object sender, System.EventArgs e )
821 private void ContextDelete_Click( object sender, System.EventArgs e )
826 private void ContextBackground_Click( object sender, System.EventArgs e )