终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

360 lines
12 KiB

  1. using BeDesignerSCADA.ViewModel;
  2. using BeDesignerSCADA.ViewModel;
  3. using BPASmart.Model;
  4. using BPASmartClient.Compiler;
  5. using BPASmartClient.DATABUS;
  6. using BPASmartClient.MessageName;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Controls.Primitives;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Forms;
  20. using System.Windows.Input;
  21. using System.Windows.Media;
  22. using System.Windows.Media.Animation;
  23. using System.Windows.Media.Imaging;
  24. using System.Windows.Navigation;
  25. using System.Windows.Shapes;
  26. namespace BeDesignerSCADA.Controls
  27. {
  28. /// <summary>
  29. /// MainCanvasPanel.xaml 的交互逻辑
  30. /// </summary>
  31. public partial class MainCanvasPanel : System.Windows.Controls.UserControl
  32. {
  33. /// <summary>
  34. /// 布局文件路径
  35. /// </summary>
  36. public string Path=string.Empty;
  37. MainViewModel viewModel = new MainViewModel();
  38. public MainCanvasPanel(string _Path)
  39. {
  40. InitializeComponent();
  41. Path= _Path;
  42. this.DataContext = viewModel;
  43. viewModel.Loaded(cav, runCanvas);
  44. //控件加载
  45. Assembly assembly = Assembly.LoadFile($"{System.AppDomain.CurrentDomain.BaseDirectory}\\BPASmartClient.SCADAControl.dll"); //Assembly.GetExecutingAssembly();
  46. var controls = assembly.GetTypes().Where(t => t.GetInterface("IExecutable") != null).OrderBy(o => o.Name);
  47. CtlList.ItemsSource = controls;
  48. //读取文件
  49. FileRead(_Path);
  50. }
  51. //public MainCanvasPanel()
  52. //{
  53. // InitializeComponent();
  54. // this.DataContext = viewModel;
  55. // viewModel.Loaded(cav, runCanvas);
  56. // //控件加载
  57. // Assembly assembly = Assembly.LoadFile($"{System.AppDomain.CurrentDomain.BaseDirectory}\\BPASmartClient.SCADAControl.dll"); //Assembly.GetExecutingAssembly();
  58. // var controls = assembly.GetTypes().Where(t => t.GetInterface("IExecutable") != null).OrderBy(o => o.Name);
  59. // CtlList.ItemsSource = controls;
  60. //}
  61. #region 位置调整
  62. /// <summary>
  63. /// 左对齐
  64. /// </summary>
  65. /// <param name="sender"></param>
  66. /// <param name="e"></param>
  67. private void AglinLeftBtn_Click(object sender, RoutedEventArgs e)
  68. {
  69. cav.AlignLeft();
  70. }
  71. /// <summary>
  72. /// 底部对齐
  73. /// </summary>
  74. /// <param name="sender"></param>
  75. /// <param name="e"></param>
  76. private void AglinBottomBtn_Click(object sender, RoutedEventArgs e)
  77. {
  78. cav.AlignBottom();
  79. }
  80. /// <summary>
  81. /// 顶部对齐
  82. /// </summary>
  83. /// <param name="sender"></param>
  84. /// <param name="e"></param>
  85. private void AglinTopBtn_Click(object sender, RoutedEventArgs e)
  86. {
  87. cav.AlignTop();
  88. }
  89. /// <summary>
  90. /// 右对齐
  91. /// </summary>
  92. /// <param name="sender"></param>
  93. /// <param name="e"></param>
  94. private void AglinRightBtn_Click(object sender, RoutedEventArgs e)
  95. {
  96. cav.AlignRight();
  97. }
  98. /// <summary>
  99. /// 居中
  100. /// </summary>
  101. /// <param name="sender"></param>
  102. /// <param name="e"></param>
  103. private void AglinCenterBtn_Click(object sender, RoutedEventArgs e)
  104. {
  105. cav.AlignCenter();
  106. }
  107. /// <summary>
  108. /// 垂直分布
  109. /// </summary>
  110. /// <param name="sender"></param>
  111. /// <param name="e"></param>
  112. private void VerticalLayoutBtn_Click(object sender, RoutedEventArgs e)
  113. {
  114. cav.VertialLayout();
  115. }
  116. /// <summary>
  117. /// 水平分布
  118. /// </summary>
  119. /// <param name="sender"></param>
  120. /// <param name="e"></param>
  121. private void HorizontalLayoutBtn_Click(object sender, RoutedEventArgs e)
  122. {
  123. cav.HorizontalLayout();
  124. }
  125. #endregion
  126. #region 其他事件操作
  127. /// <summary>
  128. /// 运行
  129. /// </summary>
  130. /// <param name="sender"></param>
  131. /// <param name="e"></param>
  132. private void RunBtn_Click(object sender, RoutedEventArgs e)
  133. {
  134. if (sender is System.Windows.Controls.Button btn)
  135. {
  136. if (btn.Tag.ToString() == "运行")
  137. {
  138. cav.ClearSelection();
  139. runCanvas.Run(cav.Generator());
  140. }
  141. else if (btn.Tag.ToString() == "停止")
  142. {
  143. runCanvas.Destory();
  144. }
  145. }
  146. }
  147. /// <summary>
  148. /// 加载
  149. /// </summary>
  150. /// <param name="sender"></param>
  151. /// <param name="e"></param>
  152. private void LoadBtn_Click(object sender, RoutedEventArgs e)
  153. {
  154. OpenFileDialog ofd = new OpenFileDialog();
  155. ofd.Filter = "布局文件|*.lay";
  156. if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  157. {
  158. cav.Load(ofd.FileName);
  159. }
  160. DoubleAnimation da = new DoubleAnimation(-200, 0, new Duration(TimeSpan.FromMilliseconds(250)));
  161. da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  162. CanvasTranslate.BeginAnimation(TranslateTransform.XProperty, da);
  163. DoubleAnimation daop = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromMilliseconds(250)));
  164. daop.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  165. cav.BeginAnimation(OpacityProperty, daop);
  166. }
  167. /// <summary>
  168. /// 读取文件
  169. /// </summary>
  170. /// <param name="path"></param>
  171. public void FileRead(string path)
  172. {
  173. try
  174. {
  175. if (File.Exists(path))
  176. {
  177. cav.Load(path);
  178. DoubleAnimation da = new DoubleAnimation(-200, 0, new Duration(TimeSpan.FromMilliseconds(250)));
  179. da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  180. CanvasTranslate.BeginAnimation(TranslateTransform.XProperty, da);
  181. DoubleAnimation daop = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromMilliseconds(250)));
  182. daop.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  183. cav.BeginAnimation(OpacityProperty, daop);
  184. }
  185. }
  186. catch (Exception ex)
  187. {
  188. }
  189. }
  190. /// <summary>
  191. /// 保存文件
  192. /// </summary>
  193. public void FileSave()
  194. {
  195. SaveBtn_Click(null, null);
  196. }
  197. /// <summary>
  198. /// 保存
  199. /// </summary>
  200. /// <param name="sender"></param>
  201. /// <param name="e"></param>
  202. private void SaveBtn_Click(object sender, RoutedEventArgs e)
  203. {
  204. string str = cav.Save();
  205. File.WriteAllText(Path, str, Encoding.Unicode);
  206. }
  207. /// <summary>
  208. /// 模拟消息发送
  209. /// </summary>
  210. /// <param name="sender"></param>
  211. /// <param name="e"></param>
  212. private void MNBtn_Click(object sender, RoutedEventArgs e)
  213. {
  214. }
  215. #endregion
  216. #region 左侧控件栏移动
  217. /// <summary>
  218. /// 移动到右侧
  219. /// </summary>
  220. /// <param name="sender"></param>
  221. /// <param name="e"></param>
  222. private void CtlList_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  223. {
  224. if (CtlList.SelectedItem != null && e.LeftButton == MouseButtonState.Pressed)
  225. {
  226. DragDrop.DoDragDrop(CtlList, CtlList.SelectedItem, System.Windows.DragDropEffects.Copy);
  227. codeEditor.Text = cav.Save();
  228. }
  229. }
  230. /// <summary>
  231. /// 显示代码
  232. /// </summary>
  233. /// <param name="sender"></param>
  234. /// <param name="e"></param>
  235. private void showCode_Click(object sender, RoutedEventArgs e)
  236. {
  237. codeEditor.Text = cav.Save();
  238. }
  239. /// <summary>
  240. /// 编辑
  241. /// </summary>
  242. /// <param name="sender"></param>
  243. /// <param name="e"></param>
  244. private void ToggleButton_Click(object sender, RoutedEventArgs e)
  245. {
  246. try
  247. {
  248. if (sender is ToggleButton)
  249. {
  250. ToggleButton toggle = (ToggleButton)sender;
  251. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  252. if (propertyGridCommand != null)
  253. {
  254. viewModel.Edit(propertyGridCommand);
  255. }
  256. }
  257. }
  258. catch (Exception ex)
  259. {
  260. }
  261. }
  262. /// <summary>
  263. /// 变量选择
  264. /// </summary>
  265. /// <param name="sender"></param>
  266. /// <param name="e"></param>
  267. private void ComboBoxValue_TextChanged(object sender, TextChangedEventArgs e)
  268. {
  269. try
  270. {
  271. if (sender is System.Windows.Controls.ComboBox)
  272. {
  273. System.Windows.Controls.ComboBox toggle = (System.Windows.Controls.ComboBox)sender;
  274. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  275. if (toggle.Tag != null && !string.IsNullOrEmpty(toggle.Text))
  276. propertyGridCommand.Value = "{" + $"Binding {toggle.Tag}.{toggle.Text}" + "}";
  277. }
  278. }
  279. catch (Exception ex)
  280. {
  281. }
  282. }
  283. /// <summary>
  284. /// 变量下拉框打开事件
  285. /// </summary>
  286. /// <param name="sender"></param>
  287. /// <param name="e"></param>
  288. private void valuebox_DropDownOpened(object sender, EventArgs e)
  289. {
  290. try
  291. {
  292. viewModel.DevValueList = new System.Collections.ObjectModel.ObservableCollection<string>();
  293. if (sender is System.Windows.Controls.ComboBox)
  294. {
  295. System.Windows.Controls.ComboBox toggle = (System.Windows.Controls.ComboBox)sender;
  296. if (toggle.Tag == null) return;
  297. if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(toggle.Tag.ToString()))
  298. {
  299. Class_DataBus.GetInstance().Dic_DeviceData[toggle.Tag.ToString()].Keys?.ToList().ForEach(key => { viewModel.DevValueList.Add(key); });
  300. }
  301. }
  302. }
  303. catch (Exception ex)
  304. {
  305. }
  306. }
  307. /// <summary>
  308. /// 设备名称选择
  309. /// </summary>
  310. /// <param name="sender"></param>
  311. /// <param name="e"></param>
  312. private void ComboBoxName_TextChanged(object sender, TextChangedEventArgs e)
  313. {
  314. try
  315. {
  316. if (sender is System.Windows.Controls.ComboBox)
  317. {
  318. System.Windows.Controls.ComboBox toggle = (System.Windows.Controls.ComboBox)sender;
  319. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  320. if (toggle.Tag != null && !string.IsNullOrEmpty(toggle.Text))
  321. propertyGridCommand.Value = "{" + $"Binding {toggle.Text}.{toggle.Tag}" + "}";
  322. else if (!string.IsNullOrEmpty(toggle.Text))
  323. propertyGridCommand.Value = "{" + $"Binding {toggle.Text}." + "}";
  324. }
  325. }
  326. catch (Exception ex)
  327. {
  328. }
  329. }
  330. /// <summary>
  331. /// 设备名称下拉框打开事件
  332. /// </summary>
  333. /// <param name="sender"></param>
  334. /// <param name="e"></param>
  335. private void namebox_DropDownOpened(object sender, EventArgs e)
  336. {
  337. viewModel.DevNameList = new System.Collections.ObjectModel.ObservableCollection<string>();
  338. Class_DataBus.GetInstance().Dic_DeviceData.Keys?.ToList().ForEach(key => { viewModel.DevNameList.Add(key); });
  339. }
  340. #endregion
  341. }
  342. }