File size: 11,218 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/***************************************************************************

 *   Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net>     *

 *                                                                         *

 *   This file is part of the FreeCAD CAx development system.              *

 *                                                                         *

 *   This library is free software; you can redistribute it and/or         *

 *   modify it under the terms of the GNU Library General Public           *

 *   License as published by the Free Software Foundation; either          *

 *   version 2 of the License, or (at your option) any later version.      *

 *                                                                         *

 *   This library  is distributed in the hope that it will be useful,      *

 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *

 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *

 *   GNU Library General Public License for more details.                  *

 *                                                                         *

 *   You should have received a copy of the GNU Library General Public     *

 *   License along with this library; see the file COPYING.LIB. If not,    *

 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *

 *   Suite 330, Boston, MA  02111-1307, USA                                *

 *                                                                         *

 ***************************************************************************/


# include <BRepBuilderAPI_MakeEdge.hxx>

#include <Base/PyWrapParseTupleAndKeywords.h>

#include "CosmeticEdgePy.h"
#include "CosmeticEdgePy.cpp"
#include "Cosmetic.h"
#include "DrawUtil.h"
#include "Geometry.h"


using namespace TechDraw;

// returns a string which represents the object e.g. when printed in python
std::string CosmeticEdgePy::representation() const

{
    std::stringstream ss;
    ss << "<CosmeticEdge object> at " << std::hex << this;
    return ss.str();
//    return "<CosmeticEdge object>";
}

PyObject *CosmeticEdgePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper

{
    // never create such objects with the constructor
    PyErr_SetString(PyExc_RuntimeError,
        "You cannot create an instance of the abstract class 'CosmeticEdge'.");
    return nullptr;
}

// constructor method
int CosmeticEdgePy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)

{
    return 0;
}

//From Part::GeometryPy.cpp
// PyObject* CosmeticEdgePy::clone(PyObject *args)
// {
//     if (!PyArg_ParseTuple(args, ""))
//         return nullptr;

//     TechDraw::CosmeticEdge* geom = this->getCosmeticEdgePtr();
//     PyTypeObject* type = this->GetType();
//     PyObject* cpy = nullptr;
//     // let the type object decide
//     if (type->tp_new)
//         cpy = type->tp_new(type, this, nullptr);
//     if (!cpy) {
//         PyErr_SetString(PyExc_RuntimeError, "failed to create clone of CosmeticEdge");
//         return nullptr;
//     }

//     TechDraw::CosmeticEdgePy* geompy = static_cast<TechDraw::CosmeticEdgePy*>(cpy);
//     // the PyMake function must have created the corresponding instance of the 'CosmeticEdge' subclass
//     // so delete it now to avoid a memory leak
//     if (geompy->_pcTwinPointer) {
//         TechDraw::CosmeticEdge* clone = static_cast<TechDraw::CosmeticEdge*>(geompy->_pcTwinPointer);
//         delete clone;
//     }
//     geompy->_pcTwinPointer = geom->clone();
//     return cpy;
// }

// PyObject* CosmeticEdgePy::copy(PyObject *args)
// {
//     if (!PyArg_ParseTuple(args, ""))
//         return nullptr;

//     TechDraw::CosmeticEdge* ce = this->getCosmeticEdgePtr();
//     PyTypeObject* type = this->GetType();
//     PyObject* cpy = nullptr;
//     // let the type object decide
//     if (type->tp_new)
//         cpy = type->tp_new(type, this, nullptr);
//     if (!cpy) {
//         PyErr_SetString(PyExc_RuntimeError, "failed to create copy of CosmeticEdge");
//         return nullptr;
//     }

//     TechDraw::CosmeticEdgePy* geompy = static_cast<TechDraw::CosmeticEdgePy*>(cpy);
//     // the PyMake function must have created the corresponding instance of the 'CosmeticEdge' subclass
//     // so delete it now to avoid a memory leak
//     if (geompy->_pcTwinPointer) {
//         TechDraw::CosmeticEdge* copy = static_cast<TechDraw::CosmeticEdge*>(geompy->_pcTwinPointer);
//         delete copy;
//     }
//     geompy->_pcTwinPointer = ce->copy();
//     return cpy;
// }

