/
DrawingExport.cpp
831 lines (745 loc) · 26.8 KB
/
DrawingExport.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
/***************************************************************************
* Copyright (c) 2011 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 <sstream>
# include <cmath>
# include <BRepAdaptor_Curve.hxx>
# include <Geom_Circle.hxx>
# include <gp_Circ.hxx>
# include <gp_Elips.hxx>
#endif
#include <Bnd_Box.hxx>
#include <BRepBndLib.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <HLRBRep_Algo.hxx>
#include <TopoDS_Shape.hxx>
#include <HLRTopoBRep_OutLiner.hxx>
//#include <BRepAPI_MakeOutLine.hxx>
#include <HLRAlgo_Projector.hxx>
#include <HLRBRep_ShapeBounds.hxx>
#include <HLRBRep_HLRToShape.hxx>
#include <gp_Ax2.hxx>
#include <gp_Pnt.hxx>
#include <gp_Dir.hxx>
#include <gp_Vec.hxx>
#include <Poly_Polygon3D.hxx>
#include <Poly_Triangulation.hxx>
#include <Poly_PolygonOnTriangulation.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>
#include <TopTools_IndexedMapOfShape.hxx>
#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <TopTools_ListOfShape.hxx>
#include <TColgp_Array1OfPnt2d.hxx>
#include <BRep_Tool.hxx>
#include <BRepAdaptor_CompCurve.hxx>
#include <Handle_BRepAdaptor_HCompCurve.hxx>
#include <Approx_Curve3d.hxx>
#include <BRepAdaptor_HCurve.hxx>
#include <Handle_BRepAdaptor_HCurve.hxx>
#include <Geom_BSplineCurve.hxx>
#include <Handle_Geom_BSplineCurve.hxx>
#include <Geom_BezierCurve.hxx>
#include <GeomConvert_BSplineCurveToBezierCurve.hxx>
#include <GeomConvert_BSplineCurveKnotSplitting.hxx>
#include <Geom2d_BSplineCurve.hxx>
#include <BRepLProp_CLProps.hxx>
#include "DrawingExport.h"
#include <Base/Tools.h>
#include <Base/Vector3D.h>
using namespace Drawing;
TopoDS_Edge DrawingOutput::asCircle(const BRepAdaptor_Curve& c) const
{
double curv=0;
gp_Pnt pnt, center;
// approximate the circle center from three positions
BRepLProp_CLProps prop(c,c.FirstParameter(),2,Precision::Confusion());
curv += prop.Curvature();
prop.CentreOfCurvature(pnt);
center.ChangeCoord().Add(pnt.Coord());
prop.SetParameter(0.5*(c.FirstParameter()+c.LastParameter()));
curv += prop.Curvature();
prop.CentreOfCurvature(pnt);
center.ChangeCoord().Add(pnt.Coord());
prop.SetParameter(c.LastParameter());
curv += prop.Curvature();
prop.CentreOfCurvature(pnt);
center.ChangeCoord().Add(pnt.Coord());
center.ChangeCoord().Divide(3);
curv /= 3;
// get circle from curvature information
double radius = 1 / curv;
TopLoc_Location location;
Handle(Poly_Polygon3D) polygon = BRep_Tool::Polygon3D(c.Edge(), location);
if (!polygon.IsNull()) {
const TColgp_Array1OfPnt& nodes = polygon->Nodes();
for (int i = nodes.Lower(); i <= nodes.Upper(); i++) {
gp_Pnt p = nodes(i);
double dist = p.Distance(center);
if (std::abs(dist - radius) > 0.001)
return TopoDS_Edge();
}
gp_Circ circ;
circ.SetLocation(center);
circ.SetRadius(radius);
gp_Pnt p1 = nodes(nodes.Lower());
gp_Pnt p2 = nodes(nodes.Upper());
double dist = p1.Distance(p2);
if (dist < Precision::Confusion()) {
BRepBuilderAPI_MakeEdge mkEdge(circ);
return mkEdge.Edge();
}
else {
gp_Vec dir1(center, p1);
dir1.Normalize();
gp_Vec dir2(center, p2);
dir2.Normalize();
p1 = gp_Pnt(center.XYZ() + radius * dir1.XYZ());
p2 = gp_Pnt(center.XYZ() + radius * dir2.XYZ());
BRepBuilderAPI_MakeEdge mkEdge(circ, p1, p2);
return mkEdge.Edge();
}
}
return TopoDS_Edge();
}
TopoDS_Edge DrawingOutput::asBSpline(const BRepAdaptor_Curve& c, int maxDegree) const
{
Standard_Real tol3D = 0.001;
Standard_Integer maxSegment = 10;
Handle_BRepAdaptor_HCurve hCurve = new BRepAdaptor_HCurve(c);
// approximate the curve using a tolerance
Approx_Curve3d approx(hCurve,tol3D,GeomAbs_C0,maxSegment,maxDegree);
if (approx.IsDone() && approx.HasResult()) {
// have the result
Handle_Geom_BSplineCurve spline = approx.Curve();
BRepBuilderAPI_MakeEdge mkEdge(spline, spline->FirstParameter(), spline->LastParameter());
return mkEdge.Edge();
}
return TopoDS_Edge();
}
SVGOutput::SVGOutput()
{
}
std::string SVGOutput::exportEdges(const TopoDS_Shape& input)
{
std::stringstream result;
TopExp_Explorer edges(input, TopAbs_EDGE);
for (int i = 1 ; edges.More(); edges.Next(),i++) {
const TopoDS_Edge& edge = TopoDS::Edge(edges.Current());
BRepAdaptor_Curve adapt(edge);
if (adapt.GetType() == GeomAbs_Circle) {
printCircle(adapt, result);
}
else if (adapt.GetType() == GeomAbs_Ellipse) {
printEllipse(adapt, i, result);
}
else if (adapt.GetType() == GeomAbs_BSplineCurve) {
TopoDS_Edge circle = asCircle(adapt);
if (circle.IsNull()) {
printBSpline(adapt, i, result);
}
else {
BRepAdaptor_Curve adapt_circle(circle);
printCircle(adapt_circle, result);
}
}
else if (adapt.GetType() == GeomAbs_BezierCurve) {
printBezier(adapt, i, result);
}
// fallback
else {
printGeneric(adapt, i, result);
}
}
return result.str();
}
void SVGOutput::printCircle(const BRepAdaptor_Curve& c, std::ostream& out)
{
gp_Circ circ = c.Circle();
const gp_Pnt& p= circ.Location();
double r = circ.Radius();
double f = c.FirstParameter();
double l = c.LastParameter();
gp_Pnt s = c.Value(f);
gp_Pnt m = c.Value((l+f)/2.0);
gp_Pnt e = c.Value(l);
gp_Vec v1(m,s);
gp_Vec v2(m,e);
gp_Vec v3(0,0,1);
double a = v3.DotCross(v1,v2);
// a full circle
if (fabs(l-f) > 1.0 && s.SquareDistance(e) < 0.001) {
out << "<circle cx =\"" << p.X() << "\" cy =\""
<< p.Y() << "\" r =\"" << r << "\" />";
}
// arc of circle
else {
// See also https://developer.mozilla.org/en/SVG/Tutorial/Paths
char xar = '0'; // x-axis-rotation
char las = (l-f > D_PI) ? '1' : '0'; // large-arc-flag
char swp = (a < 0) ? '1' : '0'; // sweep-flag, i.e. clockwise (0) or counter-clockwise (1)
out << "<path d=\"M" << s.X() << " " << s.Y()
<< " A" << r << " " << r << " "
<< xar << " " << las << " " << swp << " "
<< e.X() << " " << e.Y() << "\" />";
}
}
void SVGOutput::printEllipse(const BRepAdaptor_Curve& c, int id, std::ostream& out)
{
gp_Elips ellp = c.Ellipse();
const gp_Pnt& p= ellp.Location();
double r1 = ellp.MajorRadius();
double r2 = ellp.MinorRadius();
double f = c.FirstParameter();
double l = c.LastParameter();
gp_Pnt s = c.Value(f);
gp_Pnt m = c.Value((l+f)/2.0);
gp_Pnt e = c.Value(l);
gp_Vec v1(m,s);
gp_Vec v2(m,e);
gp_Vec v3(0,0,1);
double a = v3.DotCross(v1,v2);
// a full ellipse
// See also https://developer.mozilla.org/en/SVG/Tutorial/Paths
gp_Dir xaxis = ellp.XAxis().Direction();
Standard_Real angle = xaxis.AngleWithRef(gp_Dir(1,0,0),gp_Dir(0,0,-1));
angle = Base::toDegrees<double>(angle);
if (fabs(l-f) > 1.0 && s.SquareDistance(e) < 0.001) {
out << "<g transform = \"rotate(" << angle << "," << p.X() << "," << p.Y() << ")\">" << std::endl;
out << "<ellipse cx =\"" << p.X() << "\" cy =\""
<< p.Y() << "\" rx =\"" << r1 << "\" ry =\"" << r2 << "\"/>" << std::endl;
out << "</g>" << std::endl;
}
// arc of ellipse
else {
char las = (l-f > D_PI) ? '1' : '0'; // large-arc-flag
char swp = (a < 0) ? '1' : '0'; // sweep-flag, i.e. clockwise (0) or counter-clockwise (1)
out << "<path d=\"M" << s.X() << " " << s.Y()
<< " A" << r1 << " " << r2 << " "
<< angle << " " << las << " " << swp << " "
<< e.X() << " " << e.Y() << "\" />" << std::endl;
}
}
void SVGOutput::printBezier(const BRepAdaptor_Curve& c, int id, std::ostream& out)
{
try {
std::stringstream str;
str << "<path d=\"M";
Handle_Geom_BezierCurve bezier = c.Bezier();
Standard_Integer poles = bezier->NbPoles();
// if it's a bezier with degree higher than 3 convert it into a B-spline
if (bezier->Degree() > 3 || bezier->IsRational()) {
TopoDS_Edge edge = asBSpline(c, 3);
if (!edge.IsNull()) {
BRepAdaptor_Curve spline(edge);
printBSpline(spline, id, out);
}
else {
Standard_Failure::Raise("do it the generic way");
}
return;
}
gp_Pnt p1 = bezier->Pole(1);
str << p1.X() << "," << p1.Y();
if (bezier->Degree() == 3) {
if (poles != 4)
Standard_Failure::Raise("do it the generic way");
gp_Pnt p2 = bezier->Pole(2);
gp_Pnt p3 = bezier->Pole(3);
gp_Pnt p4 = bezier->Pole(4);
str << " C"
<< p2.X() << "," << p2.Y() << " "
<< p3.X() << "," << p3.Y() << " "
<< p4.X() << "," << p4.Y() << " ";
}
else if (bezier->Degree() == 2) {
if (poles != 3)
Standard_Failure::Raise("do it the generic way");
gp_Pnt p2 = bezier->Pole(2);
gp_Pnt p3 = bezier->Pole(3);
str << " Q"
<< p2.X() << "," << p2.Y() << " "
<< p3.X() << "," << p3.Y() << " ";
}
else if (bezier->Degree() == 1) {
if (poles != 2)
Standard_Failure::Raise("do it the generic way");
gp_Pnt p2 = bezier->Pole(2);
str << " L" << p2.X() << "," << p2.Y() << " ";
}
else {
Standard_Failure::Raise("do it the generic way");
}
str << "\" />";
out << str.str();
}
catch (Standard_Failure) {
printGeneric(c, id, out);
}
}
void SVGOutput::printBSpline(const BRepAdaptor_Curve& c, int id, std::ostream& out)
{
try {
std::stringstream str;
Handle_Geom_BSplineCurve spline = c.BSpline();
if (spline->Degree() > 3 || spline->IsRational()) {
Standard_Real tol3D = 0.001;
Standard_Integer maxDegree = 3, maxSegment = 10;
Handle_BRepAdaptor_HCurve hCurve = new BRepAdaptor_HCurve(c);
// approximate the curve using a tolerance
Approx_Curve3d approx(hCurve,tol3D,GeomAbs_C0,maxSegment,maxDegree);
if (approx.IsDone() && approx.HasResult()) {
// have the result
spline = approx.Curve();
}
}
GeomConvert_BSplineCurveToBezierCurve crt(spline);
Standard_Integer arcs = crt.NbArcs();
str << "<path d=\"M";
for (Standard_Integer i=1; i<=arcs; i++) {
Handle_Geom_BezierCurve bezier = crt.Arc(i);
Standard_Integer poles = bezier->NbPoles();
if (i == 1) {
gp_Pnt p1 = bezier->Pole(1);
str << p1.X() << "," << p1.Y();
}
if (bezier->Degree() == 3) {
if (poles != 4)
Standard_Failure::Raise("do it the generic way");
gp_Pnt p2 = bezier->Pole(2);
gp_Pnt p3 = bezier->Pole(3);
gp_Pnt p4 = bezier->Pole(4);
str << " C"
<< p2.X() << "," << p2.Y() << " "
<< p3.X() << "," << p3.Y() << " "
<< p4.X() << "," << p4.Y() << " ";
}
else if (bezier->Degree() == 2) {
if (poles != 3)
Standard_Failure::Raise("do it the generic way");
gp_Pnt p2 = bezier->Pole(2);
gp_Pnt p3 = bezier->Pole(3);
str << " Q"
<< p2.X() << "," << p2.Y() << " "
<< p3.X() << "," << p3.Y() << " ";
}
else if (bezier->Degree() == 1) {
if (poles != 2)
Standard_Failure::Raise("do it the generic way");
gp_Pnt p2 = bezier->Pole(2);
str << " L" << p2.X() << "," << p2.Y() << " ";
}
else {
Standard_Failure::Raise("do it the generic way");
}
}
str << "\" />";
out << str.str();
}
catch (Standard_Failure) {
printGeneric(c, id, out);
}
}
void SVGOutput::printGeneric(const BRepAdaptor_Curve& c, int id, std::ostream& out)
{
TopLoc_Location location;
Handle(Poly_Polygon3D) polygon = BRep_Tool::Polygon3D(c.Edge(), location);
if (!polygon.IsNull()) {
const TColgp_Array1OfPnt& nodes = polygon->Nodes();
char c = 'M';
out << "<path id= \"" /*<< ViewName*/ << id << "\" d=\" ";
for (int i = nodes.Lower(); i <= nodes.Upper(); i++){
out << c << " " << nodes(i).X() << " " << nodes(i).Y()<< " " ;
c = 'L';
}
out << "\" />" << endl;
}
}
// ----------------------------------------------------------------------------
DXFOutput::DXFOutput()
{
}
std::string DXFOutput::exportEdges(const TopoDS_Shape& input)
{
std::stringstream result;
TopExp_Explorer edges(input, TopAbs_EDGE);
for (int i = 1 ; edges.More(); edges.Next(),i++) {
const TopoDS_Edge& edge = TopoDS::Edge(edges.Current());
BRepAdaptor_Curve adapt(edge);
if (adapt.GetType() == GeomAbs_Circle) {
printCircle(adapt, result);
}
else if (adapt.GetType() == GeomAbs_Ellipse) {
printEllipse(adapt, i, result);
}
else if (adapt.GetType() == GeomAbs_BSplineCurve) {
printBSpline(adapt, i, result);
}
// fallback
else {
printGeneric(adapt, i, result);
}
}
return result.str();
}
void DXFOutput::printHeader( std::ostream& out)
{
out << 0 << endl;
out << "SECTION" << endl;
out << 2 << endl;
out << "ENTITIES" << endl;
}
void DXFOutput::printCircle(const BRepAdaptor_Curve& c, std::ostream& out)
{
gp_Circ circ = c.Circle();
//const gp_Ax1& axis = c->Axis();
const gp_Pnt& p= circ.Location();
double r = circ.Radius();
double f = c.FirstParameter();
double l = c.LastParameter();
gp_Pnt s = c.Value(f);
gp_Pnt m = c.Value((l+f)/2.0);
gp_Pnt e = c.Value(l);
gp_Vec v1(m,s);
gp_Vec v2(m,e);
gp_Vec v3(0,0,1);
double a = v3.DotCross(v1,v2);
// a full circle
if (s.SquareDistance(e) < 0.001) {
//out << "<circle cx =\"" << p.X() << "\" cy =\""
//<< p.Y() << "\" r =\"" << r << "\" />";
out << 0 << endl;
out << "CIRCLE" << endl;
out << 8 << endl; // Group code for layer name
out << "sheet_layer" << endl; // Layer number
out << 10 << endl; // Centre X
out << p.X() << endl; // X in WCS coordinates
out << 20 << endl;
out << p.Y() << endl; // Y in WCS coordinates
out << 30 << endl;
out << 0 << endl; // Z in WCS coordinates-leaving flat
out << 40 << endl; //
out << r << endl; // Radius
}
// arc of circle
else {
// See also https://developer.mozilla.org/en/SVG/Tutorial/Paths
/*char xar = '0'; // x-axis-rotation
char las = (l-f > D_PI) ? '1' : '0'; // large-arc-flag
char swp = (a < 0) ? '1' : '0'; // sweep-flag, i.e. clockwise (0) or counter-clockwise (1)
out << "<path d=\"M" << s.X() << " " << s.Y()
<< " A" << r << " " << r << " "
<< xar << " " << las << " " << swp << " "
<< e.X() << " " << e.Y() << "\" />";*/
double ax = s.X() - p.X();
double ay = s.Y() - p.Y();
double bx = e.X() - p.X();
double by = e.Y() - p.Y();
double start_angle = atan2(ay, ax) * 180/D_PI;
double end_angle = atan2(by, bx) * 180/D_PI;
if(a > 0){
double temp = start_angle;
start_angle = end_angle;
end_angle = temp;}
out << 0 << endl;
out << "ARC" << endl;
out << 8 << endl; // Group code for layer name
out << "sheet_layer" << endl; // Layer number
out << 10 << endl; // Centre X
out << p.X() << endl; // X in WCS coordinates
out << 20 << endl;
out << p.Y() << endl; // Y in WCS coordinates
out << 30 << endl;
out << 0 << endl; // Z in WCS coordinates
out << 40 << endl; //
out << r << endl; // Radius
out << 50 << endl;
out << start_angle << endl; // Start angle
out << 51 << endl;
out << end_angle << endl; // End angle
}
}
void DXFOutput::printEllipse(const BRepAdaptor_Curve& c, int id, std::ostream& out)
{
gp_Elips ellp = c.Ellipse();
const gp_Pnt& p= ellp.Location();
double r1 = ellp.MajorRadius();
double r2 = ellp.MinorRadius();
double dp = ellp.Axis().Direction().Dot(gp_Vec(0,0,1));
// a full ellipse
/* if (s.SquareDistance(e) < 0.001) {
out << "<ellipse cx =\"" << p.X() << "\" cy =\""
<< p.Y() << "\" rx =\"" << r1 << "\" ry =\"" << r2 << "\"/>";
}
// arc of ellipse
else {
// See also https://developer.mozilla.org/en/SVG/Tutorial/Paths
gp_Dir xaxis = ellp.XAxis().Direction();
Standard_Real angle = xaxis.Angle(gp_Dir(1,0,0));
angle = Base::toDegrees<double>(angle);
char las = (l-f > D_PI) ? '1' : '0'; // large-arc-flag
char swp = (a < 0) ? '1' : '0'; // sweep-flag, i.e. clockwise (0) or counter-clockwise (1)
out << "<path d=\"M" << s.X() << " " << s.Y()
<< " A" << r1 << " " << r2 << " "
<< angle << " " << las << " " << swp << " "
<< e.X() << " " << e.Y() << "\" />";
}*/
gp_Dir xaxis = ellp.XAxis().Direction();
double angle = xaxis.AngleWithRef(gp_Dir(1,0,0),gp_Dir(0,0,-1));
//double rotation = Base::toDegrees<double>(angle);
double start_angle = c.FirstParameter();
double end_angle = c.LastParameter();
double major_x;double major_y;
major_x = r1 * cos(angle);
major_y = r1 * sin(angle);
double ratio = r2/r1;
if(dp < 0){
double temp = start_angle;
start_angle = end_angle;
end_angle = temp;
}
out << 0 << endl;
out << "ELLIPSE" << endl;
out << 8 << endl; // Group code for layer name
out << "sheet_layer" << endl; // Layer number
out << 10 << endl; // Centre X
out << p.X() << endl; // X in WCS coordinates
out << 20 << endl;
out << p.Y() << endl; // Y in WCS coordinates
out << 30 << endl;
out << 0 << endl; // Z in WCS coordinates
out << 11 << endl; //
out << major_x << endl; // Major X
out << 21 << endl;
out << major_y << endl; // Major Y
out << 31 << endl;
out << 0 << endl; // Major Z
out << 40 << endl; //
out << ratio << endl; // Ratio
out << 41 << endl;
out << start_angle << endl; // Start angle
out << 42 << endl;
out << end_angle << endl; // End angle
}
void DXFOutput::printBSpline(const BRepAdaptor_Curve& c, int id, std::ostream& out) //Not even close yet- DF
{
try {
std::stringstream str;
Handle_Geom_BSplineCurve spline = c.BSpline();
if (spline->Degree() > 3 || spline->IsRational()) {
Standard_Real tol3D = 0.001;
Standard_Integer maxDegree = 3, maxSegment = 10;
Handle_BRepAdaptor_HCurve hCurve = new BRepAdaptor_HCurve(c);
// approximate the curve using a tolerance
Approx_Curve3d approx(hCurve,tol3D,GeomAbs_C0,maxSegment,maxDegree);
if (approx.IsDone() && approx.HasResult()) {
// have the result
spline = approx.Curve();
}
}
GeomConvert_BSplineCurveToBezierCurve crt(spline);
//GeomConvert_BSplineCurveKnotSplitting crt(spline,0);
Standard_Integer arcs = crt.NbArcs();
//Standard_Integer arcs = crt.NbSplits()-1;
str << 0 << endl
<< "SECTION" << endl
<< 2 << endl
<< "ENTITIES" << endl
<< 0 << endl
<< "SPLINE" << endl;
//<< 8 << endl
//<< 0 << endl
//<< 66 << endl
//<< 1 << endl
//<< 0 << endl;
for (Standard_Integer i=1; i<=arcs; i++) {
Handle_Geom_BezierCurve bezier = crt.Arc(i);
Standard_Integer poles = bezier->NbPoles();
//Standard_Integer poles = bspline->NbPoles();
//gp_Pnt p1 = bspline->Pole(1);
if (bezier->Degree() == 3) {
if (poles != 4)
Standard_Failure::Raise("do it the generic way");
gp_Pnt p1 = bezier->Pole(1);
gp_Pnt p2 = bezier->Pole(2);
gp_Pnt p3 = bezier->Pole(3);
gp_Pnt p4 = bezier->Pole(4);
if (i == 1) {
str
<< 10 << endl
<< p1.X() << endl
<< 20 << endl
<< p1.Y() << endl
<< 30 << endl
<< 0 << endl
<< 10 << endl
<< p2.X() << endl
<< 20 << endl
<< p2.Y() << endl
<< 30 << endl
<< 0 << endl
<< 10 << endl
<< p3.X() << endl
<< 20 << endl
<< p3.Y() << endl
<< 30 << endl
<< 0 << endl
<< 10 << endl
<< p4.X() << endl
<< 20 << endl
<< p4.Y() << endl
<< 30 << endl
<< 0 << endl
<< 12 << endl
<< p1.X() << endl
<< 22 << endl
<< p1.Y() << endl
<< 32 << endl
<< 0 << endl
<< 13 << endl
<< p4.X() << endl
<< 23 << endl
<< p4.Y() << endl
<< 33 << endl
<< 0 << endl;
}
else {
str
<< 10 << endl
<< p3.X() << endl
<< 20 << endl
<< p3.Y() << endl
<< 30 << endl
<< 0 << endl
<< 10 << endl
<< p4.X() << endl
<< 20 << endl
<< p4.Y() << endl
<< 30 << endl
<< 0 << endl
<< 12 << endl
<< p3.X() << endl
<< 22 << endl
<< p3.Y() << endl
<< 32 << endl
<< 0 << endl
<< 13 << endl
<< p4.X() << endl
<< 23 << endl
<< p4.Y() << endl
<< 33 << endl
<< 0 << endl;
}
}
else if (bezier->Degree() == 2) {
if (poles != 3)
Standard_Failure::Raise("do it the generic way");
gp_Pnt p1 = bezier->Pole(1);
gp_Pnt p2 = bezier->Pole(2);
gp_Pnt p3 = bezier->Pole(3);
if (i == 1) {
str
<< 10 << endl
<< p1.X() << endl
<< 20 << endl
<< p1.Y() << endl
<< 30 << endl
<< 0 << endl
<< 10 << endl
<< p2.X() << endl
<< 20 << endl
<< p2.Y() << endl
<< 30 << endl
<< 0 << endl
<< 10 << endl
<< p3.X() << endl
<< 20 << endl
<< p3.Y() << endl
<< 30 << endl
<< 0 << endl
<< 12 << endl
<< p1.X() << endl
<< 22 << endl
<< p1.Y() << endl
<< 32 << endl
<< 0 << endl
<< 13 << endl
<< p3.X() << endl
<< 23 << endl
<< p3.Y() << endl
<< 33 << endl
<< 0 << endl;
}
else {
str
<< 10 << endl
<< p3.X() << endl
<< 20 << endl
<< p3.Y() << endl
<< 30 << endl
<< 0 << endl;
}
}
else {
Standard_Failure::Raise("do it the generic way");
}
}
//str << "\" />";
out << str.str();
}
catch (Standard_Failure) {
printGeneric(c, id, out);
}
}
void DXFOutput::printGeneric(const BRepAdaptor_Curve& c, int id, std::ostream& out)
{
double uStart = c.FirstParameter();
gp_Pnt PS;
gp_Vec VS;
c.D1(uStart, PS, VS);
double uEnd = c.LastParameter();
gp_Pnt PE;
gp_Vec VE;
c.D1(uEnd, PE, VE);
out << "0" << endl;
out << "LINE" << endl;
out << "8" << endl; // Group code for layer name
out << "sheet_layer" << endl; // Layer name
out << "10" << endl; // Start point of line
out << PS.X() << endl; // X in WCS coordinates
out << "20" << endl;
out << PS.Y() << endl; // Y in WCS coordinates
out << "30" << endl;
out << "0" << endl; // Z in WCS coordinates
out << "11" << endl; // End point of line
out << PE.X() << endl; // X in WCS coordinates
out << "21" << endl;
out << PE.Y() << endl; // Y in WCS coordinates
out << "31" << endl;
out << "0" << endl; // Z in WCS coordinates
}