直接上代码
#include <QDialog>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLineEdit>
class FileOpenDialog : public QDialog
{
Q_OBJECT
public:
explicit FileOpenDialog(QWidget *parent = nullptr);
private slots:
void openStpFile();
void openAedtFile();
private:
QPushButton *openStepButton{nullptr};
QPushButton *openAedtButton{nullptr};
QLineEdit * stepPathLine_ {nullptr};
QLineEdit * aedtPathLine_ {nullptr};
};
cpp 文件
#include <QFileDialog>
#include <QDebug>
#include <QLabel>
namespace {
const size_t STEP_LINE_LENGTH = 80;
const size_t AEDT_LINE_LENGTH = 80;
const size_t STEP_LABEL_LENGTH = 80;
const size_t AEDT_LABEL_LENGTH = 80;
}
FileOpenDialog::FileOpenDialog(QWidget *parent) :
QDialog(parent)
{
// 创建按钮
openStepButton = new QPushButton("Open .stp .step File", this);
openAedtButton = new QPushButton("Open .aedt File", this);
stepPathLine_ = new QLineEdit;
stepPathLine_->setFixedWidth(STEP_LINE_LENGTH);
aedtPathLine_ = new QLineEdit;
aedtPathLine_->setFixedWidth(AEDT_LINE_LENGTH);
QLabel * stepLabel = new QLabel("Open .stp .step File",this);
stepLabel->setMinimumWidth(STEP_LABEL_LENGTH);
QLabel * aedtLabel = new QLabel("Open .aedt File",this);
aedtLabel->setMinimumWidth(AEDT_LABEL_LENGTH);
// 创建布局
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QHBoxLayout *stpLayout = new QHBoxLayout();
QHBoxLayout *aedtLayout = new QHBoxLayout();
// 添加按钮到布局
stpLayout->addWidget(stepLabel);
stpLayout->addWidget(stepPathLine_);
stpLayout->addWidget(openStepButton);
aedtLayout->addWidget(aedtLabel);
aedtLayout->addWidget(aedtPathLine_);
aedtLayout->addWidget(openAedtButton);
mainLayout->addLayout(stpLayout);
mainLayout->addLayout(aedtLayout);
setLayout(mainLayout);
// 连接按钮点击事件到对应的槽函数
connect(openStepButton, &QPushButton::clicked, this, &FileOpenDialog::openStpFile); // 复用槽函数
connect(openAedtButton, &QPushButton::clicked, this, &FileOpenDialog::openAedtFile);
}
void FileOpenDialog::openStpFile()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open STP/STEP File"), "", tr("STP Files (*.stp);;STEP Files (*.step);"));
if (!fileName.isEmpty()) {
qDebug() << "Selected STP/STEP file:" << fileName;
// 使用 Open CASCADE 读取 .stp 文件
// STEPControl_Reader reader;
// if (reader.ReadFile(fileName.toStdString().c_str()) == IFSelect_RetDone) {
// reader.TransferRoots();
// TopoDS_Shape shape = reader.OneShape();
// // 遍历形状中的顶点并打印其坐标
// for (TopExp_Explorer explorer(shape, TopAbs_VERTEX); explorer.More(); explorer.Next()) {
// TopoDS_Vertex vertex = TopoDS::Vertex(explorer.Current());
// gp_Pnt point = BRep_Tool::Pnt(vertex);
// qDebug() << "Vertex at:" << point.X() << point.Y() << point.Z();
// }
}
}
void FileOpenDialog::openAedtFile()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open AEDT File"), "", tr("AEDT Files (*.aedt);"));
if (!fileName.isEmpty()) {
qDebug() << "Selected AEDT file:" << fileName;
// 使用 QProcess 调用 Python 脚本
// QProcess process;
// QString scriptPath = "path/to/open_aedt.py"; // 替换为你的脚本路径
// process.start("python", QStringList() << scriptPath << fileName);
// process.waitForFinished();
// QString output = process.readAllStandardOutput();
// qDebug() << "Python script output:" << output;
}
}