FreeCAD

Форк
0
/
Line2dSegmentPyImp.cpp 
311 строк · 11.6 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2016 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
# include <GCE2d_MakeSegment.hxx>
26
# include <Geom2d_Line.hxx>
27
# include <Geom2d_TrimmedCurve.hxx>
28
# include <gp_Lin2d.hxx>
29
#endif
30

31
#include <Base/GeometryPyCXX.h>
32

33
#include "Geom2d/Line2dSegmentPy.h"
34
#include "Geom2d/Line2dSegmentPy.cpp"
35
#include "Geom2d/Line2dPy.h"
36
#include "OCCError.h"
37

38

39
using namespace Part;
40

41
extern const char* gce_ErrorStatusText(gce_ErrorType et);
42

43
// returns a string which represents the object e.g. when printed in python
44
std::string Line2dSegmentPy::representation() const
45
{
46
    return "<Line2dSegment object>";
47
}
48

49
PyObject *Line2dSegmentPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
50
{
51
    // create a new instance of Line2dSegmentPy and the Twin object
52
    return new Line2dSegmentPy(new Geom2dLineSegment);
53
}
54

55
// constructor method
56
int Line2dSegmentPy::PyInit(PyObject* args, PyObject* /*kwd*/)
57
{
58
    if (PyArg_ParseTuple(args, "")) {
59
        // default line
60
        return 0;
61
    }
62

63
    PyErr_Clear();
64
    PyObject *pLine;
65
    if (PyArg_ParseTuple(args, "O!", &(Line2dSegmentPy::Type), &pLine)) {
66
        // Copy line
67
        Line2dSegmentPy* pcLine = static_cast<Line2dSegmentPy*>(pLine);
68
        // get Geom_Line of line segment
69
        Handle(Geom2d_TrimmedCurve) that_curv = Handle(Geom2d_TrimmedCurve)::DownCast
70
            (pcLine->getGeom2dLineSegmentPtr()->handle());
71
        Handle(Geom2d_Line) that_line = Handle(Geom2d_Line)::DownCast
72
            (that_curv->BasisCurve());
73
        // get Geom_Line of line segment
74
        Handle(Geom2d_TrimmedCurve) this_curv = Handle(Geom2d_TrimmedCurve)::DownCast
75
            (this->getGeom2dLineSegmentPtr()->handle());
76
        Handle(Geom2d_Line) this_line = Handle(Geom2d_Line)::DownCast
77
            (this_curv->BasisCurve());
78

79
        // Assign the lines
80
        this_line->SetLin2d(that_line->Lin2d());
81
        this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter());
82
        return 0;
83
    }
84

85
    PyErr_Clear();
86
    double first, last;
87
    if (PyArg_ParseTuple(args, "O!dd", &(Line2dSegmentPy::Type), &pLine, &first, &last)) {
88
        // Copy line
89
        Line2dSegmentPy* pcLine = static_cast<Line2dSegmentPy*>(pLine);
90
        // get Geom_Line of line segment
91
        Handle(Geom2d_TrimmedCurve) that_curv = Handle(Geom2d_TrimmedCurve)::DownCast
92
            (pcLine->getGeom2dLineSegmentPtr()->handle());
93
        Handle(Geom2d_Line) that_line = Handle(Geom2d_Line)::DownCast
94
            (that_curv->BasisCurve());
95
        // get Geom_Line of line segment
96
        Handle(Geom2d_TrimmedCurve) this_curv = Handle(Geom2d_TrimmedCurve)::DownCast
97
            (this->getGeom2dLineSegmentPtr()->handle());
98
        Handle(Geom2d_Line) this_line = Handle(Geom2d_Line)::DownCast
99
            (this_curv->BasisCurve());
100

101
        // Assign the lines
102
        this_line->SetLin2d(that_line->Lin2d());
103
        this_curv->SetTrim(first, last);
104
        return 0;
105
    }
106

107
    PyErr_Clear();
