FreeCAD

Форк
0
/
MeshPointPyImp.cpp 
180 строк · 5.8 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2008 Jürgen 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 <sstream>
26
#endif
27

28
#include <Base/VectorPy.h>
29

30
#include "Mesh.h"
31
#include "MeshPoint.h"
32
// clang-format off
33
#include "MeshPointPy.h"
34
#include "MeshPointPy.cpp"
35
// clang-format on
36

37

38
using namespace Mesh;
39

40
// returns a string which represents the object e.g. when printed in python
41
std::string MeshPointPy::representation() const
42
{
43
    MeshPointPy::PointerType ptr = getMeshPointPtr();
44
    Base::Vector3d vec = *ptr;  // NOLINT
45

46
    std::stringstream str;
47
    str << "MeshPoint (";
48
    if (ptr->isBound()) {
49
        if (getMeshPointPtr()->Mesh->countPoints() <= getMeshPointPtr()->Index) {
50
            str << vec.x << ", " << vec.y << ", " << vec.z << ", Idx=" << ptr->Index
51
                << " (Out of range)";
52
        }
53
        else {
54
            vec = getMeshPointPtr()->Mesh->getPoint(getMeshPointPtr()->Index);
55
            str << vec.x << ", " << vec.y << ", " << vec.z << ", Idx=" << ptr->Index;
56
        }
57
    }
58
    else {
59
        str << vec.x << ", " << vec.y << ", " << vec.z;
60
    }
61

62
    str << ")";
63
    return str.str();
64
}
65

66
PyObject* MeshPointPy::PyMake(struct _typeobject*, PyObject*, PyObject*)  // Python wrapper
67
{
68
    // create a new instance of MeshPointPy and the Twin object
69
    return new MeshPointPy(new MeshPoint);
70
}
71

72
// constructor method
73
int MeshPointPy::PyInit(PyObject* args, PyObject* /*kwds*/)
74
{
75
    double x = 0.0, y = 0.0, z = 0.0;
76
    if (!PyArg_ParseTuple(args, "|ddd", &x, &y, &z)) {
77
        return -1;
78
    }
79

80
    getMeshPointPtr()->Set(x, y, z);
81
    return 0;
82
}
83

84
PyObject* MeshPointPy::unbound(PyObject* args)
85
{
86
    if (!PyArg_ParseTuple(args, "")) {
87
        return nullptr;
88
    }
89
    getMeshPointPtr()->Index = UINT_MAX;
90
    getMeshPointPtr()->Mesh = nullptr;
91
    Py_Return;
92
}
93

94
Py::Long MeshPointPy::getIndex() const
95
{
96
    return Py::Long((long)getMeshPointPtr()->Index);
97
}
98

99
Py::Boolean MeshPointPy::getBound() const
100
{
101
    return {getMeshPointPtr()->Index != UINT_MAX};
102
}
103

104
Py::Object MeshPointPy::getNormal() const
105
{
106
    if (!getMeshPointPtr()->isBound()) {
107
        throw Py::RuntimeError(
108
            "This object is not bound to a mesh, so no topological operation is possible!");
109
    }
110
    if (getMeshPointPtr()->Mesh->countPoints() <= getMeshPointPtr()->Index) {
111
        throw Py::IndexError("Index out of range");
112
    }
113

114
    Base::Vector3d* v =
115
        new Base::Vector3d(getMeshPointPtr()->Mesh->getPointNormal(getMeshPointPtr()->Index));
116
    Base::VectorPy* normal = new Base::VectorPy(v);
117
    normal->setConst();
118
    return Py::Object(normal, true);
119
}
120

121
Py::Object MeshPointPy::getVector() const
122
{
123
    MeshPointPy::PointerType ptr = static_cast<MeshPointPy::PointerType>(_pcTwinPointer);
124

125
    Base::VectorPy* vec = new Base::VectorPy(*ptr);
126
    vec->setConst();
127
    return Py::Object(vec, true);
128
}
129

130
Py::Float MeshPointPy::getx() const
131
{
132
    MeshPointPy::PointerType ptr = static_cast<MeshPointPy::PointerType>(_pcTwinPointer);
133
    double x = ptr->x;
134

135
    if (getMeshPointPtr()->isBound()) {
136
        if (getMeshPointPtr()->Mesh->countPoints() > getMeshPointPtr()->Index) {
137
            x = getMeshPointPtr()->Mesh->getPoint(getMeshPointPtr()->Index).x;
138
        }
139
    }
140

141
    return Py::Float(x);
142
}
143

144
Py::Float MeshPointPy::gety() const
145
{
146
    MeshPointPy::PointerType ptr = static_cast<MeshPointPy::PointerType>(_pcTwinPointer);
147
    double y = ptr->y;
148

149
    if (getMeshPointPtr()->isBound()) {
150
        if (getMeshPointPtr()->Mesh->countPoints() > getMeshPointPtr()->Index) {
151
            y = getMeshPointPtr()->Mesh->getPoint(getMeshPointPtr()->Index).y;
152
        }
153
    }
154

155
    return Py::Float(y);
156
}
157

158
Py::Float MeshPointPy::getz() const
159
{
160
    MeshPointPy::PointerType ptr = static_cast<MeshPointPy::PointerType>(_pcTwinPointer);
161
    double z = ptr->z;
162

163
    if (getMeshPointPtr()->isBound()) {
164
        if (getMeshPointPtr()->Mesh->countPoints() > getMeshPointPtr()->Index) {
165
            z = getMeshPointPtr()->Mesh->getPoint(getMeshPointPtr()->Index).z;
166
        }
167
    }
168

169
    return Py::Float(z);
170
}
171

172
PyObject* MeshPointPy::getCustomAttributes(const char* /*attr*/) const
173
{
174
    return nullptr;
175
}
176

177
int MeshPointPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
178
{
179
    return 0;
180
}
181

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

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

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

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