using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using Txgy.EWS.Client.Common;
using Txgy.EWS.Client.Common.MessageEvents;
using Txgy.EWS.Client.Entity;
using Txgy.EWS.Client.FocalMechanism.Core;
using Txgy.EWS.Client.FocalMechanism.Model;
using Txgy.EWS.Client.IBLL;
using Txgy.EWS.Client.PageModule.Models;
using Unity;
using Txgy.Microseismic.BaseLib.Models;
using FmGrid = Txgy.EWS.Client.FocalMechanism.Model.FmGrid;
using FMStation = Txgy.EWS.Client.FocalMechanism.Model.FMStation;
using Arction.Wpf.ChartingMVVM.Series3D;
using Arction.Wpf.ChartingMVVM;
using WW.Cad.Model.Entities;
using WW.Cad.Model;
using WW.Cad.IO;
using Color = System.Windows.Media.Color;
using Arction.Wpf.ChartingMVVM.Maps;
using static WW.Cad.Model.Entities.DxfHatch.BoundaryPath;
using Txgy.EWS.Client.PageModule.Services;
using Txgy.EWS.Client.Common.Helpers;
using Txgy.EWS.Client.Models;

namespace Txgy.EWS.Client.PageModule.Views
{
    /// <summary>
    /// FocalMechanismView.xaml 的交互逻辑
    /// </summary>
    public partial class FocalMechanismView : UserControl
    {
        Dispatcher _dispatcher;
        private readonly IEventAggregator _ea;
        private readonly ISearchMsEventBLL searchMsEventBLL;
        IFreeSql fsqlSqLite = FreeSqlLocalSqLite.freeLocalSqLite;
        //private readonly IRemoteDownloadDataBLL remoteDownloadDataBLL;

        private VisualHost host;
        double width = 700;
        double height = 700;
        double canvasLeft = 80;
        double canvasTop = 50;
        double canvasRight = 50;
        double canvasBottom = 50;
        double canvasWidthRange = 2000;
        double canvasHeightRange = 2000;
        double gridTextSpacing = 500;
        public double unit = 100;
        public TransformGroup transGroup;
        public double fontSize = 12;
        public double stationSize = 20;
        public double stationFontSize = 10;
        double baseX = 39701000;
        double baseY = 4352000;
        double xDrawUnit = 1;
        double yDrawUnit = 1;
        double drawUnit = 1;
        Point _downPoint = new Point(0, 0);
        bool _isMoving = false;
        public bool IsShowGridText = true;
        public bool IsShowGrid = true;
        public System.Windows.Media.Color BackGroundColor = System.Windows.Media.Colors.White;
        public Brush GridLineBrush = Brushes.Gray;
        public double GridLineWidth = 0.2;

        public Brush BrushStrikeSlip = Brushes.Black;
        public Brush BrushDipSlip = Brushes.Blue;
        public Brush BrushTensile = Brushes.Red;
        public Brush BrushEvent = Brushes.Green;

        public double EventSize = 3;
        public ProcessCore pCore { get; set; }
        public Factory factory { get; set; }
        public FMMap fmMap { get; set; }
        public MmEvent curMmEvent { get; set; }
        public bool IsShowEvent = true;