108
    if (PyArg_ParseTuple(args, "O!dd", &(Line2dPy::Type), &pLine, &first, &last)) {
109
        // Copy line
110
        Line2dPy* pcLine = static_cast<Line2dPy*>(pLine);
111
        // get Geom_Line of line
112
        Handle(Geom2d_Line) that_line = Handle(Geom2d_Line)::DownCast
113
            (pcLine->getGeom2dLinePtr()->handle());
114
        // get Geom_Line of line segment
115
        Handle(Geom2d_TrimmedCurve) this_curv = Handle(Geom2d_TrimmedCurve)::DownCast
116
            (this->getGeom2dLineSegmentPtr()->handle());
117
        Handle(Geom2d_Line) this_line = Handle(Geom2d_Line)::DownCast
118
            (this_curv->BasisCurve());
119

120
        // Assign the lines
121
        this_line->SetLin2d(that_line->Lin2d());
122
        this_curv->SetTrim(first, last);
123
        return 0;
124
    }
125

126
    PyErr_Clear();
127
    PyObject *pV1, *pV2;
128
    if (PyArg_ParseTuple(args, "O!O!", Base::Vector2dPy::type_object(), &pV1,
129
                                       Base::Vector2dPy::type_object(), &pV2)) {
130
        Base::Vector2d v1 = Py::toVector2d(pV1);
131
        Base::Vector2d v2 = Py::toVector2d(pV2);
132
        try {
133
            // Create line out of two points
134
            double distance = (v1-v2).Length();
135
            if (distance < gp::Resolution())
136
                Standard_Failure::Raise("Both points are equal");
137
            GCE2d_MakeSegment ms(gp_Pnt2d(v1.x,v1.y),
138
                                 gp_Pnt2d(v2.x,v2.y));
139
            if (!ms.IsDone()) {
140
                PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(ms.Status()));
141
                return -1;
142
            }
143

144
            // get Geom_Line of line segment
145
            Handle(Geom2d_TrimmedCurve) this_curv = Handle(Geom2d_TrimmedCurve)::DownCast
146
                (this->getGeom2dLineSegmentPtr()->handle());
147
            Handle(Geom2d_Line) this_line = Handle(Geom2d_Line)::DownCast
148
                (this_curv->BasisCurve());
149
            Handle(Geom2d_TrimmedCurve) that_curv = ms.Value();
150
            Handle(Geom2d_Line) that_line = Handle(Geom2d_Line)::DownCast(that_curv->BasisCurve());
151
            this_line->SetLin2d(that_line->Lin2d());
152
            this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter());
153
            return 0;
154
        }
155
        catch (Standard_Failure& e) {
156

157
            PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
158
            return -1;
159
        }
160
        catch (...) {
161
            PyErr_SetString(PartExceptionOCCError, "creation of line failed");
162
            return -1;
163
        }
164
    }
165

166
    PyErr_SetString(PyExc_TypeError, "Line2dSegment constructor accepts:\n"
167
        "-- empty parameter list\n"
168
        "-- Line2dSegment\n"
169
        "-- Line2dSegment, float, float\n"
170
        "-- Line2d, float, float\n"
171
        "-- Point, Point");
172
    return -1;
173
}
174

175
PyObject* Line2dSegmentPy::setParameterRange(PyObject *args)
176
{
177
    double first, last;
178
    if (!PyArg_ParseTuple(args, "dd", &first, &last))
179
        return nullptr;
180

181
    try {
182
        Handle(Geom2d_TrimmedCurve) this_curve = Handle(Geom2d_TrimmedCurve)::DownCast
183
            (this->getGeom2dLineSegmentPtr()->handle());
184
        this_curve->SetTrim(first, last);
185
    }
186
    catch (Standard_Failure& e) {
187

188
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
189
        return nullptr;
190
    }
191

192
    Py_Return;
193
}
194

195
Py::Object Line2dSegmentPy::getStartPoint() const
196
{
197
    Handle(Geom2d_TrimmedCurve) this_curve = Handle(Geom2d_TrimmedCurve)::DownCast
198
        (this->getGeom2dLineSegmentPtr()->handle());
199
    gp_Pnt2d pnt = this_curve->StartPoint();
200
    return Base::Vector2dPy::create(pnt.X(), pnt.Y());
201
}
202

