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

« back to all changes in this revision

Viewing changes to src/Mod/Part/App/TopoShapeFacePyImp.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) Jļæ½rgen Riegel          (juergen.riegel@web.de) 2008     *
 
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 <BRep_Tool.hxx>
 
27
# include <BRepBuilderAPI_MakeFace.hxx>
 
28
# include <ShapeAnalysis.hxx>
 
29
# include <BRepAdaptor_Surface.hxx>
 
30
# include <Geom_BezierSurface.hxx>
 
31
# include <Geom_BSplineSurface.hxx>
 
32
# include <Geom_Plane.hxx>
 
33
# include <Geom_CylindricalSurface.hxx>
 
34
# include <Geom_ConicalSurface.hxx>
 
35
# include <Geom_SphericalSurface.hxx>
 
36
# include <Geom_ToroidalSurface.hxx>
 
37
# include <Handle_Geom_Surface.hxx>
 
38
# include <TopoDS.hxx>
 
39
# include <TopoDS_Face.hxx>
 
40
# include <TopoDS_Wire.hxx>
 
41
# include <gp_Pln.hxx>
 
42
# include <gp_Cylinder.hxx>
 
43
# include <gp_Cone.hxx>
 
44
# include <gp_Sphere.hxx>
 
45
# include <gp_Torus.hxx>
 
46
#endif
 
47
 
 
48
#include <BRepGProp.hxx>
 
49
#include <GProp_GProps.hxx>
 
50
 
 
51
#include <Base/VectorPy.h>
 
52
#include <Base/GeometryPyCXX.h>
 
53
 
 
54
#include "TopoShape.h"
 
55
#include "TopoShapeSolidPy.h"
 
56
#include "TopoShapeFacePy.h"
 
57
#include "TopoShapeFacePy.cpp"
 
58
 
 
59
#include "BezierSurfacePy.h"
 
60
#include "BSplineSurfacePy.h"
 
61
#include "PlanePy.h"
 
62
#include "CylinderPy.h"
 
63
#include "ConePy.h"
 
64
#include "SpherePy.h"
 
65
#include "OffsetSurfacePy.h"
 
66
#include "SurfaceOfRevolutionPy.h"
 
67
#include "SurfaceOfExtrusionPy.h"
 
68
#include "ToroidPy.h"
 
69
 
 
70
using namespace Part;
 
71
 
 
72
// returns a string which represent the object e.g. when printed in python
 
73
const char *TopoShapeFacePy::representation(void) const
 
74
{
 
75
    // Note: As the return type is 'const char*' we cannot create a temporary
 
76
    // char array neither on the stack because the array would be freed when
 
77
    // leaving the scope nor on the heap because we would have a memory leak.
 
78
    // So we use a static array that is used by all instances of this class.
 
79
    // This, however, is not a problem as long as we only use this method in
 
80
    // _repr().
 
81
 
 
82
    std::stringstream str;
 
83
    str << "<Face object at " << getTopoShapePtr() << ">";
 
84
    static std::string buf;
 
85
    buf = str.str();
 
86
    return buf.c_str();
 
87
}
 
88
 
 
89
PyObject *TopoShapeFacePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
 
90
{
 
91
    // create a new instance of TopoShapeFacePy and the Twin object 
 
92
    return new TopoShapeFacePy(new TopoShape);
 
93
}
 
94
 
 
95
// constructor method
 
96
int TopoShapeFacePy::PyInit(PyObject* args, PyObject* /*kwd*/)
 
