FreeCAD

Форк
0
/
TopoShapeSolidPyImp.cpp 
307 строк · 10.7 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2008 Jürgen Riegel <juergen.riegel@web.de>              *
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
#include "PreCompiled.h"
24

25
#ifndef _PreComp_
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>
35
# include <TopoDS.hxx>
36
# include <TopoDS_CompSolid.hxx>
37
# include <TopoDS_Shell.hxx>
38
# include <TopoDS_Solid.hxx>
39
# include <gp_Ax1.hxx>
40
# include <gp_Dir.hxx>
41
# include <gp_Pnt.hxx>
42
# include <Standard_Failure.hxx>
43
# include <Standard_Version.hxx>
44
#endif
45

46
#include <Base/GeometryPyCXX.h>
47
#include <Base/VectorPy.h>
48

49
#include "OCCError.h"
50
#include "PartPyCXX.h"
51
#include "Tools.h"
52

53
// inclusion of the generated files (generated out of TopoShapeSolidPy.xml)
54
#include "TopoShapeShellPy.h"
55
#include "TopoShapeSolidPy.h"
56
#include "TopoShapeSolidPy.cpp"
57

58

59
using namespace Part;
60

61
// returns a string which represents the object e.g. when printed in python
62
std::string TopoShapeSolidPy::representation() const
63
{
64
    std::stringstream str;
65
    str << "<Solid object at " << getTopoShapePtr() << ">";
66

67
    return str.str();
68
}
69

70
PyObject *TopoShapeSolidPy::PyMake(struct _typeobject *, PyObject *, PyObject *)
71
{
72
    // create a new instance of TopoShapeSolidPy and the Twin object
73
    return new TopoShapeSolidPy(new TopoShape);
74
}
75

76
// constructor method
77
int TopoShapeSolidPy::PyInit(PyObject* args, PyObject* /*kwd*/)
78
{
79
    if (PyArg_ParseTuple(args, "")) {
80
        // Undefined Solid
81
        getTopoShapePtr()->setShape(TopoDS_Solid());
82
        return 0;
83
    }
84

85
    PyErr_Clear();
86
    PyObject *obj;
87
    if (!PyArg_ParseTuple(args, "O!", &(TopoShapePy::Type), &obj))
88
        return -1;
89

90
    try {
91
        getTopoShapePtr()->makeElementSolid(*static_cast<TopoShapePy*>(obj)->getTopoShapePtr());
92
    }
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());
97
        return -1;
98
    }
99

100
    return 0;
101
}
102

103
Py::Object TopoShapeSolidPy::getMass() const
104
{
105
    GProp_GProps props;
106
    BRepGProp::VolumeProperties(getTopoShapePtr()->getShape(), props);
107
    double c = props.Mass();
108
    return Py::Float(c);
109
}
110

111
Py::Object TopoShapeSolidPy::getCenterOfMass() const
112
{
113
    GProp_GProps props;
114
    BRepGProp::VolumeProperties(getTopoShapePtr()->getShape(), props);
115
    gp_Pnt c = props.CentreOfMass();
116
    return Py::Vector(Base::Vector3d(c.X(),c.Y(),c.Z()));
117
}
118

119
Py::Object TopoShapeSolidPy::getMatrixOfInertia() const
120
{
121
    GProp_GProps props;
122
    BRepGProp::VolumeProperties(getTopoShapePtr()->getShape(), props);
123
    gp_Mat m = props.MatrixOfInertia();
124
    Base::Matrix4D mat;
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);
128
        }
129
    }
130
    return Py::Matrix(mat);
131
}
132

133
Py::Object TopoShapeSolidPy::getStaticMoments() const
134
{
135
    GProp_GProps props;
136
    BRepGProp::VolumeProperties(getTopoShapePtr()->getShape(), props);
137
    Standard_Real lx,ly,lz;
138
    props.StaticMoments(lx,ly,lz);
139
    Py::Tuple tuple(3);
140
    tuple.setItem(0, Py::Float(lx));
141
    tuple.setItem(1, Py::Float(ly));
142
    tuple.setItem(2, Py::Float(lz));
143
    return tuple;
144
}
145

