~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Mod/Part/App/HyperbolaPyImp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
ImportĀ upstreamĀ versionĀ 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (c) 2008 Werner Mayer <wmayer@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
 
 
24
#include "PreCompiled.h"
 
25
#ifndef _PreComp_
 
26
# include <Geom_Hyperbola.hxx>
 
27
# include <GC_MakeHyperbola.hxx>
 
28
# include <gp_Hypr.hxx>
 
29
#endif
 
30
 
 
31
#include <Base/VectorPy.h>
 
32
#include <Base/GeometryPyCXX.h>
 
33
 
 
34
#include "Geometry.h"
 
35
#include "HyperbolaPy.h"
 
36
#include "HyperbolaPy.cpp"
 
37
 
 
38
using namespace Part;
 
39
 
 
40
extern const char* gce_ErrorStatusText(gce_ErrorType et);
 
41
 
 
42
// returns a string which represents the object e.g. when printed in python
 
43
const char *HyperbolaPy::representation(void) const
 
44
{
 
45
    return "<Hyperbola object>";
 
46
}
 
47
 
 
48
PyObject *HyperbolaPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
 
49
{
 
50
    // create a new instance of HyperbolaPy and the Twin object 
 
51
    return new HyperbolaPy(new GeomHyperbola);
 
52
}
 
53
 
 
54
// constructor method
 
55
int HyperbolaPy::PyInit(PyObject* args, PyObject* kwds)
 
56
{
 
57
    char* keywords_n[] = {NULL};
 
58
    if (PyArg_ParseTupleAndKeywords(args, kwds, "", keywords_n)) {
 
59
        Handle_Geom_Hyperbola hypr = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
 
60
        hypr->SetMajorRadius(2.0);
 
61
        hypr->SetMinorRadius(1.0);
 
62
        return 0;
 
63
    }
 
64
 
 
65
    char* keywords_e[] = {"Hyperbola",NULL};
 
66
    PyErr_Clear();
 
67
    PyObject *pHypr;
 
68
    if (PyArg_ParseTupleAndKeywords(args, kwds, "O!",keywords_e, &(HyperbolaPy::Type), &pHypr)) {
 
69
        HyperbolaPy* pH = static_cast<HyperbolaPy*>(pHypr);
 
70
        Handle_Geom_Hyperbola Hypr1 = Handle_Geom_Hyperbola::DownCast
 
71
            (pH->getGeometryPtr()->handle());
 
72
        Handle_Geom_Hyperbola Hypr2 = Handle_Geom_Hyperbola::DownCast
 
73
            (this->getGeometryPtr()->handle());
 
74
        Hypr2->SetHypr(Hypr1->Hypr());
 
75
        return 0;
 
76
    }
 
77
 
 
78
    char* keywords_ssc[] = {"S1","S2","Center",NULL};
 
79
    PyErr_Clear();
 
80
    PyObject *pV1, *pV2, *pV3;
 
81
    if (PyArg_ParseTupleAndKeywords(args, kwds, "O!O!O!", keywords_ssc,
 
82
                                         &(Base::VectorPy::Type), &pV1,
 
83
                                         &(Base::VectorPy::Type), &pV2,
 
84
                                         &(Base::VectorPy::Type), &pV3)) {
 
85
        Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
 
86
        Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
 
87
        Base::Vector3d v3 = static_cast<Base::VectorPy*>(pV3)->value();
 
88
        GC_MakeHyperbola mh(gp_Pnt(v1.x,v1.y,v1.z),
 
89
                            gp_Pnt(v2.x,v2.y,v2.z),
 
90
                            gp_Pnt(v3.x,v3.y,v3.z));
 
91
        if (!mh.IsDone()) {
 
92
            PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(mh.Status()));
 
93
            return -1;
 
94
        }
 
95
 
 
96
        Handle_Geom_Hyperbola hyperb = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
 
97
        hyperb->SetHypr(mh.Value()->Hypr());
 
98
        return 0;
 
99
    }
 
