1
/***************************************************************************
2
* Copyright (c) 2023 David Carter <dcarter@david.carter.ca> *
4
* This file is part of FreeCAD. *
6
* FreeCAD is free software: you can redistribute it and/or modify it *
7
* under the terms of the GNU Lesser General Public License as *
8
* published by the Free Software Foundation, either version 2.1 of the *
9
* License, or (at your option) any later version. *
11
* FreeCAD is distributed in the hope that it will be useful, but *
12
* WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14
* Lesser General Public License for more details. *
16
* You should have received a copy of the GNU Lesser General Public *
17
* License along with FreeCAD. If not, see *
18
* <https://www.gnu.org/licenses/>. *
20
**************************************************************************/
22
#include "PreCompiled.h"
25
#include "ModelLibrary.h"
26
#include "ModelManager.h"
27
#include "ModelManagerPy.h"
30
#include "ModelManagerPy.cpp"
32
using namespace Materials;
34
// returns a string which represents the object e.g. when printed in python
35
std::string ModelManagerPy::representation() const
37
std::stringstream str;
38
str << "<ModelManager object at " << getModelManagerPtr() << ">";
43
PyObject* ModelManagerPy::PyMake(struct _typeobject*, PyObject*, PyObject*) // Python wrapper
45
// never create such objects with the constructor
46
return new ModelManagerPy(new ModelManager());
50
int ModelManagerPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
55
PyObject* ModelManagerPy::getModel(PyObject* args)
58
if (!PyArg_ParseTuple(args, "s", &uuid)) {
63
auto model = getModelManagerPtr()->getModel(QString::fromStdString(uuid));
64
return new ModelPy(new Model(*model));
66
catch (ModelNotFound const&) {
67
QString error = QString::fromStdString("Model not found:\n");
68
auto _modelMap = getModelManagerPtr()->getModels();
69
error += QString::fromStdString("ModelMap:\n");
70
for (auto itp = _modelMap->begin(); itp != _modelMap->end(); itp++) {
71
error += QString::fromStdString("\t_modelMap[") + itp->first
72
+ QString::fromStdString("] = '") + itp->second->getName()
73
+ QString::fromStdString("'\n");
75
error += QString::fromStdString("\tuuid = '") + QString::fromStdString(uuid)
76
+ QString::fromStdString("'\n");
77
PyErr_SetString(PyExc_LookupError, error.toStdString().c_str());
80
catch (Uninitialized const&) {
81
PyErr_SetString(PyExc_LookupError, "Uninitialized model list");
86
PyObject* ModelManagerPy::getModelByPath(PyObject* args)
90
if (!PyArg_ParseTuple(args, "s|s", &path, &lib)) {
94
std::string libPath(lib);
95
if (libPath.length() > 0) {
97
auto model = getModelManagerPtr()->getModelByPath(QString::fromStdString(path),
98
QString::fromStdString(libPath));
99
return new ModelPy(new Model(*model));
101
catch (ModelNotFound const&) {
102
PyErr_SetString(PyExc_LookupError, "Model not found");
108
auto model = getModelManagerPtr()->getModelByPath(QString::fromStdString(path));
109
return new ModelPy(new Model(*model));
111
catch (ModelNotFound const&) {
112
PyErr_SetString(PyExc_LookupError, "Model not found");
117
Py::List ModelManagerPy::getModelLibraries() const
119
auto libraries = getModelManagerPtr()->getModelLibraries();
122
for (auto it = libraries->begin(); it != libraries->end(); it++) {
124
Py::Tuple libTuple(3);
125
libTuple.setItem(0, Py::String(lib->getName().toStdString()));
126
libTuple.setItem(1, Py::String(lib->getDirectoryPath().toStdString()));
127
libTuple.setItem(2, Py::String(lib->getIconPath().toStdString()));
129
list.append(libTuple);
135
Py::Dict ModelManagerPy::getModels() const
137
auto models = getModelManagerPtr()->getModels();
140
for (auto it = models->begin(); it != models->end(); it++) {
141
QString key = it->first;
142
auto model = it->second;
144
PyObject* modelPy = new ModelPy(new Model(*model));
145
dict.setItem(Py::String(key.toStdString()), Py::Object(modelPy, true));
151
PyObject* ModelManagerPy::getCustomAttributes(const char* /*attr*/) const
156
int ModelManagerPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)