203
void Line2dSegmentPy::setStartPoint(Py::Object arg)
204
{
205
    gp_Pnt2d p1, p2;
206
    Handle(Geom2d_TrimmedCurve) this_curv = Handle(Geom2d_TrimmedCurve)::DownCast
207
        (this->getGeom2dLineSegmentPtr()->handle());
208
    p2 = this_curv->EndPoint();
209

210
    PyObject *p = arg.ptr();
211
    if (PyObject_TypeCheck(p, Base::Vector2dPy::type_object())) {
212
        Base::Vector2d v = Py::toVector2d(p);
213
        p1.SetX(v.x);
214
        p1.SetY(v.y);
215
    }
216
    else if (PyTuple_Check(p)) {
217
        Py::Tuple tuple(arg);
218
        p1.SetX((double)Py::Float(tuple.getItem(0)));
219
        p1.SetY((double)Py::Float(tuple.getItem(1)));
220
    }
221
    else {
222
        std::string error = std::string("type must be 'Vector2d' or tuple, not ");
223
        error += p->ob_type->tp_name;
224
        throw Py::TypeError(error);
225
    }
226

227
    try {
228
        // Create line out of two points
229
        if (p1.Distance(p2) < gp::Resolution())
230
            Standard_Failure::Raise("Both points are equal");
231
        GCE2d_MakeSegment ms(p1, p2);
232
        if (!ms.IsDone()) {
233
            throw Py::RuntimeError(gce_ErrorStatusText(ms.Status()));
234
        }
235

236
        // get Geom_Line of line segment
237
        Handle(Geom2d_Line) this_line = Handle(Geom2d_Line)::DownCast
238
            (this_curv->BasisCurve());
239
        Handle(Geom2d_TrimmedCurve) that_curv = ms.Value();
240
        Handle(Geom2d_Line) that_line = Handle(Geom2d_Line)::DownCast(that_curv->BasisCurve());
241
        this_line->SetLin2d(that_line->Lin2d());
242
        this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter());
243
    }
244
    catch (Standard_Failure& e) {
245
        throw Py::RuntimeError(e.GetMessageString());
246
    }
247
}
248

249
Py::Object Line2dSegmentPy::getEndPoint() const
250
{
251
    Handle(Geom2d_TrimmedCurve) this_curve = Handle(Geom2d_TrimmedCurve)::DownCast
252
        (this->getGeom2dLineSegmentPtr()->handle());
253
    gp_Pnt2d pnt = this_curve->EndPoint();
254
    return Base::Vector2dPy::create(pnt.X(), pnt.Y());
255
}
256

257
void Line2dSegmentPy::setEndPoint(Py::Object arg)
258
{
259
    gp_Pnt2d p1, p2;
260
    Handle(Geom2d_TrimmedCurve) this_curv = Handle(Geom2d_TrimmedCurve)::DownCast
261
        (this->getGeom2dLineSegmentPtr()->handle());
262
    p1 = this_curv->StartPoint();
263

264
    PyObject *p = arg.ptr();
265
    if (PyObject_TypeCheck(p, Base::Vector2dPy::type_object())) {
266
        Base::Vector2d v = Py::toVector2d(p);
267
        p2.SetX(v.x);
268
        p2.SetY(v.y);
269
    }
270
    else if (PyTuple_Check(p)) {
271
        Py::Tuple tuple(arg);
272
        p2.SetX((double)Py::Float(tuple.getItem(0)));
273
        p2.SetY((double)Py::Float(tuple.getItem(1)));
274
    }
275
    else {
276
        std::string error = std::string("type must be 'Vector' or tuple, not ");
277
        error += p->ob_type->tp_name;
278
        throw Py::TypeError(error);
279
    }
280

281
    try {
282
        // Create line out of two points
283
        if (p1.Distance(p2) < gp::Resolution())
284
            Standard_Failure::Raise("Both points are equal");
285
        GCE2d_MakeSegment ms(p1, p2);
286
        if (!ms.IsDone()) {
287
            throw Py::RuntimeError(gce_ErrorStatusText(ms.Status()));
288
        }
289

290
        // get Geom_Line of line segment
291
        Handle(Geom2d_Line) this_line = Handle(Geom2d_Line)::DownCast
292
            (this_curv->BasisCurve());
293
        Handle(Geom2d_TrimmedCurve) that_curv = ms.Value();
294
        Handle(Geom2d_Line) that_line = Handle(Geom2d_Line)::DownCast(that_curv->BasisCurve());
295
        this_line->SetLin2d(that_line->Lin2d());
296
        this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter());
297
    }
298
    catch (Standard_Failure& e) {
299
        throw Py::RuntimeError(e.GetMessageString());
300
    }
301
}
302

303
PyObject *Line2dSegmentPy::getCustomAttributes(const char* /*attr*/) const
304
{
305
    return nullptr;
306
}
307

308
int Line2dSegmentPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
309
{
310
    return 0;
311
}
312

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

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

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

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