void CosmeticEdgePy::setFormat(Py::Dict arg)

{
    Py::Tuple dummy;
    Py::TupleN color(Py::Float(0.0), Py::Float(0.0), Py::Float(0.0), Py::Float(0.0));
    int style = 1;
    double weight = 0.5;
    PyObject* pColor = color.ptr();
    PyObject* visible = Py_True;
    static const std::array<const char *, 5> kw{"style", "weight", "color", "visible", nullptr};
    if (!Base::Wrapped_ParseTupleAndKeywords(dummy.ptr(), arg.ptr(), "|idO!O!", kw,
                                             &style, &weight, &PyTuple_Type, &pColor, &PyBool_Type, &visible)) {
        throw Py::ValueError("Expected {'style':int, 'weight':float, 'color':tuple, 'visible':bool} dict");
    }

    TechDraw::LineFormat* format = &(this->getCosmeticEdgePtr()->m_format);
    format->setStyle(style);
    format->setWidth(weight);
    format->setColor(DrawUtil::pyTupleToColor(pColor));
    format->setVisible(Base::asBoolean(visible));
}

Py::Dict CosmeticEdgePy::getFormat() const

{
    TechDraw::LineFormat* format= &(this->getCosmeticEdgePtr()->m_format);
    Py::Dict dict;

    dict.setItem("style", Py::Long(format->getStyle()));
    dict.setItem("weight", Py::Float(format->getWidth()));
    dict.setItem("color", Py::Tuple(DrawUtil::colorToPyTuple(format->getColor()), true));
    dict.setItem("visible", Py::Boolean(format->getVisible()));

    return dict;
}

Py::String CosmeticEdgePy::getTag() const

{
    std::string tmp = getCosmeticEdgePtr()->getTagAsString();
    return Py::String(tmp);
}

//Py::String CosmeticEdgePy::getOwner(void) const
//{
////    std::string tmp = boost::uuids::to_string(getCosmeticEdgePtr()->getOwner());
//    std::string tmp = "not implemented yet";
//    return Py::String(tmp);
//}

//TODO: make BaseGeom py-aware or convert TD geometry to ??Part::Geometry2d?? or other
//      py-aware class.
//Py::Object CosmeticEdgePy::getGeometry(void) const
//{
////    TechDraw::BaseGeomPtr bg = getCosmeticEdgePtr()->m_geometry;
//    Base::Console().message("Not implemented yet");
//    return Py::asObject(Py_None);
//}

//void CosmeticEdgePy::setGeometry(Py::Object arg)
//{
//    Base::Console().message("Not implemented yet");
//    PyObject* p = arg.ptr();
//    if (PyObject_TypeCheck(p, &(TechDraw::BaseGeomPy::Type))) {
//        //TODO
//    } else {
//        std::string error = std::string("type must be 'BaseGeom', not ");
//        error += p->ob_type->tp_name;
//        throw Py::TypeError(error);
//    }
//}

Py::Vector CosmeticEdgePy::getStart() const

{
    Base::Vector3d point = getCosmeticEdgePtr()->permaStart;
    point = DrawUtil::invertY(point);
    return Py::Vector(point);
}

void CosmeticEdgePy::setStart(Py::Vector arg)

{
    Base::Vector3d pNew = static_cast<Base::Vector3d>(arg);

    pNew = DrawUtil::invertY(pNew);
    Base::Vector3d pEnd = getCosmeticEdgePtr()->permaEnd;
    gp_Pnt gp1(pNew.x, pNew.y, pNew.z);
    gp_Pnt gp2(pEnd.x, pEnd.y, pEnd.z);
    TopoDS_Edge e = BRepBuilderAPI_MakeEdge(gp1, gp2);
//    auto oldGeom = getCosmeticEdgePtr()->m_geometry;
    getCosmeticEdgePtr()->m_geometry = TechDraw::BaseGeom::baseFactory(e);
    getCosmeticEdgePtr()->permaStart = pNew;
//    delete oldGeom;
}

Py::Vector CosmeticEdgePy::getEnd() const

