LAPTOP-SNT8I5JK\Boounion
2025-07-28 bfe14e41fa5b07771d78af4511ba18d706bc23cc
SourceCode/Bond/Servo/PageRecipe.cpp
@@ -5,6 +5,8 @@
#include "Servo.h"
#include "afxdialogex.h"
#include "PageRecipe.h"
#include "MsgDlg.h"
// CPageRecipe 对话框
@@ -22,7 +24,7 @@
void CPageRecipe::FillDataToListCtrl(const std::vector<RecipeInfo>& vecRecipe) {
   CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_PPID);
   if (pListCtrl == nullptr || pListCtrl->m_hWnd == nullptr) {
   if (pListCtrl == nullptr || !::IsWindow(pListCtrl->m_hWnd)) {
      return;
   }
@@ -49,12 +51,37 @@
   m_listPPID.SetColumnWidth(nColCount - 1, LVSCW_AUTOSIZE_USEHEADER);
}
void CPageRecipe::FillRecipeListToListCtrl(SERVO::CRecipeList* pList)
{
   CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_PPID);
   if (pListCtrl == nullptr || !::IsWindow(pListCtrl->m_hWnd)) {
      return;
   }
   // 清空当前CListCtrl中的所有项
   pListCtrl->DeleteAllItems();
   if (pList == nullptr) {
      return;
   }
   // 遍历数据并插入到CListCtrl中
   std::map<int, short>& ids = pList->getIds();
   for (auto item : ids) {
      int index = m_listPPID.InsertItem(m_listPPID.GetItemCount(), _T(""));
      m_listPPID.SetItemText(index, 1, std::to_string(item.first).c_str());
      m_listPPID.SetItemText(index, 2, std::to_string(item.second).c_str());
   }
   // 获取列数
   int nColCount = m_listPPID.GetHeaderCtrl()->GetItemCount();
   m_listPPID.SetColumnWidth(nColCount - 1, LVSCW_AUTOSIZE_USEHEADER);
}
void CPageRecipe::DoDataExchange(CDataExchange* pDX)
{
   CDialogEx::DoDataExchange(pDX);
   DDX_Control(pDX, IDC_LIST_PPID, m_listPPID);
   DDX_Control(pDX, IDC_EDIT_PPID, m_editPPID);
   DDX_Control(pDX, IDC_EDIT_DESC, m_editDesc);
}
@@ -66,6 +93,9 @@
   ON_BN_CLICKED(IDC_BUTTON_DELETE_ALL, &CPageRecipe::OnBnClickedButtonDeleteAll)
   ON_BN_CLICKED(IDC_BUTTON_REFRESH, &CPageRecipe::OnBnClickedButtonRefresh)
   ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_PPID, &CPageRecipe::OnLvnItemChangedListPPID)
   ON_WM_DESTROY()
   ON_CBN_SELCHANGE(IDC_COMBO_EQUIPMENT, &CPageRecipe::OnCbnSelchangeComboEquipment)
   ON_WM_SHOWWINDOW()
