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

« back to all changes in this revision

Viewing changes to src/Mod/Part/App/PlanePyImp.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_Ax1.hxx>
 
27
# include <gp_Dir.hxx>
 
28
# include <gp_Pnt.hxx>
 
29
# include <gp_Lin.hxx>
 
30
# include <gp_Pln.hxx>
 
31
# include <Geom_Line.hxx>
 
32
# include <Geom_Plane.hxx>
 
33
# include <Geom_TrimmedCurve.hxx>
 
34
# include <GC_MakePlane.hxx>
 
35
# include <Standard_Failure.hxx>
 
36
#endif
 
37
 
 
38
#include <Base/VectorPy.h>
 
39
#include <Base/GeometryPyCXX.h>
 
40
 
 
41
#include "Geometry.h"
 
42
#include "LinePy.h"
 
43
#include "PlanePy.h"
 
44
#include "PlanePy.cpp"
 
45
 
 
46
using namespace Part;
 
47
 
 
48
extern const char* gce_ErrorStatusText(gce_ErrorType et);
 
49
 
 
50
// returns a string which represents the object e.g. when printed in python
 
51
const char *PlanePy::representation(void) const
 
52
{
 
53
    return "<Plane object>";
 
54
}
 
55
 
 
56
PyObject *PlanePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
 
57
{
 
58
    // create a new instance of PlanePy and the Twin object 
 
59
    return new PlanePy(new GeomPlane);
 
60
}
 
61
 
 
62
// constructor method
 
63
int PlanePy::PyInit(PyObject* args, PyObject* kwds)
 
64
{
 
65
    // plane and distance for offset
 
66
    PyObject *pPlane;
 
67
    double dist;
 
68
    static char* keywords_pd[] = {"Plane","Distance",NULL};
 
69
    if (PyArg_ParseTupleAndKeywords(args, kwds, "O!d", keywords_pd, &(PlanePy::Type), &pPlane, &dist)) {
 
70
        PlanePy* pcPlane = static_cast<PlanePy*>(pPlane);
 
71
        Handle_Geom_Plane plane = Handle_Geom_Plane::DownCast
 
72
            (pcPlane->getGeometryPtr()->handle());
 
73
        GC_MakePlane mc(plane->Pln(), dist);
 
74
        if (!mc.IsDone()) {
 
75
            PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(mc.Status()));
 
76
            return -1;
 
77
        }
 
78
 
 
79
        Handle_Geom_Plane plan = Handle_Geom_Plane::DownCast(getGeometryPtr()->handle());
 
80
        plan->SetPln(mc.Value()->Pln());
 
81
        return 0;
 
82
    }
 
83
 
 
84
    // plane from equation
 
85
    double a,b,c,d;
 
86
    static char* keywords_abcd[] = {"A","B","C","D",NULL};
 
87
    PyErr_Clear();
 
88
    if (PyArg_ParseTupleAndKeywords(args, kwds, "dddd", keywords_abcd,
 
89
                                        &a,&b,&c,&d)) {
 
90
        GC_MakePlane mc(a,b,c,d);
 
91
        if (!mc.IsDone()) {
 
92
            PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(mc.Status()));
 
93
            return -1;
 
94
        }
 
95
 
 
96
        Handle_Geom_Plane plane = Handle_Geom_Plane::DownCast(getGeometryPtr()->handle());
 
97
        plane->SetPln(mc.Value()->Pln());
 
98
        return 0;
 
99
    }
 
100
 
 
101
    PyObject *pV1, *pV2, *pV3;
 
102
    static char* keywords_ppp[] = {"Point1","Point2","Point3",NULL};
 
103
    PyErr_Clear();
 
104
    if (PyArg_ParseTupleAndKeywords(args, kwds, "O!O!O!", keywords_ppp,
 
105
                                         &(Base::VectorPy::Type), &pV1,
 
106
                                         &(Base::VectorPy::Type), &pV2,
 
107
                                         &(Base::VectorPy::Type), &pV3)) {
 
108
        Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
 
109
        Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
 
110
        Base::Vector3d v3 = static_cast<Base::VectorPy*>(pV3)->value();
 
111
        GC_MakePlane mc(gp_Pnt(v1.x,v1.y,v1.z),
 
112
                        gp_Pnt(v2.x,v2.y,v2.z),
 
113
                        gp_Pnt(v3.x,v3.y,v3.z));
 
114
        if (!mc.IsDone()) {
 
115
            PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(mc.Status()));
 
116
            return -1;
 
117
        }
 
118
 
 
119
        Handle_Geom_Plane plane = Handle_Geom_Plane::DownCast(getGeometryPtr()->handle());
 
