FreeCAD

Форк
0
/
ParabolaPyImp.cpp 
195 строк · 7.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_Parabola.hxx>
26
# include <gce_MakeParab.hxx>
27
#endif
28

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

33
#include "ParabolaPy.h"
34
#include "ParabolaPy.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 ParabolaPy::representation() const
44
{
45
    return "<Parabola object>";
46
}
47

48
PyObject *ParabolaPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
49
{
50
    // create a new instance of ParabolaPy and the Twin object
51
    return new ParabolaPy(new GeomParabola);
52
}
53

54
// constructor method
55
int ParabolaPy::PyInit(PyObject* args, PyObject* kwds)
56
{
57
    static const std::array<const char *, 1> keywords_n {nullptr};
58
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "", keywords_n)) {
59
        Handle(Geom_Parabola) parabola = Handle(Geom_Parabola)::DownCast(getGeomParabolaPtr()->handle());
60
        parabola->SetFocal(1.0);
61
        return 0;
62
    }
63

64
    static const std::array<const char *, 2> keywords_e {"Parabola", nullptr};
65
    PyErr_Clear();
66
    PyObject *pParab;
67
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!",keywords_e, &(ParabolaPy::Type), &pParab)) {
68
        ParabolaPy* pParabola = static_cast<ParabolaPy*>(pParab);
69
        Handle(Geom_Parabola) Parab1 = Handle(Geom_Parabola)::DownCast
70
            (pParabola->getGeomParabolaPtr()->handle());
71
        Handle(Geom_Parabola) Parab2 = Handle(Geom_Parabola)::DownCast
72
            (this->getGeomParabolaPtr()->handle());
73
        Parab2->SetParab(Parab1->Parab());
74
        return 0;
75
    }
76

77
    static const std::array<const char *, 4> keywords_ssc {"Focus","Center","Normal",nullptr};
78
    PyErr_Clear();
79
    PyObject *pV1, *pV2, *pV3;
80
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!O!O!", keywords_ssc,
81
                                            &(Base::VectorPy::Type), &pV1,
82
                                            &(Base::VectorPy::Type), &pV2,
83
                                            &(Base::VectorPy::Type), &pV3)) {
84
        Base::Vector3d focus = static_cast<Base::VectorPy*>(pV1)->value();
85
        Base::Vector3d center = static_cast<Base::VectorPy*>(pV2)->value();
86
        Base::Vector3d normal = static_cast<Base::VectorPy*>(pV3)->value();
87

88
        Base::Vector3d xvect = focus-center;
89

90
        // set the geometry
91
        gp_Pnt p1(center.x,center.y,center.z);
92
        gp_Dir norm(normal.x,normal.y,normal.z);
93
        gp_Dir xdiroce(xvect.x,xvect.y,xvect.z);
94

95
        gp_Ax2 xdir(p1, norm, xdiroce);
96

97
        gce_MakeParab mc(xdir, (Standard_Real) xvect.Length());
98
        if (!mc.IsDone()) {
99
            PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
100
            return -1;
101
        }
102

103
        Handle(Geom_Parabola) parabola = Handle(Geom_Parabola)::DownCast(getGeomParabolaPtr()->handle());
104
        parabola->SetParab(mc.Value());
105
        return 0;
106
    }
107

108
    PyErr_SetString(PyExc_TypeError, "Parabola constructor accepts:\n"
109
    "-- empty parameter list\n"
110
    "-- Parabola\n"
111
    "-- Point, Point, Point");
112

113
    return -1;
114
}
115

116
PyObject* ParabolaPy::compute(PyObject *args)
117
{
118
    PyObject *p1, *p2, *p3;
119
    if (!PyArg_ParseTuple(args, "O!O!O!",
120
        &Base::VectorPy::Type,&p1,
121
        &Base::VectorPy::Type,&p2,
122
        &Base::VectorPy::Type,&p3))
123
        return nullptr;
124
    Base::Vector3d v1 = Py::Vector(p1,false).toVector();
125
    Base::Vector3d v2 = Py::Vector(p2,false).toVector();
126
    Base::Vector3d v3 = Py::Vector(p3,false).toVector();
127
    Base::Vector3d c = (v1-v2) % (v3-v2);
128
    double zValue = v1.z;
129
    if (fabs(c.Length()) < 0.0001) {
130
        PyErr_SetString(PartExceptionOCCError, "Points are collinear");
131
        return nullptr;
132
    }
133

134
    Base::Matrix4D m;
135
    Base::Vector3d v;
136
    m[0][0] = v1.y * v1.y;
137
    m[0][1] = v1.y;
138
    m[0][2] = 1;
139
    m[1][0] = v2.y * v2.y;
140
    m[1][1] = v2.y;
141
    m[1][2] = 1;
142
    m[2][0] = v3.y * v3.y;
143
    m[2][1] = v3.y;
144
    m[2][2] = 1.0;
145
    v.x = v1.x;
146
    v.y = v2.x;
147
    v.z = v3.x;
148
    m.inverseGauss();
149
    v = m * v;
150
    double a22 = v.x;
151
    double a10 = -0.5;
152
    double a20 = v.y/2.0;
153
    double a00 = v.z;
154
    Handle(Geom_Parabola) curve = Handle(Geom_Parabola)::DownCast(getGeometryPtr()->handle());
155
    curve->SetFocal(0.5*fabs(a10/a22));
156
    curve->SetLocation(gp_Pnt((a20*a20-a22*a00)/(2*a22*a10), -a20/a22, zValue));
157

158
    Py_Return;
159
}
160

161
Py::Float ParabolaPy::getFocal() const
162
{
163
    Handle(Geom_Parabola) curve = Handle(Geom_Parabola)::DownCast(getGeometryPtr()->handle());
164
    return Py::Float(curve->Focal());
165
}
166

167
void ParabolaPy::setFocal(Py::Float arg)
168
{
169
    Handle(Geom_Parabola) curve = Handle(Geom_Parabola)::DownCast(getGeometryPtr()->handle());
170
    curve->SetFocal((double)arg);
171
}
172

173
Py::Object ParabolaPy::getFocus() const
174
{
175
    Handle(Geom_Parabola) c = Handle(Geom_Parabola)::DownCast
176
        (getGeometryPtr()->handle());
177
    gp_Pnt loc = c->Focus();
178
    return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
179
}
180

181
Py::Float ParabolaPy::getParameter() const
182
{
183
    Handle(Geom_Parabola) curve = Handle(Geom_Parabola)::DownCast(getGeometryPtr()->handle());
184
    return Py::Float(curve->Parameter());
185
}
186

187
PyObject *ParabolaPy::getCustomAttributes(const char* /*attr*/) const
188
{
189
    return nullptr;
190
}
191

192
int ParabolaPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
193
{
194
    return 0;
195
}
196

197

198

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

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

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

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