DxfAnalysis.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.fdkankan.dxf.parse.analysis;
  2. import com.fdkankan.dxf.parse.enums.CadVersionEnum;
  3. import com.fdkankan.dxf.parse.enums.DxfSystemEnum;
  4. import com.fdkankan.dxf.parse.enums.entities.*;
  5. import com.fdkankan.dxf.parse.model.GeometricModel;
  6. import com.fdkankan.dxf.parse.model.headers.HeaderModel;
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.List;
  14. import java.util.Map;
  15. /**
  16. * @AUTHOR ZhangJunJie
  17. * @DATE 2023/11/25
  18. * <p/>
  19. * 概要:DXF 文件解析
  20. */
  21. public class DxfAnalysis {
  22. /**
  23. * 读取所有的行
  24. *
  25. * @param reader BufferedReader
  26. * @return 返回文件的所有数据,以每一行数据为一个item
  27. * @throws IOException IO异常
  28. */
  29. private static List<String> readAllLine(BufferedReader reader) throws IOException {
  30. List<String> list = new ArrayList<>();;
  31. String line = null;
  32. while ((line = reader.readLine()) != null) {
  33. list.add(line.trim());
  34. }
  35. return list;
  36. }
  37. /**
  38. * 解析dxf文件头信息,形成对象模型
  39. *
  40. * @param inputStream 文件流
  41. * @param charset 字符编码 (UTF-8 GBK)
  42. * @return
  43. * @throws IOException
  44. */
  45. public static HeaderModel parseDxfHeaderModel(InputStream inputStream, String charset) throws IOException {
  46. try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
  47. List<String> dxfAllLine = readAllLine(reader);
  48. return DxfHeaderParse.getHeaderParse(dxfAllLine);
  49. }
  50. }
  51. /**
  52. * 返回几何图像解析数据
  53. *
  54. * @param inputStream dxf文件流
  55. * @param dxfHeaderModel dxf解析出的头信息
  56. * @return
  57. * @throws IOException
  58. */
  59. public static Map<String, List<GeometricModel>> parseDxfGeometricList(InputStream inputStream, HeaderModel dxfHeaderModel) throws IOException {
  60. /**
  61. * 从cad版本信息中获取字符编码
  62. */
  63. CadVersionEnum cadVersionEnum = CadVersionEnum.getCadVersion(dxfHeaderModel.getCadVersion());
  64. try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, cadVersionEnum.getCharset()))) {
  65. Map<String, List<GeometricModel>> map = new HashMap<>();
  66. //读取dxf所有的数据
  67. List<String> lineList = readAllLine(reader);
  68. // 解析dxf文件结构
  69. parseFile(lineList, map, dxfHeaderModel);
  70. return map;
  71. }
  72. }
  73. /**
  74. * 解析dxf文件结构
  75. *
  76. * @param lineList 总数据
  77. * @param map 接收解析的数据map
  78. */
  79. private static void parseFile(List<String> lineList, Map<String, List<GeometricModel>> map, HeaderModel dxfHeaderModel) {
  80. if (lineList == null || lineList.isEmpty()) {
  81. return;
  82. }
  83. int i = 0;
  84. String str = lineList.get(i);
  85. //未到文件结束标志
  86. while (!DxfSystemEnum.FILE_END.getCode().equals(str)) {
  87. str = lineList.get(++i);
  88. //实体段开始
  89. if (DxfSystemEnum.ENTITIES_START.getCode().equals(str)) {
  90. //解析实体
  91. parseEntities(i, lineList, map, dxfHeaderModel);
  92. }
  93. // 文件循环语句结束
  94. }
  95. // 解析函数结束
  96. }
  97. /**
  98. * 解析实体
  99. *
  100. * @param i 实体开始读取的行数
  101. * @param lineList 总数据
  102. * @param map 接收解析的数据map
  103. * @param dxfHeaderModel dxf文件的头信息模型
  104. */
  105. private static void parseEntities(int i, List<String> lineList, Map<String, List<GeometricModel>> map, HeaderModel dxfHeaderModel) {
  106. String str = null;
  107. while (true) {
  108. str = lineList.get(++i);
  109. //点开始
  110. if (PointEnum.POINT_NAME.getCode().equals(str)) {
  111. i = DxfEntitiesParse.getPoint(i, lineList, map);
  112. }
  113. //圆开始
  114. if (CircleEnum.CIRCLE_NAME.getCode().equals(str)) {
  115. i = DxfEntitiesParse.getCircle(i, lineList, map);
  116. }
  117. // 椭圆开始
  118. if (EllipseEnum.ELLIPSE_NAME.getCode().equals(str)) {
  119. // TODO 解析椭圆
  120. }
  121. // 圆弧开始
  122. if (ArcEnum.ARC_NAME.getCode().equals(str)) {
  123. i = DxfEntitiesParse.getArc(i, lineList, map);
  124. }
  125. //直线开始
  126. if (LineEnum.LINE_NAME.getCode().equals(str)) {
  127. i = DxfEntitiesParse.getLine(i, lineList, map);
  128. }
  129. //多线段
  130. if (PolyLineEnum.LWPOLYLINE_NAME.getCode().equals(str)) {
  131. i = DxfEntitiesParse.getLWPolyLine(i, lineList, map);
  132. }
  133. // 多线段
  134. if (PolyLineEnum.POLYLINE_NAME.getCode().equals(str)) {
  135. i = DxfEntitiesParse.getPolyLine(i, lineList, map);
  136. }
  137. // 文本
  138. if (TextEnum.TEXT_NAME.getCode().equals(str)) {
  139. i = DxfEntitiesParse.getText(i, lineList, map);
  140. }
  141. // 多文本
  142. if (MTextEnum.MTEXT_NAME.getCode().equals(str)) {
  143. i = DxfEntitiesParse.getMText(i, lineList, map);
  144. }
  145. // 插入图元
  146. if (InsertEnum.INSERT_NAME.getCode().equals(str)) {
  147. i = DxfEntitiesParse.getInsert(i, lineList, map);
  148. }
  149. //实体结束
  150. if (DxfSystemEnum.END_SEC.getCode().equals(str)) {
  151. break;
  152. }
  153. }
  154. }
  155. }