Skip to content

Commit

Permalink
remove Application::runPythonCode
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Sep 15, 2016
1 parent 0ef0703 commit afa3784
Show file tree
Hide file tree
Showing 19 changed files with 118 additions and 140 deletions.
58 changes: 7 additions & 51 deletions src/Gui/Application.cpp
Expand Up @@ -622,16 +622,15 @@ void Application::exportTo(const char* FileName, const char* DocName, const char

std::string code = str.str();
// the original file name is required
if (runPythonCode(code.c_str(), false)) {
// search for a module that is able to open the exported file because otherwise
// it doesn't need to be added to the recent files list (#0002047)
std::map<std::string, std::string> importMap = App::GetApplication().getImportFilters(te.c_str());
if (!importMap.empty())
getMainWindow()->appendRecentFile(QString::fromUtf8(File.filePath().c_str()));
}
Gui::Command::runCommand(Gui::Command::App, code.c_str());
// search for a module that is able to open the exported file because otherwise
// it doesn't need to be added to the recent files list (#0002047)
std::map<std::string, std::string> importMap = App::GetApplication().getImportFilters(te.c_str());
if (!importMap.empty())
getMainWindow()->appendRecentFile(QString::fromUtf8(File.filePath().c_str()));

// allow exporters to pass _objs__ to submodules before deleting it
runPythonCode("del __objs__", false);
Gui::Command::runCommand(Gui::Command::App, "del __objs__");
}
catch (const Base::PyException& e){
// Usually thrown if the file is invalid somehow
Expand Down Expand Up @@ -1363,49 +1362,6 @@ CommandManager &Application::commandManager(void)
return d->commandManager;
}

bool Application::runPythonCode(const char* cmd, bool gui, bool pyexc)
{
if (gui)
d->macroMngr->addLine(MacroManager::Gui,cmd);
else
d->macroMngr->addLine(MacroManager::App,cmd);

try {
Base::Interpreter().runString(cmd);
return true;
}
catch (Base::PyException &e) {
if (pyexc) {
e.ReportException();
Base::Console().Error("Stack Trace: %s\n",e.getStackTrace().c_str());
}
else {
throw; // re-throw to handle in calling instance
}
}
catch (Base::AbortException&) {
}
catch (Base::Exception &e) {
e.ReportException();
}
catch (std::exception &e) {
std::string str;
str += "C++ exception thrown (";
str += e.what();
str += ")";
Base::Console().Error(str.c_str());
}
catch (const char* e) {
Base::Console().Error("%s\n", e);
}
#ifndef FC_DEBUG
catch (...) {
Base::Console().Error("Unknown C++ exception in command thrown\n");
}
#endif
return false;
}

//**************************************************************************
// Init, Destruct and ingleton

Expand Down
2 changes: 0 additions & 2 deletions src/Gui/Application.h
Expand Up @@ -178,8 +178,6 @@ class GuiExport Application
Gui::MacroManager *macroManager(void);
/// Reference to the command manager
Gui::CommandManager &commandManager(void);
/// Run a Python command
bool runPythonCode(const char* cmd, bool gui=false, bool pyexc=true);
/// helper which create the commands
void createStandardOperations();
//@}
Expand Down
1 change: 1 addition & 0 deletions src/Gui/Command.cpp
Expand Up @@ -25,6 +25,7 @@
#ifndef _PreComp_
# include <sstream>
# include <QApplication>
# include <QByteArray>
# include <QDir>
# include <QKeySequence>
# include <QMessageBox>
Expand Down
2 changes: 2 additions & 0 deletions src/Gui/Command.h
Expand Up @@ -33,6 +33,8 @@
#include <Base/Type.h>

class QWidget;
class QByteArray;

typedef struct _object PyObject;

namespace App
Expand Down
5 changes: 3 additions & 2 deletions src/Gui/DlgProjectUtility.cpp
Expand Up @@ -31,6 +31,7 @@

#include "DlgProjectUtility.h"
#include "Application.h"
#include "Command.h"
#include "ui_DlgProjectUtility.h"


Expand Down Expand Up @@ -140,7 +141,7 @@ void DlgProjectUtility::on_extractButton_clicked()
str << doctools << "\n";
str << "extractDocument(\"" << (const char*)source.toUtf8()
<< "\", \"" << (const char*)dest.toUtf8() << "\")";
Application::Instance->runPythonCode(str.str().c_str());
Gui::Command::runCommand(Gui::Command::App, str.str().c_str());
}

