Core: Fix macro recording when using module_io

This commit is contained in:
wmayer
2024-09-27 19:22:15 +02:00
committed by Chris Hennes
parent 9913d6a1aa
commit 08ff1e5eeb
2 changed files with 13 additions and 33 deletions

View File

@@ -1,11 +1,13 @@
from FreeCAD import Base
import importlib
def OpenInsertObject(importerModule, objectPath, importMethod, docName = ""):
def OpenInsertObject(importerName, objectPath, importMethod, docName = ""):
try:
importArgs = []
importKwargs = {}
importerModule = importlib.import_module(importerName)
if docName:
importArgs.append(docName)
if hasattr(importerModule, "importOptions"):

View File

@@ -42,6 +42,7 @@
#endif
#include <QLoggingCategory>
#include <fmt/format.h>
#include <App/Document.h>
#include <App/DocumentObjectPy.h>
@@ -637,21 +638,10 @@ void Application::open(const char* FileName, const char* Module)
}
}
else {
// Load using provided python module
{
Base::PyGILStateLocker locker;
Py::Module moduleIo(PyImport_ImportModule("freecad.module_io"));
const auto dictS = moduleIo.getDict().keys().as_string();
if (!moduleIo.isNull() && moduleIo.hasAttr("OpenInsertObject"))
{
const Py::TupleN args(
Py::Module(PyImport_ImportModule(Module)),
Py::String(unicodepath),
Py::String("open")
);
moduleIo.callMemberFunction("OpenInsertObject", args);
}
}
std::string code = fmt::format("from freecad import module_io\n"
"module_io.OpenInsertObject(\"{}\", \"{}\", \"{}\")\n",
Module, unicodepath, "open");
Gui::Command::runCommand(Gui::Command::App, code.c_str());
// ViewFit
if (sendHasMsgToActiveView("ViewFit")) {
@@ -716,22 +706,10 @@ void Application::importFrom(const char* FileName, const char* DocName, const ch
}
}
// Load using provided python module
{
Base::PyGILStateLocker locker;
Py::Module moduleIo(PyImport_ImportModule("freecad.module_io"));
const auto dictS = moduleIo.getDict().keys().as_string();
if (!moduleIo.isNull() && moduleIo.hasAttr("OpenInsertObject"))
{
const Py::TupleN args(
Py::Module(PyImport_ImportModule(Module)),
Py::String(unicodepath),
Py::String("insert"),
Py::String(DocName)
);
moduleIo.callMemberFunction("OpenInsertObject", args);
}
}
std::string code = fmt::format("from freecad import module_io\n"
"module_io.OpenInsertObject(\"{}\", \"{}\", \"{}\", \"{}\")\n",
Module, unicodepath, "insert", DocName);
Gui::Command::runCommand(Gui::Command::App, code.c_str());
// Commit the transaction
if (doc && !pendingCommand) {