Files
create/src/Mod/Material/App/ModelManagerPyImpl.cpp
Chris Hennes fa65438556 Core: Enable compiling with MSVC /permissive- (#11014)
* Base: Fixes for MSVC permissive-

* App: Fixes for MSVC permissive-

* Gui: Fixes for MSVC permissive-

* Main: Fixes for MSVC permissive-

* Fem: Fixes for MSVC permissive-

* Material: Fixes for MSVC permissive-

* Part: Fixes for MSVC permissive-

* Mesh: Fixes for MSVC permissive-

* Points: Fixes for MSVC permissive-

* Robot: Fixes for MSVC permissive-

* TechDraw: Fixes for MSVC permissive-

* Path: Fixes for MSVC permissive-

* Core; Changes per review comments

* TD: Revision from wandererfan

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-11-27 17:37:29 +01:00

160 lines
5.5 KiB
C++

/***************************************************************************
* Copyright (c) 2023 David Carter <dcarter@david.carter.ca> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include "PreCompiled.h"
#include "Model.h"
#include "ModelLibrary.h"
#include "ModelManager.h"
#include "ModelManagerPy.h"
#include "ModelPy.h"
#include "ModelManagerPy.cpp"
using namespace Materials;
// returns a string which represents the object e.g. when printed in python
std::string ModelManagerPy::representation() const
{
std::stringstream str;
str << "<ModelManager object at " << getModelManagerPtr() << ">";
return str.str();
}
PyObject* ModelManagerPy::PyMake(struct _typeobject*, PyObject*, PyObject*) // Python wrapper
{
// never create such objects with the constructor
return new ModelManagerPy(new ModelManager());
}
// constructor method
int ModelManagerPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
{
return 0;
}
PyObject* ModelManagerPy::getModel(PyObject* args)
{
char* uuid {};
if (!PyArg_ParseTuple(args, "s", &uuid)) {
return nullptr;
}
try {
auto model = getModelManagerPtr()->getModel(QString::fromStdString(uuid));
return new ModelPy(new Model(*model));
}
catch (ModelNotFound const&) {
QString error = QString::fromStdString("Model not found:\n");
auto _modelMap = getModelManagerPtr()->getModels();
error += QString::fromStdString("ModelMap:\n");
for (auto itp = _modelMap->begin(); itp != _modelMap->end(); itp++) {
error += QString::fromStdString("\t_modelMap[") + itp->first
+ QString::fromStdString("] = '") + itp->second->getName()
+ QString::fromStdString("'\n");
}
error += QString::fromStdString("\tuuid = '") + QString::fromStdString(uuid)
+ QString::fromStdString("'\n");
PyErr_SetString(PyExc_LookupError, error.toStdString().c_str());
return nullptr;
}
catch (Uninitialized const&) {
PyErr_SetString(PyExc_LookupError, "Uninitialized model list");
return nullptr;
}
}
PyObject* ModelManagerPy::getModelByPath(PyObject* args)
{
char* path {};
const char* lib = "";
if (!PyArg_ParseTuple(args, "s|s", &path, &lib)) {
return nullptr;
}
std::string libPath(lib);
if (libPath.length() > 0) {
try {
auto model = getModelManagerPtr()->getModelByPath(QString::fromStdString(path),
QString::fromStdString(libPath));
return new ModelPy(new Model(*model));
}
catch (ModelNotFound const&) {
PyErr_SetString(PyExc_LookupError, "Model not found");
return nullptr;
}
}
try {
auto model = getModelManagerPtr()->getModelByPath(QString::fromStdString(path));
return new ModelPy(new Model(*model));
}
catch (ModelNotFound const&) {
PyErr_SetString(PyExc_LookupError, "Model not found");
return nullptr;
}
}
Py::List ModelManagerPy::getModelLibraries() const
{
auto libraries = getModelManagerPtr()->getModelLibraries();
Py::List list;
for (auto it = libraries->begin(); it != libraries->end(); it++) {
auto lib = *it;
Py::Tuple libTuple(3);
libTuple.setItem(0, Py::String(lib->getName().toStdString()));
libTuple.setItem(1, Py::String(lib->getDirectoryPath().toStdString()));
libTuple.setItem(2, Py::String(lib->getIconPath().toStdString()));
list.append(libTuple);
}
return list;
}
Py::Dict ModelManagerPy::getModels() const
{
auto models = getModelManagerPtr()->getModels();
Py::Dict dict;
for (auto it = models->begin(); it != models->end(); it++) {
QString key = it->first;
auto model = it->second;
PyObject* modelPy = new ModelPy(new Model(*model));
dict.setItem(Py::String(key.toStdString()), Py::Object(modelPy, true));
}
return dict;
}
PyObject* ModelManagerPy::getCustomAttributes(const char* /*attr*/) const
{
return nullptr;
}
int ModelManagerPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
return 0;
}