read_from_clustered_merged.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # coding=utf8
  2. import re
  3. import csv
  4. def read(file):
  5. with open(file, "r") as f:
  6. reader = csv.reader(f, delimiter=";")
  7. result = []
  8. dict = {}
  9. for row in reader:
  10. ausrichtung = row[1]
  11. row3 = row[2]
  12. row3 = eval(row3)
  13. element = ""
  14. ymin = 100000000.0
  15. ymax = 0.0
  16. xmin = 100000000.0
  17. xmax = 0.0
  18. coords = []
  19. merged_elements = []
  20. length = 0
  21. for e in row3:
  22. length += len(e)
  23. for elem in row3:
  24. if len(row3) == 1:
  25. element = elem[4]
  26. xmin = float(elem[0])
  27. ymin = float(elem[1])
  28. xmax = float(elem[2])
  29. ymax = float(elem[3])
  30. else:
  31. if isinstance(elem[0],list):
  32. merged_elements += elem
  33. if len(merged_elements) < length:
  34. continue
  35. if int(ausrichtung) == 1:
  36. merged_elements = sorted(merged_elements, key=lambda k: [float(k[3])], reverse=True)
  37. for elemt in merged_elements:
  38. element += elemt[4] + " "
  39. if float(elemt[0]) < xmin:
  40. xmin = float(elemt[0])
  41. if float(elemt[1]) < ymin:
  42. ymin = float(elemt[1])
  43. if float(elemt[2]) > xmax:
  44. xmax = float(elemt[2])
  45. if float(elemt[3]) > ymax:
  46. ymax = float(elemt[3])
  47. else:
  48. element += elem[4] + " "
  49. if float(elem[0]) < xmin:
  50. xmin = float(elem[0])
  51. if float(elem[1]) < ymin:
  52. ymin = float(elem[1])
  53. if float(elem[2]) > xmax:
  54. xmax = float(elem[2])
  55. if float(elem[3]) > ymax:
  56. ymax = float(elem[3])
  57. result.append(element)
  58. coords.append(xmin)
  59. coords.append(ymin)
  60. coords.append(xmax)
  61. coords.append(ymax)
  62. if element in dict.keys():
  63. element = element + " "
  64. dict[element] = coords
  65. else:
  66. dict[element] = coords
  67. return dict
  68. def print_clean(dims): ##alles raus was nicht relevant ist! und zeichen ersetzen!
  69. dims_new = {}
  70. reg_clean = r"ISO|[a-zA-Z]{4,}|^\d\s\d$|^[a-zA-Z]{2,}\d.*$|^[A-Z]{1}$|^mm$|^\d{2}\.\d{2}\.\d{4}|^-$|A\d|^\d{1}$|^[A-Za-z]{3,}\.?$|^\d{5}|^\d{1}\s\W\s\d"
  71. reg_one_character_only = "^\s*\S{1}\s*x?\s*$" #get rid of singular characters or numbers, or times eg 4x
  72. for dim in dims:
  73. #dim = dim.strip()
  74. if re.search(reg_clean, dim):
  75. continue
  76. elif re.search(reg_one_character_only,dim):
  77. continue
  78. else:
  79. coords = dims[dim]
  80. if re.search(r"b\s\d*\W?\d*\s.",dim):
  81. dim = dim.replace('b', u"\u27C2")
  82. if re.search(r"g\s\d*\W?\d*", dim):
  83. dim = dim.replace('g', u"\u232D")
  84. if re.search(r"f\s\d*\W?\d*", dim):
  85. dim = dim.replace('f', u"\u2225")
  86. if re.search(r"r\s\d*\W?\d*", dim):
  87. dim = dim.replace('r', u"\u25CE")
  88. if re.search(r"i\s\d*\W?\d*", dim):
  89. dim = dim.replace('i', u"\u232F")
  90. if re.search(r"j\s\d*\W?\d*", dim):
  91. dim = dim.replace('j', u"\u2316")
  92. if re.search(r"d\s\d*\W?\d*", dim):
  93. dim = dim.replace('d', u"\u2313")
  94. if re.search(r"c\s+\d*", dim):
  95. dim = dim.replace('c', u"\u23E5")
  96. if re.search(r"n\s+\d*", dim):
  97. dim = dim.replace('n', u"\u2300")
  98. if "È" in dim:
  99. dim = dim.replace('È', 'GG')
  100. if "`" in dim:
  101. dim = dim.replace('`', u"\u00B1")
  102. if "#" in dim:
  103. dim = dim.replace('#', "↔")
  104. if "⌀" in dim:
  105. dim = dim.replace('⌀', "Ø")
  106. reg12 = re.compile(r"(.*\d{1,4}\W?\d{0,4})\s?\+\s-\s?(\d{1,4}\W?\d{0,4})\s?(\d{1,4}\W?\d{0,3})") ##???? was machst du?? nach toleranzen suchen, mit +/- blabla
  107. reg13 = re.compile(r"(.*)\+\s\+\s(\d*\W\d*)\s(\d*\W\d*)(.*)")
  108. reg14 = re.compile(r"(\+\s?\d*,?.?\d*)\s*(\d*,?.?\d*)\s*(\+?\s?\-?\s?\d*,?.?\d*)")
  109. reg15 = re.compile(r"\d\s\d\.|\.\d\s\d")
  110. g = re.search(reg12, dim)
  111. f = re.search(reg13, dim)
  112. e = re.search(reg14, dim)
  113. if g:
  114. dim = re.sub(reg12, g.group(1) + " +" + g.group(2) + " -" + g.group(3), dim) # +/- toleranzen schön darstellen
  115. elif f:
  116. dim = f.group(1) + "+" + f.group(2) + " +" + f.group(3) + f.group(4)
  117. elif e:
  118. dim = e.group(2) + " " + e.group(1) + " " + e.group(3)
  119. dim = dim.replace(" ,",".").replace(", ",".").replace(",",".")
  120. dims_new[dim] = coords
  121. return dims_new