FreeCAD

Форк
0
/
PointsPyImp.cpp 
268 строк · 7.9 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2008 Juergen Riegel <juergen.riegel@web.de>             *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
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.      *
10
 *                                                                         *
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.                  *
15
 *                                                                         *
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                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
#include <boost/math/special_functions/fpclassify.hpp>
26
#endif
27

28
#include <Base/Builder3D.h>
29
#include <Base/Converter.h>
30
#include <Base/GeometryPyCXX.h>
31
#include <Base/VectorPy.h>
32

33
#include "Points.h"
34
// inclusion of the generated files (generated out of PointsPy.xml)
35
#include "PointsPy.h"
36
#include "PointsPy.cpp"
37

38

39
using namespace Points;
40

41
// returns a string which represents the object e.g. when printed in python
42
std::string PointsPy::representation() const
43
{
44
    return {"<PointKernel object>"};
45
}
46

47
PyObject* PointsPy::PyMake(struct _typeobject*, PyObject*, PyObject*)
48
{
49
    // create a new instance of PointsPy and the Twin object
50
    return new PointsPy(new PointKernel);
51
}
52

53
// constructor method
54
int PointsPy::PyInit(PyObject* args, PyObject* /*kwd*/)
55
{
56
    PyObject* pcObj = nullptr;
57
    if (!PyArg_ParseTuple(args, "|O", &pcObj)) {
58
        return -1;
59
    }
60

61
    // if no mesh is given
62
    if (!pcObj) {
63
        return 0;
64
    }
65
    if (PyObject_TypeCheck(pcObj, &(PointsPy::Type))) {
66
        *getPointKernelPtr() = *(static_cast<PointsPy*>(pcObj)->getPointKernelPtr());
67
    }
68
    else if (PyList_Check(pcObj)) {
69
        if (!addPoints(args)) {
70
            return -1;
71
        }
72
    }
73
    else if (PyTuple_Check(pcObj)) {
74
        if (!addPoints(args)) {
75
            return -1;
76
        }
77
    }
78
    else if (PyUnicode_Check(pcObj)) {
79
        getPointKernelPtr()->load(PyUnicode_AsUTF8(pcObj));
80
    }
81
    else {
82
        PyErr_SetString(PyExc_TypeError, "optional argument must be list, tuple or string");
83
        return -1;
84
    }
85

86
    return 0;
87
}
88

89
PyObject* PointsPy::copy(PyObject* args)
90
{
91
    if (!PyArg_ParseTuple(args, "")) {
92
        return nullptr;
93
    }
94

95
    PointKernel* kernel = new PointKernel();
96
    // assign data
97
    *kernel = *getPointKernelPtr();
98
    return new PointsPy(kernel);
99
}
100

101
PyObject* PointsPy::read(PyObject* args)
102
{
103
    const char* Name {};
104
    if (!PyArg_ParseTuple(args, "s", &Name)) {
105
        return nullptr;
106
    }
107

108
    PY_TRY
109
    {
110
        getPointKernelPtr()->load(Name);
111
    }
112
    PY_CATCH;
113

114
    Py_Return;
115
}
116

117
PyObject* PointsPy::write(PyObject* args)
118
{
119
    const char* Name {};
120
    if (!PyArg_ParseTuple(args, "s", &Name)) {
121
        return nullptr;
122
    }
123

124
    PY_TRY
125
    {
126
        getPointKernelPtr()->save(Name);
127
    }
128
    PY_CATCH;
129

130
    Py_Return;
131
}
132

133
PyObject* PointsPy::writeInventor(PyObject* args)
134
{
135
    if (!PyArg_ParseTuple(args, "")) {
136
        return nullptr;
137
    }
138

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));
147
    }
148
    builder.addNode(Base::Coordinate3Item {points});
149
    builder.addNode(Base::PointSetItem {});
150
    builder.endSeparator();
151

152
    return Py::new_reference_to(Py::String(result.str()));
153
}
154

155
PyObject* PointsPy::addPoints(PyObject* args)
156
{
157
    PyObject* obj {};
158
    if (!PyArg_ParseTuple(args, "O", &obj)) {
159
        return nullptr;
160
    }
161

162
    try {
163
        Py::Sequence list(obj);
164
        Py::Type vType(Base::getTypeAsObject(&Base::VectorPy::Type));
165

166
        for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
167
            if ((*it).isType(vType)) {
168
                Py::Vector p(*it);
169
                getPointKernelPtr()->push_back(p.toVector());
170
            }
171
            else {
172
                Base::Vector3d pnt;
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);
178
            }
179
        }
180
    }
181
    catch (const Py::Exception&) {
182
        PyErr_SetString(PyExc_TypeError,
183
                        "either expect\n"
184
                        "-- [Vector,...] \n"
185
                        "-- [(x,y,z),...]");
186
        return nullptr;
187
    }
188

189
    Py_Return;
190
}
191

192
PyObject* PointsPy::fromSegment(PyObject* args)
193
{
194
    PyObject* obj {};
195
    if (!PyArg_ParseTuple(args, "O", &obj)) {
196
        return nullptr;
197
    }
198

199
    try {
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));
209
            }
210
        }
211

212
        return new PointsPy(pts.release());
213
    }
214
    catch (const Py::Exception&) {
215
        PyErr_SetString(PyExc_TypeError, "expect a list of int");
216
        return nullptr;
217
    }
218
}
219

220
PyObject* PointsPy::fromValid(PyObject* args)
221
{
222
    if (!PyArg_ParseTuple(args, "")) {
223
        return nullptr;
224
    }
225

226
    try {
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);
234
            }
235
        }
236

237
        return new PointsPy(pts.release());
238
    }
239
    catch (const Py::Exception&) {
240
        PyErr_SetString(PyExc_TypeError, "expect a list of int");
241
        return nullptr;
242
    }
243
}
244

245
Py::Long PointsPy::getCountPoints() const
246
{
247
    return Py::Long((long)getPointKernelPtr()->size());
248
}
249

250
Py::List PointsPy::getPoints() const
251
{
252
    Py::List PointList;
253
    const PointKernel* points = getPointKernelPtr();
254
    for (const auto& point : *points) {
255
        PointList.append(Py::asObject(new Base::VectorPy(point)));
256
    }
257
    return PointList;
258
}
259

260
PyObject* PointsPy::getCustomAttributes(const char* /*attr*/) const
261
{
262
    return nullptr;
263
}
264

265
int PointsPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
266
{
267
    return 0;
268
}
269

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.