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

« back to all changes in this revision

Viewing changes to src/Mod/Part/App/EllipsePyImp.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 <gp_Elips.hxx>
 
27
# include <Geom_Ellipse.hxx>
 
28
# include <GC_MakeEllipse.hxx>
 
29
#endif
 
30
 
 
31
#include <Base/GeometryPyCXX.h>
 
32
#include <Base/VectorPy.h>
 
33
 
 
34
#include "Geometry.h"
 
35
#include "EllipsePy.h"
 
36
#include "EllipsePy.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 *EllipsePy::representation(void) const
 
44
{
 
45
    return "<Ellipse object>";
 
46
}
 
47
 
 
48
PyObject *EllipsePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
 
49
{
 
50
    // create a new instance of EllipsePy and the Twin object 
 
51
    return new EllipsePy(new GeomEllipse);
 
52
}
 
53
 
 
54
// constructor method
 
55
int EllipsePy::PyInit(PyObject* args, PyObject* kwds)
 
56
{
 
57
    char* keywords_n[] = {NULL};
 
58
    if (PyArg_ParseTupleAndKeywords(args, kwds, "", keywords_n)) {
 
59
        Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
60
        ellipse->SetMajorRadius(2.0);
 
61
        ellipse->SetMinorRadius(1.0);
 
62
        return 0;
 
63
    }
 
64
 
 
65
    char* keywords_e[] = {"Ellipse",NULL};
 
66
    PyErr_Clear();
 
67
    PyObject *pElips;
 
68
    if (PyArg_ParseTupleAndKeywords(args, kwds, "O!",keywords_e, &(EllipsePy::Type), &pElips)) {
 
69
        EllipsePy* pEllipse = static_cast<EllipsePy*>(pElips);
 
70
        Handle_Geom_Ellipse Elips1 = Handle_Geom_Ellipse::DownCast
 
71
            (pEllipse->getGeomEllipsePtr()->handle());
 
72
        Handle_Geom_Ellipse Elips2 = Handle_Geom_Ellipse::DownCast
 
73
            (this->getGeomEllipsePtr()->handle());
 
74
        Elips2->SetElips(Elips1->Elips());
 
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_MakeEllipse me(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 (!me.IsDone()) {
 
92
            PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(me.Status()));
 
93
            return -1;
 
94
        }
 
95
 
 
96
        Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
97
        ellipse->SetElips(me.Value()->Elips());
 
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_MakeEllipse me(gp_Ax2(gp_Pnt(c.x,c.y,c.z), gp_Dir(0.0,0.0,1.0)),
 
110
                          major, minor);
 
111
        if (!me.IsDone()) {
 
112
            PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(me.Status()));
 
113
            return -1;
 
114
        }
 
115
 
 
116
        Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
117
        ellipse->SetElips(me.Value()->Elips());
 
118
        return 0;
 
119
    }
 
120
 
 
121
    PyErr_SetString(PyExc_TypeError, "Ellipse constructor accepts:\n"
 
122
        "-- empty parameter list\n"
 
123
        "-- Ellipse\n"
 
124
        "-- Point, double, double\n"
 
125
        "-- Point, Point, Point");
 
126
    return -1;
 
127
}
 
128
 
 
129
Py::Float EllipsePy::getMajorRadius(void) const
 
130
{
 
131
    Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
132
    return Py::Float(ellipse->MajorRadius()); 
 
133
}
 
134
 
 
135
void EllipsePy::setMajorRadius(Py::Float arg)
 
136
{
 
137
    Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
138
    ellipse->SetMajorRadius((double)arg);
 
139
}
 
140
 
 
141
Py::Float EllipsePy::getMinorRadius(void) const
 
142
{
 
143
    Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
144
    return Py::Float(ellipse->MinorRadius()); 
 
145
}
 
146
 
 
147
void EllipsePy::setMinorRadius(Py::Float arg)
 
