LAPTOP-T815PCOQ\25526
2024-11-25 6365b1d493ca23688fa1fb9a8fc51513dc6bb6d8
SourceCode/Bond/BondEq/View/AxisSettingsDlg.cpp
@@ -5,7 +5,10 @@
#include "BondEq.h"
#include "afxdialogex.h"
#include "AxisSettingsDlg.h"
#include "ToolUnits.h"
#define TIMER_INIT            1
#define TIMER_READ_PLC_DATA      2
// CAxisSettingsDlg 对话框
@@ -16,6 +19,7 @@
{
   m_nInitialWidth = 0;
   m_nInitialHeight = 0;
   m_pPLC = nullptr;
}
CAxisSettingsDlg::~CAxisSettingsDlg()
@@ -29,9 +33,56 @@
   m_mapFonts.clear();
}
void CAxisSettingsDlg::SetPLC(CPLC* pPLC)
{
   ASSERT(pPLC);
   m_pPLC = pPLC;
}
void CAxisSettingsDlg::DoDataExchange(CDataExchange* pDX)
{
   CDialogEx::DoDataExchange(pDX);
   DDX_Control(pDX, IDC_STATIC_AXIS_TEST_FLS, m_staticFLS);
   DDX_Control(pDX, IDC_STATIC_AXIS_TEST_DOG, m_staticDOG);
   DDX_Control(pDX, IDC_STATIC_AXIS_TEST_RLS, m_staticRLS);
   DDX_Control(pDX, IDC_STATIC_AXIS_TEST_READY, m_staticReady);
   DDX_Control(pDX, IDC_STATIC_AXIS_TEST_BUSY, m_staticBusy);
   DDX_Control(pDX, IDC_STATIC_AXIS_TEST_ERR, m_staticErr);
   DDX_Control(pDX, IDC_COMBO_AXIS_NAME, m_comboAxisNO);
   DDX_Control(pDX, IDC_STATIC_AXIS_NUMBER, m_staticAxisNO);
   DDX_Control(pDX, IDC_STATIC_AXIS_DESCRIP, m_staticAxisDescription);
   DDX_Control(pDX, IDC_STATIC_START_ADDRESS, m_staticStartAddress);
   DDX_Control(pDX, IDC_EDIT_AXIS_MODITFY_POS, m_editManualSpeed);
   DDX_Control(pDX, IDC_EDIT_AXIS_MODITFY_AUTO_SPEED, m_editAutoSpeed);
   DDX_Control(pDX, IDC_EDIT_AXIS_MODITFY_ACCE_TIME, m_editAccelerationTime);
   DDX_Control(pDX, IDC_EDIT_AXIS_MODITFY_DECE_TIME, m_editDecelerationTime);
   DDX_Control(pDX, IDC_EDIT_AXIS_MODITFY_MICROMENTUM, m_editJogDistance);
}
UINT CAxisSettingsDlg::FindIDByName(const CString& strControlID)
{
   // 将资源文件中定义的控件名称和 ID 加载到一个映射中
   static const std::map<CString, UINT> controlIdMap = {
      {"IDC_EDIT_AXIS_ANCHOR_POINT_DESCRIP1", IDC_EDIT_AXIS_ANCHOR_POINT_DESCRIP1},
      {"IDC_EDIT_AXIS_ANCHOR_POINT_DESCRIP2", IDC_EDIT_AXIS_ANCHOR_POINT_DESCRIP2},
      {"IDC_EDIT_AXIS_ANCHOR_POINT_DESCRIP3", IDC_EDIT_AXIS_ANCHOR_POINT_DESCRIP3},
      {"IDC_EDIT_AXIS_ANCHOR_POINT_DESCRIP4", IDC_EDIT_AXIS_ANCHOR_POINT_DESCRIP4},
      {"IDC_EDIT_AXIS_ANCHOR_POINT_DESCRIP5", IDC_EDIT_AXIS_ANCHOR_POINT_DESCRIP5},
      {"IDC_EDIT_AXIS_ANCHOR_POINT1", IDC_EDIT_AXIS_ANCHOR_POINT1},
      {"IDC_EDIT_AXIS_ANCHOR_POINT2", IDC_EDIT_AXIS_ANCHOR_POINT2},
      {"IDC_EDIT_AXIS_ANCHOR_POINT3", IDC_EDIT_AXIS_ANCHOR_POINT3},
      {"IDC_EDIT_AXIS_ANCHOR_POINT4", IDC_EDIT_AXIS_ANCHOR_POINT4},
      {"IDC_EDIT_AXIS_ANCHOR_POINT5", IDC_EDIT_AXIS_ANCHOR_POINT5}
      // 可以继续添加其他控件名称和 ID
   };
   // 查找控件名称是否在映射中
   auto it = controlIdMap.find(strControlID);
   if (it != controlIdMap.end()) {
      return it->second;
   }
   return 0;
}
CFont* CAxisSettingsDlg::GetOrCreateFont(int nFontSize)
@@ -98,6 +149,7 @@
   // 根据控件高度动态调整字体大小
   int fontSize = nHeight / 2;
   if (fontSize < 8) fontSize = 8;
   if (fontSize > 24) fontSize = 24;  // 最大字体大小
   // 获取或创建字体
   CFont* pFont = GetOrCreateFont(fontSize);
