Debian Codegraph @

src/App/ComplexGeoDataPyImp.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2007 Jürgen Riegel <[email protected]> *
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 <memory>
24
25 #include "ComplexGeoData.h"
26 #include "StringHasher.h"
27
28 // inclusion of the generated files (generated out of ComplexGeoDataPy.xml)
29 #include <App/ComplexGeoDataPy.h>
30 #include <App/ComplexGeoDataPy.cpp>
31 #include <App/StringHasherPy.h>
32 #include <App/StringIDPy.h>
33 #include <Base/BoundBoxPy.h>
34 #include <Base/MatrixPy.h>
35 #include <Base/PlacementPy.h>
36 #include "Base/PyWrapParseTupleAndKeywords.h"
37 #include <Base/VectorPy.h>
38 #include <Base/GeometryPyCXX.h>
39
40 using namespace Data;
41 using namespace Base;
42
43 // returns a string which represent the object e.g. when printed in python
44 std::string ComplexGeoDataPy::representation() const
45 {
46 return {"<ComplexGeoData object>"};
47 }
48
49 PyObject* ComplexGeoDataPy::getElementTypes(PyObject* args) const
50 {
51 if (!PyArg_ParseTuple(args, "")) {
52 return nullptr;
53 }
54
55 std::vector<const char*> types = getComplexGeoDataPtr()->getElementTypes();
56 Py::List list;
57 for (auto it : types) {
58 list.append(Py::String(it));
59 }
60 return Py::new_reference_to(list);
61 }
62
63 PyObject* ComplexGeoDataPy::countSubElements(PyObject* args) const
64 {
65 char* type;
66 if (!PyArg_ParseTuple(args, "s", &type)) {
67 return nullptr;
68 }
69
70 try {
71 unsigned long count = getComplexGeoDataPtr()->countSubElements(type);
72 return Py::new_reference_to(Py::Long(count));
73 }
74 catch (...) {
75 PyErr_SetString(PyExc_RuntimeError, "failed to count sub-elements from object");
76 return nullptr;
77 }
78 }
79
80 PyObject* ComplexGeoDataPy::getFacesFromSubElement(PyObject* args) const
81 {
82 char* type;
83 unsigned long index;
84 if (!PyArg_ParseTuple(args, "sk", &type, &index)) {
85 return nullptr;
86 }
87
88 std::vector<Base::Vector3d> points;
89 std::vector<Base::Vector3d> normals;
90 std::vector<Data::ComplexGeoData::Facet> facets;
91 try {
92 std::unique_ptr<Data::Segment> segm(getComplexGeoDataPtr()->getSubElement(type, index));
93 getComplexGeoDataPtr()->getFacesFromSubElement(segm.get(), points, normals, facets);
94 }
95 catch (...) {
96 PyErr_SetString(PyExc_RuntimeError, "failed to get sub-element from object");
97 return nullptr;
98 }
99
100 Py::Tuple tuple(2);
101 Py::List vertex;
102 for (const auto& it : points) {
103 vertex.append(Py::asObject(new Base::VectorPy(it)));
104 }
105 tuple.setItem(0, vertex);
106 Py::List facet;
107 for (const auto& it : facets) {
108 Py::Tuple f(3);
109 f.setItem(0, Py::Long(int(it.I1)));
110 f.setItem(1, Py::Long(int(it.I2)));
111 f.setItem(2, Py::Long(int(it.I3)));
112 facet.append(f);
113 }
114 tuple.setItem(1, facet);
115 return Py::new_reference_to(tuple);
116 }
117
118 PyObject* ComplexGeoDataPy::getLinesFromSubElement(PyObject* args) const
119 {
120 char* type;
121 int index;
122 if (!PyArg_ParseTuple(args, "si", &type, &index)) {
123 return nullptr;
124 }
125
126 std::vector<Base::Vector3d> points;
127 std::vector<Data::ComplexGeoData::Line> lines;
128 try {
129 std::unique_ptr<Data::Segment> segm(getComplexGeoDataPtr()->getSubElement(type, index));
130 getComplexGeoDataPtr()->getLinesFromSubElement(segm.get(), points, lines);
131 }
132 catch (...) {
133 PyErr_SetString(PyExc_RuntimeError, "failed to get sub-element from object");
134 return nullptr;
135 }
136
137 Py::Tuple tuple(2);
138 Py::List vertex;
139 for (const auto& it : points) {
140 vertex.append(Py::asObject(new Base::VectorPy(it)));
141 }
142 tuple.setItem(0, vertex);
143 Py::List line;
144 for (const auto& it : lines) {
145 Py::Tuple l(2);
146 l.setItem(0, Py::Long((int)it.I1));
147 l.setItem(1, Py::Long((int)it.I2));
148 line.append(l);
149 }
150 tuple.setItem(1, line);
151 return Py::new_reference_to(tuple);
152 }
153
154 PyObject* ComplexGeoDataPy::getPoints(PyObject* args) const
155 {
156 double accuracy = 0.05;
157 if (!PyArg_ParseTuple(args, "d", &accuracy)) {
158 return nullptr;
159 }
160
161 std::vector<Base::Vector3d> points;
162 std::vector<Base::Vector3d> normals;
163 try {
164 getComplexGeoDataPtr()->getPoints(points, normals, accuracy);
165 }
166 catch (...) {
167 PyErr_SetString(PyExc_RuntimeError, "failed to get sub-element from object");
168 return nullptr;
169 }
170
171 Py::Tuple tuple(2);
172 Py::List vertex;
173 for (const auto& it : points) {
174 vertex.append(Py::asObject(new Base::VectorPy(it)));
175 }
176 tuple.setItem(0, vertex);
177
178 Py::List normal;
179 for (const auto& it : normals) {
180 normal.append(Py::asObject(new Base::VectorPy(it)));
181 }
182 tuple.setItem(1, normal);
183 return Py::new_reference_to(tuple);
184 }
185
186 PyObject* ComplexGeoDataPy::getLines(PyObject* args) const
187 {
188 double accuracy = 0.05;
189 if (!PyArg_ParseTuple(args, "d", &accuracy)) {
190 return nullptr;
191 }
192
193 std::vector<Base::Vector3d> points;
194 std::vector<Data::ComplexGeoData::Line> lines;
195 try {
196 getComplexGeoDataPtr()->getLines(points, lines, accuracy);
197 }
198 catch (...) {
199 PyErr_SetString(PyExc_RuntimeError, "failed to get sub-element from object");
200 return nullptr;
201 }
202
203 Py::Tuple tuple(2);
204 Py::List vertex;
205 for (const auto& it : points) {
206 vertex.append(Py::asObject(new Base::VectorPy(it)));
207 }
208 tuple.setItem(0, vertex);
209 Py::List line;
210 for (const auto& it : lines) {
211 Py::Tuple l(2);
212 l.setItem(0, Py::Long((int)it.I1));
213 l.setItem(1, Py::Long((int)it.I2));
214 line.append(l);
215 }
216 tuple.setItem(1, line);
217 return Py::new_reference_to(tuple);
218 }
219
220 PyObject* ComplexGeoDataPy::getFaces(PyObject* args) const
221 {
222 double accuracy = 0.05;
223 if (!PyArg_ParseTuple(args, "d", &accuracy)) {
224 return nullptr;
225 }
226
227 std::vector<Base::Vector3d> points;
228 std::vector<Data::ComplexGeoData::Facet> facets;
229 try {
230 getComplexGeoDataPtr()->getFaces(points, facets, accuracy);
231 }
232 catch (...) {
233 PyErr_SetString(PyExc_RuntimeError, "failed to get sub-element from object");
234 return nullptr;
235 }
236
237 Py::Tuple tuple(2);
238 Py::List vertex;
239 for (const auto& it : points) {
240 vertex.append(Py::asObject(new Base::VectorPy(it)));
241 }
242 tuple.setItem(0, vertex);
243 Py::List facet;
244 for (const auto& it : facets) {
245 Py::Tuple f(3);
246 f.setItem(0, Py::Long((int)it.I1));
247 f.setItem(1, Py::Long((int)it.I2));
248 f.setItem(2, Py::Long((int)it.I3));
249 facet.append(f);
250 }
251 tuple.setItem(1, facet);
252 return Py::new_reference_to(tuple);
253 }
254
255 PyObject* ComplexGeoDataPy::applyTranslation(PyObject* args)
256 {
257 PyObject* obj;
258 if (!PyArg_ParseTuple(args, "O!", &(Base::VectorPy::Type), &obj)) {
259 return nullptr;
260 }
261
262 try {
263 Base::Vector3d move = static_cast<Base::VectorPy*>(obj)->value();
264 getComplexGeoDataPtr()->applyTranslation(move);
265 Py_Return;
266 }
267 catch (...) {
268 PyErr_SetString(PyExc_RuntimeError, "failed to apply rotation");
269 return nullptr;
270 }
271 }
272
273 PyObject* ComplexGeoDataPy::applyRotation(PyObject* args)
274 {
275 PyObject* obj;
276 if (!PyArg_ParseTuple(args, "O!", &(Base::RotationPy::Type), &obj)) {
277 return nullptr;
278 }
279
280 try {
281 Base::Rotation rot = static_cast<Base::RotationPy*>(obj)->value();
282 getComplexGeoDataPtr()->applyRotation(rot);
283 Py_Return;
284 }
285 catch (...) {
286 PyErr_SetString(PyExc_RuntimeError, "failed to apply rotation");
287 return nullptr;
288 }
289 }
290
291 PyObject* ComplexGeoDataPy::transformGeometry(PyObject* args)
292 {
293 PyObject* obj;
294 if (!PyArg_ParseTuple(args, "O!", &(Base::MatrixPy::Type), &obj)) {
295 return nullptr;
296 }
297
298 try {
299 Base::Matrix4D mat = static_cast<Base::MatrixPy*>(obj)->value();
300 getComplexGeoDataPtr()->transformGeometry(mat);
301 Py_Return;
302 }
303 catch (...) {
304 PyErr_SetString(PyExc_RuntimeError, "failed to transform geometry");
305 return nullptr;
306 }
307 }
308
309 PyObject* ComplexGeoDataPy::getElementName(PyObject* args) const
310 {
311 char* input;
312 int direction = 0;
313 if (!PyArg_ParseTuple(args, "s|i", &input, &direction)) {
314 return NULL;
315 }
316
317 Data::MappedElement res = getComplexGeoDataPtr()->getElementName(input);
318 std::string s;
319 if (direction == 1) {
320 return Py::new_reference_to(Py::String(res.name.appendToBuffer(s)));
321 }
322 else if (direction == 0) {
323 return Py::new_reference_to(Py::String(res.index.appendToStringBuffer(s)));
324 }
325 else if (Data::IndexedName(input)) {
326 return Py::new_reference_to(Py::String(res.name.appendToBuffer(s)));
327 }
328 else {
329 return Py::new_reference_to(Py::String(res.index.appendToStringBuffer(s)));
330 }
331 }
332
333 PyObject* ComplexGeoDataPy::getElementIndexedName(PyObject* args) const
334 {
335 char* input;
336 PyObject* returnID = Py_False;
337 if (!PyArg_ParseTuple(args, "s|O", &input, &returnID)) {
338 return NULL;
339 }
340
341 ElementIDRefs ids;
342 Data::MappedElement res =
343 getComplexGeoDataPtr()->getElementName(input, PyObject_IsTrue(returnID) ? &ids : nullptr);
344 std::string s;
345 Py::String name(res.index.appendToStringBuffer(s));
346 if (!PyObject_IsTrue(returnID)) {
347 return Py::new_reference_to(name);
348 }
349
350 Py::List list;
351 for (auto& id : ids) {
352 list.append(Py::Long(id.value()));
353 }
354 return Py::new_reference_to(Py::TupleN(name, list));
355 }
356
357 PyObject* ComplexGeoDataPy::getElementMappedName(PyObject* args) const
358 {
359 char* input;
360 PyObject* returnID = Py_False;
361 if (!PyArg_ParseTuple(args, "s|O", &input, &returnID)) {
362 return NULL;
363 }
364
365 ElementIDRefs ids;
366 Data::MappedElement res =
367 getComplexGeoDataPtr()->getElementName(input, PyObject_IsTrue(returnID) ? &ids : nullptr);
368 std::string s;
369 Py::String name(res.name.appendToBuffer(s));
370 if (!PyObject_IsTrue(returnID)) {
371 return Py::new_reference_to(name);
372 }
373
374 Py::List list;
375 for (auto& id : ids) {
376 list.append(Py::Long(id.value()));
377 }
378 return Py::new_reference_to(Py::TupleN(name, list));
379 }
380
381 PyObject* ComplexGeoDataPy::setElementName(PyObject* args, PyObject* kwds)
382 {
383 const char* element;
384 const char* name = 0;
385 const char* postfix = 0;
386 int tag = 0;
387 PyObject* pySid = Py_None;
388 PyObject* overwrite = Py_False;
389
390 const std::array<const char*, 7> kwlist =
391 {"element", "name", "postfix", "overwrite", "sid", "tag", nullptr};
392 if (!Wrapped_ParseTupleAndKeywords(args,
393 kwds,
394 "s|ssOOi",
395 kwlist,
396 &element,
397 &name,
398 &postfix,
399 &overwrite,
400 &pySid,
401 &tag)) {
402 return nullptr;
403 }
404 ElementIDRefs sids;
405 if (pySid != Py_None) {
406 if (PyObject_TypeCheck(pySid, &App::StringIDPy::Type)) {
407 sids.push_back(static_cast<App::StringIDPy*>(pySid)->getStringIDPtr());
408 }
409 else if (PySequence_Check(pySid)) {
410 Py::Sequence seq(pySid);
411 for (auto it = seq.begin(); it != seq.end(); ++it) {
412 auto ptr = (*it).ptr();
413 if (PyObject_TypeCheck(ptr, &App::StringIDPy::Type)) {
414 sids.push_back(static_cast<App::StringIDPy*>(ptr)->getStringIDPtr());
415 }
416 else {
417 throw Py::TypeError("expect StringID in sid sequence");
418 }
419 }
420 }
421 else {
422 throw Py::TypeError("expect sid to contain either StringID or sequence of StringID");
423 }
424 }
425 PY_TRY
426 {
427 Data::IndexedName index(element, getComplexGeoDataPtr()->getElementTypes());
428 Data::MappedName mapped = Data::MappedName::fromRawData(name);
429 std::ostringstream ss;
430 ElementMapPtr map = getComplexGeoDataPtr()->resetElementMap();
431 if (!map) {
432 throw Py::RuntimeError("no element map");
433 }
434
435 map->encodeElementName(getComplexGeoDataPtr()->elementType(index),
436 mapped,
437 ss,
438 &sids,
439 tag,
440 postfix,
441 tag);
442 Data::MappedName res =
443 map->setElementName(index, mapped, tag, &sids, PyObject_IsTrue(overwrite));
444 return Py::new_reference_to(Py::String(res.toString(0)));
445 }
446 PY_CATCH
447 }
448
449 Py::Object ComplexGeoDataPy::getHasher() const
450 {
451 auto self = getComplexGeoDataPtr();
452 if (!self->Hasher) {
453 return Py::None();
454 }
455 return Py::Object(self->Hasher->getPyObject(), true);
456 }
457
458 Py::Dict ComplexGeoDataPy::getElementMap() const
459 {
460 Py::Dict ret;
461 std::string s;
462 for (auto& v : getComplexGeoDataPtr()->getElementMap()) {
463 s.clear();
464 ret.setItem(v.name.toString(0), Py::String(v.index.appendToStringBuffer(s)));
465 }
466 return ret;
467 }
468
469 void ComplexGeoDataPy::setElementMap(Py::Dict dict)
470 {
471 std::vector<Data::MappedElement> map;
472 const auto& types = getComplexGeoDataPtr()->getElementTypes();
473 for (auto it = dict.begin(); it != dict.end(); ++it) {
474 const auto& value = *it;
475 if (!value.first.isString() || !value.second.isString()) {
476 throw Py::TypeError("expect only strings in the dict");
477 }
478 map.emplace_back(Data::MappedName(value.first.as_string().c_str()),
479 Data::IndexedName(Py::Object(value.second).as_string().c_str(), types));
480 }
481 getComplexGeoDataPtr()->setElementMap(map);
482 }
483
484 Py::Dict ComplexGeoDataPy::getElementReverseMap() const
485 {
486 Py::Dict ret;
487 std::string s;
488 for (auto& v : getComplexGeoDataPtr()->getElementMap()) {
489 s.clear();
490 auto value = ret[Py::String(v.index.appendToStringBuffer(s))];
491 Py::Object item(value);
492 if (item.isNone()) {
493 s.clear();
494 value = Py::String(v.name.appendToBuffer(s));
495 }
496 else if (item.isList()) {
497 Py::List list(item);
498 s.clear();
499 list.append(Py::String(v.name.appendToBuffer(s)));
500 value = list;
501 }
502 else {
503 Py::List list;
504 list.append(item);
505 s.clear();
506 list.append(Py::String(v.name.appendToBuffer(s)));
507 value = list;
508 }
509 }
510 return ret;
511 }
512
513 Py::Long ComplexGeoDataPy::getElementMapSize() const
514 {
515 return Py::Long((long)getComplexGeoDataPtr()->getElementMapSize());
516 }
517
518 void ComplexGeoDataPy::setHasher(Py::Object obj)
519 {
520 auto self = getComplexGeoDataPtr();
521 if (obj.isNone()) {
522 if (self->Hasher) {
523 self->Hasher = App::StringHasherRef();
524 self->resetElementMap();
525 }
526 }
527 else if (PyObject_TypeCheck(obj.ptr(), &App::StringHasherPy::Type)) {
528 App::StringHasherRef ref(
529 static_cast<App::StringHasherPy*>(obj.ptr())->getStringHasherPtr());
530 if (self->Hasher != ref) {
531 self->Hasher = ref;
532 self->resetElementMap();
533 }
534 }
535 else {
536 throw Py::TypeError("invalid type");
537 }
538 }
539
540 Py::Object ComplexGeoDataPy::getBoundBox() const
541 {
542 return Py::BoundingBox(getComplexGeoDataPtr()->getBoundBox());
543 }
544
545 Py::Object ComplexGeoDataPy::getCenterOfGravity() const
546 {
547 Base::Vector3d center;
548 if (getComplexGeoDataPtr()->getCenterOfGravity(center)) {
549 return Py::Vector(center);
550 }
551 throw Py::RuntimeError("Cannot get center of gravity");
552 }
553
554 Py::Object ComplexGeoDataPy::getPlacement() const
555 {
556 return Py::Placement(getComplexGeoDataPtr()->getPlacement());
557 }
558
559 void ComplexGeoDataPy::setPlacement(Py::Object arg)
560 {
561 PyObject* p = arg.ptr();
562 if (PyObject_TypeCheck(p, &(Base::PlacementPy::Type))) {
563 Base::Placement* trf = static_cast<Base::PlacementPy*>(p)->getPlacementPtr();
564 getComplexGeoDataPtr()->setPlacement(*trf);
565 }
566 else {
567 std::string error = std::string("type must be 'Placement', not ");
568 error += p->ob_type->tp_name;
569 throw Py::TypeError(error);
570 }
571 }
572
573 Py::String ComplexGeoDataPy::getElementMapVersion() const
574 {
575 return Py::String(getComplexGeoDataPtr()->getElementMapVersion());
576 }
577
578
579 Py::Long ComplexGeoDataPy::getTag() const
580 {
581 return Py::Long(getComplexGeoDataPtr()->Tag);
582 }
583
584 void ComplexGeoDataPy::setTag(Py::Long tag)
585 {
586 getComplexGeoDataPtr()->Tag = tag;
587 }
588
589 PyObject* ComplexGeoDataPy::getCustomAttributes(const char* attr) const
590 {
591 // Support for backward compatibility
592 if (strcmp(attr, "Matrix") == 0) {
593 Py::Matrix mat(getComplexGeoDataPtr()->getTransform());
594 return Py::new_reference_to(mat);
595 }
596 return nullptr;
597 }
598
599 int ComplexGeoDataPy::setCustomAttributes(const char* attr, PyObject* obj)
600 {
601 // Support for backward compatibility
602 if (strcmp(attr, "Matrix") == 0) {
603 if (PyObject_TypeCheck(obj, &(Base::MatrixPy::Type))) {
604 Base::Matrix4D mat = static_cast<Base::MatrixPy*>(obj)->value();
605 getComplexGeoDataPtr()->setTransform(mat);
606 return 1;
607 }
608 else {
609 std::string error = std::string("type must be 'Matrix', not ");
610 error += obj->ob_type->tp_name;
611 throw Py::TypeError(error);
612 }
613 }
614 return 0;
615 }