148
{
 
149
    Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
150
    ellipse->SetMinorRadius((double)arg);
 
151
}
 
152
 
 
153
Py::Float EllipsePy::getEccentricity(void) const
 
154
{
 
155
    Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
156
    return Py::Float(ellipse->Eccentricity()); 
 
157
}
 
158
 
 
159
Py::Float EllipsePy::getFocal(void) const
 
160
{
 
161
    Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
162
    return Py::Float(ellipse->Focal()); 
 
163
}
 
164
 
 
165
Py::Object EllipsePy::getFocus1(void) const
 
166
{
 
167
    Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
168
    gp_Pnt loc = ellipse->Focus1();
 
169
    return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
 
170
}
 
171
 
 
172
Py::Object EllipsePy::getFocus2(void) const
 
173
{
 
174
    Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
175
    gp_Pnt loc = ellipse->Focus2();
 
176
    return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
 
177
}
 
178
 
 
179
Py::Object EllipsePy::getCenter(void) const
 
180
{
 
181
    Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
182
    gp_Pnt loc = ellipse->Location();
 
183
    return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
 
184
}
 
185
 
 
186
void EllipsePy::setCenter(Py::Object arg)
 
187
{
 
188
    PyObject* p = arg.ptr();
 
189
    if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
 
190
        Base::Vector3d loc = static_cast<Base::VectorPy*>(p)->value();
 
191
        Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
192
        ellipse->SetLocation(gp_Pnt(loc.x, loc.y, loc.z));
 
193
    }
 
194
    else if (PyTuple_Check(p)) {
 
195
        Py::Tuple tuple(arg);
 
196
        gp_Pnt loc;
 
197
        loc.SetX((double)Py::Float(tuple.getItem(0)));
 
198
        loc.SetY((double)Py::Float(tuple.getItem(1)));
 
199
        loc.SetZ((double)Py::Float(tuple.getItem(2)));
 
200
        Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
201
        ellipse->SetLocation(loc);
 
202
    }
 
203
    else {
 
204
        std::string error = std::string("type must be 'Vector', not ");
 
205
        error += p->ob_type->tp_name;
 
206
        throw Py::TypeError(error);
 
207
    }
 
208
}
 
209
 
 
210
Py::Object EllipsePy::getAxis(void) const
 
211
{
 
212
    Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
213
    gp_Ax1 axis = ellipse->Axis();
 
214
    gp_Dir dir = axis.Direction();
 
215
    return Py::Vector(Base::Vector3d(dir.X(), dir.Y(), dir.Z()));
 
216
}
 
217
 
 
218
void EllipsePy::setAxis(Py::Object arg)
 
219
{
 
220
    PyObject* p = arg.ptr();
 
221
    Base::Vector3d val;
 
222
    if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
 
223
        val = static_cast<Base::VectorPy*>(p)->value();
 
224
    }
 
225
    else if (PyTuple_Check(p)) {
 
226
        val = Base::getVectorFromTuple<double>(p);
 
227
    }
 
228
    else {
 
229
        std::string error = std::string("type must be 'Vector', not ");
 
230
        error += p->ob_type->tp_name;
 
231
        throw Py::TypeError(error);
 
232
    }
 
233
 
 
234
    Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
 
235
    try {
 
236
        gp_Ax1 axis;
 
237
        axis.SetLocation(ellipse->Location());
 
238
        axis.SetDirection(gp_Dir(val.x, val.y, val.z));
 
239
        gp_Dir dir = axis.Direction();
 
240
        ellipse->SetAxis(axis);
 
241
    }
 
242
    catch (Standard_Failure) {
 
243
        throw Py::Exception("cannot set axis");
 
244
    }
 
245
}
 
246
 
 
247
PyObject *EllipsePy::getCustomAttributes(const char* /*attr*/) const
 
248
{
 
249
    return 0;
 
250
}
 
251
 
 
252
int EllipsePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
 
253
{
 
254
    return 0; 
 
255
}