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

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