FreeCAD

Форк
0
/
LineSegmentPyImp.cpp 
321 строка · 11.9 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2008 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 <GC_MakeSegment.hxx>
26
# include <Geom_Line.hxx>
27
# include <Geom_TrimmedCurve.hxx>
28
#endif
29

30
#include <Base/GeometryPyCXX.h>
31
#include <Base/VectorPy.h>
32

33
#include "LineSegmentPy.h"
34
#include "LineSegmentPy.cpp"
35
#include "LinePy.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 LineSegmentPy::representation() const
45
{
46
    std::stringstream str;
47
    Base::Vector3d start = getGeomLineSegmentPtr()->getStartPoint();
48
    Base::Vector3d end   = getGeomLineSegmentPtr()->getEndPoint();
49
    str << "<Line segment ("
50
        << start.x << "," <<start.y << "," <<start.z << ") ("
51
        << end.x   << "," <<end.y   << "," <<end.z   << ") >";
52

53
    return str.str();
54
}
55

56
PyObject *LineSegmentPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
57
{
58
    // create a new instance of LineSegmentPy and the Twin object
59
    return new LineSegmentPy(new GeomLineSegment);
60
}
61

62
// constructor method
63
int LineSegmentPy::PyInit(PyObject* args, PyObject* /*kwd*/)
64
{
65
    if (PyArg_ParseTuple(args, "")) {
66
        return 0;
67
    }
68

69
    PyErr_Clear();
70
    PyObject *pLine;
71
    if (PyArg_ParseTuple(args, "O!", &(LineSegmentPy::Type), &pLine)) {
72
        // Copy line
73
        LineSegmentPy* pcLine = static_cast<LineSegmentPy*>(pLine);
74
        // get Geom_Line of line segment
75
        Handle(Geom_TrimmedCurve) that_curv = Handle(Geom_TrimmedCurve)::DownCast
76
            (pcLine->getGeomLineSegmentPtr()->handle());
77
        Handle(Geom_Line) that_line = Handle(Geom_Line)::DownCast
78
            (that_curv->BasisCurve());
79
        // get Geom_Line of line segment
80
        Handle(Geom_TrimmedCurve) this_curv = Handle(Geom_TrimmedCurve)::DownCast
81
            (this->getGeomLineSegmentPtr()->handle());
82
        Handle(Geom_Line) this_line = Handle(Geom_Line)::DownCast
83
            (this_curv->BasisCurve());
84

85
        // Assign the lines
86
        this_line->SetLin(that_line->Lin());
87
        this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter());
88
        return 0;
89
    }
90

91
    PyErr_Clear();
92
    double first, last;
93
    if (PyArg_ParseTuple(args, "O!dd", &(LineSegmentPy::Type), &pLine, &first, &last)) {
94
        // Copy line
95
        LineSegmentPy* pcLine = static_cast<LineSegmentPy*>(pLine);
96
        // get Geom_Line of line segment
97
        Handle(Geom_TrimmedCurve) that_curv = Handle(Geom_TrimmedCurve)::DownCast
98
            (pcLine->getGeomLineSegmentPtr()->handle());
99
        Handle(Geom_Line) that_line = Handle(Geom_Line)::DownCast
100
            (that_curv->BasisCurve());
101
        // get Geom_Line of line segment
102
        Handle(Geom_TrimmedCurve) this_curv = Handle(Geom_TrimmedCurve)::DownCast
103
            (this->getGeomLineSegmentPtr()->handle());
104
        Handle(Geom_Line) this_line = Handle(Geom_Line)::DownCast
105
            (this_curv->BasisCurve());
106

107
        // Assign the lines
108
        this_line->SetLin(that_line->Lin());
109
        this_curv->SetTrim(first, last);
110
        return 0;
111
    }
112

113
    PyErr_Clear();
