FreeCAD

Форк
0
/
ChFi2d_ChamferAPIPyImp.cpp 
151 строка · 5.9 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2022 Werner Mayer <wmayer[at]users.sourceforge.net>     *
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
#ifndef _PreComp_
25
# include <Standard_Failure.hxx>
26
# include <TopoDS.hxx>
27
# include <TopoDS_Edge.hxx>
28
#endif
29

30
#include "ChFi2d/ChFi2d_ChamferAPIPy.h"
31
#include "ChFi2d/ChFi2d_ChamferAPIPy.cpp"
32
#include "TopoShapeEdgePy.h"
33
#include "TopoShapeWirePy.h"
34

35

36
using namespace Part;
37

38
PyObject *ChFi2d_ChamferAPIPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
39
{
40
    // create a new instance of ChFi2d_ChamferAPIPy and the Twin object
41
    return new ChFi2d_ChamferAPIPy(new ChFi2d_ChamferAPI);
42
}
43

44
// constructor method
45
int ChFi2d_ChamferAPIPy::PyInit(PyObject* args, PyObject* /*kwd*/)
46
{
47
    // Note: Although the C++ class has a default constructor it would cause a crash
48
    // if the algorithm is not initialized with a wire/edges.
49
    // Thus, in the Python API it is disabled.
50
    PyObject* wire;
51
    if (PyArg_ParseTuple(args, "O!", &TopoShapeWirePy::Type, &wire)) {
52
        TopoDS_Shape shape = static_cast<TopoShapeWirePy*>(wire)->getTopoShapePtr()->getShape();
53
        getChFi2d_ChamferAPIPtr()->Init(TopoDS::Wire(shape));
54
        return 0;
55
    }
56

57
    PyErr_Clear();
58
    PyObject* edge1;
59
    PyObject* edge2;
60
    if (PyArg_ParseTuple(args, "O!O!", &TopoShapeEdgePy::Type, &edge1,
61
                                       &TopoShapeEdgePy::Type, &edge2)) {
62
        TopoDS_Shape shape1 = static_cast<TopoShapeEdgePy*>(edge1)->getTopoShapePtr()->getShape();
63
        TopoDS_Shape shape2 = static_cast<TopoShapeEdgePy*>(edge2)->getTopoShapePtr()->getShape();
64
        getChFi2d_ChamferAPIPtr()->Init(TopoDS::Edge(shape1), TopoDS::Edge(shape2));
65
        return 0;
66
    }
67

68
    PyErr_SetString(PyExc_TypeError, "Wrong arguments:\n"
69
                                     "-- ChamferAPI()\n"
70
                                     "-- ChamferAPI(wire)"
71
                                     "-- ChamferAPI(edge, edge)\n");
72
    return -1;
73
}
74

75
// returns a string which represents the object e.g. when printed in python
76
std::string ChFi2d_ChamferAPIPy::representation() const
77
{
78
    return {"<ChamferAPI object>"};
79
}
80

81
PyObject* ChFi2d_ChamferAPIPy::init(PyObject *args)
82
{
83
    PyObject* wire;
84
    if (PyArg_ParseTuple(args, "O!", &TopoShapeWirePy::Type, &wire)) {
85
        TopoDS_Shape shape = static_cast<TopoShapeWirePy*>(wire)->getTopoShapePtr()->getShape();
86
        getChFi2d_ChamferAPIPtr()->Init(TopoDS::Wire(shape));
87
        Py_Return;
88
    }
89

90
    PyErr_Clear();
91
    PyObject* edge1;
92
    PyObject* edge2;
93
    if (PyArg_ParseTuple(args, "O!O!", &TopoShapeEdgePy::Type, &edge1,
94
                                       &TopoShapeEdgePy::Type, &edge2)) {
95
        TopoDS_Shape shape1 = static_cast<TopoShapeEdgePy*>(edge1)->getTopoShapePtr()->getShape();
96
        TopoDS_Shape shape2 = static_cast<TopoShapeEdgePy*>(edge2)->getTopoShapePtr()->getShape();
97
        getChFi2d_ChamferAPIPtr()->Init(TopoDS::Edge(shape1), TopoDS::Edge(shape2));
98
        Py_Return;
99
    }
100

101
    PyErr_SetString(PyExc_TypeError, "Wrong arguments:\n"
102
                                     "-- init(wire)"
103
                                     "-- init(edge, edge)\n");
104
    return nullptr;
105
}
106

107
PyObject* ChFi2d_ChamferAPIPy::perform(PyObject *args)
108
{
109
    if (!PyArg_ParseTuple(args, ""))
110
        return nullptr;
111

112
    try {
113
        bool ok = getChFi2d_ChamferAPIPtr()->Perform();
114
        return Py::new_reference_to(Py::Boolean(ok));
115
    }
116
    catch (Standard_Failure& e) {
117
        PyErr_SetString(Base::PyExc_FC_CADKernelError, e.GetMessageString());
118
        return nullptr;
119
    }
120
}
121

122
PyObject* ChFi2d_ChamferAPIPy::result(PyObject *args)
123
{
124
    double length1, length2;
125
    if (!PyArg_ParseTuple(args, "dd", &length1, &length2))
126
        return nullptr;
127

128
    try {
129
        TopoDS_Edge theEdge1, theEdge2;
130
        TopoDS_Shape res_edge = getChFi2d_ChamferAPIPtr()->Result(theEdge1, theEdge2, length1, length2);
131

132
        Py::TupleN tuple(Py::asObject(TopoShape(res_edge).getPyObject()),
133
                         Py::asObject(TopoShape(theEdge1).getPyObject()),
134
                         Py::asObject(TopoShape(theEdge2).getPyObject()));
135
        return Py::new_reference_to(tuple);
136
    }
137
    catch (Standard_Failure& e) {
138
        PyErr_SetString(Base::PyExc_FC_CADKernelError, e.GetMessageString());
139
        return nullptr;
140
    }
141
}
142

143
PyObject *ChFi2d_ChamferAPIPy::getCustomAttributes(const char* /*attr*/) const
144
{
145
    return nullptr;
146
}
147

148
int ChFi2d_ChamferAPIPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
149
{
150
    return 0;
151
}
152

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

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

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

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