WdAbHelper2.xaml.cs 3.91 KB
using Misc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// Window_AHelper.xaml 的交互逻辑
    /// </summary>
    public partial class WdAbHelper2 : FLY.ControlLibrary.WindowBigClose
    {
        WdAbHelperVm viewModel;
        public double A { get { return viewModel.A; } }
        public double B { get { return viewModel.B; } }

        //外部值
        private double a;
        private double b;

        public WdAbHelper2()
        {
            InitializeComponent();
            viewModel = new WdAbHelperVm();
            this.DataContext = viewModel;
        }

        public void Init(double a,double b, ParamDictionary paramDictionary)
        {
            this.a = a;
            this.b = b;

            viewModel.A = a;
            viewModel.B = b;

            paramDictionary.SetBinding(viewModel, nameof(viewModel.IsJustB), "AbHelper.IsJustB", true);

        }

        private void button_cal_Click(object sender, RoutedEventArgs e)
        {
            if (viewModel.IsJustB)
            {
                //只是平移补偿
                //只有X1,Y1
                viewModel.A = a;
                viewModel.B = viewModel.Y1 - viewModel.X1 + b;
            }
            else
            {

                if ((viewModel.X2 == viewModel.X1) && (viewModel.Y2 != viewModel.Y1))
                {
                    string tit = (string)Application.Current.TryFindResource("str.WdAbHelper.ApplyFailed");
                    FLY.ControlLibrary.Window_WarningTip.Show(tit, "X2 == X1 but Y2 != Y1", TimeSpan.FromSeconds(2));
                    return;
                }
                if ((viewModel.X2 != viewModel.X1) && (viewModel.Y2 == viewModel.Y1))
                {
                    string tit = (string)Application.Current.TryFindResource("str.WdAbHelper.ApplyFailed");
                    FLY.ControlLibrary.Window_WarningTip.Show(tit, "X2 != X1 but Y2 == Y1", TimeSpan.FromSeconds(2));
                    return;
                }
                else if (a == 0)
                {
                    string tit = (string)Application.Current.TryFindResource("str.WdAbHelper.ApplyFailed");
                    FLY.ControlLibrary.Window_WarningTip.Show(tit, "A==0", TimeSpan.FromSeconds(2));
                    return;
                }

                if ((viewModel.X2 == viewModel.X1) && (viewModel.Y2 == viewModel.Y1))
                {
                    viewModel.A = a;
                    viewModel.B = viewModel.Y2 - viewModel.X2 + b;
                }
                else
                {
                    double x2 = (viewModel.X2 - b) / a;
                    double x1 = (viewModel.X1 - b) / a;

                    viewModel.A = (viewModel.Y2 - viewModel.Y1) / (x2 - x1);
                    viewModel.B = viewModel.Y2 - viewModel.A * x2;
                }
            }
            {
                string tit = (string)Application.Current.TryFindResource("str.WdAbHelper.ApplySuccessfully");
                FLY.ControlLibrary.Window_Tip.Show(tit, null, TimeSpan.FromSeconds(2)) ;
            }
        }
        private void button_apply_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

    }
    public class WdAbHelperVm : INotifyPropertyChanged
    {

        public double X1 { get; set; }
        public double Y1 { get; set; }

        public double X2 { get; set; }

        public double Y2 { get; set; }

        public double A { get; set; }

        public double B { get; set; }

        public bool IsJustB { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}