当前位置:   article > 正文

opencascade c#例程解析

opencascade c#例程解析

1.编译

将msvc.bat文件拖入vs2022的x64 native tools,即可

2.about.xaml

  1. <Window
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. x:Class="IE_WPF_WinForms.AboutDialog"
  5. x:Name="Window"
  6. xmlns:local="clr-namespace:IE_WPF_WinForms"
  7. Title="About Import/Export Sample"
  8. Width="312" Height="285">
  9. <StackPanel Orientation="Vertical">
  10. <Label Content="Import/Export Sample," HorizontalAlignment="Center"/>
  11. <Label Content="Open CASCADE Technology " HorizontalAlignment="Center"/>
  12. <Image Source="res\occ_logo.bmp" Width="194" Height="100" />
  13. <Label Content="Copyright (C) 2004-2013, Open CASCADE S.A.S" HorizontalAlignment="Center"/>
  14. <Label Content="http://www.opencascade.com" HorizontalAlignment="Center" FontFamily="Modern No. 20"/>
  15. <Button Content="OK" HorizontalAlignment="Center" Width="75" Command="local:IECommands.AboutOk"/>
  16. </StackPanel>
  17. </Window>
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Data;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Imaging;
  11. using System.Windows.Shapes;
  12. namespace IE_WPF_WinForms
  13. {
  14. /// <summary>
  15. /// Interaction logic for About.xaml
  16. /// </summary>
  17. public partial class AboutDialog : Window
  18. {
  19. public AboutDialog()
  20. {
  21. this.InitializeComponent();
  22. CommandBinding aBind_Ok = new CommandBinding( IECommands.AboutOk );//CommandBinding 用于将命令(例如按钮点击)与事件处理程序(例如方法)关联起来。当命令执行时,与之关联的事件处理程序将被调用,从而执行相应的逻辑。这种机制使得开发人员可以在代码中定义一次命令处理逻辑,然后在需要的地方重用该逻辑,而不必在每个界面元素上都编写事件处理程序。
  23. aBind_Ok.Executed += OkCommand_Executed;//这行代码将 OkCommand_Executed 方法添加为 aBind_Ok 对象的 Executed 事件的事件处理程序。换句话说,当 aBind_Ok 对象引发 Executed 事件时(例如,当与该命令关联的操作被触发时),OkCommand_Executed 方法将被调用来执行相应的逻辑。
  24. aBind_Ok.CanExecute += OkCommand_CanExecute;//决定aBind_Ok.Executed是否能被执行
  25. CommandBindings.Add(aBind_Ok);
  26. }
  27. private void OkCommand_Executed( object sender, ExecutedRoutedEventArgs e )
  28. {
  29. this.Close();
  30. }
  31. private void OkCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  32. {
  33. e.CanExecute = true;//设置为false则不可执行,如果把OkCommand_CanExecute相关的都注释掉,则默认为不可执行
  34. }
  35. }
  36. }

2.iecommands

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Input;
  6. namespace IE_WPF_WinForms
  7. {
  8. public class IECommands
  9. {
  10. // 定义静态属性,表示不同的命令
  11. public static RoutedUICommand New { get; private set; }
  12. public static RoutedUICommand Close { get; private set; }
  13. public static RoutedUICommand Quit { get; private set; }
  14. public static RoutedUICommand About { get; private set; }
  15. public static RoutedUICommand AboutOk { get; private set; }
  16. static IECommands()
  17. {
  18. #region menu
  19. // 创建新命令,并设置快捷键
  20. InputGestureCollection inputsNew = new InputGestureCollection();
  21. inputsNew.Add(new KeyGesture(Key.N, ModifierKeys.Control, "Ctrl + N"));
  22. New = new RoutedUICommand("New", "New", typeof(IECommands), inputsNew);
  23. // 创建关闭命令
  24. Close = new RoutedUICommand("Close", "Close", typeof(IECommands));
  25. // 创建退出命令,并设置快捷键
  26. InputGestureCollection inputsQuit = new InputGestureCollection();
  27. inputsQuit.Add(new KeyGesture(Key.F4, ModifierKeys.Alt, "Alt + F4"));
  28. Quit = new RoutedUICommand("Quit", "Quit", typeof(IECommands), inputsQuit);
  29. // 创建关于命令,并设置快捷键
  30. InputGestureCollection inputsAbout = new InputGestureCollection();
  31. inputsAbout.Add(new KeyGesture(Key.F1));
  32. About = new RoutedUICommand("About", "About", typeof(IECommands), inputsAbout);
  33. #endregion
  34. #region aboutDlg
  35. // 创建关于对话框确定命令,并设置快捷键
  36. InputGestureCollection inputsAboutOk = new InputGestureCollection();
  37. inputsAboutOk.Add(new KeyGesture(Key.Enter));
  38. AboutOk = new RoutedUICommand("AboutOk", "AboutOk", typeof(IECommands), inputsAboutOk);
  39. #endregion
  40. }
  41. }
  42. }