{
    Base::Vector3d point = getCosmeticEdgePtr()->permaEnd;
    point = DrawUtil::invertY(point);
    return Py::Vector(point);
}

void CosmeticEdgePy::setEnd(Py::Vector arg)

{
    Base::Vector3d pNew = static_cast<Base::Vector3d>(arg);

    pNew = DrawUtil::invertY(pNew);
    Base::Vector3d pStart = getCosmeticEdgePtr()->permaStart;
    gp_Pnt gp1(pNew.x, pNew.y, pNew.z);
    gp_Pnt gp2(pStart.x, pStart.y, pStart.z);
    TopoDS_Edge e = BRepBuilderAPI_MakeEdge(gp2, gp1);
//    auto oldGeom = getCosmeticEdgePtr()->m_geometry;
    getCosmeticEdgePtr()->m_geometry = TechDraw::BaseGeom::baseFactory(e);
    getCosmeticEdgePtr()->permaEnd = pNew;
//    delete oldGeom;
}

Py::Float CosmeticEdgePy::getRadius() const

{
    GeomType gt = getCosmeticEdgePtr()->m_geometry->getGeomType();
    if ( (gt != GeomType::CIRCLE) &&
         (gt != GeomType::ARCOFCIRCLE) ) {
        throw Py::TypeError("Not a circle. Can not get radius");
    }
    double r = getCosmeticEdgePtr()->permaRadius;
    return Py::Float(r);
}

void CosmeticEdgePy::setRadius(Py::Float arg)

{
    GeomType gt = getCosmeticEdgePtr()->m_geometry->getGeomType();
    if ( (gt != GeomType::CIRCLE) &&
         (gt != GeomType::ARCOFCIRCLE) ) {
        throw Py::TypeError("Not a circle. Can not set radius");
    }

    double r = static_cast<double>(arg);
    getCosmeticEdgePtr()->permaRadius = r;
//    auto oldGeom = getCosmeticEdgePtr()->m_geometry;
    getCosmeticEdgePtr()->m_geometry =
                std::make_shared<TechDraw::Circle> (getCosmeticEdgePtr()->permaStart, r);
//    delete oldGeom;
}

Py::Vector CosmeticEdgePy::getCenter() const

{
    GeomType gt = getCosmeticEdgePtr()->m_geometry->getGeomType();
    if ( (gt != GeomType::CIRCLE) &&
         (gt != GeomType::ARCOFCIRCLE) ) {
        throw Py::TypeError("Not a circle. Can not get center");
    }
    Base::Vector3d point = getCosmeticEdgePtr()->permaStart;
    point = DrawUtil::invertY(point);
    return Py::Vector(point);
}

void CosmeticEdgePy::setCenter(Py::Vector arg)

{
    GeomType gt = getCosmeticEdgePtr()->m_geometry->getGeomType();
//    PyObject* p = arg.ptr();
    if ( (gt != GeomType::CIRCLE) &&
         (gt != GeomType::ARCOFCIRCLE) ) {
        throw Py::TypeError("Not a circle. Can not set center");
    }

    Base::Vector3d pNew = static_cast<Base::Vector3d>(arg);
    pNew = DrawUtil::invertY(pNew);
    auto oldGeom = getCosmeticEdgePtr()->m_geometry;
    TechDraw::CirclePtr oldCircle = std::dynamic_pointer_cast<TechDraw::Circle> (oldGeom);
    if (!oldCircle) {
        throw Py::TypeError("Edge geometry is not a circle");
    }

    getCosmeticEdgePtr()->permaStart = pNew;
    getCosmeticEdgePtr()->permaEnd = pNew;
    getCosmeticEdgePtr()->permaRadius = oldCircle->radius;
    getCosmeticEdgePtr()->m_geometry =
            std::make_shared<TechDraw::Circle> (getCosmeticEdgePtr()->permaStart, oldCircle->radius);
//    delete oldGeom;
}

PyObject *CosmeticEdgePy::getCustomAttributes(const char* /*attr*/) const

{
    return nullptr;
}

int CosmeticEdgePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)

{
    return 0;
}