void DlgProjectUtility::on_createButton_clicked()
Expand All @@ -162,7 +163,7 @@ void DlgProjectUtility::on_createButton_clicked()
str << doctools << "\n";
str << "createDocument(\"" << (const char*)source.toUtf8()
<< "\", \"" << (const char*)dest.toUtf8() << "\")";
Application::Instance->runPythonCode(str.str().c_str());
Gui::Command::runCommand(Gui::Command::App, str.str().c_str());

if (ui->checkLoadProject->isChecked())
Application::Instance->open((const char*)dest.toUtf8(),"FreeCAD");
Expand Down
3 changes: 2 additions & 1 deletion src/Gui/DocumentRecovery.cpp
Expand Up @@ -57,6 +57,7 @@
#include <App/Document.h>

#include <Gui/Application.h>
#include <Gui/Command.h>
#include <Gui/Document.h>

#include <QDomDocument>
Expand Down Expand Up @@ -212,7 +213,7 @@ QString DocumentRecovery::createProjectFile(const QString& documentXml)
str << doctools << "\n";
str << "createDocument(\"" << (const char*)source.toUtf8()
<< "\", \"" << (const char*)dest.toUtf8() << "\")";
Application::Instance->runPythonCode(str.str().c_str());
Gui::Command::runCommand(Gui::Command::App, str.str().c_str());

return dest;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Gui/Placement.cpp
Expand Up @@ -30,6 +30,7 @@
#include "ui_Placement.h"
#include <Gui/DockWindowManager.h>
#include <Gui/Application.h>
#include <Gui/Command.h>
#include <Gui/Document.h>
#include <Gui/Selection.h>
#include <Gui/ViewProvider.h>
Expand Down Expand Up @@ -230,7 +231,7 @@ void Placement::applyPlacement(const QString& data, bool incremental)
.arg(data);
}

Application::Instance->runPythonCode((const char*)cmd.toLatin1());
Gui::Command::runCommand(Gui::Command::App, cmd.toLatin1());
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Gui/Tree.cpp
Expand Up @@ -46,6 +46,7 @@
#include <App/DocumentObjectGroup.h>

