FreeCAD

Форк
0
/
Line2dPyImp.cpp 
225 строк · 7.5 Кб
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_MakeLine.hxx>
26
# include <Geom2d_Line.hxx>
27
# include <gp_Lin2d.hxx>
28
#endif
29

30
#include <Base/GeometryPyCXX.h>
31

32
#include "Geom2d/Line2dPy.h"
33
#include "Geom2d/Line2dPy.cpp"
34
#include "OCCError.h"
35

36

37
using namespace Part;
38

39
extern const char* gce_ErrorStatusText(gce_ErrorType et);
40

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

47
PyObject *Line2dPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
48
{
49
    // create a new instance of Line2dPy and the Twin object
50
    return new Line2dPy(new Geom2dLine);
51
}
52

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

61
    PyErr_Clear();
62
    PyObject *pLine;
63
    if (PyArg_ParseTuple(args, "O!", &(Line2dPy::Type), &pLine)) {
64
        // Copy line
65
        Line2dPy* pcLine = static_cast<Line2dPy*>(pLine);
66
        // get Geom_Line of line
67
        Handle(Geom2d_Line) that_line = Handle(Geom2d_Line)::DownCast
68
            (pcLine->getGeom2dLinePtr()->handle());
69
        // get Geom_Line of line
70
        Handle(Geom2d_Line) this_line = Handle(Geom2d_Line)::DownCast
71
            (this->getGeom2dLinePtr()->handle());
72

73
        // Assign the lines
74
        this_line->SetLin2d(that_line->Lin2d());
75
        return 0;
76
    }
77

78
    PyErr_Clear();
79
    PyObject *pV1, *pV2;
80
    if (PyArg_ParseTuple(args, "O!O!", Base::Vector2dPy::type_object(), &pV1,
81
                                       Base::Vector2dPy::type_object(), &pV2)) {
82
        Base::Vector2d v1 = Py::toVector2d(pV1);
83
        Base::Vector2d v2 = Py::toVector2d(pV2);
84
        try {
85
            // Create line out of two points
86
            double distance = (v1-v2).Length();
87
            if (distance < gp::Resolution())
88
                Standard_Failure::Raise("Both points are equal");
89
            GCE2d_MakeLine ms(gp_Pnt2d(v1.x,v1.y),
90
                              gp_Pnt2d(v2.x,v2.y));
91
            if (!ms.IsDone()) {
92
                PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(ms.Status()));
93
                return -1;
94
            }
95

96
            // get Geom_Line of line
97
            Handle(Geom2d_Line) this_line = Handle(Geom2d_Line)::DownCast
98
                (this->getGeom2dLinePtr()->handle());
99
            Handle(Geom2d_Line) that_line = ms.Value();
100
            this_line->SetLin2d(that_line->Lin2d());
101
            return 0;
102
        }
103
        catch (Standard_Failure& e) {
104

105
            PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
106
            return -1;
107
        }
108
        catch (...) {
109
            PyErr_SetString(PartExceptionOCCError, "creation of line failed");
110
            return -1;
111
        }
112
    }
113

114
    PyErr_SetString(PyExc_TypeError, "Line constructor accepts:\n"
115
        "-- empty parameter list\n"
116
        "-- Line\n"
117
        "-- Point, Point");
118
    return -1;
119
}
120

121
Py::Object Line2dPy::getLocation() const
122
{
123
    Handle(Geom2d_Line) this_curve = Handle(Geom2d_Line)::DownCast
124
        (this->getGeom2dLinePtr()->handle());
125
    gp_Pnt2d pnt = this_curve->Location();
126
    return Base::Vector2dPy::create(pnt.X(), pnt.Y());
127
}
128

129
void Line2dPy::setLocation(Py::Object arg)
130
{
131
    gp_Pnt2d pnt;
132
    gp_Dir2d dir;
133
    Handle(Geom2d_Line) this_line = Handle(Geom2d_Line)::DownCast
134
        (this->getGeom2dLinePtr()->handle());
135
    dir = this_line->Direction();
136

137
    PyObject *p = arg.ptr();
138
    if (PyObject_TypeCheck(p, Base::Vector2dPy::type_object())) {
139
        Base::Vector2d v = Py::toVector2d(p);
140
        pnt.SetX(v.x);
141
        pnt.SetY(v.y);
142
    }
143
    else if (PyTuple_Check(p)) {
144
        Py::Tuple tuple(arg);
145
        pnt.SetX((double)Py::Float(tuple.getItem(0)));
146
        pnt.SetY((double)Py::Float(tuple.getItem(1)));
147
    }
148
    else {
149
        std::string error = std::string("type must be 'Vector2d' or tuple, not ");
150
        error += p->ob_type->tp_name;
151
        throw Py::TypeError(error);
152
    }
153

154
    try {
155
        GCE2d_MakeLine ms(pnt, dir);
156
        if (!ms.IsDone()) {
157
            throw Py::RuntimeError(gce_ErrorStatusText(ms.Status()));
158
        }
159

160
        // get Geom_Line of line
161
        Handle(Geom2d_Line) that_line = ms.Value();
162
        this_line->SetLin2d(that_line->Lin2d());
163
    }
164
    catch (Standard_Failure& e) {
165
        throw Py::RuntimeError(e.GetMessageString());
166
    }
167
}
168

169
Py::Object Line2dPy::getDirection() const
170
{
171
    Handle(Geom2d_Line) this_curve = Handle(Geom2d_Line)::DownCast
172
        (this->getGeom2dLinePtr()->handle());
173
    gp_Dir2d dir = this_curve->Direction();
174
    return Base::Vector2dPy::create(dir.X(), dir.Y());
175
}
176

177
void Line2dPy::setDirection(Py::Object arg)
178
{
179
    gp_Pnt2d pnt;
180
    gp_Dir2d dir;
181
    Handle(Geom2d_Line) this_line = Handle(Geom2d_Line)::DownCast
182
        (this->getGeom2dLinePtr()->handle());
183
    pnt = this_line->Location();
184

185
    PyObject *p = arg.ptr();
186
    if (PyObject_TypeCheck(p, Base::Vector2dPy::type_object())) {
187
        Base::Vector2d v = Py::toVector2d(p);
188
        dir = gp_Dir2d(v.x,v.y);
189
    }
190
    else if (PyTuple_Check(p)) {
191
        Py::Tuple tuple(arg);
192
        double x = (double)Py::Float(tuple.getItem(0));
193
        double y = (double)Py::Float(tuple.getItem(1));
194
        dir = gp_Dir2d(x,y);
195
    }
196
    else {
197
        std::string error = std::string("type must be 'Vector2d' or tuple, not ");
198
        error += p->ob_type->tp_name;
199
        throw Py::TypeError(error);
200
    }
201

202
    try {
203
        GCE2d_MakeLine ms(pnt, dir);
204
        if (!ms.IsDone()) {
205
            throw Py::RuntimeError(gce_ErrorStatusText(ms.Status()));
206
        }
207

208
        // get Geom_Line of line
209
        Handle(Geom2d_Line) that_line = ms.Value();
210
        this_line->SetLin2d(that_line->Lin2d());
211
    }
212
    catch (Standard_Failure& e) {
213
        throw Py::RuntimeError(e.GetMessageString());
214
    }
215
}
216

217
PyObject *Line2dPy::getCustomAttributes(const char* /*attr*/) const
218
{
219
    return nullptr;
220
}
221

222
int Line2dPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
223
{
224
    return 0;
225
}
226

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

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

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

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