        private double _scale = 1;
        public double Scale
        {
            get { return _scale; }
            set
            {
                _scale = value;
            }
        }
        public List<CadLayer> CadLayers { get; set; }
        ReportPlanImage rpi = new ReportPlanImage();
        public FocalMechanismView(IUnityContainer unityContainer, IEventAggregator ea, ISearchMsEventBLL searchMsEventBLL)
        {
            InitializeComponent();
            _dispatcher = unityContainer.Resolve<Dispatcher>();
            this._ea = ea;
            this.searchMsEventBLL = searchMsEventBLL;
            //this.remoteDownloadDataBLL = remoteDownloadDataBLL;
            host = new VisualHost();
            canvas.Children.Add(host);
            //rpi.Draw(null, canvas.Width, canvas.Height);
            //canvas.Children.Add(rpi.host);

            baseX = GlobalConfig.ProjectConfig.WorkArea.EMin;
            baseY = GlobalConfig.ProjectConfig.WorkArea.NMin;

            canvasWidthRange = Math.Ceiling((GlobalConfig.ProjectConfig.WorkArea.EMax - GlobalConfig.ProjectConfig.WorkArea.EMin) / 100) * 100;
            canvasHeightRange = Math.Ceiling((GlobalConfig.ProjectConfig.WorkArea.NMax - GlobalConfig.ProjectConfig.WorkArea.NMin) / 100) * 100;

            gridTextSpacing = 500;

            width = canvas.Width;
            height = canvas.Height;

            xDrawUnit = (width - canvasLeft - canvasRight) / (double)canvasWidthRange;
            yDrawUnit = (height - canvasTop - canvasBottom) / (double)canvasHeightRange;

            drawUnit = xDrawUnit < yDrawUnit ? xDrawUnit : yDrawUnit;

            //if (xUnit < yUnit) drawUnit = yUnit;
            CadLayers = new List<CadLayer>();
            CreateBackgroudByDwg();


            this.SizeChanged += SystemMonitor_SizeChanged;

            SetFacotry(GlobalConfig.ProjectConfig.WorkArea.EMax - GlobalConfig.ProjectConfig.WorkArea.EMin,
                GlobalConfig.ProjectConfig.WorkArea.NMax - GlobalConfig.ProjectConfig.WorkArea.NMin, GlobalConfig.ProjectConfig.StationDic);
            this._ea.GetEvent<ShowWavesEvent>().Subscribe(u =>
            {
                var lre = fsqlSqLite.Select<LocalRealtimeResultEntity>().Where(re => re.EventTime == u).First();
                if (lre == null)
                {
                    //string courseSql = @"select * from " + GlobalConfig.UseResultTable + " where " + GlobalConfig.EventIDColName + "==@eid";
                    var rre = this.searchMsEventBLL.GetEvent(u);
                    if (rre != null)
                    {
                        curMmEvent = new MmEvent();
                        curMmEvent.EventID = rre.EventID;
                        curMmEvent.EventTimeStr = rre.EventTimeStr;
                        curMmEvent.EventTime = rre.EventTime;
                        curMmEvent.X = rre.X;
                        curMmEvent.Y = rre.Y;
                        curMmEvent.Z = rre.Z;
                        curMmEvent.ML = rre.ML;
                        curMmEvent.LocSta = rre.LocSta;
                        curMmEvent.MLSta = rre.MLSta;
                        curMmEvent.Energy = rre.Energy;

                    }
                }
                else
                {
                    curMmEvent = new MmEvent();
                    curMmEvent.EventID = lre.RTEventID;
                    curMmEvent.EventTimeStr = lre.EventTime;
                    curMmEvent.EventTime = DateTime.Parse(lre.EventTime);
                    curMmEvent.X = lre.X;
                    curMmEvent.Y = lre.Y;
                    curMmEvent.Z = lre.Z;
                    curMmEvent.ML = lre.ML;
                    curMmEvent.LocSta = lre.LocSta;
                    curMmEvent.MLSta = lre.MLSta;
                    curMmEvent.SetEnergy();
                }
                //计算震源机制
                ComputeFM(curMmEvent);
                //Console.WriteLine(curMmEvent.Direction);
                Draw();
            });
            transGroup = new TransformGroup();
            transGroup.Children.Add(new ScaleTransform(1.1, 1.1));
            transGroup.Children.Add(new TranslateTransform(-270, -50));
            canvas.RenderTransform = transGroup;
            Draw();
        }
        private void ShowEventFocalMechanism(MmEvent curEvent)
        {
            FracturePoint r1 = new FracturePoint(0, 0);
            FracturePoint r2 = new FracturePoint(0, 0);

            //MmEvent CurMmEvent = (MmEvent)obj;
            SetEventPhases(curEvent);
            //fmMap = CreateFM(CurMmEvent);
            ComputeFM(curEvent);
            //switch (curEvent.FocalType)
            //{
            //    case FocalMechanismType.strikeSlip:
            //        FractureLine strikeSlipLine1 = new FractureLine();
            //        //strikeSlipLine1.p1 = new FracturePoint((float)((float)CurMmEvent.X - EAxisMin + lineStep), (float)(CurMmEvent.Y - lineLen - NAxisMin));
            //        //strikeSlipLine1.p2 = new FracturePoint((float)((float)CurMmEvent.X - EAxisMin + lineStep), (float)(CurMmEvent.Y + lineLen - NAxisMin));
            //        //r1 = PointRotate(new FracturePoint((float)(CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y - NAxisMin)), strikeSlipLine1.p1, CurMmEvent.Direction);
            //        //r2 = PointRotate(new FracturePoint((float)(CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y - NAxisMin)), strikeSlipLine1.p2, CurMmEvent.Direction);

            //        //strikeSlipLine1.p1 = new FracturePoint((float)(r1.X), (float)(r1.Y));
            //        //strikeSlipLine1.p2 = new FracturePoint((float)(r2.X), (float)(r2.Y));
            //        //this.PlotModel.Annotations.Add(CreateStrikeSlipModel(strikeSlipLine1));
            //        //FractureLine strikeSlipLine2 = new FractureLine();
            //        //strikeSlipLine2.p1 = new FracturePoint((float)((float)CurMmEvent.X - EAxisMin - lineStep), (float)(CurMmEvent.Y - lineLen - NAxisMin));
            //        //strikeSlipLine2.p2 = new FracturePoint((float)((float)CurMmEvent.X - EAxisMin - lineStep), (float)(CurMmEvent.Y + lineLen - NAxisMin));
            //        //r1 = PointRotate(new FracturePoint((float)(CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y - NAxisMin)), strikeSlipLine2.p1, CurMmEvent.Direction);
            //        //r2 = PointRotate(new FracturePoint((float)(CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y - NAxisMin)), strikeSlipLine2.p2, CurMmEvent.Direction);

            //        //strikeSlipLine2.p1 = new FracturePoint((float)(r1.X), (float)(r1.Y));
            //        //strikeSlipLine2.p2 = new FracturePoint((float)(r2.X), (float)(r2.Y));
            //        //this.PlotModel.Annotations.Add(CreateStrikeSlipModel(strikeSlipLine2));
            //        break;
            //    case FocalMechanismType.dipSlip:
            //        FractureLine dipSlipLine = new FractureLine();
            //        //dipSlipLine.p1 = new FracturePoint((float)((float)CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y - lineLen - NAxisMin));
            //        //dipSlipLine.p2 = new FracturePoint((float)((float)CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y + lineLen - NAxisMin));
            //        //r1 = PointRotate(new FracturePoint((float)(CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y - NAxisMin)), dipSlipLine.p1, CurMmEvent.Direction);
            //        //r2 = PointRotate(new FracturePoint((float)(CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y - NAxisMin)), dipSlipLine.p2, CurMmEvent.Direction);

            //        //dipSlipLine.p1 = new FracturePoint((float)(r1.X), (float)(r1.Y));
            //        //dipSlipLine.p2 = new FracturePoint((float)(r2.X), (float)(r2.Y));
            //        //this.PlotModel.Annotations.Add(CreateDipSlipModel(dipSlipLine));
            //        break;
            //    case FocalMechanismType.tensile:
            //        //this.PlotModel.Annotations.Add(CreateTensileModel(CurMmEvent, EAxisMin, NAxisMin));
            //        //FractureLine tensileLine = new FractureLine();
            //        //tensileLine.p1 = new FracturePoint((float)((float)CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y - lineLen - NAxisMin));
            //        //tensileLine.p2 = new FracturePoint((float)((float)CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y + lineLen - NAxisMin));
            //        //r1 = PointRotate(new FracturePoint((float)(CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y - NAxisMin)), tensileLine.p1, CurMmEvent.Direction);
            //        //r2 = PointRotate(new FracturePoint((float)(CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y - NAxisMin)), tensileLine.p2, CurMmEvent.Direction);

            //        //tensileLine.p1 = new FracturePoint((float)(r1.X), (float)(r1.Y));
            //        //tensileLine.p2 = new FracturePoint((float)(r2.X), (float)(r2.Y));
            //        //this.PlotModel.Annotations.Add(CreateLineModel(tensileLine));
            //        break;
            //    case FocalMechanismType.unknown:
            //        strikeSlipLine1 = new FractureLine();
            //        //strikeSlipLine1.p1 = new FracturePoint((float)((float)CurMmEvent.X - EAxisMin + lineStep), (float)(CurMmEvent.Y - lineLen - NAxisMin));
            //        //strikeSlipLine1.p2 = new FracturePoint((float)((float)CurMmEvent.X - EAxisMin + lineStep), (float)(CurMmEvent.Y + lineLen - NAxisMin));
            //        //r1 = PointRotate(new FracturePoint((float)(CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y - NAxisMin)), strikeSlipLine1.p1, CurMmEvent.Direction);
            //        //r2 = PointRotate(new FracturePoint((float)(CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y - NAxisMin)), strikeSlipLine1.p2, CurMmEvent.Direction);

            //        //strikeSlipLine1.p1 = new FracturePoint((float)(r1.X), (float)(r1.Y));
            //        //strikeSlipLine1.p2 = new FracturePoint((float)(r2.X), (float)(r2.Y));
            //        //this.PlotModel.Annotations.Add(CreateStrikeSlipModel(strikeSlipLine1));
            //        //strikeSlipLine2 = new FractureLine();
            //        //strikeSlipLine2.p1 = new FracturePoint((float)((float)CurMmEvent.X - EAxisMin - lineStep), (float)(CurMmEvent.Y - lineLen - NAxisMin));
            //        //strikeSlipLine2.p2 = new FracturePoint((float)((float)CurMmEvent.X - EAxisMin - lineStep), (float)(CurMmEvent.Y + lineLen - NAxisMin));
            //        //r1 = PointRotate(new FracturePoint((float)(CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y - NAxisMin)), strikeSlipLine2.p1, CurMmEvent.Direction);
            //        //r2 = PointRotate(new FracturePoint((float)(CurMmEvent.X - EAxisMin), (float)(CurMmEvent.Y - NAxisMin)), strikeSlipLine2.p2, CurMmEvent.Direction);

            //        //strikeSlipLine2.p1 = new FracturePoint((float)(r1.X), (float)(r1.Y));
            //        //strikeSlipLine2.p2 = new FracturePoint((float)(r2.X), (float)(r2.Y));
            //        //this.PlotModel.Annotations.Add(CreateStrikeSlipModel(strikeSlipLine2));
            //        break;
            //    default:

            //        break;
            //}
        }
        public void CheckJsonFile(string eventTiemStr)
        {
            string downLoadStr = eventTiemStr;
            string datePath = downLoadStr.Substring(0, 4) + downLoadStr.Substring(5, 2) + downLoadStr.Substring(8, 2);
            string jsonStr = "HA." + downLoadStr.Substring(0, 4) + downLoadStr.Substring(5, 2)
                + downLoadStr.Substring(8, 2) + downLoadStr.Substring(10, 3)
                + downLoadStr.Substring(14, 2) + downLoadStr.Substring(17, 2) + ".01" + GlobalConfig.DataTypeString + ".json";
            string jsonFilePath = GlobalConfig.ProjectConfig.MseedFilePath + "\\" + datePath + "\\" + jsonStr;

        }
        public void SetEventPhases(MmEvent mmEvent)
        {
            string downLoadStr = mmEvent.EventTimeStr;
            string datePath = downLoadStr.Substring(0, 4) + downLoadStr.Substring(5, 2) + downLoadStr.Substring(8, 2);
            string jsonStr = "HA." + downLoadStr.Substring(0, 4) + downLoadStr.Substring(5, 2)
                + downLoadStr.Substring(8, 2) + downLoadStr.Substring(10, 3)
                + downLoadStr.Substring(14, 2) + downLoadStr.Substring(17, 2) + ".01" + GlobalConfig.DataTypeString + ".json";
            string jsonFilePath = GlobalConfig.ProjectConfig.MseedFilePath + "\\" + datePath + "\\";
            if (!File.Exists(jsonFilePath + jsonStr))
            {
                new DownloadJsonFile().Download(downLoadStr, jsonFilePath, jsonStr, GlobalConfig.UseWaveDataTable);
            }
            using (StreamReader sr = System.IO.File.OpenText(jsonFilePath + jsonStr))
            {
                mmEvent.JsonFile = jsonFilePath + jsonStr;

                JsonTextReader reader = new JsonTextReader(sr);
                JArray jArray = (JArray)JToken.ReadFrom(reader);
                //解析普通属性和数组混合的Json文件
                //数组0是普通属性集合
                JObject jobj = (JObject)jArray[0];
                mmEvent.SetEnergy();
                //mmEvent.DominantFreq = 15;
                mmEvent.Phases = new Dictionary<string, int>();
                //List<double> DominantFreqlist = new List<double>();
                JArray phaseArr = JArray.FromObject(jobj["phases"]);
                //List<double> DominantFreqList = new List<double>();
                for (int i = 0; i < phaseArr.Count; i++)
                {
                    if (phaseArr[i]["first_motion_direct"] != null)
                    {
                        mmEvent.Phases.Add(phaseArr[i]["id"].ToString().Substring(3, 3),
                            int.Parse(phaseArr[i]["first_motion_direct"].ToString()));
                    }
                   
                }
              
            }
        }
        public void ComputeFM(MmEvent mmEvent)
        {
            string downLoadStr = mmEvent.EventTimeStr;
            string datePath = downLoadStr.Substring(0, 4) + downLoadStr.Substring(5, 2) + downLoadStr.Substring(8, 2);
            string jsonStr = "HA." + downLoadStr.Substring(0, 4) + downLoadStr.Substring(5, 2)
                + downLoadStr.Substring(8, 2) + downLoadStr.Substring(10, 3)
                + downLoadStr.Substring(14, 2) + downLoadStr.Substring(17, 2) + ".01" + GlobalConfig.DataTypeString + ".json";
            string jsonFilePath = GlobalConfig.ProjectConfig.MseedFilePath + "\\" + datePath + "\\";
            if (!File.Exists(jsonFilePath + jsonStr))
            {
                new DownloadJsonFile().Download(downLoadStr, jsonFilePath, jsonStr, GlobalConfig.UseWaveDataTable);
            }
            using (StreamReader sr = System.IO.File.OpenText(jsonFilePath + jsonStr))
            {
                mmEvent.JsonFile = jsonFilePath + jsonStr;

                JsonTextReader reader = new JsonTextReader(sr);
                JArray jArray = (JArray)JToken.ReadFrom(reader);
                //解析普通属性和数组混合的Json文件
                //数组0是普通属性集合
                JObject jobj = (JObject)jArray[0];
                mmEvent.SetEnergy();
                mmEvent.Phases = new Dictionary<string, int>();
                JArray phaseArr = JArray.FromObject(jobj["phases"]);
                for (int i = 0; i < phaseArr.Count; i++)
                {
                    if (phaseArr[i]["first_motion_direct"] != null)
                    {
                        mmEvent.Phases.Add(phaseArr[i]["id"].ToString().Substring(3, 3),
                            int.Parse(phaseArr[i]["first_motion_direct"].ToString()));
                    }
                }
            }
            fmMap = CreateFM(mmEvent, GlobalConfig.ProjectConfig.WorkArea.EMin, GlobalConfig.ProjectConfig.WorkArea.NMin);
            ComputationResult cr = pCore.ComputeResult(fmMap);
            if (cr == null)
                return;
            mmEvent.FocalType = cr.FocalType;
            mmEvent.Direction = cr.Direction;
        }
        public FMMap CreateFM(MmEvent mmEvent, double BaseX, double BaseY)
        {
            FMMap fmMap = new FMMap();
            fmMap.X = mmEvent.X - BaseX;
            fmMap.Y = mmEvent.Y - BaseY;
            fmMap.R = mmEvent.RMS;
            foreach (var item in mmEvent.Phases)
            {
                if (item.Value == -1)
                {
                    fmMap.negStation.Add(item.Key);
                }
                else
                {
                    fmMap.posStation.Add(item.Key);
                }
            }
            return fmMap;
        }
        public void Draw()
        {
            if (host.visuals.Count>0)
            {
                host.RemoveVisual(host.visuals[0]);
            }
            
            DrawingVisual visual = new DrawingVisual();
            using (DrawingContext dc = visual.RenderOpen())
            {
                
                //绘制背景色
                Pen pen = new Pen(Brushes.White, 0);
                pen.Freeze();
                dc.DrawRectangle(new SolidColorBrush(BackGroundColor), pen,
                    new Rect(canvasLeft, canvasTop, width - canvasLeft - canvasRight, height - canvasTop - canvasBottom));

                int xAxisCount = (int)Math.Ceiling(canvasHeightRange / unit);
                int yAxisCount = (int)Math.Ceiling(canvasWidthRange / unit);
                DrawGrid(dc, xAxisCount, yAxisCount, unit);
                DrawDwg(dc);
                if (curMmEvent != null)
                {
                    if (IsShowEvent)
                    {
                        float sizeFactor = (float)(((4f + curMmEvent.ML) / (4f)) * 2f) + 0.1f;
                        int colorIndex = (int)(((curMmEvent.Z - GlobalConfig.ProjectConfig.WorkArea.ZMin)
                            / Math.Abs(GlobalConfig.ProjectConfig.WorkArea.ZMax - GlobalConfig.ProjectConfig.WorkArea.ZMin))
                            * GlobalConfig.ColorCountDefault);
                        Color ec = GlobalConfig.eventDepthColor[colorIndex];
                        BrushEvent = new SolidColorBrush(ec);
                        Pen eventPen=new Pen(BrushEvent, 0);
                        eventPen.Freeze();
                        dc.DrawEllipse(BrushEvent, eventPen,
                            new Point(canvasLeft + (curMmEvent.X - baseX) * drawUnit, canvasTop + (canvasHeightRange - (curMmEvent.Y - baseY)) * drawUnit),
                            EventSize* sizeFactor, EventSize * sizeFactor);
                        //Console.WriteLine($"x:{curMmEvent.X}\ty:{curMmEvent.Y}");
                    }
                    double angle = curMmEvent.Direction + 90;

                    switch (curMmEvent.FocalType)
                    {
                        case FocalMechanismType.strikeSlip:
                            DrawStrikeSlip(dc, new Point((curMmEvent.X - baseX) * drawUnit, (canvasHeightRange - (curMmEvent.Y - baseY)) * drawUnit),
                                angle, BrushStrikeSlip);
                            break;
                        case FocalMechanismType.dipSlip:
                            DrawDipSlip(dc, new Point((curMmEvent.X - baseX) * drawUnit, (canvasHeightRange - (curMmEvent.Y - baseY)) * drawUnit),
                                angle, BrushDipSlip);
                            break;
                        case FocalMechanismType.tensile:
                            //if (curMmEvent.Direction > 90)
                            //{
                            //    angle = 270 - curMmEvent.Direction;
                            //}
                            //else
                            //{
                            //    angle = curMmEvent.Direction + 90;
                            //}
                            DrawTensile(dc, new Point((curMmEvent.X - baseX) * drawUnit, (canvasHeightRange - (curMmEvent.Y - baseY)) * drawUnit),
                                angle, BrushTensile);
                            break;
                        case FocalMechanismType.unknown:
                            break;
                    }
                    foreach (var item in curMmEvent.Phases)
                    {
                        StationModel sm;
                        var stationDic = GlobalConfig.ProjectConfig.StationDic.Where(sd => sd.Key == item.Key.ToUpper());
                        if (stationDic.Count() > 0)
                        {
                            sm = stationDic.First().Value;
                            if (item.Value > 0)
                            {
                                DrawStation(dc, new Point((sm.E - baseX) * drawUnit, (canvasHeightRange - (sm.N - baseY)) * drawUnit),
                                    sm.Name, stationSize, Brushes.Red);
                            }
                            else
                            {
                                DrawStation(dc, new Point((sm.E - baseX) * drawUnit, (canvasHeightRange - (sm.N - baseY)) * drawUnit),
                                    sm.Name, stationSize, Brushes.Black);
                            }
                        }
                    }

                }
            }
            host.AddVisual(visual);
        }

