| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2024 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
**************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
#include <sstream> |
| 26 |
|
| 27 |
|
| 28 |
#include <App/GeoFeature.h> |
| 29 |
#include <App/MaterialPy.h> |
| 30 |
#include <App/PropertyStandard.h> |
| 31 |
|
| 32 |
#include "ViewProviderGeometryObjectPy.h" |
| 33 |
|
| 34 |
#include "ViewProviderGeometryObjectPy.cpp" |
| 35 |
|
| 36 |
|
| 37 |
using namespace Gui; |
| 38 |
|
| 39 |
// returns a string which represents the object e.g. when printed in python |
| 40 |
std::string ViewProviderGeometryObjectPy::representation() const |
| 41 |
{ |
| 42 |
std::stringstream str; |
| 43 |
str << "<View provider geometry object at " << getViewProviderGeometryObjectPtr() << ">"; |
| 44 |
|
| 45 |
return str.str(); |
| 46 |
} |
| 47 |
|
| 48 |
PyObject* ViewProviderGeometryObjectPy::getCustomAttributes(const char* attr) const |
| 49 |
{ |
| 50 |
ViewProviderGeometryObject* vp = getViewProviderGeometryObjectPtr(); |
| 51 |
if (strcmp(attr, "ShapeColor") == 0) { |
| 52 |
// Get material property of ViewProviderGeometryObject |
| 53 |
App::PropertyColor prop; |
| 54 |
prop.setValue(vp->ShapeAppearance.getDiffuseColor()); |
| 55 |
return prop.getPyObject(); |
| 56 |
} |
| 57 |
if (strcmp(attr, "ShapeMaterial") == 0) { |
| 58 |
// Get material property of ViewProviderGeometryObject |
| 59 |
auto geometry = vp->getObject<App::GeoFeature>(); |
| 60 |
if (geometry) { |
| 61 |
auto material = geometry->getMaterialAppearance(); |
| 62 |
App::PropertyMaterial prop; |
| 63 |
prop.setValue(material); |
| 64 |
return prop.getPyObject(); |
| 65 |
} |
| 66 |
} |
| 67 |
return nullptr; |
| 68 |
} |
| 69 |
|
| 70 |
PyObject* ViewProviderGeometryObjectPy::getUserDefinedMaterial() |
| 71 |
{ |
| 72 |
App::Material mat = App::Material::getDefaultAppearance(); |
| 73 |
return new App::MaterialPy(new App::Material(mat)); |
| 74 |
} |
| 75 |
|
| 76 |
int ViewProviderGeometryObjectPy::setCustomAttributes(const char* attr, PyObject* obj) |
| 77 |
{ |
| 78 |
ViewProviderGeometryObject* vp = getViewProviderGeometryObjectPtr(); |
| 79 |
if (strcmp(attr, "ShapeColor") == 0) { |
| 80 |
// Get material property of ViewProviderGeometryObject |
| 81 |
App::PropertyColor prop; |
| 82 |
prop.setPyObject(obj); |
| 83 |
vp->ShapeAppearance.setDiffuseColor(prop.getValue()); |
| 84 |
// Assign the value to the new property type |
| 85 |
// ... |
| 86 |
return 1; |
| 87 |
} |
| 88 |
if (strcmp(attr, "ShapeMaterial") == 0) { |
| 89 |
// Get material property of ViewProviderGeometryObject |
| 90 |
auto geometry = vp->getObject<App::GeoFeature>(); |
| 91 |
if (geometry) { |
| 92 |
App::PropertyMaterial prop; |
| 93 |
prop.setPyObject(obj); |
| 94 |
geometry->setMaterialAppearance(prop.getValue()); |
| 95 |
} |
| 96 |
return 1; |
| 97 |
} |
| 98 |
return 0; |
| 99 |
} |