| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/*************************************************************************** |
| 3 |
* Copyright (c) 2009 Jürgen Riegel <[email protected]> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 20 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
#include <App/Document.h> |
| 26 |
#include <App/DocumentObject.h> |
| 27 |
#include <Base/GeometryPyCXX.h> |
| 28 |
|
| 29 |
#include "Selection.h" |
| 30 |
#include "SelectionObject.h" |
| 31 |
|
| 32 |
|
| 33 |
// inclusion of the generated files (generated out of SelectionObjectPy.xml) |
| 34 |
#include "Selection/SelectionObjectPy.h" |
| 35 |
#include "Selection/SelectionObjectPy.cpp" |
| 36 |
|
| 37 |
|
| 38 |
using namespace Gui; |
| 39 |
|
| 40 |
// returns a string which represents the object e.g. when printed in python |
| 41 |
std::string SelectionObjectPy::representation() const |
| 42 |
{ |
| 43 |
return "<SelectionObject>"; |
| 44 |
} |
| 45 |
|
| 46 |
PyObject* SelectionObjectPy::remove(PyObject* args) |
| 47 |
{ |
| 48 |
if (!PyArg_ParseTuple(args, "")) { |
| 49 |
return nullptr; |
| 50 |
} |
| 51 |
Selection().rmvSelection( |
| 52 |
getSelectionObjectPtr()->getDocName(), |
| 53 |
getSelectionObjectPtr()->getFeatName() |
| 54 |
); |
| 55 |
Py_Return; |
| 56 |
} |
| 57 |
|
| 58 |
PyObject* SelectionObjectPy::isObjectTypeOf(PyObject* args) |
| 59 |
{ |
| 60 |
char* type; |
| 61 |
if (!PyArg_ParseTuple(args, "s", &type)) { |
| 62 |
return nullptr; |
| 63 |
} |
| 64 |
Base::Type id = Base::Type::fromName(type); |
| 65 |
if (id.isBad()) { |
| 66 |
PyErr_SetString(PyExc_TypeError, "Not a valid type"); |
| 67 |
return nullptr; |
| 68 |
} |
| 69 |
|
| 70 |
bool ok = getSelectionObjectPtr()->isObjectTypeOf(id); |
| 71 |
|
| 72 |
return Py_BuildValue("O", (ok ? Py_True : Py_False)); |
| 73 |
} |
| 74 |
|
| 75 |
Py::String SelectionObjectPy::getObjectName() const |
| 76 |
{ |
| 77 |
return {getSelectionObjectPtr()->getFeatName()}; |
| 78 |
} |
| 79 |
|
| 80 |
Py::Tuple SelectionObjectPy::getSubElementNames() const |
| 81 |
{ |
| 82 |
std::vector<std::string> objs = getSelectionObjectPtr()->getSubNames(); |
| 83 |
|
| 84 |
Py::Tuple temp(objs.size()); |
| 85 |
Py::sequence_index_type index = 0; |
| 86 |
for (const auto& obj : objs) { |
| 87 |
temp.setItem(index++, Py::String(obj)); |
| 88 |
} |
| 89 |
|
| 90 |
return temp; |
| 91 |
} |
| 92 |
|
| 93 |
Py::String SelectionObjectPy::getFullName() const |
| 94 |
{ |
| 95 |
return {getSelectionObjectPtr()->getAsPropertyLinkSubString()}; |
| 96 |
} |
| 97 |
|
| 98 |
Py::String SelectionObjectPy::getTypeName() const |
| 99 |
{ |
| 100 |
return {getSelectionObjectPtr()->getTypeName()}; |
| 101 |
} |
| 102 |
|
| 103 |
Py::String SelectionObjectPy::getDocumentName() const |
| 104 |
{ |
| 105 |
return {getSelectionObjectPtr()->getDocName()}; |
| 106 |
} |
| 107 |
|
| 108 |
Py::Object SelectionObjectPy::getDocument() const |
| 109 |
{ |
| 110 |
App::DocumentObject* obj = getSelectionObjectPtr()->getObject(); |
| 111 |
if (!obj) { |
| 112 |
throw Py::RuntimeError("Cannot get document of deleted object"); |
| 113 |
} |
| 114 |
return Py::Object(obj->getDocument()->getPyObject(), true); |
| 115 |
} |
| 116 |
|
| 117 |
Py::Object SelectionObjectPy::getObject() const |
| 118 |
{ |
| 119 |
App::DocumentObject* obj = getSelectionObjectPtr()->getObject(); |
| 120 |
if (!obj) { |
| 121 |
throw Py::RuntimeError("Object already deleted"); |
| 122 |
} |
| 123 |
return Py::Object(obj->getPyObject(), true); |
| 124 |
} |
| 125 |
|
| 126 |
Py::Tuple SelectionObjectPy::getSubObjects() const |
| 127 |
{ |
| 128 |
App::DocumentObject* obj = getSelectionObjectPtr()->getObject(); |
| 129 |
if (!obj) { |
| 130 |
throw Py::RuntimeError("Cannot get sub-objects of deleted object"); |
| 131 |
} |
| 132 |
|
| 133 |
std::vector<PyObject*> subObjs; |
| 134 |
|
| 135 |
for (const auto& subname : getSelectionObjectPtr()->getSubNames()) { |
| 136 |
PyObject* pyObj = nullptr; |
| 137 |
Base::Matrix4D mat; |
| 138 |
obj->getSubObject(subname.c_str(), &pyObj, &mat); |
| 139 |
if (pyObj) { |
| 140 |
subObjs.push_back(pyObj); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
Py::Tuple temp(subObjs.size()); |
| 145 |
Py::sequence_index_type index = 0; |
| 146 |
for (const auto& subObj : subObjs) { |
| 147 |
temp.setItem(index++, Py::asObject(subObj)); |
| 148 |
} |
| 149 |
|
| 150 |
return temp; |
| 151 |
} |
| 152 |
|
| 153 |
Py::Boolean SelectionObjectPy::getHasSubObjects() const |
| 154 |
{ |
| 155 |
return {getSelectionObjectPtr()->hasSubNames()}; |
| 156 |
} |
| 157 |
|
| 158 |
Py::Tuple SelectionObjectPy::getPickedPoints() const |
| 159 |
{ |
| 160 |
const std::vector<Base::Vector3d>& points = getSelectionObjectPtr()->getPickedPoints(); |
| 161 |
|
| 162 |
Py::Tuple temp(points.size()); |
| 163 |
Py::sequence_index_type index = 0; |
| 164 |
for (const auto& point : points) { |
| 165 |
temp.setItem(index++, Py::Vector(point)); |
| 166 |
} |
| 167 |
|
| 168 |
return temp; |
| 169 |
} |
| 170 |
|
| 171 |
PyObject* SelectionObjectPy::getCustomAttributes(const char* /*attr*/) const |
| 172 |
{ |
| 173 |
return nullptr; |
| 174 |
} |
| 175 |
|
| 176 |
int SelectionObjectPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) |
| 177 |
{ |
| 178 |
return 0; |
| 179 |
} |