        public void CreateBackgroudByDwg()
        {
            string CadFilePath = GlobalConfig.ProjectConfig.CadFileName;
            DxfModel model = DwgReader.Read(CadFilePath);
            List<string> entityTpyeList = new List<string>();
            Typeface typeface = new Typeface(new FontFamily("Arial"),
                           FontStyles.Normal,
                           FontWeights.Normal,
                           FontStretches.Normal);          
            //try
            {
                
                foreach (var clc in GlobalConfig.CadLayerConfigs)
                {
                    if (clc.isShow)
                    {
                        var ents = model.Entities.FindAll(et => et.Layer.Name == clc.name);
                        CadLayer cadLayer = new CadLayer(clc.name, clc.lineColor, clc.linewidth);
                        if (clc.name=="设计巷道")
                        {
                            //Console.WriteLine(1);
                        }
                        foreach (var ent in ents)
                        {
                            List<Point> points = new List<Point>();
                            if (!entityTpyeList.Contains(ent.EntityType))
                            {
                                entityTpyeList.Add(ent.EntityType);
                                Console.WriteLine(ent.EntityType);
                            }
                            //Console.WriteLine(ent.Layer.Name);
                            //if (ent.Layer.Name.Contains("台站"))
                            //{
                            //    Console.WriteLine(1);
                            //}
                            switch (ent.EntityType)
                            {
                                case "LINE":
                                   
                                    if (((DxfLine)ent).Start.X>0)
                                    {
                                        DxfLine lineEdge = (DxfLine)ent;
                                        points.Add(new Point(lineEdge.Start.X, lineEdge.Start.Y));
                                        points.Add(new Point(lineEdge.End.X, lineEdge.End.Y));
                                        CadGeometry cadGeometry = new CadGeometry();
                                        cadGeometry.originPoints = points;
                                        cadGeometry.IsClose = clc.lineClose;
                                        cadGeometry.IsFill = clc.isFill;
                                        cadLayer.GeometryCollection.Add(cadGeometry);
                                    }
                                   
                                    break;
                                case "POLYLINE":
                                    foreach (var point in ((DxfPolyline3D)ent).Vertices)
                                    {
                                        if (point.X >= GlobalConfig.ProjectConfig.WorkArea.EMin &&
                                            point.X <= GlobalConfig.ProjectConfig.WorkArea.EMax &&
                                            point.Y >= GlobalConfig.ProjectConfig.WorkArea.NMin &&
                                            point.Y <= GlobalConfig.ProjectConfig.WorkArea.EMax)
                                        {
                                            //double x = canvasLeft + (point.X - baseX) * drawUnit;
                                            //double y = canvasTop + (canvasHeightRange - (point.Y - baseY)) * drawUnit;
                                            //points.Add(new Point(x, y));

                                            points.Add(new Point(point.X, point.Y));
                                        }
                                    }
                                    if (points.Count > 0)
                                    {
                                        //bool isFile = clc.name.Contains("台站") ? true : false;
                                        //cadLayer.GeometryCollection.Add(CreateCadGeometry(points, clc.lineClose, clc.isFill));
                                        CadGeometry cadGeometry = new CadGeometry();
                                        cadGeometry.originPoints = points;
                                        cadGeometry.IsClose = clc.lineClose;
                                        cadGeometry.IsFill = clc.isFill;
                                        //bool isFile = clc.name.Contains("台站") ? true : false;
                                        cadLayer.GeometryCollection.Add(cadGeometry);
                                    }
                                    break;
                                case "LWPOLYLINE":
                                    foreach (var point in ((DxfLwPolyline)ent).Vertices)
                                    {
                                        if (point.X >= GlobalConfig.ProjectConfig.WorkArea.EMin &&
                                            point.X <= GlobalConfig.ProjectConfig.WorkArea.EMax &&
                                            point.Y >= GlobalConfig.ProjectConfig.WorkArea.NMin &&
                                            point.Y <= GlobalConfig.ProjectConfig.WorkArea.EMax)
                                        {
                                            //double x = canvasLeft + (point.X - baseX) * drawUnit;
                                            //double y = canvasTop + (canvasHeightRange - (point.Y - baseY)) * drawUnit;
                                            //points.Add(new Point(x, y));

                                            points.Add(new Point(point.X, point.Y));
                                        }
                                    }
                                    if (points.Count > 0)
                                    {
                                        //cadLayer.GeometryCollection.Add(CreateCadGeometry(points, clc.lineClose, clc.isFill));
                                        CadGeometry cadGeometry = new CadGeometry();
                                        cadGeometry.originPoints = points;
                                        cadGeometry.IsClose = clc.lineClose;
                                        cadGeometry.IsFill = clc.isFill;
                                        cadLayer.GeometryCollection.Add(cadGeometry);
                                    }
                                    break;
                                case "MTEXT":
                                    DxfMText met = (DxfMText)ent;
                                    //if (met.Text.Contains("42°") && clc.name.Contains("台站"))
                                    //{
                                    //    Console.WriteLine(1);
                                    //}
                                    if (!met.Text.Contains("N"))
                                    {
                                        if (met.InsertionPoint.X >= GlobalConfig.ProjectConfig.WorkArea.EMin &&
                                                                met.InsertionPoint.X <= GlobalConfig.ProjectConfig.WorkArea.EMax &&
                                                                met.InsertionPoint.Y >= GlobalConfig.ProjectConfig.WorkArea.NMin &&
                                                                met.InsertionPoint.Y <= GlobalConfig.ProjectConfig.WorkArea.EMax)
                                        {
                                            string text = met.Text;
                                            FormattedText formattedText = new FormattedText(text,
                                                new System.Globalization.CultureInfo("zh-CN"),
                                                FlowDirection.RightToLeft,
                                                typeface,
                                                clc.fontSize, new SolidColorBrush(clc.lineColor), 1
                                                );
                                            formattedText.TextAlignment = TextAlignment.Right;
                                            //double x = canvasLeft + (met.InsertionPoint.X - baseX) * drawUnit;
                                            //double y = canvasTop + (canvasHeightRange - (met.InsertionPoint.Y - baseY)) * drawUnit;
                                            //Point origin = new Point(x, y);
                                            //cadLayer.TextCollection.Add(new CadText(formattedText, origin));

                                            Point origin = new Point(met.InsertionPoint.X, met.InsertionPoint.Y);
                                            cadLayer.TextCollection.Add(new CadText(formattedText, origin));
                                        }
                                    }
                                    

                                    break;
                                case "TEXT":
                                    DxfText et = (DxfText)ent;
                                    //if (et.Text.Contains("16") && clc.name.Contains("台站"))
                                    //{
                                    //    Console.WriteLine(1);
                                    //}
                                    if (!et.Text.Contains("N"))
                                    {
                                        if (et.AlignmentPoint1.X >= GlobalConfig.ProjectConfig.WorkArea.EMin &&
                                                                et.AlignmentPoint1.X <= GlobalConfig.ProjectConfig.WorkArea.EMax &&
                                                                et.AlignmentPoint1.Y >= GlobalConfig.ProjectConfig.WorkArea.NMin &&
                                                                et.AlignmentPoint1.Y <= GlobalConfig.ProjectConfig.WorkArea.EMax)
                                        {
                                            string text = et.Text;
                                            FormattedText formattedText = new FormattedText(text,
                                                new System.Globalization.CultureInfo("zh-CN"),
                                                FlowDirection.RightToLeft,
                                                typeface,
                                                clc.fontSize, new SolidColorBrush(clc.lineColor), 1
                                                );
                                            formattedText.TextAlignment = TextAlignment.Right;
                                            //double x = canvasLeft + (et.AlignmentPoint1.X - baseX) * drawUnit;
                                            //double y = canvasTop + (canvasHeightRange - (et.AlignmentPoint1.Y - baseY)) * drawUnit;
                                            //Point origin = new Point(x, y);
                                            //cadLayer.TextCollection.Add(new CadText(formattedText, origin));

                                            Point origin = new Point(et.AlignmentPoint1.X, et.AlignmentPoint1.Y);
                                            cadLayer.TextCollection.Add(new CadText(formattedText, origin));
                                        }
                                    }
                                    
                                    break;
                                case "CIRCLE":
                                    DxfCircle dxfCircle = (DxfCircle)ent;
                                    //Console.WriteLine(1);
                                    break;
                                case "HATCH":
                                    DxfHatch dxfHatch = (DxfHatch)ent;
                                    
                                    foreach (var point in dxfHatch.BoundaryPaths[0].Edges)
                                    {
                                        LineEdge lineEdge = (LineEdge)point;
                                        //if (lineEdge.st. >= GlobalConfig.ProjectConfig.WorkArea.EMin &&
                                        //    point.X <= GlobalConfig.ProjectConfig.WorkArea.EMax &&
                                        //    point.Y >= GlobalConfig.ProjectConfig.WorkArea.NMin &&
                                        //    point.Y <= GlobalConfig.ProjectConfig.WorkArea.EMax)
                                        //{
                                        //    double x = canvasLeft + (lineEdge.Start.X - baseX) * drawUnit;
                                        //    double y = canvasTop + (canvasHeightRange - (lineEdge.Start.Y - baseY)) * drawUnit;
                                        //    points.Add(new Point(x, y));
                                        //    x = canvasLeft + (lineEdge.End.X - baseX) * drawUnit;
                                        //    y = canvasTop + (canvasHeightRange - (lineEdge.End.Y - baseY)) * drawUnit;
                                        //    points.Add(new Point(x, y));
                                        //}

                                        points.Add(new Point(lineEdge.Start.X, lineEdge.Start.Y));
                                        points.Add(new Point(lineEdge.End.X, lineEdge.End.Y));

                                    }
                                    if (points.Count > 0)
                                    {
                                        //cadLayer.GeometryCollection.Add(CreateCadGeometry(points, clc.lineClose, clc.isFill));
                                        CadGeometry cadGeometry = new CadGeometry();
                                        cadGeometry.originPoints = points;
                                        cadGeometry.IsClose = clc.lineClose;
                                        cadGeometry.IsFill = clc.isFill;
                                        cadLayer.GeometryCollection.Add(cadGeometry);
                                    }
                                    break;
                            }
                        }
                        CadLayers.Add(cadLayer);
                    }
                }               
            }
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.Message);
            //}
        }
        public void SetFacotry(double XMax, double YMax, Dictionary<string, StationModel> StationDic)
        {
            FmGrid fmGrid = new FmGrid();
            fmGrid.xmin = 0;
            fmGrid.xmax = XMax;
            fmGrid.ymin = 0;
            fmGrid.ymax = YMax;
            fmGrid.StationCount = StationDic.Count;
            fmGrid.StationList = new List<FocalMechanism.Model.FMStation>();
            int sc = 0;
            foreach (var item in StationDic)
            {
                fmGrid.StationList.Add(new FMStation(sc, item.Key, item.Value.E - baseX, item.Value.N - baseY));
                sc++;
            }
            fmGrid.initZeroCenter();
            fmGrid.computeDistance();
            pCore = new ProcessCore(fmGrid);
            factory = new Factory(fmGrid);
        }
        public PathGeometry CreateCadGeometry(List<Point> points, bool isClose, bool isFill)
        {
            PolyLineSegment lineSegment = new PolyLineSegment();

            foreach (var item in points)
            {
                lineSegment.Points.Add(item);
            }
            PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
            myPathSegmentCollection.Add(lineSegment);

            PathFigure pathFigure = new PathFigure();
            pathFigure.IsClosed = isClose;
            pathFigure.IsFilled = isFill;
            pathFigure.StartPoint = points[0];
            pathFigure.Segments = myPathSegmentCollection;

            PathFigureCollection pthFigureCollection = new PathFigureCollection();
            pthFigureCollection.Add(pathFigure);

            PathGeometry pthGeometry = new PathGeometry();
            pthGeometry.Figures = pthFigureCollection;
            return pthGeometry;
        }
        private void DrawDwg(DrawingContext dc)
        {
            if (CadLayers != null)
            {
                foreach (var layer in CadLayers)
                {
                    Pen layerPen = new Pen(new SolidColorBrush(layer.Color), layer.LineWidth);
                    layerPen.Freeze();
                    foreach (var geo in layer.GeometryCollection)
                    {
                        geo.drawPoints = new Point[geo.originPoints.Count];
                        for (int i = 0; i < geo.drawPoints.Length; i++)
                        {
                            geo.drawPoints[i] = new Point(canvasLeft + (geo.originPoints[i].X - baseX) * drawUnit,
                                canvasTop + (canvasHeightRange - (geo.originPoints[i].Y - baseY)) * drawUnit);
                        }
                        dc.DrawGeometry(new SolidColorBrush(layer.Color), layerPen, CadGeometry.CreateCadGeometry(geo.drawPoints, geo.IsClose, geo.IsFill));
                    }
                    foreach (var text in layer.TextCollection)
                    {
                        text.drawPoint = new Point(canvasLeft + (text.origin.X - baseX) * drawUnit,
                            canvasTop + (canvasHeightRange - (text.origin.Y - baseY)) * drawUnit);
                        dc.DrawText(text.formattedText, text.drawPoint);
                    }
                    foreach (var line in layer.LineCollection)
                    {
                        line.drawPoint0 = new Point(canvasLeft + (line.originPoint0.X - baseX) * drawUnit,
                           canvasTop + (canvasHeightRange - (line.originPoint0.Y - baseY)) * drawUnit);
                        line.drawPoint1 = new Point(canvasLeft + (line.originPoint1.X - baseX) * drawUnit,
                            canvasTop + (canvasHeightRange - (line.originPoint1.Y - baseY)) * drawUnit);
                        dc.DrawLine(layerPen, line.drawPoint0, line.drawPoint1);
                    }
                }
            }
        }
        private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            width = this.mainView.ActualWidth + e.Delta;
            height = this.mainView.ActualHeight + e.Delta;
            if (height < 700) height = 700;
            if (width < 700) width = 700;


