2
* ***** BEGIN GPL LICENSE BLOCK *****
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
* ***** END GPL LICENSE BLOCK *****
21
/** \file source/blender/freestyle/intern/python/BPy_ViewShape.cpp
25
#include "BPy_ViewShape.h"
27
#include "BPy_Convert.h"
28
#include "Interface0D/BPy_ViewVertex.h"
29
#include "Interface1D/BPy_ViewEdge.h"
30
#include "BPy_SShape.h"
36
///////////////////////////////////////////////////////////////////////////////////////////
38
//-------------------MODULE INITIALIZATION--------------------------------
40
int ViewShape_Init(PyObject *module)
45
if (PyType_Ready(&ViewShape_Type) < 0)
47
Py_INCREF(&ViewShape_Type);
48
PyModule_AddObject(module, "ViewShape", (PyObject *)&ViewShape_Type);
53
/*----------------------ViewShape methods ----------------------------*/
55
PyDoc_STRVAR(ViewShape_doc,
56
"Class gathering the elements of the ViewMap (i.e., :class:`ViewVertex`\n"
57
"and :class:`ViewEdge`) that are issued from the same input shape.\n"
59
".. method:: __init__()\n"
61
" Default constructor.\n"
63
".. method:: __init__(brother)\n"
65
" Copy constructor.\n"
67
" :arg brother: A ViewShape object.\n"
68
" :type brother: :class:`ViewShape`\n"
70
".. method:: __init__(sshape)\n"
72
" Builds a ViewShape from an SShape.\n"
74
" :arg sshape: An SShape object.\n"
75
" :type sshape: :class:`SShape`");
77
static int ViewShape_init(BPy_ViewShape *self, PyObject *args, PyObject *kwds)
79
static const char *kwlist_1[] = {"brother", NULL};
80
static const char *kwlist_2[] = {"sshape", NULL};
83
if (PyArg_ParseTupleAndKeywords(args, kwds, "|O!", (char **)kwlist_1, &ViewShape_Type, &obj)) {
85
self->vs = new ViewShape();
89
self->vs = new ViewShape(*(((BPy_ViewShape *)obj)->vs));
90
self->py_ss = ((BPy_ViewShape *)obj)->py_ss;
93
else if (PyErr_Clear(),
94
PyArg_ParseTupleAndKeywords(args, kwds, "O!", (char **)kwlist_2, &SShape_Type, &obj))
96
BPy_SShape *py_ss = (BPy_SShape *)obj;
97
self->vs = new ViewShape(py_ss->ss);
98
self->py_ss = (!py_ss->borrowed) ? py_ss : NULL;
101
PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
105
Py_XINCREF(self->py_ss);
109
static void ViewShape_dealloc(BPy_ViewShape *self)
112
self->vs->setSShape((SShape *)NULL);
113
Py_DECREF(self->py_ss);
115
if (self->vs && !self->borrowed)
117
Py_TYPE(self)->tp_free((PyObject *)self);
120
static PyObject *ViewShape_repr(BPy_ViewShape *self)
122
return PyUnicode_FromFormat("ViewShape - address: %p", self->vs);
125
PyDoc_STRVAR(ViewShape_add_edge_doc,
126
".. method:: add_edge(edge)\n"
128
" Adds a ViewEdge to the list of ViewEdge objects.\n"
130
" :arg edge: A ViewEdge object.\n"
131
" :type edge: :class:`ViewEdge`\n");
133
static PyObject *ViewShape_add_edge(BPy_ViewShape *self, PyObject *args, PyObject *kwds)
135
static const char *kwlist[] = {"edge", NULL};
138
if (PyArg_ParseTupleAndKeywords(args, kwds, "O!", (char **)kwlist, &ViewEdge_Type, &py_ve))
140
self->vs->AddEdge(((BPy_ViewEdge *)py_ve)->ve);
144
PyDoc_STRVAR(ViewShape_add_vertex_doc,
145
".. method:: add_vertex(vertex)\n"
147
" Adds a ViewVertex to the list of the ViewVertex objects.\n"
149
" :arg vertex: A ViewVertex object.\n"
150
" :type vertex: :class:`ViewVertex`");
152
static PyObject *ViewShape_add_vertex(BPy_ViewShape *self, PyObject *args, PyObject *kwds)
154
static const char *kwlist[] = {"vertex", NULL};
157
if (PyArg_ParseTupleAndKeywords(args, kwds, "O!", (char **)kwlist, &ViewVertex_Type, &py_vv))
159
self->vs->AddVertex(((BPy_ViewVertex *)py_vv)->vv);
163
// virtual ViewShape *duplicate()
165
static PyMethodDef BPy_ViewShape_methods[] = {
166
{"add_edge", (PyCFunction)ViewShape_add_edge, METH_VARARGS | METH_KEYWORDS, ViewShape_add_edge_doc},
167
{"add_vertex", (PyCFunction)ViewShape_add_vertex, METH_VARARGS | METH_KEYWORDS, ViewShape_add_vertex_doc},
168
{NULL, NULL, 0, NULL}
171
/*----------------------ViewShape get/setters ----------------------------*/
173
PyDoc_STRVAR(ViewShape_sshape_doc,
174
"The SShape on top of which this ViewShape is built.\n"
176
":type: :class:`SShape`");
178
static PyObject *ViewShape_sshape_get(BPy_ViewShape *self, void *UNUSED(closure))
180
SShape *ss = self->vs->sshape();
183
return BPy_SShape_from_SShape(*ss);
186
static int ViewShape_sshape_set(BPy_ViewShape *self, PyObject *value, void *UNUSED(closure))
188
if (!BPy_SShape_Check(value)) {
189
PyErr_SetString(PyExc_TypeError, "value must be an SShape");
192
BPy_SShape *py_ss = (BPy_SShape *)value;
193
self->vs->setSShape(py_ss->ss);
195
Py_DECREF(self->py_ss);
196
if (!py_ss->borrowed) {
198
Py_INCREF(self->py_ss);
203
PyDoc_STRVAR(ViewShape_vertices_doc,
204
"The list of ViewVertex objects contained in this ViewShape.\n"
206
":type: List of :class:`ViewVertex` objects");
208
static PyObject *ViewShape_vertices_get(BPy_ViewShape *self, void *UNUSED(closure))
210
PyObject *py_vertices = PyList_New(0);
212
vector<ViewVertex *> vertices = self->vs->vertices();
213
vector<ViewVertex *>::iterator it;
214
for (it = vertices.begin(); it != vertices.end(); it++) {
215
PyList_Append( py_vertices, Any_BPy_ViewVertex_from_ViewVertex(*(*it)));
220
static int ViewShape_vertices_set(BPy_ViewShape *self, PyObject *value, void *UNUSED(closure))
224
vector< ViewVertex *> v;
226
if (!PyList_Check(value)) {
227
PyErr_SetString(PyExc_TypeError, "value must be a list of ViewVertex objects");
230
for (int i = 0; i < PyList_Size(list); i++) {
231
item = PyList_GetItem(list, i);
232
if (BPy_ViewVertex_Check(item)) {
233
v.push_back(((BPy_ViewVertex *)item)->vv);
236
PyErr_SetString(PyExc_TypeError, "value must be a list of ViewVertex objects");
240
self->vs->setVertices(v);
244
PyDoc_STRVAR(ViewShape_edges_doc,
245
"The list of ViewEdge objects contained in this ViewShape.\n"
247
":type: List of :class:`ViewEdge` objects");
249
static PyObject *ViewShape_edges_get(BPy_ViewShape *self, void *UNUSED(closure))
251
PyObject *py_edges = PyList_New(0);
253
vector<ViewEdge *> edges = self->vs->edges();
254
vector<ViewEdge *>::iterator it;
256
for (it = edges.begin(); it != edges.end(); it++) {
257
PyList_Append(py_edges, BPy_ViewEdge_from_ViewEdge(*(*it)));
262
static int ViewShape_edges_set(BPy_ViewShape *self, PyObject *value, void *UNUSED(closure))
266
vector<ViewEdge *> v;
268
if (!PyList_Check(value)) {
269
PyErr_SetString(PyExc_TypeError, "value must be a list of ViewEdge objects");
272
for (int i = 0; i < PyList_Size(list); i++) {
273
item = PyList_GetItem(list, i);
274
if (BPy_ViewEdge_Check(item)) {
275
v.push_back(((BPy_ViewEdge *)item)->ve);
278
PyErr_SetString(PyExc_TypeError, "argument must be list of ViewEdge objects");
282
self->vs->setEdges(v);
286
PyDoc_STRVAR(ViewShape_name_doc,
287
"The name of the ViewShape.\n"
291
static PyObject *ViewShape_name_get(BPy_ViewShape *self, void *UNUSED(closure))
293
return PyUnicode_FromString(self->vs->getName().c_str());
296
PyDoc_STRVAR(ViewShape_id_doc,
297
"The Id of this ViewShape.\n"
299
":type: :class:`Id`");
301
static PyObject *ViewShape_id_get(BPy_ViewShape *self, void *UNUSED(closure))
303
Id id(self->vs->getId());
304
return BPy_Id_from_Id(id); // return a copy
307
static PyGetSetDef BPy_ViewShape_getseters[] = {
308
{(char *)"sshape", (getter)ViewShape_sshape_get, (setter)ViewShape_sshape_set, (char *)ViewShape_sshape_doc, NULL},
309
{(char *)"vertices", (getter)ViewShape_vertices_get, (setter)ViewShape_vertices_set,
310
(char *)ViewShape_vertices_doc, NULL},
311
{(char *)"edges", (getter)ViewShape_edges_get, (setter)ViewShape_edges_set, (char *)ViewShape_edges_doc, NULL},
312
{(char *)"name", (getter)ViewShape_name_get, (setter)NULL, (char *)ViewShape_name_doc, NULL},
313
{(char *)"id", (getter)ViewShape_id_get, (setter)NULL, (char *)ViewShape_id_doc, NULL},
314
{NULL, NULL, NULL, NULL, NULL} /* Sentinel */
317
/*-----------------------BPy_ViewShape type definition ------------------------------*/
319
PyTypeObject ViewShape_Type = {
320
PyVarObject_HEAD_INIT(NULL, 0)
321
"ViewShape", /* tp_name */
322
sizeof(BPy_ViewShape), /* tp_basicsize */
324
(destructor)ViewShape_dealloc, /* tp_dealloc */
329
(reprfunc)ViewShape_repr, /* tp_repr */
330
0, /* tp_as_number */
331
0, /* tp_as_sequence */
332
0, /* tp_as_mapping */
338
0, /* tp_as_buffer */
339
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
340
ViewShape_doc, /* tp_doc */
343
0, /* tp_richcompare */
344
0, /* tp_weaklistoffset */
347
BPy_ViewShape_methods, /* tp_methods */
349
BPy_ViewShape_getseters, /* tp_getset */
352
0, /* tp_descr_get */
353
0, /* tp_descr_set */
354
0, /* tp_dictoffset */
355
(initproc)ViewShape_init, /* tp_init */
357
PyType_GenericNew, /* tp_new */
360
///////////////////////////////////////////////////////////////////////////////////////////