feat: 增强DXF导出和命令行参数支持

This commit is contained in:
dmy
2026-01-01 14:31:46 +08:00
parent 2d50ab0df0
commit 6454a2c01e

87
main.py
View File

@@ -8,6 +8,7 @@ from collections import defaultdict
import networkx as nx import networkx as nx
import math import math
import argparse import argparse
import os
# 设置matplotlib支持中文显示 # 设置matplotlib支持中文显示
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei', 'SimHei', 'Arial'] plt.rcParams['font.sans-serif'] = ['Microsoft YaHei', 'SimHei', 'Arial']
@@ -66,6 +67,7 @@ def generate_wind_farm_data(n_turbines=30, seed=42, layout='random', spacing=800
'x': x_coords, 'x': x_coords,
'y': y_coords, 'y': y_coords,
'power': power_ratings, 'power': power_ratings,
'platform_height': np.zeros(n_turbines),
'cumulative_power': np.zeros(n_turbines) # 用于后续计算 'cumulative_power': np.zeros(n_turbines) # 用于后续计算
}) })
@@ -410,12 +412,22 @@ def get_max_cable_capacity_mw(cable_specs=None):
异常: 异常:
Exception: 当未提供 cable_specs 时抛出,提示截面不满足。 Exception: 当未提供 cable_specs 时抛出,提示截面不满足。
""" """
if cable_specs: if cable_specs is None:
# 从所有电缆规格中找到最大的额定电流容量 # Default cable specs if not provided (same as in evaluate_design)
max_current_capacity = max(spec[1] for spec in cable_specs) cable_specs = [
else: (35, 150, 0.524, 80),
# 如果没有传入电缆规格,通常意味着已尝试过所有规格但仍不满足需求 (70, 215, 0.268, 120),
raise Exception("没有提供电缆参数") (95, 260, 0.193, 150),
(120, 295, 0.153, 180),
(150, 330, 0.124, 220),
(185, 370, 0.0991, 270),
(240, 425, 0.0754, 350),
(300, 500, 0.0601, 450),
(400, 580, 0.0470, 600)
]
# 从所有电缆规格中找到最大的额定电流容量
max_current_capacity = max(spec[1] for spec in cable_specs)
# 计算最大功率P = √3 * U * I * cosφ # 计算最大功率P = √3 * U * I * cosφ
# 这里假设降额系数为 1 (不降额) # 这里假设降额系数为 1 (不降额)
@@ -593,15 +605,32 @@ def export_to_dxf(turbines, substation, connections_details, filename):
# 1. 建立图层 # 1. 建立图层
doc.layers.add('Substation', color=1) # Red doc.layers.add('Substation', color=1) # Red
doc.layers.add('Turbines', color=5) # Blue doc.layers.add('Turbines', color=5) # Blue
doc.layers.add('Cable_35mm', color=3) # Green
doc.layers.add('Cable_70mm', color=130) # 动态确定电缆颜色
doc.layers.add('Cable_95mm', color=150) # 提取所有使用到的电缆截面
doc.layers.add('Cable_120mm', color=4) # Cyan used_sections = sorted(list(set(conn['cable']['cross_section'] for conn in connections_details)))
doc.layers.add('Cable_150mm', color=6) # Magenta
doc.layers.add('Cable_185mm', color=30) # Orange # 定义颜色映射规则 (AutoCAD Color Index)
doc.layers.add('Cable_240mm', color=1) # Red # 1=Red, 2=Yellow, 3=Green, 4=Cyan, 5=Blue, 6=Magenta
doc.layers.add('Cable_300mm', color=250) color_rank = [
doc.layers.add('Cable_400mm', color=7) # White/Black 2, # 1st (Smallest): Yellow
3, # 2nd: Green
1, # 3rd: Red
5, # 4th: Blue
6 # 5th: Magenta
]
default_color = 3 # Others: Green
# 创建电缆图层
for i, section in enumerate(used_sections):
if i < len(color_rank):
c = color_rank[i]
else:
c = default_color
layer_name = f'Cable_{section}mm'
if layer_name not in doc.layers:
doc.layers.add(layer_name, color=c)
# 2. 绘制升压站 # 2. 绘制升压站
sx, sy = substation[0, 0], substation[0, 1] sx, sy = substation[0, 0], substation[0, 1]
@@ -856,13 +885,21 @@ def compare_design_methods(excel_path=None, n_clusters_override=None):
ax=axes[1]) ax=axes[1])
plt.tight_layout() plt.tight_layout()
output_filename = 'wind_farm_design_imported.png' if excel_path else 'offshore_wind_farm_design.png' output_filename = 'wind_farm_design_comparison.png'
plt.savefig(output_filename, dpi=300) plt.savefig(output_filename, dpi=300)
# 导出DXF # 导出最佳方案 DXF
dxf_filename = 'wind_farm_design.dxf' if excel_path:
# 默认导出更优的方案通常K-means扇区聚类在海上更合理或者成本更低者 base_name = os.path.splitext(os.path.basename(excel_path))[0]
# 这里我们导出Sector Clustering的结果 dir_name = os.path.dirname(excel_path)
# 如果 dir_name 为空(即当前目录),则直接使用文件名,或者使用 os.path.join('', ...) 也是安全的
dxf_filename = os.path.join(dir_name, f"{base_name}.dxf")
else:
dxf_filename = 'wind_farm_design.dxf'
# Use the connection details from the best evaluation
# Note: We need the turbine DataFrame corresponding to the best method to get cluster IDs if possible,
# but export_to_dxf mainly needs coordinates which are constant.
export_to_dxf(clustered_turbines, substation, kmeans_evaluation['details'], dxf_filename) export_to_dxf(clustered_turbines, substation, kmeans_evaluation['details'], dxf_filename)
# plt.show() # plt.show()
@@ -921,14 +958,10 @@ def compare_design_methods(excel_path=None, n_clusters_override=None):
if __name__ == "__main__": if __name__ == "__main__":
# 解析命令行参数 # 解析命令行参数
parser = argparse.ArgumentParser(description='Wind Farm Collector System Design') parser = argparse.ArgumentParser(description='Wind Farm Collector System Design')
parser.add_argument('--excel', help='Path to the Excel coordinates file', default=None)
parser.add_argument('--clusters', type=int, help='Specify the number of clusters (circuits) manually', default=None) parser.add_argument('--clusters', type=int, help='Specify the number of clusters (circuits) manually', default=None)
args = parser.parse_args() args = parser.parse_args()
# 2. 读取 Excel 坐标数据 (如果存在)
excel_path = 'coordinates2.xlsx'
# 尝试从命令行参数获取文件路径 (可选扩展)
# if len(sys.argv) > 1: excel_path = sys.argv[1]
# 3. 运行比较 # 3. 运行比较
# 如果本地没有excel文件将自动回退到生成数据模式 # 如果没有提供excel文件将自动回退到生成数据模式
compare_design_methods(excel_path, n_clusters_override=args.clusters) compare_design_methods(args.excel, n_clusters_override=args.clusters)