| 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/DocumentObjectPy.h> |
| 26 |
|
| 27 |
#include "SelectionFilterPy.h" |
| 28 |
|
| 29 |
|
| 30 |
using namespace Gui; |
| 31 |
|
| 32 |
SelectionFilterPy::SelectionFilterPy(Py::PythonClassInstance* self, Py::Tuple& args, Py::Dict& kwds) |
| 33 |
: Py::PythonClass<SelectionFilterPy>::PythonClass(self, args, kwds) |
| 34 |
, filter("") |
| 35 |
{ |
| 36 |
const char* str; |
| 37 |
if (!PyArg_ParseTuple(args.ptr(), "s", &str)) { |
| 38 |
throw Py::Exception(); |
| 39 |
} |
| 40 |
try { |
| 41 |
filter = SelectionFilter(str); |
| 42 |
} |
| 43 |
catch (const Base::Exception& e) { |
| 44 |
throw Py::Exception(PyExc_SyntaxError, e.what()); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
SelectionFilterPy::~SelectionFilterPy() = default; |
| 49 |
|
| 50 |
Py::Object SelectionFilterPy::repr() |
| 51 |
{ |
| 52 |
return Py::String("SelectionFilter"); |
| 53 |
} |
| 54 |
|
| 55 |
Py::Object SelectionFilterPy::match() |
| 56 |
{ |
| 57 |
return Py::Boolean(filter.match()); |
| 58 |
} |
| 59 |
PYCXX_NOARGS_METHOD_DECL(SelectionFilterPy, match) |
| 60 |
|
| 61 |
Py::Object SelectionFilterPy::test(const Py::Tuple& args) |
| 62 |
{ |
| 63 |
PyObject* pcObj; |
| 64 |
char* text = nullptr; |
| 65 |
if (!PyArg_ParseTuple(args.ptr(), "O!|s", &(App::DocumentObjectPy::Type), &pcObj, &text)) { |
| 66 |
throw Py::Exception(); |
| 67 |
} |
| 68 |
|
| 69 |
auto docObj = static_cast<App::DocumentObjectPy*>(pcObj); |
| 70 |
|
| 71 |
return Py::Boolean(filter.test(docObj->getDocumentObjectPtr(), text)); |
| 72 |
} |
| 73 |
PYCXX_VARARGS_METHOD_DECL(SelectionFilterPy, test) |
| 74 |
|
| 75 |
Py::Object SelectionFilterPy::result() |
| 76 |
{ |
| 77 |
Py::List list; |
| 78 |
for (const auto& vec : filter.Result) { |
| 79 |
Py::Tuple tuple(vec.size()); |
| 80 |
int index = 0; |
| 81 |
for (auto sel : vec) { |
| 82 |
tuple[index++] = Py::asObject(sel.getPyObject()); |
| 83 |
} |
| 84 |
list.append(tuple); |
| 85 |
} |
| 86 |
|
| 87 |
return list; |
| 88 |
} |
| 89 |
PYCXX_NOARGS_METHOD_DECL(SelectionFilterPy, result) |
| 90 |
|
| 91 |
Py::Object SelectionFilterPy::setFilter(const Py::Tuple& args) |
| 92 |
{ |
| 93 |
char* text = nullptr; |
| 94 |
if (!PyArg_ParseTuple(args.ptr(), "s", &text)) { |
| 95 |
throw Py::Exception(); |
| 96 |
} |
| 97 |
|
| 98 |
try { |
| 99 |
filter.setFilter(text); |
| 100 |
return Py::None(); |
| 101 |
} |
| 102 |
catch (const Base::Exception& e) { |
| 103 |
throw Py::Exception(PyExc_SyntaxError, e.what()); |
| 104 |
} |
| 105 |
} |
| 106 |
PYCXX_VARARGS_METHOD_DECL(SelectionFilterPy, setFilter) |
| 107 |
|
| 108 |
Py::Object SelectionFilterPy::getFilter() |
| 109 |
{ |
| 110 |
return Py::String(filter.getFilter()); |
| 111 |
} |
| 112 |
PYCXX_NOARGS_METHOD_DECL(SelectionFilterPy, getFilter) |
| 113 |
|
| 114 |
void SelectionFilterPy::init_type() |
| 115 |
{ |
| 116 |
behaviors().name("Gui.SelectionFilter"); |
| 117 |
behaviors().doc( |
| 118 |
"Filter for certain selection\n" |
| 119 |
"Example strings are:\n" |
| 120 |
"\"SELECT Part::Feature SUBELEMENT Edge\",\n" |
| 121 |
"\"SELECT Part::Feature\", \n" |
| 122 |
"\"SELECT Part::Feature COUNT 1..5\"\n" |
| 123 |
); |
| 124 |
// you must have overwritten the virtual functions |
| 125 |
behaviors().supportRepr(); |
| 126 |
behaviors().supportGetattro(); |
| 127 |
behaviors().supportSetattro(); |
| 128 |
PYCXX_ADD_NOARGS_METHOD(match, match, "Check if the current selection matches the filter"); |
| 129 |
PYCXX_ADD_NOARGS_METHOD( |
| 130 |
result, |
| 131 |
result, |
| 132 |
"If match() returns True then with result() you get a list of the matching objects" |
| 133 |
); |
| 134 |
PYCXX_ADD_VARARGS_METHOD( |
| 135 |
test, |
| 136 |
test, |
| 137 |
"test(Feature, SubName='')\n" |
| 138 |
"Test if a given object is described in the filter.\n" |
| 139 |
"If SubName is not empty the sub-element gets also tested." |
| 140 |
); |
| 141 |
PYCXX_ADD_VARARGS_METHOD(setFilter, setFilter, "Set a new selection filter from a string"); |
| 142 |
PYCXX_ADD_NOARGS_METHOD(getFilter, getFilter, "Get the selection filter string"); |
| 143 |
|
| 144 |
behaviors().readyType(); |
| 145 |
} |