1
/***************************************************************************
2
* Copyright (c) 2008 Jürgen Riegel <juergen.riegel@web.de> *
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
***************************************************************************/
23
#include "PreCompiled.h"
26
# include <BRepBuilderAPI_MakeSolid.hxx>
27
# include <BRepClass3d.hxx>
28
# include <BRepGProp.hxx>
29
# include <BRepLib.hxx>
30
# include <BRepOffset_MakeOffset.hxx>
31
# include <GProp_GProps.hxx>
32
# include <GProp_PrincipalProps.hxx>
33
# include <Precision.hxx>
34
# include <TopExp_Explorer.hxx>
36
# include <TopoDS_CompSolid.hxx>
37
# include <TopoDS_Shell.hxx>
38
# include <TopoDS_Solid.hxx>
42
# include <Standard_Failure.hxx>
43
# include <Standard_Version.hxx>
46
#include <Base/GeometryPyCXX.h>
47
#include <Base/VectorPy.h>
53
// inclusion of the generated files (generated out of TopoShapeSolidPy.xml)
54
#include "TopoShapeShellPy.h"
55
#include "TopoShapeSolidPy.h"
56
#include "TopoShapeSolidPy.cpp"
61
// returns a string which represents the object e.g. when printed in python
62
std::string TopoShapeSolidPy::representation() const
64
std::stringstream str;
65
str << "<Solid object at " << getTopoShapePtr() << ">";
70
PyObject *TopoShapeSolidPy::PyMake(struct _typeobject *, PyObject *, PyObject *)
72
// create a new instance of TopoShapeSolidPy and the Twin object
73
return new TopoShapeSolidPy(new TopoShape);
77
int TopoShapeSolidPy::PyInit(PyObject* args, PyObject* /*kwd*/)
79
if (PyArg_ParseTuple(args, "")) {
81
getTopoShapePtr()->setShape(TopoDS_Solid());
87
if (!PyArg_ParseTuple(args, "O!", &(TopoShapePy::Type), &obj))
91
getTopoShapePtr()->makeElementSolid(*static_cast<TopoShapePy*>(obj)->getTopoShapePtr());
93
catch (Standard_Failure& err) {
94
std::stringstream errmsg;
95
errmsg << "Creation of solid failed: " << err.GetMessageString();
96
PyErr_SetString(PartExceptionOCCError, errmsg.str().c_str());
103
Py::Object TopoShapeSolidPy::getMass() const
106
BRepGProp::VolumeProperties(getTopoShapePtr()->getShape(), props);
107
double c = props.Mass();
111
Py::Object TopoShapeSolidPy::getCenterOfMass() const
114
BRepGProp::VolumeProperties(getTopoShapePtr()->getShape(), props);
115
gp_Pnt c = props.CentreOfMass();
116
return Py::Vector(Base::Vector3d(c.X(),c.Y(),c.Z()));
119
Py::Object TopoShapeSolidPy::getMatrixOfInertia() const
122
BRepGProp::VolumeProperties(getTopoShapePtr()->getShape(), props);
123
gp_Mat m = props.MatrixOfInertia();
125
for (int i=0; i<3; i++) {
126
for (int j=0; j<3; j++) {
127
mat[i][j] = m(i+1,j+1);
130
return Py::Matrix(mat);
133
Py::Object TopoShapeSolidPy::getStaticMoments() const
136
BRepGProp::VolumeProperties(getTopoShapePtr()->getShape(), props);
137
Standard_Real lx,ly,lz;
138
props.StaticMoments(lx,ly,lz);
140
tuple.setItem(0, Py::Float(lx));
141
tuple.setItem(1, Py::Float(ly));
142
tuple.setItem(2, Py::Float(lz));
146
Py::Dict TopoShapeSolidPy::getPrincipalProperties() const
149
BRepGProp::VolumeProperties(getTopoShapePtr()->getShape(), props);
150
GProp_PrincipalProps pprops = props.PrincipalProperties();
153
dict.setItem("SymmetryAxis", Py::Boolean(pprops.HasSymmetryAxis() ? true : false));
154
dict.setItem("SymmetryPoint", Py::Boolean(pprops.HasSymmetryPoint() ? true : false));
155
Standard_Real lx,ly,lz;
156
pprops.Moments(lx,ly,lz);
158
tuple.setItem(0, Py::Float(lx));
159
tuple.setItem(1, Py::Float(ly));
160
tuple.setItem(2, Py::Float(lz));
161
dict.setItem("Moments",tuple);
162
dict.setItem("FirstAxisOfInertia",Py::Vector(Base::convertTo
163
<Base::Vector3d>(pprops.FirstAxisOfInertia())));
164
dict.setItem("SecondAxisOfInertia",Py::Vector(Base::convertTo
165
<Base::Vector3d>(pprops.SecondAxisOfInertia())));
166
dict.setItem("ThirdAxisOfInertia",Py::Vector(Base::convertTo
167
<Base::Vector3d>(pprops.ThirdAxisOfInertia())));
169
Standard_Real Rxx,Ryy,Rzz;
170
pprops.RadiusOfGyration(Rxx,Ryy,Rzz);
172
rog.setItem(0, Py::Float(Rxx));
173
rog.setItem(1, Py::Float(Ryy));
174
rog.setItem(2, Py::Float(Rzz));
175
dict.setItem("RadiusOfGyration",rog);
179
Py::Object TopoShapeSolidPy::getOuterShell() const
182
const TopoDS_Shape& shape = getTopoShapePtr()->getShape();
183
if (!shape.IsNull() && shape.ShapeType() == TopAbs_SOLID)
184
shell = BRepClass3d::OuterShell(TopoDS::Solid(shape));
187
res.mapSubElement(*getTopoShapePtr());
188
return shape2pyshape(res);
191
PyObject* TopoShapeSolidPy::getMomentOfInertia(PyObject *args)
194
if (!PyArg_ParseTuple(args, "O!O!",&Base::VectorPy::Type,&p
195
,&Base::VectorPy::Type,&d))
197
Base::Vector3d pnt = Py::Vector(p,false).toVector();
198
Base::Vector3d dir = Py::Vector(d,false).toVector();
202
BRepGProp::VolumeProperties(getTopoShapePtr()->getShape(), props);
203
double r = props.MomentOfInertia(gp_Ax1(Base::convertTo<gp_Pnt>(pnt),
204
Base::convertTo<gp_Dir>(dir)));
205
return PyFloat_FromDouble(r);
207
catch (Standard_Failure& e) {
209
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
214
PyObject* TopoShapeSolidPy::getRadiusOfGyration(PyObject *args)
217
if (!PyArg_ParseTuple(args, "O!O!",&Base::VectorPy::Type,&p
218
,&Base::VectorPy::Type,&d))
220
Base::Vector3d pnt = Py::Vector(p,false).toVector();
221
Base::Vector3d dir = Py::Vector(d,false).toVector();
225
BRepGProp::VolumeProperties(getTopoShapePtr()->getShape(), props);
226
double r = props.RadiusOfGyration(gp_Ax1(Base::convertTo<gp_Pnt>(pnt),
227
Base::convertTo<gp_Dir>(dir)));
228
return PyFloat_FromDouble(r);
230
catch (Standard_Failure& e) {
232
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
237
PyObject* TopoShapeSolidPy::offsetFaces(PyObject *args)
240
Standard_Real offset;
242
const TopoDS_Shape& shape = getTopoShapePtr()->getShape();
243
BRepOffset_MakeOffset builder;
244
// Set here an offset value higher than the tolerance
245
builder.Initialize(shape,1.0,Precision::Confusion(),BRepOffset_Skin,Standard_False,Standard_False,GeomAbs_Intersection);
246
TopExp_Explorer xp(shape,TopAbs_FACE);
248
// go through all faces and set offset to zero
249
builder.SetOffsetOnFace(TopoDS::Face(xp.Current()), 0.0);
253
bool paramOK = false;
254
if (PyArg_ParseTuple(args, "Od", &obj,&offset)) {
256
Py::Sequence list(obj);
257
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
258
if (PyObject_TypeCheck((*it).ptr(), &(Part::TopoShapePy::Type))) {
259
// set offset of the requested faces
260
const TopoDS_Shape& face = static_cast<TopoShapePy*>((*it).ptr())->getTopoShapePtr()->getShape();
261
builder.SetOffsetOnFace(TopoDS::Face(face), offset);
267
if (!paramOK && PyArg_ParseTuple(args, "O!", &PyDict_Type, &obj)) {
270
for (Py::Dict::iterator it = dict.begin(); it != dict.end(); ++it) {
271
if (PyObject_TypeCheck((*it).first.ptr(), &(Part::TopoShapePy::Type))) {
272
// set offset of the requested faces
273
const TopoDS_Shape& face = static_cast<TopoShapePy*>((*it).first.ptr())->getTopoShapePtr()->getShape();
274
Standard_Real value = (double)Py::Float((*it).second.ptr());
275
builder.SetOffsetOnFace(TopoDS::Face(face), value);
281
PyErr_SetString(PyExc_TypeError, "Wrong parameter");
286
builder.MakeOffsetShape();
287
const TopoDS_Shape& offsetshape = builder.Shape();
289
res.setShape(offsetshape);
290
return Py::new_reference_to(shape2pyshape(res));
292
catch (Standard_Failure& e) {
294
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
299
PyObject *TopoShapeSolidPy::getCustomAttributes(const char* /*attr*/) const
304
int TopoShapeSolidPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)