Program.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. using System.Drawing;
  2. using System.Net;
  3. using System.Runtime.CompilerServices;
  4. using System.Text;
  5. namespace CAD2XMAL
  6. {
  7. internal class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. StringBuilder sb = new();
  12. StringBuilder sbRaw = new();
  13. //string path = string.Empty;
  14. using StreamReader sr = new(@"E:\Test.txt");
  15. string s = sr.ReadToEnd();
  16. string[] t1 = s.Split("\r\n");
  17. Console.Clear();
  18. Dictionary<RectangleF, DrawData> RecButton = [];
  19. Dictionary<RectangleF, DrawData> CirButton = [];
  20. Dictionary<RectangleF, DrawData> RecBorder = [];
  21. Dictionary<RectangleF, DrawData> CirBorder = [];
  22. List<DrawLine> Line = [];
  23. HashSet<string> Names = [];
  24. string strock = "#000000";
  25. string pathStart1 = string.Empty;
  26. string pathStart2 = string.Empty;
  27. string geo = string.Empty;
  28. string pathEnd1 = string.Empty;
  29. string pathEnd2 = string.Empty;
  30. foreach (var item in t1)
  31. {
  32. if (item.Contains("<TextBlock") || item.Contains("<Span") || item.Contains("</TextBlock>"))
  33. {
  34. sb.AppendLine(item);
  35. }
  36. if (item.Contains("Stroke"))
  37. {
  38. string temp = item.Substring(item.IndexOf("Stroke=\""));
  39. string[] color = temp.Split("\"");
  40. strock = color[1];
  41. }
  42. switch (item)
  43. {
  44. case string sx when sx.Contains("<Path "):
  45. pathStart1 = item.Trim();
  46. break;
  47. case string sx when sx.Contains("<Path.Data>"):
  48. pathStart2 = item.Trim();
  49. break;
  50. case string sx when sx.Contains("</Path.Data>"):
  51. pathEnd1 = item.Trim();
  52. break;
  53. case string sx when sx.Contains("</Path>"):
  54. if (string.IsNullOrEmpty(geo))
  55. break;
  56. pathEnd2 = item.Trim();
  57. sbRaw.AppendLine(pathStart1);
  58. sbRaw.AppendLine(pathStart2);
  59. sbRaw.AppendLine(geo);
  60. sbRaw.AppendLine(pathEnd1);
  61. sbRaw.AppendLine(pathEnd2);
  62. geo = string.Empty;
  63. break;
  64. default:
  65. break;
  66. }
  67. if (!item.Contains("PathGeometry"))
  68. continue;
  69. string[] test = item.Split('"');
  70. test = test[1].Split(" ");
  71. switch (test.Length)
  72. {
  73. case 6:
  74. {
  75. string X1 = test[1];
  76. string Y1 = test[2];
  77. string X2 = test[4];
  78. string Y2 = test[5];
  79. Line.Add(new(X1, Y1, X2, Y2, strock));
  80. break;
  81. }
  82. case 16:
  83. {
  84. float dx1 = Convert.ToSingle(test[4]);
  85. float dy1 = Convert.ToSingle(test[5]);
  86. float dx2 = Convert.ToSingle(test[7]);
  87. float dy2 = Convert.ToSingle(test[8]);
  88. float dx3 = Convert.ToSingle(test[10]);
  89. float dy3 = Convert.ToSingle(test[11]);
  90. float dx4 = Convert.ToSingle(test[13]);
  91. float dy4 = Convert.ToSingle(test[14]);
  92. float[] dx = [dx1, dx2, dx3, dx4];
  93. float[] dy = [dy1, dy2, dy3, dy4];
  94. float width = dx.Max() - dx.Min();
  95. float height = dy.Max() - dy.Min();
  96. float left = dx.Min();
  97. float top = dy.Min();
  98. if (width > 120 && height > 120)
  99. RecBorder.TryAdd(new RectangleF(left, top, width, height), new(width, height, left, top, strock));
  100. else
  101. RecButton.TryAdd(new RectangleF(left, top, width, height), new(width, height, left, top, strock));
  102. }
  103. break;
  104. case 22:
  105. {
  106. float dStart = Convert.ToSingle(test[1]);
  107. float dCenter = Convert.ToSingle(test[2]);
  108. float dr = Convert.ToSingle(test[4]);
  109. float width = dr * 2;
  110. float height = dr * 2;
  111. float left = dStart - (dr * 2);
  112. float top = dCenter - dr;
  113. switch (dr)
  114. {
  115. case > 15:
  116. CirButton.TryAdd(new RectangleF(left, top, width, height), new(width, height, left, top, strock));
  117. break;
  118. default:
  119. //Console.WriteLine($"<Border BorderBrush=\"Black\" Width=\"{dr * 2}\" Height=\"{dr * 2}\" CornerRadius=\"{dr * 2}\" BorderThickness=\"1\" Canvas.Left=\"{dStart - (dr * 2)}\" Canvas.Top=\"{dCenter - dr}\"/>");
  120. CirBorder.TryAdd(new RectangleF(left, top, width, height), new(width, height, left, top, strock));
  121. break;
  122. }
  123. }
  124. break;
  125. default:
  126. geo = item.Trim();
  127. break;
  128. }
  129. }
  130. using StreamReader sr3 = new(@"E:\Test3.txt");
  131. string s2 = sr3.ReadToEnd();
  132. string[] t3 = s2.Split("\r\n");
  133. foreach (var item in t3)
  134. {
  135. if (item.Contains("<TextBlock") || item.Contains("<Span") || item.Contains("</TextBlock>"))
  136. {
  137. sb.AppendLine(item);
  138. }
  139. if (item.Contains("Stroke"))
  140. {
  141. string temp = item.Substring(item.IndexOf("Stroke=\""));
  142. string[] color = temp.Split("\"");
  143. strock = color[1];
  144. }
  145. switch (item)
  146. {
  147. case string sx when sx.Contains("<Path "):
  148. pathStart1 = item.Trim();
  149. break;
  150. case string sx when sx.Contains("<Path.Data>"):
  151. pathStart2 = item.Trim();
  152. break;
  153. case string sx when sx.Contains("</Path.Data>"):
  154. pathEnd1 = item.Trim();
  155. break;
  156. case string sx when sx.Contains("</Path>"):
  157. if (string.IsNullOrEmpty(geo))
  158. break;
  159. pathEnd2 = item.Trim();
  160. sbRaw.AppendLine(pathStart1);
  161. sbRaw.AppendLine(pathStart2);
  162. sbRaw.AppendLine(geo);
  163. sbRaw.AppendLine(pathEnd1);
  164. sbRaw.AppendLine(pathEnd2);
  165. geo = string.Empty;
  166. break;
  167. default:
  168. break;
  169. }
  170. if (!item.Contains("PathGeometry"))
  171. continue;
  172. string[] test = item.Split('"');
  173. test = test[1].Split(" ");
  174. switch (test.Length)
  175. {
  176. case 6:
  177. string X1 = test[1];
  178. string Y1 = test[2];
  179. string X2 = test[4];
  180. string Y2 = test[5];
  181. Line.Add(new(X1, Y1, X2, Y2, strock));
  182. break;
  183. case 8:
  184. break;
  185. case 11:
  186. break;
  187. case 15:
  188. break;
  189. case 16:
  190. {
  191. float dx1 = Convert.ToSingle(test[4]);
  192. float dy1 = Convert.ToSingle(test[5]);
  193. float dx2 = Convert.ToSingle(test[7]);
  194. float dy2 = Convert.ToSingle(test[8]);
  195. float dx3 = Convert.ToSingle(test[10]);
  196. float dy3 = Convert.ToSingle(test[11]);
  197. float dx4 = Convert.ToSingle(test[13]);
  198. float dy4 = Convert.ToSingle(test[14]);
  199. float[] dx = [dx1, dx2, dx3, dx4];
  200. float[] dy = [dy1, dy2, dy3, dy4];
  201. float width = dx.Max() - dx.Min();
  202. float height = dy.Max() - dy.Min();
  203. float left = dx.Min();
  204. float top = dy.Min();
  205. if (height > 150)
  206. RecBorder.TryAdd(new RectangleF(left, top, width, height), new(width, height, left, top, strock));
  207. else
  208. RecButton.TryAdd(new RectangleF(left, top, width, height), new(width, height, left, top, strock));
  209. }
  210. break;
  211. case 22:
  212. {
  213. float dStart = Convert.ToSingle(test[1]);
  214. float dCenter = Convert.ToSingle(test[2]);
  215. float dr = Convert.ToSingle(test[4]);
  216. float width = dr * 2;
  217. float height = dr * 2;
  218. float left = dStart - (dr * 2);
  219. float top = dCenter - dr;
  220. switch (dr)
  221. {
  222. case > 15:
  223. CirButton.TryAdd(new RectangleF(left, top, width, height), new(width, height, left, top, strock));
  224. break;
  225. default:
  226. CirBorder.TryAdd(new RectangleF(left, top, width, height), new(width, height, left, top, strock));
  227. break;
  228. }
  229. }
  230. break;
  231. default:
  232. geo = item.Trim();
  233. break;
  234. }
  235. }
  236. foreach (var item in CirButton)
  237. {
  238. foreach (var cb in CirButton)
  239. {
  240. if (cb.Key.Contains((float)item.Value.Left, (float)item.Value.Top) && cb.Value != item.Value)
  241. {
  242. CirBorder.Add(cb.Key, cb.Value);
  243. CirBorder.Add(item.Key, item.Value);
  244. }
  245. }
  246. }
  247. foreach (var item in CirBorder.Keys)
  248. {
  249. if (!CirButton.ContainsKey(item))
  250. continue;
  251. CirButton.Remove(item);
  252. }
  253. using StreamReader sr2 = new(@"E:\Test2.txt");
  254. string text = sr2.ReadToEnd();
  255. sb.AppendLine(text);
  256. string[] t2 = sb.ToString().Split("</TextBlock>");
  257. Console.WriteLine("<Canvas x:Name=\"Text\">");
  258. foreach (var item in t2)
  259. {
  260. if (!item.Contains("<TextBlock"))
  261. continue;
  262. string[] te = item.Trim().Replace("\r\n", string.Empty).Split('>');
  263. string[] content1 = te[0].Split("\"");
  264. float strLeft = Convert.ToSingle(content1[7]);
  265. float strTop = Convert.ToSingle(content1[9]) + 20;
  266. float fontsize = Convert.ToSingle(content1[1]);
  267. string foreground = content1[5];
  268. string str = te[2].Trim().Split('<')[0];
  269. if (str.Contains("slm") || str.Contains("0.0"))
  270. continue;
  271. if (fontsize == 0f)
  272. fontsize = 30;
  273. foreach (var recb in RecButton)
  274. {
  275. if (!recb.Key.Contains(strLeft, strTop))
  276. continue;
  277. recb.Value.Fontsize = fontsize;
  278. recb.Value.Content = str;
  279. recb.Value.Fontsize = fontsize;
  280. recb.Value.Foreground = foreground;
  281. goto End;
  282. }
  283. foreach (var recb in CirButton)
  284. {
  285. if (!recb.Key.Contains(strLeft, strTop))
  286. continue;
  287. recb.Value.Fontsize = fontsize;
  288. recb.Value.Content = str;
  289. recb.Value.Fontsize = fontsize;
  290. recb.Value.Foreground = foreground;
  291. goto End;
  292. }
  293. //foreach (var recb in RecBorder)
  294. //{
  295. // if (!recb.Key.Contains(strLeft, strTop))
  296. // continue;
  297. // recb.Value.Fontsize = fontsize;
  298. // recb.Value.Content = str;
  299. // recb.Value.Fontsize = fontsize;
  300. // recb.Value.Foreground = foreground;
  301. // goto End;
  302. //}
  303. foreach (var recb in CirBorder)
  304. {
  305. if (!recb.Key.Contains(strLeft, strTop + 70))
  306. continue;
  307. recb.Value.Fontsize = fontsize;
  308. recb.Value.Content = str;
  309. recb.Value.Fontsize = fontsize;
  310. recb.Value.Foreground = foreground;
  311. break;
  312. }
  313. Console.WriteLine($"<TextBlock Text=\"{str}\" FontSize=\"32\" Foreground=\"{foreground}\" Canvas.Left=\"{strLeft:0}\" Canvas.Top=\"{strTop:0}\" />");
  314. End:
  315. continue;
  316. }
  317. Console.WriteLine("</Canvas>");
  318. Console.WriteLine();
  319. int noName = 0;
  320. Console.WriteLine("<Canvas x:Name=\"RecButton\" Panel.ZIndex=\"1\">");
  321. foreach (var item in RecButton)
  322. {
  323. var value = item.Value;
  324. string name = string.IsNullOrEmpty(value.Content) ? $"NoName_{noName++}" : value.Content;
  325. name = name.Replace(" ", "");
  326. name = name.Replace("-", "");
  327. name = name.Replace(".", "");
  328. int innerLines = Line.Where(t => item.Key.Contains((Convert.ToSingle(t.X1) + Convert.ToSingle(t.X2)) / 2, (Convert.ToSingle(t.Y1) + Convert.ToSingle(t.Y2)) / 2)).Count();
  329. string baseName = innerLines switch
  330. {
  331. >= 2 => "MFC",
  332. _ => "RBU_"
  333. };
  334. if (int.TryParse(name, out _))
  335. {
  336. for (int i = 1; ; i++)
  337. {
  338. if (Names.Add(name))
  339. break;
  340. if (Names.Add($"{name}_{i}"))
  341. {
  342. name = $"{name}_{i}";
  343. break;
  344. }
  345. }
  346. Console.WriteLine($"<Button x:Name=\"{baseName}{name}\" Width=\"{value.Width:0}\" Height=\"{value.Height:0}\" Canvas.Left=\"{value.Left}\" Canvas.Top=\"{value.Top:0}\" BorderBrush=\"{value.Strock}\" Background=\"Transparent\" Panel.ZIndex=\"1\" BorderThickness=\"2\" Click=\"BTN_Click\">");
  347. Console.WriteLine($"<Grid Width=\"{value.Width:0}\">");
  348. Console.WriteLine("<Grid.RowDefinitions>");
  349. Console.WriteLine("<RowDefinition/>");
  350. Console.WriteLine("<RowDefinition Height=\"8\"/>");
  351. Console.WriteLine("<RowDefinition/>");
  352. Console.WriteLine("</Grid.RowDefinitions>");
  353. Console.WriteLine($"<TextBlock Text=\"{value.Content}\" FontSize=\"28\" Foreground=\"{value.Foreground}\" HorizontalAlignment=\"Center\" />");
  354. Console.WriteLine($"<TextBlock Grid.Row=\"2\" x:Name=\"{baseName}{name}_Value\" Text=\"\" FontSize=\"28\" Foreground=\"#FF000000\" HorizontalAlignment=\"Left\" />");
  355. Console.WriteLine("</Grid>");
  356. Console.WriteLine("</Button>");
  357. }
  358. else
  359. {
  360. for (int i = 1; ; i++)
  361. {
  362. if (Names.Add(name))
  363. break;
  364. if (Names.Add($"{name}_{i}"))
  365. {
  366. name = $"{name}_{i}";
  367. break;
  368. }
  369. }
  370. if (Math.Abs(value.Width - value.Height) < 2)
  371. Console.WriteLine($"<Button x:Name=\"Sensor{name}\" Width=\"{value.Width:0}\" Height=\"{value.Height:0}\" Canvas.Left=\"{value.Left}\" Canvas.Top=\"{value.Top:0}\" BorderBrush=\"{value.Strock}\" Background=\"Transparent\" Panel.ZIndex=\"1\" BorderThickness=\"2\" Click=\"BTN_Click\">");
  372. else
  373. Console.WriteLine($"<Button x:Name=\"RBU_{name}\" Width=\"{value.Width:0}\" Height=\"{value.Height:0}\" Canvas.Left=\"{value.Left}\" Canvas.Top=\"{value.Top:0}\" BorderBrush=\"{value.Strock}\" Background=\"Transparent\" Panel.ZIndex=\"1\" BorderThickness=\"2\" Click=\"BTN_Click\">");
  374. Console.WriteLine($"<TextBlock Text=\"{value.Content}\" FontSize=\"28\" Foreground=\"{value.Foreground}\"/>");
  375. Console.WriteLine("</Button>");
  376. }
  377. }
  378. Console.WriteLine("</Canvas>");
  379. Console.WriteLine("<Canvas x:Name=\"RecBorder\" Panel.ZIndex=\"0\">");
  380. foreach (var item in RecBorder.Values)
  381. {
  382. string name = string.IsNullOrEmpty(item.Content) ? $"NoName_{noName++}" : item.Content;
  383. name = name.Replace(" ", "");
  384. name = name.Replace("-", "");
  385. name = name.Replace(".", "");
  386. for (int i = 1; ; i++)
  387. {
  388. if (Names.Add(name))
  389. break;
  390. if (Names.Add($"{name}_{i}"))
  391. {
  392. name = $"{name}_{i}";
  393. break;
  394. }
  395. }
  396. Console.WriteLine($"<Border x:Name=\"RBO_{name}\" Width=\"{item.Width:0}\" Height=\"{item.Height:0}\" Canvas.Left=\"{item.Left:0}\" Canvas.Top=\"{item.Top:0}\" BorderBrush=\"{item.Strock}\" Background=\"Transparent\" Panel.ZIndex=\"0\" BorderThickness=\"2\" >");
  397. Console.WriteLine($"<TextBlock Text=\"{item.Content}\" FontSize=\"28\" Foreground=\"{item.Foreground}\"/>");
  398. Console.WriteLine("</Border>");
  399. }
  400. Console.WriteLine("</Canvas>");
  401. Console.WriteLine();
  402. Console.WriteLine("<Canvas x:Name=\"CirButton\" Panel.ZIndex=\"1\">");
  403. foreach (var item in CirButton.Values)
  404. {
  405. string name = string.IsNullOrEmpty(item.Content) ? $"NoName_{noName++}" : item.Content;
  406. name = name.Replace(" ", "");
  407. name = name.Replace("-", "");
  408. name = name.Replace(".", "");
  409. for (int i = 1; ; i++)
  410. {
  411. if (Names.Add(name))
  412. break;
  413. if (Names.Add($"{name}_{i}"))
  414. {
  415. name = $"{name}_{i}";
  416. break;
  417. }
  418. }
  419. Console.WriteLine($"<Button x:Name=\"ValveAV{name}\" Width=\"{item.Width:0}\" Height=\"{item.Height:0}\" Canvas.Left=\"{item.Left:0}\" Canvas.Top=\"{item.Top:0}\" BorderBrush=\"{item.Strock}\" Background=\"Transparent\" Panel.ZIndex=\"1\" BorderThickness=\"2\" Click=\"BTN_Click\">");
  420. Console.WriteLine("<Button.Resources>");
  421. Console.WriteLine("<Style TargetType=\"{x:Type Border}\">");
  422. Console.WriteLine("<Setter Property=\"CornerRadius\" Value=\"75\"/>");
  423. Console.WriteLine("</Style>");
  424. Console.WriteLine("</Button.Resources>");
  425. Console.WriteLine($"<TextBlock Text=\"{item.Content}\" FontSize=\"32\" Foreground=\"{item.Foreground}\"/>");
  426. Console.WriteLine("</Button>");
  427. }
  428. Console.WriteLine("</Canvas>");
  429. Console.WriteLine();
  430. Console.WriteLine("<Canvas x:Name=\"CirBorder\" Panel.ZIndex=\"0\">");
  431. foreach (var item in CirBorder.Values)
  432. {
  433. string name = string.IsNullOrEmpty(item.Content) ? $"NoName_{noName++}" : item.Content;
  434. name = name.Replace(" ", "");
  435. name = name.Replace("-", "");
  436. name = name.Replace(".", "");
  437. for (int i = 1; ; i++)
  438. {
  439. if (Names.Add(name))
  440. break;
  441. if (Names.Add($"{name}_{i}"))
  442. {
  443. name = $"{name}_{i}";
  444. break;
  445. }
  446. }
  447. if (item.Width > 50)
  448. Console.WriteLine($"<Border x:Name=\"Sensor{name}\" BorderBrush=\"{item.Strock}\" Width=\"{item.Width:0}\" Height=\"{item.Height:0}\" CornerRadius=\"{item.Height:0}\" BorderThickness=\"2\" Canvas.Left=\"{item.Left:0}\" Canvas.Top=\"{item.Top:0}\" />");
  449. else
  450. Console.WriteLine($"<Border x:Name=\"CBO_{name}\" BorderBrush=\"{item.Strock}\" Width=\"{item.Width:0}\" Height=\"{item.Height:0}\" CornerRadius=\"{item.Height:0}\" BorderThickness=\"2\" Canvas.Left=\"{item.Left:0}\" Canvas.Top=\"{item.Top:0}\" />");
  451. }
  452. Console.WriteLine("</Canvas>");
  453. Console.WriteLine();
  454. Console.WriteLine("<Canvas x:Name=\"Line\" Panel.ZIndex=\"0\">");
  455. foreach (var item in Line)
  456. {
  457. Console.WriteLine($"<Line X1=\"{item.X1:0}\" Y1=\"{item.Y1:0}\" X2=\"{item.X2:0}\" Y2=\"{item.Y2:0}\" Stroke=\"{item.Foreground}\" StrokeThickness=\"2\"/>");
  458. }
  459. Console.WriteLine("</Canvas>");
  460. Console.WriteLine();
  461. Console.WriteLine("<Canvas x:Name=\"RawPath\" Panel.ZIndex=\"0\">");
  462. Console.WriteLine(sbRaw);
  463. Console.WriteLine("</Canvas>");
  464. }
  465. }
  466. }
  467. public class DrawData(double width, double height, double left, double top, string strock = "#000000")
  468. {
  469. public double Width { get; } = width;
  470. public double Height { get; } = height;
  471. public double Left { get; } = left;
  472. public double Top { get; } = top;
  473. public string Strock { get; set; } = strock;
  474. public string Content { get; set; } = string.Empty;
  475. public double Fontsize { get; set; }
  476. public string Foreground { get; set; } = "#000000";
  477. }
  478. public class DrawLine(string x1, string y1, string x2, string y2, string strock = "#000000")
  479. {
  480. public string X1 { get; } = x1;
  481. public string Y1 { get; } = y1;
  482. public string X2 { get; } = x2;
  483. public string Y2 { get; } = y2;
  484. public string Foreground { get; set; } = strock;
  485. }