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

675 lines
22 KiB

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