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

663 regels
22 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. DoubleAnimation da = new DoubleAnimation(-200, 0, new Duration(TimeSpan.FromMilliseconds(250)));
  391. da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  392. CanvasTranslate.BeginAnimation(TranslateTransform.XProperty, da);
  393. DoubleAnimation daop = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromMilliseconds(250)));
  394. daop.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  395. cav.BeginAnimation(OpacityProperty, daop);
  396. }
  397. /// <summary>
  398. /// 保存
  399. /// </summary>
  400. /// <param name="sender"></param>
  401. /// <param name="e"></param>
  402. private void SaveBtn_Click(object sender, RoutedEventArgs e)
  403. {
  404. SaveFileDialog sfd = new SaveFileDialog();
  405. sfd.Filter = "布局文件|*.lay";
  406. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  407. {
  408. string str = cav.Save();
  409. File.WriteAllText(sfd.FileName, str, Encoding.Unicode);
  410. }
  411. }
  412. /// <summary>
  413. /// 保存页面
  414. /// </summary>
  415. /// <param name="sender"></param>
  416. /// <param name="e"></param>
  417. private void SavePage_Click(object sender, RoutedEventArgs e)
  418. {
  419. try
  420. {
  421. LeftSelectTab.SelectedIndex = 0;
  422. if (viewModel.MenuModel.SelectPageModels == null)
  423. viewModel.MenuModel.SelectPageModels = viewModel.MenuModel.pageModels[0];
  424. viewModel.MenuModel.SelectPageModels.visual?.Children.Clear();
  425. foreach (FrameworkElement element in cav.Children)
  426. {
  427. string xamlText = XamlWriter.Save(element);
  428. FrameworkElement item = XamlReader.Parse(xamlText) as FrameworkElement;
  429. viewModel.MenuModel.SelectPageModels.visual.Children.Add(item);
  430. }
  431. viewModel.UpdatePageBase(viewModel.MenuModel.SelectPageModels);
  432. }
  433. catch (Exception ex)
  434. {
  435. }
  436. }
  437. /// <summary>
  438. /// 选中Tab改变事件
  439. /// </summary>
  440. /// <param name="sender"></param>
  441. /// <param name="e"></param>
  442. private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  443. {
  444. try
  445. {
  446. if (LeftSelectTab.SelectedIndex <= 1 && ReditSeleceTab != null)
  447. {
  448. ReditSeleceTab.SelectedIndex = LeftSelectTab.SelectedIndex;
  449. }
  450. }
  451. catch (Exception ex)
  452. {
  453. }
  454. }
  455. /// <summary>
  456. /// 页选中改变事件
  457. /// </summary>
  458. /// <param name="sender"></param>
  459. /// <param name="e"></param>
  460. private void PageList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  461. {
  462. ReditSeleceTab.SelectedIndex = 0;
  463. }
  464. #endregion
  465. #region 左侧控件栏移动
  466. /// <summary>
  467. /// 移动到右侧
  468. /// </summary>
  469. /// <param name="sender"></param>
  470. /// <param name="e"></param>
  471. private void CtlList_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  472. {
  473. if (CtlList.SelectedItem != null && e.LeftButton == MouseButtonState.Pressed)
  474. {
  475. DragDrop.DoDragDrop(CtlList, CtlList.SelectedItem, System.Windows.DragDropEffects.Copy);
  476. codeEditor.Text = cav.Save();
  477. }
  478. }
  479. /// <summary>
  480. /// 显示代码
  481. /// </summary>
  482. /// <param name="sender"></param>
  483. /// <param name="e"></param>
  484. private void showCode_Click(object sender, RoutedEventArgs e)
  485. {
  486. codeEditor.Text = cav.Save();
  487. }
  488. /// <summary>
  489. /// 编辑
  490. /// </summary>
  491. /// <param name="sender"></param>
  492. /// <param name="e"></param>
  493. private void ToggleButton_Click(object sender, RoutedEventArgs e)
  494. {
  495. try
  496. {
  497. if (sender is ToggleButton)
  498. {
  499. ToggleButton toggle = (ToggleButton)sender;
  500. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  501. if (propertyGridCommand != null)
  502. {
  503. viewModel.Edit(propertyGridCommand);
  504. }
  505. }
  506. }
  507. catch (Exception ex)
  508. {
  509. }
  510. }
  511. /// <summary>
  512. /// 路径资源选择
  513. /// </summary>
  514. /// <param name="sender"></param>
  515. /// <param name="e"></param>
  516. private void LJToggleButton_Click(object sender, RoutedEventArgs e)
  517. {
  518. try
  519. {
  520. if (sender is ToggleButton)
  521. {
  522. ToggleButton toggle = (ToggleButton)sender;
  523. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  524. if (propertyGridCommand != null)
  525. {
  526. viewModel.SelectPath(propertyGridCommand);
  527. }
  528. }
  529. }
  530. catch (Exception ex)
  531. {
  532. }
  533. }
  534. /// <summary>
  535. /// 选择进程
  536. /// </summary>
  537. /// <param name="sender"></param>
  538. /// <param name="e"></param>
  539. private void XZToggleButton_Click(object sender, RoutedEventArgs e)
  540. {
  541. try
  542. {
  543. if (sender is ToggleButton)
  544. {
  545. ToggleButton toggle = (ToggleButton)sender;
  546. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  547. if (propertyGridCommand != null)
  548. {
  549. viewModel.SelectValuePath(propertyGridCommand);
  550. }
  551. }
  552. }
  553. catch (Exception ex)
  554. {
  555. }
  556. }
  557. /// <summary>
  558. /// 设置子控件模板
  559. /// </summary>
  560. /// <param name="sender"></param>
  561. /// <param name="e"></param>
  562. private void ChildToggleButton_Click(object sender, RoutedEventArgs e)
  563. {
  564. try
  565. {
  566. if (sender is ToggleButton)
  567. {
  568. ToggleButton toggle = (ToggleButton)sender;
  569. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  570. if (propertyGridCommand != null)
  571. {
  572. viewModel.ChildEdit(propertyGridCommand);
  573. }
  574. }
  575. }
  576. catch (Exception ex)
  577. {
  578. }
  579. }
  580. /// <summary>
  581. /// 数据绑定
  582. /// </summary>
  583. /// <param name="sender"></param>
  584. /// <param name="e"></param>
  585. private void BingToggleButton_Click(object sender, RoutedEventArgs e)
  586. {
  587. try
  588. {
  589. if (sender is ToggleButton)
  590. {
  591. ToggleButton toggle = (ToggleButton)sender;
  592. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  593. if (propertyGridCommand != null)
  594. {
  595. viewModel.BingEdit(propertyGridCommand);
  596. }
  597. }
  598. }
  599. catch (Exception ex)
  600. {
  601. }
  602. }
  603. /// <summary>
  604. /// 清除绑定
  605. /// </summary>
  606. /// <param name="sender"></param>
  607. /// <param name="e"></param>
  608. private void ClearBingToggleButton_Click(object sender, RoutedEventArgs e)
  609. {
  610. try
  611. {
  612. if (sender is ToggleButton)
  613. {
  614. ToggleButton toggle = (ToggleButton)sender;
  615. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  616. if (propertyGridCommand != null)
  617. {
  618. viewModel.ClearBingEdit(propertyGridCommand);
  619. }
  620. }
  621. }
  622. catch (Exception ex)
  623. {
  624. }
  625. }
  626. #endregion
  627. }
  628. }