@@ -106,6 +158,178 @@
   pWnd->Invalidate(); // 刷新控件显示
}
void CAxisSettingsDlg::AdjustLabelFont(CBLLabel& label)
{
   // 获取控件的矩形区域
   CRect rect;
   label.GetClientRect(&rect);
   // 动态计算字体大小,基于控件的高度
   int fontSize = rect.Height() / 2; // 控件高度的一半作为字体大小
   if (fontSize < 8) fontSize = 8;   // 最小字体大小
   if (fontSize > 30) fontSize = 30; // 最大字体大小
   // 设置字体大小
   label.SetFontSize(fontSize);
   // 刷新控件显示
   label.Invalidate();
   label.UpdateWindow();
}
void CAxisSettingsDlg::SetStatusColor(CBLLabel& label, BOOL bStatus)
{
   if (bStatus) {
      label.SetBkColor(RGB(0, 255, 0)); // 绿色
   }
   else {
      label.SetBkColor(RGB(255, 0, 0)); // 红色
   }
   label.Invalidate();              // 标记区域无效
   label.UpdateWindow();            // 立即刷新
}
int CAxisSettingsDlg::getCurrentSelectedAxisID()
{
   int currentIndex = m_comboAxisNO.GetCurSel();
   if (currentIndex == CB_ERR) {
      AfxMessageBox(_T("请选择一个有效的轴编号!"));
      return -1;
   }
   CString axisIDStr;
   m_comboAxisNO.GetLBText(currentIndex, axisIDStr);
   return _ttoi(axisIDStr);
}
void CAxisSettingsDlg::initializeAxisIDCombo()
{
   // 获取所有轴的轴NO
   auto axisNumbers = AxisManager::getInstance().getUsedAxisIds();
   // 清空下拉框
   m_comboAxisNO.ResetContent();
   // 填充数据到下拉框
   for (const auto& axisID : axisNumbers) {
      CString axisCString;
      axisCString.Format(_T("%d"), axisID);
      m_comboAxisNO.AddString(axisCString);
   }
   // 默认选择第一项
   if (m_comboAxisNO.GetCount() > 0) {
      m_comboAxisNO.SetCurSel(0);
   }
}
void CAxisSettingsDlg::refreshAxisDetails()
{
   // 获取当前选中的轴ID
   int axisId = getCurrentSelectedAxisID();
   // 获取轴信息
   auto axisDetails = AxisManager::getInstance().getAxis(axisId);
   if (axisDetails.empty()) {
      AfxMessageBox(_T("未找到轴信息!"));
      return;
   }
   // 格式化浮点数为 3 位小数的 CString
   auto formatDouble = [](const std::string& value) -> CString {
      char buffer[32];
      snprintf(buffer, sizeof(buffer), "%.3f", std::stod(value));
      return CString(buffer);
   };
   // 刷新界面控件数据
   m_staticAxisNO.SetWindowText(CString(axisDetails[1].c_str()));         // 轴NO
   m_staticAxisDescription.SetWindowText(CString(axisDetails[2].c_str())); // 轴描述
   m_staticStartAddress.SetWindowText(CString(axisDetails[3].c_str()));   // 起始地址
   m_editJogDistance.SetWindowText(formatDouble(axisDetails[4]));         // 微动量
   m_editManualSpeed.SetWindowText(formatDouble(axisDetails[5]));         // 手动速度
   m_editAutoSpeed.SetWindowText(formatDouble(axisDetails[8]));         // 自动速度
   m_editAccelerationTime.SetWindowText(formatDouble(axisDetails[11]));   // 加速时间
   m_editDecelerationTime.SetWindowText(formatDouble(axisDetails[12]));   // 减速时间
}
void CAxisSettingsDlg::refreshPositionDetails(int pageNumber)
{
   // 每页显示的定位点数量
   const int pageSize = 5;
   // 获取当前选中的轴ID
   int axisId = getCurrentSelectedAxisID();
   // 获取定位点数据
   auto positions = AxisManager::getInstance().getPositions(axisId, pageNumber, pageSize);
   for (int i = 0; i < pageSize; ++i) {
      // 动态构造控件名称
      CString descriptionName;
      CString positionName;
      // IDC_EDIT_AXIS_ANCHOR_POINT_DESCRIP1;
      // IDC_EDIT_AXIS_ANCHOR_POINT1;
      descriptionName.Format(_T("IDC_EDIT_AXIS_ANCHOR_POINT_DESCRIP%d"), i + 1);
      positionName.Format(_T("IDC_EDIT_AXIS_ANCHOR_POINT%d"), i + 1);
      // 获取控件指针
      UINT unDescription = FindIDByName(descriptionName);
      UINT unPosition = FindIDByName(positionName);
      TRACE("CAxisSettingsDlg::refreshPositionDetails %unDescription[%d], unPosition[%d]\n", unDescription, unPosition);
      if (unDescription == 0 || unPosition == 0) {
         continue;
      }
      CWnd* pDescription = GetDlgItem(unDescription);
      CWnd* pPosition = GetDlgItem(unPosition);
      if (i < positions.size()
         && !positions[i][2].empty()
         && !positions[i][3].empty()) {
         // 有数据,刷新描述和位置
         CString description = CString(positions[i][2].c_str()); // 定位点描述
         CString positionValue;
         // 保留3位小数
         char buffer[32];
         snprintf(buffer, sizeof(buffer), "%.3f", std::stod(positions[i][3]));
         positionValue = CString(buffer);
         if (pDescription) pDescription->SetWindowText(description);
         if (pPosition) pPosition->SetWindowText(positionValue);
      }
      else {
         // 无数据,清空控件内容
         if (pDescription) pDescription->SetWindowText(_T(""));
         if (pPosition) pPosition->SetWindowText(_T(""));
      }
   }
}
void CAxisSettingsDlg::updateAxisSelection(int offset)
{
   int currentIndex = m_comboAxisNO.GetCurSel();
   if (currentIndex == CB_ERR) {
      AfxMessageBox(_T("请选择一个有效的轴编号!"));
      return;
   }
   int newIndex = currentIndex + offset;
   if (newIndex < 0 || newIndex >= m_comboAxisNO.GetCount()) {
      CString error;
      error.Format(_T("已经到达%s一个轴!"), offset < 0 ? _T("上") : _T("下"));
      AfxMessageBox(error);
      return;
   }
   m_comboAxisNO.SetCurSel(newIndex);
   refreshAxisDetails();
   refreshPositionDetails(1);
}
BEGIN_MESSAGE_MAP(CAxisSettingsDlg, CDialogEx)
   ON_BN_CLICKED(IDC_BUTTON_AXIS_LAST, &CAxisSettingsDlg::OnBnClickedButtonAxisLast)
