FreeCAD

Форк
0
/
CylinderPyImp.cpp 
249 строк · 9.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_MakeCylindricalSurface.hxx>
26
# include <Geom_Circle.hxx>
27
# include <Geom_CylindricalSurface.hxx>
28
# include <gp_Cylinder.hxx>
29
#endif
30

31
#include <Base/GeometryPyCXX.h>
32
#include <Base/PyWrapParseTupleAndKeywords.h>
33
#include <Base/VectorPy.h>
34

35
#include "CylinderPy.h"
36
#include "CylinderPy.cpp"
37
#include "CirclePy.h"
38
#include "OCCError.h"
39

40

41
using namespace Part;
42

43
extern const char* gce_ErrorStatusText(gce_ErrorType et);
44

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

51
PyObject *CylinderPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
52
{
53
    // create a new instance of CylinderPy and the Twin object
54
    return new CylinderPy(new GeomCylinder);
55
}
56

57
// constructor method
58
int CylinderPy::PyInit(PyObject* args, PyObject* kwds)
59
{
60
    // cylinder and distance for offset
61
    PyObject *pCyl;
62
    double dist;
63
    static const std::array<const char *, 3> keywords_cd{"Cylinder", "Distance", nullptr};
64
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!d", keywords_cd, &(CylinderPy::Type), &pCyl, &dist)) {
65
        CylinderPy* pcCylinder = static_cast<CylinderPy*>(pCyl);
66
        Handle(Geom_CylindricalSurface) cylinder = Handle(Geom_CylindricalSurface)::DownCast
67
            (pcCylinder->getGeomCylinderPtr()->handle());
68
        GC_MakeCylindricalSurface mc(cylinder->Cylinder(), dist);
69
        if (!mc.IsDone()) {
70
            PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
71
            return -1;
72
        }
73

74
        Handle(Geom_CylindricalSurface) cyl = Handle(Geom_CylindricalSurface)::DownCast
75
            (getGeomCylinderPtr()->handle());
76
        cyl->SetCylinder(mc.Value()->Cylinder());
77
        return 0;
78
    }
79

80
    static const std::array<const char *, 2> keywords_c {"Cylinder", nullptr};
81
    PyErr_Clear();
82
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!", keywords_c, &(CylinderPy::Type), &pCyl)) {
83
        CylinderPy* pcCylinder = static_cast<CylinderPy*>(pCyl);
84
        Handle(Geom_CylindricalSurface) cyl1 = Handle(Geom_CylindricalSurface)::DownCast
85
            (pcCylinder->getGeomCylinderPtr()->handle());
86
        Handle(Geom_CylindricalSurface) cyl2 = Handle(Geom_CylindricalSurface)::DownCast
87
            (this->getGeomCylinderPtr()->handle());
88
        cyl2->SetCylinder(cyl1->Cylinder());
89
        return 0;
90
    }
91

92
    PyObject *pV1, *pV2, *pV3;
93
    static const std::array<const char *, 4> keywords_ppp {"Point1", "Point2", "Point3", nullptr};
94
    PyErr_Clear();
95
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!O!O!", keywords_ppp,
96
                                            &(Base::VectorPy::Type), &pV1,
97
                                            &(Base::VectorPy::Type), &pV2,
98
                                            &(Base::VectorPy::Type), &pV3)) {
99
        Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
100
        Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
101
        Base::Vector3d v3 = static_cast<Base::VectorPy*>(pV3)->value();
102
        GC_MakeCylindricalSurface mc(gp_Pnt(v1.x,v1.y,v1.z),
103
                                     gp_Pnt(v2.x,v2.y,v2.z),
104
                                     gp_Pnt(v3.x,v3.y,v3.z));
105
        if (!mc.IsDone()) {
106
            PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
107
            return -1;
108
        }
109

110
        Handle(Geom_CylindricalSurface) cyl = Handle(Geom_CylindricalSurface)::DownCast
111
            (getGeomCylinderPtr()->handle());
112
        cyl->SetCylinder(mc.Value()->Cylinder());
113
        return 0;
114
    }
115

116
    static const std::array<const char *, 2> keywords_cc {"Circle", nullptr};
117
    PyErr_Clear();
118
    PyObject *pCirc;
119
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!", keywords_cc, &(CirclePy::Type), &pCirc)) {
120
        CirclePy* pcCircle = static_cast<CirclePy*>(pCirc);
121
        Handle(Geom_Circle) circ = Handle(Geom_Circle)::DownCast
122
            (pcCircle->getGeomCirclePtr()->handle());
123
        GC_MakeCylindricalSurface mc(circ->Circ());
124
        if (!mc.IsDone()) {
125
            PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
126
            return -1;
127
        }
128

129
        Handle(Geom_CylindricalSurface) cyl = Handle(Geom_CylindricalSurface)::DownCast
130
            (getGeomCylinderPtr()->handle());
