organize_drawing_according_to_details_new.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import re
  2. def get_details(result): #search for all details in drawing and store it in list details, first need to append all text elements of one line and then check if regular expression is found in this text element
  3. reg = r"([A-Z])-\1|([A-Z]\W?[A-Z]?\s?\W\s?\d\d?\s?\s?:\s?\d\d?\s?\W)"
  4. details = []
  5. for element in result:
  6. new = []
  7. if re.search(reg,element):
  8. new.extend(result[element])
  9. new.append(element)
  10. details.append(new)
  11. number = len(details)
  12. return details, number
  13. def get_borders(details, tables):
  14. sections = []
  15. #print(coords)
  16. for first in details:
  17. x_min = -1
  18. y_min = -1
  19. x_max = -1
  20. y_max = -1
  21. firstx_max = first[2]
  22. firstx_min = first[0]
  23. firsty_max = first[3]
  24. firsty_min = first[1]
  25. distance_xmax = 100000000000
  26. distance_xmin = 100000000000
  27. distance_ymin = 100000000000
  28. distance_ymax = 100000000000
  29. for second in details:
  30. secondx_min = second[0]
  31. secondx_max = second[2]
  32. secondy_min = second[1]
  33. secondy_max = second[3]
  34. if secondx_max < firstx_min and abs(firsty_min-secondy_min) < 90 and first != second: ###check for left side, are there any other details at the left side at a certain y-span
  35. if abs(firstx_min - secondx_max)/2 < distance_xmax:
  36. distance_xmax = abs(firstx_min - secondx_max)/2
  37. x_min = secondx_max + distance_xmax
  38. if secondx_min > firstx_max and abs(firsty_min-secondy_min) < 190 and first != second: ####check for right side
  39. if abs(secondx_min - firstx_max)/2 < distance_xmin:
  40. distance_xmin = abs(secondx_min - firstx_max)/2
  41. x_max = firstx_max + distance_xmin
  42. if firsty_min > secondy_max and abs(firstx_min-secondx_min) < 80 and first != second: ####check above
  43. if abs(firsty_min - secondy_max)/2 < distance_ymin:
  44. distance_ymin = abs(firsty_min - secondy_max)/2
  45. y_min = firsty_min
  46. if firsty_max < secondy_min and abs(firstx_min-secondx_min) < 80 and first != second: ####check below
  47. if abs(firsty_max - secondy_min)/2 < distance_ymax:
  48. distance_ymax = abs(firsty_max - secondy_min)/2
  49. y_max = secondy_min
  50. if y_min == -1:
  51. y_min = firsty_min
  52. if x_min == -1:
  53. x_min = 0
  54. if x_max == -1:
  55. x_max = firstx_max + distance_xmax
  56. if y_max == -1:
  57. y_max = 1000000000
  58. ##check if it intersects with tables
  59. for table in tables:
  60. table_xmin = table[0]
  61. if "Start drawing" in table[4]:
  62. table_xmax = 100000000
  63. else:
  64. table_xmax = table[2]
  65. table_ymin = table[1]
  66. if y_max > table_ymin:
  67. if table_xmin < firstx_min < table_xmax:
  68. y_max = table_ymin
  69. elif table_xmin < x_max < table_xmax:
  70. y_max = table_ymin
  71. sections.append((first,x_min, y_min,x_max,y_max))
  72. """for section in sections:
  73. print(section)"""
  74. return sections
  75. def intersects(detail, rectangle): #using the separating axis theorem
  76. rect1_bottom_left_x = float(detail[1][0])
  77. rect1_top_right_x = float(detail[1][2])
  78. rect1_bottom_left_y = float(detail[1][3])
  79. rect1_top_right_y = float(detail[1][1])
  80. rect2_bottom_left_x = float(rectangle[0])
  81. rect2_top_right_x = float(rectangle[2])
  82. rect2_bottom_left_y = float(rectangle[3])
  83. rect2_top_right_y = float(rectangle[1])
  84. return not (rect1_top_right_x < rect2_bottom_left_x or rect1_bottom_left_x > rect2_top_right_x or rect1_top_right_y > rect2_bottom_left_y or rect1_bottom_left_y < rect2_top_right_y)
  85. def main_function(result, tables):
  86. reg = r"([A-Z])-\1|([A-Z]\W?[A-Z]?\s?\W\s?\d\d?\s?\s?:\s?\d\d?\s?\W)"
  87. details, number = get_details(result)
  88. details = sorted(details, key=lambda x: x[0]) #sort by distance from 0,0
  89. sections = get_borders(details, tables)
  90. section = []
  91. details_dict = {}
  92. for sect in sections:
  93. coord = []
  94. coord_name = sect[0][4]
  95. for sec in sect[1:]:
  96. coord.append(sec)
  97. details_dict[coord_name] = coord
  98. section.append(list((coord_name,coord)))
  99. for table in tables:
  100. table[3] = 10000000
  101. coord = []
  102. name = "ZZZZZTable"
  103. for tab in table[:4]:
  104. coord.append(tab)
  105. details_dict[name] = coord
  106. section.append(list((name,coord)))
  107. if number == 0 | len(section) == 0:
  108. section.append(list(("No details",list((000.000,000.000,100000000.000,10000000.000)))))
  109. dict_help = {}
  110. for res in result:
  111. for det in section:
  112. help_dict = {}
  113. if re.match(reg, res): ###damit nicht details zu details zugeordnet werden!!!
  114. break
  115. if intersects(det,result[res]):
  116. name = det[0]
  117. help_dict[res] = result[res]
  118. if name in dict_help:
  119. dict_help[name].update(help_dict)
  120. else:
  121. dict_help[name] = help_dict
  122. break
  123. return dict_help, details_dict