@@ -124,9 +348,11 @@
   ON_BN_CLICKED(IDC_BUTTON_AXIS_TEST_JOG_ADD, &CAxisSettingsDlg::OnBnClickedButtonAxisTestJogAdd)
   ON_BN_CLICKED(IDC_BUTTON_AXIS_TEST_JOG_SUB, &CAxisSettingsDlg::OnBnClickedButtonAxisTestJogSub)
   ON_BN_CLICKED(IDC_BUTTON_AXIS_TEST_STOP, &CAxisSettingsDlg::OnBnClickedButtonAxisTestStop)
   ON_CBN_SELCHANGE(IDC_COMBO_AXIS_NAME, &CAxisSettingsDlg::OnSelchangeComboAxisName)
   ON_WM_SIZE()
   ON_WM_CTLCOLOR()
   ON_WM_SIZING()
   ON_WM_TIMER()
END_MESSAGE_MAP()
@@ -138,6 +364,29 @@
   CDialogEx::OnInitDialog();
   // TODO:  在此添加额外的初始化
   SetWindowText(_T("Axis设定"));
   // 设置测试状态
   CBLLabel* pLabels[] = { &m_staticFLS, &m_staticDOG, &m_staticRLS, &m_staticReady, &m_staticBusy, &m_staticErr };
   for (auto pLabel : pLabels) {
      SetStatusColor(*pLabel, FALSE);
      pLabel->ModifyStyle(0, SS_NOTIFY);
      pLabel->SetTextColor(RGB(255, 255, 255));
      pLabel->SetAlignment(AlignCenter);
      pLabel->SetDynamicFont(TRUE);
   }
   try {
      initializeAxisIDCombo();
      refreshAxisDetails();
      refreshPositionDetails(1);
   }
   catch (const std::exception& ex) {
      CString errorMsg;
      errorMsg.Format(_T("初始化控件失败:%s"), CString(ex.what()));
      AfxMessageBox(errorMsg, MB_ICONERROR);
   }
   CRect screenRect, dlgRect, clientRect;
   GetClientRect(&clientRect);
   m_nInitialWidth = clientRect.Width();
