216 lines
6.6 KiB
Python
216 lines
6.6 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
极电子程序测试项转换脚本
|
||
将极电子程序的 .ats 文件转换为我的项目的 .ats 文件
|
||
|
||
设备映射关系:
|
||
- EA3040_40C → ANEVH80(水冷机,占位符)
|
||
- IT6724C → IT6724C(直流源)
|
||
- PSB11500_60 → PSB11500_60(交流源/负载)
|
||
- SQ0090G1D1 → SQ0090G1D1(三相交流源)
|
||
- IOBoardCard → IOBoardGroup(占位符,只传台架序号)
|
||
- TSMasterCAN.CAN → TSMasterCAN.CAN(相同)
|
||
"""
|
||
|
||
import json
|
||
import os
|
||
import uuid
|
||
import re
|
||
from pathlib import Path
|
||
|
||
# 输入输出路径
|
||
INPUT_DIR = Path(r"C:\Users\kk\Desktop\ACP\极电子程序")
|
||
OUTPUT_DIR = Path(r"D:\ACP\测试项\极电子对应")
|
||
|
||
# 设备映射
|
||
DEVICE_MAPPING = {
|
||
"DeviceCommand.Device.EA3040_40C": "DeviceCommand.Device.ANEVH80", # 水冷机(占位符)
|
||
"DeviceCommand.Device.IT6724C": "DeviceCommand.Device.IT6724C",
|
||
"DeviceCommand.Device.PSB11500_60": "DeviceCommand.Device.PSB11500_60",
|
||
"DeviceCommand.Device.SQ0090G1D1": "DeviceCommand.Device.SQ0090G1D1",
|
||
"DeviceCommand.Device.IOBoardCard": "DeviceCommand.Device.IOBoardGroup", # IO板卡(占位符)
|
||
"TSMasterCAN.CAN": "TSMasterCAN.CAN",
|
||
}
|
||
|
||
# 方法映射(ANEVH80 占位符方法)
|
||
ANVH80_METHOD_MAPPING = {
|
||
"Set_RemoteMode": "Placeholder_SetRemoteMode",
|
||
"Set_Voltage": "Placeholder_SetVoltage",
|
||
"Set_Current": "Placeholder_SetCurrent",
|
||
"Set_Power": "Placeholder_SetPower",
|
||
"ON": "Placeholder_ON",
|
||
"OFF": "Placeholder_OFF",
|
||
}
|
||
|
||
# IOBoardGroup 占位符方法
|
||
IOBOARDGROUP_METHOD_MAPPING = {
|
||
"WriteMultiIO": "Placeholder_WriteMultiIO",
|
||
"WriteSingleIO": "Placeholder_WriteSingleIO",
|
||
"ReadMultiIO": "Placeholder_ReadMultiIO",
|
||
"ReadSingleIO": "Placeholder_ReadSingleIO",
|
||
}
|
||
|
||
|
||
def generate_guid():
|
||
"""生成新的 GUID"""
|
||
return str(uuid.uuid4())
|
||
|
||
|
||
def transform_parameter(param, add_station_var=False):
|
||
"""转换参数格式"""
|
||
new_param = {
|
||
"ID": generate_guid(),
|
||
"IsVisible": param.get("IsVisible", True),
|
||
"Name": param.get("Name", ""),
|
||
"Type": param.get("Type", ""),
|
||
"Category": param.get("Category", 0),
|
||
"IsGlobal": param.get("IsGlobal", False),
|
||
"InitialValue": param.get("InitialValue"),
|
||
"Value": param.get("Value"),
|
||
"LowerLimit": param.get("LowerLimit"),
|
||
"UpperLimit": param.get("UpperLimit"),
|
||
"Result": param.get("Result", True),
|
||
"IsUseVar": param.get("IsUseVar", False),
|
||
"IsSave": param.get("IsSave", False),
|
||
"VariableName": param.get("VariableName"),
|
||
"VariableID": param.get("VariableID"),
|
||
"IsOutputToReport": param.get("IsOutputToReport", False),
|
||
}
|
||
return new_param
|
||
|
||
|
||
def transform_method(method, step_name=""):
|
||
"""转换方法"""
|
||
if method is None:
|
||
return None
|
||
|
||
full_name = method.get("FullName", "")
|
||
method_name = method.get("Name", "")
|
||
|
||
# 设备映射
|
||
new_full_name = DEVICE_MAPPING.get(full_name, full_name) if full_name else None
|
||
|
||
# 方法映射(占位符)
|
||
if new_full_name and "ANEVH80" in new_full_name:
|
||
new_method_name = ANVH80_METHOD_MAPPING.get(method_name, method_name)
|
||
elif new_full_name and "IOBoardGroup" in new_full_name:
|
||
new_method_name = IOBOARDGROUP_METHOD_MAPPING.get(method_name, method_name)
|
||
else:
|
||
new_method_name = method_name
|
||
|
||
# 转换参数
|
||
new_params = []
|
||
for param in method.get("Parameters", []):
|
||
new_param = transform_parameter(param)
|
||
new_params.append(new_param)
|
||
|
||
new_method = {
|
||
"Name": new_method_name,
|
||
"FullName": new_full_name,
|
||
"DeviceID": method.get("DeviceID"),
|
||
"Parameters": new_params,
|
||
}
|
||
|
||
return new_method
|
||
|
||
|
||
def transform_step(step):
|
||
"""转换步骤"""
|
||
new_step = {
|
||
"ID": generate_guid(),
|
||
"IsUsed": step.get("IsUsed", True),
|
||
"Index": step.get("Index", 1),
|
||
"Name": step.get("Name", ""),
|
||
"StepType": step.get("StepType", "方法"),
|
||
"Method": None,
|
||
"SubProgram": None,
|
||
"LoopCount": step.get("LoopCount"),
|
||
"LoopStartStepId": step.get("LoopStartStepId"),
|
||
"OKExpression": step.get("OKExpression"),
|
||
"GotoSettingString": step.get("GotoSettingString", ""),
|
||
"OKGotoStepID": step.get("OKGotoStepID"),
|
||
"NGGotoStepID": step.get("NGGotoStepID"),
|
||
"Description": step.get("Description"),
|
||
}
|
||
|
||
# 转换方法
|
||
if step.get("Method"):
|
||
new_step["Method"] = transform_method(step["Method"], step.get("Name", ""))
|
||
|
||
# 转换子程序
|
||
if step.get("SubProgram"):
|
||
sub_program = step["SubProgram"]
|
||
new_sub_steps = []
|
||
for sub_step in sub_program.get("StepCollection", []):
|
||
new_sub_step = transform_step(sub_step)
|
||
new_sub_steps.append(new_sub_step)
|
||
|
||
new_step["SubProgram"] = {
|
||
"ID": generate_guid(),
|
||
"StepCollection": new_sub_steps,
|
||
"Parameters": [transform_parameter(p) for p in sub_program.get("Parameters", [])],
|
||
"Devices": sub_program.get("Devices", []),
|
||
}
|
||
|
||
return new_step
|
||
|
||
|
||
def transform_program(program):
|
||
"""转换整个程序"""
|
||
new_steps = []
|
||
for step in program.get("StepCollection", []):
|
||
new_step = transform_step(step)
|
||
new_steps.append(new_step)
|
||
|
||
new_program = {
|
||
"ID": generate_guid(),
|
||
"StepCollection": new_steps,
|
||
"Parameters": [transform_parameter(p) for p in program.get("Parameters", [])],
|
||
"Devices": program.get("Devices", []),
|
||
}
|
||
|
||
return new_program
|
||
|
||
|
||
def process_file(input_file, output_file):
|
||
"""处理单个文件"""
|
||
print(f"处理: {input_file.name}")
|
||
|
||
# 读取文件(支持 UTF-8 BOM)
|
||
with open(input_file, 'r', encoding='utf-8-sig') as f:
|
||
program = json.load(f)
|
||
|
||
# 转换
|
||
new_program = transform_program(program)
|
||
|
||
# 写入文件
|
||
with open(output_file, 'w', encoding='utf-8') as f:
|
||
json.dump(new_program, f, ensure_ascii=False, indent=2)
|
||
|
||
print(f" -> 已生成: {output_file.name}")
|
||
|
||
|
||
def main():
|
||
"""主函数"""
|
||
# 创建输出目录
|
||
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
||
|
||
# 获取所有 .ats 文件
|
||
ats_files = list(INPUT_DIR.glob("*.ats"))
|
||
|
||
print(f"找到 {len(ats_files)} 个 .ats 文件")
|
||
print(f"输出目录: {OUTPUT_DIR}")
|
||
print()
|
||
|
||
# 处理每个文件
|
||
for input_file in ats_files:
|
||
output_file = OUTPUT_DIR / input_file.name
|
||
process_file(input_file, output_file)
|
||
|
||
print()
|
||
print(f"完成!共处理 {len(ats_files)} 个文件")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|