FreeCAD

Форк
0
/
Array2DPyImp.cpp 
147 строк · 4.3 Кб
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 "Array2DPy.h"
33
#include "Exceptions.h"
34
#include "Model.h"
35
#include "ModelLibrary.h"
36
#include "ModelPropertyPy.h"
37
#include "ModelUuids.h"
38

39
#include "Array2DPy.cpp"
40

41
using namespace Materials;
42

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

49
    return str.str();
50
}
51

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

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

64
Py::List Array2DPy::getArray() const
65
{
66
    Py::List list;
67

68
    auto array = getMaterial2DArrayPtr()->getArray();
69

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

78
        list.append(rowList);
79
    }
80

81
    return list;
82
}
83

84
Py::Int Array2DPy::getRows() const
85
{
86
    return Py::Int(getMaterial2DArrayPtr()->rows());
87
}
88

89
Py::Int Array2DPy::getColumns() const
90
{
91
    return Py::Int(getMaterial2DArrayPtr()->columns());
92
}
93

94
PyObject* Array2DPy::getRow(PyObject* args)
95
{
96
    int row;
97
    if (!PyArg_ParseTuple(args, "i", &row)) {
98
        return nullptr;
99
    }
100

101
    try {
102
        Py::List list;
103

104
        auto arrayRow = getMaterial2DArrayPtr()->getRow(row);
105
        for (auto& column : *arrayRow) {
106
            auto quantity =
107
                new Base::QuantityPy(new Base::Quantity(column.value<Base::Quantity>()));
108
            list.append(Py::asObject(quantity));
109
        }
110

111
        return Py::new_reference_to(list);
112
    }
113
    catch (const InvalidIndex&) {
114
    }
115

116
    PyErr_SetString(PyExc_IndexError, "Invalid array index");
117
    return nullptr;
118
}
119

120
PyObject* Array2DPy::getValue(PyObject* args)
121
{
122
    int row;
123
    int column;
124
    if (!PyArg_ParseTuple(args, "ii", &row, &column)) {
125
        return nullptr;
126
    }
127

128
    try {
129
        auto value = getMaterial2DArrayPtr()->getValue(row, column);
130
        return new Base::QuantityPy(new Base::Quantity(value.value<Base::Quantity>()));
131
    }
132
    catch (const InvalidIndex&) {
133
    }
134

135
    PyErr_SetString(PyExc_IndexError, "Invalid array index");
136
    return nullptr;
137
}
138

139
PyObject* Array2DPy::getCustomAttributes(const char* /*attr*/) const
140
{
141
    return nullptr;
142
}
143

144
int Array2DPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
145
{
146
    return 0;
147
}
148

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

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

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

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