File size: 6,694 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************
 *   Copyright (c) 2023 David Carter <dcarter@david.carter.ca>             *
 *                                                                         *
 *   This file is part of FreeCAD.                                         *
 *                                                                         *
 *   FreeCAD is free software: you can redistribute it and/or modify it    *
 *   under the terms of the GNU Lesser General Public License as           *
 *   published by the Free Software Foundation, either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   FreeCAD is distributed in the hope that it will be useful, but        *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with FreeCAD. If not, see                               *
 *   <https://www.gnu.org/licenses/>.                                      *
 *                                                                         *
 **************************************************************************/


#include <QList>
#include <QMetaType>

#include <Base/Quantity.h>
#include <Base/QuantityPy.h>
#include <CXX/Objects.hxx>
#include <Gui/MetaTypes.h>

#include "Array3DPy.h"
#include "Exceptions.h"
#include "Model.h"
#include "ModelLibrary.h"
#include "ModelPropertyPy.h"
#include "ModelUuids.h"

#include "Array3DPy.cpp"

using namespace Materials;

// returns a string which represents the object e.g. when printed in python
std::string Array3DPy::representation() const
{
    std::stringstream str;
    str << "<Array3D object at " << getArray3DPtr() << ">";

    return str.str();
}

PyObject* Array3DPy::PyMake(struct _typeobject*, PyObject*, PyObject*)  // Python wrapper
{
    // never create such objects with the constructor
    return new Array3DPy(new Array3D());
}

// constructor method
int Array3DPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
{
    return 0;
}

Py::List Array3DPy::getArray() const
{
    Py::List list;
    auto array = getArray3DPtr()->getArray();

    for (auto& depth : array) {
        Py::List depthList;
        for (auto& row : *std::get<1>(depth)) {
            Py::List rowList;
            for (auto& column : *row) {
                auto quantity = new Base::QuantityPy(new Base::Quantity(column));
                rowList.append(Py::asObject(quantity));
            }

            depthList.append(rowList);
        }
        list.append(depthList);
    }

    return list;
}

Py::Long Array3DPy::getDimensions() const
{
    return Py::Long(3);
}

Py::Long Array3DPy::getColumns() const
{
    return Py::Long(getArray3DPtr()->columns());
}

void Array3DPy::setColumns(Py::Long arg)
{
    getArray3DPtr()->setColumns(arg);
}

Py::Long Array3DPy::getDepth() const
{
    return Py::Long(getArray3DPtr()->depth());
}

void Array3DPy::setDepth(Py::Long arg)
{
    getArray3DPtr()->setDepth(arg);
}

PyObject* Array3DPy::getRows(PyObject* args) const
{
    int depth = getArray3DPtr()->currentDepth();
    if (!PyArg_ParseTuple(args, "|i", &depth)) {
        return nullptr;
    }

    return PyLong_FromLong(getArray3DPtr()->rows(depth));
}

PyObject* Array3DPy::getValue(PyObject* args) const
{
    int depth;
    int row;
    int column;
    if (!PyArg_ParseTuple(args, "iii", &depth, &row, &column)) {
        return nullptr;
    }

    try {
        auto value = getArray3DPtr()->getValue(depth, row, column);
        return new Base::QuantityPy(new Base::Quantity(value));
    }
    catch (const InvalidIndex&) {
    }

    PyErr_SetString(PyExc_IndexError, "Invalid array index");
    return nullptr;
}

PyObject* Array3DPy::getDepthValue(PyObject* args) const
{
    int depth;
    if (!PyArg_ParseTuple(args, "i", &depth)) {
        return nullptr;
    }

    try {
        auto value = getArray3DPtr()->getDepthValue(depth);
        return new Base::QuantityPy(new Base::Quantity(value));
    }
    catch (const InvalidIndex&) {
    }

    PyErr_SetString(PyExc_IndexError, "Invalid array index");
    return nullptr;
}

PyObject* Array3DPy::setDepthValue(PyObject* args)
{
    int depth;
    PyObject* valueObj;
    if (PyArg_ParseTuple(args, "iO!", &depth, &PyUnicode_Type, &valueObj)) {
        Py::String item(valueObj);
        try {
            auto quantity = Base::Quantity::parse(item.as_string());
            quantity.setFormat(MaterialValue::getQuantityFormat());
            getArray3DPtr()->setDepthValue(depth, quantity);
        }
        catch (const Base::ParserError& e) {
            PyErr_SetString(PyExc_ValueError, e.what());
            return nullptr;
        }
        catch (const InvalidIndex& e) {
            PyErr_SetString(PyExc_IndexError, e.what());
            return nullptr;
        }
        Py_Return;
    }

    PyErr_SetString(PyExc_TypeError, "Expected (integer, string) arguments");
    return nullptr;
}

PyObject* Array3DPy::setValue(PyObject* args)
{
    int depth;
    int row;
    int column;
    PyObject* valueObj;
    if (PyArg_ParseTuple(args, "iiiO!", &depth, &row, &column, &PyUnicode_Type, &valueObj)) {
        Py::String item(valueObj);
        try {
            auto quantity = Base::Quantity::parse(item.as_string());
            quantity.setFormat(MaterialValue::getQuantityFormat());
            getArray3DPtr()->setValue(depth, row, column, quantity);
        }
        catch (const Base::ParserError& e) {
            PyErr_SetString(PyExc_ValueError, e.what());
            return nullptr;
        }
        catch (const InvalidIndex& e) {
            PyErr_SetString(PyExc_IndexError, e.what());
            return nullptr;
        }
        Py_Return;
    }

    PyErr_SetString(PyExc_TypeError, "Expected (integer, integer, integer, string) arguments");
    return nullptr;
}

PyObject* Array3DPy::setRows(PyObject* args)
{
    int depth;
    int rows;
    if (!PyArg_ParseTuple(args, "ii", &depth, &rows)) {
        return nullptr;
    }

    getArray3DPtr()->setRows(depth, rows);
    Py_Return;
}

PyObject* Array3DPy::getCustomAttributes(const char* /*attr*/) const
{
    return nullptr;
}

int Array3DPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
    return 0;
}