From 2de3857d1cb894992acad2fadb07cf989023748e Mon Sep 17 00:00:00 2001 From: pengqi Date: Thu, 8 Jan 2026 15:36:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=A4=E7=A7=8D=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E7=A7=8D=E7=B1=BB=E7=9A=843D=E7=BA=BF=E6=9D=A1?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E6=95=88=E6=9E=9C+=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E7=95=8C=E9=9D=A2bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UI/test.py | 78 ++++++++++++++++++------------------- UI/ui.py | 78 ++++++++++++++++++------------------- UI/ui_3d.py | 108 ++++++++++++++++++++++++++++------------------------ 3 files changed, 137 insertions(+), 127 deletions(-) diff --git a/UI/test.py b/UI/test.py index 9e2ed6f..d5b841a 100644 --- a/UI/test.py +++ b/UI/test.py @@ -19,7 +19,6 @@ from ui_3d import Target3DWidgetA, Target3DWidgetB class LengthMotorController(QMainWindow): """线条长度标记与电机位置控制系统主界面""" - update_time_signal = Signal(str) record_data_signal = Signal(float, float, float, float) line_select_signal = Signal(str, float, float, float, float) @@ -241,6 +240,7 @@ class LengthMotorController(QMainWindow): self.time_label = QLabel() self.time_label.setAlignment(Qt.AlignCenter) self.time_label.setFont(QFont("Microsoft YaHei", 14, QFont.Bold)) + self._update_current_time() # 手动触发一次,避免延迟 time_layout.addWidget(self.time_label) right_v_layout.addWidget(time_group) @@ -390,39 +390,56 @@ class LengthMotorController(QMainWindow): display_layout = QVBoxLayout(display_group) display_layout.setContentsMargins(20, 20, 20, 20) - # 实例化ui_3d.py中的Target3DWidget - self.ui_3d_widget = Target3DWidgetA() # 创建3D绘图控件实例 - self.ui_3d_widget.setMaximumSize(500, 330) + # 创建3D控件的容器 + self.ui_3d_container = QWidget() # 空容器,专门放3D控件 + self.ui_3d_container_layout = QVBoxLayout(self.ui_3d_container) + self.ui_3d_container_layout.setContentsMargins(0, 0, 0, 0) + self.ui_3d_container.setFixedSize(500, 330) - test_btn = QPushButton("测试参数传递") - test_btn.setFont(self.font2) - test_btn.setFixedWidth(150) - test_btn.clicked.connect(self._test_3d_param_pass) + # 初始化默认显示A控件 + self.current_3d_widget = Target3DWidgetA() + self.ui_3d_container_layout.addWidget(self.current_3d_widget) - display_layout.addWidget(self.ui_3d_widget) - display_layout.addWidget(test_btn, alignment=Qt.AlignCenter) + display_layout.addWidget(self.ui_3d_container) parent_layout.addWidget(display_group) + def _switch_3d_widget(self, widget_type): + """ + 动态切换3D控件 + :param widget_type: "A"或"B" + """ + # 移除旧的3D控件 + if self.current_3d_widget: + self.current_3d_widget.setParent(None) # 从布局中移除 + self.current_3d_widget.deleteLater() # 释放资源 + + # 创建新的3D控件 + if widget_type == "A": + self.current_3d_widget = Target3DWidgetA() + else: + self.current_3d_widget = Target3DWidgetB() + + # 添加新控件到容器 + self.ui_3d_container_layout.addWidget(self.current_3d_widget) + def _init_timer(self): """初始化定时器""" self.timer = QTimer(self) self.timer.timeout.connect(self._update_current_time) self.timer.start(1000) - self._update_current_time() def _connect_signals(self): """连接信号与槽函数""" self.save_btn.clicked.connect(self._save_settings) self.clear_btn.clicked.connect(self._clear_inputs) self.export_btn.clicked.connect(self._export_records) - self.update_time_signal.connect(self.time_label.setText) self.record_data_signal.connect(self._log_param_change) self.line_select_signal.connect(self._set_line_params) def _update_current_time(self): """更新当前时间""" current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) - self.update_time_signal.emit(current_time) + self.time_label.setText(current_time) @Slot(str) def _import_line_param(self, line_type): @@ -431,13 +448,17 @@ class LengthMotorController(QMainWindow): selected_type = self.selected_line_a params = self.line_a_params[selected_type] line_name = f"线条A-{selected_type}" + # 切换为A控件 + self._switch_3d_widget("A") else: selected_type = self.selected_line_b params = self.line_b_params[selected_type] line_name = f"线条B-{selected_type}" + # 切换为B控件 + self._switch_3d_widget("B") self.line_select_signal.emit(line_name, *params) - QMessageBox.information(self, "导入成功", f"已导入{line_name}的参数") + QMessageBox.information(self, "导入成功", f"已导入{line_name}的参数\n3D视图已切换为{line_type}类型") @Slot(str, float, float, float, float) def _set_line_params(self, line_name, a, b, c, d): @@ -449,10 +470,10 @@ class LengthMotorController(QMainWindow): d_str = f"{d:.0f}" # 参数d是整数(25/26/28等) """设置参数到下拉框""" - self.combo_a.setCurrentText(str(a)) - self.combo_b.setCurrentText(str(b)) - self.combo_c.setCurrentText(str(c)) - self.combo_d.setCurrentText(str(d)) + self.combo_a.setCurrentText(a_str) + self.combo_b.setCurrentText(b_str) + self.combo_c.setCurrentText(c_str) + self.combo_d.setCurrentText(d_str) @Slot() def _save_settings(self): @@ -482,27 +503,6 @@ class LengthMotorController(QMainWindow): "time": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) }) - @Slot() - def _test_3d_param_pass(self): - """测试参数传递""" - try: - param_a = float(self.combo_a.currentText()) - param_b = float(self.combo_b.currentText()) - param_c = float(self.combo_c.currentText()) - param_d = float(self.combo_d.currentText()) - except ValueError: - QMessageBox.critical(self, "测试失败", "当前参数格式无效!") - return - - QMessageBox.information( - self, "参数传递测试成功", - f"当前参数已准备好传递给3D模块:\n" - f"参数a(半径):{param_a}\n" - f"参数b(长度):{param_b}\n" - f"参数c(颜色通道1):{param_c}\n" - f"参数d(颜色通道2):{param_d}" - ) - @Slot() def _clear_inputs(self): """清空所有选择""" diff --git a/UI/ui.py b/UI/ui.py index 10c6e43..e1fc97a 100644 --- a/UI/ui.py +++ b/UI/ui.py @@ -19,7 +19,6 @@ from ui_3d import Target3DWidgetA, Target3DWidgetB class LengthMotorController(QMainWindow): """线条长度标记与电机位置控制系统主界面""" - update_time_signal = Signal(str) record_data_signal = Signal(float, float, float, float) line_select_signal = Signal(str, float, float, float, float) @@ -241,6 +240,7 @@ class LengthMotorController(QMainWindow): self.time_label = QLabel() self.time_label.setAlignment(Qt.AlignCenter) self.time_label.setFont(QFont("Microsoft YaHei", 14, QFont.Bold)) + self._update_current_time() # 手动触发一次,避免延迟 time_layout.addWidget(self.time_label) right_v_layout.addWidget(time_group) @@ -390,39 +390,56 @@ class LengthMotorController(QMainWindow): display_layout = QVBoxLayout(display_group) display_layout.setContentsMargins(20, 20, 20, 20) - # 实例化ui_3d.py中的Target3DWidget - self.ui_3d_widget = Target3DWidgetA() # 创建3D绘图控件实例 - self.ui_3d_widget.setMaximumSize(500, 330) + # 创建3D控件的容器 + self.ui_3d_container = QWidget() # 空容器,专门放3D控件 + self.ui_3d_container_layout = QVBoxLayout(self.ui_3d_container) + self.ui_3d_container_layout.setContentsMargins(0, 0, 0, 0) + self.ui_3d_container.setFixedSize(500, 330) - test_btn = QPushButton("测试参数传递") - test_btn.setFont(self.font2) - test_btn.setFixedWidth(150) - test_btn.clicked.connect(self._test_3d_param_pass) + # 初始化默认显示A控件 + self.current_3d_widget = Target3DWidgetA() + self.ui_3d_container_layout.addWidget(self.current_3d_widget) - display_layout.addWidget(self.ui_3d_widget) - display_layout.addWidget(test_btn, alignment=Qt.AlignCenter) + display_layout.addWidget(self.ui_3d_container) parent_layout.addWidget(display_group) + def _switch_3d_widget(self, widget_type): + """ + 动态切换3D控件 + :param widget_type: "A"或"B" + """ + # 移除旧的3D控件 + if self.current_3d_widget: + self.current_3d_widget.setParent(None) # 从布局中移除 + self.current_3d_widget.deleteLater() # 释放资源 + + # 创建新的3D控件 + if widget_type == "A": + self.current_3d_widget = Target3DWidgetA() + else: + self.current_3d_widget = Target3DWidgetB() + + # 添加新控件到容器 + self.ui_3d_container_layout.addWidget(self.current_3d_widget) + def _init_timer(self): """初始化定时器""" self.timer = QTimer(self) self.timer.timeout.connect(self._update_current_time) self.timer.start(1000) - self._update_current_time() def _connect_signals(self): """连接信号与槽函数""" self.save_btn.clicked.connect(self._save_settings) self.clear_btn.clicked.connect(self._clear_inputs) self.export_btn.clicked.connect(self._export_records) - self.update_time_signal.connect(self.time_label.setText) self.record_data_signal.connect(self._log_param_change) self.line_select_signal.connect(self._set_line_params) def _update_current_time(self): """更新当前时间""" current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) - self.update_time_signal.emit(current_time) + self.time_label.setText(current_time) @Slot(str) def _import_line_param(self, line_type): @@ -431,13 +448,17 @@ class LengthMotorController(QMainWindow): selected_type = self.selected_line_a params = self.line_a_params[selected_type] line_name = f"线条A-{selected_type}" + # 切换为A控件 + self._switch_3d_widget("A") else: selected_type = self.selected_line_b params = self.line_b_params[selected_type] line_name = f"线条B-{selected_type}" + # 切换为B控件 + self._switch_3d_widget("B") self.line_select_signal.emit(line_name, *params) - QMessageBox.information(self, "导入成功", f"已导入{line_name}的参数") + QMessageBox.information(self, "导入成功", f"已导入{line_name}的参数\n3D视图已切换为{line_type}类型") @Slot(str, float, float, float, float) def _set_line_params(self, line_name, a, b, c, d): @@ -449,10 +470,10 @@ class LengthMotorController(QMainWindow): d_str = f"{d:.0f}" # 参数d是整数(25/26/28等) """设置参数到下拉框""" - self.combo_a.setCurrentText(str(a)) - self.combo_b.setCurrentText(str(b)) - self.combo_c.setCurrentText(str(c)) - self.combo_d.setCurrentText(str(d)) + self.combo_a.setCurrentText(a_str) + self.combo_b.setCurrentText(b_str) + self.combo_c.setCurrentText(c_str) + self.combo_d.setCurrentText(d_str) @Slot() def _save_settings(self): @@ -482,27 +503,6 @@ class LengthMotorController(QMainWindow): "time": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) }) - @Slot() - def _test_3d_param_pass(self): - """测试参数传递""" - try: - param_a = float(self.combo_a.currentText()) - param_b = float(self.combo_b.currentText()) - param_c = float(self.combo_c.currentText()) - param_d = float(self.combo_d.currentText()) - except ValueError: - QMessageBox.critical(self, "测试失败", "当前参数格式无效!") - return - - QMessageBox.information( - self, "参数传递测试成功", - f"当前参数已准备好传递给3D模块:\n" - f"参数a(半径):{param_a}\n" - f"参数b(长度):{param_b}\n" - f"参数c(颜色通道1):{param_c}\n" - f"参数d(颜色通道2):{param_d}" - ) - @Slot() def _clear_inputs(self): """清空所有选择""" diff --git a/UI/ui_3d.py b/UI/ui_3d.py index ffc6b6e..ebcade9 100644 --- a/UI/ui_3d.py +++ b/UI/ui_3d.py @@ -35,16 +35,21 @@ class Target3DWidgetA(QWidget): self.ax.set_ylabel('Y') self.ax.set_zlabel('Z') - # 1. 轴范围(X轴设为0-60cm,Y/Z设为0-5cm) - self.ax.set_xlim(0, 60) + # 1. 清空刻度数值(隐藏刻度数字) + self.ax.set_xticklabels([]) + self.ax.set_yticklabels([]) + self.ax.set_zticklabels([]) + + # 2. 隐藏刻度线(针对3D轴的特殊设置) + self.ax.tick_params(axis='x', which='both', length=0) # X轴刻度线长度设为0 + self.ax.tick_params(axis='y', which='both', length=0) # Y轴刻度线长度设为0 + self.ax.tick_params(axis='z', which='both', length=0) # Z轴刻度线长度设为0 + + # 轴范围保留(保证3D图形显示范围正确) + self.ax.set_xlim(0, 120) self.ax.set_ylim(0, 5) self.ax.set_zlim(0, 5) - # 2. 刻度间隔统一为1cm - self.ax.set_xticks(range(0, 61, 5)) # X轴每5cm标一次刻度(避免太密集) - self.ax.set_yticks(range(0, 6, 1)) # Y轴每1cm标一次刻度 - self.ax.set_zticks(range(0, 6, 1)) # Z轴每1cm标一次刻度 - # 调整视角(适配X轴1cm刻度) self.ax.view_init(elev=20, azim=60) @@ -52,27 +57,27 @@ class Target3DWidgetA(QWidget): """绘制3D结构""" vertices = [ # 底部截面(Z=0平面) - (0, 3, 0), (50, 3, 0), - (50, 3, 0), (50, 0, 0), + (0, 3, 0), (100, 3, 0), + (100, 3, 0), (100, 0, 0), # 中间截面(Z=0.5平面) (0, 0.5, 0.5), (0, 3, 0.5), - (0, 3, 0.5), (50, 3, 0.5), - (50, 3, 0.5), (50, 0.5, 0.5), - (50, 0.5, 0.5), (0, 0.5, 0.5), + (0, 3, 0.5), (100, 3, 0.5), + (100, 3, 0.5), (100, 0.5, 0.5), + (100, 0.5, 0.5), (0, 0.5, 0.5), # 顶部界面(Z=2平面) (0, 0, 2), (0, 0.5, 2), - (0, 0.5, 2), (50, 0.5, 2), - (50, 0.5, 2), (50, 0, 2), - (50, 0, 2), (0, 0, 2), + (0, 0.5, 2), (100, 0.5, 2), + (100, 0.5, 2), (100, 0, 2), + (100, 0, 2), (0, 0, 2), # z方向的连线 (0, 3, 0.5), (0, 3, 0), - (50, 3, 0.5), (50, 3, 0), + (100, 3, 0.5), (100, 3, 0), (0, 0.5, 2), (0, 0.5, 0.5), - (50, 0.5, 2), (50, 0.5, 0.5), - (50, 0, 2), (50, 0, 0), + (100, 0.5, 2), (100, 0.5, 0.5), + (100, 0, 2), (100, 0, 0), ] # 绘制所有棱边 @@ -85,10 +90,10 @@ class Target3DWidgetA(QWidget): def add_annotations(self): """标注位置(适配X轴1cm单位)""" annotations = [ - ("a", 50, 0, 1, 'red'), - ("b", 50, 1, 0, 'green'), - ("c", 50, 3, 0.25, 'blue'), - ("d", 25, 2, 0, 'magenta') + ("a", 100, 0, 1, 'red'), + ("b", 100, 1, 0, 'green'), + ("c", 100, 3, 0.25, 'blue'), + ("d", 50, 2, 0, 'magenta') ] for text, x, y, z, color in annotations: self.ax.text(x, y, z, text, fontsize=14, color=color, weight='bold') @@ -117,16 +122,21 @@ class Target3DWidgetB(QWidget): self.ax.set_ylabel('Y') self.ax.set_zlabel('Z') - # 1. 轴范围(X轴设为0-60cm,Y/Z设为0-5cm) - self.ax.set_xlim(0, 60) + # 1. 清空刻度数值(隐藏刻度数字) + self.ax.set_xticklabels([]) + self.ax.set_yticklabels([]) + self.ax.set_zticklabels([]) + + # 2. 隐藏刻度线(针对3D轴的特殊设置) + self.ax.tick_params(axis='x', which='both', length=0) # X轴刻度线长度设为0 + self.ax.tick_params(axis='y', which='both', length=0) # Y轴刻度线长度设为0 + self.ax.tick_params(axis='z', which='both', length=0) # Z轴刻度线长度设为0 + + # 轴范围保留(保证3D图形显示范围正确) + self.ax.set_xlim(0, 120) self.ax.set_ylim(0, 5) self.ax.set_zlim(0, 5) - # 2. 刻度间隔统一为1cm - self.ax.set_xticks(range(0, 61, 5)) # X轴每5cm标一次刻度(避免太密集) - self.ax.set_yticks(range(0, 6, 1)) # Y轴每1cm标一次刻度 - self.ax.set_zticks(range(0, 6, 1)) # Z轴每1cm标一次刻度 - # 调整视角(适配X轴1cm刻度) self.ax.view_init(elev=20, azim=60) @@ -134,34 +144,34 @@ class Target3DWidgetB(QWidget): """绘制3D结构""" vertices = [ # 底部截面(Z=0平面) - (0, 3, 0), (50, 3, 0), - (50, 3, 0), (50, 0, 0), + (0, 3, 0), (100, 3, 0), + (100, 3, 0), (100, 0, 0), # 中间截面(Z=0.5平面) (0, 0.5, 0.5), (0, 2.5, 0.5), - (50, 2.5, 0.5), (50, 0.5, 0.5), - (50, 0.5, 0.5), (0, 0.5, 0.5), + (100, 2.5, 0.5), (100, 0.5, 0.5), + (100, 0.5, 0.5), (0, 0.5, 0.5), # 中间截面(Z=1平面) (0, 2.5, 1),(0, 3, 1), - (0, 3, 1), (50, 3, 1), - (50, 3, 1), (50, 2.5, 1), - (50, 2.5, 1), (0, 2.5, 1), + (0, 3, 1), (100, 3, 1), + (100, 3, 1), (100, 2.5, 1), + (100, 2.5, 1), (0, 2.5, 1), # 顶部界面(Z=2平面) (0, 0, 2), (0, 0.5, 2), - (0, 0.5, 2), (50, 0.5, 2), - (50, 0.5, 2), (50, 0, 2), - (50, 0, 2), (0, 0, 2), + (0, 0.5, 2), (100, 0.5, 2), + (100, 0.5, 2), (100, 0, 2), + (100, 0, 2), (0, 0, 2), # z方向的连线 (0, 3, 1), (0, 3, 0), - (50, 3, 1), (50, 3, 0), + (100, 3, 1), (100, 3, 0), (0, 0.5, 2), (0, 0.5, 0.5), - (50, 0.5, 2), (50, 0.5, 0.5), - (50, 0, 2), (50, 0, 0), + (100, 0.5, 2), (100, 0.5, 0.5), + (100, 0, 2), (100, 0, 0), (0, 2.5, 1),(0, 2.5, 0.5), - (50, 2.5, 1),(50, 2.5, 0.5) + (100, 2.5, 1),(100, 2.5, 0.5) ] # 绘制所有棱边 @@ -174,10 +184,10 @@ class Target3DWidgetB(QWidget): def add_annotations(self): """标注位置(适配X轴1cm单位)""" annotations = [ - ("a", 50, 0, 1, 'red'), - ("b", 50, 1, 0, 'green'), - ("c", 50, 3, 0.5, 'blue'), - ("d", 25, 2, 0, 'magenta') + ("a", 100, 0, 1, 'red'), + ("b", 100, 1, 0, 'green'), + ("c", 100, 3, 0.5, 'blue'), + ("d", 50, 2, 0, 'magenta') ] for text, x, y, z, color in annotations: self.ax.text(x, y, z, text, fontsize=14, color=color, weight='bold') @@ -211,6 +221,6 @@ def ui_3d_b(): # ----------测试接口---------- if __name__ == '__main__': - ui_3d_a() - # ui_3d_b() + # ui_3d_a() + ui_3d_b()