main_simple.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # main_simple.py - 使用QOpenGLWidget的新主程序
  2. import sys
  3. import os
  4. from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QHBoxLayout, QWidget, QPushButton, QFileDialog
  5. from PyQt5.QtCore import Qt
  6. from OpenGL.GLUT import glutInit
  7. from glWidget_simple import Simple3DWidget
  8. from handle_simple import Dataload
  9. class MainWindow(QMainWindow):
  10. def __init__(self):
  11. super().__init__()
  12. self.setWindowTitle('3D模型查看器')
  13. self.setGeometry(100, 100, 1200, 800)
  14. # 创建中央部件
  15. central_widget = QWidget()
  16. self.setCentralWidget(central_widget)
  17. # 创建布局
  18. layout = QHBoxLayout()
  19. central_widget.setLayout(layout)
  20. # 创建3D渲染器
  21. self.gl_widget = Simple3DWidget()
  22. # 创建控制面板
  23. control_panel = QWidget()
  24. control_panel.setFixedWidth(200)
  25. control_layout = QVBoxLayout()
  26. control_panel.setLayout(control_layout)
  27. # 创建按钮
  28. self.open_button = QPushButton("打开STL文件")
  29. self.switch_button = QPushButton("切换显示模式")
  30. self.axes_button = QPushButton("坐标系显示")
  31. self.open_button.clicked.connect(self.open_file)
  32. self.switch_button.clicked.connect(self.gl_widget.toggle_display_mode)
  33. self.axes_button.clicked.connect(self.gl_widget.toggle_axes_display_mode)
  34. control_layout.addWidget(self.open_button)
  35. control_layout.addWidget(self.switch_button)
  36. control_layout.addWidget(self.axes_button)
  37. # 添加一些空间
  38. control_layout.addStretch()
  39. # 添加到主布局
  40. layout.addWidget(control_panel)
  41. layout.addWidget(self.gl_widget)
  42. # 连接信号
  43. self.gl_widget.pointPicked.connect(self.on_point_picked)
  44. # 创建数据加载器
  45. self.dataload = Dataload()
  46. def open_file(self):
  47. """打开文件对话框,选择 3D 模型文件并根据类型调用对应加载函数"""
  48. # 设置文件过滤器
  49. filter = (
  50. "3D Model Files (*.stp *.step *.stl);;"
  51. "STL Files (*.stl);;"
  52. "All Files (*)"
  53. )
  54. file_path, _ = QFileDialog.getOpenFileName(
  55. self,
  56. "选择 3D 模型文件",
  57. "", # 初始路径,可改为上次打开的目录
  58. filter
  59. )
  60. if not file_path:
  61. print("用户取消选择")
  62. return
  63. if not os.path.exists(file_path):
  64. print(f"❌ 文件不存在: {file_path}")
  65. return
  66. # 获取文件扩展名(转小写,便于判断)
  67. ext = os.path.splitext(file_path)[1].lower()
  68. print(f"选中的文件: {file_path}")
  69. print(f"文件扩展名: {ext}")
  70. # 根据扩展名分发到不同加载函数
  71. if ext in ['.stp', '.step']:
  72. self.load_step_file(file_path) # 加载 STEP 文件
  73. elif ext == '.stl':
  74. self.load_stl_file(file_path) # 加载 STL 文件
  75. else:
  76. print(f"❌ 不支持的文件格式: {ext}")
  77. # 可选:弹出警告框
  78. from PyQt5.QtWidgets import QMessageBox
  79. QMessageBox.warning(self, "不支持的格式", f"无法加载文件类型: {ext}")
  80. def load_stl_file(self, file_path):
  81. """加载STL文件"""
  82. print(f"📁 正在加载: {file_path}")
  83. # 使用Dataload类加载数据
  84. vertices, colors,triangles, normals = self.dataload.load_data_from_file(file_path)
  85. if len(vertices) == 0:
  86. print("❌ 加载失败")
  87. return
  88. # 设置数据到渲染器
  89. self.gl_widget.set_data(vertices, colors,triangles, normals)
  90. print("✅ 数据已设置到渲染器")
  91. def load_step_file(self, file_path):
  92. """加载STL文件"""
  93. print(f"📁 正在加载: {file_path}")
  94. # 使用Dataload类加载数据
  95. vertices, colors, triangles, normals = self.dataload.load_step_file(file_path)
  96. if len(vertices) == 0:
  97. print("❌ 加载失败")
  98. return
  99. # 设置数据到渲染器
  100. self.gl_widget.set_data(vertices, colors, triangles, normals)
  101. print("✅ 数据已设置到渲染器")
  102. def on_point_picked(self, x, y, z):
  103. """点拾取回调"""
  104. print(f"✅ 拾取到点: ({x:.3f}, {y:.3f}, {z:.3f})")
  105. def main():
  106. print("当前工作目录:", os.getcwd())
  107. app = QApplication(sys.argv)
  108. # 初始化 GLUT(必须在创建任何 OpenGL 上下文前调用)
  109. glutInit(sys.argv)
  110. window = MainWindow()
  111. window.show()
  112. print("✅ 3D模型查看器启动成功")
  113. print("点击'打开STL文件'按钮来加载模型")
  114. return app.exec_()
  115. if __name__ == "__main__":
  116. main()