            this.mainView.Width = width;
            this.mainView.Height = height;
            //xDrawUnit = (width - canvasLeft - canvasRight) / (double)canvasWidthRange;
            //yDrawUnit = (height - canvasTop - canvasBottom) / (double)canvasHeightRange;
            //drawUnit = xDrawUnit < yDrawUnit ? xDrawUnit : yDrawUnit;

            this.mainView.SetValue(Canvas.LeftProperty, (this.RenderSize.Width - this.mainView.Width) / 2);

            e.Handled = true;
        }

        /// <summary>
        /// 绘制张性
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="center"></param>
        /// <param name="angle"></param>
        private void DrawTensile(DrawingContext dc, Point center, double angle, Brush brush)
        {
            Point drawCenter = new Point(center.X + canvasLeft, center.Y + canvasTop);
            RotateTransform _rotateTrans = new RotateTransform();
            _rotateTrans.Angle = angle;
            _rotateTrans.CenterX = drawCenter.X;
            _rotateTrans.CenterY = drawCenter.Y;
            double LRadis = 10;
            double WRadis = 5;
            EllipseGeometry ellipseGeometry = new EllipseGeometry(new Point(drawCenter.X, canvasTop + center.Y), LRadis, WRadis);
            LineGeometry line = new LineGeometry(new Point(drawCenter.X - LRadis, drawCenter.Y), new Point(drawCenter.X + LRadis, drawCenter.Y));

            ellipseGeometry.Transform = _rotateTrans;
            line.Transform = _rotateTrans;
            Pen pen = new Pen(brush, 1.5);
            pen.Freeze();
            dc.DrawGeometry(Brushes.Transparent, pen, ellipseGeometry);
            dc.DrawGeometry(Brushes.Transparent, pen, line);

        }
        /// <summary>
        /// 绘制倾滑
        /// </summary>
        private void DrawDipSlip(DrawingContext dc, Point center, double angle, Brush brush)
        {
            Point drawCenter = new Point(center.X + canvasLeft, center.Y + canvasTop);
            RotateTransform _rotateTrans = new RotateTransform();
            _rotateTrans.Angle = angle;
            _rotateTrans.CenterX = drawCenter.X;
            _rotateTrans.CenterY = drawCenter.Y;
            double len = 10;

            Point p1 = new Point(drawCenter.X - len, drawCenter.Y);
            Point p2 = new Point(drawCenter.X - len, drawCenter.Y - 4);
            Point p3 = new Point(drawCenter.X + len, drawCenter.Y - 4);
            Point p4 = new Point(drawCenter.X + len, drawCenter.Y);
            Point p5 = new Point(drawCenter.X + 2, drawCenter.Y);
            Point p6 = new Point(drawCenter.X + 2, drawCenter.Y + len);
            Point p7 = new Point(drawCenter.X - 4, drawCenter.Y + 4.5);
            Point p8 = new Point(drawCenter.X - 2, drawCenter.Y + 4.5);
            Point p9 = new Point(drawCenter.X - 2, drawCenter.Y);
            PolyLineSegment lineSegment = new PolyLineSegment();
            lineSegment.Points.Add(p1);
            lineSegment.Points.Add(p2);
            lineSegment.Points.Add(p3);
            lineSegment.Points.Add(p4);
            lineSegment.Points.Add(p5);
            lineSegment.Points.Add(p6);
            lineSegment.Points.Add(p7);
            lineSegment.Points.Add(p8);
            lineSegment.Points.Add(p9);
            PathFigure pathFigure = new PathFigure();
            pathFigure.IsClosed = true;
            pathFigure.IsFilled = true;
            pathFigure.StartPoint = p1;

            PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
            myPathSegmentCollection.Add(lineSegment);
            pathFigure.Segments = myPathSegmentCollection;
            PathFigureCollection pthFigureCollection = new PathFigureCollection();

            pthFigureCollection.Add(pathFigure);

            PathGeometry pthGeometry = new PathGeometry();
            pthGeometry.Figures = pthFigureCollection;
            pthGeometry.Transform = _rotateTrans;
            Pen pen = new Pen(Brushes.White,0);
            pen.Freeze();
            dc.DrawGeometry(brush, pen, pthGeometry);
            //PolyLineSegment polyLineSegment= new PolyLineSegment() 
        }
        /// <summary>
        /// 绘制走滑
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="center"></param>
        /// <param name="angle"></param>
        /// <param name="brush"></param>
        private void DrawStrikeSlip(DrawingContext dc, Point center, double angle, Brush brush)
        {
            Point drawCenter = new Point(center.X + canvasLeft, center.Y + canvasTop);
            RotateTransform _rotateTrans = new RotateTransform();
            _rotateTrans.Angle = angle;
            _rotateTrans.CenterX = canvasLeft + center.X;
            _rotateTrans.CenterY = canvasTop + center.Y;

            //上箭头
            Point p1 = new Point(drawCenter.X - 9, drawCenter.Y - 2);
            Point p2 = new Point(drawCenter.X - 9, drawCenter.Y - 5);
            Point p3 = new Point(drawCenter.X + 3, drawCenter.Y - 5);
            Point p4 = new Point(drawCenter.X + 3, drawCenter.Y - 9);
            Point p5 = new Point(drawCenter.X + 9, drawCenter.Y - 2);
            PolyLineSegment lineSegment = new PolyLineSegment();
            lineSegment.Points.Add(p1);
            lineSegment.Points.Add(p2);
            lineSegment.Points.Add(p3);
            lineSegment.Points.Add(p4);
            lineSegment.Points.Add(p5);
            PathFigure pathFigure = new PathFigure();
            pathFigure.IsClosed = true;
            pathFigure.IsFilled = true;
            pathFigure.StartPoint = p1;


            PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
            myPathSegmentCollection.Add(lineSegment);
            pathFigure.Segments = myPathSegmentCollection;

            PathFigureCollection pthFigureCollection = new PathFigureCollection();
            pthFigureCollection.Add(pathFigure);

            PathGeometry pthGeometry = new PathGeometry();
            pthGeometry.Figures = pthFigureCollection;
            pthGeometry.Transform = _rotateTrans;
            Pen pen = new Pen(Brushes.White, 0);
            pen.Freeze();
            dc.DrawGeometry(brush, pen, pthGeometry);


            //下箭头
            Point xp1 = new Point(drawCenter.X - 9, drawCenter.Y + 2);
            Point xp2 = new Point(drawCenter.X + 9, drawCenter.Y + 2);
            Point xp3 = new Point(drawCenter.X + 9, drawCenter.Y + 5);
            Point xp4 = new Point(drawCenter.X - 3, drawCenter.Y + 5);
            Point xp5 = new Point(drawCenter.X - 3, drawCenter.Y + 9);
            PolyLineSegment lineSegment2 = new PolyLineSegment();
            lineSegment2.Points.Add(xp1);
            lineSegment2.Points.Add(xp2);
            lineSegment2.Points.Add(xp3);
            lineSegment2.Points.Add(xp4);
            lineSegment2.Points.Add(xp5);
            PathFigure pathFigure2 = new PathFigure();
            pathFigure2.IsClosed = true;
            pathFigure2.IsFilled = true;
            pathFigure2.StartPoint = xp1;

            PathSegmentCollection myPathSegmentCollection2 = new PathSegmentCollection();
            pathFigure2.Segments = myPathSegmentCollection2;
            myPathSegmentCollection2.Add(lineSegment2);

            PathFigureCollection pthFigureCollection2 = new PathFigureCollection();
            pthFigureCollection2.Add(pathFigure2);

            PathGeometry pthGeometry2 = new PathGeometry();
            pthGeometry2.Figures = pthFigureCollection2;
            pthGeometry2.Transform = _rotateTrans;

            dc.DrawGeometry(brush, pen, pthGeometry2);


        }
        /// <summary>
        /// 绘制台站
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="center"></param>
        /// <param name="text"></param>
        /// <param name="size"></param>
        /// <param name="brush"></param>
        /// <exception cref="InvalidOperationException"></exception>
        private void DrawStation(DrawingContext dc, Point center, string text, double size, Brush brush)
        {
            Point drawCenter = new Point(center.X + canvasLeft, center.Y + canvasTop);
            Rect rect = new Rect(drawCenter.X - size / 2.0, drawCenter.Y - size / 2.0, size, size);
            Pen stationPen = new Pen(brush, 1);
            stationPen.Freeze();
            dc.DrawRectangle(Brushes.Transparent, stationPen, rect);
            FormattedText formattedText = new FormattedText(text,
                new System.Globalization.CultureInfo("zh-CN"),
                FlowDirection.LeftToRight,
                new Typeface("Arail"),
                stationFontSize, brush, 1
                );
            dc.DrawText(formattedText, new Point(drawCenter.X - size / 2.0 + 1, drawCenter.Y - size / 2.0 + 5));
        }
        /// <summary>
        /// 绘制网格
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="xLines"></param>
        /// <param name="yLines"></param>
        /// <param name="unit"></param>
        /// <param name="pen"></param>
        /// <exception cref="InvalidOperationException"></exception>
        private void DrawGrid(DrawingContext dc, int xLines, int yLines, double unit)
        {
            Point drawPoint = new Point(canvasLeft, canvasTop);
            int xWidth = (int)(drawUnit * canvasWidthRange);
            int ylength = (int)(drawUnit * canvasHeightRange);
            //int canLen=
            if (IsShowGrid)
            {
                System.Windows.Media.Pen normalPen = new System.Windows.Media.Pen(GridLineBrush, GridLineWidth);
                Pen drawPen = normalPen;
                Pen stressPen = new Pen(GridLineBrush, 0.5);
                //绘制X轴
                for (int i = 0; i < xLines + 1; i++)
                {
                    if ((i % (gridTextSpacing / unit)) == 0)
                    {
                        drawPen = stressPen;
                    }
                    else
                    {
                        drawPen = normalPen;
                    }
                    drawPen.Freeze();
                    dc.DrawLine(drawPen, new Point(drawPoint.X + 0, drawPoint.Y + i * drawUnit * unit),
                        new Point(drawPoint.X + xWidth, drawPoint.Y + i * drawUnit * unit));
                }
                //绘制Y轴
                for (int i = 0; i < yLines + 1; i++)
                {
                    if ((i % (gridTextSpacing / unit)) == 0)
                    {
                        drawPen = stressPen;
                    }
                    else
                    {
                        drawPen = normalPen;
                    }
                    dc.DrawLine(drawPen, new Point(drawPoint.X + i * drawUnit * unit, drawPoint.Y + 0),
                       new Point(drawPoint.X + i * drawUnit * unit, drawPoint.Y + ylength));
                }
            }
            //绘制网格文字
            if (IsShowGridText)
            {
                string txtTmp = (baseX + unit).ToString("F0");
                Typeface typeface = new Typeface(new FontFamily("Arial"),
                           FontStyles.Normal,
                           FontWeights.Normal,
                           FontStretches.Normal);
                GlyphTypeface glyphTypeface;
                if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
                    throw new InvalidOperationException("No glyphtypeface found");

                ushort[] glyphIndexes = new ushort[txtTmp.Length];
                double[] advanceWidths = new double[txtTmp.Length];
                double totalWidth = 0;
                for (int n = 0; n < txtTmp.Length; n++)
                {
                    ushort glyphIndex = glyphTypeface.CharacterToGlyphMap[txtTmp[n]];
                    glyphIndexes[n] = glyphIndex;
                    double twidth = glyphTypeface.AdvanceWidths[glyphIndex] * fontSize;
                    advanceWidths[n] = twidth;
                    totalWidth += twidth;
                }

                for (int i = 0; i < xLines + 1; i++)
                {
                    if (((i * unit) % gridTextSpacing) == 0)
                    {
                        string text = (baseY + i * unit-GlobalConfig.BaseY).ToString("F0");
                        FormattedText formattedText = new FormattedText(text,
                            new System.Globalization.CultureInfo("zh-CN"),
                            FlowDirection.RightToLeft,
                            typeface,
                            fontSize, Brushes.Black, 1
                            );
                        dc.DrawText(formattedText, new Point(drawPoint.X - 10, drawPoint.Y + (xLines - i) * drawUnit * unit - 6));
                    } //dc.DrawLine(pen, new Point(drawPoint.X + 0, drawPoint.Y + i * drawUnit * unit), new Point(drawPoint.X + xWidth, drawPoint.Y + i * drawUnit * unit));
                }

                for (int i = 0; i < yLines + 1; i++)
                {
                    if (((i * unit) % gridTextSpacing) == 0)
                    {
                        string text = (baseX + i * unit-GlobalConfig.BaseX).ToString("F0");
                        FormattedText formattedText = new FormattedText(text,
                            new System.Globalization.CultureInfo("zh-CN"),
                            FlowDirection.RightToLeft,
                            typeface,
                            fontSize, Brushes.Black, 1
                            );

                        //dc.DrawText(formattedText, new Point(drawPoint.X + i * drawUnit * unit + totalWidth / 2.0, height - canvasBottom + 5));
                        dc.DrawText(formattedText, new Point(drawPoint.X + i * drawUnit * unit + totalWidth / 2.0, (xLines + 1) * drawUnit * unit + 20));
                    }//dc.DrawLine(pen, new Point(drawPoint.X + i * drawUnit * unit, height - canvasBottom + 10), new Point(drawPoint.X + i * drawUnit * unit, height - canvasBottom + 10));
                }
            }
        }
        private void ZoomImage(TransformGroup group, Point point, double scale)
        {
            Point pointToContent = group.Inverse.Transform(point);

            ScaleTransform scaleT = group.Children[0] as ScaleTransform;

            if (scaleT.ScaleX + scale < 1) return;

            scaleT.ScaleX += scale;

            scaleT.ScaleY += scale;

            TranslateTransform translateT = group.Children[1] as TranslateTransform;

            translateT.X = -1 * ((pointToContent.X * scaleT.ScaleX) - point.X);

            translateT.Y = -1 * ((pointToContent.Y * scaleT.ScaleY) - point.Y);

        }
        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _downPoint = e.GetPosition(sender as Canvas);
            this.previousMousePoint = _downPoint;
            //left = double.Parse(this.mainView.GetValue(Canvas.LeftProperty).ToString());
            //top = double.Parse(this.mainView.GetValue(Canvas.TopProperty).ToString());

            _isMoving = true;
            (sender as Canvas).CaptureMouse();
            e.Handled = true;
        }
        private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _isMoving = false;
            (sender as Canvas).ReleaseMouseCapture();
            e.Handled = true;
        }
        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            //if (_isMoving)
            //{
            //    Point currentPoint = e.GetPosition(sender as Canvas);

            //    this.mainView.SetValue(Canvas.LeftProperty, left + (currentPoint.X - _downPoint.X));
            //    this.mainView.SetValue(Canvas.TopProperty, top + (currentPoint.Y - _downPoint.Y));
            //    //this.tbScalse.Text=
            //    e.Handled = true;
            //}
            if (_isMoving)
            {
                Point currentPoint = e.GetPosition(sender as Canvas);

                //this.mainView.SetValue(Canvas.LeftProperty, left + (currentPoint.X - _downPoint.X));
                //this.mainView.SetValue(Canvas.TopProperty, top + (currentPoint.Y - _downPoint.Y));
                TranslateTransform tt = transGroup.Children[1] as TranslateTransform;
                this.DoImageMove(tt, currentPoint);
                e.Handled = true;
            }
        }
        Point previousMousePoint;
        private void DoImageMove(TranslateTransform transform, Point position)
        {
            //trgroup = group.Children[1] as TransformGroup;

            //Debug.Assert(group != null, "Can't find transform group from image compare panel resource");

            //TranslateTransform transform = group.Children[1] as TranslateTransform;

            transform.X += position.X - this.previousMousePoint.X;

            transform.Y += position.Y - this.previousMousePoint.Y;

            this.previousMousePoint = position;

        }
        private void SystemMonitor_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.mainView.SetValue(Canvas.LeftProperty, (this.RenderSize.Width - this.mainView.ActualWidth) / 2);
            width = canvas.Width;
            height = canvas.Height;
            //xDrawUnit = (width - canvasLeft - canvasRight) / (double)canvasWidthRange;
            //yDrawUnit = (height - canvasTop - canvasBottom) / (double)canvasHeightRange;
            //drawUnit = xDrawUnit < yDrawUnit ? xDrawUnit : yDrawUnit;
        }
    }
}