114
    if (PyArg_ParseTuple(args, "O!dd", &(LinePy::Type), &pLine, &first, &last)) {
115
        // Copy line
116
        LinePy* pcLine = static_cast<LinePy*>(pLine);
117
        // get Geom_Line of line segment
118
        Handle(Geom_Line) that_line = Handle(Geom_Line)::DownCast
119
            (pcLine->getGeomLinePtr()->handle());
120
        // get Geom_Line of line segment
121
        Handle(Geom_TrimmedCurve) this_curv = Handle(Geom_TrimmedCurve)::DownCast
122
            (this->getGeomLineSegmentPtr()->handle());
123
        Handle(Geom_Line) this_line = Handle(Geom_Line)::DownCast
124
            (this_curv->BasisCurve());
125

126
        // Assign the lines
127
        this_line->SetLin(that_line->Lin());
128
        this_curv->SetTrim(first, last);
129
        return 0;
130
    }
131

132
    PyErr_Clear();
133
    PyObject *pV1, *pV2;
134
    if (PyArg_ParseTuple(args, "O!O!", &(Base::VectorPy::Type), &pV1,
135
                                       &(Base::VectorPy::Type), &pV2)) {
136
        Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
137
        Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
138
        try {
139
            // Create line out of two points
140
            double distance = Base::Distance(v1, v2);
141
            if (distance < gp::Resolution())
142
                Standard_Failure::Raise("Both points are equal");
143
            GC_MakeSegment ms(gp_Pnt(v1.x,v1.y,v1.z),
144
                              gp_Pnt(v2.x,v2.y,v2.z));
145
            if (!ms.IsDone()) {
146
                PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(ms.Status()));
147
                return -1;
148
            }
149

150
            // get Geom_Line of line segment
151
            Handle(Geom_TrimmedCurve) this_curv = Handle(Geom_TrimmedCurve)::DownCast
152
                (this->getGeomLineSegmentPtr()->handle());
153
            Handle(Geom_Line) this_line = Handle(Geom_Line)::DownCast
154
                (this_curv->BasisCurve());
155
            Handle(Geom_TrimmedCurve) that_curv = ms.Value();
156
            Handle(Geom_Line) that_line = Handle(Geom_Line)::DownCast(that_curv->BasisCurve());
157
            this_line->SetLin(that_line->Lin());
158
            this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter());
159

160
            return 0;
161
        }
162
        catch (Standard_Failure& e) {
163

164
            PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
165
            return -1;
166
        }
167
        catch (...) {
168
            PyErr_SetString(PartExceptionOCCError, "creation of line failed");
169
            return -1;
170
        }
171
    }
172

173
    PyErr_SetString(PyExc_TypeError, "Line constructor accepts:\n"
174
        "-- empty parameter list\n"
175
        "-- LineSegment\n"
176
        "-- LineSegment,double,double\n"
177
        "-- Line,double,double\n"
178
        "-- Point, Point");
179
    return -1;
180
}
181

182
PyObject* LineSegmentPy::setParameterRange(PyObject *args)
183
{
184
    double first, last;
185
    if (!PyArg_ParseTuple(args, "dd", &first, &last))
186
        return nullptr;
187

188
    try {
189
        Handle(Geom_TrimmedCurve) this_curve = Handle(Geom_TrimmedCurve)::DownCast
190
            (this->getGeomLineSegmentPtr()->handle());
191
        this_curve->SetTrim(first, last);
192
    }
193
    catch (Standard_Failure& e) {
194
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
195
        return nullptr;
196
    }
197

198
    Py_Return;
199
}
200

201
Py::Object LineSegmentPy::getStartPoint() const
202
{
203
    Handle(Geom_TrimmedCurve) this_curve = Handle(Geom_TrimmedCurve)::DownCast
204
        (this->getGeomLineSegmentPtr()->handle());
205
    gp_Pnt pnt = this_curve->StartPoint();
206
    return Py::Vector(Base::Vector3d(pnt.X(), pnt.Y(), pnt.Z()));
207
}
208

209
void LineSegmentPy::setStartPoint(Py::Object arg)
210
{
211
    gp_Pnt p1, p2;
212
    Handle(Geom_TrimmedCurve) this_curv = Handle(Geom_TrimmedCurve)::DownCast
213
        (this->getGeomLineSegmentPtr()->handle());
214
    p2 = this_curv->EndPoint();
215

216
    PyObject *p = arg.ptr();
217
    if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
218
        Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
219
        p1.SetX(v.x);
220
        p1.SetY(v.y);
221
        p1.SetZ(v.z);
222
    }
