FreeCAD

Форк
0
/
ModelManagerPyImp.cpp 
159 строк · 5.5 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2023 David Carter <dcarter@david.carter.ca>             *
3
 *                                                                         *
4
 *   This file is part of FreeCAD.                                         *
5
 *                                                                         *
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.                       *
10
 *                                                                         *
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.                       *
15
 *                                                                         *
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/>.                                      *
19
 *                                                                         *
20
 **************************************************************************/
21

22
#include "PreCompiled.h"
23

24
#include "Model.h"
25
#include "ModelLibrary.h"
26
#include "ModelManager.h"
27
#include "ModelManagerPy.h"
28
#include "ModelPy.h"
29

30
#include "ModelManagerPy.cpp"
31

32
using namespace Materials;
33

34
// returns a string which represents the object e.g. when printed in python
35
std::string ModelManagerPy::representation() const
36
{
37
    std::stringstream str;
38
    str << "<ModelManager object at " << getModelManagerPtr() << ">";
39

40
    return str.str();
41
}
42

43
PyObject* ModelManagerPy::PyMake(struct _typeobject*, PyObject*, PyObject*)  // Python wrapper
44
{
45
    // never create such objects with the constructor
46
    return new ModelManagerPy(new ModelManager());
47
}
48

49
// constructor method
50
int ModelManagerPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
51
{
52
    return 0;
53
}
54

55
PyObject* ModelManagerPy::getModel(PyObject* args)
56
{
57
    char* uuid {};
58
    if (!PyArg_ParseTuple(args, "s", &uuid)) {
59
        return nullptr;
60
    }
61

62
    try {
63
        auto model = getModelManagerPtr()->getModel(QString::fromStdString(uuid));
64
        return new ModelPy(new Model(*model));
65
    }
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");
74
        }
75
        error += QString::fromStdString("\tuuid = '") + QString::fromStdString(uuid)
76
            + QString::fromStdString("'\n");
77
        PyErr_SetString(PyExc_LookupError, error.toStdString().c_str());
78
        return nullptr;
79
    }
80
    catch (Uninitialized const&) {
81
        PyErr_SetString(PyExc_LookupError, "Uninitialized model list");
82
        return nullptr;
83
    }
84
}
85

86
PyObject* ModelManagerPy::getModelByPath(PyObject* args)
87
{
88
    char* path {};
89
    const char* lib = "";
90
    if (!PyArg_ParseTuple(args, "s|s", &path, &lib)) {
91
        return nullptr;
92
    }
93

94
    std::string libPath(lib);
95
    if (libPath.length() > 0) {
96
        try {
97
            auto model = getModelManagerPtr()->getModelByPath(QString::fromStdString(path),
98
                                                              QString::fromStdString(libPath));
99
            return new ModelPy(new Model(*model));
100
        }
101
        catch (ModelNotFound const&) {
102
            PyErr_SetString(PyExc_LookupError, "Model not found");
103
            return nullptr;
104
        }
105
    }
106

107
    try {
108
        auto model = getModelManagerPtr()->getModelByPath(QString::fromStdString(path));
109
        return new ModelPy(new Model(*model));
110
    }
111
    catch (ModelNotFound const&) {
112
        PyErr_SetString(PyExc_LookupError, "Model not found");
113
        return nullptr;
114
    }
115
}
116

117
Py::List ModelManagerPy::getModelLibraries() const
118
{
119
    auto libraries = getModelManagerPtr()->getModelLibraries();
120
    Py::List list;
121

122
    for (auto it = libraries->begin(); it != libraries->end(); it++) {
123
        auto lib = *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()));
128

129
        list.append(libTuple);
130
    }
131

132
    return list;
133
}
134

135
Py::Dict ModelManagerPy::getModels() const
136
{
137
    auto models = getModelManagerPtr()->getModels();
138
    Py::Dict dict;
139

140
    for (auto it = models->begin(); it != models->end(); it++) {
141
        QString key = it->first;
142
        auto model = it->second;
143

144
        PyObject* modelPy = new ModelPy(new Model(*model));
145
        dict.setItem(Py::String(key.toStdString()), Py::Object(modelPy, true));
146
    }
147

148
    return dict;
149
}
150

151
PyObject* ModelManagerPy::getCustomAttributes(const char* /*attr*/) const
152
{
153
    return nullptr;
154
}
155

156
int ModelManagerPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
157
{
158
    return 0;
159
}
160

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

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

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

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