check_grid.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. def check_grid_upright(model_list, grid_data_dict):
  2. grid_data_dict = dict(grid_data_dict)
  3. group = [] # 保存结果
  4. calc_dict = dict() # 结构 dict(dict()) key --> modelId, value( key --> name, value --> location )
  5. for model_id, all_grid in grid_data_dict.items():
  6. name_dict = dict()
  7. for data_row in all_grid:
  8. name_dict[data_row.get('name')] = data_row.get('location')
  9. calc_dict[model_id] = name_dict
  10. for model_id, name_location_dict in calc_dict.items():
  11. # 判断并插入分组
  12. insert_group(group, model_id, calc_dict)
  13. print(group)
  14. index = 0
  15. group_dict = dict()
  16. for model_id_list in group:
  17. index = index + 1
  18. for tmp_model_id in model_id_list:
  19. group_dict[tmp_model_id] = index
  20. for floor in model_list:
  21. if floor.get('fid') in group_dict:
  22. floor['group_id'] = group_dict.get(floor.get('fid'))
  23. else:
  24. floor['group_id'] = None
  25. print(model_list)
  26. return True
  27. def insert_group(group, model_id, calc_dict):
  28. # 如果第一次往group里添加元素, 直接添加
  29. count = 0
  30. for single_group in group:
  31. count += len(single_group)
  32. if count == 0:
  33. group.append([model_id])
  34. return
  35. # 判断跟组内是否有冲突, 如果有则创建新组添加
  36. # 没有冲突则添加到一个组内
  37. conflict = False
  38. for single_group in group:
  39. conflict = False
  40. base_data = calc_dict.get(model_id)
  41. for compare_model_id in single_group:
  42. compare_data = calc_dict.get(compare_model_id)
  43. if has_conflict(base_data, compare_data):
  44. conflict = True
  45. break
  46. if not conflict:
  47. single_group.append(model_id)
  48. if conflict:
  49. group.append([model_id])
  50. # 返回False是没冲突, True是有冲突
  51. def has_conflict(base, compare):
  52. for name, location in base.items():
  53. if name in compare:
  54. type1 = location.get('Type')
  55. type2 = compare.get(name).get('Type')
  56. if type1 in type2:
  57. if 'Line' in type1:
  58. if not is_same_line(location.get('Points'), compare.get(name).get('Points')):
  59. return True
  60. else:
  61. return True
  62. return False
  63. def is_same_line(line1, line2):
  64. line1_point1 = line1[0]
  65. line1_point2 = line1[1]
  66. line2_point1 = line2[0]
  67. line2_point2 = line2[1]
  68. try:
  69. a1 = (line1_point1.get('Y') - line1_point2.get('Y')) / (line1_point1.get('X') - line1_point2.get('X'))
  70. k1 = line1_point1.get('Y') - a1 * line1_point1.get('X')
  71. except ZeroDivisionError as error:
  72. if line2_point1.get('X') == line2_point2.get('X') == line1_point2.get('X'):
  73. return True
  74. else:
  75. return False
  76. try:
  77. a2 = (line2_point1.get('Y') - line2_point2.get('Y')) / (line2_point1.get('X') - line2_point2.get('X'))
  78. k2 = line2_point1.get('Y') - a2 * line2_point1.get('X')
  79. except ZeroDivisionError as error:
  80. return False
  81. return a1 == a2 and k1 == k2