97
{
 
98
    PyObject *pW;
 
99
    if (PyArg_ParseTuple(args, "O!", &(Part::TopoShapePy::Type), &pW)) {
 
100
        try {
 
101
            TopoDS_Shape sh = static_cast<Part::TopoShapePy*>(pW)->getTopoShapePtr()->_Shape;
 
102
            if (sh.IsNull()) {
 
103
                PyErr_SetString(PyExc_Exception, "cannot create face out of empty wire");
 
104
                return -1;
 
105
            }
 
106
 
 
107
            if (sh.ShapeType() == TopAbs_WIRE) {
 
108
                BRepBuilderAPI_MakeFace mkFace(TopoDS::Wire(sh));
 
109
                getTopoShapePtr()->_Shape = mkFace.Face();
 
110
                return 0;
 
111
            }
 
112
        }
 
113
        catch (Standard_Failure) {
 
114
            Handle_Standard_Failure e = Standard_Failure::Caught();
 
115
            PyErr_SetString(PyExc_Exception, e->GetMessageString());
 
116
            return -1;
 
117
        }
 
118
    }
 
119
 
 
120
    PyErr_Clear();
 
121
    PyObject *surf, *bound=0;
 
122
    if (PyArg_ParseTuple(args, "O!|O!", &(GeometryPy::Type), &surf, &(PyList_Type), &bound)) {
 
123
        try {
 
124
            Handle_Geom_Surface S = Handle_Geom_Surface::DownCast
 
125
                (static_cast<GeometryPy*>(surf)->getGeometryPtr()->handle());
 
126
            if (S.IsNull()) {
 
127
                PyErr_SetString(PyExc_TypeError, "geometry is not a valid surface");
 
128
                return -1;
 
129
            }
 
130
 
 
131
            BRepBuilderAPI_MakeFace mkFace(S);
 
132
            if (bound) {
 
133
                Py::List list(bound);
 
134
                for (Py::List::iterator it = list.begin(); it != list.end(); ++it) {
 
135
                    PyObject* item = (*it).ptr();
 
136
                    if (PyObject_TypeCheck(item, &(Part::TopoShapePy::Type))) {
 
137
                        TopoDS_Shape sh = static_cast<Part::TopoShapePy*>(item)->getTopoShapePtr()->_Shape;
 
138
                        if (sh.ShapeType() == TopAbs_WIRE)
 
139
                            mkFace.Add(TopoDS::Wire(sh));
 
140
                        else {
 
141
                            PyErr_SetString(PyExc_TypeError, "shape is not a wire");
 
142
                            return -1;
 
143
                        }
 
144
                    }
 
145
                    else {
 
146
                        PyErr_SetString(PyExc_TypeError, "item is not a shape");
 
147
                        return -1;
 
148
                    }
 
149
                }
 
150
            }
 
151
 
 
152
            getTopoShapePtr()->_Shape = mkFace.Face();
 
153
            return 0;
 
154
        }
 
155
        catch (Standard_Failure) {
 
156
            Handle_Standard_Failure e = Standard_Failure::Caught();
 
157
            PyErr_SetString(PyExc_Exception, e->GetMessageString());
 
158
            return -1;
 
159
        }
 
160
    }
 
161
 
 
162
    PyErr_SetString(PyExc_Exception, "wire or list of wires expected");
 
163
    return -1;
 
164
}
 
165
 
 
166
Py::Object TopoShapeFacePy::getSurface() const
 
