FreeCAD

Форк
0
/
Circle2dPyImp.cpp 
185 строк · 7.3 Кб
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_MakeCircle.hxx>
26
# include <Geom2d_Circle.hxx>
27
# include <gp_Circ2d.hxx>
28
#endif
29

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

33
#include "Geom2d/Circle2dPy.h"
34
#include "Geom2d/Circle2dPy.cpp"
35
#include "OCCError.h"
36

37

38
using namespace Part;
39

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

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

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

54
// constructor method
55
int Circle2dPy::PyInit(PyObject* args, PyObject* kwds)
56
{
57
    // circle and distance for offset
58
    PyObject *pCirc;
59
    double dist;
60
    static const std::array<const char *, 3> keywords_cd {"Circle","Distance",nullptr};
61
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!d", keywords_cd, &(Circle2dPy::Type), &pCirc, &dist)) {
62
        Circle2dPy* pcCircle = static_cast<Circle2dPy*>(pCirc);
63
        Handle(Geom2d_Circle) circle = Handle(Geom2d_Circle)::DownCast
64
            (pcCircle->getGeom2dCirclePtr()->handle());
65
        GCE2d_MakeCircle mc(circle->Circ2d(), dist);
66
        if (!mc.IsDone()) {
67
            PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
68
            return -1;
69
        }
70

71
        Handle(Geom2d_Circle) circ = Handle(Geom2d_Circle)::DownCast(getGeom2dCirclePtr()->handle());
72
        circ->SetCirc2d(mc.Value()->Circ2d());
73
        return 0;
74
    }
75

76
    // center and radius
77
    PyObject *pV1, *pV2, *pV3;
78
    static const std::array<const char *, 3> keywords_cnr {"Center","Radius",nullptr};
79
    PyErr_Clear();
80
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!d", keywords_cnr,
81
                                        Base::Vector2dPy::type_object(), &pV1,
82
                                        &dist)) {
83
        Base::Vector2d v1 = Py::toVector2d(pV1);
84
        GCE2d_MakeCircle mc(gp_Pnt2d(v1.x,v1.y), dist);
85
        if (!mc.IsDone()) {
86
            PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
87
            return -1;
88
        }
89

90
        Handle(Geom2d_Circle) circle = Handle(Geom2d_Circle)::DownCast(getGeom2dCirclePtr()->handle());
91
        circle->SetCirc2d(mc.Value()->Circ2d());
92
        return 0;
93
    }
94

95
    static const std::array<const char *, 2> keywords_c {"Circle",nullptr};
96
    PyErr_Clear();
97
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!", keywords_c, &(Circle2dPy::Type), &pCirc)) {
98
        Circle2dPy* pcCircle = static_cast<Circle2dPy*>(pCirc);
99
        Handle(Geom2d_Circle) circ1 = Handle(Geom2d_Circle)::DownCast
100
            (pcCircle->getGeom2dCirclePtr()->handle());
101
        Handle(Geom2d_Circle) circ2 = Handle(Geom2d_Circle)::DownCast
102
            (this->getGeom2dCirclePtr()->handle());
103
        circ2->SetCirc2d(circ1->Circ2d());
104
        return 0;
105
    }
106

107
    static const std::array<const char *, 4> keywords_ppp {"Point1","Point2","Point3",nullptr};
108
    PyErr_Clear();
109
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!O!O!", keywords_ppp,
110
                                            Base::Vector2dPy::type_object(), &pV1,
111
                                            Base::Vector2dPy::type_object(), &pV2,
112
                                            Base::Vector2dPy::type_object(), &pV3)) {
113
        Base::Vector2d v1 = Py::toVector2d(pV1);
114
        Base::Vector2d v2 = Py::toVector2d(pV2);
115
        Base::Vector2d v3 = Py::toVector2d(pV3);
116
        GCE2d_MakeCircle mc(gp_Pnt2d(v1.x,v1.y),
117
                            gp_Pnt2d(v2.x,v2.y),
118
                            gp_Pnt2d(v3.x,v3.y));
119
        if (!mc.IsDone()) {
120
            PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
121
            return -1;
122
        }
123

124
        Handle(Geom2d_Circle) circle = Handle(Geom2d_Circle)::DownCast(getGeom2dCirclePtr()->handle());
125
        circle->SetCirc2d(mc.Value()->Circ2d());
126
        return 0;
127
    }
128

129
    // default circle
130
    static const std::array<const char *, 1> keywords_n = {nullptr};
131
    PyErr_Clear();
132
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "", keywords_n) != 0) {
133
        Handle(Geom2d_Circle) circle = Handle(Geom2d_Circle)::DownCast(getGeom2dCirclePtr()->handle());
134
        circle->SetRadius(1.0);
135
        return 0;
136
    }
137

138
    PyErr_SetString(PyExc_TypeError, "Circle constructor accepts:\n"
139
        "-- empty parameter list\n"
140
        "-- Circle\n"
141
        "-- Circle, Distance\n"
142
        "-- Center, Radius\n"
143
        "-- Point1, Point2, Point3");
144
    return -1;
145
}
146

147
PyObject* Circle2dPy::getCircleCenter(PyObject *args)
148
{
149
    PyObject* p1;
150
    PyObject* p2;
151
    PyObject* p3;
152
    if (!PyArg_ParseTuple(args, "O!O!O!", Base::Vector2dPy::type_object(), &p1
153
                                        , Base::Vector2dPy::type_object(), &p2
154
                                        , Base::Vector2dPy::type_object(), &p3))
155
        return nullptr;
156

157
    Base::Vector2d v1 = Py::toVector2d(p1);
158
    Base::Vector2d v2 = Py::toVector2d(p2);
159
    Base::Vector2d v3 = Py::toVector2d(p3);
160

161
    Base::Vector2d cnt = Geom2dCircle::getCircleCenter(v1, v2, v3);
162
    return Py::new_reference_to(Base::Vector2dPy::create(cnt));
163
}
164

165
Py::Float Circle2dPy::getRadius() const
166
{
167
    Handle(Geom2d_Circle) circle = Handle(Geom2d_Circle)::DownCast(getGeom2dCirclePtr()->handle());
168
    return Py::Float(circle->Radius());
169
}
170

171
void  Circle2dPy::setRadius(Py::Float arg)
172
{
173
    Handle(Geom2d_Circle) circle = Handle(Geom2d_Circle)::DownCast(getGeom2dCirclePtr()->handle());
174
    circle->SetRadius((double)arg);
175
}
176

177
PyObject *Circle2dPy::getCustomAttributes(const char* ) const
178
{
179
    return nullptr;
180
}
181

182
int Circle2dPy::setCustomAttributes(const char* , PyObject *)
183
{
184
    return 0;
185
}
186

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

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

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

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