1
/***************************************************************************
2
* Copyright (c) 2023 David Carter <dcarter@david.carter.ca> *
4
* This file is part of FreeCAD. *
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. *
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. *
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/>. *
20
**************************************************************************/
22
#include "PreCompiled.h"
27
#include <Base/Quantity.h>
28
#include <Base/QuantityPy.h>
29
#include <CXX/Objects.hxx>
30
#include <Gui/MetaTypes.h>
33
#include "Exceptions.h"
35
#include "ModelLibrary.h"
36
#include "ModelPropertyPy.h"
37
#include "ModelUuids.h"
39
#include "Array2DPy.cpp"
41
using namespace Materials;
43
// returns a string which represents the object e.g. when printed in python
44
std::string Array2DPy::representation() const
46
std::stringstream str;
47
str << "<Array2D object at " << getMaterial2DArrayPtr() << ">";
52
PyObject* Array2DPy::PyMake(struct _typeobject*, PyObject*, PyObject*) // Python wrapper
54
// never create such objects with the constructor
55
return new Array2DPy(new Material2DArray());
59
int Array2DPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
64
Py::List Array2DPy::getArray() const
68
auto array = getMaterial2DArrayPtr()->getArray();
70
for (auto& row : array) {
72
for (auto& column : *row) {
74
new Base::QuantityPy(new Base::Quantity(column.value<Base::Quantity>()));
75
rowList.append(Py::asObject(quantity));
84
Py::Int Array2DPy::getRows() const
86
return Py::Int(getMaterial2DArrayPtr()->rows());
89
Py::Int Array2DPy::getColumns() const
91
return Py::Int(getMaterial2DArrayPtr()->columns());
94
PyObject* Array2DPy::getRow(PyObject* args)
97
if (!PyArg_ParseTuple(args, "i", &row)) {
104
auto arrayRow = getMaterial2DArrayPtr()->getRow(row);
105
for (auto& column : *arrayRow) {
107
new Base::QuantityPy(new Base::Quantity(column.value<Base::Quantity>()));
108
list.append(Py::asObject(quantity));
111
return Py::new_reference_to(list);
113
catch (const InvalidIndex&) {
116
PyErr_SetString(PyExc_IndexError, "Invalid array index");
120
PyObject* Array2DPy::getValue(PyObject* args)
124
if (!PyArg_ParseTuple(args, "ii", &row, &column)) {
129
auto value = getMaterial2DArrayPtr()->getValue(row, column);
130
return new Base::QuantityPy(new Base::Quantity(value.value<Base::Quantity>()));
132
catch (const InvalidIndex&) {
135
PyErr_SetString(PyExc_IndexError, "Invalid array index");
139
PyObject* Array2DPy::getCustomAttributes(const char* /*attr*/) const
144
int Array2DPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)