using netDxf; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace MetroGauges.Controls { /// /// GeoWidget.xaml 的交互逻辑 /// public partial class GeoWidget : UserControl { private string m_BlockPath; private DesignControl m_designCtrl; public DesignControl DesignCtrl { get { return m_designCtrl; } } public string BlockName { get; set; } public GeoWidget() { InitializeComponent(); this.MouseLeftButtonUp += GeoWidget_MouseLeftButtonUp; //拦截,不拦截会影响到画布拖动。 this.Loaded += GeoWidget_Loaded; m_BlockPath = AppDomain.CurrentDomain.BaseDirectory + "Blocks\\"; } private void GeoWidget_Loaded(object sender, RoutedEventArgs e) { m_designCtrl = this.geoThumb.Template.FindName("designCtrl", this.geoThumb) as DesignControl; m_designCtrl.CancelMove = true; m_designCtrl.ScaleVisble = false; //LoadBlockData(); } private void GeoWidget_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { e.Handled = true; } public void LoadBlockData() { DxfDocument dxf = DxfDocument.Load(m_BlockPath + BlockName+".dxf"); if (dxf.Blocks.Count > 0 && dxf.Blocks.Contains(BlockName)) { netDxf.Blocks.Block block = dxf.Blocks[BlockName]; foreach (netDxf.Entities.EntityObject item in block.Entities) { switch (item.Type.ToString()) { case "Line": AddLine(item); break; case "Polyline": case "LightWeightPolyline": AddPolyline(item); break; } } } else { MessageBox.Show("找不到块数据", "错误"); } } public void AddLine(netDxf.Entities.EntityObject line) { double xyRate = this.m_designCtrl.ScaleRale; Line drawline = new Line(); netDxf.Entities.Line dataLine = line as netDxf.Entities.Line; drawline.X1 = dataLine.StartPoint.X / xyRate; drawline.Y1 = -dataLine.StartPoint.Y / xyRate; drawline.X2 = dataLine.EndPoint.X / xyRate; drawline.Y2 = -dataLine.EndPoint.Y / xyRate; drawline.Stroke = Brushes.White; drawline.StrokeThickness = 1; m_designCtrl.canvasDesign.Children.Add(drawline); } public void AddPolyline(netDxf.Entities.EntityObject line) { double xyRate = this.m_designCtrl.ScaleRale; PointCollection ptcol = new PointCollection(); switch (line.Type.ToString()) { case "Polyline": netDxf.Entities.Polyline polyline = line as netDxf.Entities.Polyline; foreach (netDxf.Entities.PolylineVertex item in polyline.Vertexes) { ptcol.Add(new Point() { X = item.Location.X / xyRate, Y = -item.Location.Y / xyRate }); } break; case "LightWeightPolyline": netDxf.Entities.LwPolyline lwline = line as netDxf.Entities.LwPolyline; foreach (var item in lwline.Vertexes) { ptcol.Add(new Point() { X = item.Location.X / xyRate, Y = -item.Location.Y / xyRate }); } if (lwline.IsClosed) { Vector2 last = lwline.Vertexes[0].Location; ptcol.Add(new Point() { X = last.X / xyRate, Y = -last.Y / xyRate }); } break; } Polyline pl = new Polyline { Stroke = Brushes.White, StrokeThickness = 1, Points = ptcol, }; this.m_designCtrl.canvasDesign.Children.Add(pl); } public void AddCircle(Ellipse circle) { m_designCtrl.canvasDesign.Children.Add(circle); } public void AddRectangle(Rectangle rectangle) { m_designCtrl.canvasDesign.Children.Add(rectangle); } public void AddText1(String text) { TextBlock textBlock = new TextBlock(); textBlock.Text = text; textBlock.Foreground = Brushes.White; m_designCtrl.canvasDesign.Children.Add(textBlock); } /// /// 移动控件 /// /// /// private void geoThumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) { double X = Canvas.GetLeft(this); double Y = Canvas.GetTop(this); Canvas.SetLeft(this, X + e.HorizontalChange); Canvas.SetTop(this, Y + e.VerticalChange); //isMoved = true; } } }