@@ -178,6 +427,8 @@
   int centerX = screenRect.left + (screenRect.Width() - dlgWidth) / 2;
   int centerY = screenRect.top + (screenRect.Height() - dlgHeight) / 2;
   MoveWindow(centerX, centerY, dlgWidth, dlgHeight);
   SetTimer(TIMER_READ_PLC_DATA, 500, nullptr);
   return TRUE;  // return TRUE unless you set the focus to a control
   // 异常: OCX 属性页应返回 FALSE
@@ -188,11 +439,21 @@
   CDialogEx::OnSize(nType, cx, cy);
   // TODO: 在此处添加消息处理程序代码
   CRect rect;
   GetClientRect(&rect);
   if (nType == SIZE_MINIMIZED || m_mapCtrlLayouts.empty()) {
      return;
   }
   float dScaleX = static_cast<float>(cx) / m_nInitialWidth;
   float dScaleY = static_cast<float>(cy) / m_nInitialHeight;
   // 遍历对话框中的所有控件
   AdjustControls(rect.Width(), rect.Height());
   AdjustControls(dScaleX, dScaleY);
   // 动态调整各个 CBLLabel 的字体大小
   CBLLabel* pLabels[] = { &m_staticFLS, &m_staticDOG, &m_staticRLS, &m_staticReady, &m_staticBusy, &m_staticErr };
   for (auto pLabel : pLabels) {
      AdjustLabelFont(*pLabel);
   }
}
void CAxisSettingsDlg::OnSizing(UINT fwSide, LPRECT pRect)
@@ -222,36 +483,92 @@
void CAxisSettingsDlg::OnBnClickedButtonAxisLast()
{
   // TODO: 在此添加控件通知处理程序代码
   try {
      updateAxisSelection(-1);
   }
   catch (const std::exception& ex) {
      CString errorMsg;
      errorMsg.Format(_T("获取下一个轴失败:%s"), CString(ex.what()));
      AfxMessageBox(errorMsg, MB_ICONERROR);
   }
}
void CAxisSettingsDlg::OnBnClickedButtonAxisNext()
{
   // TODO: 在此添加控件通知处理程序代码
   try {
      updateAxisSelection(1);
   }
   catch (const std::exception& ex) {
      CString errorMsg;
      errorMsg.Format(_T("获取上一个轴失败:%s"), CString(ex.what()));
      AfxMessageBox(errorMsg, MB_ICONERROR);
   }
}
void CAxisSettingsDlg::OnBnClickedButtonAxisAnchorPointGroup1()
{
   // TODO: 在此添加控件通知处理程序代码
   try {
      refreshPositionDetails(1);
   }
   catch (const std::exception& ex) {
      CString errorMsg;
      errorMsg.Format(_T("刷新定位组1失败:%s"), CString(ex.what()));
      AfxMessageBox(errorMsg, MB_ICONERROR);
   }
}
void CAxisSettingsDlg::OnBnClickedButtonAxisAnchorPointGroup2()
{
   // TODO: 在此添加控件通知处理程序代码
   try {
      refreshPositionDetails(2);
   }
   catch (const std::exception& ex) {
      CString errorMsg;
      errorMsg.Format(_T("刷新定位组2失败:%s"), CString(ex.what()));
      AfxMessageBox(errorMsg, MB_ICONERROR);
   }
}
void CAxisSettingsDlg::OnBnClickedButtonAxisAnchorPointGroup3()
{
   // TODO: 在此添加控件通知处理程序代码
   try {
      refreshPositionDetails(3);
   }
   catch (const std::exception& ex) {
      CString errorMsg;
      errorMsg.Format(_T("刷新定位组3失败:%s"), CString(ex.what()));
      AfxMessageBox(errorMsg, MB_ICONERROR);
   }
}
void CAxisSettingsDlg::OnBnClickedButtonAxisAnchorPointGroup4()
{
   // TODO: 在此添加控件通知处理程序代码
   try {
      refreshPositionDetails(4);
   }
   catch (const std::exception& ex) {
      CString errorMsg;
      errorMsg.Format(_T("刷新定位组4失败:%s"), CString(ex.what()));
      AfxMessageBox(errorMsg, MB_ICONERROR);
   }
}
void CAxisSettingsDlg::OnBnClickedButtonAxisAnchorPointGroup5()
{
   // TODO: 在此添加控件通知处理程序代码
   try {
      refreshPositionDetails(5);
   }
   catch (const std::exception& ex) {
      CString errorMsg;
      errorMsg.Format(_T("刷新定位组5失败:%s"), CString(ex.what()));
      AfxMessageBox(errorMsg, MB_ICONERROR);
   }
}
void CAxisSettingsDlg::OnBnClickedButtonAxisAnchorPoint1()
@@ -297,4 +614,51 @@
void CAxisSettingsDlg::OnBnClickedButtonAxisTestStop()
{
   // TODO: 在此添加控件通知处理程序代码
}
}
void CAxisSettingsDlg::OnSelchangeComboAxisName()
{
   // TODO: 在此添加控件通知处理程序代码
   try {
      refreshAxisDetails();
      refreshPositionDetails(1);
   }
   catch (const std::exception& ex) {
      CString errorMsg;
      errorMsg.Format(_T("刷新控件失败:%s"), CString(ex.what()));
      AfxMessageBox(errorMsg, MB_ICONERROR);
   }
}
void CAxisSettingsDlg::OnTimer(UINT_PTR nIDEvent)
{
   if (TIMER_READ_PLC_DATA == nIDEvent) {
      ASSERT(m_pPLC);
      int addr1, addr2, readSize;
      addr1 = 5120;
      addr2 = 5425;
      readSize = (addr2 - addr1 + 1) * 2;
      auto funOnReadData = [&, addr1, readSize](IMcChannel* pChannel, int addr, char* pData, unsigned int nDataSize, int flag) -> void {
         if (nDataSize == readSize && flag == 0) {
            double fCurPos = CToolUnits::toInt32(pData) * 0.001;
            double fManualSpeed = CToolUnits::toInt32(&pData[(5422- addr1)*2]) * 0.001;
            double fAutoSpeed = CToolUnits::toInt32(&pData[(5424 - addr1) * 2]) * 0.001;
            double fPrm = CToolUnits::toInt32(&pData[(5150 - addr1) * 2]) * 0.1;
            int nLoad = CToolUnits::toInt16(&pData[(5154 - addr1) * 2]);
            int nErrCode = CToolUnits::toInt16(&pData[(5126 - addr1) * 2]);
            int nAlarmCode = CToolUnits::toInt16(&pData[(5127 - addr1) * 2]);
            CToolUnits::setDlgItemDouble(this, IDC_EDIT_AXIS_CURR_POS, fCurPos);
            CToolUnits::setDlgItemDouble(this, IDC_EDIT_AXIS_CURR_MANUAL_SPEED, fManualSpeed);
            CToolUnits::setDlgItemDouble(this, IDC_EDIT_AXIS_CURR_AUTO_SPEED, fAutoSpeed);
            CToolUnits::setDlgItemDouble(this, IDC_EDIT_AXIS_CURR_ROTA_SPEED, fPrm);
            SetDlgItemInt(IDC_EDIT_AXIS_CURR_LOAD, nLoad);
            SetDlgItemInt(IDC_EDIT_AXIS_CURR_ERROR_NUMBER, nErrCode);
            SetDlgItemInt(IDC_EDIT_AXIS_CURR_ALARM_NUMBER, nAlarmCode);
         }
      };
      m_pPLC->readData(MC::SOFT_COMPONENT::D, addr1, readSize, funOnReadData);
   }
   CDialogEx::OnTimer(nIDEvent);
}