/
SketchObjectPyImp.cpp
1921 lines (1569 loc) · 63.9 KB
/
SketchObjectPyImp.cpp
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (c) 2010 Jürgen Riegel <juergen.riegel@web.de> *
* *
* 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 "PreCompiled.h"
#ifndef _PreComp_
# include <sstream>
# include <Geom_TrimmedCurve.hxx>
# include <memory>
#endif
#include <Mod/Sketcher/App/SketchObject.h>
#include <Mod/Part/App/LinePy.h>
#include <Mod/Part/App/Geometry.h>
#include <Mod/Part/App/DatumFeature.h>
#include <Base/GeometryPyCXX.h>
#include <Base/VectorPy.h>
#include <Base/AxisPy.h>
#include <Base/Tools.h>
#include <Base/QuantityPy.h>
#include <App/Document.h>
#include <App/OriginFeature.h>
#include <CXX/Objects.hxx>
// inclusion of the generated files (generated out of SketchObjectSFPy.xml)
#include "SketchObjectPy.h"
#include "SketchObjectPy.cpp"
// other python types
#include "ConstraintPy.h"
#include "GeometryFacade.h"
#include "GeometryFacadePy.h"
using namespace Sketcher;
// returns a string which represents the object e.g. when printed in python
std::string SketchObjectPy::representation(void) const
{
return "<Sketcher::SketchObject>";
}
PyObject* SketchObjectPy::solve(PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
int ret = this->getSketchObjectPtr()->solve();
return Py_BuildValue("i", ret);
}
PyObject* SketchObjectPy::addGeometry(PyObject *args)
{
PyObject *pcObj;
PyObject* construction; // this is an optional argument default false
bool isConstruction;
if (!PyArg_ParseTuple(args, "OO!", &pcObj, &PyBool_Type, &construction)) {
PyErr_Clear();
if (!PyArg_ParseTuple(args, "O", &pcObj))
return 0;
else
isConstruction=false;
}
else {
isConstruction = PyObject_IsTrue(construction) ? true : false;
}
if (PyObject_TypeCheck(pcObj, &(Part::GeometryPy::Type))) {
Part::Geometry *geo = static_cast<Part::GeometryPy*>(pcObj)->getGeometryPtr();
int ret;
// An arc created with Part.Arc will be converted into a Part.ArcOfCircle
if (geo->getTypeId() == Part::GeomTrimmedCurve::getClassTypeId()) {
Handle(Geom_TrimmedCurve) trim = Handle(Geom_TrimmedCurve)::DownCast(geo->handle());
Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast(trim->BasisCurve());
Handle(Geom_Ellipse) ellipse = Handle(Geom_Ellipse)::DownCast(trim->BasisCurve());
if (!circle.IsNull()) {
// create the definition struct for that geom
Part::GeomArcOfCircle aoc;
aoc.setHandle(trim);
ret = this->getSketchObjectPtr()->addGeometry(&aoc,isConstruction);
}
else if (!ellipse.IsNull()) {
// create the definition struct for that geom
Part::GeomArcOfEllipse aoe;
aoe.setHandle(trim);
ret = this->getSketchObjectPtr()->addGeometry(&aoe,isConstruction);
}
else {
std::stringstream str;
str << "Unsupported geometry type: " << geo->getTypeId().getName();
PyErr_SetString(PyExc_TypeError, str.str().c_str());
return 0;
}
}
else if (geo->getTypeId() == Part::GeomPoint::getClassTypeId() ||
geo->getTypeId() == Part::GeomCircle::getClassTypeId() ||
geo->getTypeId() == Part::GeomEllipse::getClassTypeId() ||
geo->getTypeId() == Part::GeomArcOfCircle::getClassTypeId() ||
geo->getTypeId() == Part::GeomArcOfEllipse::getClassTypeId() ||
geo->getTypeId() == Part::GeomArcOfHyperbola::getClassTypeId() ||
geo->getTypeId() == Part::GeomArcOfParabola::getClassTypeId() ||
geo->getTypeId() == Part::GeomBSplineCurve::getClassTypeId() ||
geo->getTypeId() == Part::GeomLineSegment::getClassTypeId()) {
ret = this->getSketchObjectPtr()->addGeometry(geo,isConstruction);
}
else {
std::stringstream str;
str << "Unsupported geometry type: " << geo->getTypeId().getName();
PyErr_SetString(PyExc_TypeError, str.str().c_str());
return 0;
}
return Py::new_reference_to(Py::Long(ret));
}
else if (PyObject_TypeCheck(pcObj, &(PyList_Type)) ||
PyObject_TypeCheck(pcObj, &(PyTuple_Type))) {
std::vector<Part::Geometry *> geoList;
std::vector<std::shared_ptr <Part::Geometry> > tmpList;
Py::Sequence list(pcObj);
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
if (PyObject_TypeCheck((*it).ptr(), &(Part::GeometryPy::Type))) {
Part::Geometry *geo = static_cast<Part::GeometryPy*>((*it).ptr())->getGeometryPtr();
// An arc created with Part.Arc will be converted into a Part.ArcOfCircle
if (geo->getTypeId() == Part::GeomTrimmedCurve::getClassTypeId()) {
Handle(Geom_TrimmedCurve) trim = Handle(Geom_TrimmedCurve)::DownCast(geo->handle());
Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast(trim->BasisCurve());
Handle(Geom_Ellipse) ellipse = Handle(Geom_Ellipse)::DownCast(trim->BasisCurve());
if (!circle.IsNull()) {
// create the definition struct for that geom
std::shared_ptr<Part::GeomArcOfCircle> aoc(new Part::GeomArcOfCircle());
aoc->setHandle(trim);
geoList.push_back(aoc.get());
tmpList.push_back(aoc);
}
else if (!ellipse.IsNull()) {
// create the definition struct for that geom
std::shared_ptr<Part::GeomArcOfEllipse> aoe(new Part::GeomArcOfEllipse());
aoe->setHandle(trim);
geoList.push_back(aoe.get());
tmpList.push_back(aoe);
}
else {
std::stringstream str;
str << "Unsupported geometry type: " << geo->getTypeId().getName();
PyErr_SetString(PyExc_TypeError, str.str().c_str());
return 0;
}
}
else if (geo->getTypeId() == Part::GeomPoint::getClassTypeId() ||
geo->getTypeId() == Part::GeomCircle::getClassTypeId() ||
geo->getTypeId() == Part::GeomEllipse::getClassTypeId() ||
geo->getTypeId() == Part::GeomArcOfCircle::getClassTypeId() ||
geo->getTypeId() == Part::GeomArcOfEllipse::getClassTypeId() ||
geo->getTypeId() == Part::GeomArcOfHyperbola::getClassTypeId() ||
geo->getTypeId() == Part::GeomArcOfParabola::getClassTypeId() ||
geo->getTypeId() == Part::GeomBSplineCurve::getClassTypeId() ||
geo->getTypeId() == Part::GeomLineSegment::getClassTypeId()) {
geoList.push_back(geo);
}
else {
std::stringstream str;
str << "Unsupported geometry type: " << geo->getTypeId().getName();
PyErr_SetString(PyExc_TypeError, str.str().c_str());
return 0;
}
}
}
int ret = this->getSketchObjectPtr()->addGeometry(geoList,isConstruction) + 1;
std::size_t numGeo = geoList.size();
Py::Tuple tuple(numGeo);
for (std::size_t i=0; i<numGeo; ++i) {
int geoId = ret - int(numGeo - i);
tuple.setItem(i, Py::Long(geoId));
}
return Py::new_reference_to(tuple);
}
std::string error = std::string("type must be 'Geometry' or list of 'Geometry', not ");
error += pcObj->ob_type->tp_name;
throw Py::TypeError(error);
}
PyObject* SketchObjectPy::delGeometry(PyObject *args)
{
int Index;
if (!PyArg_ParseTuple(args, "i", &Index))
return 0;
if (this->getSketchObjectPtr()->delGeometry(Index)) {
std::stringstream str;
str << "Not able to delete a geometry with the given index: " << Index;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::delGeometries(PyObject *args)
{
PyObject *pcObj;
if (!PyArg_ParseTuple(args, "O", &pcObj))
return 0;
if (PyObject_TypeCheck(pcObj, &(PyList_Type)) ||
PyObject_TypeCheck(pcObj, &(PyTuple_Type)) ) {
std::vector<int> geoIdList;
Py::Sequence list(pcObj);
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
if (PyLong_Check((*it).ptr()))
geoIdList.push_back(PyLong_AsLong((*it).ptr()));
}
if(this->getSketchObjectPtr()->delGeometries(geoIdList)) {
std::stringstream str;
str << "Not able to delete geometries";
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
std::string error = std::string("type must be list of GeoIds, not ");
error += pcObj->ob_type->tp_name;
throw Py::TypeError(error);
}
PyObject* SketchObjectPy::deleteAllGeometry(PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
if (this->getSketchObjectPtr()->deleteAllGeometry()) {
std::stringstream str;
str << "Unable to delete Geometry";
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::deleteAllConstraints(PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
if (this->getSketchObjectPtr()->deleteAllConstraints()) {
std::stringstream str;
str << "Unable to delete Constraints";
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::toggleConstruction(PyObject *args)
{
int Index;
if (!PyArg_ParseTuple(args, "i", &Index))
return 0;
if (this->getSketchObjectPtr()->toggleConstruction(Index)) {
std::stringstream str;
str << "Not able to toggle a geometry with the given index: " << Index;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::setConstruction(PyObject *args)
{
int Index;
PyObject *Mode;
if (!PyArg_ParseTuple(args, "iO!", &Index, &PyBool_Type, &Mode))
return 0;
if (this->getSketchObjectPtr()->setConstruction(Index, PyObject_IsTrue(Mode) ? true : false)) {
std::stringstream str;
str << "Not able to set construction mode of a geometry with the given index: " << Index;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::getConstruction(PyObject *args)
{
int Index;
if (!PyArg_ParseTuple(args, "i", &Index))
return 0;
auto gf = this->getSketchObjectPtr()->getGeometryFacade(Index);
if(gf)
return Py::new_reference_to(Py::Boolean(gf->getConstruction()));
std::stringstream str;
str << "Not able to retrieve construction mode of a geometry with the given index: " << Index;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
PyObject* SketchObjectPy::addConstraint(PyObject *args)
{
PyObject *pcObj;
if (!PyArg_ParseTuple(args, "O", &pcObj))
return 0;
if (PyObject_TypeCheck(pcObj, &(Sketcher::ConstraintPy::Type))) {
Sketcher::Constraint *constr = static_cast<Sketcher::ConstraintPy*>(pcObj)->getConstraintPtr();
if (!this->getSketchObjectPtr()->evaluateConstraint(constr)) {
PyErr_SetString(PyExc_IndexError, "Constraint has invalid indexes");
return 0;
}
int ret = this->getSketchObjectPtr()->addConstraint(constr);
// this solve is necessary because:
// 1. The addition of constraint is part of a command addition
// 2. This solve happens before the command is committed
// 3. A constraint, may effect a geometry change (think of coincident,
// a line's point moves to meet the other line's point
// 4. The transaction is committed before any other solve, for example
// the one of execute() triggered by a recompute (UpdateActive) is generated.
// 5. Upon "undo", the constraint is removed (it was before the command was committed)
// however, the geometry changed after the command was committed, so the point that
// moved do not go back to the position where it was.
//
// N.B.: However, the solve itself may be inhibited in cases where groups of geometry/constraints
// are added together, because in that case undoing will also make the geometry disappear.
this->getSketchObjectPtr()->solve();
// if the geometry moved during the solve, then the initial solution is invalid
// at this point, so a point movement may not work in cases where redundant constraints exist.
// this forces recalculation of the initial solution (not a full solve)
if(this->getSketchObjectPtr()->noRecomputes) {
this->getSketchObjectPtr()->setUpSketch();
this->getSketchObjectPtr()->Constraints.touch(); // update solver information
}
return Py::new_reference_to(Py::Long(ret));
}
else if (PyObject_TypeCheck(pcObj, &(PyList_Type)) ||
PyObject_TypeCheck(pcObj, &(PyTuple_Type))) {
std::vector<Constraint*> values;
Py::Sequence list(pcObj);
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
if (PyObject_TypeCheck((*it).ptr(), &(ConstraintPy::Type))) {
Constraint *con = static_cast<ConstraintPy*>((*it).ptr())->getConstraintPtr();
values.push_back(con);
}
}
for (std::vector<Constraint*>::iterator it = values.begin(); it != values.end(); ++it) {
if (!this->getSketchObjectPtr()->evaluateConstraint(*it)) {
PyErr_SetString(PyExc_IndexError, "Constraint has invalid indexes");
return 0;
}
}
int ret = getSketchObjectPtr()->addConstraints(values) + 1;
std::size_t numCon = values.size();
Py::Tuple tuple(numCon);
for (std::size_t i=0; i<numCon; ++i) {
int conId = ret - int(numCon - i);
tuple.setItem(i, Py::Long(conId));
}
return Py::new_reference_to(tuple);
}
std::string error = std::string("type must be 'Constraint' or list of 'Constraint', not ");
error += pcObj->ob_type->tp_name;
throw Py::TypeError(error);
}
PyObject* SketchObjectPy::delConstraint(PyObject *args)
{
int Index;
if (!PyArg_ParseTuple(args, "i", &Index))
return 0;
if (this->getSketchObjectPtr()->delConstraint(Index)) {
std::stringstream str;
str << "Not able to delete a constraint with the given index: " << Index;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::renameConstraint(PyObject *args)
{
int Index;
char* utf8Name;
if (!PyArg_ParseTuple(args, "iet", &Index, "utf-8", &utf8Name))
return 0;
std::string Name = utf8Name;
PyMem_Free(utf8Name);
if (this->getSketchObjectPtr()->Constraints.getSize() <= Index) {
std::stringstream str;
str << "Not able to rename a constraint with the given index: " << Index;
PyErr_SetString(PyExc_IndexError, str.str().c_str());
return 0;
}
if (!Name.empty()) {
if (!Sketcher::PropertyConstraintList::validConstraintName(Name)) {
std::stringstream str;
str << "Invalid constraint name with the given index: " << Index;
PyErr_SetString(PyExc_IndexError, str.str().c_str());
return 0;
}
const std::vector< Sketcher::Constraint * > &vals = getSketchObjectPtr()->Constraints.getValues();
for (std::size_t i = 0; i < vals.size(); ++i) {
if (static_cast<int>(i) != Index && Name == vals[i]->Name) {
PyErr_SetString(PyExc_ValueError, "Duplicate constraint not allowed");
return 0;
}
}
}
this->getSketchObjectPtr()->renameConstraint(Index, Name);
Py_Return;
}
PyObject* SketchObjectPy::getIndexByName(PyObject *args)
{
char* utf8Name;
if (!PyArg_ParseTuple(args, "et", "utf-8", &utf8Name))
return 0;
std::string Name = utf8Name;
PyMem_Free(utf8Name);
if (Name.empty()) {
PyErr_SetString(PyExc_ValueError, "Passed string is empty");
return 0;
}
const std::vector< Sketcher::Constraint * > &vals = getSketchObjectPtr()->Constraints.getValues();
for (std::size_t i = 0; i < vals.size(); ++i) {
if (Name == vals[i]->Name) {
return Py_BuildValue("i", i);
}
}
PyErr_SetString(PyExc_LookupError, "No such constraint found");
return 0;
}
PyObject* SketchObjectPy::carbonCopy(PyObject *args)
{
char *ObjectName;
PyObject *construction = Py_True;
if (!PyArg_ParseTuple(args, "s|O!:Give an object", &ObjectName, &PyBool_Type, &construction))
return 0;
Sketcher::SketchObject* skObj = this->getSketchObjectPtr();
App::DocumentObject * Obj = skObj->getDocument()->getObject(ObjectName);
if (!Obj) {
std::stringstream str;
str << ObjectName << " does not exist in the document";
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
// check if this type of external geometry is allowed
if (!skObj->isExternalAllowed(Obj->getDocument(), Obj) && (Obj->getTypeId() != Sketcher::SketchObject::getClassTypeId())) {
std::stringstream str;
str << ObjectName << " is not allowed for a carbon copy operation in this sketch";
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
// add the external
if (skObj->carbonCopy(Obj, PyObject_IsTrue(construction) ? true : false) < 0) {
std::stringstream str;
str << "Not able to add the requested geometry";
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::addExternal(PyObject *args)
{
char *ObjectName;
char *SubName;
if (!PyArg_ParseTuple(args, "ss:Give an object and subelement name", &ObjectName,&SubName))
return 0;
// get the target object for the external link
Sketcher::SketchObject* skObj = this->getSketchObjectPtr();
App::DocumentObject * Obj = skObj->getDocument()->getObject(ObjectName);
if (!Obj) {
std::stringstream str;
str << ObjectName << " does not exist in the document";
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
// check if this type of external geometry is allowed
if (!skObj->isExternalAllowed(Obj->getDocument(), Obj)) {
std::stringstream str;
str << ObjectName << " is not allowed as external geometry of this sketch";
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
// add the external
if (skObj->addExternal(Obj,SubName) < 0) {
std::stringstream str;
str << "Not able to add external shape element";
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::delExternal(PyObject *args)
{
int Index;
if (!PyArg_ParseTuple(args, "i", &Index))
return 0;
if (this->getSketchObjectPtr()->delExternal(Index)) {
std::stringstream str;
str << "Not able to delete an external geometry with the given index: " << Index;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::delConstraintOnPoint(PyObject *args)
{
int Index, pos=-1;
if (!PyArg_ParseTuple(args, "i|i", &Index, &pos))
return 0;
if (pos>=0 && pos<=3) { // Sketcher::none Sketcher::mid
if (this->getSketchObjectPtr()->delConstraintOnPoint(Index,(Sketcher::PointPos)pos)) {
std::stringstream str;
str << "Not able to delete a constraint on point with the given index: " << Index
<< " and position: " << pos;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
}
else if (pos==-1) {
if (this->getSketchObjectPtr()->delConstraintOnPoint(Index)) {
std::stringstream str;
str << "Not able to delete a constraint on point with the given index: " << Index;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
}
else {
PyErr_SetString(PyExc_ValueError, "Wrong PointPos argument");
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::setDatum(PyObject *args)
{
double Datum;
int Index;
PyObject* object;
Base::Quantity Quantity;
do {
// handle (int,Quantity)
if (PyArg_ParseTuple(args,"iO!", &Index, &(Base::QuantityPy::Type), &object)) {
Quantity = *(static_cast<Base::QuantityPy*>(object)->getQuantityPtr());
if (Quantity.getUnit() == Base::Unit::Angle) {
Datum = Base::toRadians<double>(Quantity.getValue());
break;
}
else {
Datum = Quantity.getValue();
break;
}
}
// handle (int,double)
PyErr_Clear();
if (PyArg_ParseTuple(args, "id", &Index, &Datum)) {
Quantity.setValue(Datum);
break;
}
// handle (string,Quantity)
char* constrName;
PyErr_Clear();
if (PyArg_ParseTuple(args,"sO!", &constrName, &(Base::QuantityPy::Type), &object)) {
Quantity = *(static_cast<Base::QuantityPy*>(object)->getQuantityPtr());
if (Quantity.getUnit() == Base::Unit::Angle) {
Datum = Base::toRadians<double>(Quantity.getValue());
}
else {
Datum = Quantity.getValue();
}
int i = 0;
Index = -1;
const std::vector<Constraint*>& vals = this->getSketchObjectPtr()->Constraints.getValues();
for (std::vector<Constraint*>::const_iterator it = vals.begin(); it != vals.end(); ++it, ++i) {
if ((*it)->Name == constrName) {
Index = i;
break;
}
}
if (Index >= 0) {
break;
}
else {
std::stringstream str;
str << "Invalid constraint name: '" << constrName << "'";
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
}
// handle (string,double)
PyErr_Clear();
if (PyArg_ParseTuple(args, "sd", &constrName, &Datum)) {
Quantity.setValue(Datum);
int i = 0;
Index = -1;
const std::vector<Constraint*>& vals = this->getSketchObjectPtr()->Constraints.getValues();
for (std::vector<Constraint*>::const_iterator it = vals.begin(); it != vals.end(); ++it, ++i) {
if ((*it)->Name == constrName) {
Index = i;
break;
}
}
if (Index >= 0) {
break;
}
else {
std::stringstream str;
str << "Invalid constraint name: '" << constrName << "'";
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
}
// error handling
PyErr_SetString(PyExc_TypeError, "Wrong arguments");
return 0;
}
while (false);
int err=this->getSketchObjectPtr()->setDatum(Index, Datum);
if (err) {
std::stringstream str;
if (err == -1)
str << "Invalid constraint index: " << Index;
else if (err == -3)
str << "Cannot set the datum because the sketch contains conflicting constraints";
else if (err == -2)
str << "Datum " << (const char*)Quantity.getUserString().toUtf8() << " for the constraint with index " << Index << " is invalid";
else if (err == -4)
str << "Negative datum values are not valid for the constraint with index " << Index;
else if (err == -5)
str << "Zero is not a valid datum for the constraint with index " << Index;
else if (err == -6)
str << "Cannot set the datum because of invalid geometry";
else
str << "Unexpected problem at setting datum " << (const char*)Quantity.getUserString().toUtf8() << " for the constraint with index " << Index;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::getDatum(PyObject *args)
{
const std::vector<Constraint*>& vals = this->getSketchObjectPtr()->Constraints.getValues();
Constraint* constr = 0;
do {
int index = 0;
if (PyArg_ParseTuple(args,"i", &index)) {
if (index < 0 || index >= static_cast<int>(vals.size())) {
PyErr_SetString(PyExc_IndexError, "index out of range");
return 0;
}
constr = vals[index];
break;
}
PyErr_Clear();
char* name;
if (PyArg_ParseTuple(args,"s", &name)) {
int id = 0;
for (std::vector<Constraint*>::const_iterator it = vals.begin(); it != vals.end(); ++it, ++id) {
if (Sketcher::PropertyConstraintList::getConstraintName((*it)->Name, id) == name) {
constr = *it;
break;
}
}
if (!constr) {
std::stringstream str;
str << "Invalid constraint name: '" << name << "'";
PyErr_SetString(PyExc_NameError, str.str().c_str());
return 0;
}
else {
break;
}
}
// error handling
PyErr_SetString(PyExc_TypeError, "Wrong arguments");
return 0;
}
while (false);
ConstraintType type = constr->Type;
if (type != Distance &&
type != DistanceX &&
type != DistanceY &&
type != Radius &&
type != Diameter &&
type != Angle) {
PyErr_SetString(PyExc_TypeError, "Constraint is not a datum");
return 0;
}
Base::Quantity datum;
datum.setValue(constr->getValue());
if (type == Angle) {
datum.setValue(Base::toDegrees<double>(datum.getValue()));
datum.setUnit(Base::Unit::Angle);
}
else {
datum.setUnit(Base::Unit::Length);
}
return new Base::QuantityPy(new Base::Quantity(datum));
}
PyObject* SketchObjectPy::setDriving(PyObject *args)
{
PyObject* driving;
int constrid;
if (!PyArg_ParseTuple(args, "iO!", &constrid, &PyBool_Type, &driving))
return 0;
if (this->getSketchObjectPtr()->setDriving(constrid, PyObject_IsTrue(driving) ? true : false)) {
std::stringstream str;
str << "Not able set Driving/reference for constraint with the given index: " << constrid;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::setDatumsDriving(PyObject *args)
{
PyObject* driving;
if (!PyArg_ParseTuple(args, "O!", &PyBool_Type, &driving))
return 0;
if (this->getSketchObjectPtr()->setDatumsDriving(PyObject_IsTrue(driving) ? true : false)) {
std::stringstream str;
str << "Not able set all dimensionals driving/reference";
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::moveDatumsToEnd(PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
if (this->getSketchObjectPtr()->moveDatumsToEnd()) {
std::stringstream str;
str << "Not able move all dimensionals to end";
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::getDriving(PyObject *args)
{
int constrid;
bool driving;
if (!PyArg_ParseTuple(args, "i", &constrid))
return 0;
if (this->getSketchObjectPtr()->getDriving(constrid, driving)) {
PyErr_SetString(PyExc_ValueError, "Invalid constraint id");
return 0;
}
return Py::new_reference_to(Py::Boolean(driving));
}
PyObject* SketchObjectPy::toggleDriving(PyObject *args)
{
int constrid;
if (!PyArg_ParseTuple(args, "i", &constrid))
return 0;
if (this->getSketchObjectPtr()->toggleDriving(constrid)) {
std::stringstream str;
str << "Not able toggle Driving for constraint with the given index: " << constrid;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::setVirtualSpace(PyObject *args)
{
PyObject* invirtualspace;
int constrid;
if (!PyArg_ParseTuple(args, "iO!", &constrid, &PyBool_Type, &invirtualspace))
return 0;
if (this->getSketchObjectPtr()->setVirtualSpace(constrid, PyObject_IsTrue(invirtualspace) ? true : false)) {
std::stringstream str;
str << "Not able set virtual space for constraint with the given index: " << constrid;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::getVirtualSpace(PyObject *args)
{
int constrid;
bool invirtualspace;
if (!PyArg_ParseTuple(args, "i", &constrid))
return 0;
if (this->getSketchObjectPtr()->getVirtualSpace(constrid, invirtualspace)) {
PyErr_SetString(PyExc_ValueError, "Invalid constraint id");
return 0;
}
return Py::new_reference_to(Py::Boolean(invirtualspace));
}
PyObject* SketchObjectPy::toggleVirtualSpace(PyObject *args)
{
int constrid;
if (!PyArg_ParseTuple(args, "i", &constrid))
return 0;
if (this->getSketchObjectPtr()->toggleVirtualSpace(constrid)) {
std::stringstream str;
str << "Not able toggle virtual space for constraint with the given index: " << constrid;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::setActive(PyObject *args)
{
PyObject* isactive;
int constrid;
if (!PyArg_ParseTuple(args, "iO!", &constrid, &PyBool_Type, &isactive))
return 0;
if (this->getSketchObjectPtr()->setActive(constrid, PyObject_IsTrue(isactive) ? true : false)) {
std::stringstream str;
str << "Not able set active/disabled status for constraint with the given index: " << constrid;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::getActive(PyObject *args)
{
int constrid;
bool isactive;
if (!PyArg_ParseTuple(args, "i", &constrid))
return 0;
if (this->getSketchObjectPtr()->getActive(constrid, isactive)) {
PyErr_SetString(PyExc_ValueError, "Invalid constraint id");
return 0;
}
return Py::new_reference_to(Py::Boolean(isactive));
}
PyObject* SketchObjectPy::toggleActive(PyObject *args)
{
int constrid;
if (!PyArg_ParseTuple(args, "i", &constrid))
return 0;
if (this->getSketchObjectPtr()->toggleActive(constrid)) {
std::stringstream str;
str << "Not able toggle on/off constraint with the given index: " << constrid;
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::movePoint(PyObject *args)
{
PyObject *pcObj;
int GeoId, PointType;
int relative=0;
if (!PyArg_ParseTuple(args, "iiO!|i", &GeoId, &PointType, &(Base::VectorPy::Type), &pcObj, &relative))
return 0;
Base::Vector3d v1 = static_cast<Base::VectorPy*>(pcObj)->value();
if (this->getSketchObjectPtr()->movePoint(GeoId,(Sketcher::PointPos)PointType,v1,(relative>0))) {
std::stringstream str;
str << "Not able to move point with the id and type: (" << GeoId << ", " << PointType << ")";
PyErr_SetString(PyExc_ValueError, str.str().c_str());
return 0;
}
Py_Return;
}
PyObject* SketchObjectPy::getGeoVertexIndex(PyObject *args)
{
int index;
if (!PyArg_ParseTuple(args, "i", &index))
return 0;
SketchObject* obj = this->getSketchObjectPtr();
int geoId;
PointPos posId;
obj->getGeoVertexIndex(index, geoId, posId);