DxfView.xaml.cs 12 KB

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