基本信息
源码名称:windorm 加载WPF控件 ,实现dxf文件显示
源码大小:0.78M
文件格式:.rar
开发语言:C#
更新时间:2020-12-25
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 10 元 
   源码介绍
本例程实现dxf文件的加载及显示,其中显示部门通过winform加载WPF控件实现

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using netDxf;
using netDxf.Entities;
using System.Globalization;
using System.Windows.Input;

namespace DXFMaiform
{
    public partial class MainForm : Form
    {
        
        private DxfDocument dxf;
        public MainForm()
        {
            InitializeComponent();
            
            WindowState = FormWindowState.Maximized;
            
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            //  wpfUserControl.MouseWheel = new MouseWheelEventHandler(OnMouseWheel);
          //  base.OnLoad(e);
        }
        
        protected override void OnMouseWheel(System.Windows.Forms.MouseEventArgs e)
        {
          //  wpfUserControl.On_MouseWheel((System.Windows.Input.MouseWheelEventArgs)e);
        }
        protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
           
        }
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string filename = string.Empty;
            using (OpenFileDialog openDlg = new OpenFileDialog())
            {
                openDlg.InitialDirectory = "C:\\";
                openDlg.Filter = "DXF 文件 |*.dxf";
                if (openDlg.ShowDialog() == DialogResult.OK)
                {
                    filename = openDlg.FileName;
                    if (wpfUserControl.MyCanvas.Children.Count != 0)
                    {
                        wpfUserControl.MyCanvas.Children.Clear();
                    }

                    dxf = new DxfDocument();
                    dxf.Load(filename);
                }
            }
            AddLayers();
            AddGraph();
            AdjustGraph();
        }

        private void AddLayers()
        {
            foreach(var lay in dxf.Layers)
            {
                System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
                path.Stroke = new SolidColorBrush(System.Windows.Media.Color.FromArgb(lay.Color.ToColor().A, lay.Color.ToColor().R, lay.Color.ToColor().G, lay.Color.ToColor().B));
                path.Tag = lay.Name;

                GeometryGroup GeoGroup = new GeometryGroup();
                path.Data = GeoGroup;

                wpfUserControl.MyCanvas.Children.Add(path);
            }

            System.Windows.Shapes.Path label = new System.Windows.Shapes.Path();

            GeometryGroup Character = new GeometryGroup();
            label.Data = Character;
            label.Tag = "Character";

            wpfUserControl.MyCanvas.Children.Add(label);
        }

        private void AddGraph()
        {
            AddCircle();

            AddEllipses();

            AddArcs();

            AddLines();

            AddPolylines();

            AddText();

            AddMText();
        }
        private void AdjustGraph()
        {
            wpfUserControl.MyCanvas.Offset = new System.Windows.Point(0, 0);
            wpfUserControl.MyCanvas.Scale = 1;

            //double maxLeft=0 ;
            //double maxRight=0;
            //double maxTop=0;
            //double maxBottom=0 ;

            var bound = ((GeometryGroup)((System.Windows.Shapes.Path)(wpfUserControl.MyCanvas.Children[0])).Data).Bounds;

            double maxLeft = bound.Left;
            double maxRight = bound.Right;
            double maxTop = bound.Top;
            double maxBottom = bound.Bottom;

            foreach (var p in wpfUserControl.MyCanvas.Children)
            {
                System.Windows.Shapes.Path path = (System.Windows.Shapes.Path)p;
                if ((string)path.Tag == "Character")
                {
                    path.StrokeThickness = 0.05;
                    path.Stroke = System.Windows.Media.Brushes.White;
                    path.Fill = System.Windows.Media.Brushes.White;

                }
                else
                {

                    path.StrokeThickness = 1;
                }
                CalBounds(ref maxLeft, ref maxRight, ref maxTop, ref maxBottom, path);


                //path.Stroke = Brushes.White;
                //path.StrokeThickness = 2;

                //double Sx = path.Data.Bounds.Left (path.Data.Bounds.Right - path.Data.Bounds.Left) / 2;
                //double Sy = path.Data.Bounds.Top (path.Data.Bounds.Bottom - path.Data.Bounds.Top) / 2;
                //double width = path.Data.Bounds.Right - path.Data.Bounds.Left;
                //double length = path.Data.Bounds.Bottom - path.Data.Bounds.Top;

                //double scaleX = Benchmark.ActualWidth / (path.Data.Bounds.Right - path.Data.Bounds.Left);
                //double scaleY = Benchmark.ActualHeight / (path.Data.Bounds.Bottom - path.Data.Bounds.Top);


            }

            double Ex = wpfUserControl.Benchmark.ActualWidth / 2;
            double Ey = wpfUserControl.Benchmark.ActualHeight / 2;

            double Sx = maxLeft (maxRight - maxLeft) / 2;
            double Sy = maxTop (maxBottom - maxTop) / 2;

            double scaleX = wpfUserControl.Benchmark.ActualWidth / (maxRight - maxLeft);
            double scaleY = wpfUserControl.Benchmark.ActualHeight / (maxBottom - maxTop);

            var scale = (scaleX < scaleY ? scaleX : scaleY) * 0.8;

            wpfUserControl.MyCanvas.Scale *= scale;

            foreach (var p in wpfUserControl.MyCanvas.Children)
            {
                System.Windows.Shapes.Path path = (System.Windows.Shapes.Path)p;

                path.StrokeThickness /= scale;
            }

            System.Windows.Point size = new System.Windows.Point(Ex - scale * Sx, Ey - scale * Sy);
            wpfUserControl.MyCanvas.Offset -= (Vector)size;

        }
        private static void CalBounds(ref double maxLeft, ref double maxRight, ref double maxTop, ref double maxBottom, System.Windows.Shapes.Path path)
        {
            maxLeft = maxLeft < ((GeometryGroup)path.Data).Bounds.Left ? maxLeft : ((GeometryGroup)path.Data).Bounds.Left;
            maxRight = maxRight > ((GeometryGroup)path.Data).Bounds.Right ? maxRight : ((GeometryGroup)path.Data).Bounds.Right;
            maxTop = maxTop < ((GeometryGroup)path.Data).Bounds.Top ? maxTop : ((GeometryGroup)path.Data).Bounds.Top;
            maxBottom = maxBottom > ((GeometryGroup)path.Data).Bounds.Bottom ? maxBottom : ((GeometryGroup)path.Data).Bounds.Bottom;
        }
        private void AddCircle()
        {
            foreach(var c in dxf.Circles)
            {
                EllipseGeometry circle = new EllipseGeometry(new System.Windows.Point(c.Center.X, -c.Center.Y), c.Radius, c.Radius);
                System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();

                GeometryGroup GeoGroup = new GeometryGroup();
                GeoGroup.Children.Add(circle);
                path.Stroke = new SolidColorBrush(System.Windows.Media.Colors.White);

                path.Data = GeoGroup;
                path.Tag = "tu";
                path.StrokeThickness = 2;
                path.MouseLeftButtonDown = (o, s) =>
                {
                    CalculateIntersection(path);
                };
                wpfUserControl.MyCanvas.Children.Add(path);
            }
        }

        private void CalculateIntersection(System.Windows.Shapes.Path path)
        {
            path.Stroke = new SolidColorBrush(System.Windows.Media.Colors.Yellow);


            if (path.Tag.ToString() == "line")
            {
                LineGeometry line = (LineGeometry)(((GeometryGroup)path.Data).Children[0]);

                double dx = line.StartPoint.X - line.EndPoint.X;
                double k = 0;
                double b = 0;

                if (dx != 0)
                {
                    k = (line.StartPoint.Y - line.EndPoint.Y) / (line.StartPoint.X - line.EndPoint.X);
                    b = line.StartPoint.Y - line.StartPoint.X;

                }
                else
                {
                    k = 1000;
                    b = 1000;
                }

           //     line1.Text = string.Format("{0},{1}", k.ToString(), b.ToString());


            }
        }
        
        private void AddEllipses()
        {
            foreach(var e in dxf.Ellipses)
            {
                RotateTransform trans = new RotateTransform(-e.Rotation, e.Center.X, -e.Center.Y);
                EllipseGeometry ellipse = new EllipseGeometry(new System.Windows.Point(e.Center.X, -e.Center.Y), e.MajorAxis / 2, e.MinorAxis / 2, trans);

                System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();

                GeometryGroup GeoGroup = new GeometryGroup();
                GeoGroup.Children.Add(ellipse);
                path.Stroke = new SolidColorBrush(System.Windows.Media.Colors.White);

                path.Data = GeoGroup;
                path.Tag = "ellipse";
                path.StrokeThickness = 2;
                path.MouseLeftButtonDown = (o, s) =>
                {
                    CalculateIntersection(path);
                };

                wpfUserControl.MyCanvas.Children.Add(path);
            }
        }
        private void AddArcs()
        {
            foreach (var a in dxf.Arcs)
            {
                double Sx = a.Center.X a.Radius * System.Math.Cos(a.StartAngle / 180 * Math.PI);
                double Sy = -a.Center.Y - a.Radius * System.Math.Sin(a.StartAngle / 180 * Math.PI);
                System.Windows.Point Start = new System.Windows.Point(Sx, Sy);

                double Ex = a.Center.X a.Radius * System.Math.Cos(a.EndAngle / 180 * Math.PI);
                double Ey = -a.Center.Y - a.Radius * System.Math.Sin(a.EndAngle / 180 * Math.PI);
                System.Windows.Point End = new System.Windows.Point(Ex, Ey);



                ArcSegment arc = new ArcSegment();
                arc.Point = End;

                arc.RotationAngle = a.EndAngle - a.StartAngle;
                arc.RotationAngle = arc.RotationAngle >= 0 ? arc.RotationAngle : arc.RotationAngle 360;


                arc.IsLargeArc = arc.RotationAngle > 180 ? true : false;
                arc.SweepDirection = SweepDirection.Counterclockwise;

                arc.Size = new System.Windows.Size(a.Radius, a.Radius);

                PathFigure path = new PathFigure();
                path.StartPoint = Start;
                path.Segments.Add(arc);

                PathGeometry pathgeo = new PathGeometry();
                pathgeo.Figures.Add(path);

                System.Windows.Shapes.Path _path = new System.Windows.Shapes.Path();

                GeometryGroup GeoGroup = new GeometryGroup();
                GeoGroup.Children.Add(pathgeo);
                _path.Stroke = new SolidColorBrush(System.Windows.Media.Colors.White);

                _path.Data = GeoGroup;
                _path.Tag = "arc";
                _path.StrokeThickness = 2;
                _path.MouseLeftButtonDown = (o, s) =>
                {
                    CalculateIntersection(_path);
                };


                wpfUserControl.MyCanvas.Children.Add(_path);
            }
        }
        private void AddPolylines()
        {
            foreach (var p in dxf.Polylines)
            {
                if (p.Flags == PolylineTypeFlags.OpenPolyline || p.Flags == PolylineTypeFlags.ClosedPolylineOrClosedPolygonMeshInM)
                {
                    LightWeightPolyline polygon = (LightWeightPolyline)p;
                    PathFigure path = new PathFigure();
                    float bulge = 0;
                    System.Windows.Point prePoint = new System.Windows.Point();
                    System.Windows.Point point = new System.Windows.Point();

                    path.IsClosed = polygon.IsClosed;

                    for (int i = 0; i < polygon.Vertexes.Count(); i)
                    {
                        var seg = polygon.Vertexes[i];
                        point = new System.Windows.Point(seg.Location.X, -seg.Location.Y);

                        if (i == 0)
                        {
                            path.StartPoint = point;
                            prePoint = point;
                            bulge = seg.Bulge;
                            //angle = 4 * System.Math.Atan(seg.Bulge) / Math.PI * 180;
                        }
                        else
                        {
                            ArcSegment arc = new ArcSegment();
                            arc.Point = point;

                            //if (angle != 0)
                            if (bulge != 0)
                            {
                                double angle = 4 * Math.Atan(Math.Abs(bulge)) / Math.PI * 180;
                                double length = Math.Sqrt((point.X - prePoint.X) * (point.X - prePoint.X) (point.Y - prePoint.Y) * (point.Y - prePoint.Y));
                                //double radius = length / (Math.Sqrt(2 * (1 - Math.Cos(angle / 180 * Math.PI))));

                                double radius = Math.Abs(length / (2 * Math.Sin(angle / 360 * Math.PI)));

                                arc.Size = new System.Windows.Size(radius, radius);
                                arc.RotationAngle = angle;

                                arc.SweepDirection = bulge < 0 ? SweepDirection.Clockwise : SweepDirection.Counterclockwise;
                                arc.IsLargeArc = Math.Abs(bulge) > 1 ? true : false;
                            }

                            prePoint = point;
                            bulge = seg.Bulge;
                            //angle = 4 * System.Math.Atan(seg.Bulge) / Math.PI * 180;
                            path.Segments.Add(arc);

                        }
                    }
                    PathGeometry pathgeo = new PathGeometry();
                    pathgeo.Figures.Add(path);

                    System.Windows.Shapes.Path _path = new System.Windows.Shapes.Path();

                    GeometryGroup GeoGroup = new GeometryGroup();
                    GeoGroup.Children.Add(pathgeo);
                    _path.Stroke = new SolidColorBrush(System.Windows.Media.Colors.White);

                    _path.Data = GeoGroup;
                    _path.Tag = "polylines";
                    _path.StrokeThickness = 2;
                    _path.MouseLeftButtonDown = (o, s) =>
                    {
                        CalculateIntersection(_path);
                    };


                    wpfUserControl.MyCanvas.Children.Add(_path);
                }
            }
        }

        private void AddText()
        {

            foreach (var t in dxf.Texts)
            {
                FormattedText formattedText = new FormattedText(t.Value, CultureInfo.GetCultureInfo("zh-CN"), System.Windows.FlowDirection.LeftToRight, new Typeface("仿宋体"), t.Height * (96.0 / 72.0), System.Windows.Media.Brushes.White);


                //SolidColorBrush brush = new SolidColorBrush(Colors.Orange);
                LinearGradientBrush brush = new LinearGradientBrush(
                            Colors.Orange,
                            Colors.Teal,
                            90.0);
                formattedText.SetForegroundBrush(brush);

                Geometry textGeometry = formattedText.BuildGeometry(new System.Windows.Point(t.BasePoint.X, -t.BasePoint.Y - t.Height * (96.0 / 72.0)));
                PathGeometry pathGeometry = textGeometry.GetFlattenedPathGeometry();

                foreach (var p in wpfUserControl.MyCanvas.Children)
                {
                    System.Windows.Shapes.Path path = (System.Windows.Shapes.Path)p;

                    if ((string)path.Tag == "Character")
                    {

                        ((GeometryGroup)path.Data).Children.Add(pathGeometry);
                    }
                }
                //Characters.Children.Add(pathGeometry);
            }
        }

        private void AddMText()
        {
            foreach (var t in dxf.MTexts)
            {
                FormattedText formattedText = new FormattedText(t.Value, CultureInfo.GetCultureInfo("zh-CN"), System.Windows.FlowDirection.LeftToRight, new Typeface("仿宋体"), t.Height * (96.0 / 72.0), System.Windows.Media.Brushes.White);

                double angle = 0;
                if (t.Normal.X != 0)
                {
                    angle = Math.Atan(t.Normal.Y / t.Normal.X) / Math.PI * 180;
                    angle = angle > 0 ? angle : angle 360;
                }
                else
                {
                    if (t.Normal.Y > 0)
                    {
                        angle = 90;
                    }
                    else if (t.Normal.Y < 0)
                    {
                        angle = 270;
                    }
                    else
                    {
                        angle = 0;
                    }

                }

                SolidColorBrush brush = new SolidColorBrush(Colors.White);
                //LinearGradientBrush brush = new LinearGradientBrush(
                //            Colors.Orange,
                //            Colors.Teal,
                //            90.0);

                formattedText.SetForegroundBrush(brush);

                //Geometry textGeometry = formattedText.BuildGeometry(new System.Windows.Point(t.BasePoint.X, -t.BasePoint.Y - t.Height * (96.0 / 72.0)));
                Geometry textGeometry = formattedText.BuildGeometry(new System.Windows.Point(t.BasePoint.X, -t.BasePoint.Y));

                RotateTransform rt = new RotateTransform(-angle, t.BasePoint.X, -t.BasePoint.Y);

                textGeometry.Transform = rt;

                PathGeometry pathGeometry = textGeometry.GetFlattenedPathGeometry();

                foreach (var p in  wpfUserControl.MyCanvas.Children)
                {
                    System.Windows.Shapes.Path path = (System.Windows.Shapes.Path)p;

                    if ((string)path.Tag == "Character")
                    {

                        ((GeometryGroup)path.Data).Children.Add(pathGeometry);
                    }
                }
                //Characters.Children.Add(pathGeometry);
            }
        }
        private void AddLines()
        {
            foreach (var l in dxf.Lines)
            {
                LineGeometry line = new LineGeometry(new System.Windows.Point(l.StartPoint.X, -l.StartPoint.Y), new System.Windows.Point(l.EndPoint.X, -l.EndPoint.Y));

                System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();

                GeometryGroup GeoGroup = new GeometryGroup();
                GeoGroup.Children.Add(line);
                path.Stroke = new SolidColorBrush(System.Windows.Media.Colors.White);

                path.Data = GeoGroup;
                path.Tag = "line";
                path.StrokeThickness = 2;
                path.MouseLeftButtonDown = (o, s) =>
                {
                    CalculateIntersection(path);
                };
                wpfUserControl.MyCanvas.Children.Add(path);
            }
        }
    }
}