FreeCAD

Форк
0
/
OffsetCurve2dPyImp.cpp 
135 строк · 4.9 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2016 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 <Geom2d_OffsetCurve.hxx>
26
#endif
27

28
#include "Geom2d/OffsetCurve2dPy.h"
29
#include "Geom2d/OffsetCurve2dPy.cpp"
30
#include "OCCError.h"
31

32

33
using namespace Part;
34

35
// returns a string which represents the object e.g. when printed in python
36
std::string OffsetCurve2dPy::representation() const
37
{
38
    return "<OffsetCurve2d object>";
39
}
40

41
PyObject *OffsetCurve2dPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
42
{
43
    // create a new instance of OffsetCurve2dPy and the Twin object
44
    return new OffsetCurve2dPy(new Geom2dOffsetCurve);
45
}
46

47
// constructor method
48
int OffsetCurve2dPy::PyInit(PyObject* args, PyObject* /*kwd*/)
49
{
50
    PyObject* pGeom;
51
    double offset;
52
    if (!PyArg_ParseTuple(args, "O!d",
53
                            &(Curve2dPy::Type), &pGeom,
54
                            &offset))
55
        return -1;
56

57
    Curve2dPy* pcGeo = static_cast<Curve2dPy*>(pGeom);
58
    Handle(Geom2d_Curve) curve = Handle(Geom2d_Curve)::DownCast
59
        (pcGeo->getGeometry2dPtr()->handle());
60
    if (curve.IsNull()) {
61
        PyErr_SetString(PyExc_TypeError, "geometry is not a curve");
62
        return -1;
63
    }
64

65
    try {
66
        Handle(Geom2d_OffsetCurve) curve2 = new Geom2d_OffsetCurve(curve, offset);
67
        getGeom2dOffsetCurvePtr()->setHandle(curve2);
68
        return 0;
69
    }
70
    catch (Standard_Failure& e) {
71

72
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
73
        return -1;
74
    }
75
}
76

77
Py::Float OffsetCurve2dPy::getOffsetValue() const
78
{
79
    Handle(Geom2d_OffsetCurve) curve = Handle(Geom2d_OffsetCurve)::DownCast(getGeometry2dPtr()->handle());
80
    return Py::Float(curve->Offset());
81
}
82

83
void OffsetCurve2dPy::setOffsetValue(Py::Float arg)
84
{
85
    Handle(Geom2d_OffsetCurve) curve = Handle(Geom2d_OffsetCurve)::DownCast(getGeometry2dPtr()->handle());
86
    curve->SetOffsetValue((double)arg);
87
}
88

89
Py::Object OffsetCurve2dPy::getBasisCurve() const
90
{
91
    Handle(Geom2d_OffsetCurve) curve = Handle(Geom2d_OffsetCurve)::DownCast(getGeometry2dPtr()->handle());
92
    Handle(Geom2d_Curve) basis = curve->BasisCurve();
93
    if (basis.IsNull())
94
        return Py::None();
95
    std::unique_ptr<Geom2dCurve> geo2d = makeFromCurve2d(basis);
96
    if (!geo2d)
97
        throw Py::RuntimeError("Unknown curve type");
98
    return Py::asObject(geo2d->getPyObject());
99
}
100

101
void OffsetCurve2dPy::setBasisCurve(Py::Object arg)
102
{
103
    PyObject* p = arg.ptr();
104
    if (PyObject_TypeCheck(p, &(Curve2dPy::Type))) {
105
        Curve2dPy* pcGeo = static_cast<Curve2dPy*>(p);
106
        Handle(Geom2d_Curve) curve = Handle(Geom2d_Curve)::DownCast
107
            (pcGeo->getGeometry2dPtr()->handle());
108
        if (curve.IsNull()) {
109
            throw Py::TypeError("geometry is not a curve");
110
        }
111

112
        Handle(Geom2d_OffsetCurve) curve2 = Handle(Geom2d_OffsetCurve)::DownCast
113
            (getGeometry2dPtr()->handle());
114
        if (curve == curve2) {
115
            throw Py::RuntimeError("cannot set this curve as basis");
116
        }
117

118
        try {
119
            curve2->SetBasisCurve(curve);
120
        }
121
        catch (Standard_Failure& e) {
122
            throw Py::RuntimeError(e.GetMessageString());
123
        }
124
    }
125
}
126

127
PyObject *OffsetCurve2dPy::getCustomAttributes(const char* /*attr*/) const
128
{
129
    return nullptr;
130
}
131

132
int OffsetCurve2dPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
133
{
134
    return 0;
135
}
136

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

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

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

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