终端一体化运控平台
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.
 
 
 

399 lines
14 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. MainViewModelNew viewModel = new MainViewModelNew();
  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="path"></param>
  201. public void VariableManagerPath(string path)
  202. {
  203. try
  204. {
  205. viewModel.VariablePath = path;
  206. }
  207. catch (Exception ex)
  208. {
  209. }
  210. }
  211. /// <summary>
  212. /// 保存
  213. /// </summary>
  214. /// <param name="sender"></param>
  215. /// <param name="e"></param>
  216. private void SaveBtn_Click(object sender, RoutedEventArgs e)
  217. {
  218. string str = cav.Save();
  219. File.WriteAllText(Path, str, Encoding.Unicode);
  220. }
  221. /// <summary>
  222. /// 模拟消息发送
  223. /// </summary>
  224. /// <param name="sender"></param>
  225. /// <param name="e"></param>
  226. private void MNBtn_Click(object sender, RoutedEventArgs e)
  227. {
  228. }
  229. #endregion
  230. #region 左侧控件栏移动
  231. /// <summary>
  232. /// 移动到右侧
  233. /// </summary>
  234. /// <param name="sender"></param>
  235. /// <param name="e"></param>
  236. private void CtlList_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  237. {
  238. if (CtlList.SelectedItem != null && e.LeftButton == MouseButtonState.Pressed)
  239. {
  240. DragDrop.DoDragDrop(CtlList, CtlList.SelectedItem, System.Windows.DragDropEffects.Copy);
  241. codeEditor.Text = cav.Save();
  242. }
  243. }
  244. /// <summary>
  245. /// 显示代码
  246. /// </summary>
  247. /// <param name="sender"></param>
  248. /// <param name="e"></param>
  249. private void showCode_Click(object sender, RoutedEventArgs e)
  250. {
  251. codeEditor.Text = cav.Save();
  252. }
  253. /// <summary>
  254. /// 编辑
  255. /// </summary>
  256. /// <param name="sender"></param>
  257. /// <param name="e"></param>
  258. private void ToggleButton_Click(object sender, RoutedEventArgs e)
  259. {
  260. try
  261. {
  262. if (sender is ToggleButton)
  263. {
  264. ToggleButton toggle = (ToggleButton)sender;
  265. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  266. if (propertyGridCommand != null)
  267. {
  268. viewModel.Edit(propertyGridCommand);
  269. }
  270. }
  271. }
  272. catch (Exception ex)
  273. {
  274. }
  275. }
  276. /// <summary>
  277. /// 路径资源选择
  278. /// </summary>
  279. /// <param name="sender"></param>
  280. /// <param name="e"></param>
  281. private void LJToggleButton_Click(object sender, RoutedEventArgs e)
  282. {
  283. try
  284. {
  285. if (sender is ToggleButton)
  286. {
  287. ToggleButton toggle = (ToggleButton)sender;
  288. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  289. if (propertyGridCommand != null)
  290. {
  291. viewModel.SelectPath(propertyGridCommand);
  292. }
  293. }
  294. }
  295. catch (Exception ex)
  296. {
  297. }
  298. }
  299. /// <summary>
  300. /// 变量选择
  301. /// </summary>
  302. /// <param name="sender"></param>
  303. /// <param name="e"></param>
  304. private void ComboBoxValue_TextChanged(object sender, TextChangedEventArgs e)
  305. {
  306. try
  307. {
  308. if (sender is System.Windows.Controls.ComboBox)
  309. {
  310. System.Windows.Controls.ComboBox toggle = (System.Windows.Controls.ComboBox)sender;
  311. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  312. if (toggle.Tag != null && !string.IsNullOrEmpty(toggle.Text))
  313. propertyGridCommand.Value = "{" + $"Binding {toggle.Tag}.{toggle.Text}" + "}";
  314. }
  315. }
  316. catch (Exception ex)
  317. {
  318. }
  319. }
  320. /// <summary>
  321. /// 变量下拉框打开事件
  322. /// </summary>
  323. /// <param name="sender"></param>
  324. /// <param name="e"></param>
  325. private void valuebox_DropDownOpened(object sender, EventArgs e)
  326. {
  327. try
  328. {
  329. viewModel.DevValueList = new System.Collections.ObjectModel.ObservableCollection<string>();
  330. if (sender is System.Windows.Controls.ComboBox)
  331. {
  332. System.Windows.Controls.ComboBox toggle = (System.Windows.Controls.ComboBox)sender;
  333. if (toggle.Tag == null) return;
  334. CommunicationModel mode=viewModel.communicationPar?.CommunicationDevices?.ToList().Find(par => par.DeviceName == toggle.Tag.ToString());
  335. if (mode != null)
  336. {
  337. mode?.VarTableModels?.ToList().ForEach(par => { viewModel.DevValueList.Add(par.VarName); });
  338. }
  339. }
  340. }
  341. catch (Exception ex)
  342. {
  343. }
  344. }
  345. /// <summary>
  346. /// 设备名称选择
  347. /// </summary>
  348. /// <param name="sender"></param>
  349. /// <param name="e"></param>
  350. private void ComboBoxName_TextChanged(object sender, TextChangedEventArgs e)
  351. {
  352. try
  353. {
  354. if (sender is System.Windows.Controls.ComboBox)
  355. {
  356. System.Windows.Controls.ComboBox toggle = (System.Windows.Controls.ComboBox)sender;
  357. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  358. if (toggle.Tag != null && !string.IsNullOrEmpty(toggle.Text))
  359. propertyGridCommand.Value = "{" + $"Binding {toggle.Text}.{toggle.Tag}" + "}";
  360. else if (!string.IsNullOrEmpty(toggle.Text))
  361. propertyGridCommand.Value = "{" + $"Binding {toggle.Text}." + "}";
  362. }
  363. }
  364. catch (Exception ex)
  365. {
  366. }
  367. }
  368. /// <summary>
  369. /// 设备名称下拉框打开事件
  370. /// </summary>
  371. /// <param name="sender"></param>
  372. /// <param name="e"></param>
  373. private void namebox_DropDownOpened(object sender, EventArgs e)
  374. {
  375. viewModel.DevNameList = new System.Collections.ObjectModel.ObservableCollection<string>();
  376. viewModel.communicationPar?.CommunicationDevices?.ToList().ForEach(x => { viewModel.DevNameList.Add(x.DeviceName); });
  377. }
  378. #endregion
  379. }
  380. }