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;
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("CSF_OCCTDataPath");
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( "Can'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("CSF_OCCTDataPath");
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 );
487 public void OnKeyDown( System.Windows.Input.Key theKey )
489 if ( theKey == System.Windows.Input.Key.LeftShift ||
490 theKey == System.Windows.Input.Key.RightShift )
492 CurrentPressedKey = CurrentPressedKey.CurPressedKey_Shift;
494 else if (theKey == System.Windows.Input.Key.LeftCtrl ||
495 theKey == System.Windows.Input.Key.RightCtrl )
497 CurrentPressedKey = CurrentPressedKey.CurPressedKey_Ctrl;
501 public void OnKeyUp()
503 CurrentPressedKey = CurrentPressedKey.CurPressedKey_Nothing;
506 protected void MultiDragEvent( int x, int y, int theState )
508 if ( theState == -1 ) //mouse is down
513 else if ( theState == 1) //mouse is up
515 View.ShiftSelect( Math.Min( myButtonDownX, x ), Math.Min( myButtonDownY, y ),
516 Math.Max( myButtonDownX, x ), Math.Max( myButtonDownY, y ) );
520 protected void DragEvent( int x, int y, int theState )
522 if ( theState == -1 ) //mouse is down
527 else if ( theState == 1 ) //mouse is up
529 View.Select( Math.Min( myButtonDownX, x ), Math.Min( myButtonDownY, y ),
530 Math.Max( myButtonDownX, x ), Math.Max( myButtonDownY, y ) );
534 private void DrawRectangle( bool draw )
536 System.Drawing.Graphics gr = System.Drawing.Graphics.FromHwnd(Handle);
537 System.Drawing.Pen p = null;
538 if ( IsRectVisible || !draw )//erase the rect
540 int r = View.GetBGColR();
541 int g = View.GetBGColG();
542 int b = View.GetBGColB();
543 p = new System.Drawing.Pen( System.Drawing.Color.FromArgb(r, g, b) );
544 IsRectVisible = false;
549 p = new System.Drawing.Pen( System.Drawing.Color.White );
550 IsRectVisible = true;
556 int x = Math.Min( myXmin, myXmax );
557 int y = Math.Min( myYmin, myYmax );
558 gr.DrawRectangle( p, x, y, Math.Abs(myXmax - myXmin), Math.Abs(myYmax - myYmin) );
559 myRectDownX = Math.Max( myXmin, myXmax );
560 myRectDownY = Math.Max( myYmin, myYmax );
563 private void OnMouseDown( object sender, System.Windows.Forms.MouseEventArgs e )
565 if ( e.Button == MouseButtons.Left )
571 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl )
573 // start the dynamic zooming....
574 CurrentMode = CurrentAction3d.CurAction3d_DynamicZooming;
578 switch ( CurrentMode )
580 case CurrentAction3d.CurAction3d_Nothing:
581 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Shift )
583 MultiDragEvent( myXmax, myYmax, -1 );
587 DragEvent( myXmax, myYmax, -1 );
590 case CurrentAction3d.CurAction3d_DynamicRotation:
591 if ( !DegenerateMode )
593 View.SetDegenerateModeOn();
595 View.StartRotation( e.X, e.Y );
597 case CurrentAction3d.CurAction3d_WindowZooming:
598 Cursor = Cursors.Hand;
605 else if ( e.Button == MouseButtons.Right )
607 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl )
609 if ( !DegenerateMode )
611 View.SetDegenerateModeOn();
613 View.StartRotation( e.X, e.Y );
617 Popup.Show( this, new System.Drawing.Point( e.X, e.Y ) );
622 private void OnMouseUp( object sender, System.Windows.Forms.MouseEventArgs e )
624 if ( e.Button == MouseButtons.Left )
626 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl )
630 switch ( CurrentMode )
632 case CurrentAction3d.CurAction3d_Nothing:
633 if ( e.X == myXmin && e.Y == myYmin )
637 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Shift )
650 DrawRectangle( false );
651 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Shift )
653 MultiDragEvent( myXmax, myYmax, 1 );
657 DragEvent( myXmax, myYmax, 1 );
661 case CurrentAction3d.CurAction3d_DynamicZooming:
662 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
664 case CurrentAction3d.CurAction3d_WindowZooming:
667 DrawRectangle( false );
669 if ( Math.Abs(myXmax - myXmin) > ValZWMin &&
670 Math.Abs(myXmax - myYmax) > ValZWMin )
672 View.WindowFitAll( myXmin, myYmin, myXmax, myYmax );
674 Cursor = Cursors.Arrow;
675 RaiseZoomingFinished();
676 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
678 case CurrentAction3d.CurAction3d_DynamicPanning:
679 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
681 case CurrentAction3d.CurAction3d_GlobalPanning:
682 View.Place( e.X, e.Y, myCurZoom );
683 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
685 case CurrentAction3d.CurAction3d_DynamicRotation:
686 CurrentMode = CurrentAction3d.CurAction3d_Nothing;
687 if ( !DegenerateMode )
689 View.SetDegenerateModeOff();
693 View.SetDegenerateModeOn();
700 else if ( e.Button == MouseButtons.Right )
702 if ( !DegenerateMode )
704 View.SetDegenerateModeOff();
708 View.SetDegenerateModeOn();
715 private void OnMouseMove( object sender, System.Windows.Forms.MouseEventArgs e )
717 if ( e.Button == MouseButtons.Left ) //left button is pressed
719 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl )
721 View.Zoom(myXmax, myYmax, e.X, e.Y);
727 switch ( CurrentMode )
729 case CurrentAction3d.CurAction3d_Nothing:
730 DrawRectangle( false );
733 DrawRectangle( true );
735 case CurrentAction3d.CurAction3d_DynamicZooming:
736 View.Zoom( myXmax, myYmax, e.X, e.Y );
740 case CurrentAction3d.CurAction3d_WindowZooming:
741 DrawRectangle( false );
744 DrawRectangle( true );//add brush here
746 case CurrentAction3d.CurAction3d_DynamicPanning:
747 View.Pan( e.X - myXmax, myYmax - e.Y );
751 case CurrentAction3d.CurAction3d_GlobalPanning:
753 case CurrentAction3d.CurAction3d_DynamicRotation:
754 View.Rotation( e.X, e.Y );
762 else if ( e.Button == MouseButtons.Middle ) //middle button is pressed
764 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl )
766 View.Pan( e.X - myXmax, myYmax - e.Y );
771 else if ( e.Button == MouseButtons.Right ) //right button is pressed
773 if ( CurrentPressedKey == CurrentPressedKey.CurPressedKey_Ctrl)
775 View.Rotation( e.X, e.Y );
778 else // no buttons are pressed
782 View.MoveTo( e.X, e.Y );
786 private void OnPopup( object sender, System.EventArgs e )
788 ContextWireframe.Enabled = IsWireframeEnabled;
789 ContextShading.Enabled = IsShadingEnabled;
790 ContextColor.Enabled = IsColorEnabled;
791 ContextMaterial.Enabled = IsMaterialEnabled;
792 ContextDelete.Enabled = IsDeleteEnabled;
793 ContextTransparency.Enabled = IsTransparencyEnabled;
794 ContextBackground.Enabled = true;
797 private void ContextWireframe_Click( object sender, System.EventArgs e )
802 private void ContextShading_Click( object sender, System.EventArgs e )
807 private void ContextColor_Click( object sender, System.EventArgs e )
812 private void ContextMaterial_Click( object sender, System.EventArgs e )
817 private void ContextTransparency_Click( object sender, System.EventArgs e )
822 private void ContextDelete_Click( object sender, System.EventArgs e )
827 private void ContextBackground_Click( object sender, System.EventArgs e )