FreeCAD

Форк
0
/
OffsetCurvePyImp.cpp 
162 строки · 6.1 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2008 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 <Geom_OffsetCurve.hxx>
26
#endif
27

28
#include <Base/GeometryPyCXX.h>
29
#include <Base/Vector3D.h>
30
#include <Base/VectorPy.h>
31

32
#include "OffsetCurvePy.h"
33
#include "OffsetCurvePy.cpp"
34
#include "Geometry.h"
35
#include "OCCError.h"
36

37

38
using namespace Part;
39

40
// returns a string which represents the object e.g. when printed in python
41
std::string OffsetCurvePy::representation() const
42
{
43
    return "<OffsetCurve object>";
44
}
45

46
PyObject *OffsetCurvePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
47
{
48
    // create a new instance of OffsetCurvePy and the Twin object
49
    return new OffsetCurvePy(new GeomOffsetCurve);
50
}
51

52
// constructor method
53
int OffsetCurvePy::PyInit(PyObject* args, PyObject* /*kwd*/)
54
{
55
    PyObject* pGeom;
56
    PyObject* pDir;
57
    double offset;
58
    if (!PyArg_ParseTuple(args, "O!dO!",
59
                            &(GeometryPy::Type), &pGeom,
60
                            &offset,
61
                            &(Base::VectorPy::Type),&pDir))
62
        return -1;
63

64
    GeometryPy* pcGeo = static_cast<GeometryPy*>(pGeom);
65
    Handle(Geom_Curve) curve = Handle(Geom_Curve)::DownCast
66
        (pcGeo->getGeometryPtr()->handle());
67
    if (curve.IsNull()) {
68
        PyErr_SetString(PyExc_TypeError, "geometry is not a curve");
69
        return -1;
70
    }
71

72
    try {
73
        Base::Vector3d dir = static_cast<Base::VectorPy*>(pDir)->value();
74
        Handle(Geom_OffsetCurve) curve2 = new Geom_OffsetCurve(curve, offset, gp_Dir(dir.x,dir.y,dir.z));
75
        getGeomOffsetCurvePtr()->setHandle(curve2);
76
        return 0;
77
    }
78
    catch (Standard_Failure& e) {
79

80
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
81
        return -1;
82
    }
83
}
84

85
Py::Float OffsetCurvePy::getOffsetValue() const
86
{
87
    Handle(Geom_OffsetCurve) curve = Handle(Geom_OffsetCurve)::DownCast(getGeometryPtr()->handle());
88
    return Py::Float(curve->Offset());
89
}
90

91
void OffsetCurvePy::setOffsetValue(Py::Float arg)
92
{
93
    Handle(Geom_OffsetCurve) curve = Handle(Geom_OffsetCurve)::DownCast(getGeometryPtr()->handle());
94
    curve->SetOffsetValue((double)arg);
95
}
96

97
Py::Object OffsetCurvePy::getOffsetDirection() const
98
{
99
    Handle(Geom_OffsetCurve) curve = Handle(Geom_OffsetCurve)::DownCast(getGeometryPtr()->handle());
100
    const gp_Dir& dir = curve->Direction();
101
    return Py::Vector(Base::Vector3d(dir.X(),dir.Y(),dir.Z()));
102
}
103

104
void OffsetCurvePy::setOffsetDirection(Py::Object arg)
105
{
106
    PyObject* p = arg.ptr();
107
    if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
108
        Base::Vector3d dir = static_cast<Base::VectorPy*>(p)->value();
109
        Handle(Geom_OffsetCurve) curve = Handle(Geom_OffsetCurve)::DownCast(getGeometryPtr()->handle());
110
        curve->SetDirection(gp_Dir(dir.x,dir.y,dir.z));
111
    }
112
    else if (PyObject_TypeCheck(p, &PyTuple_Type)) {
113
        Base::Vector3d dir = Base::getVectorFromTuple<double>(p);
114
        Handle(Geom_OffsetCurve) curve = Handle(Geom_OffsetCurve)::DownCast(getGeometryPtr()->handle());
115
        curve->SetDirection(gp_Dir(dir.x,dir.y,dir.z));
116
    }
117
    else {
118
        std::string error = std::string("type must be 'Vector', not ");
119
        error += p->ob_type->tp_name;
120
        throw Py::TypeError(error);
121
    }
122
}
123

124
Py::Object OffsetCurvePy::getBasisCurve() const
125
{
126
    Handle(Geom_OffsetCurve) curve = Handle(Geom_OffsetCurve)::DownCast(getGeometryPtr()->handle());
127
    Handle(Geom_Curve) basis = curve->BasisCurve();
128
    std::unique_ptr<GeomCurve> ptr(Part::makeFromCurve(basis));
129
    return Py::asObject(ptr->getPyObject());
130
}
131

132
void OffsetCurvePy::setBasisCurve(Py::Object arg)
133
{
134
    PyObject* p = arg.ptr();
135
    if (PyObject_TypeCheck(p, &(GeometryPy::Type))) {
136
        GeometryPy* pcGeo = static_cast<GeometryPy*>(p);
137
        Handle(Geom_Curve) curve = Handle(Geom_Curve)::DownCast
138
            (pcGeo->getGeometryPtr()->handle());
139
        if (curve.IsNull()) {
140
            throw Py::TypeError("geometry is not a curve");
141
        }
142

143
        try {
144
            Handle(Geom_OffsetCurve) curve2 = Handle(Geom_OffsetCurve)::DownCast
145
                (getGeometryPtr()->handle());
146
            curve2->SetBasisCurve(curve);
147
        }
148
        catch (Standard_Failure& e) {
149
                throw Py::RuntimeError(e.GetMessageString());
150
        }
151
    }
152
}
153

154
PyObject *OffsetCurvePy::getCustomAttributes(const char* /*attr*/) const
155
{
156
    return nullptr;
157
}
158

159
int OffsetCurvePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
160
{
161
    return 0;
162
}
163

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

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

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

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