using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows;
using Txgy.EWS.Client.Models;
using System.Windows.Controls;
using WW.Cad.Model.Entities;
using static WW.Cad.Model.Entities.DxfHatch.BoundaryPath;
using WW.Cad.Model;
using WW.Cad.IO;
using System.Windows.Media.Imaging;
using System.IO;
using Prism.Mvvm;
using static System.Net.Mime.MediaTypeNames;
using Txgy.EWS.Client.Entity;
using System.Diagnostics;

namespace Txgy.EWS.Client.Common.Helpers
{
    public class ReportPlanImage : BindableBase
    {
        public VisualHost host;
        string planImagePath;
        double width = 700;
        double height = 700;
        double canvasLeft = 60;
        double canvasTop = 50;
        double canvasRight = 50;
        double canvasBottom = 50;
        double canvasWidthRange = 2000;
        double canvasHeightRange = 2000;
        double gridTextSpacing = 500;
        public double unit = 100;

        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);
        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 = 5;
        public bool IsShowEvent = true;
        private string dwgFile;

        public string DwgFile
        {
            get { return dwgFile; }
            set
            {
                SetProperty(ref dwgFile, value);
                CreateCadLayersByDwg();
            }
        }

        public List<CadLayer> CadLayers { get; set; }
        public ReportPlanImage()
        {
            planImagePath = AppDomain.CurrentDomain.BaseDirectory + "\\resources\\planImageTemp.png";
            host = new VisualHost();
            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 = 1600;
            height = 2000;
            xDrawUnit = (width - canvasLeft - canvasRight) / (double)canvasWidthRange;
            yDrawUnit = (height - canvasTop - canvasBottom) / (double)canvasHeightRange;

            drawUnit = xDrawUnit < yDrawUnit ? xDrawUnit : yDrawUnit;
            CadLayers = new List<CadLayer>();
            DwgFile = GlobalConfig.ProjectConfig.CadFileName;
            CreateCadLayersByDwg();
        }

        public VisualHost DrawPlanClipToDrawVisual(List<GridItemEventResult> events, int w, int h)
        {
            double dpi = 96d;
            Draw(events);
            RenderTargetBitmap rtb = new RenderTargetBitmap(w, h, dpi, dpi, PixelFormats.Pbgra32);
            rtb.Render(host);

            BitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(rtb));
            using (Stream stm = File.Create(planImagePath))
            {
                encoder.Save(stm);
            }
            ////当前为固定尺寸,待修改为动态尺寸
            //int top = 0;
            //int left = 0;
            //int clipWidth = 0;
            //int clipHeight = 0;
            //if (w > h)
            //{
            //    left = (w - h) / 2;
            //    clipWidth = h;
            //    clipHeight = h;
            //}
            //else
            //{
            //    top = (h - w) / 2;
            //    clipWidth = w;
            //    clipHeight = w;
            //}
            ////int maxLen = w > h ? w : h;
            ////int minLen = w < h ? w : h;
            ////int clipWidth = w > h ? h : w;
            ////int clipHeight = w > h ? h : w;
            ////int top = (maxLen - minLen) / 2;
            //int clipWidth = w;
            //int clipHeight = h;
            //var bitmapImage = new BitmapImage(new Uri(planImagePath));           
            //var drawingVisual = new DrawingVisual();
            //using (DrawingContext dc = drawingVisual.RenderOpen())
            //{
            //    //dc.PushClip(new EllipseGeometry(new Point(30, 30), 20, 20));
            //    // 裁剪图片的 50x50 部分
            //    var croppedBitmap = new CroppedBitmap(bitmapImage, new Int32Rect(left, top, clipWidth, clipHeight));
            //    dc.DrawImage(croppedBitmap, new Rect(0, 0, clipWidth, clipHeight));
            //    //dc.Pop();

