在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例桌面应用界面/GUI → windorm 加载WPF控件 ,实现dxf文件显示

windorm 加载WPF控件 ,实现dxf文件显示

桌面应用界面/GUI

下载此实例
  • 开发语言:C#
  • 实例大小:0.78M
  • 下载次数:54
  • 浏览次数:522
  • 发布时间:2020-12-25
  • 实例类别:桌面应用界面/GUI
  • 发 布 人:xiaobai@2020
  • 文件格式:.rar
  • 所需积分:10
 相关标签: wpf DXF winform

实例介绍

【实例简介】本例程实现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);
            }
        }
    }
}

标签: wpf DXF winform

实例下载地址

windorm 加载WPF控件 ,实现dxf文件显示

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警