#include "Tree.h"
#include "Command.h"
#include "Document.h"
#include "BitmapFactory.h"
#include "ViewProviderDocumentObject.h"
Expand Down Expand Up @@ -226,7 +227,7 @@ void TreeWidget::onCreateGroup()
.arg(QString::fromLatin1(doc->getName())).arg(name);
Gui::Document* gui = Gui::Application::Instance->getDocument(doc);
gui->openCommand("Create group");
Gui::Application::Instance->runPythonCode(cmd.toUtf8());
Gui::Command::runCommand(Gui::Command::App, cmd.toUtf8());
gui->commitCommand();
}
else if (this->contextItem->type() == ObjectType) {
Expand All @@ -241,7 +242,7 @@ void TreeWidget::onCreateGroup()
.arg(name);
Gui::Document* gui = Gui::Application::Instance->getDocument(doc);
gui->openCommand("Create group");
Gui::Application::Instance->runPythonCode(cmd.toUtf8());
Gui::Command::runCommand(Gui::Command::App, cmd.toUtf8());
gui->commitCommand();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Gui/ViewProviderDocumentObjectGroup.cpp
Expand Up @@ -208,7 +208,7 @@ void ViewProviderDocumentObjectGroup::drop(const std::vector<const App::Document
.arg(QString::fromLatin1(doc->getName()))
.arg(QString::fromLatin1(par->getNameInDocument()))
.arg(QString::fromLatin1(obj->getNameInDocument()));
Gui::Application::Instance->runPythonCode(cmd.toUtf8());
Gui::Command::runCommand(Gui::Command::App, cmd.toUtf8());
}

// build Python command for execution
Expand All @@ -219,7 +219,7 @@ void ViewProviderDocumentObjectGroup::drop(const std::vector<const App::Document
.arg(QString::fromLatin1(grp->getNameInDocument()))
.arg(QString::fromLatin1(obj->getNameInDocument()));

Gui::Application::Instance->runPythonCode(cmd.toUtf8());
Gui::Command::runCommand(Gui::Command::App, cmd.toUtf8());
}
gui->commitCommand();

Expand Down
18 changes: 16 additions & 2 deletions src/Gui/propertyeditor/PropertyItem.cpp
Expand Up @@ -36,6 +36,7 @@

#include <Base/Tools.h>
#include <Base/Console.h>
#include <Base/Interpreter.h>
#include <App/Application.h>
#include <App/Document.h>
#include <App/DocumentObject.h>
Expand All @@ -44,6 +45,7 @@
#include <App/PropertyUnits.h>
#include <Gui/Application.h>
#include <Gui/Control.h>
#include <Gui/Command.h>
#include <Gui/Document.h>
#include <Gui/Selection.h>
#include <Gui/ViewProviderDocumentObject.h>
Expand All @@ -53,7 +55,7 @@
#include <Gui/QuantitySpinBox.h>

#include "PropertyItem.h"
#include <SpinBox.h>
#include <Gui/SpinBox.h>

using namespace Gui::PropertyEditor;

Expand Down Expand Up @@ -353,7 +355,19 @@ void PropertyItem::setPropertyValue(const QString& value)
App::PropertyContainer* parent = (*it)->getContainer();
if (parent && !parent->isReadOnly(*it) && !(*it)->testStatus(App::Property::ReadOnly)) {
QString cmd = QString::fromLatin1("%1 = %2").arg(pythonIdentifier(*it)).arg(value);
Gui::Application::Instance->runPythonCode((const char*)cmd.toUtf8());
try {
Gui::Command::runCommand(Gui::Command::App, cmd.toUtf8());
}
catch (Base::PyException &e) {
e.ReportException();
Base::Console().Error("Stack Trace: %s\n",e.getStackTrace().c_str());
}
catch (Base::Exception &e) {
e.ReportException();
}
catch (...) {
Base::Console().Error("Unknown C++ exception in PropertyItem::setPropertyValue thrown\n");
}
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Mod/Part/Gui/CrossSections.cpp
Expand Up @@ -51,6 +51,7 @@
#include <Gui/BitmapFactory.h>
#include <Gui/ViewProvider.h>
#include <Gui/Application.h>
#include <Gui/Command.h>
#include <Gui/Document.h>
#include <Gui/View3DInventor.h>
#include <Gui/View3DInventorViewer.h>
Expand Down Expand Up @@ -232,29 +233,28 @@ void CrossSections::apply()
section->purgeTouched();
}
#else
Gui::Application* app = Gui::Application::Instance;
Base::SequencerLauncher seq("Cross-sections...", obj.size() * (d.size() +1));
app->runPythonCode("import Part\n");
app->runPythonCode("from FreeCAD import Base\n");
Gui::Command::runCommand(Gui::Command::App, "import Part\n");
Gui::Command::runCommand(Gui::Command::App, "from FreeCAD import Base\n");
for (std::vector<App::DocumentObject*>::iterator it = obj.begin(); it != obj.end(); ++it) {
App::Document* doc = (*it)->getDocument();
std::string s = (*it)->getNameInDocument();
s += "_cs";
app->runPythonCode(QString::fromLatin1(
Gui::Command::runCommand(Gui::Command::App, QString::fromLatin1(
"wires=list()\n"
"shape=FreeCAD.getDocument(\"%1\").%2.Shape\n")
.arg(QLatin1String(doc->getName()))
.arg(QLatin1String((*it)->getNameInDocument())).toLatin1());

for (std::vector<double>::iterator jt = d.begin(); jt != d.end(); ++jt) {
app->runPythonCode(QString::fromLatin1(
Gui::Command::runCommand(Gui::Command::App, QString::fromLatin1(
"for i in shape.slice(Base.Vector(%1,%2,%3),%4):\n"
" wires.append(i)\n"
).arg(a).arg(b).arg(c).arg(*jt).toLatin1());
seq.next();
}

app->runPythonCode(QString::fromLatin1(
Gui::Command::runCommand(Gui::Command::App, QString::fromLatin1(
"comp=Part.Compound(wires)\n"
"slice=FreeCAD.getDocument(\"%1\").addObject(\"Part::Feature\",\"%2\")\n"
"slice.Shape=comp\n"
Expand Down
4 changes: 2 additions & 2 deletions src/Mod/Part/Gui/DlgFilletEdges.cpp
Expand Up @@ -679,7 +679,7 @@ void DlgFilletEdges::changeEvent(QEvent *e)
int id = model->data(model->index(i, 0), Qt::UserRole).toInt();
model->setData(model->index(i, 0), QVariant(tr("Edge%1").arg(id)));
}
}
}
else {
QWidget::changeEvent(e);
}
Expand Down Expand Up @@ -936,7 +936,7 @@ bool DlgFilletEdges::accept()
"del __fillets__\n"
"FreeCADGui.ActiveDocument.%2.Visibility = False\n")
.arg(name).arg(shape);
Gui::Application::Instance->runPythonCode((const char*)code.toLatin1());
Gui::Command::runCommand(Gui::Command::App, code.toLatin1());
activeDoc->commitTransaction();
activeDoc->recompute();
if (d->fillet) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/Part/Gui/DlgRevolution.cpp
Expand Up @@ -405,7 +405,7 @@ void DlgRevolution::accept()
.arg(strAxisLink) //%12
.arg(symmetric) //13
;
Gui::Application::Instance->runPythonCode((const char*)code.toLatin1());
Gui::Command::runCommand(Gui::Command::App, code.toLatin1());
QByteArray to = name.toLatin1();
QByteArray from = shape.toLatin1();
Gui::Command::copyVisual(to, "ShapeColor", from);
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/Part/Gui/Mirroring.cpp
Expand Up @@ -171,7 +171,7 @@ bool Mirroring::accept()
.arg(this->document).arg(shape).arg(label)
.arg(normx).arg(normy).arg(normz)
.arg(basex).arg(basey).arg(basez);
Gui::Application::Instance->runPythonCode((const char*)code.toLatin1());
Gui::Command::runCommand(Gui::Command::App, code.toLatin1());
QByteArray from = shape.toLatin1();
Gui::Command::copyVisual("ActiveObject", "ShapeColor", from);
Gui::Command::copyVisual("ActiveObject", "LineColor", from);
Expand Down
7 changes: 4 additions & 3 deletions src/Mod/Part/Gui/TaskLoft.cpp
Expand Up @@ -34,6 +34,7 @@

#include <Gui/Application.h>
#include <Gui/BitmapFactory.h>
#include <Gui/Command.h>
#include <Gui/Document.h>
#include <Gui/Selection.h>
#include <Gui/ViewProvider.h>
Expand Down Expand Up @@ -66,8 +67,8 @@ class LoftWidget::Private
LoftWidget::LoftWidget(QWidget* parent)
: d(new Private())
{
Gui::Application::Instance->runPythonCode("from FreeCAD import Base");
Gui::Application::Instance->runPythonCode("import Part");
Gui::Command::runCommand(Gui::Command::App, "from FreeCAD import Base");
Gui::Command::runCommand(Gui::Command::App, "import Part");

d->ui.setupUi(this);
d->ui.selector->setAvailableLabel(tr("Vertex/Edge/Wire/Face"));
Expand Down Expand Up @@ -175,7 +176,7 @@ bool LoftWidget::accept()
Gui::Document* doc = Gui::Application::Instance->getDocument(d->document.c_str());
if (!doc) throw Base::Exception("Document doesn't exist anymore");
doc->openCommand("Loft");
Gui::Application::Instance->runPythonCode((const char*)cmd.toLatin1(), false, false);
Gui::Command::runCommand(Gui::Command::App, cmd.toLatin1());
doc->getDocument()->recompute();
App::DocumentObject* obj = doc->getDocument()->getActiveObject();
if (obj && !obj->isValid()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Mod/Part/Gui/TaskOffset.cpp
Expand Up @@ -68,8 +68,8 @@ class OffsetWidget::Private
OffsetWidget::OffsetWidget(Part::Offset* offset, QWidget* parent)
: d(new Private())
{
Gui::Application::Instance->runPythonCode("from FreeCAD import Base");
Gui::Application::Instance->runPythonCode("import Part");
Gui::Command::runCommand(Gui::Command::App, "from FreeCAD import Base");
Gui::Command::runCommand(Gui::Command::App, "import Part");

d->offset = offset;
d->ui.setupUi(this);
Expand Down

0 comments on commit afa3784

Please sign in to comment.