这段代码定义了一个静态类 IECommands,其中包含了几个静态属性,每个属性表示一个命令。每个命令都是一个 RoutedUICommand 类型的对象,用于处理 WPF 应用程序中的命令操作。具体注释如下:

  • public static RoutedUICommand New { get; private set; }:表示新建命令。
  • public static RoutedUICommand Close { get; private set; }:表示关闭命令。
  • public static RoutedUICommand Quit { get; private set; }:表示退出命令。
  • public static RoutedUICommand About { get; private set; }:表示关于命令。
  • public static RoutedUICommand AboutOk { get; private set; }:表示关于对话框确定命令。

在静态构造函数中,每个命令对象都被实例化并初始化,其中包括命令名称、显示文本和快捷键等信息。

3.mainwindow.xaml

  1. <Window
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" <!-- 命名空间定义 -->
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" <!-- 命名空间定义 -->
  4. x:Class="IE_WPF_WinForms.MainWindow" <!-- 定义窗口类 -->
  5. xmlns:local="clr-namespace:IE_WPF_WinForms" <!-- 命名空间定义 -->
  6. Title="Sample Import/Export" Height="600" Width="900" Icon="res/MainFrame.ico"> <!-- 窗口标题、大小、图标 -->
  7. <Window.Background> <!-- 设置窗口背景 -->
  8. <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlDarkColorKey}}"/> <!-- 使用动态资源设置背景颜色 -->
  9. </Window.Background>
  10. <Window.Resources> <!-- 设置窗口资源 -->
  11. <BooleanToVisibilityConverter x:Key="boolToVisibilityConverter"/> <!-- 定义布尔值转换为可见性的转换器 -->
  12. </Window.Resources>
  13. <Grid> <!-- 定义主体布局为网格 -->
  14. <Grid.RowDefinitions> <!-- 定义网格行 -->
  15. <RowDefinition Height="auto"/> <!-- 第一行高度自适应 -->
  16. <RowDefinition Height="auto"/> <!-- 第二行高度自适应 -->
  17. <RowDefinition /> <!-- 第三行高度为剩余空间 -->
  18. <RowDefinition Height="auto"/> <!-- 第四行高度自适应 -->
  19. </Grid.RowDefinitions>
  20. <Menu Height="25" Grid.Row="0"> <!-- 菜单栏,位于第一行 -->
  21. <MenuItem Header="File"> <!-- 文件菜单项 -->
  22. <MenuItem Command="local:IECommands.New"/> <!-- 新建命令 -->
  23. <MenuItem Command="local:IECommands.Close"/> <!-- 关闭命令 -->
  24. <MenuItem Header="Import" IsEnabled="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=IsDocumentOpen}"> <!-- 导入子菜单 -->
  25. <MenuItem Name="ImportBrep" Header="BRep..." Click="ImportBRep_Click"/> <!-- BRep 导入选项 -->
  26. <MenuItem Name="ImportIges" Header="Iges..." Click="ImportIges_Click"/> <!-- Iges 导入选项 -->
  27. <MenuItem Name="ImportStep" Header="Step..." Click="ImportStep_Click"/> <!-- Step 导入选项 --在这段 XAML 代码中,最下面的 </MenuItem> 标签表示对 <MenuItem> 元素的闭合,它用于结束一个 MenuItem 的定义。>
  28. </MenuItem>
  29. <MenuItem Header="Export" IsEnabled="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=IsDocumentOpen}"> <!-- 导出子菜单 -->
  30. <MenuItem Name="ExportBrep" Header="BRep..." Click="ExportBRep_Click"/> <!-- BRep 导出选项 -->
  31. <MenuItem Name="ExportIges" Header="Iges..." Click="ExportIges_Click"/> <!-- Iges 导出选项 -->
  32. <MenuItem Name="ExportStep" Header="Step..." Click="ExportStep_Click"/> <!-- Step 导出选项 -->
  33. <MenuItem Name="ExportStl" Header="Stl..." Click="ExportStl_Click"/> <!-- Stl 导出选项 -->
  34. <MenuItem Name="ExportVrml" Header="Vrml..." Click="ExportVrml_Click"/> <!-- Vrml 导出选项 -->
  35. <Separator/> <!-- 分隔线 -->
  36. <MenuItem Name="ExportImage" Header="Image..." Click="ExportImage_Click"/> <!-- 图像导出选项 -->
  37. </MenuItem>
  38. <Separator/> <!-- 分隔线 -->
  39. <MenuItem Command="local:IECommands.Quit"/> <!-- 退出命令 -->
  40. </MenuItem>
  41. <MenuItem Header="View"> <!-- 视图菜单项 -->
  42. <MenuItem Name="ActivateToolbar" IsCheckable="True" IsChecked="True" Header="Toolbar"/> <!-- 工具栏显示/隐藏选项 -->
  43. <MenuItem Name="ActivateStatusbar" IsCheckable="True" IsChecked="True" Header="Statusbar"/> <!-- 状态栏显示/隐藏选项 -->
  44. </MenuItem>
  45. <MenuItem Header="Help"> <!-- 帮助菜单项 -->
  46. <MenuItem Header="About" Command="local:IECommands.About"/> <!-- 关于命令 -->
  47. </MenuItem>
  48. </Menu>
  49. <ToolBarTray Background="White" Grid.Row="1"> <!-- 工具栏托盘,位于第二行 -->
  50. <ToolBar Name="ToolBar" Band="1" BandIndex="1" <!-- 工具栏 -->
  51. Visibility="{Binding ElementName=ActivateToolbar, Path=IsChecked, Converter={StaticResource boolToVisibilityConverter}}" <!-- 根据绑定的可见性属性显示/隐藏工具栏 -->
  52. MouseEnter="ToolBar_MouseEnter" MouseLeave="ToolBar_MouseLeave"> <!-- 鼠标进入/离开事件 -->
  53. <Button ToolTip="New" Command="local:IECommands.New"> <!-- 新建按钮 -->
  54. <Image Source="res/new.png" Style="{StaticResource toolbarImageStyle}"/> <!-- 图像 -->
  55. </Button>
  56. <Button ToolTip="About" Command="local:IECommands.About"> <!-- 关于按钮 -->
  57. <Image Source="res/help.png" Style="{StaticResource toolbarImageStyle}"/> <!-- 图像 -->
  58. </Button>
  59. </ToolBar>
  60. <!-- 其他工具栏按钮省略 -->
  61. </ToolBarTray>
  62. <TabControl Name="ViewPanel" Grid.Row="2" <!-- 标签页控件,位于第三行 -->
  63. SelectionChanged="OnViewerChanged" <!-- 选择改变事件 -->
  64. Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=IsDocumentOpen, Converter={StaticResource boolToVisibilityConverter}}"> <!-- 根据绑定的可见性属性显示/隐藏标签页 -->
  65. <TabControl.ContextMenu> <!-- 标签页右键菜单 -->
  66. <ContextMenu>
  67. <MenuItem Command="local:IECommands.New" Style="{StaticResource popupItem}"/> <!-- 新建命令 -->
  68. <MenuItem Command="local:IECommands.Close" Style="{StaticResource popupItem}"/> <!-- 关闭命令 -->
  69. </ContextMenu>
  70. </TabControl.ContextMenu>
  71. </TabControl>
  72. <StatusBar Background="White" Grid.Row="3" Visibility="{Binding ElementName=ActivateStatusbar, Path=IsChecked, Converter={StaticResource boolToVisibilityConverter}}"> <!-- 状态栏,位于第四行 -->
  73. <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=StatusBarText}"/> <!-- 状态栏文本 -->
  74. </StatusBar>
  75. </Grid>
  76. </Window>

