终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

655 rindas
21 KiB

  1. using BeDesignerSCADA.Common;
  2. using BeDesignerSCADA.View;
  3. using BeDesignerSCADA.ViewModel;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Controls.Primitives;
  14. using System.Windows.Data;
  15. using System.Windows.Forms;
  16. using System.Windows.Input;
  17. using System.Windows.Markup;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Animation;
  20. namespace BeDesignerSCADA.Controls
  21. {
  22. /// <summary>
  23. /// CanvasPanelNew.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class CanvasPanelNew : System.Windows.Controls.UserControl
  26. {
  27. /// <summary>
  28. /// 界面Model
  29. /// </summary>
  30. public MainViewModelNew viewModel = new MainViewModelNew();
  31. /// <summary>
  32. /// 文件夹监视
  33. /// </summary>
  34. public FileSystemWatcher _watcher;
  35. public CanvasPanelNew(string _Path)
  36. {
  37. InitializeComponent();
  38. EditorHelper.Register<BindingExpression, BindingConvertor>();
  39. viewModel.LayoutsPath = _Path;
  40. this.DataContext = viewModel;
  41. viewModel.Loaded(cav, runCanvas);
  42. //控件加载
  43. Assembly assembly = Assembly.LoadFile($"{System.AppDomain.CurrentDomain.BaseDirectory}\\BPASmartClient.SCADAControl.dll"); //Assembly.GetExecutingAssembly();
  44. List<Type> types = assembly.GetTypes().Where(t => t.GetInterface("IExecutable") != null)?.ToList();
  45. List<Type> typesView = new List<Type>();
  46. viewModel.ControlsNameValues.ToList().OrderBy(o => o.Value)?.ToList().ForEach(par =>
  47. {
  48. Type type= types?.Find(p => p.Name == par.Key);
  49. if (type != null)
  50. typesView.Add(type);
  51. });
  52. CtlList.ItemsSource = typesView;
  53. //读取文件
  54. CreateDir();
  55. FileRead(_Path);
  56. cav.SelectedItemAction += new Action<FrameworkElement>(o => {
  57. if (ReditSeleceTab.SelectedIndex!=1)
  58. ReditSeleceTab.SelectedIndex = 1;
  59. });
  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. public void CreateDir()
  131. {
  132. try
  133. {
  134. //不存在则创建布局文件夹
  135. if (!Directory.Exists($"{System.AppDomain.CurrentDomain.BaseDirectory}Layouts"))
  136. {
  137. Directory.CreateDirectory($"{System.AppDomain.CurrentDomain.BaseDirectory}Layouts");
  138. }
  139. //不存在则创建变量管理器路径 AccessFile
  140. if (!Directory.Exists($"{System.AppDomain.CurrentDomain.BaseDirectory}AccessFile"))
  141. {
  142. Directory.CreateDirectory($"{System.AppDomain.CurrentDomain.BaseDirectory}AccessFile");
  143. }
  144. if (!Directory.Exists($"{System.AppDomain.CurrentDomain.BaseDirectory}AccessFile/JSON"))
  145. {
  146. Directory.CreateDirectory($"{System.AppDomain.CurrentDomain.BaseDirectory}AccessFile/JSON");
  147. }
  148. try
  149. {
  150. this._watcher = new FileSystemWatcher();
  151. _watcher.Path = $"{System.AppDomain.CurrentDomain.BaseDirectory}AccessFile/JSON";
  152. _watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.DirectoryName;
  153. _watcher.IncludeSubdirectories = true;
  154. _watcher.Created += new FileSystemEventHandler(FileWatcher_Created);
  155. _watcher.Changed += new FileSystemEventHandler(FileWatcher_Changed);
  156. _watcher.Deleted += new FileSystemEventHandler(FileWatcher_Deleted);
  157. _watcher.Renamed += new RenamedEventHandler(FileWatcher_Renamed);
  158. Start();
  159. }
  160. catch (Exception ex)
  161. {
  162. Console.WriteLine("Error:" + ex.Message);
  163. }
  164. }
  165. catch (Exception ex)
  166. {
  167. }
  168. }
  169. /// <summary>
  170. /// 监视启动
  171. /// </summary>
  172. public void Start()
  173. {
  174. this._watcher.EnableRaisingEvents = true;
  175. Console.WriteLine("文件监控已经启动...");
  176. ReadFileVlaue();
  177. }
  178. /// <summary>
  179. /// 监视停止
  180. /// </summary>
  181. public void Stop()
  182. {
  183. this._watcher.EnableRaisingEvents = false;
  184. this._watcher.Dispose();
  185. this._watcher = null;
  186. }
  187. /// <summary>
  188. /// 文件夹新增
  189. /// </summary>
  190. /// <param name="sender"></param>
  191. /// <param name="e"></param>
  192. protected void FileWatcher_Created(object sender, FileSystemEventArgs e)
  193. {
  194. Console.WriteLine("新增:" + e.ChangeType + ";" + e.FullPath + ";" + e.Name);
  195. ReadFileVlaue();
  196. }
  197. /// <summary>
  198. /// 文件名变更-文件大小变化
  199. /// </summary>
  200. /// <param name="sender"></param>
  201. /// <param name="e"></param>
  202. protected void FileWatcher_Changed(object sender, FileSystemEventArgs e)
  203. {
  204. Console.WriteLine("变更:" + e.ChangeType + ";" + e.FullPath + ";" + e.Name);
  205. ReadFileVlaue();
  206. }
  207. /// <summary>
  208. /// 删除
  209. /// </summary>
  210. /// <param name="sender"></param>
  211. /// <param name="e"></param>
  212. protected void FileWatcher_Deleted(object sender, FileSystemEventArgs e)
  213. {
  214. Console.WriteLine("删除:" + e.ChangeType + ";" + e.FullPath + ";" + e.Name);
  215. ReadFileVlaue();
  216. }
  217. /// <summary>
  218. /// 文件名
  219. /// </summary>
  220. /// <param name="sender"></param>
  221. /// <param name="e"></param>
  222. protected void FileWatcher_Renamed(object sender, RenamedEventArgs e)
  223. {
  224. Console.WriteLine("重命名: OldPath:{0} NewPath:{1} OldFileName{2} NewFileName:{3}", e.OldFullPath, e.FullPath, e.OldName, e.Name);
  225. ReadFileVlaue();
  226. }
  227. /// <summary>
  228. /// 读取文件变量
  229. /// </summary>
  230. public void ReadFileVlaue()
  231. {
  232. try
  233. {
  234. Directory.CreateDirectory(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AccessFile\\JSON"));
  235. viewModel.VariablePath = AppDomain.CurrentDomain.BaseDirectory + "AccessFile\\JSON\\CommunicationPar.json";
  236. DataBusModel.GetInstance().RefreshVariableManager(viewModel.VariablePath);
  237. }
  238. catch (Exception ex)
  239. {
  240. }
  241. }
  242. #endregion
  243. #region 外部调用事件
  244. /// <summary>
  245. /// 传入变量管理器地址
  246. /// </summary>
  247. /// <param name="path"></param>
  248. public void VariableManagerPath(string path)
  249. {
  250. try
  251. {
  252. viewModel.VariablePath = path;
  253. }
  254. catch (Exception ex)
  255. {
  256. }
  257. }
  258. /// <summary>
  259. /// 保存文件流 布局文件
  260. /// </summary>
  261. public void FileSave()
  262. {
  263. viewModel.SaveAllPageHeaderPath(viewModel.LayoutsPath);
  264. }
  265. /// <summary>
  266. /// 读取文件,加载布局
  267. /// </summary>
  268. /// <param name="path"></param>
  269. public void FileRead(string path)
  270. {
  271. try
  272. {
  273. if (File.Exists(path))
  274. {
  275. viewModel.LoadAllPageHeaderPath(path);
  276. }
  277. }
  278. catch (Exception ex)
  279. {
  280. }
  281. }
  282. /// <summary>
  283. /// 运行程序
  284. /// </summary>
  285. public void Run()
  286. {
  287. try
  288. {
  289. cav.ClearSelection();
  290. runCanvas.Run(cav.Generator());
  291. }
  292. catch (Exception ex)
  293. {
  294. }
  295. }
  296. /// <summary>
  297. /// 停止运行程序
  298. /// </summary>
  299. public void Destory()
  300. {
  301. try
  302. {
  303. runCanvas.Destory();
  304. }
  305. catch (Exception ex)
  306. {
  307. throw;
  308. }
  309. }
  310. #endregion
  311. #region 执行操作事件
  312. /// <summary>
  313. /// 查询输入框
  314. /// </summary>
  315. /// <param name="sender"></param>
  316. /// <param name="e"></param>
  317. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  318. {
  319. try
  320. {
  321. System.Windows.Controls.TextBox textBox = sender as System.Windows.Controls.TextBox;
  322. var info = viewModel.ControlsNameValues?.ToList().Find(par => par.Value.ToLower().StartsWith(textBox.Text.ToLower()));
  323. if (info.Value.Key != null)
  324. {
  325. foreach (var item in CtlList.Items)
  326. {
  327. if ((item as Type).Name== info.Value.Key)
  328. {
  329. CtlList.ScrollIntoView(item);
  330. CtlList.SelectedItem = item;
  331. }
  332. }
  333. }
  334. }
  335. catch (Exception ex)
  336. {
  337. }
  338. }
  339. /// <summary>
  340. /// 运行
  341. /// </summary>
  342. /// <param name="sender"></param>
  343. /// <param name="e"></param>
  344. private void RunBtn_Click(object sender, RoutedEventArgs e)
  345. {
  346. if (sender is System.Windows.Controls.Button btn)
  347. {
  348. if (btn.Tag.ToString() == "运行")
  349. {
  350. cav.ClearSelection();
  351. runCanvas.Run(cav.Generator());
  352. }
  353. else if (btn.Tag.ToString() == "停止")
  354. {
  355. runCanvas.Destory();
  356. }
  357. }
  358. }
  359. /// <summary>
  360. /// 模拟运行
  361. /// </summary>
  362. /// <param name="sender"></param>
  363. /// <param name="e"></param>
  364. RunWindows runWindows = null;
  365. private void MNRunBtn_Click(object sender, RoutedEventArgs e)
  366. {
  367. cav.ClearSelection();
  368. if (runWindows != null)
  369. {
  370. runWindows.Close();
  371. runWindows = null;
  372. }
  373. runWindows = new RunWindows();
  374. runWindows.LoadingData(JsonConvert.DeserializeObject<MenuModel>(JsonConvert.SerializeObject(viewModel.MenuModel)));
  375. runWindows.Show();
  376. }
  377. /// <summary>
  378. /// 加载
  379. /// </summary>
  380. /// <param name="sender"></param>
  381. /// <param name="e"></param>
  382. private void LoadBtn_Click(object sender, RoutedEventArgs e)
  383. {
  384. OpenFileDialog ofd = new OpenFileDialog();
  385. ofd.Filter = "布局文件|*.lay";
  386. if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  387. {
  388. cav.Load(ofd.FileName);
  389. }
  390. }
  391. /// <summary>
  392. /// 保存
  393. /// </summary>
  394. /// <param name="sender"></param>
  395. /// <param name="e"></param>
  396. private void SaveBtn_Click(object sender, RoutedEventArgs e)
  397. {
  398. SaveFileDialog sfd = new SaveFileDialog();
  399. sfd.Filter = "布局文件|*.lay";
  400. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  401. {
  402. string str = cav.Save();
  403. File.WriteAllText(sfd.FileName, str, Encoding.Unicode);
  404. }
  405. }
  406. /// <summary>
  407. /// 保存页面
  408. /// </summary>
  409. /// <param name="sender"></param>
  410. /// <param name="e"></param>
  411. private void SavePage_Click(object sender, RoutedEventArgs e)
  412. {
  413. try
  414. {
  415. LeftSelectTab.SelectedIndex = 0;
  416. if (viewModel.MenuModel.SelectPageModels == null)
  417. viewModel.MenuModel.SelectPageModels = viewModel.MenuModel.pageModels[0];
  418. viewModel.MenuModel.SelectPageModels.visual?.Children.Clear();
  419. foreach (FrameworkElement element in cav.Children)
  420. {
  421. string xamlText = XamlWriter.Save(element);
  422. FrameworkElement item = XamlReader.Parse(xamlText) as FrameworkElement;
  423. viewModel.MenuModel.SelectPageModels.visual.Children.Add(item);
  424. }
  425. viewModel.UpdatePageBase(viewModel.MenuModel.SelectPageModels);
  426. }
  427. catch (Exception ex)
  428. {
  429. }
  430. }
  431. /// <summary>
  432. /// 选中Tab改变事件
  433. /// </summary>
  434. /// <param name="sender"></param>
  435. /// <param name="e"></param>
  436. private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  437. {
  438. try
  439. {
  440. if (LeftSelectTab.SelectedIndex <= 1 && ReditSeleceTab != null)
  441. {
  442. ReditSeleceTab.SelectedIndex = LeftSelectTab.SelectedIndex;
  443. }
  444. }
  445. catch (Exception ex)
  446. {
  447. }
  448. }
  449. /// <summary>
  450. /// 页选中改变事件
  451. /// </summary>
  452. /// <param name="sender"></param>
  453. /// <param name="e"></param>
  454. private void PageList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  455. {
  456. ReditSeleceTab.SelectedIndex = 0;
  457. }
  458. #endregion
  459. #region 左侧控件栏移动
  460. /// <summary>
  461. /// 移动到右侧
  462. /// </summary>
  463. /// <param name="sender"></param>
  464. /// <param name="e"></param>
  465. private void CtlList_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  466. {
  467. if (CtlList.SelectedItem != null && e.LeftButton == MouseButtonState.Pressed)
  468. {
  469. DragDrop.DoDragDrop(CtlList, CtlList.SelectedItem, System.Windows.DragDropEffects.Copy);
  470. codeEditor.Text = cav.Save();
  471. }
  472. }
  473. /// <summary>
  474. /// 显示代码
  475. /// </summary>
  476. /// <param name="sender"></param>
  477. /// <param name="e"></param>
  478. private void showCode_Click(object sender, RoutedEventArgs e)
  479. {
  480. codeEditor.Text = cav.Save();
  481. }
  482. /// <summary>
  483. /// 编辑
  484. /// </summary>
  485. /// <param name="sender"></param>
  486. /// <param name="e"></param>
  487. private void ToggleButton_Click(object sender, RoutedEventArgs e)
  488. {
  489. try
  490. {
  491. if (sender is ToggleButton)
  492. {
  493. ToggleButton toggle = (ToggleButton)sender;
  494. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  495. if (propertyGridCommand != null)
  496. {
  497. viewModel.Edit(propertyGridCommand);
  498. }
  499. }
  500. }
  501. catch (Exception ex)
  502. {
  503. }
  504. }
  505. /// <summary>
  506. /// 路径资源选择
  507. /// </summary>
  508. /// <param name="sender"></param>
  509. /// <param name="e"></param>
  510. private void LJToggleButton_Click(object sender, RoutedEventArgs e)
  511. {
  512. try
  513. {
  514. if (sender is ToggleButton)
  515. {
  516. ToggleButton toggle = (ToggleButton)sender;
  517. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  518. if (propertyGridCommand != null)
  519. {
  520. viewModel.SelectPath(propertyGridCommand);
  521. }
  522. }
  523. }
  524. catch (Exception ex)
  525. {
  526. }
  527. }
  528. /// <summary>
  529. /// 选择进程
  530. /// </summary>
  531. /// <param name="sender"></param>
  532. /// <param name="e"></param>
  533. private void XZToggleButton_Click(object sender, RoutedEventArgs e)
  534. {
  535. try
  536. {
  537. if (sender is ToggleButton)
  538. {
  539. ToggleButton toggle = (ToggleButton)sender;
  540. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  541. if (propertyGridCommand != null)
  542. {
  543. viewModel.SelectValuePath(propertyGridCommand);
  544. }
  545. }
  546. }
  547. catch (Exception ex)
  548. {
  549. }
  550. }
  551. /// <summary>
  552. /// 设置子控件模板
  553. /// </summary>
  554. /// <param name="sender"></param>
  555. /// <param name="e"></param>
  556. private void ChildToggleButton_Click(object sender, RoutedEventArgs e)
  557. {
  558. try
  559. {
  560. if (sender is ToggleButton)
  561. {
  562. ToggleButton toggle = (ToggleButton)sender;
  563. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  564. if (propertyGridCommand != null)
  565. {
  566. viewModel.ChildEdit(propertyGridCommand);
  567. }
  568. }
  569. }
  570. catch (Exception ex)
  571. {
  572. }
  573. }
  574. /// <summary>
  575. /// 数据绑定
  576. /// </summary>
  577. /// <param name="sender"></param>
  578. /// <param name="e"></param>
  579. private void BingToggleButton_Click(object sender, RoutedEventArgs e)
  580. {
  581. try
  582. {
  583. if (sender is ToggleButton)
  584. {
  585. ToggleButton toggle = (ToggleButton)sender;
  586. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  587. if (propertyGridCommand != null)
  588. {
  589. viewModel.BingEdit(propertyGridCommand);
  590. }
  591. }
  592. }
  593. catch (Exception ex)
  594. {
  595. }
  596. }
  597. /// <summary>
  598. /// 清除绑定
  599. /// </summary>
  600. /// <param name="sender"></param>
  601. /// <param name="e"></param>
  602. private void ClearBingToggleButton_Click(object sender, RoutedEventArgs e)
  603. {
  604. try
  605. {
  606. if (sender is ToggleButton)
  607. {
  608. ToggleButton toggle = (ToggleButton)sender;
  609. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  610. if (propertyGridCommand != null)
  611. {
  612. viewModel.ClearBingEdit(propertyGridCommand);
  613. }
  614. }
  615. }
  616. catch (Exception ex)
  617. {
  618. }
  619. }
  620. #endregion
  621. }
  622. }