DxfView.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using netDxf.Entities;
  2. using OxyPlot;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using WpfLine = System.Windows.Shapes.Line;
  18. using DxfLine = netDxf.Entities.Line;
  19. using DxfLwPolyline = netDxf.Entities.Polyline2D;
  20. using Microsoft.Win32;
  21. using System.Drawing;
  22. namespace TeamAAS_VP.DxfModule
  23. {
  24. /// <summary>
  25. /// DxfView.xaml 的交互逻辑
  26. /// </summary>
  27. public partial class DxfView : UserControl
  28. {
  29. private DxfParserService _dxfParser;
  30. private InteractionController _interactionController;
  31. private double _currentScale = 1.0;
  32. private double _zoomFactor = 1.2;
  33. public DxfView()
  34. {
  35. InitializeComponent();
  36. _dxfParser = new DxfParserService();
  37. InitializeInteractionController();
  38. }
  39. #region 依赖属性-PointF集合
  40. public static readonly DependencyProperty PointsProperty =
  41. DependencyProperty.Register("Points", typeof(List<PointF>), typeof(DxfView),
  42. new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnPointsChanged)));
  43. public List<PointF> Points
  44. {
  45. get { return (List<PointF>)GetValue(PointsProperty); }
  46. set { SetValue(PointsProperty, value); }
  47. }
  48. private static void OnPointsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  49. {
  50. var view = d as DxfView;
  51. if (view != null)
  52. {
  53. // Handle points changed
  54. }
  55. }
  56. #endregion
  57. private void InitializeInteractionController()
  58. {
  59. _interactionController = new InteractionController(DrawingCanvas);
  60. _interactionController.SelectionChanged += OnSelectionChanged;
  61. }
  62. private void BtnLoadDxf_Click(object sender, RoutedEventArgs e)
  63. {
  64. var openFileDialog = new OpenFileDialog
  65. {
  66. Filter = "DXF文件 (*.dxf)|*.dxf|所有文件 (*.*)|*.*",
  67. Title = "选择DXF文件"
  68. };
  69. if (openFileDialog.ShowDialog() == true)
  70. {
  71. LoadDxfFile(openFileDialog.FileName);
  72. }
  73. }
  74. private void LoadDxfFile(string filePath)
  75. {
  76. TxtStatus.Text = "正在加载DXF文件...";
  77. DrawingCanvas.Children.Clear();
  78. _interactionController?.ClearSelection();
  79. if (_dxfParser.LoadDxfFile(filePath))
  80. {
  81. var entities = _dxfParser.GetAllEntities();
  82. TxtStatus.Text = $"成功加载: {System.IO.Path.GetFileName(filePath)}";
  83. TxtEntityCount.Text = $"实体数: {entities.Count}";
  84. // Calculate bounds and scale
  85. var bounds = _dxfParser.GetBounds();
  86. double boundsWidth = bounds.maxX - bounds.minX;
  87. double boundsHeight = bounds.maxY - bounds.minY;
  88. if (boundsWidth > 0 && boundsHeight > 0)
  89. {
  90. double scaleX = (DrawingCanvas.Width * 0.8) / boundsWidth;
  91. double scaleY = (DrawingCanvas.Height * 0.8) / boundsHeight;
  92. _currentScale = Math.Min(scaleX, scaleY);
  93. }
  94. // Render entities
  95. foreach (var entity in entities)
  96. {
  97. var renderedElements = GraphicsRenderer.RenderEntity(entity, _currentScale);
  98. foreach (var element in renderedElements)
  99. {
  100. DrawingCanvas.Children.Add(element);
  101. }
  102. }
  103. // Center the drawing
  104. CenterDrawing(bounds);
  105. // Update statistics
  106. UpdateStatistics();
  107. }
  108. else
  109. {
  110. TxtStatus.Text = "加载DXF文件失败";
  111. MessageBox.Show("无法加载DXF文件,请检查文件是否有效。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  112. }
  113. }
  114. private void CenterDrawing((double minX, double minY, double maxX, double maxY) bounds)
  115. {
  116. double centerX = (bounds.minX + bounds.maxX) / 2 * _currentScale;
  117. double centerY = -(bounds.minY + bounds.maxY) / 2 * _currentScale;
  118. double offsetX = DrawingCanvas.Width / 2 - centerX;
  119. double offsetY = DrawingCanvas.Height / 2 - centerY;
  120. // Translate all elements
  121. foreach (UIElement element in DrawingCanvas.Children)
  122. {
  123. if (element is WpfLine line)
  124. {
  125. line.X1 += offsetX;
  126. line.X2 += offsetX;
  127. line.Y1 += offsetY;
  128. line.Y2 += offsetY;
  129. }
  130. else if (element is FrameworkElement fe)
  131. {
  132. double left = Canvas.GetLeft(fe);
  133. double top = Canvas.GetTop(fe);
  134. Canvas.SetLeft(fe, left + offsetX);
  135. Canvas.SetTop(fe, top + offsetY);
  136. }
  137. }
  138. }
  139. private void UpdateStatistics()
  140. {
  141. var stats = _dxfParser.GetEntityStatistics();
  142. var sb = new StringBuilder();
  143. foreach (var stat in stats.OrderByDescending(s => s.Value))
  144. {
  145. if (stat.Value > 0)
  146. {
  147. sb.AppendLine($"{stat.Key}: {stat.Value}");
  148. }
  149. }
  150. TxtStatistics.Text = sb.ToString();
  151. }
  152. private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
  153. {
  154. TxtSelectedCount.Text = $"选中: {e.SelectedEntities.Count}";
  155. var sb = new StringBuilder();
  156. if (e.SelectedEntities.Count > 0)
  157. {
  158. sb.AppendLine($"已选择 {e.SelectedEntities.Count} 个图形实体:\n");
  159. var grouped = e.SelectedEntities.GroupBy(entity => entity.Type.ToString());
  160. foreach (var group in grouped)
  161. {
  162. sb.AppendLine($"{group.Key}: {group.Count()}");
  163. }
  164. sb.AppendLine("\n详细信息:");
  165. int index = 1;
  166. foreach (var entity in e.SelectedEntities.Take(10))
  167. {
  168. sb.AppendLine($"\n[{index}] {GetEntityInfo(entity)}");
  169. index++;
  170. }
  171. if (e.SelectedEntities.Count > 10)
  172. {
  173. sb.AppendLine($"\n... 还有 {e.SelectedEntities.Count - 10} 个实体");
  174. }
  175. //如果选中的是圆,则输出圆心坐标,如果是直线,则输出中心点坐标,如果是矩形,则输出中心点坐标,如果是点,则输出点坐标,如果是圆弧,则输出中心点坐标
  176. if (Points!=null)
  177. {
  178. Points = new List<PointF>();
  179. }
  180. Points.Clear();
  181. foreach (var item in e.SelectedEntities)
  182. {
  183. if (item is Circle circle)
  184. {
  185. Points.Add(new PointF((float)circle.Center.X, (float)circle.Center.Y));
  186. }
  187. else if (item is DxfLine line)
  188. {
  189. float centerX = (float)((line.StartPoint.X + line.EndPoint.X) / 2);
  190. float centerY = (float)((line.StartPoint.Y + line.EndPoint.Y) / 2);
  191. Points.Add(new PointF(centerX, centerY));
  192. }
  193. else if (item is DxfLwPolyline polyline && polyline.IsClosed)
  194. {
  195. //计算多边形中心点
  196. float centerX = 0;
  197. float centerY = 0;
  198. foreach (var vertex in polyline.Vertexes)
  199. {
  200. centerX += (float)vertex.Position.X;
  201. centerY += (float)vertex.Position.Y;
  202. }
  203. centerX /= polyline.Vertexes.Count;
  204. centerY /= polyline.Vertexes.Count;
  205. Points.Add(new PointF(centerX, centerY));
  206. }
  207. else if (item is netDxf.Entities.Point point)
  208. {
  209. Points.Add(new PointF((float)point.Position.X, (float)point.Position.Y));
  210. }
  211. else if (item is Arc arc)
  212. {
  213. Points.Add(new PointF((float)arc.Center.X, (float)arc.Center.Y));
  214. }
  215. }
  216. }
  217. else
  218. {
  219. sb.AppendLine("未选择任何图形");
  220. }
  221. TxtSelectedInfo.Text = sb.ToString();
  222. }
  223. private string GetEntityInfo(EntityObject entity)
  224. {
  225. switch (entity)
  226. {
  227. case DxfLine line:
  228. return $"直线: ({line.StartPoint.X:F2}, {line.StartPoint.Y:F2}) → ({line.EndPoint.X:F2}, {line.EndPoint.Y:F2})";
  229. case Circle circle:
  230. return $"圆: 中心({circle.Center.X:F2}, {circle.Center.Y:F2}), 半径={circle.Radius:F2}";
  231. case Arc arc:
  232. return $"圆弧: 中心({arc.Center.X:F2}, {arc.Center.Y:F2}), 半径={arc.Radius:F2}, 角度={arc.StartAngle:F1}°~{arc.EndAngle:F1}°";
  233. case Text text:
  234. return $"文本: \"{text.Value}\" 位置({text.Position.X:F2}, {text.Position.Y:F2})";
  235. case MText mtext:
  236. return $"多行文本: \"{mtext.Value}\" 位置({mtext.Position.X:F2}, {mtext.Position.Y:F2})";
  237. case DxfLwPolyline lwPoly:
  238. return $"轻量多段线: {lwPoly.Vertexes.Count} 个顶点{(lwPoly.IsClosed ? " (闭合)" : "")}";
  239. case Spline spline:
  240. // return $"样条曲线: {spline.ControlPoints.Count} 个控制点";
  241. default:
  242. return $"{entity.Type}: {entity.CodeName}";
  243. }
  244. }
  245. private void BtnClearSelection_Click(object sender, RoutedEventArgs e)
  246. {
  247. Points?.Clear();
  248. _interactionController?.ClearSelection();
  249. }
  250. private void BtnExtractSelected_Click(object sender, RoutedEventArgs e)
  251. {
  252. var selectedEntities = _interactionController?.GetSelectedEntities();
  253. if (selectedEntities == null || selectedEntities.Count == 0)
  254. {
  255. MessageBox.Show("请先选择要提取的图形。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  256. return;
  257. }
  258. var sb = new StringBuilder();
  259. sb.AppendLine($"提取的图形信息 (共 {selectedEntities.Count} 个):\n");
  260. var grouped = selectedEntities.GroupBy(p => p.Type.ToString());
  261. foreach (var group in grouped)
  262. {
  263. sb.AppendLine($"\n{group.Key} ({group.Count()} 个):");
  264. sb.AppendLine(new string('-', 50));
  265. foreach (var entity in group)
  266. {
  267. sb.AppendLine(GetEntityInfo(entity));
  268. }
  269. }
  270. MessageBox.Show(sb.ToString(), "提取的图形信息", MessageBoxButton.OK, MessageBoxImage.Information);
  271. }
  272. private void BtnZoomIn_Click(object sender, RoutedEventArgs e)
  273. {
  274. Zoom(_zoomFactor);
  275. }
  276. private void BtnZoomOut_Click(object sender, RoutedEventArgs e)
  277. {
  278. Zoom(1.0 / _zoomFactor);
  279. }
  280. private void BtnFitToScreen_Click(object sender, RoutedEventArgs e)
  281. {
  282. CanvasScaleTransform.ScaleX = 1.0;
  283. CanvasScaleTransform.ScaleY = 1.0;
  284. UpdateZoomDisplay();
  285. }
  286. private void DrawingCanvas_MouseWheel(object sender, MouseWheelEventArgs e)
  287. {
  288. double zoom = e.Delta > 0 ? _zoomFactor : 1.0 / _zoomFactor;
  289. Zoom(zoom);
  290. }
  291. private void Zoom(double factor)
  292. {
  293. CanvasScaleTransform.ScaleX *= factor;
  294. CanvasScaleTransform.ScaleY *= factor;
  295. UpdateZoomDisplay();
  296. }
  297. private void UpdateZoomDisplay()
  298. {
  299. TxtZoom.Text = $"缩放: {CanvasScaleTransform.ScaleX * 100:F0}%";
  300. }
  301. }
  302. }