100
 
 
101
    char* keywords_cmm[] = {"Center","MajorRadius","MinorRadius",NULL};
 
102
    PyErr_Clear();
 
103
    PyObject *pV;
 
104
    double major, minor;
 
105
    if (PyArg_ParseTupleAndKeywords(args, kwds, "O!dd", keywords_cmm,
 
106
                                        &(Base::VectorPy::Type), &pV,
 
107
                                        &major, &minor)) {
 
108
        Base::Vector3d c = static_cast<Base::VectorPy*>(pV)->value();
 
109
        GC_MakeHyperbola mh(gp_Ax2(gp_Pnt(c.x,c.y,c.z), gp_Dir(0.0,0.0,1.0)),
 
110
                          major, minor);
 
111
        if (!mh.IsDone()) {
 
112
            PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(mh.Status()));
 
113
            return -1;
 
114
        }
 
115
 
 
116
        Handle_Geom_Hyperbola hyperb = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
 
117
        hyperb->SetHypr(mh.Value()->Hypr());
 
118
        return 0;
 
119
    }
 
120
 
 
121
    PyErr_SetString(PyExc_TypeError, "Hyperbola constructor accepts:\n"
 
122
        "-- empty parameter list\n"
 
123
        "-- Hyperbola\n"
 
124
        "-- Point, double, double\n"
 
125
        "-- Point, Point, Point");
 
126
    return -1;
 
127
}
 
128
 
 
129
Py::Float HyperbolaPy::getEccentricity(void) const
 
130
{
 
131
    Handle_Geom_Hyperbola curve = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
 
132
    return Py::Float(curve->Eccentricity()); 
 
133
}
 
134
 
 
135
Py::Float HyperbolaPy::getFocal(void) const
 
136
{
 
137
    Handle_Geom_Hyperbola curve = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
 
138
    return Py::Float(curve->Focal()); 
 
139
}
 
140
 
 
141
Py::Object HyperbolaPy::getFocus1(void) const
 
142
{
 
143
    Handle_Geom_Hyperbola c = Handle_Geom_Hyperbola::DownCast
 
144
        (getGeometryPtr()->handle());
 
145
    gp_Pnt loc = c->Focus1();
 
146
    return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
 
147
}
 
148
 
 
149
Py::Object HyperbolaPy::getFocus2(void) const
 
150
{
 
151
    Handle_Geom_Hyperbola c = Handle_Geom_Hyperbola::DownCast
 
152
        (getGeometryPtr()->handle());
 
153
    gp_Pnt loc = c->Focus2();
 
154
    return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
 
155
}
 
156
 
 
157
Py::Float HyperbolaPy::getParameter(void) const
 
158
{
 
159
    Handle_Geom_Hyperbola curve = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
 
160
    return Py::Float(curve->Parameter()); 
 
161
}
 
162
 
 
163
Py::Float HyperbolaPy::getMajorRadius(void) const
 
164
{
 
165
    Handle_Geom_Hyperbola curve = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
 
166
    return Py::Float(curve->MajorRadius()); 
 
167
}
 
168
 
 
169
void HyperbolaPy::setMajorRadius(Py::Float arg)
 
170
{
 
171
    Handle_Geom_Hyperbola curve = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
 
172
    curve->SetMajorRadius((double)arg); 
 
173
}
 
174
 
 
175
Py::Float HyperbolaPy::getMinorRadius(void) const
 
176
{
 
177
    Handle_Geom_Hyperbola curve = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
 
178
    return Py::Float(curve->MinorRadius()); 
 
179
}
 
180
 
 
181
void HyperbolaPy::setMinorRadius(Py::Float arg)
 
182
{
 
183
    Handle_Geom_Hyperbola curve = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
 
184
    curve->SetMinorRadius((double)arg); 
 
185
}
 
186
 
 
187
Py::Object HyperbolaPy::getLocation(void) const
 
