File size: 7,864 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
236
237
238
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
 *   Copyright (c) 2014 Yorik van Havre <yorik@uncreated.net>              *
 *                                                                         *
 *   This file is part of the FreeCAD CAx development system.              *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License as published by the Free Software Foundation; either          *
 *   version 2 of the License, or (at your option) any later version.      *
 *                                                                         *
 *   This library  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 Library General Public License for more details.                  *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; see the file COPYING.LIB. If not,    *
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 *                                                                         *
 ***************************************************************************/


#include "Base/GeometryPyCXX.h"

// inclusion of the generated files (generated out of PathPy.xml)
#include "PathPy.h"
#include "PathPy.cpp"

#include "CommandPy.h"


using namespace Path;

// returns a string which represents the object e.g. when printed in python
std::string PathPy::representation() const
{
    std::stringstream str;
    str.precision(5);
    str << "Path [ ";
    str << "size:" << getToolpathPtr()->getSize() << " ";
    str << "length:" << getToolpathPtr()->getLength();
    str << " ]";

    return str.str();
}

PyObject* PathPy::PyMake(struct _typeobject*, PyObject*, PyObject*)  // Python wrapper
{
    // create a new instance of PathPy and the Twin object
    return new PathPy(new Toolpath);
}

// constructor method
int PathPy::PyInit(PyObject* args, PyObject* /*kwd*/)
{
    PyObject* pcObj = nullptr;
    char* gcode;
    if (PyArg_ParseTuple(args, "|O!", &(PyList_Type), &pcObj)) {
        if (pcObj) {
            Py::List list(pcObj);
            for (Py::List::iterator it = list.begin(); it != list.end(); ++it) {
                if (PyObject_TypeCheck((*it).ptr(), &(Path::CommandPy::Type))) {
                    Path::Command& cmd = *static_cast<Path::CommandPy*>((*it).ptr())->getCommandPtr();
                    getToolpathPtr()->addCommand(cmd);
                }
                else {
                    PyErr_SetString(PyExc_TypeError, "The list must contain only Path Commands");
                    return -1;
                }
            }
        }
        return 0;
    }
    PyErr_Clear();  // set by PyArg_ParseTuple()
    if (PyArg_ParseTuple(args, "|s", &gcode)) {
        getToolpathPtr()->setFromGCode(gcode);
        return 0;
    }
    PyErr_SetString(PyExc_TypeError, "Argument must be a list of commands or a gcode string");
    return -1;
}


// Commands get/set

Py::List PathPy::getCommands() const
{
    Py::List list;
    for (unsigned int i = 0; i < getToolpathPtr()->getSize(); i++) {
        list.append(
            Py::asObject(new Path::CommandPy(new Path::Command(getToolpathPtr()->getCommand(i))))
        );
    }
    return list;
}

void PathPy::setCommands(Py::List list)
{
    getToolpathPtr()->clear();
    for (Py::List::iterator it = list.begin(); it != list.end(); ++it) {
        if (PyObject_TypeCheck((*it).ptr(), &(Path::CommandPy::Type))) {
            Path::Command& cmd = *static_cast<Path::CommandPy*>((*it).ptr())->getCommandPtr();
            getToolpathPtr()->addCommand(cmd);
        }
        else {
            throw Py::TypeError("The list can only contain Path Commands");
        }
    }
}

Py::Object PathPy::getCenter() const
{
    return Py::Vector(getToolpathPtr()->getCenter());
}

void PathPy::setCenter(Py::Object obj)
{
    getToolpathPtr()->setCenter(Py::Vector(obj).toVector());
}

// read-only attributes

Py::Float PathPy::getLength() const
{
    return Py::Float(getToolpathPtr()->getLength());
}

Py::Long PathPy::getSize() const
{
    return Py::Long((long)getToolpathPtr()->getSize());
}

Py::Object PathPy::getBoundBox() const
{
    return Py::BoundingBox(getToolpathPtr()->getBoundBox());
}

// specific methods

PyObject* PathPy::copy(PyObject* args) const
{
    if (PyArg_ParseTuple(args, "")) {
        return new PathPy(new Path::Toolpath(*getToolpathPtr()));
    }
    throw Py::TypeError("This method accepts no argument");
}

PyObject* PathPy::addCommands(PyObject* args)
{
    PyObject* o;
    if (PyArg_ParseTuple(args, "O!", &(Path::CommandPy::Type), &o)) {
        Path::Command& cmd = *static_cast<Path::CommandPy*>(o)->getCommandPtr();
        getToolpathPtr()->addCommand(cmd);
        return new PathPy(new Path::Toolpath(*getToolpathPtr()));
        // Py_Return;
    }
    PyErr_Clear();
    if (PyArg_ParseTuple(args, "O!", &(PyList_Type), &o)) {
        Py::List list(o);
        for (Py::List::iterator it = list.begin(); it != list.end(); ++it) {
            if (PyObject_TypeCheck((*it).ptr(), &(Path::CommandPy::Type))) {
                Path::Command& cmd = *static_cast<Path::CommandPy*>((*it).ptr())->getCommandPtr();
                getToolpathPtr()->addCommand(cmd);
            }
        }
        return new PathPy(new Path::Toolpath(*getToolpathPtr()));
    }
    Py_Error(PyExc_TypeError, "Wrong parameters - command or list of commands expected");
}

PyObject* PathPy::insertCommand(PyObject* args)
{
    PyObject* o;
    int pos = -1;
    if (PyArg_ParseTuple(args, "O!|i", &(Path::CommandPy::Type), &o, &pos)) {
        Path::Command& cmd = *static_cast<Path::CommandPy*>(o)->getCommandPtr();
        getToolpathPtr()->insertCommand(cmd, pos);
        return new PathPy(new Path::Toolpath(*getToolpathPtr()));
    }
    Py_Error(PyExc_TypeError, "Wrong parameters - expected command and optional integer");
}

PyObject* PathPy::deleteCommand(PyObject* args)
{
    int pos = -1;
    if (PyArg_ParseTuple(args, "|i", &pos)) {
        getToolpathPtr()->deleteCommand(pos);
        return new PathPy(new Path::Toolpath(*getToolpathPtr()));
    }
    Py_Error(PyExc_TypeError, "Wrong parameters - expected an integer (optional)");
}

PyObject* PathPy::getCycleTime(PyObject* args) const
{
    double hFeed, vFeed, hRapid, vRapid;
    if (PyArg_ParseTuple(args, "dddd", &hFeed, &vFeed, &hRapid, &vRapid)) {
        return PyFloat_FromDouble(getToolpathPtr()->getCycleTime(hFeed, vFeed, hRapid, vRapid));
    }
    return nullptr;
}

// GCode methods

PyObject* PathPy::toGCode(PyObject* args) const
{
    if (PyArg_ParseTuple(args, "")) {
        std::string result = getToolpathPtr()->toGCode();
        return PyUnicode_FromString(result.c_str());
    }
    throw Py::TypeError("This method accepts no argument");
}

PyObject* PathPy::setFromGCode(PyObject* args)
{
    char* pstr = nullptr;
    if (PyArg_ParseTuple(args, "s", &pstr)) {
        std::string gcode(pstr);
        getToolpathPtr()->setFromGCode(gcode);
        Py_INCREF(Py_None);
        return Py_None;
    }
    throw Py::TypeError("Argument must be a string");
}

// custom attributes get/set

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

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