622 lines
32 KiB
Python
622 lines
32 KiB
Python
|
||
import json
|
||
import math
|
||
|
||
def modify_json_values(json_file):
|
||
# 常量定义
|
||
BogenProfileWidth = 42.080
|
||
WeichenkProfileWidth = 42.000
|
||
WeichenGerade = 360.000
|
||
|
||
# 读取JSON文件
|
||
with open(json_file, 'r', encoding='utf-8') as f:
|
||
data = json.load(f)
|
||
process_einzelweiche_items(data, WeichenkProfileWidth,BogenProfileWidth, WeichenGerade)
|
||
|
||
process_doppelweiche_items(data, BogenProfileWidth, WeichenGerade)
|
||
|
||
process_dreifachweiche_items(data, WeichenkProfileWidth)
|
||
# 询问是否保存
|
||
save_choice = input("\n是否保存所有修改? (直接回车保存,输入n取消): ").lower()
|
||
if save_choice == 'n':
|
||
print("所有修改未保存")
|
||
else:
|
||
with open(json_file, 'w', encoding='utf-8') as f:
|
||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||
print("所有修改已保存!")
|
||
|
||
def process_einzelweiche_items(data, WeichenkProfileWidth,BogenProfileWidth, WeichenGerade): # 筛选符合条件的项
|
||
filtered_items = [
|
||
item for item in data
|
||
if (item.get("SivasnrTEF") is None and
|
||
item.get("KurvenRichtung") == 1 and
|
||
item.get("Schaltungstyp") == "M")
|
||
]
|
||
|
||
print(f"共找到 {len(filtered_items)} 项符合条件的记录")
|
||
print("="*50)
|
||
|
||
# 遍历每一项
|
||
for idx, item in enumerate(filtered_items, 1):
|
||
print(f"\n第 {idx} 项:")
|
||
print(f"Sivasnr: {item['Sivasnr']}")
|
||
print(f"ProfilTyp: {item['ProfilTyp']}")
|
||
print(f"Schaltungstyp: {item['Schaltungstyp']}")
|
||
|
||
# 处理 OFWeiche_center_line_width_mm
|
||
current_value = item.get("OFWeiche_center_line_width_mm")
|
||
print(f"\n当前 OFWeiche_center_line_width_mm: {current_value}")
|
||
choice = input("是否修改? (y修改,直接回车跳过): ").lower()
|
||
if choice == 'y':
|
||
new_value = input(f"请输入新值 (当前: {current_value}): ")
|
||
try:
|
||
old_value = item["OFWeiche_center_line_width_mm"]
|
||
item["OFWeiche_center_line_width_mm"] = round(float(new_value), 3) if new_value.lower() != 'null' else None
|
||
print(f"值已更新: {old_value} → {item['OFWeiche_center_line_width_mm']}")
|
||
except ValueError:
|
||
print("输入无效,保持原值")
|
||
else:
|
||
print("跳过修改")
|
||
|
||
# 处理 OFWeiche_center_line_height_mm
|
||
current_value = item.get("OFWeiche_center_line_height_mm")
|
||
print(f"\n当前 OFWeiche_center_line_height_mm: {current_value}")
|
||
choice = input("是否修改? (y修改,直接回车跳过): ").lower()
|
||
if choice == 'y':
|
||
new_value = input(f"请输入新值 (当前: {current_value}): ")
|
||
try:
|
||
old_value = item["OFWeiche_center_line_height_mm"]
|
||
item["OFWeiche_center_line_height_mm"] = round(float(new_value), 3) if new_value.lower() != 'null' else None
|
||
print(f"值已更新: {old_value} → {item['OFWeiche_center_line_height_mm']}")
|
||
except ValueError:
|
||
print("输入无效,保持原值")
|
||
else:
|
||
print("跳过修改")
|
||
|
||
# 计算相关值
|
||
if (item["OFWeiche_center_line_width_mm"] is not None and
|
||
item["OFWeiche_center_line_height_mm"] is not None):
|
||
|
||
angle_rad = math.radians(item["KurvenWinkel"])
|
||
|
||
# 计算并打印基本值
|
||
old_width = item.get("Objekte_width_mm")
|
||
item["Objekte_width_mm"] = round(
|
||
(BogenProfileWidth/2 * math.cos(angle_rad)) +
|
||
item["OFWeiche_center_line_width_mm"] +
|
||
WeichenkProfileWidth/2, 3)
|
||
print(f"\nObjekte_width_mm 计算更新: {old_width} → {item['Objekte_width_mm']}")
|
||
|
||
old_height = item.get("Objekte_height_mm")
|
||
if item["KurvenWinkel"] == 22.5:
|
||
item["Objekte_height_mm"] = round(
|
||
item["OFWeiche_center_line_height_mm"], 3)
|
||
else:
|
||
item["Objekte_height_mm"] = round(
|
||
(BogenProfileWidth/2 * math.sin(angle_rad)) +
|
||
item["OFWeiche_center_line_height_mm"], 3)
|
||
print(f"Objekte_height_mm 计算更新: {old_height} → {item['Objekte_height_mm']}")
|
||
|
||
# 计算并打印CP点坐标
|
||
old_cp1x = item.get("OFWeiche_CP1_x_mm")
|
||
item["OFWeiche_CP1_x_mm"] = round(
|
||
(BogenProfileWidth/2 * math.sin(angle_rad)) +
|
||
item["OFWeiche_center_line_width_mm"], 3)
|
||
print(f"OFWeiche_CP1_x_mm 计算更新: {old_cp1x} → {item['OFWeiche_CP1_x_mm']}")
|
||
|
||
old_cp1y = item.get("OFWeiche_CP1_y_mm")
|
||
item["OFWeiche_CP1_y_mm"] = round(
|
||
item["Objekte_height_mm"] - WeichenGerade, 3)
|
||
print(f"OFWeiche_CP1_y_mm 计算更新: {old_cp1y} → {item['OFWeiche_CP1_y_mm']}")
|
||
|
||
item["OFWeiche_CP2_x_mm"] = round(item["OFWeiche_CP1_x_mm"], 3)
|
||
print(f"OFWeiche_CP2_x_mm 设置: {item['OFWeiche_CP2_x_mm']}")
|
||
|
||
item["OFWeiche_CP2_y_mm"] = round(item["Objekte_height_mm"], 3)
|
||
print(f"OFWeiche_CP2_y_mm 设置: {item['OFWeiche_CP2_y_mm']}")
|
||
|
||
item["OFWeiche_CP3_x_mm"] = round(
|
||
BogenProfileWidth/2 * math.cos(angle_rad), 3)
|
||
print(f"OFWeiche_CP3_x_mm 计算更新: {item['OFWeiche_CP3_x_mm']}")
|
||
|
||
item["OFWeiche_CP3_y_mm"] = round(
|
||
BogenProfileWidth/2 * math.sin(angle_rad), 3)
|
||
print(f"OFWeiche_CP3_y_mm 计算更新: {item['OFWeiche_CP3_y_mm']}")
|
||
|
||
# 1. 查找Schaltungstyp为P的相似项(更新所有CP点)
|
||
current_profil = item["ProfilTyp"]
|
||
if "S" in current_profil:
|
||
prefix = current_profil.rsplit(" ", 1)[0]
|
||
|
||
similar_items_p = [x for x in data
|
||
if x["ProfilTyp"].startswith(prefix) and
|
||
x["ProfilTyp"] != current_profil and
|
||
"WEICHE" in x["ProfilTyp"] and
|
||
x.get("SivasnrTEF") is None and
|
||
x.get("Schaltungstyp") == "P"]
|
||
|
||
if similar_items_p:
|
||
print(f"\n找到 {len(similar_items_p)} 个Schaltungstyp=P的相似项")
|
||
for similar in similar_items_p:
|
||
print(f"正在更新相似项: {similar['ProfilTyp']} (Sivasnr: {similar['Sivasnr']})")
|
||
|
||
|
||
# 更新所有字段包括CP点
|
||
fields_to_copy = [
|
||
"OFWeiche_center_line_width_mm",
|
||
"OFWeiche_center_line_height_mm",
|
||
"Objekte_width_mm",
|
||
"Objekte_height_mm",
|
||
"OFWeiche_CP1_x_mm",
|
||
"OFWeiche_CP1_y_mm",
|
||
"OFWeiche_CP2_x_mm",
|
||
"OFWeiche_CP2_y_mm",
|
||
"OFWeiche_CP3_x_mm",
|
||
"OFWeiche_CP3_y_mm"
|
||
]
|
||
|
||
for field in fields_to_copy:
|
||
old_val = similar.get(field)
|
||
similar[field] = item[field]
|
||
print(f" {field}: {old_val} → {similar[field]}")
|
||
|
||
else:
|
||
print("没有找到Schaltungstyp=P的相似项")
|
||
|
||
# 2. 查找L→R且KurvenRichtung=2的相似项
|
||
if "S" in current_profil and "-L-" in current_profil:
|
||
r_profil = current_profil.replace("-L-", "-R-")
|
||
|
||
similar_items_r = [x for x in data
|
||
if x["ProfilTyp"] == r_profil and
|
||
x.get("SivasnrTEF") is None and
|
||
x.get("KurvenRichtung") == 2 and
|
||
x.get("Schaltungstyp") == "M"]
|
||
|
||
if similar_items_r:
|
||
print(f"\n找到 {len(similar_items_r)} 个L→R的相似项(KurvenRichtung=2)")
|
||
for similar in similar_items_r:
|
||
print(f"正在更新R型相似项: {similar['ProfilTyp']} (Sivasnr: {similar['Sivasnr']})")
|
||
|
||
# 复制基础值
|
||
base_fields = [
|
||
"OFWeiche_center_line_width_mm",
|
||
"OFWeiche_center_line_height_mm",
|
||
"Objekte_width_mm",
|
||
"Objekte_height_mm"
|
||
]
|
||
|
||
for field in base_fields:
|
||
old_val = similar.get(field)
|
||
similar[field] = item[field]
|
||
print(f" {field}: {old_val} → {similar[field]}")
|
||
|
||
# 计算并打印R型特有的CP点
|
||
old_cp1x = similar.get("OFWeiche_CP1_x_mm")
|
||
similar["OFWeiche_CP1_x_mm"] = round(WeichenkProfileWidth/2, 3)
|
||
print(f" OFWeiche_CP1_x_mm (R型): {old_cp1x} → {similar['OFWeiche_CP1_x_mm']}")
|
||
|
||
old_cp1y = similar.get("OFWeiche_CP1_y_mm")
|
||
similar["OFWeiche_CP1_y_mm"] = round(item["Objekte_height_mm"] - WeichenGerade, 3)
|
||
print(f" OFWeiche_CP1_y_mm (R型): {old_cp1y} → {similar['OFWeiche_CP1_y_mm']}")
|
||
|
||
similar["OFWeiche_CP2_x_mm"] = similar["OFWeiche_CP1_x_mm"]
|
||
print(f" OFWeiche_CP2_x_mm (R型): 设置为 {similar['OFWeiche_CP2_x_mm']}")
|
||
|
||
old_cp2y = similar.get("OFWeiche_CP2_y_mm")
|
||
similar["OFWeiche_CP2_y_mm"] = round(item["Objekte_height_mm"], 3)
|
||
print(f" OFWeiche_CP2_y_mm (R型): {old_cp2y} → {similar['OFWeiche_CP2_y_mm']}")
|
||
|
||
old_cp3x = similar.get("OFWeiche_CP3_x_mm")
|
||
similar["OFWeiche_CP3_x_mm"] = round(
|
||
similar["OFWeiche_CP1_x_mm"] + item["OFWeiche_center_line_width_mm"], 3)
|
||
print(f" OFWeiche_CP3_x_mm (R型): {old_cp3x} → {similar['OFWeiche_CP3_x_mm']}")
|
||
|
||
old_cp3y = similar.get("OFWeiche_CP3_y_mm")
|
||
similar["OFWeiche_CP3_y_mm"] = round(
|
||
BogenProfileWidth/2 * math.sin(angle_rad), 3)
|
||
print(f" OFWeiche_CP3_y_mm (R型): {old_cp3y} → {similar['OFWeiche_CP3_y_mm']}")
|
||
|
||
# 3. 查找该R型项的P型对应项
|
||
r_p_profil = r_profil.replace("MIT M", "MIT P")
|
||
|
||
similar_items_r_p = [x for x in data
|
||
if x["ProfilTyp"] == r_p_profil and
|
||
x.get("SivasnrTEF") is None and
|
||
x.get("Schaltungstyp") == "P"]
|
||
|
||
if similar_items_r_p:
|
||
print(f"\n找到 {len(similar_items_r_p)} 个R型P型对应项")
|
||
for similar_r_p in similar_items_r_p:
|
||
print(f"正在更新R型P型对应项: {similar_r_p['ProfilTyp']} (Sivasnr: {similar_r_p['Sivasnr']})")
|
||
|
||
fields_to_copy_r_p = [
|
||
"OFWeiche_center_line_width_mm",
|
||
"OFWeiche_center_line_height_mm",
|
||
"Objekte_width_mm",
|
||
"Objekte_height_mm",
|
||
"OFWeiche_CP1_x_mm",
|
||
"OFWeiche_CP1_y_mm",
|
||
"OFWeiche_CP2_x_mm",
|
||
"OFWeiche_CP2_y_mm",
|
||
"OFWeiche_CP3_x_mm",
|
||
"OFWeiche_CP3_y_mm"
|
||
]
|
||
|
||
for field in fields_to_copy_r_p:
|
||
old_val = similar_r_p.get(field)
|
||
similar_r_p[field] = similar[field]
|
||
print(f" {field}: {old_val} → {similar_r_p[field]}")
|
||
|
||
save_choice = input(f"是否确认更新此项? (直接回车确认,输入n取消): ").lower()
|
||
if save_choice == 'n':
|
||
print("此项更新已取消")
|
||
# 恢复原值
|
||
for field in fields_to_copy_r_p:
|
||
if field in similar_r_p:
|
||
similar_r_p[field] = old_val
|
||
else:
|
||
print("没有找到R型P型对应项")
|
||
else:
|
||
|
||
print("没有找到L→R的相似项")
|
||
|
||
def process_doppelweiche_items(data,BogenProfileWidth, WeichenGerade):
|
||
# 筛选Doppelweiche类型的项
|
||
filtered_items = [
|
||
|
||
item for item in data
|
||
if (item.get("WeichenTyp") == "Doppelweiche" and
|
||
item.get("Schaltungstyp") == "M" and
|
||
item.get("SivasnrTEF") is None)
|
||
]
|
||
|
||
print(f"\n\n共找到 {len(filtered_items)} 项Doppelweiche类型记录")
|
||
print("="*50)
|
||
|
||
# 遍历每一项Doppelweiche
|
||
for idx, item in enumerate(filtered_items, 1):
|
||
print(f"\n第 {idx} 项Doppelweiche:")
|
||
print(f"Sivasnr: {item['Sivasnr']}")
|
||
print(f"ProfilTyp: {item['ProfilTyp']}")
|
||
print(f"Schaltungstyp: {item['Schaltungstyp']}")
|
||
print(f"KurvenWinkel: {item['KurvenWinkel']}")
|
||
|
||
# 处理 OFWeiche_center_line_width_mm
|
||
current_value = item.get("OFWeiche_center_line_width_mm")
|
||
print(f"\n当前 OFWeiche_center_line_width_mm: {current_value}")
|
||
choice = input("是否修改? (y修改,直接回车跳过): ").lower()
|
||
if choice == 'y':
|
||
new_value = input(f"请输入新值 (当前: {current_value}): ")
|
||
try:
|
||
old_value = item["OFWeiche_center_line_width_mm"]
|
||
item["OFWeiche_center_line_width_mm"] = round(float(new_value), 3) if new_value.lower() != 'null' else None
|
||
print(f"值已更新: {old_value} → {item['OFWeiche_center_line_width_mm']}")
|
||
except ValueError:
|
||
print("输入无效,保持原值")
|
||
else:
|
||
print("跳过修改")
|
||
|
||
# 处理 OFWeiche_center_line_height_mm
|
||
current_value = item.get("OFWeiche_center_line_height_mm")
|
||
print(f"\n当前 OFWeiche_center_line_height_mm: {current_value}")
|
||
choice = input("是否修改? (y修改,直接回车跳过): ").lower()
|
||
if choice == 'y':
|
||
new_value = input(f"请输入新值 (当前: {current_value}): ")
|
||
try:
|
||
old_value = item["OFWeiche_center_line_height_mm"]
|
||
item["OFWeiche_center_line_height_mm"] = round(float(new_value), 3) if new_value.lower() != 'null' else None
|
||
print(f"值已更新: {old_value} → {item['OFWeiche_center_line_height_mm']}")
|
||
except ValueError:
|
||
print("输入无效,保持原值")
|
||
else:
|
||
print("跳过修改")
|
||
|
||
# 计算相关值
|
||
if (item["OFWeiche_center_line_width_mm"] is not None and
|
||
item["OFWeiche_center_line_height_mm"] is not None):
|
||
|
||
angle_rad = math.radians(item["KurvenWinkel"])
|
||
|
||
# 计算并打印基本值(Doppelweiche特有公式)
|
||
old_width = item.get("Objekte_width_mm")
|
||
item["Objekte_width_mm"] = round(
|
||
(BogenProfileWidth/2 * math.cos(angle_rad)) +
|
||
item["OFWeiche_center_line_width_mm"] +
|
||
BogenProfileWidth/2 * math.cos(angle_rad), 3)
|
||
print(f"\nObjekte_width_mm 计算更新: {old_width} → {item['Objekte_width_mm']}")
|
||
|
||
old_height = item.get("Objekte_height_mm")
|
||
item["Objekte_height_mm"] = round(
|
||
(BogenProfileWidth/2 * math.sin(angle_rad)) +
|
||
item["OFWeiche_center_line_height_mm"], 3)
|
||
print(f"Objekte_height_mm 计算更新: {old_height} → {item['Objekte_height_mm']}")
|
||
|
||
# 计算并打印CP点坐标(Doppelweiche特有公式)
|
||
item["OFWeiche_CP1_x_mm"] = round(item["Objekte_width_mm"]/2, 3)
|
||
print(f"OFWeiche_CP1_x_mm 计算更新: {item['OFWeiche_CP1_x_mm']}")
|
||
|
||
item["OFWeiche_CP1_y_mm"] = round(item["Objekte_height_mm"], 3)
|
||
print(f"OFWeiche_CP1_y_mm 计算更新: {item['OFWeiche_CP1_y_mm']}")
|
||
|
||
item["OFWeiche_CP2_x_mm"] = round(BogenProfileWidth/2 * math.cos(angle_rad), 3)
|
||
print(f"OFWeiche_CP2_x_mm 计算更新: {item['OFWeiche_CP2_x_mm']}")
|
||
|
||
item["OFWeiche_CP2_y_mm"] = round(BogenProfileWidth/2 * math.sin(angle_rad), 3)
|
||
print(f"OFWeiche_CP2_y_mm 计算更新: {item['OFWeiche_CP2_y_mm']}")
|
||
|
||
item["OFWeiche_CP3_x_mm"] = round(
|
||
BogenProfileWidth/2 * math.cos(angle_rad) +
|
||
item["OFWeiche_center_line_width_mm"], 3)
|
||
print(f"OFWeiche_CP3_x_mm 计算更新: {item['OFWeiche_CP3_x_mm']}")
|
||
|
||
item["OFWeiche_CP3_y_mm"] = item["OFWeiche_CP2_y_mm"]
|
||
print(f"OFWeiche_CP3_y_mm 设置: {item['OFWeiche_CP3_y_mm']}")
|
||
|
||
# 1. 查找类似项1(D型P型)
|
||
current_profil = item["ProfilTyp"]
|
||
if "S D" in current_profil:
|
||
d_p_profil = current_profil.replace("MIT M", "MIT P")
|
||
|
||
similar_items_d_p = [x for x in data
|
||
if x["ProfilTyp"] == d_p_profil and
|
||
x.get("SivasnrTEF") is None]
|
||
|
||
if similar_items_d_p:
|
||
print(f"\n找到 {len(similar_items_d_p)} 个D型P型相似项")
|
||
for similar in similar_items_d_p:
|
||
print(f"正在更新D型P型相似项: {similar['ProfilTyp']} (Sivasnr: {similar['Sivasnr']})")
|
||
|
||
# 更新所有字段
|
||
fields_to_copy = [
|
||
"OFWeiche_center_line_width_mm",
|
||
"OFWeiche_center_line_height_mm",
|
||
"Objekte_width_mm",
|
||
"Objekte_height_mm",
|
||
"OFWeiche_CP1_x_mm",
|
||
"OFWeiche_CP1_y_mm",
|
||
"OFWeiche_CP2_x_mm",
|
||
"OFWeiche_CP2_y_mm",
|
||
"OFWeiche_CP3_x_mm",
|
||
"OFWeiche_CP3_y_mm"
|
||
]
|
||
|
||
for field in fields_to_copy:
|
||
old_val = similar.get(field)
|
||
similar[field] = item[field]
|
||
print(f" {field}: {old_val} → {similar[field]}")
|
||
else:
|
||
print("没有找到D型P型相似项")
|
||
|
||
# 2. 查找类似项2(T型M型)
|
||
if "S D" in current_profil:
|
||
t_m_profil = current_profil.replace("S D", "S T")
|
||
|
||
similar_items_t_m = [x for x in data
|
||
if x["ProfilTyp"] == t_m_profil and
|
||
x.get("SivasnrTEF") is None and
|
||
x.get("Schaltungstyp") == "M"]
|
||
|
||
if similar_items_t_m:
|
||
print(f"\n找到 {len(similar_items_t_m)} 个T型M型相似项")
|
||
for similar in similar_items_t_m:
|
||
print(f"正在更新T型M型相似项: {similar['ProfilTyp']} (Sivasnr: {similar['Sivasnr']})")
|
||
if similar["KurvenWinkel"] == 22.5:
|
||
print("检测到KurvenWinkel=22.5,采用特殊处理方式")
|
||
|
||
# 特殊处理逻辑
|
||
similar["OFWeiche_center_line_width_mm"] = item["OFWeiche_center_line_width_mm"]
|
||
print(f" OFWeiche_center_line_width_mm: → {similar['OFWeiche_center_line_width_mm']}")
|
||
|
||
similar["OFWeiche_center_line_height_mm"] = WeichenGerade
|
||
print(f" OFWeiche_center_line_height_mm: → {WeichenGerade}")
|
||
|
||
similar["Objekte_width_mm"] = item["Objekte_width_mm"]
|
||
print(f" Objekte_width_mm: → {similar['Objekte_width_mm']}")
|
||
|
||
similar["Objekte_height_mm"] = WeichenGerade
|
||
print(f" Objekte_height_mm: → {WeichenGerade}")
|
||
|
||
similar["OFWeiche_CP1_x_mm"] = item["OFWeiche_CP1_x_mm"]
|
||
print(f" OFWeiche_CP1_x_mm: → {similar['OFWeiche_CP1_x_mm']}")
|
||
|
||
similar["OFWeiche_CP1_y_mm"] = similar["Objekte_height_mm"]
|
||
print(f" OFWeiche_CP1_y_mm: → {similar['OFWeiche_CP1_y_mm']}")
|
||
|
||
similar["OFWeiche_CP2_x_mm"] = item["OFWeiche_CP2_x_mm"]
|
||
print(f" OFWeiche_CP2_x_mm: → {similar['OFWeiche_CP2_x_mm']}")
|
||
|
||
similar["OFWeiche_CP2_y_mm"] = 20
|
||
print(f" OFWeiche_CP2_y_mm: → 20")
|
||
|
||
similar["OFWeiche_CP3_x_mm"] = item["OFWeiche_CP3_x_mm"]
|
||
print(f" OFWeiche_CP3_x_mm: → {similar['OFWeiche_CP3_x_mm']}")
|
||
|
||
similar["OFWeiche_CP3_y_mm"] = 20
|
||
print(f" OFWeiche_CP3_y_mm: → 20")
|
||
|
||
else:
|
||
# 更新基本字段
|
||
base_fields = [
|
||
"OFWeiche_center_line_width_mm",
|
||
"OFWeiche_center_line_height_mm",
|
||
"Objekte_width_mm",
|
||
"Objekte_height_mm",
|
||
"OFWeiche_CP1_x_mm",
|
||
"OFWeiche_CP1_y_mm",
|
||
"OFWeiche_CP2_x_mm",
|
||
"OFWeiche_CP2_y_mm",
|
||
"OFWeiche_CP3_x_mm",
|
||
"OFWeiche_CP3_y_mm"
|
||
]
|
||
|
||
for field in base_fields:
|
||
old_val = similar.get(field)
|
||
similar[field] = item[field]
|
||
print(f" {field}: {old_val} → {similar[field]}")
|
||
|
||
# 添加CP4点(T型特有)
|
||
similar["OFWeiche_CP4_x_mm"] = round(item["Objekte_width_mm"]/2, 3)
|
||
print(f" OFWeiche_CP4_x_mm 添加: {similar['OFWeiche_CP4_x_mm']}")
|
||
|
||
similar["OFWeiche_CP4_y_mm"] = round(item["Objekte_height_mm"] - WeichenGerade, 3)
|
||
print(f" OFWeiche_CP4_y_mm 添加: {similar['OFWeiche_CP4_y_mm']}")
|
||
|
||
# 3. 查找T型P型对应项
|
||
t_p_profil = t_m_profil.replace("MIT M", "MIT P")
|
||
|
||
similar_items_t_p = [x for x in data
|
||
if x["ProfilTyp"] == t_p_profil and
|
||
x.get("SivasnrTEF") is None and
|
||
x.get("Schaltungstyp") == "P"]
|
||
|
||
if similar_items_t_p:
|
||
print(f"\n找到 {len(similar_items_t_p)} 个T型P型对应项")
|
||
for similar_t_p in similar_items_t_p:
|
||
print(f"正在更新T型P型对应项: {similar_t_p['ProfilTyp']} (Sivasnr: {similar_t_p['Sivasnr']})")
|
||
|
||
# 更新所有字段包括CP4点
|
||
fields_to_copy_t_p = [
|
||
"OFWeiche_center_line_width_mm",
|
||
"OFWeiche_center_line_height_mm",
|
||
"Objekte_width_mm",
|
||
"Objekte_height_mm",
|
||
"OFWeiche_CP1_x_mm",
|
||
"OFWeiche_CP1_y_mm",
|
||
"OFWeiche_CP2_x_mm",
|
||
"OFWeiche_CP2_y_mm",
|
||
"OFWeiche_CP3_x_mm",
|
||
"OFWeiche_CP3_y_mm",
|
||
"OFWeiche_CP4_x_mm",
|
||
"OFWeiche_CP4_y_mm"
|
||
]
|
||
|
||
for field in fields_to_copy_t_p:
|
||
old_val = similar_t_p.get(field)
|
||
similar_t_p[field] = similar[field]
|
||
print(f" {field}: {old_val} → {similar_t_p[field]}")
|
||
|
||
# 添加保存确认提示
|
||
save_choice = input(f"是否确认更新此项? (直接回车确认,输入n取消): ").lower()
|
||
if save_choice == 'n':
|
||
print("此项更新已取消")
|
||
# 恢复原值
|
||
for field in fields_to_copy_t_p:
|
||
similar_t_p[field] = old_val
|
||
else:
|
||
print("没有找到T型P型对应项")
|
||
else:
|
||
print("没有找到T型M型相似项")
|
||
def process_dreifachweiche_items(data, WeichenkProfileWidth):
|
||
# 筛选Dreifachweiche类型的项
|
||
filtered_items = [
|
||
item for item in data
|
||
if (item.get("WeichenTyp") == "Dreifachweiche" and
|
||
item.get("Schaltungstyp") == "M" and
|
||
item.get("SivasnrTEF") is None)
|
||
]
|
||
|
||
print(f"\n\n共找到 {len(filtered_items)} 项Dreifachweiche类型记录")
|
||
print("="*50)
|
||
|
||
for idx, item in enumerate(filtered_items, 1):
|
||
print(f"\n第 {idx} 项Dreifachweiche:")
|
||
print(f"Sivasnr: {item['Sivasnr']}")
|
||
print(f"ProfilTyp: {item['ProfilTyp']}")
|
||
|
||
# 交互修改OFWeiche_center_line_width_mm
|
||
current_value = item.get("OFWeiche_center_line_width_mm")
|
||
print(f"\n当前 OFWeiche_center_line_width_mm: {current_value}")
|
||
choice = input("是否修改? (y修改,直接回车跳过): ").lower()
|
||
if choice == 'y':
|
||
new_value = input(f"请输入新值 (当前: {current_value}): ")
|
||
try:
|
||
old_value = item["OFWeiche_center_line_width_mm"]
|
||
item["OFWeiche_center_line_width_mm"] = round(float(new_value), 3) if new_value.lower() != 'null' else None
|
||
print(f"值已更新: {old_value} → {item['OFWeiche_center_line_width_mm']}")
|
||
except ValueError:
|
||
print("输入无效,保持原值")
|
||
|
||
# 交互修改OFWeiche_center_line_height_mm
|
||
current_value = item.get("OFWeiche_center_line_height_mm")
|
||
print(f"\n当前 OFWeiche_center_line_height_mm: {current_value}")
|
||
choice = input("是否修改? (y修改,直接回车跳过): ").lower()
|
||
if choice == 'y':
|
||
new_value = input(f"请输入新值 (当前: {current_value}): ")
|
||
try:
|
||
old_value = item["OFWeiche_center_line_height_mm"]
|
||
item["OFWeiche_center_line_height_mm"] = round(float(new_value), 3) if new_value.lower() != 'null' else None
|
||
print(f"值已更新: {old_value} → {item['OFWeiche_center_line_height_mm']}")
|
||
except ValueError:
|
||
print("输入无效,保持原值")
|
||
|
||
# 计算相关值
|
||
if (item["OFWeiche_center_line_width_mm"] is not None and
|
||
item["OFWeiche_center_line_height_mm"] is not None):
|
||
|
||
# 计算基本尺寸
|
||
item["Objekte_width_mm"] = round(item["OFWeiche_center_line_width_mm"], 3)
|
||
print(f"\nObjekte_width_mm 计算更新: {item['Objekte_width_mm']}")
|
||
|
||
item["Objekte_height_mm"] = round(WeichenkProfileWidth/2 + item["OFWeiche_center_line_height_mm"], 3)
|
||
print(f"Objekte_height_mm 计算更新: {item['Objekte_height_mm']}")
|
||
|
||
# 计算控制点
|
||
item["OFWeiche_CP1_x_mm"] = round(item["Objekte_width_mm"]/2, 3)
|
||
item["OFWeiche_CP1_y_mm"] = 0
|
||
item["OFWeiche_CP2_x_mm"] = 0
|
||
item["OFWeiche_CP2_y_mm"] = round(item["OFWeiche_center_line_height_mm"], 3)
|
||
item["OFWeiche_CP3_x_mm"] = round(item["OFWeiche_center_line_width_mm"], 3)
|
||
item["OFWeiche_CP3_y_mm"] = item["OFWeiche_CP2_y_mm"]
|
||
print(f"OFWeiche_CP1_x_mm: {item['OFWeiche_CP1_x_mm']}")
|
||
print(f"OFWeiche_CP1_y_mm: {item['OFWeiche_CP1_y_mm']}")
|
||
print(f"OFWeiche_CP2_x_mm: {item['OFWeiche_CP2_x_mm']}")
|
||
print(f"OFWeiche_CP2_y_mm: {item['OFWeiche_CP2_y_mm']}")
|
||
print(f"OFWeiche_CP3_x_mm: {item['OFWeiche_CP3_x_mm']}")
|
||
print(f"OFWeiche_CP3_y_mm: {item['OFWeiche_CP3_y_mm']}")
|
||
|
||
# 查找相似项(P型)
|
||
if "WEICHE S C DELTA" in item["ProfilTyp"]:
|
||
similar_profil = item["ProfilTyp"].replace("KPL. M", "KPL. P")
|
||
|
||
similar_items = [x for x in data
|
||
if x["ProfilTyp"] == similar_profil and
|
||
x.get("SivasnrTEF") is None]
|
||
|
||
if similar_items:
|
||
print(f"\n找到 {len(similar_items)} 个相似P型项")
|
||
for similar in similar_items:
|
||
print(f"\n正在更新: {similar['ProfilTyp']} (Sivasnr: {similar['Sivasnr']})")
|
||
|
||
fields_to_copy = [
|
||
"OFWeiche_center_line_width_mm",
|
||
"OFWeiche_center_line_height_mm",
|
||
"Objekte_width_mm",
|
||
"Objekte_height_mm",
|
||
"OFWeiche_CP1_x_mm",
|
||
"OFWeiche_CP1_y_mm",
|
||
"OFWeiche_CP2_x_mm",
|
||
"OFWeiche_CP2_y_mm",
|
||
"OFWeiche_CP3_x_mm",
|
||
"OFWeiche_CP3_y_mm"
|
||
]
|
||
|
||
print("\n更新中:")
|
||
for field in fields_to_copy:
|
||
print(f" {field}: {similar.get(field)} → {item[field]}")
|
||
similar[field] = item[field]# 实际更新相似项的值
|
||
|
||
choice = input("\n确认更新此项? (回车确认/n取消): ").lower()
|
||
if choice == 'n':
|
||
print("取消此项更新")
|
||
for field in fields_to_copy:
|
||
similar[field] = similar[field]
|
||
else:
|
||
print("此项更新已确认")
|
||
|
||
else:
|
||
print("没有找到相似P型项")
|
||
|
||
if __name__ == "__main__":
|
||
json_file_path = "omniflo_weichen.json" # 替换为你的JSON文件路径
|
||
modify_json_values(json_file_path) |