167
{
 
168
    TopoDS_Face f = TopoDS::Face(getTopoShapePtr()->_Shape);
 
169
    BRepAdaptor_Surface adapt(f);
 
170
    switch(adapt.GetType())
 
171
    {
 
172
    case GeomAbs_Plane:
 
173
        {
 
174
            GeomPlane* plane = new GeomPlane();
 
175
            Handle_Geom_Plane this_surf = Handle_Geom_Plane::DownCast
 
176
                (plane->handle());
 
177
            this_surf->SetPln(adapt.Plane());
 
178
            return Py::Object(new PlanePy(plane),true);
 
179
        }
 
180
    case GeomAbs_Cylinder:
 
181
        {
 
182
            GeomCylinder* cylinder = new GeomCylinder();
 
183
            Handle_Geom_CylindricalSurface this_surf = Handle_Geom_CylindricalSurface::DownCast
 
184
                (cylinder->handle());
 
185
            this_surf->SetCylinder(adapt.Cylinder());
 
186
            return Py::Object(new CylinderPy(cylinder),true);
 
187
        }
 
188
    case GeomAbs_Cone:
 
189
        {
 
190
            GeomCone* cone = new GeomCone();
 
191
            Handle_Geom_ConicalSurface this_surf = Handle_Geom_ConicalSurface::DownCast
 
192
                (cone->handle());
 
193
            this_surf->SetCone(adapt.Cone());
 
194
            return Py::Object(new ConePy(cone),true);
 
195
        }
 
196
    case GeomAbs_Sphere:
 
197
        {
 
198
            GeomSphere* sphere = new GeomSphere();
 
199
            Handle_Geom_SphericalSurface this_surf = Handle_Geom_SphericalSurface::DownCast
 
200
                (sphere->handle());
 
201
            this_surf->SetSphere(adapt.Sphere());
 
202
            return Py::Object(new SpherePy(sphere),true);
 
203
        }
 
204
    case GeomAbs_Torus:
 
205
        {
 
206
            GeomToroid* toroid = new GeomToroid();
 
207
            Handle_Geom_ToroidalSurface this_surf = Handle_Geom_ToroidalSurface::DownCast
 
208
                (toroid->handle());
 
209
            this_surf->SetTorus(adapt.Torus());
 
210
            return Py::Object(new ToroidPy(toroid),true);
 
211
        }
 
212
    case GeomAbs_BezierSurface:
 
213
        {
 
214
            GeomBezierSurface* surf = new GeomBezierSurface(adapt.Bezier());
 
215
            return Py::Object(new BezierSurfacePy(surf),true);
 
216
        }
 
217
    case GeomAbs_BSplineSurface:
 
218
        {
 
219
            GeomBSplineSurface* surf = new GeomBSplineSurface(adapt.BSpline());
 
220
            return Py::Object(new BSplineSurfacePy(surf),true);
 
221
        }
 
222
    case GeomAbs_SurfaceOfRevolution:
 
223
        {
 
224
            Handle_Geom_Surface s = BRep_Tool::Surface(f);
 
225
            Handle_Geom_SurfaceOfRevolution rev = Handle_Geom_SurfaceOfRevolution::DownCast(s);
 
226
            if (!rev.IsNull()) {
 
227
                GeomSurfaceOfRevolution* surf = new GeomSurfaceOfRevolution(rev);
 
228
                return Py::Object(new SurfaceOfRevolutionPy(surf),true);
 
229
            }
 
230
        }
 
231
    case GeomAbs_SurfaceOfExtrusion:
 
232
        {
 
233
            Handle_Geom_Surface s = BRep_Tool::Surface(f);
 
234
            Handle_Geom_SurfaceOfLinearExtrusion ext = Handle_Geom_SurfaceOfLinearExtrusion::DownCast(s);
 
235
            if (!ext.IsNull()) {
 
236
                GeomSurfaceOfExtrusion* surf = new GeomSurfaceOfExtrusion(ext);
 
237
                return Py::Object(new SurfaceOfExtrusionPy(surf),true);
 
238
            }
 
239
        }
 
240
    case GeomAbs_OffsetSurface:
 
241
        {
 
242
            Handle_Geom_Surface s = BRep_Tool::Surface(f);
 
243
            Handle_Geom_OffsetSurface off = Handle_Geom_OffsetSurface::DownCast(s);
 
244
            if (!off.IsNull()) {
 
245
                GeomOffsetSurface* surf = new GeomOffsetSurface(off);
 
246
                return Py::Object(new OffsetSurfacePy(surf),true);
 
247
            }
 
248
        }
 
249
    case GeomAbs_OtherSurface:
 
250
        break;
 
251
    }
 
252
 
 
253
    throw Py::TypeError("undefined surface type");
 
254
}
 
255
 
 
256
Py::Object TopoShapeFacePy::getWire(void) const
 
257
{
 
258
    TopoDS_Shape clSh = getTopoShapePtr()->_Shape;
 
259
    if (clSh.ShapeType() == TopAbs_FACE) {
 
260
        TopoDS_Face clFace = (TopoDS_Face&)clSh;
 
261
        TopoDS_Wire clWire = ShapeAnalysis::OuterWire(clFace);
 
262
        return Py::Object(new TopoShapePy(new TopoShape(clWire)),true);
 
263
    }
 
264
    else
 
265
        throw "Internal error, TopoDS_Shape is not a face!";
 
266
 
 
267
    return Py::Object();
 
268
}
 
269
 
 
270
Py::Object TopoShapeFacePy::getCenterOfMass(void) const
 
271
{
 
272
    GProp_GProps props;
 
273
    BRepGProp::SurfaceProperties(getTopoShapePtr()->_Shape, props);
 
274
    gp_Pnt c = props.CentreOfMass();
 
275
    return Py::Vector(Base::Vector3d(c.X(),c.Y(),c.Z()));
 
276
}
 
277
 
 
278
PyObject *TopoShapeFacePy::getCustomAttributes(const char* attr) const
 
279
{
 
280
    return 0;
 
281
}
 
282
 
 
283
int TopoShapeFacePy::setCustomAttributes(const char* attr, PyObject *obj)
 
284
{
 
285
    return 0; 
 
286
}