1
/***************************************************************************
2
* Copyright (c) Jļæ½rgen Riegel (juergen.riegel@web.de) 2008 *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
24
#include "PreCompiled.h"
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>
48
#include <BRepGProp.hxx>
49
#include <GProp_GProps.hxx>
51
#include <Base/VectorPy.h>
52
#include <Base/GeometryPyCXX.h>
54
#include "TopoShape.h"
55
#include "TopoShapeSolidPy.h"
56
#include "TopoShapeFacePy.h"
57
#include "TopoShapeFacePy.cpp"
59
#include "BezierSurfacePy.h"
60
#include "BSplineSurfacePy.h"
62
#include "CylinderPy.h"
65
#include "OffsetSurfacePy.h"
66
#include "SurfaceOfRevolutionPy.h"
67
#include "SurfaceOfExtrusionPy.h"
72
// returns a string which represent the object e.g. when printed in python
73
const char *TopoShapeFacePy::representation(void) const
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
82
std::stringstream str;
83
str << "<Face object at " << getTopoShapePtr() << ">";
84
static std::string buf;
89
PyObject *TopoShapeFacePy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
91
// create a new instance of TopoShapeFacePy and the Twin object
92
return new TopoShapeFacePy(new TopoShape);
96
int TopoShapeFacePy::PyInit(PyObject* args, PyObject* /*kwd*/)
99
if (PyArg_ParseTuple(args, "O!", &(Part::TopoShapePy::Type), &pW)) {
101
TopoDS_Shape sh = static_cast<Part::TopoShapePy*>(pW)->getTopoShapePtr()->_Shape;
103
PyErr_SetString(PyExc_Exception, "cannot create face out of empty wire");
107
if (sh.ShapeType() == TopAbs_WIRE) {
108
BRepBuilderAPI_MakeFace mkFace(TopoDS::Wire(sh));
109
getTopoShapePtr()->_Shape = mkFace.Face();
113
catch (Standard_Failure) {
114
Handle_Standard_Failure e = Standard_Failure::Caught();
115
PyErr_SetString(PyExc_Exception, e->GetMessageString());
121
PyObject *surf, *bound=0;
122
if (PyArg_ParseTuple(args, "O!|O!", &(GeometryPy::Type), &surf, &(PyList_Type), &bound)) {
124
Handle_Geom_Surface S = Handle_Geom_Surface::DownCast
125
(static_cast<GeometryPy*>(surf)->getGeometryPtr()->handle());
127
PyErr_SetString(PyExc_TypeError, "geometry is not a valid surface");
131
BRepBuilderAPI_MakeFace mkFace(S);
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));
141
PyErr_SetString(PyExc_TypeError, "shape is not a wire");
146
PyErr_SetString(PyExc_TypeError, "item is not a shape");
152
getTopoShapePtr()->_Shape = mkFace.Face();
155
catch (Standard_Failure) {
156
Handle_Standard_Failure e = Standard_Failure::Caught();
157
PyErr_SetString(PyExc_Exception, e->GetMessageString());
162
PyErr_SetString(PyExc_Exception, "wire or list of wires expected");
166
Py::Object TopoShapeFacePy::getSurface() const
168
TopoDS_Face f = TopoDS::Face(getTopoShapePtr()->_Shape);
169
BRepAdaptor_Surface adapt(f);
170
switch(adapt.GetType())
174
GeomPlane* plane = new GeomPlane();
175
Handle_Geom_Plane this_surf = Handle_Geom_Plane::DownCast
177
this_surf->SetPln(adapt.Plane());
178
return Py::Object(new PlanePy(plane),true);
180
case GeomAbs_Cylinder:
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);
190
GeomCone* cone = new GeomCone();
191
Handle_Geom_ConicalSurface this_surf = Handle_Geom_ConicalSurface::DownCast
193
this_surf->SetCone(adapt.Cone());
194
return Py::Object(new ConePy(cone),true);
198
GeomSphere* sphere = new GeomSphere();
199
Handle_Geom_SphericalSurface this_surf = Handle_Geom_SphericalSurface::DownCast
201
this_surf->SetSphere(adapt.Sphere());
202
return Py::Object(new SpherePy(sphere),true);
206
GeomToroid* toroid = new GeomToroid();
207
Handle_Geom_ToroidalSurface this_surf = Handle_Geom_ToroidalSurface::DownCast
209
this_surf->SetTorus(adapt.Torus());
210
return Py::Object(new ToroidPy(toroid),true);
212
case GeomAbs_BezierSurface:
214
GeomBezierSurface* surf = new GeomBezierSurface(adapt.Bezier());
215
return Py::Object(new BezierSurfacePy(surf),true);
217
case GeomAbs_BSplineSurface:
219
GeomBSplineSurface* surf = new GeomBSplineSurface(adapt.BSpline());
220
return Py::Object(new BSplineSurfacePy(surf),true);
222
case GeomAbs_SurfaceOfRevolution:
224
Handle_Geom_Surface s = BRep_Tool::Surface(f);
225
Handle_Geom_SurfaceOfRevolution rev = Handle_Geom_SurfaceOfRevolution::DownCast(s);
227
GeomSurfaceOfRevolution* surf = new GeomSurfaceOfRevolution(rev);
228
return Py::Object(new SurfaceOfRevolutionPy(surf),true);
231
case GeomAbs_SurfaceOfExtrusion:
233
Handle_Geom_Surface s = BRep_Tool::Surface(f);
234
Handle_Geom_SurfaceOfLinearExtrusion ext = Handle_Geom_SurfaceOfLinearExtrusion::DownCast(s);
236
GeomSurfaceOfExtrusion* surf = new GeomSurfaceOfExtrusion(ext);
237
return Py::Object(new SurfaceOfExtrusionPy(surf),true);
240
case GeomAbs_OffsetSurface:
242
Handle_Geom_Surface s = BRep_Tool::Surface(f);
243
Handle_Geom_OffsetSurface off = Handle_Geom_OffsetSurface::DownCast(s);
245
GeomOffsetSurface* surf = new GeomOffsetSurface(off);
246
return Py::Object(new OffsetSurfacePy(surf),true);
249
case GeomAbs_OtherSurface:
253
throw Py::TypeError("undefined surface type");
256
Py::Object TopoShapeFacePy::getWire(void) const
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);
265
throw "Internal error, TopoDS_Shape is not a face!";
270
Py::Object TopoShapeFacePy::getCenterOfMass(void) const
273
BRepGProp::SurfaceProperties(getTopoShapePtr()->_Shape, props);
274
gp_Pnt c = props.CentreOfMass();
275
return Py::Vector(Base::Vector3d(c.X(),c.Y(),c.Z()));
278
PyObject *TopoShapeFacePy::getCustomAttributes(const char* attr) const
283
int TopoShapeFacePy::setCustomAttributes(const char* attr, PyObject *obj)