Dxf.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. namespace BaseLibRWFile
  6. {
  7. public class Dxf
  8. {
  9. public struct DataLineStruct //线状结构
  10. {
  11. //public int iLineType;
  12. public float fX1Pos;
  13. public float fY1Pos;
  14. public float fZ1Pos;
  15. public float fX2Pos;
  16. public float fY2Pos;
  17. public float fZ2Pos;
  18. };
  19. public struct DataPolyLineStruct //聚合结构
  20. {
  21. //public int iLineType;
  22. public int iNum;
  23. public float fX1Pos;
  24. public float fY1Pos;
  25. public float fX2Pos;
  26. public float fY2Pos;
  27. };
  28. public struct DataArcStruct //圆弧或拱形结构
  29. {
  30. public float fXPos;
  31. public float fYPos;
  32. public float fX1Pos;
  33. public float fY1Pos;
  34. public float fRPos;
  35. public float fptXPos;
  36. public float fptYPos;
  37. public float fAngle1Pos;
  38. public float fAngle2Pos;
  39. public float fIPos;
  40. public float fJPos;
  41. };
  42. public struct DataCircleStruct //圆形结构
  43. {
  44. public float fXPos;
  45. public float fYPos;
  46. public float fZPos;
  47. public float fRPos;
  48. };
  49. public struct DataTypeStruct //字体结构
  50. {
  51. //public int iCutNum;
  52. //public bool xIsMoveDown;
  53. //public int iLineType;
  54. //public float fXPos;
  55. //public float fYPos;
  56. //public float fIPos;
  57. //public float fJPos;
  58. };
  59. static List<DataPolyLineStruct> stuPolyLineArray = new List<DataPolyLineStruct>(); //聚合结构集合
  60. //static List<DataTypeStruct> stuDataArray = new List<DataTypeStruct>(); //字体结构集合
  61. static List<DataLineStruct> stuLineArray = new List<DataLineStruct>(); //线状结构集合
  62. static List<DataArcStruct> stuArcArray = new List<DataArcStruct>(); //圆弧或拱形结构集合
  63. static List<DataCircleStruct> stuCircleArray = new List<DataCircleStruct>(); //圆形结构集合
  64. private static FileInfo theSourceFile;
  65. private static double XMax, XMin;
  66. private static double YMax, YMin;
  67. private static double ZMax, ZMin;
  68. //static int xStrat, xEnd, yStart, yEnd;
  69. static bool xIsRead = false;
  70. //public static int Pointnum;
  71. public static string[][] ReadDXF(string filePath, string filename, out int Pointnum)
  72. {
  73. string[][] Allpoint = new string[1000][];
  74. //xStrat = xEnd = yStart = yEnd = 0;
  75. for (int i = 0; i < 1000; i++)
  76. {
  77. Allpoint[i] = new string[10];
  78. for (int j = 0; j < 10; j++)
  79. {
  80. Allpoint[i][j] = null;
  81. }
  82. }
  83. string line1, line2; //这些line1和line2用于获取a/m数据
  84. List<string> table1 = new List<string>();
  85. List<string> table2 = new List<string>();
  86. line1 = "0"; //初始化line1 and line2
  87. line2 = "0";
  88. Pointnum = 0;
  89. filename = filePath + "\\" + filename; //treeView1.SelectedNode.Text;//e.Node.Text;
  90. stuLineArray.Clear();
  91. stuCircleArray.Clear();
  92. stuArcArray.Clear();
  93. stuPolyLineArray.Clear();
  94. theSourceFile = new FileInfo(filename); //设定读取文件位置
  95. StreamReader reader; //StreamReader reader = new StreamReader(filename, Encoding.Default);
  96. reader = theSourceFile.OpenText(); //读取DXF文件
  97. //该部分对DXF文件中的绘图对象进行解析,判断线段类型
  98. do
  99. {
  100. table1.Add(line1);
  101. table2.Add(line2);
  102. if (line1 == "2" && line2 == "ENTITIES")
  103. {
  104. xIsRead = true;
  105. }
  106. if (line2 == "ENDSEC")
  107. {
  108. xIsRead = false;
  109. }
  110. if (xIsRead)
  111. {
  112. if (line1 == "0" && line2 == "LINE")
  113. {
  114. float x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  115. LineModule(reader, ref x1, ref y1, ref x2, ref y2);
  116. Allpoint[Pointnum][0] = (Pointnum + 1).ToString();
  117. Allpoint[Pointnum][1] = "LINE";
  118. Allpoint[Pointnum][2] = x1.ToString();
  119. Allpoint[Pointnum][3] = y1.ToString();
  120. Allpoint[Pointnum][4] = x2.ToString();
  121. Allpoint[Pointnum][5] = y2.ToString();
  122. Pointnum++;
  123. }
  124. //else if (line1 == "0" && line2 == "LWPOLYLINE");
  125. //PolylineModule(reader);
  126. else if (line1 == "0" && line2 == "CIRCLE")
  127. {
  128. float x1 = 0, y1 = 0, radius = 0;
  129. CircleModule(reader, ref x1, ref y1, ref radius);
  130. Allpoint[Pointnum][0] = (Pointnum + 1).ToString();
  131. Allpoint[Pointnum][1] = "CIRCLE";
  132. Allpoint[Pointnum][2] = x1.ToString();
  133. Allpoint[Pointnum][3] = y1.ToString();
  134. Allpoint[Pointnum][4] = radius.ToString();
  135. Pointnum++;
  136. }
  137. else if (line1 == "0" && line2 == "ARC")
  138. {
  139. float x1 = 0, y1 = 0, x2 = 0, y2 = 0, radius = 0;
  140. ArcModule(reader, ref x1, ref y1, ref x2, ref y2, ref radius);
  141. Allpoint[Pointnum][0] = (Pointnum + 1).ToString();
  142. Allpoint[Pointnum][1] = "ARC";
  143. Allpoint[Pointnum][4] = x1.ToString();
  144. Allpoint[Pointnum][5] = y1.ToString();
  145. Allpoint[Pointnum][2] = x2.ToString();
  146. Allpoint[Pointnum][3] = y2.ToString();
  147. Allpoint[Pointnum][6] = radius.ToString();
  148. Pointnum++;
  149. }
  150. else if (line1 == "0" && line2 == "POLYLINE")
  151. PolylineModule(reader);
  152. //else if (line1 == "0" && line2 == "TEXT")
  153. // ;
  154. }
  155. GetLineCouple(reader, out line1, out line2); // 区分线段类型,将值赋给line1和line2
  156. }
  157. while (line2 != "EOF");
  158. reader.DiscardBufferedData(); //清除读取状态.
  159. theSourceFile = null;
  160. reader.Close();
  161. return Allpoint;
  162. //PaintBack();
  163. //Compare();
  164. //Creat();
  165. }
  166. private static void GetLineCouple(StreamReader theReader, out string line1, out string line2) //区分线段类型,将值赋给line1和line2
  167. {
  168. System.Globalization.CultureInfo ci = Thread.CurrentThread.CurrentCulture;//
  169. string decimalSeparator = ci.NumberFormat.CurrencyDecimalSeparator;
  170. line1 = line2 = "";
  171. if (theReader == null)
  172. return;
  173. line1 = theReader.ReadLine();
  174. if (line1 != null)
  175. {
  176. line1 = line1.Trim();
  177. line1 = line1.Replace('.', decimalSeparator[0]);
  178. }
  179. line2 = theReader.ReadLine();
  180. if (line2 != null)
  181. {
  182. line2 = line2.Trim();
  183. line2 = line2.Replace('.', decimalSeparator[0]);
  184. }
  185. }
  186. private static void LineModule(StreamReader reader, ref float x1, ref float y1, ref float x2, ref float y2) //解析DXF文件中的线段
  187. {
  188. string line1, line2;
  189. line1 = "0";
  190. line2 = "0";
  191. x1 = 0;
  192. y1 = 0;
  193. float z1 = 0;
  194. x2 = 0;
  195. y2 = 0;
  196. float z2 = 0;
  197. DataLineStruct stu1 = new DataLineStruct();
  198. do
  199. {
  200. GetLineCouple(reader, out line1, out line2);
  201. if (line1 == "10")
  202. {
  203. x1 = (float)Convert.ToDouble(line2);
  204. if (x1 > XMax)
  205. XMax = x1;
  206. if (x1 < XMin)
  207. XMin = x1;
  208. }
  209. if (line1 == "20")
  210. {
  211. y1 = (float)Convert.ToDouble(line2);
  212. if (y1 > YMax)
  213. YMax = y1;
  214. if (y1 < YMin)
  215. YMin = y1;
  216. }
  217. if (line1 == "30")
  218. {
  219. z1 = (float)Convert.ToDouble(line2);
  220. if (z1 > ZMax)
  221. ZMax = y1;
  222. if (z1 < ZMin)
  223. ZMin = y1;
  224. }
  225. if (line1 == "11")
  226. {
  227. x2 = (float)Convert.ToDouble(line2);
  228. if (x2 > XMax)
  229. XMax = x2;
  230. if (x2 < XMin)
  231. XMin = x2;
  232. }
  233. if (line1 == "21")
  234. {
  235. y2 = (float)Convert.ToDouble(line2);
  236. if (y2 > YMax)
  237. YMax = y2;
  238. if (y2 < YMin)
  239. YMin = y2;
  240. }
  241. if (line1 == "31")
  242. {
  243. z2 = (float)Convert.ToDouble(line2);
  244. if (z2 > ZMax)
  245. ZMax = y1;
  246. if (z2 < ZMin)
  247. ZMin = y1;
  248. }
  249. }
  250. while (line1 != "31");
  251. stu1.fX1Pos = x1;
  252. stu1.fX2Pos = x2;
  253. stu1.fZ1Pos = z1;
  254. stu1.fZ2Pos = z2;
  255. stu1.fY1Pos = y1;
  256. stu1.fY2Pos = y2;
  257. //DXFLibrary.Line line3 = new DXFLibrary.Line("Doors", 0, 0, 0, 10);
  258. //DXFLibrary.Text text = new DXFLibrary.Text("L" + stuLineArray.Count.ToString(), x1, y1, 40, "PartialHeightDoors");
  259. stuLineArray.Add(stu1);
  260. }
  261. private static void CircleModule(StreamReader reader, ref float x1, ref float y1, ref float radius) //解析DXF文件中的圆
  262. {
  263. string line1, line2;
  264. line1 = "0";
  265. line2 = "0";
  266. x1 = 0;
  267. y1 = 0;
  268. float z1 = 0;
  269. radius = 0;
  270. DataCircleStruct stu1;
  271. do
  272. {
  273. GetLineCouple(reader, out line1, out line2);
  274. if (line1 == "10")
  275. {
  276. x1 = (float)Convert.ToDouble(line2);
  277. }
  278. if (line1 == "20")
  279. {
  280. y1 = (float)Convert.ToDouble(line2);
  281. }
  282. if (line1 == "30")
  283. {
  284. z1 = (float)Convert.ToDouble(line2);
  285. }
  286. if (line1 == "40")
  287. {
  288. radius = (float)Convert.ToDouble(line2);
  289. if (x1 + radius > XMax)
  290. XMax = x1 + radius;
  291. if (x1 - radius < XMin)
  292. XMin = x1 - radius;
  293. if (y1 + radius > YMax)
  294. YMax = y1 + radius;
  295. if (y1 - radius < YMin)
  296. YMin = y1 - radius;
  297. }
  298. }
  299. while (line1 != "40");
  300. stu1.fXPos = x1;
  301. stu1.fYPos = y1;
  302. stu1.fZPos = z1;
  303. stu1.fRPos = radius;
  304. stuCircleArray.Add(stu1);
  305. }
  306. private static void ArcModule(StreamReader reader, ref float x1, ref float y1, ref float x2, ref float y2, ref float radius) //解析DXF文件中圆弧
  307. {
  308. string line1, line2;
  309. line1 = "0";
  310. line2 = "0";
  311. float ptx = 0;
  312. float pty = 0;
  313. x1 = 0;
  314. y1 = 0;
  315. x2 = 0;
  316. y2 = 0;
  317. radius = 0;
  318. float angle1 = 0;
  319. float angle2 = 0;
  320. float i = 0;
  321. float j = 0;
  322. DataArcStruct stu1;
  323. do
  324. {
  325. GetLineCouple(reader, out line1, out line2);
  326. if (line1 == "10")
  327. {
  328. ptx = (float)Convert.ToDouble(line2);
  329. if (ptx > XMax)
  330. XMax = ptx;
  331. if (ptx < XMin)
  332. XMin = ptx;
  333. }
  334. if (line1 == "20")
  335. {
  336. pty = (float)Convert.ToDouble(line2);
  337. if (pty > YMax)
  338. YMax = pty;
  339. if (pty < YMin)
  340. YMin = pty;
  341. }
  342. if (line1 == "40")
  343. {
  344. radius = (float)Convert.ToDouble(line2);
  345. if (ptx + radius > XMax)
  346. XMax = ptx + radius;
  347. if (ptx - radius < XMin)
  348. XMin = ptx - radius;
  349. if (pty + radius > YMax)
  350. YMax = pty + radius;
  351. if (pty - radius < YMin)
  352. YMin = pty - radius;
  353. }
  354. if (line1 == "50")
  355. angle1 = (float)Convert.ToDouble(line2);
  356. if (line1 == "51")
  357. angle2 = (float)Convert.ToDouble(line2);
  358. }
  359. while (line1 != "51");
  360. x1 = ptx + (float)Math.Cos(angle1 / 180.0 * Math.PI) * radius;
  361. y1 = pty + (float)Math.Sin(angle1 / 180.0 * Math.PI) * radius;
  362. x2 = ptx + (float)Math.Cos(angle2 / 180.0 * Math.PI) * radius;
  363. y2 = pty + (float)Math.Sin(angle2 / 180.0 * Math.PI) * radius;
  364. i = ptx - x1;
  365. j = pty - y1;
  366. stu1.fXPos = x1;
  367. stu1.fYPos = y1;
  368. stu1.fX1Pos = x2;
  369. stu1.fY1Pos = y2;
  370. stu1.fIPos = i;
  371. stu1.fJPos = j;
  372. stu1.fRPos = radius;
  373. stu1.fptXPos = ptx;
  374. stu1.fptYPos = pty;
  375. stu1.fAngle1Pos = angle1;
  376. stu1.fAngle2Pos = angle2;
  377. stuArcArray.Add(stu1);
  378. //这部分与绘图编辑器有关。从dxf文件中获取的数据
  379. // if ((Math.Abs(XMax) - Math.Abs(XMin)) > this.pictureBox1.Size.Width)
  380. // {
  381. // scaleX = (double)(this.pictureBox1.Size.Width) / (double)(Math.Abs(XMax) - Math.Abs(XMin));
  382. // }
  383. // else
  384. // scaleX = 1;
  385. //
  386. //
  387. // if ((Math.Abs(YMax) - Math.Abs(YMin)) > this.pictureBox1.Size.Height)
  388. // {
  389. // scaleY = (double)(this.pictureBox1.Size.Height) / (double)(Math.Abs(YMax) - Math.Abs(YMin));
  390. // }
  391. // else
  392. // scaleY = 1;
  393. //
  394. // mainScale = Math.Min(scaleX, scaleY);
  395. //
  396. //
  397. // int ix = drawingList.Add(new arc(new Point((int)x1, (int)-y1), radius, angle1, angle2, Color.White, Color.Red, 1));
  398. // objectIdentifier.Add(new DrawingObject(6, ix));
  399. }
  400. private static void PolylineModule(StreamReader reader) //解析DXF文件中折线
  401. {
  402. string line1, line2;
  403. line1 = "0";
  404. line2 = "0";
  405. float x1 = 0;
  406. float y1 = 0;
  407. float x2 = 0;
  408. float y2 = 0;
  409. int count = 0;
  410. int num = 1;
  411. DataPolyLineStruct stu1 = new DataPolyLineStruct();
  412. do
  413. {
  414. GetLineCouple(reader, out line1, out line2);
  415. if (line1 == "10")
  416. {
  417. if (count <= 1)
  418. {
  419. x1 = (float)Convert.ToDouble(line2);
  420. }
  421. else
  422. {
  423. x2 = (float)Convert.ToDouble(line2);
  424. }
  425. }
  426. if (line1 == "20")
  427. {
  428. if (count <= 1)
  429. {
  430. y1 = (float)Convert.ToDouble(line2);
  431. }
  432. else
  433. {
  434. y2 = (float)Convert.ToDouble(line2);
  435. }
  436. }
  437. if (line1 == "0" && line2 == "VERTEX")
  438. {
  439. if (count > 1)
  440. {
  441. stu1.fX1Pos = x1;
  442. stu1.fX2Pos = x2;
  443. stu1.fY1Pos = y1;
  444. stu1.fY2Pos = y2;
  445. stu1.iNum = num;
  446. stuPolyLineArray.Add(stu1);
  447. x1 = x2;
  448. y1 = y2;
  449. num++;
  450. }
  451. count += 1;
  452. }
  453. }
  454. while (line2 != "SEQEND");
  455. }
  456. }
  457. }