146
Py::Dict TopoShapeSolidPy::getPrincipalProperties() const
147
{
148
    GProp_GProps props;
149
    BRepGProp::VolumeProperties(getTopoShapePtr()->getShape(), props);
150
    GProp_PrincipalProps pprops = props.PrincipalProperties();
151

152
    Py::Dict dict;
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);
157
    Py::Tuple tuple(3);
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())));
168

169
    Standard_Real Rxx,Ryy,Rzz;
170
    pprops.RadiusOfGyration(Rxx,Ryy,Rzz);
171
    Py::Tuple rog(3);
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);
176
    return dict;
177
}
178

179
Py::Object TopoShapeSolidPy::getOuterShell() const
180
{
181
    TopoDS_Shell shell;
182
    const TopoDS_Shape& shape = getTopoShapePtr()->getShape();
183
    if (!shape.IsNull() && shape.ShapeType() == TopAbs_SOLID)
184
        shell = BRepClass3d::OuterShell(TopoDS::Solid(shape));
185
    TopoShape res;
186
    res.setShape(shell);
187
    res.mapSubElement(*getTopoShapePtr());
188
    return shape2pyshape(res);
189
}
190

191
PyObject* TopoShapeSolidPy::getMomentOfInertia(PyObject *args)
192
{
193
    PyObject *p,*d;
194
    if (!PyArg_ParseTuple(args, "O!O!",&Base::VectorPy::Type,&p
195
                                      ,&Base::VectorPy::Type,&d))
196
        return nullptr;
197
    Base::Vector3d pnt = Py::Vector(p,false).toVector();
198
    Base::Vector3d dir = Py::Vector(d,false).toVector();
199

200
    try {
201
        GProp_GProps props;
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);
206
    }
207
    catch (Standard_Failure& e) {
208

209
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
210
        return nullptr;
211
    }
212
}
213

214
PyObject* TopoShapeSolidPy::getRadiusOfGyration(PyObject *args)
215
{
216
    PyObject *p,*d;
217
    if (!PyArg_ParseTuple(args, "O!O!",&Base::VectorPy::Type,&p
218
                                      ,&Base::VectorPy::Type,&d))
219
        return nullptr;
220
    Base::Vector3d pnt = Py::Vector(p,false).toVector();
221
    Base::Vector3d dir = Py::Vector(d,false).toVector();
222

223
    try {
224
        GProp_GProps props;
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);
229
    }
230
    catch (Standard_Failure& e) {
231

232
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
233
        return nullptr;
234
    }
235
}
236

237
PyObject* TopoShapeSolidPy::offsetFaces(PyObject *args)
238
{
239
    PyObject *obj;
240
    Standard_Real offset;
241

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);
247
    while (xp.More()) {
248
        // go through all faces and set offset to zero
249
        builder.SetOffsetOnFace(TopoDS::Face(xp.Current()), 0.0);
250
        xp.Next();
251
    }
252

253
    bool paramOK = false;
254
    if (PyArg_ParseTuple(args, "Od", &obj,&offset)) {
255
        paramOK = true;
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);
262
            }
263
        }
264
    }
265

266
    PyErr_Clear();
267
    if (!paramOK && PyArg_ParseTuple(args, "O!", &PyDict_Type, &obj)) {
268
        paramOK = true;
269
        Py::Dict dict(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);
276
            }
277
        }
278
    }
279

280
    if (!paramOK) {
281
        PyErr_SetString(PyExc_TypeError, "Wrong parameter");
282
        return nullptr;
283
    }
284

285
    try {
286
        builder.MakeOffsetShape();
287
        const TopoDS_Shape& offsetshape = builder.Shape();
288
        TopoShape res;
289
        res.setShape(offsetshape);
290
        return Py::new_reference_to(shape2pyshape(res));
291
    }
292
    catch (Standard_Failure& e) {
293

294
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
295
        return nullptr;
296
    }
297
}
298

299
PyObject *TopoShapeSolidPy::getCustomAttributes(const char* /*attr*/) const
300
{
301
    return nullptr;
302
}
303

304
int TopoShapeSolidPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
305
{
306
    return 0;
307
}
308

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.