            //    //dc.DrawImage(bitmapImage, new Rect(100, 100, 500, 500));
            //}
            //VisualHost vh = new VisualHost();
            //vh.AddVisual(drawingVisual);
            return null;
        }
        public byte[] DrawPlanClipToBytes(List<GridItemEventResult> events)
        {
            double dpi = 96d;
            Draw(events);
            RenderTargetBitmap rtb = new RenderTargetBitmap((int)width, (int)height, dpi, dpi, PixelFormats.Pbgra32);
            rtb.Render(host);
            BitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(rtb));
            using (Stream stm = File.Create(planImagePath))
            {
                encoder.Save(stm);
            }
            //当前为固定尺寸,待修改为动态尺寸
            int clipWidth = 1496;
            int clipHeight = 1192;
            var bitmapImage = new BitmapImage(new Uri(planImagePath));
            var drawingVisual = new DrawingVisual();
            using (DrawingContext dc = drawingVisual.RenderOpen())
            {
                //dc.PushClip(new EllipseGeometry(new Point(30, 30), 20, 20));
                // 裁剪图片的 50x50 部分
                var croppedBitmap = new CroppedBitmap(bitmapImage, new Int32Rect(57, 250, clipWidth, clipHeight));
                dc.DrawImage(croppedBitmap, new Rect(0, 0, clipWidth, clipHeight));
                //dc.Pop();

                //dc.DrawImage(bitmapImage, new Rect(100, 100, 500, 500));
            }
            //VisualHost vh = new VisualHost();
            //vh.AddVisual(drawingVisual);
            RenderTargetBitmap bitmap = new RenderTargetBitmap(clipWidth, clipHeight, 96d, 96d, PixelFormats.Pbgra32);
            bitmap.Render(drawingVisual);
            ////保存图片
            //BitmapEncoder encoder2 = new PngBitmapEncoder();
            //encoder2.Frames.Add(BitmapFrame.Create(bitmap));
            //using (Stream stm = File.Create("e:\\3.png"))
            //{
            //    encoder2.Save(stm);
            //}
            var bb = BitmapHelper.ConvertToBytes(bitmap);
            return bb;

            //动态尺寸
            //double minX = events.Min(es => es.X);
            //double maxX = events.Max(es => es.X);
            //double minY = events.Min(es => es.Y);
            //double maxY = events.Max(es => es.Y);
            //if ((maxY - minY) > (maxX - minX))
            //{

            //}
            //else
            //{

            //}
            //double heightCenter = (maxY - minY) / 2.0 + minY;
            //double drawTop = GlobalConfig.ProjectConfig.WorkArea.NMax - (heightCenter + 500);


        }
        public byte[] DrawPlanToBytes(List<GridItemEventResult> events)
        {
            TransformGroup transformGroup = new TransformGroup();
            transformGroup.Children.Add(new ScaleTransform(2, 2));
            transformGroup.Children.Add(new TranslateTransform(0, -150));
            host.RenderTransform = transformGroup;
            Draw(events);

            RenderTargetBitmap bitmap = new RenderTargetBitmap((int)width, (int)height, 96d, 96d, PixelFormats.Pbgra32);
            bitmap.Render(host);
            //bitmap.d
            var bb = BitmapHelper.ConvertToBytes(bitmap);
            return bb;
        }
        public void Draw(List<GridItemEventResult> events)
        {

            if (host.visuals.Count > 0)
            {
                host.RemoveVisual(host.visuals[0]);
            }
            DrawingVisual visual = new DrawingVisual();
            //if (events!=null)
            //{
            //    if (events.Count>0)
            //    {
            //        double minX= events.Min(es => es.X) - 100;
            //        double maxX= events.Max(es => es.X) + 100;
            //        double minY= events.Min(es => es.Y) - 100;
            //        double maxY = events.Max(es => es.Y) + 100;
            //        baseX = minX;
            //        baseY = minY;

            //        canvasWidthRange = Math.Ceiling((maxX - minX) / 100) * 100;
            //        canvasHeightRange = Math.Ceiling((maxY - minY) / 100) * 100;
            //        gridTextSpacing = 500;
            //        xDrawUnit = (width - canvasLeft - canvasRight) / (double)canvasWidthRange;
            //        yDrawUnit = (height - canvasTop - canvasBottom) / (double)canvasHeightRange;

            //        drawUnit = xDrawUnit < yDrawUnit ? xDrawUnit : yDrawUnit;
            //    }
            //}
            using (DrawingContext dc = visual.RenderOpen())
            {
                //绘制背景色
                Pen pen = new Pen(Brushes.White, 0);
                pen.Freeze();
                dc.DrawRectangle(new SolidColorBrush(BackGroundColor), pen,
                    new Rect(0, 0, width, height));

                int xAxisCount = (int)Math.Ceiling(canvasHeightRange / unit);
                int yAxisCount = (int)Math.Ceiling(canvasWidthRange / unit);
                DrawGrid(dc, xAxisCount, yAxisCount, unit);
                DrawDwg(dc);
                if (IsShowEvent)
                {
                    if (events != null)
                    {
                        if (events.Count > 0)
                        {
                            foreach (var item in events)
                            {
                                float sizeFactor = (float)(((4f + item.ML) / (4f)) * 2f) + 0.1f;
                                int colorIndex = (int)(((item.Z - GlobalConfig.ProjectConfig.WorkArea.ZMin)
                                / Math.Abs(GlobalConfig.ProjectConfig.WorkArea.ZMax - GlobalConfig.ProjectConfig.WorkArea.ZMin))
                                * GlobalConfig.ColorCountDefault);
                                System.Windows.Media.Color ec = GlobalConfig.eventDepthColor[colorIndex];
                                BrushEvent = new SolidColorBrush(ec);
                                Pen eventPen = new Pen(BrushEvent, 0);
                                eventPen.Freeze();
                                dc.DrawEllipse(BrushEvent, eventPen,
                                    new Point(canvasLeft + (item.X - baseX) * drawUnit, canvasTop + (canvasHeightRange - (item.Y - baseY)) * drawUnit),
                                    EventSize * sizeFactor, EventSize * sizeFactor);
                            }
                        }
                    }
                }
            }
            host.AddVisual(visual);
        }
        public void DrawGierInTime(List<GridItemEventResult> events, double dw, double dh, int EventShowTotalSeconds, DateTime inTime)
        {
            width = dw;
            height = dh;
            xDrawUnit = (width - canvasLeft - canvasRight) / (double)canvasWidthRange;
            yDrawUnit = (height - canvasTop - canvasBottom) / (double)canvasHeightRange;

            drawUnit = xDrawUnit < yDrawUnit ? xDrawUnit : yDrawUnit;
         
            if (host.visuals.Count > 0)
            {
                while (host.visuals.Count > 0)
                {
                    host.RemoveVisual(host.visuals[0]);
                }
            }
            //Debug.WriteLine("host cnt:" + host.visuals.Count);

            DrawingVisual visualbc = new DrawingVisual();
            using (DrawingContext dc = visualbc.RenderOpen())
            {
                int xAxisCount = (int)Math.Ceiling(canvasHeightRange / unit);
                int yAxisCount = (int)Math.Ceiling(canvasWidthRange / unit);
                DrawGrid(dc, xAxisCount, yAxisCount, unit);
                DrawDwg(dc);
            }
            host.AddVisual(visualbc);
            if (IsShowEvent)
            {
                if (events != null)
                {
                    foreach (GridItemEventResult item in events)
                    {
                        if ((inTime - DateTime.Parse(item.EventTime)).TotalSeconds > EventShowTotalSeconds)
                        {
                            continue;
                        }

                        TaggedVisual visualE = new TaggedVisual();
                        
                        using (DrawingContext dc = visualE.RenderOpen())
                        {
                            float sizeFactor = (float)(((4f + item.ML) / (10f)) * 10f) + 0.1f;

                            //float sizeFactor = GlobalData.ReportEventLevelList.Where(rel => rel.lowerbound <= item.ML && rel.upperbound > item.ML).First().level;

                            int colorIndex = (int)(((item.Z - GlobalConfig.ProjectConfig.WorkArea.ZMin)
                            / Math.Abs(GlobalConfig.ProjectConfig.WorkArea.ZMax - GlobalConfig.ProjectConfig.WorkArea.ZMin))
                            * GlobalConfig.ColorCountDefault);
                            System.Windows.Media.Color ec = GlobalConfig.eventDepthColor[colorIndex];
                            BrushEvent = new SolidColorBrush(ec);
                            Pen eventPen = new Pen(BrushEvent, 0);
                            eventPen.Freeze();
                            double px = item.X - baseX;
                            double py = item.Y - baseY;
                            double dx = canvasLeft + (item.X - baseX) * drawUnit;
                            double dy = canvasTop + (canvasHeightRange - (item.Y - baseY)) * drawUnit;
                            double dsize = EventSize * drawUnit * sizeFactor;
                            //Debug.WriteLine($"--------px:{px}\tpy:{py}\tdx:{dx}\tdy:{dy}\tsize:{dsize}--------");
                            dc.DrawEllipse(BrushEvent, eventPen,new Point(dx, dy),dsize, dsize);
                        }
                        TaggedVisual.SetTag(visualE, item);
                        host.AddVisual(visualE);
                    }
                }
            }
        }
        //public void DrawGierInTime(List<GridItemEventResult> events, double dw, double dh, int EventShowTotalSeconds, DateTime inTime)
        //{
        //    width = dw;
        //    height = dh;
        //    xDrawUnit = (width - canvasLeft - canvasRight) / (double)canvasWidthRange;
        //    yDrawUnit = (height - canvasTop - canvasBottom) / (double)canvasHeightRange;

        //    drawUnit = xDrawUnit < yDrawUnit ? xDrawUnit : yDrawUnit;
        //    //CadLayers = new List<CadLayer>();
        //    //CreateCadLayersByDwg();
        //    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(0, 0, width, height));

        //        int xAxisCount = (int)Math.Ceiling(canvasHeightRange / unit);
        //        int yAxisCount = (int)Math.Ceiling(canvasWidthRange / unit);
        //        DrawGrid(dc, xAxisCount, yAxisCount, unit);
        //        DrawDwg(dc);
        //        if (IsShowEvent)
        //        {
        //            if (events != null)
        //            {
        //                if (events.Count > 0)
        //                {
        //                    foreach (var item in events)
        //                    {
        //                        if ((DateTime.Parse(item.EventTime) - inTime).TotalSeconds < EventShowTotalSeconds)
        //                        {
        //                            float sizeFactor = (float)(((4f + item.ML) / (10f)) * 10f) + 0.1f;

        //                            //float sizeFactor = GlobalData.ReportEventLevelList.Where(rel => rel.lowerbound <= item.ML && rel.upperbound > item.ML).First().level;

        //                            int colorIndex = (int)(((item.Z - GlobalConfig.ProjectConfig.WorkArea.ZMin)
        //                            / Math.Abs(GlobalConfig.ProjectConfig.WorkArea.ZMax - GlobalConfig.ProjectConfig.WorkArea.ZMin))
        //                            * GlobalConfig.ColorCountDefault);
        //                            System.Windows.Media.Color ec = GlobalConfig.eventDepthColor[colorIndex];
        //                            BrushEvent = new SolidColorBrush(ec);
        //                            Pen eventPen = new Pen(BrushEvent, 0);
        //                            eventPen.Freeze();
        //                            dc.DrawEllipse(BrushEvent, eventPen,
        //                                new Point(canvasLeft + (item.X - baseX) * drawUnit, canvasTop + (canvasHeightRange - (item.Y - baseY)) * drawUnit),
        //                                EventSize * drawUnit * sizeFactor, EventSize * drawUnit * sizeFactor);
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //    }

        //    host.AddVisual(visual);
        //}
        public void DrawGier(List<GridItemEventResult> events, double dw, double dh)
        {
            width = dw;
            height = dh;
            xDrawUnit = (width - canvasLeft - canvasRight) / (double)canvasWidthRange;
            yDrawUnit = (height - canvasTop - canvasBottom) / (double)canvasHeightRange;

            drawUnit = xDrawUnit < yDrawUnit ? xDrawUnit : yDrawUnit;
          
            if (host.visuals.Count > 0)
            {
                while (host.visuals.Count > 0)
                {
                    host.RemoveVisual(host.visuals[0]);
                }
            }
            DrawingVisual visualbc = new DrawingVisual();
            using (DrawingContext dc = visualbc.RenderOpen())
            {
                int xAxisCount = (int)Math.Ceiling(canvasHeightRange / unit);
                int yAxisCount = (int)Math.Ceiling(canvasWidthRange / unit);
                DrawGrid(dc, xAxisCount, yAxisCount, unit);
                DrawDwg(dc);
            }
            host.AddVisual(visualbc);
            if (IsShowEvent)
            {
                if (events != null)
                {
                    foreach (GridItemEventResult item in events)
                    {
                        TaggedVisual visualE = new TaggedVisual();

                        using (DrawingContext dc = visualE.RenderOpen())
                        {
                            float sizeFactor = (float)(((4f + item.ML) / (10f)) * 10f) + 0.1f;

                            //float sizeFactor = GlobalData.ReportEventLevelList.Where(rel => rel.lowerbound <= item.ML && rel.upperbound > item.ML).First().level;

                            int colorIndex = (int)(((item.Z - GlobalConfig.ProjectConfig.WorkArea.ZMin)
                            / Math.Abs(GlobalConfig.ProjectConfig.WorkArea.ZMax - GlobalConfig.ProjectConfig.WorkArea.ZMin))
                            * GlobalConfig.ColorCountDefault);
                            System.Windows.Media.Color ec = GlobalConfig.eventDepthColor[colorIndex];
                            BrushEvent = new SolidColorBrush(ec);
                            Pen eventPen = new Pen(BrushEvent, 0);
                            eventPen.Freeze();
                            double px = item.X - baseX;
                            double py = item.Y - baseY;
                            double dx = canvasLeft + (item.X - baseX) * drawUnit;
                            double dy = canvasTop + (canvasHeightRange - (item.Y - baseY)) * drawUnit;
                            double dsize = EventSize * drawUnit * sizeFactor;
                            //Debug.WriteLine($"--------px:{px}\tpy:{py}\tdx:{dx}\tdy:{dy}\tsize:{dsize}--------");
                            dc.DrawEllipse(BrushEvent, eventPen, new Point(dx, dy), dsize, dsize);
                        }
                        TaggedVisual.SetTag(visualE, item);
                        host.AddVisual(visualE);
                    }
                }
            }
        }
        public EventAinimationItem ConvertGierToEai(GridItemEventResult gier)
        {
            EventAinimationItem eai = new EventAinimationItem();
            float sizeFactor = (float)(((4f + gier.ML) / (10f)) * 10f) + 0.1f;

            eai.Width = EventSize * drawUnit * sizeFactor;
            eai.Height = EventSize * drawUnit * sizeFactor;
            eai.OffsetX = canvasLeft + (gier.X - baseX) * drawUnit;
            eai.OffsetY = canvasTop + (canvasHeightRange - (gier.Y - baseY)) * drawUnit;
            int colorIndex = (int)(((gier.Z - GlobalConfig.ProjectConfig.WorkArea.ZMin)
                                / Math.Abs(GlobalConfig.ProjectConfig.WorkArea.ZMax - GlobalConfig.ProjectConfig.WorkArea.ZMin))
                                * GlobalConfig.ColorCountDefault);
            System.Windows.Media.Color ec = GlobalConfig.eventDepthColor[colorIndex];
            eai.Fill = new SolidColorBrush(ec);
            return eai;
        }
        public void DrawRRREInTime(List<RemoteRealtimeResultEntity> events, double dw, double dh, int EventShowTotalSeconds, DateTime inTime)
        {
            width = dw;
            height = dh;
            xDrawUnit = (width - canvasLeft - canvasRight) / (double)canvasWidthRange;
            yDrawUnit = (height - canvasTop - canvasBottom) / (double)canvasHeightRange;

            drawUnit = xDrawUnit < yDrawUnit ? xDrawUnit : yDrawUnit;
            //CadLayers = new List<CadLayer>();
            //CreateCadLayersByDwg();
            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(0, 0, width, height));

                int xAxisCount = (int)Math.Ceiling(canvasHeightRange / unit);
                int yAxisCount = (int)Math.Ceiling(canvasWidthRange / unit);
                DrawGrid(dc, xAxisCount, yAxisCount, unit);
                DrawDwg(dc);
                if (IsShowEvent)
                {
                    if (events != null)
                    {
                        if (events.Count > 0)
                        {
                            foreach (var item in events)
                            {
                                if ((inTime - DateTime.Parse(item.EventTime)).TotalSeconds <= EventShowTotalSeconds)
                                {
                                    float sizeFactor = (float)(((4f + item.ML) / (10f)) * 10f) + 0.1f;

                                    //float sizeFactor = GlobalData.ReportEventLevelList.Where(rel => rel.lowerbound <= item.ML && rel.upperbound > item.ML).First().level;

                                    int colorIndex = (int)(((item.Z - GlobalConfig.ProjectConfig.WorkArea.ZMin)
                                    / Math.Abs(GlobalConfig.ProjectConfig.WorkArea.ZMax - GlobalConfig.ProjectConfig.WorkArea.ZMin))
                                    * GlobalConfig.ColorCountDefault);
                                    System.Windows.Media.Color ec = GlobalConfig.eventDepthColor[colorIndex];
                                    BrushEvent = new SolidColorBrush(ec);
                                    Pen eventPen = new Pen(BrushEvent, 0);
                                    eventPen.Freeze();
                                    dc.DrawEllipse(BrushEvent, eventPen,
                                        new Point(canvasLeft + (item.X - baseX) * drawUnit, canvasTop + (canvasHeightRange - (item.Y - baseY)) * drawUnit),
                                        EventSize * drawUnit * sizeFactor, EventSize * drawUnit * sizeFactor);
                                }
                            }
                        }
                    }
                }
            }

            host.AddVisual(visual);
        }
        public void DrawRRRE(List<RemoteRealtimeResultEntity> events, double dw, double dh)
        {
            width = dw;
            height = dh;
            xDrawUnit = (width - canvasLeft - canvasRight) / (double)canvasWidthRange;
            yDrawUnit = (height - canvasTop - canvasBottom) / (double)canvasHeightRange;

            drawUnit = xDrawUnit < yDrawUnit ? xDrawUnit : yDrawUnit;
            //CadLayers = new List<CadLayer>();
            //CreateCadLayersByDwg();
            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(0, 0, width, height));

                int xAxisCount = (int)Math.Ceiling(canvasHeightRange / unit);
                int yAxisCount = (int)Math.Ceiling(canvasWidthRange / unit);
                DrawGrid(dc, xAxisCount, yAxisCount, unit);
                DrawDwg(dc);
                if (IsShowEvent)
                {
                    if (events != null)
                    {
                        if (events.Count > 0)
                        {
                            foreach (var item in events)
                            {
                                float sizeFactor = (float)(((4f + item.ML) / (10f)) * 10f) + 0.1f;

                                //float sizeFactor = GlobalData.ReportEventLevelList.Where(rel => rel.lowerbound <= item.ML && rel.upperbound > item.ML).First().level;

                                int colorIndex = (int)(((item.Z - GlobalConfig.ProjectConfig.WorkArea.ZMin)
                                / Math.Abs(GlobalConfig.ProjectConfig.WorkArea.ZMax - GlobalConfig.ProjectConfig.WorkArea.ZMin))
                                * GlobalConfig.ColorCountDefault);
                                System.Windows.Media.Color ec = GlobalConfig.eventDepthColor[colorIndex];
                                BrushEvent = new SolidColorBrush(ec);
                                Pen eventPen = new Pen(BrushEvent, 0);
                                eventPen.Freeze();
                                dc.DrawEllipse(BrushEvent, eventPen,
                                    new Point(canvasLeft + (item.X - baseX) * drawUnit, canvasTop + (canvasHeightRange - (item.Y - baseY)) * drawUnit),
                                    EventSize * drawUnit * sizeFactor, EventSize * drawUnit * sizeFactor);
                            }
                        }
                    }
                }
            }

            host.AddVisual(visual);
        }
        public void Draw(List<GridItemEventResult> events, double dw, double dh)
        {
            width = dw;
            height = dh;
            xDrawUnit = (width - canvasLeft - canvasRight) / (double)canvasWidthRange;
            yDrawUnit = (height - canvasTop - canvasBottom) / (double)canvasHeightRange;

            drawUnit = xDrawUnit < yDrawUnit ? xDrawUnit : yDrawUnit;
            //CadLayers = new List<CadLayer>();
            //CreateCadLayersByDwg();
            
            if (host.visuals.Count > 0)
            {
               host.visuals.Clear();
                //for (int i = 0; i < host.visuals.Count; i++)
                //{
                //    host.RemoveVisual(host.visuals[i]);
                //}
            }
            DrawingVisual visualbc = new DrawingVisual();
            using (DrawingContext dc = visualbc.RenderOpen())
            {
                int xAxisCount = (int)Math.Ceiling(canvasHeightRange / unit);
                int yAxisCount = (int)Math.Ceiling(canvasWidthRange / unit);
                DrawGrid(dc, xAxisCount, yAxisCount, unit);
                DrawDwg(dc);
            }
            host.AddVisual(visualbc);
            if (IsShowEvent)
            {
                if (events != null)
                {
                    foreach (GridItemEventResult item in events)
                    {
                        TaggedVisual visualE = new TaggedVisual();
                        
                        using (DrawingContext dc = visualE.RenderOpen())
                        {
                            float sizeFactor = (float)(((4f + item.ML) / (10f)) * 10f) + 0.1f;

                            //float sizeFactor = GlobalData.ReportEventLevelList.Where(rel => rel.lowerbound <= item.ML && rel.upperbound > item.ML).First().level;

                            int colorIndex = (int)(((item.Z - GlobalConfig.ProjectConfig.WorkArea.ZMin)
                            / Math.Abs(GlobalConfig.ProjectConfig.WorkArea.ZMax - GlobalConfig.ProjectConfig.WorkArea.ZMin))
                            * GlobalConfig.ColorCountDefault);
                            System.Windows.Media.Color ec = GlobalConfig.eventDepthColor[colorIndex];
                            BrushEvent = new SolidColorBrush(ec);
                            Pen eventPen = new Pen(BrushEvent, 0);
                            eventPen.Freeze();
                            dc.DrawEllipse(BrushEvent, eventPen,
                                new Point(canvasLeft + (item.X - baseX) * drawUnit, canvasTop + (canvasHeightRange - (item.Y - baseY)) * drawUnit),
                                EventSize * drawUnit * sizeFactor, EventSize * drawUnit * sizeFactor);
                        }
                        TaggedVisual.SetTag(visualE, "event");
                        host.AddVisual(visualE);
                    }
                }
            }
            //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(0, 0, width, height));

            //    int xAxisCount = (int)Math.Ceiling(canvasHeightRange / unit);
            //    int yAxisCount = (int)Math.Ceiling(canvasWidthRange / unit);
            //    DrawGrid(dc, xAxisCount, yAxisCount, unit);
            //    DrawDwg(dc);
            //    if (IsShowEvent)
            //    {
            //        if (events != null)
            //        {
            //            if (events.Count > 0)
            //            {
            //                foreach (var item in events)
            //                {
            //                    float sizeFactor = (float)(((4f + item.ML) / (10f)) * 10f) + 0.1f;

            //                    //float sizeFactor = GlobalData.ReportEventLevelList.Where(rel => rel.lowerbound <= item.ML && rel.upperbound > item.ML).First().level;

            //                    int colorIndex = (int)(((item.Z - GlobalConfig.ProjectConfig.WorkArea.ZMin)
            //                    / Math.Abs(GlobalConfig.ProjectConfig.WorkArea.ZMax - GlobalConfig.ProjectConfig.WorkArea.ZMin))
            //                    * GlobalConfig.ColorCountDefault);
            //                    System.Windows.Media.Color ec = GlobalConfig.eventDepthColor[colorIndex];
            //                    BrushEvent = new SolidColorBrush(ec);
            //                    Pen eventPen = new Pen(BrushEvent, 0);
            //                    eventPen.Freeze();
            //                    dc.DrawEllipse(BrushEvent, eventPen,
            //                        new Point(canvasLeft + (item.X - baseX) * drawUnit, canvasTop + (canvasHeightRange - (item.Y - baseY)) * drawUnit),
            //                        EventSize * drawUnit * sizeFactor, EventSize * drawUnit * sizeFactor);
            //                }
            //            }
            //        }
            //    }
            //}

            //host.AddVisual(visual);
        }
        //public void Draw(List<GridItemEventResult> events, double dw, double dh)
        //{
        //    width = dw;
        //    height = dh;
        //    xDrawUnit = (width - canvasLeft - canvasRight) / (double)canvasWidthRange;
        //    yDrawUnit = (height - canvasTop - canvasBottom) / (double)canvasHeightRange;

        //    drawUnit = xDrawUnit < yDrawUnit ? xDrawUnit : yDrawUnit;
        //    //CadLayers = new List<CadLayer>();
        //    //CreateCadLayersByDwg();
        //    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(0, 0, width, height));

        //        int xAxisCount = (int)Math.Ceiling(canvasHeightRange / unit);
        //        int yAxisCount = (int)Math.Ceiling(canvasWidthRange / unit);
        //        DrawGrid(dc, xAxisCount, yAxisCount, unit);
        //        DrawDwg(dc);
        //        if (IsShowEvent)
        //        {
        //            if (events != null)
        //            {
        //                if (events.Count > 0)
        //                {
        //                    foreach (var item in events)
        //                    {
        //                        float sizeFactor = (float)(((4f + item.ML) / (10f)) * 10f) + 0.1f;

        //                        //float sizeFactor = GlobalData.ReportEventLevelList.Where(rel => rel.lowerbound <= item.ML && rel.upperbound > item.ML).First().level;

        //                        int colorIndex = (int)(((item.Z - GlobalConfig.ProjectConfig.WorkArea.ZMin)
        //                        / Math.Abs(GlobalConfig.ProjectConfig.WorkArea.ZMax - GlobalConfig.ProjectConfig.WorkArea.ZMin))
        //                        * GlobalConfig.ColorCountDefault);
        //                        System.Windows.Media.Color ec = GlobalConfig.eventDepthColor[colorIndex];
        //                        BrushEvent = new SolidColorBrush(ec);
        //                        Pen eventPen = new Pen(BrushEvent, 0);
        //                        eventPen.Freeze();
        //                        dc.DrawEllipse(BrushEvent, eventPen,
        //                            new Point(canvasLeft + (item.X - baseX) * drawUnit, canvasTop + (canvasHeightRange - (item.Y - baseY)) * drawUnit),
        //                            EventSize * drawUnit * sizeFactor, EventSize * drawUnit * sizeFactor);
        //                    }
        //                }
        //            }
        //        }
        //    }

        //    host.AddVisual(visual);
        //}
        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)
                    {
                        if (geo.Visible == true)
                        {
                            if (layer.Color.R == 0 && layer.Color.G == 0 && layer.Color.B == 255)
                            {
                                //if (geo.originPoints[0].Y > 4031900)
                                //{
                                //    //Console.WriteLine(1);
                                //}

                            }
                            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)
                    {
                        //if (!text.formattedText.Text.Contains("N"))
                        //{
                        //    text.drawPoint = new Point(canvasLeft + (text.origin.X - baseX) * drawUnit,
                        //    canvasTop + (canvasHeightRange - (text.origin.Y - baseY + text.formattedText.Height)) * drawUnit);
                        //    dc.DrawText(text.formattedText, text.drawPoint);
                        //}
                        //else
                        if (text.Visible == true)
                        {
                            //Console.WriteLine(text.formattedText.Height+"\t"+drawUnit.ToString());
                            text.drawPoint = new Point(canvasLeft + (text.origin.X - baseX) * drawUnit,
                            canvasTop + (canvasHeightRange - (text.origin.Y - baseY)) * drawUnit - text.formattedText.Height);
                            dc.DrawText(text.formattedText, text.drawPoint);
                        }

                    }
                    foreach (var line in layer.LineCollection)
                    {
                        if (line.Visible == true)
                        {
                            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 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, (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));
                }
            }
        }
        public void CreateCadLayersByDwg()
        {
            //string CadFilePath = GlobalConfig.ProjectConfig.CadFileName;
            DxfModel model = DwgReader.Read(DwgFile);
            List<string> entityTpyeList = new List<string>();
            Typeface typeface = new Typeface(new FontFamily("宋体"),
                           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("123");
                        //}
                        foreach (var ent in ents)
                        {
                            List<Point> points = new List<Point>();
                            switch (ent.EntityType)
                            {
                                case "LINE":
                                    //if (clc.name.Contains("断层"))
                                    //{
                                    //    Console.WriteLine("In Line");
                                    //}
                                    DxfLine line = (DxfLine)ent;
                                    if (line.Start.X >= GlobalConfig.ProjectConfig.WorkArea.EMin &&
                                            line.End.X <= GlobalConfig.ProjectConfig.WorkArea.EMax &&
                                            line.Start.Y >= GlobalConfig.ProjectConfig.WorkArea.NMin &&
                                            line.End.Y <= GlobalConfig.ProjectConfig.WorkArea.EMax)
                                    {
                                        points.Add(new Point(line.Start.X, line.Start.Y));
                                        points.Add(new Point(line.End.X, line.End.Y));
                                        CadGeometry cadGeometry = new CadGeometry();
                                        cadGeometry.originPoints = points;
                                        cadGeometry.IsClose = clc.lineClose;
                                        cadGeometry.IsFill = clc.isFill;
                                        cadGeometry.Visible = clc.isShow;
                                        cadLayer.GeometryCollection.Add(cadGeometry);
                                    }

                                    break;
                                case "POLYLINE":
                                    //if (clc.name.Contains("断层"))
                                    //{
                                    //    Console.WriteLine(1);
                                    //}
                                    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(point.X, point.Y));
                                        }
                                    }
                                    if (points.Count > 0)
                                    {
                                        CadGeometry cadGeometry = new CadGeometry();
                                        cadGeometry.originPoints = points;
                                        cadGeometry.IsClose = clc.lineClose;
                                        cadGeometry.IsFill = clc.isFill;
                                        cadGeometry.Visible = clc.isShow;
                                        //bool isFile = clc.name.Contains("台站") ? true : false;
                                        cadLayer.GeometryCollection.Add(cadGeometry);
                                    }
                                    break;
                                case "LWPOLYLINE":
                                    //if (clc.name.Contains("断层"))
                                    //{
                                    //    Console.WriteLine(1);
                                    //}
                                    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(point.X, point.Y));
                                        }
                                    }
                                    if (points.Count > 0)
                                    {
                                        CadGeometry cadGeometry = new CadGeometry();
                                        cadGeometry.originPoints = points;
                                        cadGeometry.IsClose = clc.lineClose;
                                        cadGeometry.IsFill = clc.isFill;
                                        cadGeometry.Visible = clc.isShow;
                                        cadLayer.GeometryCollection.Add(cadGeometry);
                                    }
                                    break;
                                case "MTEXT":
                                    DxfMText met = (DxfMText)ent;
                                    //if (met.Text.Contains("16") && clc.name.Contains("台站"))
                                    //{
                                    //    Console.WriteLine(1);
                                    //}
                                    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)
                                    {
                                        //Console.WriteLine("mtext:"+met.Text);
                                        string text = met.Text;
                                        double yOff = 0;
                                        if (ent.Layer.Name.Contains("台站"))
                                        {
                                            yOff = 10;
                                            //Console.WriteLine(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(met.InsertionPoint.X, met.InsertionPoint.Y - yOff);
                                        cadLayer.TextCollection.Add(new CadText(formattedText, origin, clc.isShow));
                                    }

                                    break;
                                case "TEXT":
                                    DxfText et = (DxfText)ent;
                                    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;
                                        //if (et.Text == "N2106")
                                        //{
                                        //    text = "北2106";
                                        //    Console.WriteLine(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(et.AlignmentPoint1.X, et.AlignmentPoint1.Y);
                                        cadLayer.TextCollection.Add(new CadText(formattedText, origin, clc.isShow));

                                    }
                                    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.Start.X >= GlobalConfig.ProjectConfig.WorkArea.EMin &&
                                            lineEdge.End.X <= GlobalConfig.ProjectConfig.WorkArea.EMax &&
                                            lineEdge.Start.Y >= GlobalConfig.ProjectConfig.WorkArea.NMin &&
                                            lineEdge.End.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(lineEdge.Start.X, lineEdge.Start.Y));
                                            //x = canvasLeft + (lineEdge.End.X - baseX) * drawUnit;
                                            //y = canvasTop + (canvasHeightRange - (lineEdge.End.Y - baseY)) * drawUnit;
                                            points.Add(new Point(lineEdge.End.X, lineEdge.End.Y));
                                        }
                                    }
                                    if (points.Count > 0)
                                    {
                                        CadGeometry cadGeometry = new CadGeometry();
                                        cadGeometry.originPoints = points;
                                        cadGeometry.IsClose = clc.lineClose;
                                        cadGeometry.IsFill = clc.isFill;
                                        cadGeometry.Visible = clc.isShow;
                                        cadLayer.GeometryCollection.Add(cadGeometry);
                                    }
                                    break;
                            }
                        }
                        CadLayers.Add(cadLayer);
                    }
                }
            }
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.Message);
            //}
        }
        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;
        }
    }
}
