FreeCAD

Форк
0
/
Array3DPyImp.cpp 
152 строки · 4.4 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2023 David Carter <dcarter@david.carter.ca>             *
3
 *                                                                         *
4
 *   This file is part of FreeCAD.                                         *
5
 *                                                                         *
6
 *   FreeCAD is free software: you can redistribute it and/or modify it    *
7
 *   under the terms of the GNU Lesser General Public License as           *
8
 *   published by the Free Software Foundation, either version 2.1 of the  *
9
 *   License, or (at your option) any later version.                       *
10
 *                                                                         *
11
 *   FreeCAD is distributed in the hope that it will be useful, but        *
12
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
14
 *   Lesser General Public License for more details.                       *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Lesser General Public      *
17
 *   License along with FreeCAD. If not, see                               *
18
 *   <https://www.gnu.org/licenses/>.                                      *
19
 *                                                                         *
20
 **************************************************************************/
21

22
#include "PreCompiled.h"
23

24
#include <QList>
25
#include <QMetaType>
26

27
#include <Base/Quantity.h>
28
#include <Base/QuantityPy.h>
29
#include <CXX/Objects.hxx>
30
#include <Gui/MetaTypes.h>
31

32
#include "Array3DPy.h"
33
#include "Exceptions.h"
34
#include "Model.h"
35
#include "ModelLibrary.h"
36
#include "ModelPropertyPy.h"
37
#include "ModelUuids.h"
38

39
#include "Array3DPy.cpp"
40

41
using namespace Materials;
42

43
// returns a string which represents the object e.g. when printed in python
44
std::string Array3DPy::representation() const
45
{
46
    std::stringstream str;
47
    str << "<Array3D object at " << getMaterial3DArrayPtr() << ">";
48

49
    return str.str();
50
}
51

52
PyObject* Array3DPy::PyMake(struct _typeobject*, PyObject*, PyObject*)  // Python wrapper
53
{
54
    // never create such objects with the constructor
55
    return new Array3DPy(new Material3DArray());
56
}
57

58
// constructor method
59
int Array3DPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
60
{
61
    return 0;
62
}
63

64
Py::List Array3DPy::getArray() const
65
{
66
    Py::List list;
67
    auto array = getMaterial3DArrayPtr()->getArray();
68

69
    for (auto& depth : array) {
70
        Py::List depthList;
71
        for (auto& row : *std::get<1>(depth)) {
72
            Py::List rowList;
73
            for (auto& column : *row) {
74
                auto quantity = new Base::QuantityPy(new Base::Quantity(column));
75
                rowList.append(Py::asObject(quantity));
76
            }
77

78
            depthList.append(rowList);
79
        }
80
        list.append(depthList);
81
    }
82

83
    return list;
84
}
85

86
Py::Int Array3DPy::getColumns() const
87
{
88
    return Py::Int(getMaterial3DArrayPtr()->columns());
89
}
90

91
Py::Int Array3DPy::getDepth() const
92
{
93
    return Py::Int(getMaterial3DArrayPtr()->depth());
94
}
95

96
PyObject* Array3DPy::getRows(PyObject* args)
97
{
98
    int depth = getMaterial3DArrayPtr()->currentDepth();
99
    if (!PyArg_ParseTuple(args, "|i", &depth)) {
100
        return nullptr;
101
    }
102

103
    return PyLong_FromLong(getMaterial3DArrayPtr()->rows(depth));
104
}
105

106
PyObject* Array3DPy::getValue(PyObject* args)
107
{
108
    int depth;
109
    int row;
110
    int column;
111
    if (!PyArg_ParseTuple(args, "iii", &depth, &row, &column)) {
112
        return nullptr;
113
    }
114

115
    try {
116
        auto value = getMaterial3DArrayPtr()->getValue(depth, row, column);
117
        return new Base::QuantityPy(new Base::Quantity(value));
118
    }
119
    catch (const InvalidIndex&) {
120
    }
121

122
    PyErr_SetString(PyExc_IndexError, "Invalid array index");
123
    return nullptr;
124
}
125

126
PyObject* Array3DPy::getDepthValue(PyObject* args)
127
{
128
    int depth;
129
    if (!PyArg_ParseTuple(args, "i", &depth)) {
130
        return nullptr;
131
    }
132

133
    try {
134
        auto value = getMaterial3DArrayPtr()->getDepthValue(depth);
135
        return new Base::QuantityPy(new Base::Quantity(value));
136
    }
137
    catch (const InvalidIndex&) {
138
    }
139

140
    PyErr_SetString(PyExc_IndexError, "Invalid array index");
141
    return nullptr;
142
}
143

144
PyObject* Array3DPy::getCustomAttributes(const char* /*attr*/) const
145
{
146
    return nullptr;
147
}
148

149
int Array3DPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
150
{
151
    return 0;
152
}
153

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

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

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

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