-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
TopoShapeSolidPyImp.cpp
311 lines (276 loc) · 11 KB
/
TopoShapeSolidPyImp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/***************************************************************************
* Copyright (c) Jürgen Riegel (juergen.riegel@web.de) 2008 *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#include <Standard_Version.hxx>
#include <BRepGProp.hxx>
#include <BRepTools.hxx>
#include <BRepOffset_MakeOffset.hxx>
#if OCC_VERSION_HEX >= 0x060600
#include <BRepClass3d.hxx>
#endif
#include <GProp_GProps.hxx>
#include <GProp_PrincipalProps.hxx>
#include <BRepBuilderAPI_MakeSolid.hxx>
#include <BRepLib.hxx>
# include <Precision.hxx>
#include <TopExp_Explorer.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Solid.hxx>
#include <TopoDS_Shell.hxx>
#include <gp_Ax1.hxx>
#include <gp_Pnt.hxx>
#include <gp_Dir.hxx>
#include <Standard_Failure.hxx>
#include <Base/VectorPy.h>
#include <Base/GeometryPyCXX.h>
#include "TopoShape.h"
#include "Tools.h"
// inclusion of the generated files (generated out of TopoShapeSolidPy.xml)
#include "TopoShapeShellPy.h"
#include "TopoShapeSolidPy.h"
#include "TopoShapeSolidPy.cpp"
using namespace Part;
// returns a string which represents the object e.g. when printed in python
std::string TopoShapeSolidPy::representation(void) const
{
std::stringstream str;
str << "<Solid object at " << getTopoShapePtr() << ">";
return str.str();
}
PyObject *TopoShapeSolidPy::PyMake(struct _typeobject *, PyObject *, PyObject *)
{
// create a new instance of TopoShapeSolidPy and the Twin object
return new TopoShapeSolidPy(new TopoShape);
}
// constructor method
int TopoShapeSolidPy::PyInit(PyObject* args, PyObject* /*kwd*/)
{
PyObject *obj;
if (!PyArg_ParseTuple(args, "O!", &(TopoShapePy::Type), &obj))
return -1;
try {
BRepBuilderAPI_MakeSolid mkSolid;
const TopoDS_Shape& shape = static_cast<TopoShapePy*>(obj)
->getTopoShapePtr()->_Shape;
TopExp_Explorer anExp (shape, TopAbs_SHELL);
int count=0;
for (; anExp.More(); anExp.Next()) {
++count;
mkSolid.Add(TopoDS::Shell(anExp.Current()));
}
if (count == 0)
Standard_Failure::Raise("No shells found in shape");
TopoDS_Solid solid = mkSolid.Solid();
BRepLib::OrientClosedSolid(solid);
getTopoShapePtr()->_Shape = solid;
}
catch (Standard_Failure) {
PyErr_SetString(PyExc_Exception, "creation of solid failed");
return -1;
}
return 0;
}
Py::Object TopoShapeSolidPy::getMass(void) const
{
GProp_GProps props;
BRepGProp::VolumeProperties(getTopoShapePtr()->_Shape, props);
double c = props.Mass();
return Py::Float(c);
}
Py::Object TopoShapeSolidPy::getCenterOfMass(void) const
{
GProp_GProps props;
BRepGProp::VolumeProperties(getTopoShapePtr()->_Shape, props);
gp_Pnt c = props.CentreOfMass();
return Py::Vector(Base::Vector3d(c.X(),c.Y(),c.Z()));
}
Py::Object TopoShapeSolidPy::getMatrixOfInertia(void) const
{
GProp_GProps props;
BRepGProp::VolumeProperties(getTopoShapePtr()->_Shape, props);
gp_Mat m = props.MatrixOfInertia();
Base::Matrix4D mat;
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
mat[i][j] = m(i+1,j+1);
}
}
return Py::Matrix(mat);
}
Py::Object TopoShapeSolidPy::getStaticMoments(void) const
{
GProp_GProps props;
BRepGProp::VolumeProperties(getTopoShapePtr()->_Shape, props);
Standard_Real lx,ly,lz;
props.StaticMoments(lx,ly,lz);
Py::Tuple tuple(3);
tuple.setItem(0, Py::Float(lx));
tuple.setItem(1, Py::Float(ly));
tuple.setItem(2, Py::Float(lz));
return tuple;
}
Py::Dict TopoShapeSolidPy::getPrincipalProperties(void) const
{
GProp_GProps props;
BRepGProp::VolumeProperties(getTopoShapePtr()->_Shape, props);
GProp_PrincipalProps pprops = props.PrincipalProperties();
Py::Dict dict;
dict.setItem("SymmetryAxis", Py::Boolean(pprops.HasSymmetryAxis() ? true : false));
dict.setItem("SymmetryPoint", Py::Boolean(pprops.HasSymmetryPoint() ? true : false));
Standard_Real lx,ly,lz;
pprops.Moments(lx,ly,lz);
Py::Tuple tuple(3);
tuple.setItem(0, Py::Float(lx));
tuple.setItem(1, Py::Float(ly));
tuple.setItem(2, Py::Float(lz));
dict.setItem("Moments",tuple);
dict.setItem("FirstAxisOfInertia",Py::Vector(Base::convertTo
<Base::Vector3d>(pprops.FirstAxisOfInertia())));
dict.setItem("SecondAxisOfInertia",Py::Vector(Base::convertTo
<Base::Vector3d>(pprops.SecondAxisOfInertia())));
dict.setItem("ThirdAxisOfInertia",Py::Vector(Base::convertTo
<Base::Vector3d>(pprops.ThirdAxisOfInertia())));
Standard_Real Rxx,Ryy,Rzz;
pprops.RadiusOfGyration(Rxx,Ryy,Rzz);
Py::Tuple rog(3);
rog.setItem(0, Py::Float(Rxx));
rog.setItem(1, Py::Float(Ryy));
rog.setItem(2, Py::Float(Rzz));
dict.setItem("RadiusOfGyration",rog);
return dict;
}
Py::Object TopoShapeSolidPy::getOuterShell(void) const
{
TopoDS_Shell shell;
const TopoDS_Shape& shape = getTopoShapePtr()->_Shape;
if (!shape.IsNull() && shape.ShapeType() == TopAbs_SOLID)
#if OCC_VERSION_HEX >= 0x060600
shell = BRepClass3d::OuterShell(TopoDS::Solid(shape));
#else
shell = BRepTools::OuterShell(TopoDS::Solid(shape));
#endif
return Py::Object(new TopoShapeShellPy(new TopoShape(shell)),true);
}
PyObject* TopoShapeSolidPy::getMomentOfInertia(PyObject *args)
{
PyObject *p,*d;
if (!PyArg_ParseTuple(args, "O!O!",&Base::VectorPy::Type,&p
,&Base::VectorPy::Type,&d))
return 0;
Base::Vector3d pnt = Py::Vector(p,false).toVector();
Base::Vector3d dir = Py::Vector(d,false).toVector();
try {
GProp_GProps props;
BRepGProp::VolumeProperties(getTopoShapePtr()->_Shape, props);
double r = props.MomentOfInertia(gp_Ax1(Base::convertTo<gp_Pnt>(pnt),
Base::convertTo<gp_Dir>(dir)));
return PyFloat_FromDouble(r);
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
PyErr_SetString(PyExc_Exception, e->GetMessageString());
return 0;
}
}
PyObject* TopoShapeSolidPy::getRadiusOfGyration(PyObject *args)
{
PyObject *p,*d;
if (!PyArg_ParseTuple(args, "O!O!",&Base::VectorPy::Type,&p
,&Base::VectorPy::Type,&d))
return 0;
Base::Vector3d pnt = Py::Vector(p,false).toVector();
Base::Vector3d dir = Py::Vector(d,false).toVector();
try {
GProp_GProps props;
BRepGProp::VolumeProperties(getTopoShapePtr()->_Shape, props);
double r = props.RadiusOfGyration(gp_Ax1(Base::convertTo<gp_Pnt>(pnt),
Base::convertTo<gp_Dir>(dir)));
return PyFloat_FromDouble(r);
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
PyErr_SetString(PyExc_Exception, e->GetMessageString());
return 0;
}
}
PyObject* TopoShapeSolidPy::extrudeFaces(PyObject *args)
{
PyObject *obj;
Standard_Real offset;
const TopoDS_Shape& shape = getTopoShapePtr()->_Shape;
BRepOffset_MakeOffset builder;
// Set here an offset value higher than the tolerance
builder.Initialize(shape,1.0,Precision::Confusion(),BRepOffset_Skin,Standard_False,Standard_False,GeomAbs_Intersection);
TopExp_Explorer xp(shape,TopAbs_FACE);
while (xp.More()) {
// go through all faces and set offset to zero
builder.SetOffsetOnFace(TopoDS::Face(xp.Current()), 0.0);
xp.Next();
}
bool paramOK = false;
if (!paramOK && PyArg_ParseTuple(args, "Od", &obj,&offset)) {
paramOK = true;
Py::Sequence list(obj);
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
if (PyObject_TypeCheck((*it).ptr(), &(Part::TopoShapePy::Type))) {
// set offset of the requested faces
const TopoDS_Shape& face = static_cast<TopoShapePy*>((*it).ptr())->getTopoShapePtr()->_Shape;
builder.SetOffsetOnFace(TopoDS::Face(face), offset);
}
}
}
PyErr_Clear();
if (!paramOK && PyArg_ParseTuple(args, "O!", &PyDict_Type, &obj)) {
paramOK = true;
Py::Dict dict(obj);
for (Py::Dict::iterator it = dict.begin(); it != dict.end(); ++it) {
if (PyObject_TypeCheck((*it).first.ptr(), &(Part::TopoShapePy::Type))) {
// set offset of the requested faces
const TopoDS_Shape& face = static_cast<TopoShapePy*>((*it).first.ptr())->getTopoShapePtr()->_Shape;
Standard_Real value = (double)Py::Float((*it).second.ptr());
builder.SetOffsetOnFace(TopoDS::Face(face), value);
}
}
}
if (!paramOK) {
PyErr_SetString(PyExc_TypeError, "Wrong parameter");
return 0;
}
try {
builder.MakeOffsetShape();
const TopoDS_Shape& offsetshape = builder.Shape();
return new TopoShapeSolidPy(new TopoShape(offsetshape));
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
PyErr_SetString(PyExc_Exception, e->GetMessageString());
return 0;
}
}
PyObject *TopoShapeSolidPy::getCustomAttributes(const char* /*attr*/) const
{
return 0;
}
int TopoShapeSolidPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
return 0;
}