END_MESSAGE_MAP()
@@ -74,6 +104,16 @@
BOOL CPageRecipe::OnInitDialog()
{
   CDialogEx::OnInitDialog();
   // 读出列宽
   CString strIniFile, strItem;
   strIniFile.Format(_T("%s\\configuration.ini"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir);
   int width[8] = { 0, 80, 180, 80, 80, 100, 80, 180 };
   for (int i = 0; i < 8; i++) {
      strItem.Format(_T("Col_%d_Width"), i);
      width[i] = GetPrivateProfileInt("PageRecipeListCtrl", strItem, width[i], strIniFile);
   }
   // TODO:  在此添加额外的初始化
   CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_PPID);
@@ -85,11 +125,12 @@
   HIMAGELIST imageList = ImageList_Create(24, 24, ILC_COLOR24, 1, 1);
   ListView_SetImageList(pListCtrl->GetSafeHwnd(), imageList, LVSIL_SMALL);
   pListCtrl->InsertColumn(0, _T(""), LVCFMT_RIGHT, 0); // 隐藏列
   pListCtrl->InsertColumn(1, _T("No."), LVCFMT_LEFT, 80);
   pListCtrl->InsertColumn(2, _T("PPID"), LVCFMT_LEFT, 120);
   pListCtrl->InsertColumn(3, _T("描述"), LVCFMT_LEFT, 180);
   pListCtrl->InsertColumn(4, _T("创建时间"), LVCFMT_LEFT, 160);
   pListCtrl->InsertColumn(1, _T("No."), LVCFMT_LEFT, width[1]);
   pListCtrl->InsertColumn(2, _T("PPID/Recipe ID"), LVCFMT_LEFT, width[2]);
   pListCtrl->InsertColumn(3, _T("描述"), LVCFMT_LEFT, width[3]);
   pListCtrl->InsertColumn(4, _T("创建时间"), LVCFMT_LEFT, width[4]);
   pListCtrl->SetColumnWidth(4, LVSCW_AUTOSIZE_USEHEADER);
   // 获取所有数据
   auto vecData = RecipeManager::getInstance().getAllRecipes();
@@ -103,85 +144,58 @@
{
   CDialogEx::OnSize(nType, cx, cy);
   if (m_listPPID.GetSafeHwnd()) {
      // 左侧列表宽高自适应
      int margin = 10;
      int buttonWidth = 80;
      int buttonHeight = 30;
      int buttonSpacing = 10;
   if (!m_listPPID.GetSafeHwnd())
      return;
      CRect rect;
      GetClientRect(&rect);
      int listWidth = rect.Width() - buttonWidth - 3 * margin;
      int listHeight = rect.Height() - 2 * margin;
   // 左侧列表宽高自适应
   int margin = 12;
   int buttonWidth = 80;
   int buttonHeight = 30;
   int buttonSpacing = 10;
      m_listPPID.MoveWindow(margin, margin + 80, listWidth, listHeight - 80);
   CWnd* pItem;
   CRect rect, rcItem;
   GetClientRect(&rect);
      // 编辑框调整位置:右边对齐列表,左边固定起始
      int labelWidth = 60;
      int rightEdge = rect.right - buttonWidth - 2 * margin;
   pItem = GetDlgItem(IDC_EDIT_KEYWORD);
   ASSERT(pItem);
   pItem->GetWindowRect(rcItem);
   ScreenToClient(&rcItem);
      if (m_editPPID.GetSafeHwnd()) {
         m_editPPID.MoveWindow(labelWidth, margin, rightEdge - labelWidth, 25);
   int y = rcItem.bottom;
   y += 12;
   int listWidth = rect.Width() - buttonWidth - 3 * margin;
   m_listPPID.MoveWindow(margin, y, listWidth, rect.bottom - 12 - y);
   // 按钮竖直排列在右侧
   CWnd* buttons[] = {
      GetDlgItem(IDC_BUTTON_REFRESH),
      GetDlgItem(IDC_BUTTON_DELETE),
      GetDlgItem(IDC_BUTTON_DELETE_ALL),
      GetDlgItem(IDC_BUTTON_MODIFY)
   };
   for (auto pBtn : buttons) {
      if (pBtn && pBtn->GetSafeHwnd()) {
         pBtn->MoveWindow(rect.right - buttonWidth - margin, y, buttonWidth, buttonHeight);
         y += buttonHeight + buttonSpacing;
      }
      if (m_editDesc.GetSafeHwnd()) {
         m_editDesc.MoveWindow(labelWidth, margin + 35, rightEdge - labelWidth, 25);
      }
      // 按钮竖直排列在右侧
      CWnd* buttons[] = {
         GetDlgItem(IDC_BUTTON_SEARCH),
         GetDlgItem(IDC_BUTTON_MODIFY),
         GetDlgItem(IDC_BUTTON_DELETE),
         GetDlgItem(IDC_BUTTON_DELETE_ALL),
         GetDlgItem(IDC_BUTTON_REFRESH)
      };
      int y = margin;
      for (auto pBtn : buttons) {
         if (pBtn && pBtn->GetSafeHwnd()) {
            pBtn->MoveWindow(rect.right - buttonWidth - margin, y, buttonWidth, buttonHeight);
            y += buttonHeight + buttonSpacing;
         }
      }
      // 列宽重设
      int col0 = 50;  // No.
      int col1 = 120; // PPID
      int col3 = 160; // 创建时间自动填充
      int col2 = listWidth - col0 - col1 - col3 - 2;  // 描述自动填充
      if (col2 < 80) {
         col2 = 80;
      }
      m_listPPID.SetColumnWidth(1, col0);
      m_listPPID.SetColumnWidth(2, col1);
      m_listPPID.SetColumnWidth(3, col2);
      m_listPPID.SetColumnWidth(4, col3);
   }
}
void CPageRecipe::OnBnClickedButtonSearch()
{
   // TODO: 在此添加控件通知处理程序代码
   CString strInput;
   m_editPPID.GetWindowText(strInput);
   int nCount = m_listPPID.GetItemCount();
   for (int i = 0; i < nCount; ++i) {
      CString strItemText = m_listPPID.GetItemText(i, 2); // 第2列为PPID
      if (strItemText == strInput) {
         m_listPPID.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
         m_listPPID.EnsureVisible(i, FALSE);
         break;
      }
   }
   CString strKeyword;
   GetDlgItemText(IDC_EDIT_KEYWORD, strKeyword);
   AfxMessageBox(strKeyword);
}
void CPageRecipe::OnBnClickedButtonModify()
{
   // TODO: 在此添加控件通知处理程序代码
   /*
   POSITION pos = m_listPPID.GetFirstSelectedItemPosition();
   if (!pos) {
      AfxMessageBox(_T("请选择要修改的配方"));
@@ -238,6 +252,7 @@
         AfxMessageBox(_T("描述更新失败"));
      }
   }
   */
}
void CPageRecipe::OnBnClickedButtonDelete()
@@ -289,9 +304,42 @@
void CPageRecipe::OnBnClickedButtonRefresh()
{
   // TODO: 在此添加控件通知处理程序代码
   auto vecData = RecipeManager::getInstance().getAllRecipes();
   FillDataToListCtrl(vecData);
   CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
   int nSel = pComboBox->GetCurSel();
   SERVO::CEquipment* pEq = (SERVO::CEquipment*)pComboBox->GetItemDataPtr(nSel);
   if (pEq == nullptr) {
      // 获取master配方(本地)
      auto vecData = RecipeManager::getInstance().getAllRecipes();
      FillDataToListCtrl(vecData);
   }
   else {
      // enable port
      CMsgDlg msgDlg("请等待", "正在获取配方...");
      pEq->masterRecipeListRequest(0, [&](int status) -> void {
         if (status == SS_FAILED) {
            CString strMsg;
            strMsg.Format(_T("获取配方失败!"));
            msgDlg.DelayClose(3000);
            msgDlg.SetIcon(MSG_BOX_ERROR);
            msgDlg.SetTitle(_T("操作失败"));
            msgDlg.SetMessage((LPTSTR)(LPCTSTR)strMsg);
            msgDlg.SetMarquee(FALSE, 0);
            msgDlg.SetCompleteCode(-1);
         }
         else if (status == SS_COMPLETE) {
            CString strMsg;
            strMsg.Format(_T("获取配方完成!"));
            msgDlg.DelayClose(3000);
            msgDlg.SetIcon(MSG_BOX_SUCCEED);
            msgDlg.SetTitle(_T("操作成功"));
            msgDlg.SetMessage((LPTSTR)(LPCTSTR)strMsg);
            msgDlg.SetMarquee(FALSE, 0);
            msgDlg.SetCompleteCode(0);
         }
         });
      msgDlg.DoModal();
   }
}
void CPageRecipe::OnLvnItemChangedListPPID(NMHDR* pNMHDR, LRESULT* pResult)
@@ -299,14 +347,85 @@
   LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
   *pResult = 0;
   if ((pNMLV->uChanged & LVIF_STATE) &&
      (pNMLV->uNewState & LVIS_SELECTED)) {
   CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
   int nEqSel = pComboBox->GetCurSel();
   int selectedCount = ListView_GetSelectedCount(m_listPPID.GetSafeHwnd());
      int nItem = pNMLV->iItem;
      CString strPPID = m_listPPID.GetItemText(nItem, 2);
      m_editPPID.SetWindowText(strPPID);
   GetDlgItem(IDC_BUTTON_MODIFY)->EnableWindow(nEqSel == 0 && selectedCount > 0);
   GetDlgItem(IDC_BUTTON_DELETE)->EnableWindow(nEqSel == 0 && selectedCount > 0);
   GetDlgItem(IDC_BUTTON_DELETE_ALL)->EnableWindow(nEqSel == 0 && selectedCount > 0);
}
      CString strDesc = m_listPPID.GetItemText(nItem, 3);
      m_editDesc.SetWindowText(strDesc);
void CPageRecipe::OnCbnSelchangeComboEquipment()
{
   CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
   int nEqSel = pComboBox->GetCurSel();
   int selectedCount = ListView_GetSelectedCount(m_listPPID.GetSafeHwnd());
   GetDlgItem(IDC_BUTTON_MODIFY)->EnableWindow(nEqSel == 0 && selectedCount > 0);
   GetDlgItem(IDC_BUTTON_DELETE)->EnableWindow(nEqSel == 0 && selectedCount > 0);
   GetDlgItem(IDC_BUTTON_DELETE_ALL)->EnableWindow(nEqSel == 0 && selectedCount > 0);
   GetDlgItem(IDC_EDIT_KEYWORD)->EnableWindow(nEqSel == 0);
   GetDlgItem(IDC_BUTTON_SEARCH)->EnableWindow(nEqSel == 0);
   SERVO::CEquipment* pEq = (SERVO::CEquipment*)pComboBox->GetItemDataPtr(nEqSel);
   if (pEq == nullptr) {
      auto vecData = RecipeManager::getInstance().getAllRecipes();
      FillDataToListCtrl(vecData);
   }
}
   else {
      SERVO::CRecipeList* pRecipeList = pEq->getRecipeList(0);
      FillRecipeListToListCtrl(pRecipeList);
   }
}
void CPageRecipe::OnDestroy()
{
   CDialogEx::OnDestroy();
   // 保存列宽
   CString strIniFile, strItem, strTemp;
   strIniFile.Format(_T("%s\\configuration.ini"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir);
   CHeaderCtrl* pHeader = m_listPPID.GetHeaderCtrl();
   for (int i = 0; i < pHeader->GetItemCount(); i++) {
      RECT rect;
      pHeader->GetItemRect(i, &rect);
      strItem.Format(_T("Col_%d_Width"), i);
      strTemp.Format(_T("%d"), rect.right - rect.left);
      WritePrivateProfileString("PageRecipeListCtrl", strItem, strTemp, strIniFile);
   }
}
void CPageRecipe::OnShowWindow(BOOL bShow, UINT nStatus)
{
   CDialogEx::OnShowWindow(bShow, nStatus);
   if (bShow) {
      CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
      if (pComboBox->GetCount() == 0) {
         SERVO::CMaster& master = theApp.m_model.getMaster();
         SERVO::CEquipment* pEq[] = {
            nullptr,
            master.getEquipment(EQ_ID_EFEM),
            master.getEquipment(EQ_ID_Bonder1),
            master.getEquipment(EQ_ID_Bonder2),
            master.getEquipment(EQ_ID_BAKE_COOLING),
            master.getEquipment(EQ_ID_VACUUMBAKE),
            master.getEquipment(EQ_ID_MEASUREMENT),
         };
         CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
         for (int i = 0; i < sizeof(pEq) / sizeof(pEq[0]); i++) {
            pComboBox->InsertString(i,
               pEq[i] == nullptr ? _T("Master") : pEq[i]->getName().c_str());
            pComboBox->SetItemDataPtr(i, pEq[i]);
         }
         pComboBox->SetCurSel(0);
      }
   }
}