/
Geometry.cpp
3903 lines (3215 loc) · 121 KB
/
Geometry.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) 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 "PreCompiled.h"
#ifndef _PreComp_
# include <BRepBuilderAPI_MakeEdge.hxx>
# include <BRepBuilderAPI_MakeFace.hxx>
# include <BRepBuilderAPI_MakeVertex.hxx>
# include <Geom_CartesianPoint.hxx>
# include <Geom_Circle.hxx>
# include <Geom_Curve.hxx>
# include <Geom_Ellipse.hxx>
# include <Geom_Hyperbola.hxx>
# include <Geom_Parabola.hxx>
# include <Geom_OffsetCurve.hxx>
# include <Geom_TrimmedCurve.hxx>
# include <Geom_Line.hxx>
# include <Geom_BezierCurve.hxx>
# include <Geom_BezierSurface.hxx>
# include <Geom_BSplineCurve.hxx>
# include <Geom_BSplineSurface.hxx>
# include <Geom_Surface.hxx>
# include <Geom_Plane.hxx>
# include <Geom_CylindricalSurface.hxx>
# include <Geom_ConicalSurface.hxx>
# include <Geom_SphericalSurface.hxx>
# include <Geom_ToroidalSurface.hxx>
# include <Geom_OffsetSurface.hxx>
# include <GeomPlate_Surface.hxx>
# include <Geom_RectangularTrimmedSurface.hxx>
# include <Geom_SurfaceOfRevolution.hxx>
# include <Geom_SurfaceOfLinearExtrusion.hxx>
# include <GeomAPI_Interpolate.hxx>
# include <GeomConvert.hxx>
# include <GeomConvert_CompCurveToBSplineCurve.hxx>
# include <GeomLProp_CLProps.hxx>
# include <GeomLProp_SLProps.hxx>
# include <gp.hxx>
# include <gp_Ax2.hxx>
# include <gp_Circ.hxx>
# include <gp_Elips.hxx>
# include <gp_Hypr.hxx>
# include <gp_Parab.hxx>
# include <gce_ErrorType.hxx>
# include <gp_Lin.hxx>
# include <gp_Pln.hxx>
# include <gp_Pnt.hxx>
# include <gp_Cylinder.hxx>
# include <gp_Cone.hxx>
# include <gp_Sphere.hxx>
# include <gp_Torus.hxx>
# include <Standard_Real.hxx>
# include <Standard_Version.hxx>
# include <Standard_ConstructionError.hxx>
# include <TColgp_Array1OfPnt.hxx>
# include <TColgp_Array2OfPnt.hxx>
# include <TColgp_Array1OfVec.hxx>
# include <TColgp_HArray1OfPnt.hxx>
# include <TColStd_HArray1OfBoolean.hxx>
# include <TColStd_Array1OfReal.hxx>
# include <TColStd_Array1OfInteger.hxx>
# include <gp.hxx>
# include <gp_Lin.hxx>
# include <Geom_Line.hxx>
# include <Geom_TrimmedCurve.hxx>
# include <GC_MakeArcOfCircle.hxx>
# include <GC_MakeCircle.hxx>
# include <GC_MakeArcOfEllipse.hxx>
# include <GC_MakeEllipse.hxx>
# include <gce_MakeParab.hxx>
# include <GC_MakeArcOfParabola.hxx>
# include <GC_MakeHyperbola.hxx>
# include <GC_MakeArcOfHyperbola.hxx>
# include <GC_MakeLine.hxx>
# include <GC_MakeSegment.hxx>
# include <Precision.hxx>
# include <GeomAPI_ProjectPointOnCurve.hxx>
#endif
#include "LinePy.h"
#include <Base/VectorPy.h>
#include "CirclePy.h"
#include "EllipsePy.h"
#include "ArcPy.h"
#include "ArcOfCirclePy.h"
#include "ArcOfEllipsePy.h"
#include "ArcOfParabolaPy.h"
#include "BezierCurvePy.h"
#include "BSplineCurvePy.h"
#include "HermiteCurvePy.h"
#include "HyperbolaPy.h"
#include "ArcOfHyperbolaPy.h"
#include "OffsetCurvePy.h"
#include "ParabolaPy.h"
#include "BezierSurfacePy.h"
#include "BSplineSurfacePy.h"
#include "ConePy.h"
#include "CylinderPy.h"
#include "OffsetSurfacePy.h"
#include "PlateSurfacePy.h"
#include "PlanePy.h"
#include "RectangularTrimmedSurfacePy.h"
#include "SpherePy.h"
#include "SurfaceOfExtrusionPy.h"
#include "SurfaceOfRevolutionPy.h"
#include "ToroidPy.h"
#include <Base/Exception.h>
#include <Base/Writer.h>
#include <Base/Reader.h>
#include <Base/Tools.h>
#include "Geometry.h"
using namespace Part;
const char* gce_ErrorStatusText(gce_ErrorType et)
{
switch (et)
{
case gce_Done:
return "Construction was successful";
case gce_ConfusedPoints:
return "Two points are coincident";
case gce_NegativeRadius:
return "Radius value is negative";
case gce_ColinearPoints:
return "Three points are collinear";
case gce_IntersectionError:
return "Intersection cannot be computed";
case gce_NullAxis:
return "Axis is undefined";
case gce_NullAngle:
return "Angle value is invalid (usually null)";
case gce_NullRadius:
return "Radius is null";
case gce_InvertAxis:
return "Axis value is invalid";
case gce_BadAngle:
return "Angle value is invalid";
case gce_InvertRadius:
return "Radius value is incorrect (usually with respect to another radius)";
case gce_NullFocusLength:
return "Focal distance is null";
case gce_NullVector:
return "Vector is null";
case gce_BadEquation:
return "Coefficients are incorrect (applies to the equation of a geometric object)";
default:
return "Creation of geometry failed";
}
}
// ---------------------------------------------------------------
TYPESYSTEM_SOURCE_ABSTRACT(Part::Geometry,Base::Persistence);
Geometry::Geometry()
: Construction(false)
{
}
Geometry::~Geometry()
{
}
// Persistence implementer
unsigned int Geometry::getMemSize (void) const
{
return 1;
}
void Geometry::Save(Base::Writer &writer) const
{
const char c = Construction?'1':'0';
writer.Stream() << writer.ind() << "<Construction value=\"" << c << "\"/>" << endl;
}
void Geometry::Restore(Base::XMLReader &reader)
{
// read my Element
reader.readElement("Construction");
// get the value of my Attribute
Construction = (int)reader.getAttributeAsInteger("value")==0?false:true;
}
// -------------------------------------------------
TYPESYSTEM_SOURCE(Part::GeomPoint,Part::Geometry);
GeomPoint::GeomPoint()
{
this->myPoint = new Geom_CartesianPoint(0,0,0);
}
GeomPoint::GeomPoint(const Handle_Geom_CartesianPoint& p)
{
this->myPoint = Handle_Geom_CartesianPoint::DownCast(p->Copy());
}
GeomPoint::GeomPoint(const Base::Vector3d& p)
{
this->myPoint = new Geom_CartesianPoint(p.x,p.y,p.z);
}
GeomPoint::~GeomPoint()
{
}
const Handle_Geom_Geometry& GeomPoint::handle() const
{
return myPoint;
}
Geometry *GeomPoint::clone(void) const
{
GeomPoint *newPoint = new GeomPoint(myPoint);
newPoint->Construction = this->Construction;
return newPoint;
}
TopoDS_Shape GeomPoint::toShape() const
{
return BRepBuilderAPI_MakeVertex(myPoint->Pnt());
}
Base::Vector3d GeomPoint::getPoint(void)const
{
return Base::Vector3d(myPoint->X(),myPoint->Y(),myPoint->Z());
}
void GeomPoint::setPoint(const Base::Vector3d& p)
{
this->myPoint->SetCoord(p.x,p.y,p.z);
}
// Persistence implementer
unsigned int GeomPoint::getMemSize (void) const
{
return sizeof(Geom_CartesianPoint);
}
void GeomPoint::Save(Base::Writer &writer) const
{
// save the attributes of the father class
Geometry::Save(writer);
Base::Vector3d Point = getPoint();
writer.Stream()
<< writer.ind()
<< "<GeomPoint "
<< "X=\"" << Point.x <<
"\" Y=\"" << Point.y <<
"\" Z=\"" << Point.z <<
"\"/>" << endl;
}
void GeomPoint::Restore(Base::XMLReader &reader)
{
// read the attributes of the father class
Geometry::Restore(reader);
double X,Y,Z;
// read my Element
reader.readElement("GeomPoint");
// get the value of my Attribute
X = reader.getAttributeAsFloat("X");
Y = reader.getAttributeAsFloat("Y");
Z = reader.getAttributeAsFloat("Z");
// set the read geometry
setPoint(Base::Vector3d(X,Y,Z) );
}
PyObject *GeomPoint::getPyObject(void)
{
return new Base::VectorPy(getPoint());
}
// -------------------------------------------------
TYPESYSTEM_SOURCE_ABSTRACT(Part::GeomCurve,Part::Geometry);
GeomCurve::GeomCurve()
{
}
GeomCurve::~GeomCurve()
{
}
TopoDS_Shape GeomCurve::toShape() const
{
Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(handle());
BRepBuilderAPI_MakeEdge mkBuilder(c, c->FirstParameter(), c->LastParameter());
return mkBuilder.Shape();
}
bool GeomCurve::tangent(double u, gp_Dir& dir) const
{
Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(handle());
GeomLProp_CLProps prop(c,u,1,Precision::Confusion());
if (prop.IsTangentDefined()) {
prop.Tangent(dir);
return true;
}
return false;
}
Base::Vector3d GeomCurve::pointAtParameter(double u) const
{
Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(handle());
GeomLProp_CLProps prop(c,u,0,Precision::Confusion());
const gp_Pnt &point=prop.Value();
return Base::Vector3d(point.X(),point.Y(),point.Z());
}
Base::Vector3d GeomCurve::firstDerivativeAtParameter(double u) const
{
Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(handle());
GeomLProp_CLProps prop(c,u,1,Precision::Confusion());
const gp_Vec &vec=prop.D1();
return Base::Vector3d(vec.X(),vec.Y(),vec.Z());
}
Base::Vector3d GeomCurve::secondDerivativeAtParameter(double u) const
{
Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(handle());
GeomLProp_CLProps prop(c,u,2,Precision::Confusion());
const gp_Vec &vec=prop.D2();
return Base::Vector3d(vec.X(),vec.Y(),vec.Z());
}
bool GeomCurve::normal(double u, gp_Dir& dir) const
{
Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(handle());
GeomLProp_CLProps prop(c,u,1,Precision::Confusion());
if (prop.IsTangentDefined()) {
prop.Normal(dir);
return true;
}
return false;
}
bool GeomCurve::closestParameter(const Base::Vector3d& point, double &u) const
{
Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(handle());
try {
if (!c.IsNull()) {
gp_Pnt pnt(point.x,point.y,point.z);
GeomAPI_ProjectPointOnCurve ppc(pnt, c);
u = ppc.LowerDistanceParameter();
return true;
}
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
std::cout << e->GetMessageString() << std::endl;
return false;
}
return false;
}
bool GeomCurve::closestParameterToBasicCurve(const Base::Vector3d& point, double &u) const
{
Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(handle());
if (c->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))){
Handle_Geom_TrimmedCurve tc = Handle_Geom_TrimmedCurve::DownCast(handle());
Handle_Geom_Curve bc = Handle_Geom_Curve::DownCast(tc->BasisCurve());
try {
if (!bc.IsNull()) {
gp_Pnt pnt(point.x,point.y,point.z);
GeomAPI_ProjectPointOnCurve ppc(pnt, bc);
u = ppc.LowerDistanceParameter();
return true;
}
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
std::cout << e->GetMessageString() << std::endl;
return false;
}
return false;
}
else {
return this->closestParameter(point, u);
}
}
// -------------------------------------------------
TYPESYSTEM_SOURCE(Part::GeomBezierCurve,Part::GeomCurve);
GeomBezierCurve::GeomBezierCurve()
{
TColgp_Array1OfPnt poles(1,2);
poles(1) = gp_Pnt(0.0,0.0,0.0);
poles(2) = gp_Pnt(0.0,0.0,1.0);
Handle_Geom_BezierCurve b = new Geom_BezierCurve(poles);
this->myCurve = b;
}
GeomBezierCurve::GeomBezierCurve(const Handle_Geom_BezierCurve& b)
{
this->myCurve = Handle_Geom_BezierCurve::DownCast(b->Copy());
}
GeomBezierCurve::~GeomBezierCurve()
{
}
void GeomBezierCurve::setHandle(const Handle_Geom_BezierCurve& c)
{
myCurve = Handle_Geom_BezierCurve::DownCast(c->Copy());
}
const Handle_Geom_Geometry& GeomBezierCurve::handle() const
{
return myCurve;
}
Geometry *GeomBezierCurve::clone(void) const
{
GeomBezierCurve *newCurve = new GeomBezierCurve(myCurve);
newCurve->Construction = this->Construction;
return newCurve;
}
// Persistence implementer
unsigned int GeomBezierCurve::getMemSize (void) const {assert(0); return 0;/* not implemented yet */}
void GeomBezierCurve::Save (Base::Writer &/*writer*/) const {assert(0); /* not implemented yet */}
void GeomBezierCurve::Restore (Base::XMLReader &/*reader*/) {assert(0); /* not implemented yet */}
PyObject *GeomBezierCurve::getPyObject(void)
{
return new BezierCurvePy((GeomBezierCurve*)this->clone());
}
// -------------------------------------------------
TYPESYSTEM_SOURCE(Part::GeomHermiteCurve,Part::GeomCurve);
GeomHermiteCurve::GeomHermiteCurve()
{
std::vector<gp_Pnt> p;
p.push_back(gp_Pnt(0.0,0.0,0.0));
p.push_back(gp_Pnt(1.0,0.0,0.0));
std::vector<gp_Vec> t;
t.push_back(gp_Vec(1,0,0));
t.push_back(gp_Vec(1,0,0));
compute(p, t);
}
GeomHermiteCurve::GeomHermiteCurve(const std::vector<gp_Pnt>& p,
const std::vector<gp_Vec>& t)
{
compute(p, t);
}
GeomHermiteCurve::~GeomHermiteCurve()
{
}
void GeomHermiteCurve::interpolate(const std::vector<gp_Pnt>& p,
const std::vector<gp_Vec>& t)
{
if (p.size() < 2)
Standard_ConstructionError::Raise();
if (p.size() != t.size())
Standard_ConstructionError::Raise();
compute(p, t);
}
void GeomHermiteCurve::getCardinalSplineTangents(const std::vector<gp_Pnt>& p,
const std::vector<double>& c,
std::vector<gp_Vec>& t) const
{
// https://de.wikipedia.org/wiki/Kubisch_Hermitescher_Spline#Cardinal_Spline
if (p.size() < 2)
Standard_ConstructionError::Raise();
if (p.size() != c.size())
Standard_ConstructionError::Raise();
t.resize(p.size());
if (p.size() == 2) {
t[0] = gp_Vec(p[0], p[1]);
t[1] = gp_Vec(p[0], p[1]);
}
else {
std::size_t e = p.size() - 1;
for (std::size_t i = 1; i < e; i++) {
gp_Vec v = gp_Vec(p[i-1], p[i+1]);
double f = 0.5 * (1-c[i]);
v.Scale(f);
t[i] = v;
}
t[0] = t[1];
t[t.size()-1] = t[t.size()-2];
}
}
void GeomHermiteCurve::getCardinalSplineTangents(const std::vector<gp_Pnt>& p, double c,
std::vector<gp_Vec>& t) const
{
// https://de.wikipedia.org/wiki/Kubisch_Hermitescher_Spline#Cardinal_Spline
if (p.size() < 2)
Standard_ConstructionError::Raise();
t.resize(p.size());
if (p.size() == 2) {
t[0] = gp_Vec(p[0], p[1]);
t[1] = gp_Vec(p[0], p[1]);
}
else {
std::size_t e = p.size() - 1;
double f = 0.5 * (1-c);
for (std::size_t i = 1; i < e; i++) {
gp_Vec v = gp_Vec(p[i-1], p[i+1]);
v.Scale(f);
t[i] = v;
}
t[0] = t[1];
t[t.size()-1] = t[t.size()-2];
}
}
const Handle_Geom_Geometry& GeomHermiteCurve::handle() const
{
return myCurve;
}
void GeomHermiteCurve::compute(const std::vector<gp_Pnt>& p,
const std::vector<gp_Vec>& t)
{
double tol3d = Precision::Approximation();
Handle_TColgp_HArray1OfPnt pts = new TColgp_HArray1OfPnt(1, p.size());
for (std::size_t i=0; i<p.size(); i++) {
pts->SetValue(i+1, p[i]);
}
TColgp_Array1OfVec tgs(1, t.size());
Handle_TColStd_HArray1OfBoolean fgs = new TColStd_HArray1OfBoolean(1, t.size());
for (std::size_t i=0; i<p.size(); i++) {
tgs.SetValue(i+1, t[i]);
fgs->SetValue(i+1, Standard_True);
}
GeomAPI_Interpolate interpolate(pts, Standard_False, tol3d);
interpolate.Load(tgs, fgs);
interpolate.Perform();
this->myCurve = interpolate.Curve();
this->poles = p;
this->tangents = t;
}
Geometry *GeomHermiteCurve::clone(void) const
{
GeomHermiteCurve *newCurve = new GeomHermiteCurve(poles, tangents);
newCurve->Construction = this->Construction;
return newCurve;
}
// Persistence implementer
unsigned int GeomHermiteCurve::getMemSize (void) const {assert(0); return 0;/* not implemented yet */}
void GeomHermiteCurve::Save (Base::Writer &/*writer*/) const {assert(0); /* not implemented yet */}
void GeomHermiteCurve::Restore (Base::XMLReader &/*reader*/) {assert(0); /* not implemented yet */}
PyObject *GeomHermiteCurve::getPyObject(void)
{
return new HermiteCurvePy(static_cast<GeomHermiteCurve*>(this->clone()));
}
// -------------------------------------------------
TYPESYSTEM_SOURCE(Part::GeomBSplineCurve,Part::GeomCurve);
GeomBSplineCurve::GeomBSplineCurve()
{
TColgp_Array1OfPnt poles(1,2);
poles(1) = gp_Pnt(0.0,0.0,0.0);
poles(2) = gp_Pnt(1.0,0.0,0.0);
TColStd_Array1OfReal knots(1,2);
knots(1) = 0.0;
knots(2) = 1.0;
TColStd_Array1OfInteger mults(1,2);
mults(1) = 2;
mults(2) = 2;
this->myCurve = new Geom_BSplineCurve(poles, knots, mults, 1);
}
GeomBSplineCurve::GeomBSplineCurve(const Handle_Geom_BSplineCurve& b)
{
this->myCurve = Handle_Geom_BSplineCurve::DownCast(b->Copy());
}
GeomBSplineCurve::~GeomBSplineCurve()
{
}
void GeomBSplineCurve::setHandle(const Handle_Geom_BSplineCurve& c)
{
myCurve = Handle_Geom_BSplineCurve::DownCast(c->Copy());
}
const Handle_Geom_Geometry& GeomBSplineCurve::handle() const
{
return myCurve;
}
Geometry *GeomBSplineCurve::clone(void) const
{
GeomBSplineCurve *newCurve = new GeomBSplineCurve(myCurve);
newCurve->Construction = this->Construction;
return newCurve;
}
int GeomBSplineCurve::countPoles() const
{
return myCurve->NbPoles();
}
void GeomBSplineCurve::setPole(int index, const Base::Vector3d& pole, double weight)
{
try {
gp_Pnt pnt(pole.x,pole.y,pole.z);
if (weight < 0.0)
myCurve->SetPole(index+1,pnt);
else
myCurve->SetPole(index+1,pnt,weight);
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
std::cout << e->GetMessageString() << std::endl;
}
}
std::vector<Base::Vector3d> GeomBSplineCurve::getPoles() const
{
std::vector<Base::Vector3d> poles;
poles.reserve(myCurve->NbPoles());
TColgp_Array1OfPnt p(1,myCurve->NbPoles());
myCurve->Poles(p);
for (Standard_Integer i=p.Lower(); i<=p.Upper(); i++) {
const gp_Pnt& pnt = p(i);
poles.push_back(Base::Vector3d(pnt.X(), pnt.Y(), pnt.Z()));
}
return poles;
}
bool GeomBSplineCurve::join(const Handle_Geom_BSplineCurve& spline)
{
GeomConvert_CompCurveToBSplineCurve ccbc(this->myCurve);
if (!ccbc.Add(spline, Precision::Approximation()))
return false;
this->myCurve = ccbc.BSplineCurve();
return true;
}
void GeomBSplineCurve::makeC1Continuous(double tol, double ang_tol)
{
GeomConvert::C0BSplineToC1BSplineCurve(this->myCurve, tol, ang_tol);
}
// Persistence implementer
unsigned int GeomBSplineCurve::getMemSize (void) const {assert(0); return 0;/* not implemented yet */}
void GeomBSplineCurve::Save (Base::Writer &/*writer*/) const {assert(0); /* not implemented yet */}
void GeomBSplineCurve::Restore (Base::XMLReader &/*reader*/) {assert(0); /* not implemented yet */}
PyObject *GeomBSplineCurve::getPyObject(void)
{
return new BSplineCurvePy((GeomBSplineCurve*)this->clone());
}
// -------------------------------------------------
TYPESYSTEM_SOURCE(Part::GeomCircle,Part::GeomCurve);
GeomCircle::GeomCircle()
{
Handle_Geom_Circle c = new Geom_Circle(gp_Circ());
this->myCurve = c;
}
GeomCircle::GeomCircle(const Handle_Geom_Circle& c)
{
this->myCurve = Handle_Geom_Circle::DownCast(c->Copy());
}
GeomCircle::~GeomCircle()
{
}
const Handle_Geom_Geometry& GeomCircle::handle() const
{
return myCurve;
}
Geometry *GeomCircle::clone(void) const
{
GeomCircle *newCirc = new GeomCircle(myCurve);
newCirc->Construction = this->Construction;
return newCirc;
}
Base::Vector3d GeomCircle::getCenter(void) const
{
Handle_Geom_Circle circle = Handle_Geom_Circle::DownCast(handle());
gp_Ax1 axis = circle->Axis();
const gp_Pnt& loc = axis.Location();
return Base::Vector3d(loc.X(),loc.Y(),loc.Z());
}
double GeomCircle::getRadius(void) const
{
Handle_Geom_Circle circle = Handle_Geom_Circle::DownCast(handle());
return circle->Radius();
}
void GeomCircle::setCenter(const Base::Vector3d& Center)
{
gp_Pnt p1(Center.x,Center.y,Center.z);
Handle_Geom_Circle circle = Handle_Geom_Circle::DownCast(handle());
try {
circle->SetLocation(p1);
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
throw Base::Exception(e->GetMessageString());
}
}
void GeomCircle::setRadius(double Radius)
{
Handle_Geom_Circle circle = Handle_Geom_Circle::DownCast(handle());
try {
gp_Circ c = circle->Circ();
c.SetRadius(Radius);
circle->SetCirc(c);
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
throw Base::Exception(e->GetMessageString());
}
}
bool GeomCircle::isReversed() const
{
Handle_Geom_Circle c = myCurve;
assert(!c.IsNull());
return c->Axis().Direction().Z() < 0;
}
// Persistence implementer
unsigned int GeomCircle::getMemSize (void) const
{
return sizeof(Geom_Circle);
}
void GeomCircle::Save(Base::Writer& writer) const
{
// save the attributes of the father class
GeomCurve::Save(writer);
gp_Pnt center = this->myCurve->Axis().Location();
gp_Dir norm = this->myCurve->Axis().Direction();
writer.Stream()
<< writer.ind()
<< "<Circle "
<< "CenterX=\"" << center.X() <<
"\" CenterY=\"" << center.Y() <<
"\" CenterZ=\"" << center.Z() <<
"\" NormalX=\"" << norm.X() <<
"\" NormalY=\"" << norm.Y() <<
"\" NormalZ=\"" << norm.Z() <<
"\" Radius=\"" << this->myCurve->Radius() <<
"\"/>" << endl;
}
void GeomCircle::Restore(Base::XMLReader& reader)
{
// read the attributes of the father class
GeomCurve::Restore(reader);
double CenterX,CenterY,CenterZ,NormalX,NormalY,NormalZ,Radius;
// read my Element
reader.readElement("Circle");
// get the value of my Attribute
CenterX = reader.getAttributeAsFloat("CenterX");
CenterY = reader.getAttributeAsFloat("CenterY");
CenterZ = reader.getAttributeAsFloat("CenterZ");
NormalX = reader.getAttributeAsFloat("NormalX");
NormalY = reader.getAttributeAsFloat("NormalY");
NormalZ = reader.getAttributeAsFloat("NormalZ");
Radius = reader.getAttributeAsFloat("Radius");
// set the read geometry
gp_Pnt p1(CenterX,CenterY,CenterZ);
gp_Dir norm(NormalX,NormalY,NormalZ);
try {
GC_MakeCircle mc(p1, norm, Radius);
if (!mc.IsDone())
throw Base::Exception(gce_ErrorStatusText(mc.Status()));
this->myCurve = mc.Value();
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
throw Base::Exception(e->GetMessageString());
}
}
PyObject *GeomCircle::getPyObject(void)
{
return new CirclePy((GeomCircle*)this->clone());
}
// -------------------------------------------------
TYPESYSTEM_SOURCE(Part::GeomArcOfCircle,Part::GeomCurve);
GeomArcOfCircle::GeomArcOfCircle()
{
Handle_Geom_Circle c = new Geom_Circle(gp_Circ());
this->myCurve = new Geom_TrimmedCurve(c, c->FirstParameter(),c->LastParameter());
}
GeomArcOfCircle::GeomArcOfCircle(const Handle_Geom_Circle& c)
{
this->myCurve = new Geom_TrimmedCurve(c, c->FirstParameter(),c->LastParameter());
}
GeomArcOfCircle::~GeomArcOfCircle()
{
}
void GeomArcOfCircle::setHandle(const Handle_Geom_TrimmedCurve& c)
{
Handle_Geom_Circle basis = Handle_Geom_Circle::DownCast(c->BasisCurve());
if (basis.IsNull())
Standard_Failure::Raise("Basis curve is not a circle");
this->myCurve = Handle_Geom_TrimmedCurve::DownCast(c->Copy());
}
const Handle_Geom_Geometry& GeomArcOfCircle::handle() const
{
return myCurve;
}
Geometry *GeomArcOfCircle::clone(void) const
{
GeomArcOfCircle* copy = new GeomArcOfCircle();
copy->setHandle(this->myCurve);
copy->Construction = this->Construction;
return copy;
}
/*!
* \brief GeomArcOfCircle::getStartPoint
* \param emulateCCWXY: if true, the arc will pretent to be a CCW arc in XY plane.
* For this to work, the arc must lie in XY plane (i.e. Axis is either +Z or -Z).
* \return XYZ of the arc's starting point.
*/
Base::Vector3d GeomArcOfCircle::getStartPoint(bool emulateCCWXY) const
{
gp_Pnt pnt = this->myCurve->StartPoint();
if(emulateCCWXY)
if(isReversedInXY())
pnt = this->myCurve->EndPoint();
return Base::Vector3d(pnt.X(), pnt.Y(), pnt.Z());
}
/*!
* \brief GeomArcOfCircle::getEndPoint
* \param emulateCCWXY: if true, the arc will pretent to be a CCW arc in XY plane.
* For this to work, the arc must lie in XY plane (i.e. Axis is either +Z or -Z).
* \return
*/
Base::Vector3d GeomArcOfCircle::getEndPoint(bool emulateCCWXY) const
{
gp_Pnt pnt = this->myCurve->EndPoint();
if(emulateCCWXY)
if(isReversedInXY())
pnt = this->myCurve->StartPoint();
return Base::Vector3d(pnt.X(), pnt.Y(), pnt.Z());
}
Base::Vector3d GeomArcOfCircle::getCenter(void) const
{
Handle_Geom_Circle circle = Handle_Geom_Circle::DownCast(myCurve->BasisCurve());
gp_Ax1 axis = circle->Axis();
const gp_Pnt& loc = axis.Location();
return Base::Vector3d(loc.X(),loc.Y(),loc.Z());
}
double GeomArcOfCircle::getRadius(void) const
{
Handle_Geom_Circle circle = Handle_Geom_Circle::DownCast(myCurve->BasisCurve());
return circle->Radius();
}
void GeomArcOfCircle::setCenter(const Base::Vector3d& Center)
{
gp_Pnt p1(Center.x,Center.y,Center.z);
Handle_Geom_Circle circle = Handle_Geom_Circle::DownCast(myCurve->BasisCurve());
try {
circle->SetLocation(p1);
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
throw Base::Exception(e->GetMessageString());
}
}
void GeomArcOfCircle::setRadius(double Radius)
{
Handle_Geom_Circle circle = Handle_Geom_Circle::DownCast(myCurve->BasisCurve());
try {
gp_Circ c = circle->Circ();
c.SetRadius(Radius);
circle->SetCirc(c);
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
throw Base::Exception(e->GetMessageString());
}
}
/*!
* \brief GeomArcOfCircle::getRange
* \param u [out] start angle of the arc, in radians.
* \param v [out] end angle of the arc, in radians.
* \param emulateCCWXY: if true, the arc will pretent to be a CCW arc in XY plane.
* For this to work, the arc must lie in XY plane (i.e. Axis is either +Z or -Z).
* Additionally, arc's rotation as a whole will be included in the returned u,v
* (ArcOfCircle specific).
*/
void GeomArcOfCircle::getRange(double& u, double& v, bool emulateCCWXY) const
{
u = myCurve->FirstParameter();
v = myCurve->LastParameter();
if(emulateCCWXY){
Handle_Geom_Circle cir = Handle_Geom_Circle::DownCast(myCurve->BasisCurve());
double angleXU = -cir->Position().XDirection().AngleWithRef(gp_Dir(1.0,0.0,0.0), gp_Dir(0.0,0.0,1.0));
double u1 = u, v1 = v;//the true arc curve parameters, cached. u,v will contain the rotation-corrected and swapped angles.
if(cir->Axis().Direction().Z() > 0.0){
//normal CCW arc
u = u1 + angleXU;
v = v1 + angleXU;