|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- using BeDesignerSCADA.Common;
- using BeDesignerSCADA.View;
- using BeDesignerSCADA.ViewModel;
- using BPASmart.Model;
- using BPASmartClient.Compiler;
- using BPASmartClient.DATABUS;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Forms;
- using System.Windows.Input;
- using System.Windows.Markup;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
-
- namespace BeDesignerSCADA.Controls
- {
- /// <summary>
- /// MainCanvasPanel.xaml 的交互逻辑
- /// </summary>
- public partial class MainCanvasPanel : System.Windows.Controls.UserControl
- {
- /// <summary>
- /// 布局文件路径
- /// </summary>
- public string Path = string.Empty;
- List<Type> controls = new List<Type>();
- MainViewModelNew viewModel = new MainViewModelNew();
- public MainCanvasPanel(string _Path)
- {
- InitializeComponent();
- EditorHelper.Register<BindingExpression, BindingConvertor>();
- Path = _Path;
- viewModel.LayoutsPath=Path;
- this.DataContext = viewModel;
- viewModel.Loaded(cav, runCanvas);
- //控件加载
- Assembly assembly = Assembly.LoadFile($"{System.AppDomain.CurrentDomain.BaseDirectory}\\BPASmartClient.SCADAControl.dll"); //Assembly.GetExecutingAssembly();
- controls = assembly.GetTypes().Where(t => t.GetInterface("IExecutable") != null).OrderBy(o => o.Name)?.ToList();
- //controls.Add(new Line().GetType());
- CtlList.ItemsSource = controls;
- //读取文件
- FileRead(_Path);
- }
- #region 位置调整
- /// <summary>
- /// 左对齐
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AglinLeftBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.AlignLeft();
- }
- /// <summary>
- /// 底部对齐
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AglinBottomBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.AlignBottom();
- }
- /// <summary>
- /// 顶部对齐
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AglinTopBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.AlignTop();
- }
- /// <summary>
- /// 右对齐
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AglinRightBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.AlignRight();
- }
- /// <summary>
- /// 居中
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AglinCenterBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.AlignCenter();
- }
- /// <summary>
- /// 垂直分布
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void VerticalLayoutBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.VertialLayout();
- }
- /// <summary>
- /// 水平分布
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void HorizontalLayoutBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.HorizontalLayout();
- }
- #endregion
-
- #region 其他事件操作
- /// <summary>
- /// 传入变量管理器地址
- /// </summary>
- /// <param name="path"></param>
- private void LoadBtnValue_Click(object sender, RoutedEventArgs e)
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = "变量管理器文件|*.json";
- if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- viewModel.VariablePath = ofd.FileName;
- bool isSucess= DataBusModel.GetInstance().RefreshVariableManager(ofd.FileName);
- System.Windows.MessageBox.Show(isSucess ? "加载成功!" : "加载失败!");
- }
- }
- /// <summary>
- /// 运行
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void RunBtn_Click(object sender, RoutedEventArgs e)
- {
- if (sender is System.Windows.Controls.Button btn)
- {
- if (btn.Tag.ToString() == "运行")
- {
- cav.ClearSelection();
- runCanvas.Run(cav.Generator());
- }
- else if (btn.Tag.ToString() == "停止")
- {
- runCanvas.Destory();
- }
- }
- }
- /// <summary>
- /// 模拟运行
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MNRunBtn_Click(object sender, RoutedEventArgs e)
- {
- cav.ClearSelection();
- RunWindowsLao runWindows = new RunWindowsLao();
- runWindows.LoadingData(cav.Generator());
- runWindows.Show();
- }
- /// <summary>
- /// 加载
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void LoadBtn_Click(object sender, RoutedEventArgs e)
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = "布局文件|*.lay";
- if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- cav.Load(ofd.FileName);
- }
-
- DoubleAnimation da = new DoubleAnimation(-200, 0, new Duration(TimeSpan.FromMilliseconds(250)));
- da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
- CanvasTranslate.BeginAnimation(TranslateTransform.XProperty, da);
-
- DoubleAnimation daop = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromMilliseconds(250)));
- daop.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
- cav.BeginAnimation(OpacityProperty, daop);
- }
- /// <summary>
- /// 读取文件
- /// </summary>
- /// <param name="path"></param>
- public void FileRead(string path)
- {
- try
- {
- if (File.Exists(path))
- {
- cav.Load(path);
-
- DoubleAnimation da = new DoubleAnimation(-200, 0, new Duration(TimeSpan.FromMilliseconds(250)));
- da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
- CanvasTranslate.BeginAnimation(TranslateTransform.XProperty, da);
-
- DoubleAnimation daop = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromMilliseconds(250)));
- daop.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
- cav.BeginAnimation(OpacityProperty, daop);
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 根据流加载数据
- /// </summary>
- public void LoadFrameworkElement(UIElementCollection frameworks)
- {
- try
- {
- cav.Children.Clear();
- foreach (FrameworkElement element in frameworks)
- {
- string xamlText = XamlWriter.Save(element);
- FrameworkElement item = XamlReader.Parse(xamlText) as FrameworkElement;
- cav.Children.Add(item);
- }
- cav.SelectedItems?.Clear();
- }
- catch (Exception ex)
- {
-
- throw;
- }
- }
- /// <summary>
- /// 获取所有控件
- /// </summary>
- /// <returns></returns>
- public List<FrameworkElement> GetChildren()
- {
- List<FrameworkElement> frameworks = new List<FrameworkElement>();
- foreach (FrameworkElement child in cav.Children)
- {
- string xamlText = XamlWriter.Save(child);
- FrameworkElement item = XamlReader.Parse(xamlText) as FrameworkElement;
- frameworks.Add(item);
- }
- return frameworks;
- }
- /// <summary>
- /// 保存文件
- /// </summary>
- public void FileSave()
- {
- SaveBtn_Click(null, null);
- }
- /// <summary>
- /// 传入变量管理器地址
- /// </summary>
- /// <param name="path"></param>
- public void VariableManagerPath(string path)
- {
- try
- {
- viewModel.VariablePath = path;
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 运行程序
- /// </summary>
- public void Run()
- {
- try
- {
- cav.ClearSelection();
- runCanvas.Run(cav.Generator());
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 停止运行程序
- /// </summary>
- public void Destory()
- {
- try
- {
- runCanvas.Destory();
- }
- catch (Exception ex)
- {
-
- throw;
- }
- }
- /// <summary>
- /// 保存
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void SaveBtn_Click(object sender, RoutedEventArgs e)
- {
- if (File.Exists(Path))
- {
- string str = cav.Save();
- File.WriteAllText(Path, str, Encoding.Unicode);
- }
- else
- {
- SaveFileDialog sfd = new SaveFileDialog();
- sfd.Filter = "布局文件|*.lay";
-
- if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- string str = cav.Save();
- File.WriteAllText(sfd.FileName, str, Encoding.Unicode);
- }
- }
- }
- /// <summary>
- /// 模拟消息发送
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MNBtn_Click(object sender, RoutedEventArgs e)
- {
-
- }
- #endregion
-
- #region 左侧控件栏移动
- /// <summary>
- /// 移动到右侧
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void CtlList_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
- {
- if (CtlList.SelectedItem != null && e.LeftButton == MouseButtonState.Pressed)
- {
- DragDrop.DoDragDrop(CtlList, CtlList.SelectedItem, System.Windows.DragDropEffects.Copy);
- codeEditor.Text = cav.Save();
- }
- }
- /// <summary>
- /// 显示代码
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void showCode_Click(object sender, RoutedEventArgs e)
- {
- codeEditor.Text = cav.Save();
- }
- /// <summary>
- /// 编辑
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ToggleButton_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (sender is ToggleButton)
- {
- ToggleButton toggle = (ToggleButton)sender;
- Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
- if (propertyGridCommand != null)
- {
- viewModel.Edit(propertyGridCommand);
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 路径资源选择
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void LJToggleButton_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (sender is ToggleButton)
- {
- ToggleButton toggle = (ToggleButton)sender;
- Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
- if (propertyGridCommand != null)
- {
- viewModel.SelectPath(propertyGridCommand);
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 选择进程
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void XZToggleButton_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (sender is ToggleButton)
- {
- ToggleButton toggle = (ToggleButton)sender;
- Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
- if (propertyGridCommand != null)
- {
- viewModel.SelectValuePath(propertyGridCommand);
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 设置子控件模板
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ChildToggleButton_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (sender is ToggleButton)
- {
- ToggleButton toggle = (ToggleButton)sender;
- Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
- if (propertyGridCommand != null)
- {
- viewModel.ChildEdit(propertyGridCommand);
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 数据绑定
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BingToggleButton_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (sender is ToggleButton)
- {
-
- ToggleButton toggle = (ToggleButton)sender;
- Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
- if (propertyGridCommand != null)
- {
- viewModel.BingEdit(propertyGridCommand);
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- /// <summary>
- /// 清除绑定
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ClearBingToggleButton_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (sender is ToggleButton)
- {
-
- ToggleButton toggle = (ToggleButton)sender;
- Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
- if (propertyGridCommand != null)
- {
- viewModel.ClearBingEdit(propertyGridCommand);
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- #endregion
-
-
-
- }
- }
|