FreeCAD

Форк
0
/
CirclePyImp.cpp 
185 строк · 7.6 Кб
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 <GC_MakeCircle.hxx>
26
# include <Geom_Circle.hxx>
27
# include <gp_Circ.hxx>
28
#endif
29

30
#include <Base/PyWrapParseTupleAndKeywords.h>
31
#include <Base/VectorPy.h>
32

33
#include "CirclePy.h"
34
#include "CirclePy.cpp"
35
#include "OCCError.h"
36

37

38
using namespace Part;
39

40
extern const char* gce_ErrorStatusText(gce_ErrorType et);
41

42
// returns a string which represents the object e.g. when printed in python
43
std::string CirclePy::representation() const
44
{
45
    Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast(getGeomCirclePtr()->handle());
46
    gp_Ax1 axis = circle->Axis();
47
    gp_Dir dir = axis.Direction();
48
    gp_Pnt loc = axis.Location();
49
    Standard_Real fRad = circle->Radius();
50

51
    std::stringstream str;
52
    str << "Circle (";
53
    str << "Radius : " << fRad << ", ";
54
    str << "Position : (" << loc.X() << ", "<< loc.Y() << ", "<< loc.Z() << "), ";
55
    str << "Direction : (" << dir.X() << ", "<< dir.Y() << ", "<< dir.Z() << ")";
56
    str << ")";
57

58
    return str.str();
59
}
60

61
PyObject *CirclePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
62
{
63
    // create a new instance of CirclePy and the Twin object
64
    Handle(Geom_Circle) circle = new Geom_Circle(gp_Circ());
65
    return new CirclePy(new GeomCircle(circle));
66
}
67

68
// constructor method
69
int CirclePy::PyInit(PyObject* args, PyObject* kwds)
70
{
71
    // circle and distance for offset
72
    PyObject *pCirc;
73
    double dist;
74
    static const std::array<const char *, 3> keywords_cd {"Circle", "Distance", nullptr};
75
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!d", keywords_cd, &(CirclePy::Type), &pCirc, &dist)) {
76
        CirclePy* pcCircle = static_cast<CirclePy*>(pCirc);
77
        Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast
78
            (pcCircle->getGeomCirclePtr()->handle());
79
        GC_MakeCircle mc(circle->Circ(), dist);
80
        if (!mc.IsDone()) {
81
            PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
82
            return -1;
83
        }
84

85
        Handle(Geom_Circle) circ = Handle(Geom_Circle)::DownCast(getGeomCirclePtr()->handle());
86
        circ->SetCirc(mc.Value()->Circ());
87
        return 0;
88
    }
89

90
    // center, normal and radius
91
    PyObject *pV1, *pV2, *pV3;
92
    static const std::array<const char *, 4> keywords_cnr {"Center", "Normal", "Radius", nullptr};
93
    PyErr_Clear();
94
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!O!d", keywords_cnr,
95
                                            &(Base::VectorPy::Type), &pV1,
96
                                            &(Base::VectorPy::Type), &pV2,
97
                                            &dist)) {
98
        Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
99
        Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
100
        GC_MakeCircle mc(gp_Pnt(v1.x,v1.y,v1.z),
101
                         gp_Dir(v2.x,v2.y,v2.z),
102
                         dist);
103
        if (!mc.IsDone()) {
104
            PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
105
            return -1;
106
        }
107

108
        Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast(getGeomCirclePtr()->handle());
109
        circle->SetCirc(mc.Value()->Circ());
110
        return 0;
111
    }
112

113
    static const std::array<const char *, 2> keywords_c {"Circle", nullptr};
114
    PyErr_Clear();
115
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!", keywords_c, &(CirclePy::Type), &pCirc)) {
116
        CirclePy* pcCircle = static_cast<CirclePy*>(pCirc);
117
        Handle(Geom_Circle) circ1 = Handle(Geom_Circle)::DownCast
118
            (pcCircle->getGeomCirclePtr()->handle());
119
        Handle(Geom_Circle) circ2 = Handle(Geom_Circle)::DownCast
120
            (this->getGeomCirclePtr()->handle());
121
        circ2->SetCirc(circ1->Circ());
122
        return 0;
123
    }
124

125
    static const std::array<const char *, 4> keywords_ppp {"Point1", "Point2", "Point3", nullptr};
126
    PyErr_Clear();
127
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!O!O!", keywords_ppp,
128
                                            &(Base::VectorPy::Type), &pV1,
129
                                            &(Base::VectorPy::Type), &pV2,
130
                                            &(Base::VectorPy::Type), &pV3)) {
131
        Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
132
        Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
133
        Base::Vector3d v3 = static_cast<Base::VectorPy*>(pV3)->value();
134
        GC_MakeCircle mc(gp_Pnt(v1.x,v1.y,v1.z),
135
                         gp_Pnt(v2.x,v2.y,v2.z),
136
                         gp_Pnt(v3.x,v3.y,v3.z));
137
        if (!mc.IsDone()) {
138
            PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
139
            return -1;
140
        }
141

142
        Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast(getGeomCirclePtr()->handle());
143
        circle->SetCirc(mc.Value()->Circ());
144
        return 0;
145
    }
146

147
    // default circle
148
    static const std::array<const char *, 1> keywords_n {nullptr};
149
    PyErr_Clear();
150
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "", keywords_n)) {
151
        Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast(getGeomCirclePtr()->handle());
152
        circle->SetRadius(1.0);
153
        return 0;
154
    }
155

156
    PyErr_SetString(PyExc_TypeError, "Circle constructor accepts:\n"
157
        "-- empty parameter list\n"
158
        "-- Circle\n"
159
        "-- Circle, Distance\n"
160
        "-- Center, Normal, Radius\n"
161
        "-- Point1, Point2, Point3");
162
    return -1;
163
}
164

165
Py::Float CirclePy::getRadius() const
166
{
167
    Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast(getGeomCirclePtr()->handle());
168
    return Py::Float(circle->Radius());
169
}
170

171
void  CirclePy::setRadius(Py::Float arg)
172
{
173
    Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast(getGeomCirclePtr()->handle());
174
    circle->SetRadius((double)arg);
175
}
176

177
PyObject *CirclePy::getCustomAttributes(const char* ) const
178
{
179
    return nullptr;
180
}
181

182
int CirclePy::setCustomAttributes(const char* , PyObject *)
183
{
184
    return 0;
185
}
186

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

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

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

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