import matlab.engine import sys import os import json import mainMapEcharts import shutil # 获取当前文件的父目录的绝对路径 current_dir = os.path.dirname(os.path.abspath(__file__)) # 获取父目录的绝对路径 parent_dir = os.path.abspath(os.path.join(current_dir, os.pardir)) # 将父目录添加到 Python 模块搜索路径中 sys.path.append(parent_dir) from Utils import utils as globalUtils # python.exe .\main.py E:\01-Projects\01-CAE-FullLink\output\workingDir\test1\PlatformPerformance\a_sat.txt E:\01-Projects\01-CAE-FullLink\output\workingDir\test2\GeometricSightTracking\output outputDir = "output" # 执行一次求解 def calculation(eng: matlab.engine): # 执行一次求解 eng.main(nargout=0) def readInputGeo(): filePath = "input_geo.txt" paramNameList = "" paramValueList = [] with open(filePath, "r", encoding="utf-8") as file: lines = file.readlines() paramNameList = lines[0] paramValueList = lines[1] return [paramNameList, paramValueList.split(",")] # 生成参数组合 def genParamList(filePath: str): paramList = [] with open(filePath, "r", encoding="utf-8") as file: for line in file: paramList.append(line.split()[1:]) return paramList # 生成一个求解参数文件 def genParamFile(paramVector: list, geoParamNameList: list, geoParamValueList: list): paramValueList = [geoParamValueList[0]] + paramVector + geoParamValueList[1:] globalUtils.printWithFlush(paramValueList) globalUtils.printWithFlush(geoParamNameList) with open("input.txt", "w") as file: file.write(geoParamNameList) file.write(",".join(map(str, paramValueList))) pass # 将输出文件转换为json格式 def genGeoJsonFileFromOutput(outputFilePath: str): if not os.path.exists(outputFilePath): return jsonFilePath = outputFilePath.replace(".txt", ".json") geoDataDict = {} i = 1 with open(outputFilePath, "r", encoding="utf-8") as file: for line in file: geoDataDict[str(i)] = [float(x) for x in line.split()] i += 1 with open(jsonFilePath, "w") as file: file.write(json.dumps(geoDataDict)) pass def isDir(dirPath: str): return os.path.exists(dirPath) and os.path.isdir(dirPath) def rmPath(fileOrDirPath: str): if os.path.exists(fileOrDirPath): if os.path.isdir(fileOrDirPath): shutil.rmtree(fileOrDirPath) elif os.path.isfile(fileOrDirPath): os.remove(fileOrDirPath) # 主函数 def main(): # 平台性能模块参数部分 aSatFilePath = sys.argv[1] # 结果文件的目录地址 outputDistDir = sys.argv[2] # 读取参数组合 paramList = genParamList(aSatFilePath) # TODO,检查,是否有 input_geo.txt、平台性能模块文件有参数 # 读取 geo模块的参数 paramDict = readInputGeo() paramNameList = paramDict[0] paramValueList = paramDict[1] # 判断output目录是否存在,不存在在创建 rmPath(outputDir) os.mkdir(outputDir) allPoints = [] eng = matlab.engine.start_matlab() for index, paramVector in enumerate(paramList): # 生成求解参数文件 genParamFile(paramVector, paramNameList, paramValueList) # 调用求解器求解 calculation(eng) # 将结果文件移动到output目录中 outputFileName = outputDir + "/output" + str(index) + ".txt" if os.path.exists(outputFileName): os.remove(outputFileName) os.rename("output.txt", outputFileName) # 将结果数据生成html mainMapEcharts.rendOutputGeo(outputFileName) # 将每个结果文件的第一行独立存储 allPoints.append(globalUtils.readFileFirstLine(outputFileName)) # 将结果数据生成为json文件 # genGeoJsonFileFromOutput(outputFilePath=outputFileName) # break # 删除旧的目录 rmPath(outputDistDir) # 生成总体的图 globalUtils.writeFile(allPoints, outputDir + "/output_all.txt") mainMapEcharts.rendOutputGeo(outputDir + "/output_all.txt") # 将结果拷贝的目标目录下 shutil.copytree(outputDir, outputDistDir) # 将2个js文件拷贝到output目录下 echartjsFilePath = "../../resources/echarts.min.js" worldjsFilePath = "../../resources/world.js" shutil.copy(echartjsFilePath, os.path.join(outputDistDir, os.path.basename(echartjsFilePath))) shutil.copy(worldjsFilePath, os.path.join(outputDistDir, os.path.basename(worldjsFilePath))) if __name__ == "__main__": try: # 尝试执行的代码块 main() except Exception as e: # 捕获除了ZeroDivisionError外的所有异常 globalUtils.printWithFlush("发生异常:", str(e)) finally: # 无论是否发生异常,最终都会执行该代码块 globalUtils.printWithFlush("求解执行完毕!")