123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- def check_grid_upright(model_list, grid_data_dict):
- grid_data_dict = dict(grid_data_dict)
- group = [] # 保存结果
- calc_dict = dict() # 结构 dict(dict()) key --> modelId, value( key --> name, value --> location )
- for model_id, all_grid in grid_data_dict.items():
- name_dict = dict()
- for data_row in all_grid:
- name_dict[data_row.get('name')] = data_row.get('location')
- calc_dict[model_id] = name_dict
- for model_id, name_location_dict in calc_dict.items():
- # 判断并插入分组
- insert_group(group, model_id, calc_dict)
- print(group)
- index = 0
- group_dict = dict()
- for model_id_list in group:
- index = index + 1
- for tmp_model_id in model_id_list:
- group_dict[tmp_model_id] = index
- for floor in model_list:
- if floor.get('fid') in group_dict:
- floor['group_id'] = group_dict.get(floor.get('fid'))
- else:
- floor['group_id'] = None
- print(model_list)
- return True
- def insert_group(group, model_id, calc_dict):
- # 如果第一次往group里添加元素, 直接添加
- count = 0
- for single_group in group:
- count += len(single_group)
- if count == 0:
- group.append([model_id])
- return
- # 判断跟组内是否有冲突, 如果有则创建新组添加
- # 没有冲突则添加到一个组内
- conflict = False
- for single_group in group:
- conflict = False
- base_data = calc_dict.get(model_id)
- for compare_model_id in single_group:
- compare_data = calc_dict.get(compare_model_id)
- if has_conflict(base_data, compare_data):
- conflict = True
- break
- if not conflict:
- single_group.append(model_id)
- if conflict:
- group.append([model_id])
- # 返回False是没冲突, True是有冲突
- def has_conflict(base, compare):
- for name, location in base.items():
- if name in compare:
- type1 = location.get('Type')
- type2 = compare.get(name).get('Type')
- if type1 in type2:
- if 'Line' in type1:
- if not is_same_line(location.get('Points'), compare.get(name).get('Points')):
- return True
- else:
- return True
- return False
- def is_same_line(line1, line2):
- line1_point1 = line1[0]
- line1_point2 = line1[1]
- line2_point1 = line2[0]
- line2_point2 = line2[1]
- try:
- a1 = (line1_point1.get('Y') - line1_point2.get('Y')) / (line1_point1.get('X') - line1_point2.get('X'))
- k1 = line1_point1.get('Y') - a1 * line1_point1.get('X')
- except ZeroDivisionError as error:
- if line2_point1.get('X') == line2_point2.get('X') == line1_point2.get('X'):
- return True
- else:
- return False
- try:
- a2 = (line2_point1.get('Y') - line2_point2.get('Y')) / (line2_point1.get('X') - line2_point2.get('X'))
- k2 = line2_point1.get('Y') - a2 * line2_point1.get('X')
- except ZeroDivisionError as error:
- return False
- return a1 == a2 and k1 == k2
|