131
        cyl->SetCylinder(mc.Value()->Cylinder());
132
        return 0;
133
    }
134

135
    static const std::array<const char *, 1> keywords_n {nullptr};
136
    PyErr_Clear();
137
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "", keywords_n)) {
138
        Handle(Geom_CylindricalSurface) cyl = Handle(Geom_CylindricalSurface)::DownCast
139
            (getGeomCylinderPtr()->handle());
140
        cyl->SetRadius(1.0);
141
        return 0;
142
    }
143

144
    // All checks failed
145
    PyErr_SetString(PyExc_TypeError, "Cylinder constructor accepts:\n"
146
        "-- empty parameter list\n"
147
        "-- Cylinder\n"
148
        "-- Cylinder, Distance\n"
149
        "-- Point1, Point2, Point3\n"
150
        "-- Circle");
151
    return -1;
152
}
153

154
Py::Float CylinderPy::getRadius() const
155
{
156
    Handle(Geom_CylindricalSurface) cyl = Handle(Geom_CylindricalSurface)::DownCast
157
        (getGeomCylinderPtr()->handle());
158
    return Py::Float(cyl->Radius());
159
}
160

161
void CylinderPy::setRadius(Py::Float arg)
162
{
163
    Handle(Geom_CylindricalSurface) cyl = Handle(Geom_CylindricalSurface)::DownCast
164
        (getGeomCylinderPtr()->handle());
165
    cyl->SetRadius((double)arg);
166
}
167

168
Py::Object CylinderPy::getCenter() const
169
{
170
    Handle(Geom_CylindricalSurface) cyl = Handle(Geom_CylindricalSurface)::DownCast
171
        (getGeomCylinderPtr()->handle());
172
    gp_Pnt loc = cyl->Location();
173
    return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
174
}
175

176
void CylinderPy::setCenter(Py::Object arg)
177
{
178
    PyObject* p = arg.ptr();
179
    if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
180
        Base::Vector3d loc = static_cast<Base::VectorPy*>(p)->value();
181
        Handle(Geom_CylindricalSurface) cyl = Handle(Geom_CylindricalSurface)::DownCast
182
            (getGeomCylinderPtr()->handle());
183
        cyl->SetLocation(gp_Pnt(loc.x, loc.y, loc.z));
184
    }
185
    else if (PyObject_TypeCheck(p, &PyTuple_Type)) {
186
        Base::Vector3d loc = Base::getVectorFromTuple<double>(p);
187
        Handle(Geom_CylindricalSurface) cyl = Handle(Geom_CylindricalSurface)::DownCast
188
            (getGeomCylinderPtr()->handle());
189
        cyl->SetLocation(gp_Pnt(loc.x, loc.y, loc.z));
190
    }
191
    else {
192
        std::string error = std::string("type must be 'Vector', not ");
193
        error += p->ob_type->tp_name;
194
        throw Py::TypeError(error);
195
    }
196
}
197

198
Py::Object CylinderPy::getAxis() const
199
{
200
    Handle(Geom_ElementarySurface) s = Handle(Geom_ElementarySurface)::DownCast
201
        (getGeometryPtr()->handle());
202
    gp_Dir dir = s->Axis().Direction();
203
    return Py::Vector(Base::Vector3d(dir.X(), dir.Y(), dir.Z()));
204
}
205

206
void CylinderPy::setAxis(Py::Object arg)
207
{
208
    Standard_Real dir_x, dir_y, dir_z;
209
    PyObject *p = arg.ptr();
210
    if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
211
        Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
212
        dir_x = v.x;
213
        dir_y = v.y;
214
        dir_z = v.z;
215
    }
216
    else if (PyTuple_Check(p)) {
217
        Py::Tuple tuple(arg);
218
        dir_x = (double)Py::Float(tuple.getItem(0));
219
        dir_y = (double)Py::Float(tuple.getItem(1));
220
        dir_z = (double)Py::Float(tuple.getItem(2));
221
    }
222
    else {
223
        std::string error = std::string("type must be 'Vector' or tuple, not ");
224
        error += p->ob_type->tp_name;
225
        throw Py::TypeError(error);
226
    }
227

228
    try {
229
        Handle(Geom_ElementarySurface) this_surf = Handle(Geom_ElementarySurface)::DownCast
230
            (this->getGeometryPtr()->handle());
231
        gp_Ax1 axis;
232
        axis.SetLocation(this_surf->Location());
233
        axis.SetDirection(gp_Dir(dir_x, dir_y, dir_z));
234
        this_surf->SetAxis(axis);
235
    }
236
    catch (Standard_Failure&) {
237
        throw Py::RuntimeError("cannot set axis");
238
    }
239
}
240

241
PyObject *CylinderPy::getCustomAttributes(const char* /*attr*/) const
242
{
243
    return nullptr;
244
}
245

246
int CylinderPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
247
{
248
    return 0;
249
}
250

251

252

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

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

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

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