1
/***************************************************************************
2
* Copyright (c) 2008 Juergen Riegel <juergen.riegel@web.de> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
#include <boost/math/special_functions/fpclassify.hpp>
28
#include <Base/Builder3D.h>
29
#include <Base/Converter.h>
30
#include <Base/GeometryPyCXX.h>
31
#include <Base/VectorPy.h>
34
// inclusion of the generated files (generated out of PointsPy.xml)
36
#include "PointsPy.cpp"
39
using namespace Points;
41
// returns a string which represents the object e.g. when printed in python
42
std::string PointsPy::representation() const
44
return {"<PointKernel object>"};
47
PyObject* PointsPy::PyMake(struct _typeobject*, PyObject*, PyObject*)
49
// create a new instance of PointsPy and the Twin object
50
return new PointsPy(new PointKernel);
54
int PointsPy::PyInit(PyObject* args, PyObject* /*kwd*/)
56
PyObject* pcObj = nullptr;
57
if (!PyArg_ParseTuple(args, "|O", &pcObj)) {
61
// if no mesh is given
65
if (PyObject_TypeCheck(pcObj, &(PointsPy::Type))) {
66
*getPointKernelPtr() = *(static_cast<PointsPy*>(pcObj)->getPointKernelPtr());
68
else if (PyList_Check(pcObj)) {
69
if (!addPoints(args)) {
73
else if (PyTuple_Check(pcObj)) {
74
if (!addPoints(args)) {
78
else if (PyUnicode_Check(pcObj)) {
79
getPointKernelPtr()->load(PyUnicode_AsUTF8(pcObj));
82
PyErr_SetString(PyExc_TypeError, "optional argument must be list, tuple or string");
89
PyObject* PointsPy::copy(PyObject* args)
91
if (!PyArg_ParseTuple(args, "")) {
95
PointKernel* kernel = new PointKernel();
97
*kernel = *getPointKernelPtr();
98
return new PointsPy(kernel);
101
PyObject* PointsPy::read(PyObject* args)
104
if (!PyArg_ParseTuple(args, "s", &Name)) {
110
getPointKernelPtr()->load(Name);
117
PyObject* PointsPy::write(PyObject* args)
120
if (!PyArg_ParseTuple(args, "s", &Name)) {
126
getPointKernelPtr()->save(Name);
133
PyObject* PointsPy::writeInventor(PyObject* args)
135
if (!PyArg_ParseTuple(args, "")) {
139
std::stringstream result;
140
Base::InventorBuilder builder(result);
141
builder.beginSeparator();
142
std::vector<Base::Vector3f> points;
143
PointKernel* kernel = getPointKernelPtr();
144
points.reserve(kernel->size());
145
for (const auto& it : *kernel) {
146
points.push_back(Base::convertTo<Base::Vector3f>(it));
148
builder.addNode(Base::Coordinate3Item {points});
149
builder.addNode(Base::PointSetItem {});
150
builder.endSeparator();
152
return Py::new_reference_to(Py::String(result.str()));
155
PyObject* PointsPy::addPoints(PyObject* args)
158
if (!PyArg_ParseTuple(args, "O", &obj)) {
163
Py::Sequence list(obj);
164
Py::Type vType(Base::getTypeAsObject(&Base::VectorPy::Type));
166
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
167
if ((*it).isType(vType)) {
169
getPointKernelPtr()->push_back(p.toVector());
173
Py::Tuple tuple(*it);
174
pnt.x = (double)Py::Float(tuple[0]);
175
pnt.y = (double)Py::Float(tuple[1]);
176
pnt.z = (double)Py::Float(tuple[2]);
177
getPointKernelPtr()->push_back(pnt);
181
catch (const Py::Exception&) {
182
PyErr_SetString(PyExc_TypeError,
192
PyObject* PointsPy::fromSegment(PyObject* args)
195
if (!PyArg_ParseTuple(args, "O", &obj)) {
200
const PointKernel* points = getPointKernelPtr();
201
Py::Sequence list(obj);
202
std::unique_ptr<PointKernel> pts(new PointKernel());
203
pts->reserve(list.size());
204
int numPoints = static_cast<int>(points->size());
205
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
206
long index = static_cast<long>(Py::Long(*it));
207
if (index >= 0 && index < numPoints) {
208
pts->push_back(points->getPoint(index));
212
return new PointsPy(pts.release());
214
catch (const Py::Exception&) {
215
PyErr_SetString(PyExc_TypeError, "expect a list of int");
220
PyObject* PointsPy::fromValid(PyObject* args)
222
if (!PyArg_ParseTuple(args, "")) {
227
const PointKernel* points = getPointKernelPtr();
228
std::unique_ptr<PointKernel> pts(new PointKernel());
229
pts->reserve(points->size());
230
for (const auto& point : *points) {
231
if (!boost::math::isnan(point.x) && !boost::math::isnan(point.y)
232
&& !boost::math::isnan(point.z)) {
233
pts->push_back(point);
237
return new PointsPy(pts.release());
239
catch (const Py::Exception&) {
240
PyErr_SetString(PyExc_TypeError, "expect a list of int");
245
Py::Long PointsPy::getCountPoints() const
247
return Py::Long((long)getPointKernelPtr()->size());
250
Py::List PointsPy::getPoints() const
253
const PointKernel* points = getPointKernelPtr();
254
for (const auto& point : *points) {
255
PointList.append(Py::asObject(new Base::VectorPy(point)));
260
PyObject* PointsPy::getCustomAttributes(const char* /*attr*/) const
265
int PointsPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)