import sysfrom PyQt6.QtWidgets import ( QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QTextEdit, QLabel, QFileDialog, QProgressBar, QGroupBox, QMessageBox)from PyQt6.QtCore import Qt, QSettingsfrom PyQt6.QtGui import QFont, QPalette, QColor, QTextCursorimport timeimport osclass CADToPDFApp(QMainWindow): def __init__(self): super().__init__() self.cad_path = '' self.folder_path = '' # 初始化QSettings self.settings = QSettings("CADToPDF", "Settings") self.init_ui() self.load_settings() def init_ui(self): self.setWindowTitle("中望CAD模拟人工批量打印CAD图纸(欢迎关注微信公账号:码海听潮)") self.setFixedSize(800, 650) # 主窗口居中 screen = QApplication.primaryScreen().geometry() self.move( (screen.width() - self.width()) // 2, (screen.height() - self.height()) // 2 ) # 中央部件 central_widget = QWidget() self.setCentralWidget(central_widget) # 主布局 main_layout = QVBoxLayout(central_widget) main_layout.setSpacing(15) main_layout.setContentsMargins(25, 20, 25, 20) # 设置组 settings_group = QGroupBox(" 基本设置") settings_group.setStyleSheet(""" QGroupBox { font-weight: bold; border: 2px solid #3498db; border-radius: 8px; margin-top: 10px; padding-top: 25px; padding-bottom: 10px; background-color: white; } QGroupBox::title { subcontrol-origin: margin; left: 15px; padding: 0 8px 0 8px; color: #2980b9; font-size: 13px; } """) settings_layout = QVBoxLayout() settings_layout.setSpacing(12) # CAD路径设置 cad_layout = QHBoxLayout() cad_label = QLabel("中望CAD程序:") cad_label.setFixedWidth(80) cad_label.setStyleSheet("font-weight: normal; font-size: 12px;") self.cad_path_edit = QLabel(self.cad_path.replace('"', '')) self.cad_path_edit.setWordWrap(True) self.cad_path_edit.setStyleSheet(""" QLabel { padding: 8px; border: 1px solid #bdc3c7; border-radius: 5px; background-color: #f8f9fa; font-size: 11px; min-height: 20px; } """) cad_browse_btn = QPushButton("浏览") cad_browse_btn.setFixedWidth(90) cad_browse_btn.setStyleSheet(""" QPushButton { background-color: #3498db; color: white; border: none; border-radius: 5px; padding: 8px; font-size: 12px; } QPushButton:hover { background-color: #2980b9; } """) cad_browse_btn.clicked.connect(self.browse_cad_path) cad_layout.addWidget(cad_label) cad_layout.addWidget(self.cad_path_edit) cad_layout.addWidget(cad_browse_btn) settings_layout.addLayout(cad_layout) # 文件夹路径设置 folder_layout = QHBoxLayout() folder_label = QLabel("CAD图纸目录:") folder_label.setFixedWidth(80) folder_label.setStyleSheet("font-weight: normal; font-size: 12px;") self.folder_path_edit = QLabel(self.folder_path) self.folder_path_edit.setWordWrap(True) self.folder_path_edit.setStyleSheet(""" QLabel { padding: 8px; border: 1px solid #bdc3c7; border-radius: 5px; background-color: #f8f9fa; font-size: 11px; min-height: 20px; } """) folder_browse_btn = QPushButton("浏览") folder_browse_btn.setFixedWidth(90) folder_browse_btn.setStyleSheet(""" QPushButton { background-color: #3498db; color: white; border: none; border-radius: 5px; padding: 8px; font-size: 12px; } QPushButton:hover { background-color: #2980b9; } """) folder_browse_btn.clicked.connect(self.browse_folder_path) folder_layout.addWidget(folder_label) folder_layout.addWidget(self.folder_path_edit) folder_layout.addWidget(folder_browse_btn) settings_layout.addLayout(folder_layout) settings_group.setLayout(settings_layout) main_layout.addWidget(settings_group) # 按钮组 button_layout = QHBoxLayout() button_layout.setSpacing(15) self.start_btn = QPushButton("▶️ 开始打印") self.start_btn.setMinimumHeight(45) self.start_btn.setStyleSheet(""" QPushButton { background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #27ae60, stop:1 #229954); color: white; border: none; border-radius: 8px; font-weight: bold; font-size: 14px; padding: 10px; } QPushButton:hover { background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #2ecc71, stop:1 #27ae60); } QPushButton:pressed { background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #229954, stop:1 #1e8449); } QPushButton:disabled { background: #95a5a6; color: #ecf0f1; } """) self.start_btn.clicked.connect(self.start_conversion) self.clear_btn = QPushButton("🗑️ 清空日志") self.clear_btn.setMinimumHeight(45) self.clear_btn.setStyleSheet(""" QPushButton { background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #e67e22, stop:1 #d35400); color: white; border: none; border-radius: 8px; font-weight: bold; font-size: 14px; padding: 10px; } QPushButton:hover { background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #f39c12, stop:1 #e67e22); } QPushButton:pressed { background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #d35400, stop:1 #a04000); } """) self.clear_btn.clicked.connect(self.clear_log) self.stop_btn = QPushButton("⏹️ 停止") self.stop_btn.setMinimumHeight(45) self.stop_btn.setEnabled(False) self.stop_btn.setStyleSheet(""" QPushButton { background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #e74c3c, stop:1 #c0392b); color: white; border: none; border-radius: 8px; font-weight: bold; font-size: 14px; padding: 10px; } QPushButton:hover { background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #ec7063, stop:1 #e74c3c); } QPushButton:disabled { background: #95a5a6; color: #ecf0f1; } """) button_layout.addWidget(self.start_btn) button_layout.addWidget(self.clear_btn) button_layout.addWidget(self.stop_btn) main_layout.addLayout(button_layout) # 进度条 self.progress_bar = QProgressBar() self.progress_bar.setTextVisible(True) self.progress_bar.setStyleSheet(""" QProgressBar { border: 2px solid #bdc3c7; border-radius: 8px; text-align: center; font-weight: bold; height: 25px; background-color: #ecf0f1; } QProgressBar::chunk { background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #27ae60, stop:1 #2ecc71); border-radius: 6px; } """) main_layout.addWidget(self.progress_bar) # 日志组 log_group = QGroupBox("📝 处理日志") log_group.setStyleSheet(""" QGroupBox { font-weight: bold; border: 2px solid #8e44ad; border-radius: 8px; margin-top: 10px; padding-top: 25px; background-color: white; } QGroupBox::title { subcontrol-origin: margin; left: 15px; padding: 0 8px 0 8px; color: #8e44ad; font-size: 13px; } """) log_layout = QVBoxLayout() self.log_text = QTextEdit() self.log_text.setReadOnly(True) self.log_text.setStyleSheet(""" QTextEdit { border: 2px solid #bdc3c7; border-radius: 6px; background-color: #2c3e50; color: #ecf0f1; font-family: 'Consolas', 'Courier New', monospace; font-size: 11px; padding: 10px; line-height: 1.4; } QScrollBar:vertical { border: none; background: #34495e; width: 10px; border-radius: 5px; } QScrollBar::handle:vertical { background: #7f8c8d; border-radius: 5px; min-height: 20px; } QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical { height: 0px; } """) log_layout.addWidget(self.log_text) log_group.setLayout(log_layout) main_layout.addWidget(log_group) # 底部状态栏 self.status_bar = self.statusBar() self.status_bar.showMessage("✅ 程序就绪 | 请设置路径后点击开始转换") self.status_bar.setStyleSheet(""" QStatusBar { color: #2c3e50; font-size: 11px; padding: 5px; background-color: #ecf0f1; border-top: 1px solid #bdc3c7; } """) def load_settings(self): """从QSettings加载保存的设置""" saved_cad_path = self.settings.value("cad_path", "") if saved_cad_path and os.path.exists(saved_cad_path): self.cad_path = f'"{saved_cad_path}"' self.cad_path_edit.setText(saved_cad_path) saved_folder_path = self.settings.value("folder_path", "") if saved_folder_path and os.path.exists(saved_folder_path): self.folder_path = saved_folder_path self.folder_path_edit.setText(saved_folder_path) def save_settings(self): """保存设置到QSettings""" cad_path_value = self.cad_path.replace('"', '') if cad_path_value: self.settings.setValue("cad_path", cad_path_value) if self.folder_path: self.settings.setValue("folder_path", self.folder_path) def browse_cad_path(self): file_path, _ = QFileDialog.getOpenFileName( self, "选择中望CAD执行文件", os.path.dirname(self.cad_path.replace('"', '')), "可执行文件 (*.exe);;所有文件 (*.*)" ) if file_path: self.cad_path = f'"{file_path}"' self.cad_path_edit.setText(file_path) self.save_settings() self.log_message(f"已设置CAD路径: {file_path}") def browse_folder_path(self): folder_path = QFileDialog.getExistingDirectory( self, "选择CAD图纸文件夹", self.folder_path ) if folder_path: self.folder_path = os.path.normpath(folder_path) self.folder_path_edit.setText(folder_path) self.save_settings() self.log_message(f"已设置图纸目录: {folder_path}") def log_message(self, message): timestamp = time.strftime("%H:%M:%S") self.log_text.append(f"[{timestamp}] {message}") cursor = self.log_text.textCursor() cursor.movePosition(QTextCursor.MoveOperation.End) self.log_text.setTextCursor(cursor) def start_conversion(self): """开始转换(仅界面演示)""" if not os.path.exists(self.cad_path.replace('"', '')): QMessageBox.warning(self, "警告", "CAD程序路径不存在!") return if not os.path.exists(self.folder_path): QMessageBox.warning(self, "警告", "图纸目录不存在!") return self.save_settings() self.log_message("开始转换...") self.log_message("【演示模式】实际转换逻辑已移除") def clear_log(self): self.log_text.clear() self.status_bar.showMessage("✅ 日志已清空") def closeEvent(self, event): self.save_settings() event.accept()def main(): app = QApplication(sys.argv) app.setOrganizationName("CADToPDF") app.setApplicationName("CADToPDF") app.setStyle('Fusion') palette = QPalette() palette.setColor(QPalette.ColorRole.Window, QColor(240, 242, 245)) palette.setColor(QPalette.ColorRole.WindowText, QColor(44, 62, 80)) palette.setColor(QPalette.ColorRole.Base, QColor(255, 255, 255)) palette.setColor(QPalette.ColorRole.AlternateBase, QColor(245, 245, 245)) palette.setColor(QPalette.ColorRole.ToolTipBase, QColor(255, 255, 255)) palette.setColor(QPalette.ColorRole.ToolTipText, QColor(44, 62, 80)) palette.setColor(QPalette.ColorRole.Text, QColor(44, 62, 80)) palette.setColor(QPalette.ColorRole.Button, QColor(240, 242, 245)) palette.setColor(QPalette.ColorRole.ButtonText, QColor(44, 62, 80)) palette.setColor(QPalette.ColorRole.Highlight, QColor(52, 152, 219)) palette.setColor(QPalette.ColorRole.HighlightedText, QColor(255, 255, 255)) app.setPalette(palette) font = QFont("Microsoft YaHei", 9) app.setFont(font) window = CADToPDFApp() window.show() sys.exit(app.exec())if __name__ == "__main__": main()