120
        plane->SetPln(mc.Value()->Pln());
 
121
        return 0;
 
122
    }
 
123
 
 
124
    // location and normal
 
125
    static char* keywords_cnr[] = {"Location","Normal",NULL};
 
126
    PyErr_Clear();
 
127
    if (PyArg_ParseTupleAndKeywords(args, kwds, "O!O!", keywords_cnr,
 
128
                                        &(Base::VectorPy::Type), &pV1,
 
129
                                        &(Base::VectorPy::Type), &pV2)) {
 
130
        Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
 
131
        Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
 
132
        GC_MakePlane mc(gp_Pnt(v1.x,v1.y,v1.z),
 
133
                        gp_Dir(v2.x,v2.y,v2.z));
 
134
        if (!mc.IsDone()) {
 
135
            PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(mc.Status()));
 
136
            return -1;
 
137
        }
 
138
 
 
139
        Handle_Geom_Plane plane = Handle_Geom_Plane::DownCast(getGeometryPtr()->handle());
 
140
        plane->SetPln(mc.Value()->Pln());
 
141
        return 0;
 
142
    }
 
143
 
 
144
    static char* keywords_p[] = {"Plane",NULL};
 
145
    PyErr_Clear();
 
146
    if (PyArg_ParseTupleAndKeywords(args, kwds, "O!", keywords_p, &(PlanePy::Type), &pPlane)) {
 
147
        PlanePy* pcPlane = static_cast<PlanePy*>(pPlane);
 
148
        Handle_Geom_Plane plane1 = Handle_Geom_Plane::DownCast
 
149
            (pcPlane->getGeometryPtr()->handle());
 
150
        Handle_Geom_Plane plane2 = Handle_Geom_Plane::DownCast
 
151
            (this->getGeometryPtr()->handle());
 
152
        plane2->SetPln(plane1->Pln());
 
153
        return 0;
 
154
    }
 
155
 
 
156
    static char* keywords_n[] = {NULL};
 
157
    PyErr_Clear();
 
158
    if (PyArg_ParseTupleAndKeywords(args, kwds, "", keywords_n)) {
 
159
        // do nothing
 
160
        return 0;
 
161
    }
 
162
 
 
163
    PyErr_SetString(PyExc_TypeError, "Plane constructor accepts:\n"
 
164
        "-- empty parameter list\n"
 
165
        "-- Plane\n"
 
166
        "-- Plane, Distance\n"
 
167
        "-- Location, Normal\n"
 
168
        "-- Point1, Point2, Point3\n"
 
169
        "-- A, B, C, D\n"
 
170
        "   (as equation: Ax + By + Cz + D = 0.0)");
 
171
    return -1;
 
172
}
 
173
 
 
174
Py::Object PlanePy::getPosition(void) const
 
175
{
 
176
    Handle_Geom_Plane this_surf = Handle_Geom_Plane::DownCast
 
177
        (this->getGeomPlanePtr()->handle());
 
178
    gp_Pnt pnt = this_surf->Location();
 
179
    return Py::Vector(Base::Vector3d(pnt.X(), pnt.Y(), pnt.Z()));
 
180
}
 
181
 
 
182
void PlanePy::setPosition(Py::Object arg)
 
183
{
 
184
    gp_Pnt loc;
 
185
    PyObject *p = arg.ptr();
 
186
    if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
 
187
        Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
 
188
        loc.SetX(v.x);
 
189
        loc.SetY(v.y);
 
190
        loc.SetZ(v.z);
 
191
    }
 
192
    else if (PyTuple_Check(p)) {
 
193
        Py::Tuple tuple(arg);
 
194
        loc.SetX((double)Py::Float(tuple.getItem(0)));
 
195
        loc.SetY((double)Py::Float(tuple.getItem(1)));
 
196
        loc.SetZ((double)Py::Float(tuple.getItem(2)));
 
197
    }
 
198
    else {
 
199
        std::string error = std::string("type must be 'Vector' or tuple, not ");
 
200
        error += p->ob_type->tp_name;
 
201
        throw Py::TypeError(error);
 
202
    }
 
203
 
 
204
    try {
 
205
        Handle_Geom_Plane this_surf = Handle_Geom_Plane::DownCast
 
206
            (this->getGeomPlanePtr()->handle());
 
207
        this_surf->SetLocation(loc);
 
208
    }
 
209
    catch (Standard_Failure) {
 
210
        Handle_Standard_Failure e = Standard_Failure::Caught();
 
211
        throw Py::Exception(e->GetMessageString());
 
212
    }
 
213
}
 
214
 
 
215
Py::Object PlanePy::getAxis(void) const
 
