FreeCAD

Форк
0
/
TopoShapeVertexPyImp.cpp 
212 строк · 7.1 Кб
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 <gp_Pnt.hxx>
27
# include <BRep_Builder.hxx>
28
# include <BRep_Tool.hxx>
29
# include <BRepBuilderAPI_MakeVertex.hxx>
30
# include <Geom_CartesianPoint.hxx>
31
# include <Standard_Failure.hxx>
32
# include <TopoDS.hxx>
33
# include <TopoDS_Vertex.hxx>
34
#endif
35

36
#include <Base/Vector3D.h>
37
#include <Base/VectorPy.h>
38

39
#include "TopoShapeVertexPy.h"
40
#include "TopoShapeVertexPy.cpp"
41
#include "PointPy.h"
42

43

44
using namespace Part;
45

46
// returns a string which represents the object e.g. when printed in python
47
std::string TopoShapeVertexPy::representation() const
48
{
49
    std::stringstream str;
50
    str << "<Vertex object at " << getTopoShapePtr() << ">";
51

52
    return str.str();
53
}
54

55
PyObject *TopoShapeVertexPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
56
{
57
    // create a new instance of TopoShapeVertexPy and the Twin object
58
    return new TopoShapeVertexPy(new TopoShape);
59
}
60

61
// constructor method
62
int TopoShapeVertexPy::PyInit(PyObject* args, PyObject* /*kwd*/)
63
{
64
    if (PyArg_ParseTuple(args, "")) {
65
        // Undefined Vertex
66
        getTopoShapePtr()->setShape(TopoDS_Vertex());
67
        return 0;
68
    }
69

70
    PyErr_Clear();
71
    double x=0.0,y=0.0,z=0.0;
72
    PyObject *object;
73
    bool success = false;
74
    if (PyArg_ParseTuple(args, "|ddd", &x,&y,&z)) {
75
        // do nothing here
76
        success = true;
77
    }
78
    if (!success) {
79
        PyErr_Clear(); // set by PyArg_ParseTuple()
80
        if (PyArg_ParseTuple(args,"O!",&(Base::VectorPy::Type), &object)) {
81
            Base::Vector3d* ptr = static_cast<Base::VectorPy*>(object)->getVectorPtr();
82
            x = ptr->x;
83
            y = ptr->y;
84
            z = ptr->z;
85
            success = true;
86
        }
87
    }
88
    if (!success) {
89
        PyErr_Clear(); // set by PyArg_ParseTuple()
90
        if (PyArg_ParseTuple(args,"O!",&(PyTuple_Type), &object)) {
91
            try {
92
                Py::Tuple tuple(object);
93
                x = Py::Float(tuple.getItem(0));
94
                y = Py::Float(tuple.getItem(1));
95
                z = Py::Float(tuple.getItem(2));
96
                success = true;
97
            }
98
            catch (const Py::Exception&) {
99
                return -1;
100
            }
101
        }
102
    }
103
    if (!success) {
104
        PyErr_Clear(); // set by PyArg_ParseTuple()
105
        if (PyArg_ParseTuple(args,"O!",&(PointPy::Type), &object)) {
106
            Handle(Geom_CartesianPoint) this_point = Handle(Geom_CartesianPoint)::DownCast
107
                (static_cast<PointPy*>(object)->getGeomPointPtr()->handle());
108
            gp_Pnt pnt = this_point->Pnt();
109
            x = pnt.X();
110
            y = pnt.Y();
111
            z = pnt.Z();
112
            success = true;
113
        }
114
    }
115
    if (!success) {
116
        PyErr_Clear(); // set by PyArg_ParseTuple()
117
        if (PyArg_ParseTuple(args,"O!",&(Part::TopoShapePy::Type), &object)) {
118
            TopoShape* ptr = static_cast<TopoShapePy*>(object)->getTopoShapePtr();
119
            TopoDS_Shape shape = ptr->getShape();
120
            if (!shape.IsNull() && shape.ShapeType() == TopAbs_VERTEX) {
121
                TopoShapeVertexPy::PointerType vert = getTopoShapePtr();
122
                vert->setShape(ptr->getShape());
123
                return 0;
124
            }
125
        }
126
    }
127
    if (!success) {
128
        PyErr_SetString(PyExc_TypeError, "Either three floats, tuple, vector or vertex expected");
129
        return -1;
130
    }
131

132
    TopoShapeVertexPy::PointerType ptr = getTopoShapePtr();
133
    BRepBuilderAPI_MakeVertex aBuilder(gp_Pnt(x,y,z));
134
    TopoDS_Shape s = aBuilder.Vertex();
135
    ptr->setShape(s);
136

137
    return 0;
138
}
139

140
Py::Float TopoShapeVertexPy::getTolerance() const
141
{
142
    const TopoDS_Vertex& v = TopoDS::Vertex(getTopoShapePtr()->getShape());
143
    return Py::Float(BRep_Tool::Tolerance(v));
144
}
145

146
void TopoShapeVertexPy::setTolerance(Py::Float tol)
147
{
148
    BRep_Builder aBuilder;
149
    const TopoDS_Vertex& v = TopoDS::Vertex(getTopoShapePtr()->getShape());
150
    aBuilder.UpdateVertex(v, (double)tol);
151
}
152

153
Py::Float TopoShapeVertexPy::getX() const
154
{
155
    try {
156
        const TopoDS_Vertex& v = TopoDS::Vertex(getTopoShapePtr()->getShape());
157
        return Py::Float(BRep_Tool::Pnt(v).X());
158
    }
159
    catch (Standard_Failure& e) {
160

161
        throw Py::RuntimeError(e.GetMessageString());
162
    }
163
}
164

165
Py::Float TopoShapeVertexPy::getY() const
166
{
167
    try {
168
        const TopoDS_Vertex& v = TopoDS::Vertex(getTopoShapePtr()->getShape());
169
        return Py::Float(BRep_Tool::Pnt(v).Y());
170
    }
171
    catch (Standard_Failure& e) {
172

173
        throw Py::RuntimeError(e.GetMessageString());
174
    }
175
}
176

177
Py::Float TopoShapeVertexPy::getZ() const
178
{
179
    try {
180
        const TopoDS_Vertex& v = TopoDS::Vertex(getTopoShapePtr()->getShape());
181
        return Py::Float(BRep_Tool::Pnt(v).Z());
182
    }
183
    catch (Standard_Failure& e) {
184

185
        throw Py::RuntimeError(e.GetMessageString());
186
    }
187
}
188

189
Py::Object TopoShapeVertexPy::getPoint() const
190
{
191
    try {
192
        const TopoDS_Vertex& v = TopoDS::Vertex(getTopoShapePtr()->getShape());
193
        gp_Pnt p = BRep_Tool::Pnt(v);
194
        Base::PyObjectBase* pnt = new Base::VectorPy(new Base::Vector3d(p.X(),p.Y(),p.Z()));
195
        pnt->setNotTracking();
196
        return Py::asObject(pnt);
197
    }
198
    catch (Standard_Failure& e) {
199

200
        throw Py::RuntimeError(e.GetMessageString());
201
    }
202
}
203

204
PyObject *TopoShapeVertexPy::getCustomAttributes(const char* /*attr*/) const
205
{
206
    return nullptr;
207
}
208

209
int TopoShapeVertexPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
210
{
211
    return 0;
212
}
213

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

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

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

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