From 81df82be1f5f401b80780f65e5811685d9a8e4c4 Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期五, 19 九月 2025 18:03:24 +0800
Subject: [PATCH] 1.修复打开软件加载CControlJob时,玻璃context丢失的问题;

---
 SourceCode/Bond/Servo/CMaster.cpp |   51 +++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 43 insertions(+), 8 deletions(-)

diff --git a/SourceCode/Bond/Servo/CMaster.cpp b/SourceCode/Bond/Servo/CMaster.cpp
index ac0a419..3c1adba 100644
--- a/SourceCode/Bond/Servo/CMaster.cpp
+++ b/SourceCode/Bond/Servo/CMaster.cpp
@@ -213,10 +213,9 @@
 
 
 
-
-		
 		// 读缓存数据
 		readCache();
+		loadState();
 
 
 		// 定时器
@@ -2576,13 +2575,9 @@
 		return true;
 	}
 
-	bool CMaster::loadState(const std::string& path)
+	bool CMaster::loadState()
 	{
-		// 保存文件路径
-		m_strStatePath = path;
-
-
-		std::ifstream ifs(path, std::ios::binary);
+		std::ifstream ifs(m_strStatePath, std::ios::binary);
 		if (!ifs) return false;
 
 		// 文件头
@@ -2635,10 +2630,33 @@
 		m_pControlJob->setPJs(tempPjs);
 
 
+		// 更新contexts
+		auto pjs = m_pControlJob->getPjs();
+		for (auto pj : pjs) {
+			for (auto& c : pj->carriers()) {
+				auto p = getPortWithCarrierId(c.carrierId);
+				if (p == nullptr) continue;
+
+				std::vector<void*> contexts;
+				for (auto s : c.slots) {
+					auto g = getGlass(p->getIndex(), s - 1);
+					if (g == nullptr) continue;
+					contexts.push_back(g);
+				}
+				pj->setCarrierContexts(c.carrierId, contexts);
+			}
+		}
+
+
 		// 如果版本升级,可在这里判断 version 来加载新字段
 
 
 		return true;
+	}
+
+	void CMaster::setStateFile(const std::string& path)
+	{
+		m_strStatePath = path;
 	}
 
 	CProcessJob* CMaster::acquireNextProcessJob()
@@ -2899,4 +2917,21 @@
 
 		return true;
 	}
+
+	CGlass* CMaster::getGlass(int scrPort, int scrSlot)
+	{
+		for (auto eq : m_listEquipment) {
+			std::vector<CGlass*> glasses;
+			eq->getAllGlass(glasses);
+			for (auto g : glasses) {
+				int p, s;
+				g->getOrginPort(p, s);
+				if (p == scrPort && s == scrSlot) {
+					return g;
+				}
+			}
+		}
+
+		return nullptr;
+	}
 }

--
Gitblit v1.9.3