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"
24
#include "Exceptions.h"
25
#include "MaterialFilter.h"
26
#include "MaterialFilterPy.h"
27
#include "MaterialManager.h"
28
#include "MaterialManagerPy.h"
29
#include "MaterialPy.h"
32
#include "MaterialManagerPy.cpp"
34
#include <Base/PyWrapParseTupleAndKeywords.h>
36
using namespace Materials;
38
// returns a string which represents the object e.g. when printed in python
39
std::string MaterialManagerPy::representation() const
41
std::stringstream str;
42
str << "<MaterialManager object at " << getMaterialManagerPtr() << ">";
47
PyObject* MaterialManagerPy::PyMake(struct _typeobject*, PyObject*, PyObject*) // Python wrapper
49
// never create such objects with the constructor
50
return new MaterialManagerPy(new MaterialManager());
54
int MaterialManagerPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
59
PyObject* MaterialManagerPy::getMaterial(PyObject* args)
62
if (!PyArg_ParseTuple(args, "s", &uuid)) {
67
auto material = getMaterialManagerPtr()->getMaterial(QString::fromStdString(uuid));
68
return new MaterialPy(new Material(*material));
70
catch (const MaterialNotFound&) {
71
PyErr_SetString(PyExc_LookupError, "Material not found");
76
PyObject* MaterialManagerPy::getMaterialByPath(PyObject* args)
80
if (!PyArg_ParseTuple(args, "et|s", "utf-8", &path, &lib)) {
84
std::string utf8Path = std::string(path);
87
QString libPath(QString::fromStdString(lib));
88
if (!libPath.isEmpty()) {
91
getMaterialManagerPtr()->getMaterialByPath(QString::fromUtf8(utf8Path.c_str()),
93
return new MaterialPy(new Material(*material));
95
catch (const MaterialNotFound&) {
96
PyErr_SetString(PyExc_LookupError, "Material not found");
99
catch (const LibraryNotFound&) {
100
PyErr_SetString(PyExc_LookupError, "Library not found");
107
getMaterialManagerPtr()->getMaterialByPath(QString::fromUtf8(utf8Path.c_str()));
108
return new MaterialPy(new Material(*material));
110
catch (const MaterialNotFound&) {
111
PyErr_SetString(PyExc_LookupError, "Material not found");
116
PyObject* MaterialManagerPy::inheritMaterial(PyObject* args)
119
if (!PyArg_ParseTuple(args, "s", &uuid)) {
124
auto parent = getMaterialManagerPtr()->getMaterial(QString::fromStdString(uuid));
126
// Found the parent. Create a new material with this as parent
127
auto material = new Material();
128
material->setParentUUID(QString::fromLatin1(uuid));
129
return new MaterialPy(material); // Transfers ownership
131
catch (const MaterialNotFound&) {
132
PyErr_SetString(PyExc_LookupError, "Material not found");
137
Py::List MaterialManagerPy::getMaterialLibraries() const
139
auto libraries = getMaterialManagerPtr()->getMaterialLibraries();
142
for (auto it = libraries->begin(); it != libraries->end(); it++) {
144
Py::Tuple libTuple(3);
145
libTuple.setItem(0, Py::String(lib->getName().toStdString()));
146
libTuple.setItem(1, Py::String(lib->getDirectoryPath().toStdString()));
147
libTuple.setItem(2, Py::String(lib->getIconPath().toStdString()));
149
list.append(libTuple);
155
Py::Dict MaterialManagerPy::getMaterials() const
159
auto materials = getMaterialManagerPtr()->getMaterials();
161
for (auto it = materials->begin(); it != materials->end(); it++) {
162
QString key = it->first;
163
auto material = it->second;
165
PyObject* materialPy = new MaterialPy(new Material(*material));
166
dict.setItem(Py::String(key.toStdString()), Py::Object(materialPy, true));
169
// return Py::new_reference_to(dict);
173
PyObject* MaterialManagerPy::getCustomAttributes(const char* /*attr*/) const
178
int MaterialManagerPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
183
PyObject* MaterialManagerPy::materialsWithModel(PyObject* args)
186
if (!PyArg_ParseTuple(args, "s", &uuid)) {
190
auto materials = getMaterialManagerPtr()->materialsWithModel(QString::fromStdString(uuid));
193
for (auto it = materials->begin(); it != materials->end(); it++) {
194
QString key = it->first;
195
auto material = it->second;
197
PyObject* materialPy = new MaterialPy(new Material(*material));
198
dict.setItem(key.toStdString(), Py::asObject(materialPy));
201
return Py::new_reference_to(dict);
204
PyObject* MaterialManagerPy::materialsWithModelComplete(PyObject* args)
207
if (!PyArg_ParseTuple(args, "s", &uuid)) {
212
getMaterialManagerPtr()->materialsWithModelComplete(QString::fromStdString(uuid));
215
for (auto it = materials->begin(); it != materials->end(); it++) {
216
QString key = it->first;
217
auto material = it->second;
219
PyObject* materialPy = new MaterialPy(new Material(*material));
220
dict.setItem(key.toStdString(), Py::asObject(materialPy));
223
return Py::new_reference_to(dict);
226
PyObject* MaterialManagerPy::save(PyObject* args, PyObject* kwds)
228
char* libraryName {};
231
PyObject* overwrite = Py_False;
232
PyObject* saveAsCopy = Py_False;
233
PyObject* saveInherited = Py_False;
234
static const std::array<const char *, 7> kwlist { "library", "material", "path", "overwrite", "saveAsCopy", "saveInherited", nullptr };
235
if (!Base::Wrapped_ParseTupleAndKeywords(args,
239
"utf-8", &libraryName,
242
&PyBool_Type, &overwrite,
243
&PyBool_Type, &saveAsCopy,
244
&PyBool_Type, &saveInherited)) {
248
Base::Console().Log("library name %s\n", libraryName);
249
Base::Console().Log("path %s\n", path);
251
MaterialPy* material;
252
if (QLatin1String(obj->ob_type->tp_name) == QLatin1String("Materials.Material")) {
253
material = static_cast<MaterialPy*>(obj);
256
PyErr_Format(PyExc_TypeError, "Material expected not '%s'", obj->ob_type->tp_name);
260
PyErr_SetString(PyExc_TypeError, "Invalid material object");
263
auto sharedMaterial = std::make_shared<Material>(*(material->getMaterialPtr()));
265
std::shared_ptr<MaterialLibrary> library;
267
library = getMaterialManagerPtr()->getLibrary(QString::fromUtf8(libraryName));
269
catch (const LibraryNotFound&) {
270
PyErr_SetString(PyExc_LookupError, "Unknown library");
275
getMaterialManagerPtr()->saveMaterial(library,
277
QString::fromUtf8(path),
278
PyObject_IsTrue(overwrite),
279
PyObject_IsTrue(saveAsCopy),
280
PyObject_IsTrue(saveInherited));
281
material->getMaterialPtr()->setUUID(sharedMaterial->getUUID()); // Make sure they match
287
void addMaterials(Py::List& list,
288
const std::shared_ptr<std::map<QString, std::shared_ptr<MaterialTreeNode>>>& tree)
290
for (auto& node : *tree) {
291
if (node.second->getType() == MaterialTreeNode::DataNode) {
292
auto material = node.second->getData();
293
PyObject* materialPy = new MaterialPy(new Material(*material));
294
list.append(Py::Object(materialPy, true));
297
addMaterials(list, node.second->getFolder());
302
PyObject* MaterialManagerPy::filterMaterials(PyObject* args, PyObject* kwds)
304
PyObject* filterPy {};
305
PyObject* includeLegacy = Py_False;
306
static const std::array<const char*, 3> kwds_save{ "filter",
309
if (!Base::Wrapped_ParseTupleAndKeywords(args,
314
&MaterialFilterPy::Type,
321
MaterialFilterOptions options;
322
options.setIncludeFavorites(false);
323
options.setIncludeRecent(false);
324
options.setIncludeEmptyFolders(false);
325
options.setIncludeEmptyLibraries(false);
326
options.setIncludeLegacy(PyObject_IsTrue(includeLegacy));
328
auto filter = std::make_shared<MaterialFilter>(*(static_cast<MaterialFilterPy*>(filterPy)->getMaterialFilterPtr()));
330
auto libraries = getMaterialManagerPtr()->getMaterialLibraries();
333
for (auto lib : *libraries) {
334
auto tree = getMaterialManagerPtr()->getMaterialTree(lib, filter, options);
335
if (tree->size() > 0) {
336
addMaterials(list, tree);
344
PyObject* MaterialManagerPy::refresh(PyObject* /*args*/)
346
getMaterialManagerPtr()->refresh();