forked from huangxin/ailai
105 lines
4.2 KiB
Python
105 lines
4.2 KiB
Python
# File: drop_position_manager.py
|
||
|
||
import configparser
|
||
import os
|
||
from typing import Optional
|
||
from Model.Position import Real_Position
|
||
|
||
class DropPositionManager:
|
||
def __init__(self, config_path="drop.ini"):
|
||
self.config_path = config_path
|
||
self.config = configparser.ConfigParser()
|
||
self._load_config()
|
||
|
||
def _load_config(self):
|
||
if not os.path.exists(self.config_path):
|
||
raise FileNotFoundError(f"配置文件不存在: {self.config_path}")
|
||
self.config.read(self.config_path, encoding='utf-8')
|
||
|
||
def _save_config(self):
|
||
with open(self.config_path, 'w', encoding='utf-8') as f:
|
||
self.config.write(f)
|
||
|
||
def get_next_drop_position(self, lineid: int = 1) -> Optional[Real_Position]:
|
||
print(f"\n🔄 开始获取 lineid={lineid} 的下一个 drop 点...")
|
||
|
||
line_section = f"DropLine{lineid}"
|
||
print(f"🔍 查找路径 section: {line_section}")
|
||
|
||
if not self.config.has_section(line_section):
|
||
print(f"❌ 错误:配置文件中不存在 section [{line_section}]")
|
||
print(f"📊 所有 sections: {list(self.config.sections())}")
|
||
return None
|
||
|
||
current_index = self.config.getint(line_section, "current_index", fallback=0)
|
||
print(f"📌 当前索引: {current_index} (fallback=0 如果未设置)")
|
||
|
||
points = []
|
||
found_sections = []
|
||
matched_sections = []
|
||
|
||
for sec in self.config.sections():
|
||
if sec.startswith("DropPoints"):
|
||
found_sections.append(sec)
|
||
try:
|
||
file_lineid = self.config.getint(sec, "lineid")
|
||
print(f"🔍 {sec}: lineid={file_lineid}")
|
||
if file_lineid == lineid:
|
||
matched_sections.append(sec)
|
||
pos = Real_Position()
|
||
pos.X = self.config.getfloat(sec, "x")
|
||
pos.Y = self.config.getfloat(sec, "y")
|
||
pos.Z = self.config.getfloat(sec, "z")
|
||
pos.U = self.config.getfloat(sec, "u")
|
||
pos.V = self.config.getfloat(sec, "v")
|
||
pos.W = self.config.getfloat(sec, "w")
|
||
order = self.config.getint(sec, "order")
|
||
points.append((order, pos))
|
||
print(f"✅ 匹配到点: {sec}, order={order}, 位置=({pos.X:.3f}, {pos.Y:.3f})")
|
||
except Exception as e:
|
||
print(f"❌ 读取 {sec} 失败: {e}")
|
||
|
||
print(f"📁 找到 DropPoints: {found_sections}")
|
||
print(f"🟢 匹配 lineid={lineid} 的点: {matched_sections}")
|
||
print(f"📊 共收集 {len(points)} 个有效点")
|
||
|
||
if not points:
|
||
print("❌ 没有可用的 drop 点!请检查:lineid 是否匹配,字段是否正确")
|
||
return None
|
||
|
||
# 按 order 排序
|
||
points.sort(key=lambda x: x[0])
|
||
sorted_positions = [pos for _, pos in points]
|
||
count = len(sorted_positions)
|
||
actual_index = current_index % count
|
||
|
||
selected_pos = sorted_positions[actual_index]
|
||
print(f"🎯 选择第 {actual_index} 个点: X={selected_pos.X:.3f}, Y={selected_pos.Y:.3f}, Z={selected_pos.Z:.3f}")
|
||
|
||
# 索引 +1 并保存回 ini
|
||
next_index = current_index + 1
|
||
self.config.set(line_section, "current_index", str(next_index))
|
||
self._save_config()
|
||
print(f"💾 已保存 current_index = {next_index} 到 [{line_section}]")
|
||
|
||
return selected_pos
|
||
|
||
|
||
# ✅ 主函数:用于测试
|
||
if __name__ == "__main__":
|
||
# 创建管理器,加载 drop.ini
|
||
manager = DropPositionManager("drop.ini")
|
||
|
||
# 获取第一个 drop 点(lineid=1)
|
||
pos = manager.get_next_drop_position(lineid=1)
|
||
|
||
if pos is not None:
|
||
print(f"\n🎉 成功返回点位: X={pos.X}, Y={pos.Y}, Z={pos.Z}, U={pos.U}, V={pos.V}, W={pos.W}")
|
||
else:
|
||
print("\n❌ get_next_drop_position 返回了 None,请根据上面的日志排查")
|
||
print("💡 常见原因:")
|
||
print(" 1. drop.ini 缺少 current_index")
|
||
print(" 2. lineid 不匹配")
|
||
print(" 3. Real_Position 属性名错误(应为大写 X/Y/Z)")
|
||
print(" 4. 文件路径不对")
|