216
{
 
217
    Handle_Geom_ElementarySurface s = Handle_Geom_ElementarySurface::DownCast
 
218
        (getGeometryPtr()->handle());
 
219
    gp_Dir dir = s->Axis().Direction();
 
220
    return Py::Vector(Base::Vector3d(dir.X(), dir.Y(), dir.Z()));
 
221
}
 
222
 
 
223
void PlanePy::setAxis(Py::Object arg)
 
224
{
 
225
    Standard_Real dir_x, dir_y, dir_z;
 
226
    PyObject *p = arg.ptr();
 
227
    if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
 
228
        Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
 
229
        dir_x = v.x;
 
230
        dir_y = v.y;
 
231
        dir_z = v.z;
 
232
    }
 
233
    else if (PyTuple_Check(p)) {
 
234
        Py::Tuple tuple(arg);
 
235
        dir_x = (double)Py::Float(tuple.getItem(0));
 
236
        dir_y = (double)Py::Float(tuple.getItem(1));
 
237
        dir_z = (double)Py::Float(tuple.getItem(2));
 
238
    }
 
239
    else {
 
240
        std::string error = std::string("type must be 'Vector' or tuple, not ");
 
241
        error += p->ob_type->tp_name;
 
242
        throw Py::TypeError(error);
 
243
    }
 
244
 
 
245
    try {
 
246
        Handle_Geom_ElementarySurface this_surf = Handle_Geom_ElementarySurface::DownCast
 
247
            (this->getGeometryPtr()->handle());
 
248
        gp_Ax1 axis;
 
249
        axis.SetLocation(this_surf->Location());
 
250
        axis.SetDirection(gp_Dir(dir_x, dir_y, dir_z));
 
251
        gp_Dir dir = axis.Direction();
 
252
        this_surf->SetAxis(axis);
 
253
    }
 
254
    catch (Standard_Failure) {
 
255
        throw Py::Exception("cannot set axis");
 
256
    }
 
257
}
 
258
 
 
259
PyObject* PlanePy::uIso(PyObject * args)
 
260
{
 
261
    double u;
 
262
    if (!PyArg_ParseTuple(args, "d", &u))
 
263
        return 0;
 
264
 
 
265
    try {
 
266
        Handle_Geom_Plane plane = Handle_Geom_Plane::DownCast
 
267
            (getGeomPlanePtr()->handle());
 
268
        Handle_Geom_Line c = Handle_Geom_Line::DownCast(plane->UIso(u));
 
269
        GeomLineSegment* line = new GeomLineSegment();
 
270
        Handle_Geom_TrimmedCurve this_curv = Handle_Geom_TrimmedCurve::DownCast
 
271
            (line->handle());
 
272
        Handle_Geom_Line this_line = Handle_Geom_Line::DownCast
 
273
            (this_curv->BasisCurve());
 
274
        this_line->SetLin(c->Lin());
 
275
        return new LinePy(line);
 
276
    }
 
277
    catch (Standard_Failure) {
 
278
        Handle_Standard_Failure e = Standard_Failure::Caught();
 
279
        PyErr_SetString(PyExc_Exception, e->GetMessageString());
 
280
        return 0;
 
281
    }
 
282
}
 
283
 
 
284
PyObject* PlanePy::vIso(PyObject * args)
 
285
{
 
286
    double v;
 
287
    if (!PyArg_ParseTuple(args, "d", &v))
 
288
        return 0;
 
289
 
 
290
    try {
 
291
        Handle_Geom_Plane plane = Handle_Geom_Plane::DownCast
 
292
            (getGeomPlanePtr()->handle());
 
293
        Handle_Geom_Line c = Handle_Geom_Line::DownCast(plane->VIso(v));
 
294
        GeomLineSegment* line = new GeomLineSegment();
 
295
        Handle_Geom_TrimmedCurve this_curv = Handle_Geom_TrimmedCurve::DownCast
 
296
            (line->handle());
 
297
        Handle_Geom_Line this_line = Handle_Geom_Line::DownCast
 
298
            (this_curv->BasisCurve());
 
299
        this_line->SetLin(c->Lin());
 
300
        return new LinePy(line);
 
301
    }
 
302
    catch (Standard_Failure) {
 
303
        Handle_Standard_Failure e = Standard_Failure::Caught();
 
304
        PyErr_SetString(PyExc_Exception, e->GetMessageString());
 
305
        return 0;
 
306
    }
 
307
}
 
308
 
 
309
PyObject *PlanePy::getCustomAttributes(const char* /*attr*/) const
 
310
{
 
311
    return 0;
 
312
}
 
313
 
 
314
int PlanePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
 
315
{
 
316
    return 0; 
 
317
}