FreeCAD

Форк
0
/
ToroidPyImp.cpp 
192 строки · 6.6 Кб
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_ToroidalSurface.hxx>
26
# include <Standard_Failure.hxx>
27
#endif
28

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

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

36

37
using namespace Part;
38

39
// returns a string which represents the object e.g. when printed in python
40
std::string ToroidPy::representation() const
41
{
42
    return "<Toroid object>";
43
}
44

45
PyObject *ToroidPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
46
{
47
    // create a new instance of ToroidPy and the Twin object
48
    return new ToroidPy(new GeomToroid);
49
}
50

51
// constructor method
52
int ToroidPy::PyInit(PyObject* args, PyObject* /*kwd*/)
53
{
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);
59
        return 0;
60
    }
61

62
    return -1;
63
}
64

65
Py::Float ToroidPy::getMajorRadius() const
66
{
67
    Handle(Geom_ToroidalSurface) torus = Handle(Geom_ToroidalSurface)::DownCast
68
        (getGeomToroidPtr()->handle());
69
    return Py::Float(torus->MajorRadius());
70
}
71

72
void ToroidPy::setMajorRadius(Py::Float arg)
73
{
74
    try {
75
        Handle(Geom_ToroidalSurface) torus = Handle(Geom_ToroidalSurface)::DownCast
76
            (getGeomToroidPtr()->handle());
77
        torus->SetMajorRadius((double)arg);
78
    }
79
    catch (Standard_Failure&) {
80
        throw Py::RuntimeError("Major radius must be positive and higher than minor radius");
81
    }
82
}
83

84
Py::Float ToroidPy::getMinorRadius() const
85
{
86
    Handle(Geom_ToroidalSurface) torus = Handle(Geom_ToroidalSurface)::DownCast
87
        (getGeomToroidPtr()->handle());
88
    return Py::Float(torus->MinorRadius());
89
}
90

91
void ToroidPy::setMinorRadius(Py::Float arg)
92
{
93
    try {
94
        Handle(Geom_ToroidalSurface) torus = Handle(Geom_ToroidalSurface)::DownCast
95
            (getGeomToroidPtr()->handle());
96
        torus->SetMinorRadius((double)arg);
97
    }
98
    catch (Standard_Failure&) {
99
        throw Py::RuntimeError("Minor radius must be positive and lower than major radius");
100
    }
101
}
102

103
Py::Object ToroidPy::getCenter() const
104
{
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()));
109
}
110

111
void ToroidPy::setCenter(Py::Object arg)
112
{
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));
119
    }
120
    else {
121
        std::string error = std::string("type must be 'Vector', not ");
122
        error += p->ob_type->tp_name;
123
        throw Py::TypeError(error);
124
    }
125
}
126

127
Py::Object ToroidPy::getAxis() const
128
{
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()));
133
}
134

135
void ToroidPy::setAxis(Py::Object arg)
136
{
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();
141
        dir_x = v.x;
142
        dir_y = v.y;
143
        dir_z = v.z;
144
    }
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));
150
    }
151
    else {
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);
155
    }
156

157
    try {
158
        Handle(Geom_ElementarySurface) this_surf = Handle(Geom_ElementarySurface)::DownCast
159
            (this->getGeometryPtr()->handle());
160
        gp_Ax1 axis;
161
        axis.SetLocation(this_surf->Location());
162
        axis.SetDirection(gp_Dir(dir_x, dir_y, dir_z));
163
        this_surf->SetAxis(axis);
164
    }
165
    catch (Standard_Failure&) {
166
        throw Py::RuntimeError("cannot set axis");
167
    }
168
}
169

170
Py::Float ToroidPy::getArea() const
171
{
172
    Handle(Geom_ToroidalSurface) torus = Handle(Geom_ToroidalSurface)::DownCast
173
        (getGeomToroidPtr()->handle());
174
    return Py::Float(torus->Area());
175
}
176

177
Py::Float ToroidPy::getVolume() const
178
{
179
    Handle(Geom_ToroidalSurface) torus = Handle(Geom_ToroidalSurface)::DownCast
180
        (getGeomToroidPtr()->handle());
181
    return Py::Float(torus->Volume());
182
}
183

184
PyObject *ToroidPy::getCustomAttributes(const char* /*attr*/) const
185
{
186
    return nullptr;
187
}
188

189
int ToroidPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
190
{
191
    return 0;
192
}
193

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

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

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

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