4.mainwindow.xaml.cs

  1. using System.Text; // 导入System.Text命名空间
  2. using System.Windows; // 导入System.Windows命名空间
  3. using System.Windows.Controls; // 导入System.Windows.Controls命名空间
  4. using System.Windows.Data; // 导入System.Windows.Data命名空间
  5. using System.Windows.Documents; // 导入System.Windows.Documents命名空间
  6. using System.Windows.Input; // 导入System.Windows.Input命名空间
  7. using System.Windows.Media; // 导入System.Windows.Media命名空间
  8. using System.Windows.Media.Imaging; // 导入System.Windows.Media.Imaging命名空间
  9. using System.Windows.Navigation; // 导入System.Windows.Navigation命名空间
  10. using System.Windows.Shapes; // 导入System.Windows.Shapes命名空间
  11. using System.ComponentModel; // 导入System.ComponentModel命名空间
  12. using System.Windows.Forms.Integration; // 导入System.Windows.Forms.Integration命名空间
  13. namespace IE_WPF_WinForms
  14. {
  15. /// <summary>
  16. /// Interaction logic for MainWindow.xaml
  17. /// </summary>
  18. public partial class MainWindow : Window, INotifyPropertyChanged // 定义MainWindow类,继承自Window类,实现了INotifyPropertyChanged接口
  19. {
  20. public event PropertyChangedEventHandler PropertyChanged; // PropertyChanged事件,用于通知属性更改
  21. protected void RaisePropertyChanged(string thePropertyName) // 属性更改通知方法
  22. {
  23. if (PropertyChanged != null)
  24. {
  25. PropertyChanged(this, new PropertyChangedEventArgs(thePropertyName));
  26. }
  27. }
  28. // 构造函数
  29. public MainWindow()
  30. {
  31. InitializeComponent(); // 初始化窗口组件
  32. // 初始化属性值
  33. StatusBarText = String.Empty;
  34. IsHlrOffPushed = false;
  35. IsHlrOnPushed = true;
  36. IsZoomWinEnabled = true;
  37. // 添加命令绑定
  38. #region menu operations
  39. // 添加New命令绑定
  40. CommandBinding aBind_New = new CommandBinding(IECommands.New);
  41. aBind_New.Executed += NewCommand_Executed;
  42. CommandBindings.Add(aBind_New);
  43. // 添加Close命令绑定
  44. CommandBinding aBind_Close = new CommandBinding(IECommands.Close);
  45. aBind_Close.Executed += CloseCommand_Executed;
  46. aBind_Close.CanExecute += CloseCommand_CanExecute;
  47. CommandBindings.Add(aBind_Close);
  48. // 添加Quit命令绑定
  49. CommandBinding aBind_Quit = new CommandBinding(IECommands.Quit);
  50. aBind_Quit.Executed += QuitCommand_Executed;
  51. CommandBindings.Add(aBind_Quit);
  52. // 添加About命令绑定
  53. CommandBinding aBind_About = new CommandBinding(IECommands.About);
  54. aBind_About.Executed += AboutCommand_Executed;
  55. CommandBindings.Add(aBind_About);
  56. # endregion
  57. PreviewKeyDown += new KeyEventHandler(OnPreviewKeyDown); // 添加键盘按下事件处理程序
  58. PreviewKeyUp += new KeyEventHandler(OnPreviewKeyUp); // 添加键盘释放事件处理程序
  59. }
  60. // 状态栏文本属性
  61. private String myStatusBarText;
  62. public String StatusBarText
  63. {
  64. get
  65. {
  66. return myStatusBarText;
  67. }
  68. private set
  69. {
  70. myStatusBarText = value;
  71. RaisePropertyChanged("StatusBarText");
  72. }
  73. }
  74. // HlrOff按钮状态属性
  75. private bool isHlrOffPushed;
  76. public Boolean IsHlrOffPushed
  77. {
  78. get
  79. {
  80. return isHlrOffPushed;
  81. }
  82. set
  83. {
  84. isHlrOffPushed = value;
  85. RaisePropertyChanged("isHlrOffPushed");
  86. }
  87. }
  88. // HlrOn按钮状态属性
  89. private bool isHlrOnPushed;
  90. public Boolean IsHlrOnPushed
  91. {
  92. get
  93. {
  94. return isHlrOnPushed;
  95. }
  96. set
  97. {
  98. isHlrOnPushed = value;
  99. RaisePropertyChanged("IsHlrOnPushed");
  100. }
  101. }
  102. // ZoomWin按钮状态属性
  103. private bool isZoomWinEnabled;
  104. public Boolean IsZoomWinEnabled
  105. {
  106. get
  107. {
  108. return isZoomWinEnabled;
  109. }
  110. set
  111. {
  112. isZoomWinEnabled = value;
  113. RaisePropertyChanged("IsZoomWinEnabled");
  114. }
  115. }
  116. // 获取当前活动的OCCViewer
  117. private OCCViewer ActiveViewer
  118. {
  119. get
  120. {
  121. if (!IsDocumentOpen)
  122. {
  123. return null;
  124. }
  125. WindowsFormsHost aHost = (ViewPanel.SelectedContent) as WindowsFormsHost;
  126. if (aHost == null)
  127. {
  128. return null;
  129. }
  130. return aHost.Child as OCCViewer;
  131. }
  132. }
  133. // 判断文档是否打开属性
  134. public Boolean IsDocumentOpen
  135. {
  136. get
  137. {
  138. return ViewPanel.Items.Count > 0;
  139. }
  140. }
  141. // 新文档计数器
  142. private int myDocumentCounter = 1;
  143. // New命令执行方法
  144. private void NewCommand_Executed(object sender, ExecutedRoutedEventArgs e)
  145. {
  146. // 创建新文档
  147. WindowsFormsHost aHost = new WindowsFormsHost();
  148. OCCViewer aForm = new OCCViewer();
  149. aForm.Show();
  150. aHost.Child = aForm;
  151. TabItem aNewTab = new TabItem();
  152. aNewTab.Content = aHost;
  153. aNewTab.IsSelected = true;
  154. aNewTab.Header = "Document " + myDocumentCounter.ToString();
  155. myDocumentCounter++;
  156. ViewPanel.Items.Add(aNewTab);
  157. // 更新XAML属性
  158. RaisePropertyChanged("IsDocumentOpen");
  159. }
  160. // Close命令执行方法
  161. private void CloseCommand_Executed(object sender, ExecutedRoutedEventArgs e)
  162. {
  163. if (ViewPanel.Items.Count > 0)
  164. {
  165. ViewPanel.Items.Remove(ViewPanel.SelectedItem);
  166. }
  167. // 更新XAML属性
  168. RaisePropertyChanged("IsDocumentOpen");
  169. }
  170. // Close命令执行时是否可执行方法
  171. private void CloseCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  172. {
  173. e.CanExecute = IsDocumentOpen;
  174. }
  175. // Quit命令执行方法
  176. private void QuitCommand_Executed(object sender, ExecutedRoutedEventArgs e)
  177. {
  178. this.Close();
  179. }
  180. // ImportBRep按钮点击事件处理方法
  181. private void ImportBRep_Click(object sender, RoutedEventArgs e)
  182. {
  183. if (ActiveViewer != null)
  184. {
  185. ActiveViewer.ImportModel(ModelFormat.BREP);
  186. }
  187. }
  188. // ImportIges按钮点击事件处理方法
  189. private void ImportIges_Click(object sender, RoutedEventArgs e)
  190. {
  191. if (ActiveViewer != null)
  192. {
  193. ActiveViewer.ImportModel(ModelFormat.IGES);
  194. }
  195. }
  196. // ImportStep按钮点击事件处理方法
  197. private void ImportStep_Click(object sender, RoutedEventArgs e)
  198. {
  199. if (ActiveViewer != null)
  200. {
  201. ActiveViewer.ImportModel(ModelFormat.STEP);
  202. }
  203. }
  204. // ExportBRep按钮点击事件处理方法
  205. private void ExportBRep_Click(object sender, RoutedEventArgs e)
  206. {
  207. if (ActiveViewer != null)
  208. {
  209. ActiveViewer.ExportModel(ModelFormat.BREP);
  210. }
  211. }
  212. // ExportStep按钮点击事件处理方法
  213. private void ExportStep_Click(object sender, RoutedEventArgs e)
  214. {
  215. if (ActiveViewer != null)
  216. {
  217. ActiveViewer.ExportModel(ModelFormat.STEP);
  218. }
  219. }
  220. // ExportIges按钮点击事件处理方法
  221. private void ExportIges_Click(object sender, RoutedEventArgs e)
  222. {
  223. if (ActiveViewer != null)
  224. {
  225. ActiveViewer.ExportModel(ModelFormat.IGES);
  226. }
  227. }
  228. // ExportStl按钮点击事件处理方法
  229. private void ExportStl_Click(object sender, RoutedEventArgs e)
  230. {
  231. if (ActiveViewer != null)
  232. {
  233. ActiveViewer.ExportModel(ModelFormat.STL);
  234. }
  235. }
  236. // ExportVrml按钮点击事件处理方法
  237. private void ExportVrml_Click(object sender, RoutedEventArgs e)
  238. {
  239. if (ActiveViewer != null)
  240. {
  241. ActiveViewer.ExportModel(ModelFormat.VRML);
  242. }
  243. }
  244. // ExportImage按钮点击事件处理方法
  245. private void ExportImage_Click(object sender, RoutedEventArgs e)
  246. {
  247. if (ActiveViewer != null)
  248. {
  249. ActiveViewer.ExportModel(ModelFormat.IMAGE);
  250. }
  251. }
  252. // FitAll按钮点击事件处理方法
  253. private void FitAllBtn_Click(object sender, RoutedEventArgs e)
  254. {
  255. if (ActiveViewer != null)
  256. {
  257. ActiveViewer.FitAll();
  258. }
  259. }
  260. // ZoomWindow按钮点击事件处理方法
  261. private void ZoomWindowBtn_Click(object sender, RoutedEventArgs e)
  262. {
  263. if (ActiveViewer != null)
  264. {
  265. IsZoomWinEnabled = false;
  266. ActiveViewer.ZoomWindow();
  267. }
  268. }
  269. // DynamicZooming按钮点击事件处理方法
  270. private void DynamicZoomingBtn_Click(object sender, RoutedEventArgs e)
  271. {
  272. if (ActiveViewer != null)
  273. {
  274. ActiveViewer.DynamicZooming();
  275. }
  276. }
  277. // DynamicPanning按钮点击事件处理方法
  278. private void DynamicPanningBtn_Click(object sender, RoutedEventArgs e)
  279. {
  280. if (ActiveViewer != null)
  281. {
  282. ActiveViewer.DynamicPanning();
  283. }
  284. }
  285. // GlobalPanning按钮点击事件处理方法
  286. private void GlobalPanningBtn_Click(object sender, RoutedEventArgs e)
  287. {
  288. if (ActiveViewer != null)
  289. {
  290. ActiveViewer.GlobalPanning();
  291. }
  292. }
  293. // FrontBtn按钮点击事件处理方法
  294. private void FrontBtn_Click(object sender, RoutedEventArgs e)
  295. {
  296. if (ActiveViewer != null)
  297. {
  298. ActiveViewer.FrontView();
  299. }
  300. }
  301. // BackBtn按钮点击事件处理方法
  302. private void BackBtn_Click(object sender, RoutedEventArgs e)
  303. {
  304. if (ActiveViewer != null)
  305. {
  306. ActiveViewer.BackView();
  307. }
  308. }
  309. // TopBtn按钮点击事件处理方法
  310. private void TopBtn_Click(object sender, RoutedEventArgs e)
  311. {
  312. if (ActiveViewer != null)
  313. {
  314. ActiveViewer.TopView();
  315. }
  316. }
  317. // BottomBtn按钮点击事件处理方法
  318. private void BottomBtn_Click(object sender, RoutedEventArgs e)
  319. {
  320. if (ActiveViewer != null)
  321. {
  322. ActiveViewer.BottomView();
  323. }
  324. }
  325. // LeftBtn按钮点击事件处理方法
  326. private void LeftBtn_Click(object sender, RoutedEventArgs e)
  327. {
  328. if (ActiveViewer != null)
  329. {
  330. ActiveViewer.LeftView();
  331. }
  332. }
  333. // RightBtn按钮点击事件处理方法
  334. private void RightBtn_Click(object sender, RoutedEventArgs e)
  335. {
  336. if (ActiveViewer != null)
  337. {
  338. ActiveViewer.RightView();
  339. }
  340. }
  341. // AxoBtn按钮点击事件处理方法
  342. private void AxoBtn_Click(object sender, RoutedEventArgs e)
  343. {
  344. if (ActiveViewer != null)
  345. {
  346. ActiveViewer.AxoView();
  347. }
  348. }
  349. // ResetBtn按钮点击事件处理方法
  350. private void ResetBtn_Click(object sender, RoutedEventArgs e)
  351. {
  352. if (ActiveViewer != null)
  353. {
  354. ActiveViewer.Reset();
  355. }
  356. }
  357. // DynamicRotationBtn按钮点击事件处理方法
  358. private void DynamicRotationBtn_Click(object sender, RoutedEventArgs e)
  359. {
  360. if (ActiveViewer != null)
  361. {
  362. ActiveViewer.DynamicRotation();
  363. }
  364. }
  365. // HiddenOffBtn按钮点击事件处理方法
  366. private void HiddenOffBtn_Click(object sender, RoutedEventArgs e)
  367. {
  368. IsHlrOffPushed = true;
  369. IsHlrOnPushed = false;
  370. if (ActiveViewer != null)
  371. {
  372. ActiveViewer.HiddenOff();
  373. }
  374. }
  375. // HiddenOnBtn按钮点击事件处理方法
  376. private void HiddenOnBtn_Click(object sender, RoutedEventArgs e)
  377. {
  378. IsHlrOffPushed = false;
  379. IsHlrOnPushed = true;
  380. if (ActiveViewer != null)
  381. {
  382. ActiveViewer.HiddenOn();
  383. }
  384. }
  385. // About命令执行方法
  386. private void AboutCommand_Executed(object sender, ExecutedRoutedEventArgs e)
  387. {
  388. AboutDialog aDlg = new AboutDialog();
  389. aDlg.ShowDialog();
  390. }
  391. // Toolbar鼠标进入事件处理方法
  392. private void ToolBar_MouseEnter(object sender, MouseEventArgs e)
  393. {
  394. StatusBarText = "Toolbar";
  395. }
  396. // DocumentToolBar鼠标进入事件处理方法
  397. private void DocumentToolBar_MouseEnter(object sender, MouseEventArgs e)
  398. {
  399. StatusBarText = "Document toolbar";
  400. }
  401. // ViewToolBar鼠标进入事件处理方法
  402. private void ViewToolBar_MouseEnter(object sender, MouseEventArgs e)
  403. {
  404. StatusBarText = "View toolbar";
  405. }
  406. // Toolbar鼠标离开事件处理方法
  407. private void ToolBar_MouseLeave(object sender, MouseEventArgs e)
  408. {
  409. StatusBarText = "";
  410. }
  411. // 缩放完成事件处理方法
  412. public void OnZoomingFinished(object sender, EventArgs e)
  413. {
  414. IsZoomWinEnabled = true;
  415. }
  416. // 是否启用Wireframe属性
  417. public bool IsWireframeEnabled
  418. {
  419. get
  420. {
  421. if (ActiveViewer != null)
  422. {
  423. return ActiveViewer.IsWireframeEnabled;
  424. }
  425. return false;
  426. }
  427. }
  428. // Wireframe按钮点击事件处理方法
  429. private void Wireframe_Click(object sender, RoutedEventArgs e)
  430. {
  431. if (ActiveViewer != null)
  432. {
  433. ActiveViewer.Wireframe();
  434. }
  435. }
  436. // Shading按钮点击事件处理方法
  437. private void Shading_Click(object sender, RoutedEventArgs e)
  438. {
  439. if (ActiveViewer != null)
  440. {
  441. ActiveViewer.Shading();
  442. }
  443. }
  444. // 是否启用Shading属性
  445. public bool IsShadingEnabled
  446. {
  447. get
  448. {
  449. if (ActiveViewer != null)
  450. {
  451. return ActiveViewer.IsShadingEnabled;
  452. }
  453. return false;
  454. }
  455. }
  456. // Color按钮点击事件处理方法
  457. private void Color_Click(object sender, RoutedEventArgs e)
  458. {
  459. if (ActiveViewer != null)
  460. {
  461. ActiveViewer.Color();
  462. }
  463. }
  464. // 是否启用Color属性
  465. public bool IsColorEnabled
  466. {
  467. get
  468. {
  469. if (ActiveViewer != null)
  470. {
  471. return ActiveViewer.IsColorEnabled;
  472. }
  473. return false;
  474. }
  475. }
  476. // Material按钮点击事件处理方法
  477. private void Material_Click(object sender, RoutedEventArgs e)
  478. {
  479. if (ActiveViewer != null)
  480. {
  481. ActiveViewer.Material();
  482. }
  483. }
  484. // 是否启用Material属性
  485. public bool IsMaterialEnabled
  486. {
  487. get
  488. {
  489. if (ActiveViewer != null)
  490. {
  491. return ActiveViewer.IsMaterialEnabled;
  492. }
  493. return false;
  494. }
  495. }
  496. // Transparency按钮点击事件处理方法
  497. private void Transparency_Click(object sender, RoutedEventArgs e)
  498. {
  499. if (ActiveViewer != null)
  500. {
  501. ActiveViewer.Transparency();
  502. }
  503. }
  504. // 是否启用Transparency属性
  505. public bool IsTransparencyEnabled
  506. {
  507. get
  508. {
  509. if (ActiveViewer != null)
  510. {
  511. return ActiveViewer.IsTransparencyEnabled;
  512. }
  513. return false;
  514. }
  515. }
  516. // Delete按钮点击事件处理方法
  517. private void Delete_Click(object sender, RoutedEventArgs e)
  518. {
  519. if (ActiveViewer != null)
  520. {
  521. ActiveViewer.Delete();
  522. }
  523. }
  524. // 是否启用Delete属性
  525. public bool IsDeleteEnabled
  526. {
  527. get
  528. {
  529. if (ActiveViewer != null)
  530. {
  531. return ActiveViewer.IsDeleteEnabled;
  532. }
  533. return false;
  534. }
  535. }
  536. // 工具栏操作可用性改变时更新属性
  537. private void AvaliabiltyOfOperationToolbarChanged()
  538. {
  539. RaisePropertyChanged("IsWireframeEnabled");
  540. RaisePropertyChanged("IsShadingEnabled");
  541. RaisePropertyChanged("IsTransparencyEnabled");
  542. RaisePropertyChanged("IsColorEnabled");
  543. RaisePropertyChanged("IsMaterialEnabled");
  544. RaisePropertyChanged("IsDeleteEnabled");
  545. }
  546. // 操作可用性改变事件处理方法
  547. public void OnAvaliabiltyOfOperationsChanged(object sender, EventArgs e)
  548. {
  549. AvaliabiltyOfOperationToolbarChanged();
  550. }
  551. // 视图更改事件处理方法
  552. private void OnViewerChanged(object sender, SelectionChangedEventArgs e)
  553. {
  554. if (e.RemovedItems.Count > 0)
  555. {
  556. WindowsFormsHost aHost = ((e.RemovedItems[0] as TabItem).Content) as WindowsFormsHost;
  557. if (aHost == null)
  558. {
  559. return;
  560. }
  561. OCCViewer aViewer = aHost.Child as OCCViewer;
  562. if (aViewer != null)
  563. {
  564. aViewer.ZoomingFinished -= new EventHandler(OnZoomingFinished);
  565. aViewer.AvaliabiltyOfOperationsChanged -= new EventHandler(OnAvaliabiltyOfOperationsChanged);
  566. }
  567. }
  568. if (e.AddedItems.Count > 0)
  569. {
  570. WindowsFormsHost aHost = ((e.AddedItems[0] as TabItem).Content) as WindowsFormsHost;
  571. if (aHost == null)
  572. {
  573. return;
  574. }
  575. OCCViewer aViewer = aHost.Child as OCCViewer;
  576. if (aViewer != null)
  577. {
  578. aViewer.ZoomingFinished += new EventHandler(OnZoomingFinished);
  579. aViewer.AvaliabiltyOfOperationsChanged += new EventHandler(OnAvaliabiltyOfOperationsChanged);
  580. }
  581. }
  582. AvaliabiltyOfOperationToolbarChanged();
  583. }
  584. // 键盘按下事件处理方法
  585. private void OnPreviewKeyDown(object sender, KeyEventArgs args)
  586. {
  587. if (ActiveViewer != null)
  588. {
  589. ActiveViewer.OnKeyDown(args.Key);
  590. }
  591. }
  592. // 键盘释放事件处理方法
  593. private void OnPreviewKeyUp(object sender, KeyEventArgs args)
  594. {
  595. if (ActiveViewer != null)
  596. {
  597. ActiveViewer.OnKeyUp();
  598. }
  599. }
  600. }
  601. }

这段代码实现了一个包含多个选项卡的主窗口,每个选项卡中嵌套了Windows Forms控件。窗口中包含了各种命令和事件处理逻辑,用于控制图形视图的显示和交互。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/161875
推荐阅读
相关标签
  

闽ICP备14008679号