FreeCAD

Форк
0
/
LinePyImp.cpp 
229 строк · 7.8 Кб
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 <Geom_Line.hxx>
26
# include <GC_MakeLine.hxx>
27
#endif
28

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

32
#include "LinePy.h"
33
#include "LinePy.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 LinePy::representation() const
43
{
44
    return "<Line object>";
45
}
46

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

53
// constructor method
54
int LinePy::PyInit(PyObject* args, PyObject* /*kwd*/)
55
{
56

57
    if (PyArg_ParseTuple(args, "")) {
58
        // default line
59
        return 0;
60
    }
61

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

74
        // Assign the lines
75
        this_line->SetLin(that_line->Lin());
76
        return 0;
77
    }
78

79
    PyErr_Clear();
80
    PyObject *pV1, *pV2;
81
    if (PyArg_ParseTuple(args, "O!O!", &(Base::VectorPy::Type), &pV1,
82
                                       &(Base::VectorPy::Type), &pV2)) {
83
        Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
84
        Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
85
        try {
86
            // Create line out of two points
87
            double distance = Base::Distance(v1, v2);
88
            if (distance < gp::Resolution())
89
                Standard_Failure::Raise("Both points are equal");
90
            GC_MakeLine ms(gp_Pnt(v1.x,v1.y,v1.z),
91
                           gp_Pnt(v2.x,v2.y,v2.z));
92
            if (!ms.IsDone()) {
93
                PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(ms.Status()));
94
                return -1;
95
            }
96

97
            // get Geom_Line of line
98
            Handle(Geom_Line) this_curv = Handle(Geom_Line)::DownCast
99
                (this->getGeomLinePtr()->handle());
100
            Handle(Geom_Line) that_curv = ms.Value();
101
            this_curv->SetLin(that_curv->Lin());
102
            return 0;
103
        }
104
        catch (Standard_Failure& e) {
105

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

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

122
Py::Object LinePy::getLocation() const
123
{
124
    Handle(Geom_Line) this_curve = Handle(Geom_Line)::DownCast
125
        (this->getGeomLinePtr()->handle());
126
    gp_Pnt pnt = this_curve->Position().Location();
127
    return Py::Vector(Base::Vector3d(pnt.X(), pnt.Y(), pnt.Z()));
128
}
129

130
void LinePy::setLocation(Py::Object arg)
131
{
132
    gp_Pnt pnt;
133
    gp_Dir dir;
134
    Handle(Geom_Line) this_curv = Handle(Geom_Line)::DownCast
135
        (this->getGeomLinePtr()->handle());
136
    dir = this_curv->Position().Direction();
137

138
    PyObject *p = arg.ptr();
139
    if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
140
        Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
141
        pnt.SetX(v.x);
142
        pnt.SetY(v.y);
143
        pnt.SetZ(v.z);
144
    }
145
    else if (PyTuple_Check(p)) {
146
        Py::Tuple tuple(arg);
147
        pnt.SetX((double)Py::Float(tuple.getItem(0)));
148
        pnt.SetY((double)Py::Float(tuple.getItem(1)));
149
        pnt.SetZ((double)Py::Float(tuple.getItem(2)));
150
    }
151
    else {
152
        std::string error = std::string("type must be 'Vector' or tuple, not ");
153
        error += p->ob_type->tp_name;
154
        throw Py::TypeError(error);
155
    }
156

157
    try {
158
        GC_MakeLine ms(pnt, dir);
159
        if (!ms.IsDone()) {
160
            throw Py::RuntimeError(gce_ErrorStatusText(ms.Status()));
161
        }
162

163
        // get Geom_Line of line
164
        Handle(Geom_Line) that_curv = ms.Value();
165
        this_curv->SetLin(that_curv->Lin());
166
    }
167
    catch (Standard_Failure& e) {
168
        throw Py::RuntimeError(e.GetMessageString());
169
    }
170
}
171

172
Py::Object LinePy::getDirection() const
173
{
174
    Handle(Geom_Line) this_curve = Handle(Geom_Line)::DownCast
175
        (this->getGeomLinePtr()->handle());
176
    gp_Dir dir = this_curve->Position().Direction();
177
    return Py::Vector(Base::Vector3d(dir.X(), dir.Y(), dir.Z()));
178
}
179

180
void LinePy::setDirection(Py::Object arg)
181
{
182
    gp_Pnt pnt;
183
    gp_Dir dir;
184
    Handle(Geom_Line) this_curv = Handle(Geom_Line)::DownCast
185
        (this->getGeomLinePtr()->handle());
186
    pnt = this_curv->Position().Location();
187

188
    PyObject *p = arg.ptr();
189
    if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
190
        Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
191
        dir = gp_Dir(v.x,v.y,v.z);
192
    }
193
    else if (PyTuple_Check(p)) {
194
        Py::Tuple tuple(arg);
195
        double x = (double)Py::Float(tuple.getItem(0));
196
        double y = (double)Py::Float(tuple.getItem(1));
197
        double z = (double)Py::Float(tuple.getItem(2));
198
        dir = gp_Dir(x,y,z);
199
    }
200
    else {
201
        std::string error = std::string("type must be 'Vector' or tuple, not ");
202
        error += p->ob_type->tp_name;
203
        throw Py::TypeError(error);
204
    }
205

206
    try {
207
        GC_MakeLine ms(pnt, dir);
208
        if (!ms.IsDone()) {
209
            throw Py::RuntimeError(gce_ErrorStatusText(ms.Status()));
210
        }
211

212
        // get Geom_Line of line
213
        Handle(Geom_Line) that_curv = ms.Value();
214
        this_curv->SetLin(that_curv->Lin());
215
    }
216
    catch (Standard_Failure& e) {
217
        throw Py::RuntimeError(e.GetMessageString());
218
    }
219
}
220

221
PyObject *LinePy::getCustomAttributes(const char* /*attr*/) const
222
{
223
    return nullptr;
224
}
225

226
int LinePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
227
{
228
    return 0;
229
}
230

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

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

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

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