188
{
 
189
    Handle_Geom_Hyperbola c = Handle_Geom_Hyperbola::DownCast
 
190
        (getGeometryPtr()->handle());
 
191
    gp_Pnt loc = c->Location();
 
192
    return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
 
193
}
 
194
 
 
195
void HyperbolaPy::setLocation(Py::Object arg)
 
196
{
 
197
    PyObject* p = arg.ptr();
 
198
    if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
 
199
        Base::Vector3d loc = static_cast<Base::VectorPy*>(p)->value();
 
200
        Handle_Geom_Hyperbola c = Handle_Geom_Hyperbola::DownCast
 
201
            (getGeometryPtr()->handle());
 
202
        c->SetLocation(gp_Pnt(loc.x, loc.y, loc.z));
 
203
    }
 
204
    else if (PyTuple_Check(p)) {
 
205
        gp_Pnt loc;
 
206
        Py::Tuple tuple(arg);
 
207
        loc.SetX((double)Py::Float(tuple.getItem(0)));
 
208
        loc.SetY((double)Py::Float(tuple.getItem(1)));
 
209
        loc.SetZ((double)Py::Float(tuple.getItem(2)));
 
210
        Handle_Geom_Hyperbola c = Handle_Geom_Hyperbola::DownCast
 
211
            (getGeometryPtr()->handle());
 
212
        c->SetLocation(loc);
 
213
    }
 
214
    else {
 
215
        std::string error = std::string("type must be 'Vector', not ");
 
216
        error += p->ob_type->tp_name;
 
217
        throw Py::TypeError(error);
 
218
    }
 
219
}
 
220
 
 
221
Py::Object HyperbolaPy::getAxis(void) const
 
222
{
 
223
    Handle_Geom_Hyperbola c = Handle_Geom_Hyperbola::DownCast
 
224
        (getGeometryPtr()->handle());
 
225
    gp_Dir dir = c->Axis().Direction();
 
226
    return Py::Vector(Base::Vector3d(dir.X(), dir.Y(), dir.Z()));
 
227
}
 
228
 
 
229
void HyperbolaPy::setAxis(Py::Object arg)
 
230
{
 
231
    Standard_Real dir_x, dir_y, dir_z;
 
232
    PyObject *p = arg.ptr();
 
233
    if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
 
234
        Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
 
235
        dir_x = v.x;
 
236
        dir_y = v.y;
 
237
        dir_z = v.z;
 
238
    }
 
239
    else if (PyTuple_Check(p)) {
 
240
        Py::Tuple tuple(arg);
 
241
        dir_x = (double)Py::Float(tuple.getItem(0));
 
242
        dir_y = (double)Py::Float(tuple.getItem(1));
 
243
        dir_z = (double)Py::Float(tuple.getItem(2));
 
244
    }
 
245
    else {
 
246
        std::string error = std::string("type must be 'Vector' or tuple, not ");
 
247
        error += p->ob_type->tp_name;
 
248
        throw Py::TypeError(error);
 
249
    }
 
250
 
 
251
    try {
 
252
        Handle_Geom_Hyperbola this_curv = Handle_Geom_Hyperbola::DownCast
 
253
            (this->getGeometryPtr()->handle());
 
254
        gp_Ax1 axis;
 
255
        axis.SetLocation(this_curv->Location());
 
256
        axis.SetDirection(gp_Dir(dir_x, dir_y, dir_z));
 
257
        gp_Dir dir = axis.Direction();
 
258
        this_curv->SetAxis(axis);
 
259
    }
 
260
    catch (Standard_Failure) {
 
261
        throw Py::Exception("cannot set axis");
 
262
    }
 
263
}
 
264
 
 
265
PyObject *HyperbolaPy::getCustomAttributes(const char* /*attr*/) const
 
266
{
 
267
    return 0;
 
268
}
 
269
 
 
270
int HyperbolaPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
 
271
{
 
272
    return 0; 
 
273
}
 
274
 
 
275