FreeCAD

Форк
0
/
SpherePyImp.cpp 
187 строк · 6.7 Кб
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_SphericalSurface.hxx>
26
# include <Standard_Failure.hxx>
27
#endif
28

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

32
#include "SpherePy.h"
33
#include "SpherePy.cpp"
34
#include "OCCError.h"
35

36

37
using namespace Part;
38

39
// returns a string which represents the object e.g. when printed in python
40
std::string SpherePy::representation() const
41
{
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();
48

49
    std::stringstream str;
50
    str << "Sphere (";
51
    str << "Radius : " << fRad << ", ";
52
    str << "Center : (" << loc.X() << ", "<< loc.Y() << ", "<< loc.Z() << "), ";
53
    str << "Direction : (" << dir.X() << ", "<< dir.Y() << ", "<< dir.Z() << ")";
54
    str << ")";
55

56
    return str.str();
57
}
58

59
PyObject *SpherePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
60
{
61
    // create a new instance of SpherePy and the Twin object
62
    return new SpherePy(new GeomSphere);
63
}
64

65
// constructor method
66
int SpherePy::PyInit(PyObject* args, PyObject* /*kwd*/)
67
{
68
    if (PyArg_ParseTuple(args, "")) {
69
        Handle(Geom_SphericalSurface) sphere = Handle(Geom_SphericalSurface)::DownCast
70
            (getGeomSpherePtr()->handle());
71
        sphere->SetRadius(1.0);
72
        return 0;
73
    }
74

75
    return -1;
76
}
77

78
Py::Float SpherePy::getRadius() const
79
{
80
    Handle(Geom_SphericalSurface) sphere = Handle(Geom_SphericalSurface)::DownCast
81
        (getGeomSpherePtr()->handle());
82
    return Py::Float(sphere->Radius());
83
}
84

85
void  SpherePy::setRadius(Py::Float arg)
86
{
87
    Handle(Geom_SphericalSurface) sphere = Handle(Geom_SphericalSurface)::DownCast
88
        (getGeomSpherePtr()->handle());
89
    sphere->SetRadius((double)arg);
90
}
91

92
Py::Float SpherePy::getArea() const
93
{
94
    Handle(Geom_SphericalSurface) sphere = Handle(Geom_SphericalSurface)::DownCast
95
        (getGeomSpherePtr()->handle());
96
    return Py::Float(sphere->Area());
97
}
98

99
Py::Float SpherePy::getVolume() const
100
{
101
    Handle(Geom_SphericalSurface) sphere = Handle(Geom_SphericalSurface)::DownCast
102
        (getGeomSpherePtr()->handle());
103
    return Py::Float(sphere->Volume());
104
}
105

106
Py::Object SpherePy::getCenter() const
107
{
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()));
112
}
113

114
void SpherePy::setCenter(Py::Object arg)
115
{
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));
122
    }
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));
128
    }
129
    else {
130
        std::string error = std::string("type must be 'Vector', not ");
131
        error += p->ob_type->tp_name;
132
        throw Py::TypeError(error);
133
    }
134
}
135

136
Py::Object SpherePy::getAxis() const
137
{
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()));
142
}
143

144
void SpherePy::setAxis(Py::Object arg)
145
{
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();
150
        dir_x = v.x;
151
        dir_y = v.y;
152
        dir_z = v.z;
153
    }
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));
159
    }
160
    else {
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);
164
    }
165

166
    try {
167
        Handle(Geom_ElementarySurface) this_surf = Handle(Geom_ElementarySurface)::DownCast
168
            (this->getGeometryPtr()->handle());
169
        gp_Ax1 axis;
170
        axis.SetLocation(this_surf->Location());
171
        axis.SetDirection(gp_Dir(dir_x, dir_y, dir_z));
172
        this_surf->SetAxis(axis);
173
    }
174
    catch (Standard_Failure&) {
175
        throw Py::RuntimeError("cannot set axis");
176
    }
177
}
178

179
PyObject *SpherePy::getCustomAttributes(const char* /*attr*/) const
180
{
181
    return nullptr;
182
}
183

184
int SpherePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
185
{
186
    return 0;
187
}
188

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

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

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

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