223
    else if (PyTuple_Check(p)) {
224
        Py::Tuple tuple(arg);
225
        p1.SetX((double)Py::Float(tuple.getItem(0)));
226
        p1.SetY((double)Py::Float(tuple.getItem(1)));
227
        p1.SetZ((double)Py::Float(tuple.getItem(2)));
228
    }
229
    else {
230
        std::string error = std::string("type must be 'Vector' or tuple, not ");
231
        error += p->ob_type->tp_name;
232
        throw Py::TypeError(error);
233
    }
234

235
    try {
236
        // Create line out of two points
237
        if (p1.Distance(p2) < gp::Resolution())
238
            Standard_Failure::Raise("Both points are equal");
239
        GC_MakeSegment ms(p1, p2);
240
        if (!ms.IsDone()) {
241
            throw Py::RuntimeError(gce_ErrorStatusText(ms.Status()));
242
        }
243

244
        // get Geom_Line of line segment
245
        Handle(Geom_Line) this_line = Handle(Geom_Line)::DownCast
246
            (this_curv->BasisCurve());
247
        Handle(Geom_TrimmedCurve) that_curv = ms.Value();
248
        Handle(Geom_Line) that_line = Handle(Geom_Line)::DownCast(that_curv->BasisCurve());
249
        this_line->SetLin(that_line->Lin());
250
        this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter());
251
    }
252
    catch (Standard_Failure& e) {
253
        throw Py::RuntimeError(e.GetMessageString());
254
    }
255
}
256

257
Py::Object LineSegmentPy::getEndPoint() const
258
{
259
    Handle(Geom_TrimmedCurve) this_curve = Handle(Geom_TrimmedCurve)::DownCast
260
        (this->getGeomLineSegmentPtr()->handle());
261
    gp_Pnt pnt = this_curve->EndPoint();
262
    return Py::Vector(Base::Vector3d(pnt.X(), pnt.Y(), pnt.Z()));
263
}
264

265
void LineSegmentPy::setEndPoint(Py::Object arg)
266
{
267
    gp_Pnt p1, p2;
268
    Handle(Geom_TrimmedCurve) this_curv = Handle(Geom_TrimmedCurve)::DownCast
269
        (this->getGeomLineSegmentPtr()->handle());
270
    p1 = this_curv->StartPoint();
271

272
    PyObject *p = arg.ptr();
273
    if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
274
        Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
275
        p2.SetX(v.x);
276
        p2.SetY(v.y);
277
        p2.SetZ(v.z);
278
    }
279
    else if (PyTuple_Check(p)) {
280
        Py::Tuple tuple(arg);
281
        p2.SetX((double)Py::Float(tuple.getItem(0)));
282
        p2.SetY((double)Py::Float(tuple.getItem(1)));
283
        p2.SetZ((double)Py::Float(tuple.getItem(2)));
284
    }
285
    else {
286
        std::string error = std::string("type must be 'Vector' or tuple, not ");
287
        error += p->ob_type->tp_name;
288
        throw Py::TypeError(error);
289
    }
290

291
    try {
292
        // Create line out of two points
293
        if (p1.Distance(p2) < gp::Resolution())
294
            Standard_Failure::Raise("Both points are equal");
295
        GC_MakeSegment ms(p1, p2);
296
        if (!ms.IsDone()) {
297
            throw Py::RuntimeError(gce_ErrorStatusText(ms.Status()));
298
        }
299

300
        // get Geom_Line of line segment
301
        Handle(Geom_Line) this_line = Handle(Geom_Line)::DownCast
302
            (this_curv->BasisCurve());
303
        Handle(Geom_TrimmedCurve) that_curv = ms.Value();
304
        Handle(Geom_Line) that_line = Handle(Geom_Line)::DownCast(that_curv->BasisCurve());
305
        this_line->SetLin(that_line->Lin());
306
        this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter());
307
    }
308
    catch (Standard_Failure& e) {
309
        throw Py::RuntimeError(e.GetMessageString());
310
    }
311
}
312

313
PyObject *LineSegmentPy::getCustomAttributes(const char* /*attr*/) const
314
{
315
    return nullptr;
316
}
317

318
int LineSegmentPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
319
{
320
    return 0;
321
}
322

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

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

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

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