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_SphericalSurface.hxx>
26
# include <Standard_Failure.hxx>
29
#include <Base/GeometryPyCXX.h>
30
#include <Base/VectorPy.h>
33
#include "SpherePy.cpp"
39
// returns a string which represents the object e.g. when printed in python
40
std::string SpherePy::representation() const
42
Handle(Geom_SphericalSurface) sphere = Handle(Geom_SphericalSurface)::DownCast
43
(getGeomSpherePtr()->handle());
44
gp_Ax1 axis = sphere->Axis();
45
gp_Dir dir = axis.Direction();
46
gp_Pnt loc = axis.Location();
47
Standard_Real fRad = sphere->Radius();
49
std::stringstream str;
51
str << "Radius : " << fRad << ", ";
52
str << "Center : (" << loc.X() << ", "<< loc.Y() << ", "<< loc.Z() << "), ";
53
str << "Direction : (" << dir.X() << ", "<< dir.Y() << ", "<< dir.Z() << ")";
59
PyObject *SpherePy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
61
// create a new instance of SpherePy and the Twin object
62
return new SpherePy(new GeomSphere);
66
int SpherePy::PyInit(PyObject* args, PyObject* /*kwd*/)
68
if (PyArg_ParseTuple(args, "")) {
69
Handle(Geom_SphericalSurface) sphere = Handle(Geom_SphericalSurface)::DownCast
70
(getGeomSpherePtr()->handle());
71
sphere->SetRadius(1.0);
78
Py::Float SpherePy::getRadius() const
80
Handle(Geom_SphericalSurface) sphere = Handle(Geom_SphericalSurface)::DownCast
81
(getGeomSpherePtr()->handle());
82
return Py::Float(sphere->Radius());
85
void SpherePy::setRadius(Py::Float arg)
87
Handle(Geom_SphericalSurface) sphere = Handle(Geom_SphericalSurface)::DownCast
88
(getGeomSpherePtr()->handle());
89
sphere->SetRadius((double)arg);
92
Py::Float SpherePy::getArea() const
94
Handle(Geom_SphericalSurface) sphere = Handle(Geom_SphericalSurface)::DownCast
95
(getGeomSpherePtr()->handle());
96
return Py::Float(sphere->Area());
99
Py::Float SpherePy::getVolume() const
101
Handle(Geom_SphericalSurface) sphere = Handle(Geom_SphericalSurface)::DownCast
102
(getGeomSpherePtr()->handle());
103
return Py::Float(sphere->Volume());
106
Py::Object SpherePy::getCenter() const
108
Handle(Geom_SphericalSurface) sphere = Handle(Geom_SphericalSurface)::DownCast
109
(getGeomSpherePtr()->handle());
110
gp_Pnt loc = sphere->Location();
111
return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
114
void SpherePy::setCenter(Py::Object arg)
116
PyObject* p = arg.ptr();
117
if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
118
Base::Vector3d loc = static_cast<Base::VectorPy*>(p)->value();
119
Handle(Geom_SphericalSurface) sphere = Handle(Geom_SphericalSurface)::DownCast
120
(getGeomSpherePtr()->handle());
121
sphere->SetLocation(gp_Pnt(loc.x, loc.y, loc.z));
123
else if (PyObject_TypeCheck(p, &PyTuple_Type)) {
124
Base::Vector3d loc = Base::getVectorFromTuple<double>(p);
125
Handle(Geom_SphericalSurface) sphere = Handle(Geom_SphericalSurface)::DownCast
126
(getGeomSpherePtr()->handle());
127
sphere->SetLocation(gp_Pnt(loc.x, loc.y, loc.z));
130
std::string error = std::string("type must be 'Vector', not ");
131
error += p->ob_type->tp_name;
132
throw Py::TypeError(error);
136
Py::Object SpherePy::getAxis() const
138
Handle(Geom_ElementarySurface) s = Handle(Geom_ElementarySurface)::DownCast
139
(getGeometryPtr()->handle());
140
gp_Dir dir = s->Axis().Direction();
141
return Py::Vector(Base::Vector3d(dir.X(), dir.Y(), dir.Z()));
144
void SpherePy::setAxis(Py::Object arg)
146
Standard_Real dir_x, dir_y, dir_z;
147
PyObject *p = arg.ptr();
148
if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
149
Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
154
else if (PyTuple_Check(p)) {
155
Py::Tuple tuple(arg);
156
dir_x = (double)Py::Float(tuple.getItem(0));
157
dir_y = (double)Py::Float(tuple.getItem(1));
158
dir_z = (double)Py::Float(tuple.getItem(2));
161
std::string error = std::string("type must be 'Vector' or tuple, not ");
162
error += p->ob_type->tp_name;
163
throw Py::TypeError(error);
167
Handle(Geom_ElementarySurface) this_surf = Handle(Geom_ElementarySurface)::DownCast
168
(this->getGeometryPtr()->handle());
170
axis.SetLocation(this_surf->Location());
171
axis.SetDirection(gp_Dir(dir_x, dir_y, dir_z));
172
this_surf->SetAxis(axis);
174
catch (Standard_Failure&) {
175
throw Py::RuntimeError("cannot set axis");
179
PyObject *SpherePy::getCustomAttributes(const char* /*attr*/) const
184
int SpherePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)