FreeCAD

Форк
0
/
DlgProjectUtility.cpp 
125 строк · 4.9 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
#include <sstream>
26
#include <QDir>
27
#include <QMessageBox>
28
#endif
29

30
#include <App/Document.h>
31

32
#include "DlgProjectUtility.h"
33
#include "ui_DlgProjectUtility.h"
34
#include "Application.h"
35
#include "Command.h"
36

37

38
using namespace Gui::Dialog;
39

40

41
/* TRANSLATOR Gui::Dialog::DlgProjectUtility */
42

43
DlgProjectUtility::DlgProjectUtility(QWidget* parent, Qt::WindowFlags fl)
44
  : QDialog(parent, fl)
45
  , ui(new Ui_DlgProjectUtility)
46
{
47
    ui->setupUi(this);
48
    connect(ui->extractButton, &QPushButton::clicked, this, &DlgProjectUtility::extractButton);
49
    connect(ui->createButton, &QPushButton::clicked, this, &DlgProjectUtility::createButton);
50
    ui->extractSource->setFilter(QString::fromLatin1("%1 (*.FCStd)").arg(tr("Project file")));
51
}
52

53
/**
54
 *  Destroys the object and frees any allocated resources
55
 */
56
DlgProjectUtility::~DlgProjectUtility() = default;
57

58
void DlgProjectUtility::extractButton()
59
{
60
    QString source = ui->extractSource->fileName();
61
    QString dest = ui->extractDest->fileName();
62
    if (source.isEmpty()) {
63
        QMessageBox::critical(this, tr("Empty source"), tr("No source is defined."));
64
        return;
65
    }
66

67
    if (dest.isEmpty()) {
68
        QMessageBox::critical(this, tr("Empty destination"), tr("No destination is defined."));
69
        return;
70
    }
71

72
    tryExtractArchive(source, dest);
73
}
74

75
void DlgProjectUtility::createButton()
76
{
77
    QString source = ui->createSource->fileName();
78
    QString dest = ui->createDest->fileName();
79
    if (source.isEmpty()) {
80
        QMessageBox::critical(this, tr("Empty source"), tr("No source is defined."));
81
        return;
82
    }
83
    if (dest.isEmpty()) {
84
        QMessageBox::critical(this, tr("Empty destination"), tr("No destination is defined."));
85
        return;
86
    }
87

88
    dest = QDir(dest).absoluteFilePath(QString::fromUtf8("project.fcstd"));
89

90
    bool openFile = ui->checkLoadProject->isChecked();
91
    tryCreateArchive(source, dest, openFile);
92
}
93

94
void DlgProjectUtility::tryExtractArchive(const QString& source, const QString& target)
95
{
96
    try {
97
        std::stringstream str;
98
        str << "from freecad import project_utility\n";
99
        str << "project_utility.extractDocument(\"" << (const char*)source.toUtf8()
100
            << "\", \"" << (const char*)target.toUtf8() << "\")";
101
        Gui::Command::runCommand(Gui::Command::App, str.str().c_str());
102
    }
103
    catch (const Base::Exception& e) {
104
        QMessageBox::critical(this, tr("Failed to extract document"), QString::fromLatin1(e.what()));
105
    }
106
}
107

108
void DlgProjectUtility::tryCreateArchive(const QString& source, const QString& target, bool openFile)
109
{
110
    try {
111
        std::stringstream str;
112
        str << "from freecad import project_utility\n";
113
        str << "project_utility.createDocument(\"" << (const char*)source.toUtf8()
114
            << "\", \"" << (const char*)target.toUtf8() << "\")";
115
        Gui::Command::runCommand(Gui::Command::App, str.str().c_str());
116
        if (openFile) {
117
            Application::Instance->open((const char*)target.toUtf8(),"FreeCAD");
118
        }
119
    }
120
    catch (const Base::Exception& e) {
121
        QMessageBox::critical(this, tr("Failed to create document"), QString::fromLatin1(e.what()));
122
    }
123
}
124

125
#include "moc_DlgProjectUtility.cpp"
126

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.