1
/***************************************************************************
2
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
# include <Geom_ToroidalSurface.hxx>
26
# include <Standard_Failure.hxx>
29
#include <Base/GeometryPyCXX.h>
30
#include <Base/VectorPy.h>
34
#include "ToroidPy.cpp"
39
// returns a string which represents the object e.g. when printed in python
40
std::string ToroidPy::representation() const
42
return "<Toroid object>";
45
PyObject *ToroidPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
47
// create a new instance of ToroidPy and the Twin object
48
return new ToroidPy(new GeomToroid);
52
int ToroidPy::PyInit(PyObject* args, PyObject* /*kwd*/)
54
if (PyArg_ParseTuple(args, "")) {
55
Handle(Geom_ToroidalSurface) torus = Handle(Geom_ToroidalSurface)::DownCast
56
(getGeomToroidPtr()->handle());
57
torus->SetMajorRadius(5.0);
58
torus->SetMinorRadius(1.0);
65
Py::Float ToroidPy::getMajorRadius() const
67
Handle(Geom_ToroidalSurface) torus = Handle(Geom_ToroidalSurface)::DownCast
68
(getGeomToroidPtr()->handle());
69
return Py::Float(torus->MajorRadius());
72
void ToroidPy::setMajorRadius(Py::Float arg)
75
Handle(Geom_ToroidalSurface) torus = Handle(Geom_ToroidalSurface)::DownCast
76
(getGeomToroidPtr()->handle());
77
torus->SetMajorRadius((double)arg);
79
catch (Standard_Failure&) {
80
throw Py::RuntimeError("Major radius must be positive and higher than minor radius");
84
Py::Float ToroidPy::getMinorRadius() const
86
Handle(Geom_ToroidalSurface) torus = Handle(Geom_ToroidalSurface)::DownCast
87
(getGeomToroidPtr()->handle());
88
return Py::Float(torus->MinorRadius());
91
void ToroidPy::setMinorRadius(Py::Float arg)
94
Handle(Geom_ToroidalSurface) torus = Handle(Geom_ToroidalSurface)::DownCast
95
(getGeomToroidPtr()->handle());
96
torus->SetMinorRadius((double)arg);
98
catch (Standard_Failure&) {
99
throw Py::RuntimeError("Minor radius must be positive and lower than major radius");
103
Py::Object ToroidPy::getCenter() const
105
Handle(Geom_ToroidalSurface) torus = Handle(Geom_ToroidalSurface)::DownCast
106
(getGeomToroidPtr()->handle());
107
gp_Pnt loc = torus->Location();
108
return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
111
void ToroidPy::setCenter(Py::Object arg)
113
PyObject* p = arg.ptr();
114
if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
115
Base::Vector3d loc = static_cast<Base::VectorPy*>(p)->value();
116
Handle(Geom_ToroidalSurface) torus = Handle(Geom_ToroidalSurface)::DownCast
117
(getGeomToroidPtr()->handle());
118
torus->SetLocation(gp_Pnt(loc.x, loc.y, loc.z));
121
std::string error = std::string("type must be 'Vector', not ");
122
error += p->ob_type->tp_name;
123
throw Py::TypeError(error);
127
Py::Object ToroidPy::getAxis() const
129
Handle(Geom_ElementarySurface) s = Handle(Geom_ElementarySurface)::DownCast
130
(getGeometryPtr()->handle());
131
gp_Dir dir = s->Axis().Direction();
132
return Py::Vector(Base::Vector3d(dir.X(), dir.Y(), dir.Z()));
135
void ToroidPy::setAxis(Py::Object arg)
137
Standard_Real dir_x, dir_y, dir_z;
138
PyObject *p = arg.ptr();
139
if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
140
Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
145
else if (PyTuple_Check(p)) {
146
Py::Tuple tuple(arg);
147
dir_x = (double)Py::Float(tuple.getItem(0));
148
dir_y = (double)Py::Float(tuple.getItem(1));
149
dir_z = (double)Py::Float(tuple.getItem(2));
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);
158
Handle(Geom_ElementarySurface) this_surf = Handle(Geom_ElementarySurface)::DownCast
159
(this->getGeometryPtr()->handle());
161
axis.SetLocation(this_surf->Location());
162
axis.SetDirection(gp_Dir(dir_x, dir_y, dir_z));
163
this_surf->SetAxis(axis);
165
catch (Standard_Failure&) {
166
throw Py::RuntimeError("cannot set axis");
170
Py::Float ToroidPy::getArea() const
172
Handle(Geom_ToroidalSurface) torus = Handle(Geom_ToroidalSurface)::DownCast
173
(getGeomToroidPtr()->handle());
174
return Py::Float(torus->Area());
177
Py::Float ToroidPy::getVolume() const
179
Handle(Geom_ToroidalSurface) torus = Handle(Geom_ToroidalSurface)::DownCast
180
(getGeomToroidPtr()->handle());
181
return Py::Float(torus->Volume());
184
PyObject *ToroidPy::getCustomAttributes(const char* /*attr*/) const
189
int ToroidPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)