FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
solvespace/src/exportvector.cpp at master · interfect/solvespace · GitHub
interfect
/
solvespace
Public
forked from
solvespace/solvespace
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
solvespace
/
src
/
exportvector.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1358 lines (1194 loc) · 45.8 KB
Breadcrumbs
solvespace
/
src
/
exportvector.cpp
Copy path
File metadata and controls
1358 lines (1194 loc) · 45.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
-----------------------------------------------------------------------------
//
The file format-specific stuff for all of the 2d vector output formats.
//
//
Copyright 2008-2013 Jonathan Westhues.
//
-----------------------------------------------------------------------------
#
include
<
libdxfrw.h
>
#
include
"
solvespace.h
"
//
-----------------------------------------------------------------------------
//
Routines for DXF export
//
-----------------------------------------------------------------------------
class
DxfWriteInterface
:
public
DRW_Interface
{
public:
DxfFileWriter *writer;
dxfRW *dxf;
std::set<std::string> messages;
static
DRW_Coord
toCoord
(
const
Vector &v) {
return
DRW_Coord
(v.
x
, v.
y
, v.
z
);
}
Vector
xfrm
(Vector v) {
return
writer->
Transform
(v);
}
void
writeTextstyles
()
override
{
DRW_Textstyle ts;
ts.
name
=
"
unicode
"
;
ts.
font
=
"
unicode
"
;
dxf->
writeTextstyle
(&ts);
}
void
writeLayers
()
override
{
DRW_Layer layer;
layer.
name
=
"
dimensions
"
;
dxf->
writeLayer
(&layer);
layer.
name
=
"
text
"
;
dxf->
writeLayer
(&layer);
std::set<
uint32_t
> usedStyles;
for
(DxfFileWriter::BezierPath &path : writer->
paths
) {
for
(SBezier *sb : path.
beziers
) {
usedStyles.
insert
((
uint32_t
)sb->
auxA
);
}
}
for
(
uint32_t
v : usedStyles) {
Style *s =
Style::Get
(hStyle{v});
layer.
name
= s->
DescriptionString
();
dxf->
writeLayer
(&layer);
}
}
void
writeLTypes
()
override
{
for
(
uint32_t
i =
0
; i <= (
uint32_t
)StipplePattern::
LAST
; i++) {
StipplePattern st = (StipplePattern)i;
DRW_LType type;
//
LibreCAD requires the line type to have one of these exact names,
//
or otherwise it overwrites it with its own (continuous) style.
type.
name
=
DxfFileWriter::lineTypeName
(st);
double
sw =
1.0
;
switch
(st) {
case
StipplePattern::
CONTINUOUS
:
break
;
case
StipplePattern::
SHORT_DASH
:
type.
path
.
push_back
(sw);
type.
path
.
push_back
(-sw *
2.0
);
break
;
case
StipplePattern::
DASH
:
type.
path
.
push_back
(sw);
type.
path
.
push_back
(-sw);
break
;
case
StipplePattern::
LONG_DASH
:
type.
path
.
push_back
(sw *
2.0
);
type.
path
.
push_back
(-sw);
break
;
case
StipplePattern::
DASH_DOT
:
type.
path
.
push_back
(sw);
type.
path
.
push_back
(-sw);
type.
path
.
push_back
(
0.0
);
type.
path
.
push_back
(-sw);
break
;
case
StipplePattern::
DOT
:
type.
path
.
push_back
(sw);
type.
path
.
push_back
(
0.0
);
break
;
case
StipplePattern::
DASH_DOT_DOT
:
type.
path
.
push_back
(sw);
type.
path
.
push_back
(-sw);
type.
path
.
push_back
(
0.0
);
type.
path
.
push_back
(-sw);
type.
path
.
push_back
(
0.0
);
type.
path
.
push_back
(-sw);
break
;
case
StipplePattern::
FREEHAND
:
case
StipplePattern::
ZIGZAG
:
//
Not implemented; exported as continuous.
break
;
}
dxf->
writeLineType
(&type);
}
}
void
writePolylines
() {
PolylineBuilder builder;
for
(DxfFileWriter::BezierPath &path : writer->
paths
) {
for
(SBezier *sb : path.
beziers
) {
if
(sb->
deg
!=
1
)
continue
;
builder.
AddEdge
(sb->
ctrl
[
0
], sb->
ctrl
[
1
], (
uint32_t
)sb->
auxA
);
}
}
DRW_Polyline polyline;
auto
startFunc = [&](PolylineBuilder::Vertex *start,
PolylineBuilder::Vertex *next,
PolylineBuilder::Edge *e) {
hStyle hs = { e->
kind
};
polyline = {};
assignEntityDefaults
(&polyline, hs);
if
(!(
EXACT
(start->
pos
.
z
==
0.0
) &&
EXACT
(next->
pos
.
z
==
0.0
))) {
polyline.
flags
|=
8
/*
3d polyline
*/
;
}
polyline.
vertlist
.
push_back
(
new
DRW_Vertex
(start->
pos
.
x
, start->
pos
.
y
, start->
pos
.
z
,
0.0
));
polyline.
vertlist
.
push_back
(
new
DRW_Vertex
(next->
pos
.
x
, next->
pos
.
y
, next->
pos
.
z
,
0.0
));
};
auto
nextFunc = [&](PolylineBuilder::Vertex *next, PolylineBuilder::Edge *e) {
if
(!
EXACT
(next->
pos
.
z
==
0.0
)) {
polyline.
flags
|=
8
/*
3d polyline
*/
;
}
polyline.
vertlist
.
push_back
(
new
DRW_Vertex
(next->
pos
.
x
, next->
pos
.
y
, next->
pos
.
z
,
0.0
));
};
auto
endFunc = [&]() {
dxf->
writePolyline
(&polyline);
};
auto
aloneFunc = [&](PolylineBuilder::Edge *e) {
hStyle hs = { e->
kind
};
writeLine
(e->
a
->
pos
, e->
b
->
pos
, hs);
};
builder.
Generate
(startFunc, nextFunc, aloneFunc, endFunc);
}
void
writeEntities
()
override
{
writePolylines
();
for
(DxfFileWriter::BezierPath &path : writer->
paths
) {
for
(SBezier *sb : path.
beziers
) {
if
(sb->
deg
==
1
)
continue
;
writeBezier
(sb);
}
}
if
(writer->
constraint
) {
for
(Constraint &c : *writer->
constraint
) {
if
(!writer->
NeedToOutput
(&c))
continue
;
switch
(c.
type
) {
case
Constraint::Type::
PT_PT_DISTANCE
: {
Vector ap =
SK
.
GetEntity
(c.
ptA
)->
PointGetNum
();
Vector bp =
SK
.
GetEntity
(c.
ptB
)->
PointGetNum
();
Vector ref = ((ap.
Plus
(bp)).
ScaledBy
(
0.5
)).
Plus
(c.
disp
.
offset
);
writeAlignedDimension
(
xfrm
(ap),
xfrm
(bp),
xfrm
(ref),
xfrm
(ref), c.
Label
(), c.
GetStyle
(), c.
valA
);
break
;
}
case
Constraint::Type::
PT_LINE_DISTANCE
: {
Vector pt =
SK
.
GetEntity
(c.
ptA
)->
PointGetNum
();
Entity *line =
SK
.
GetEntity
(c.
entityA
);
Vector lA =
SK
.
GetEntity
(line->
point
[
0
])->
PointGetNum
();
Vector lB =
SK
.
GetEntity
(line->
point
[
1
])->
PointGetNum
();
Vector dl = lB.
Minus
(lA);
Vector closest = pt.
ClosestPointOnLine
(lA, dl);
if
(pt.
Equals
(closest))
break
;
Vector ref = ((closest.
Plus
(pt)).
ScaledBy
(
0.5
)).
Plus
(c.
disp
.
offset
);
Vector refClosest = ref.
ClosestPointOnLine
(lA, dl);
double
ddl = dl.
Dot
(dl);
if
(
fabs
(ddl) >
LENGTH_EPS
*
LENGTH_EPS
) {
double
t = refClosest.
Minus
(lA).
Dot
(dl) / ddl;
if
(t <
0.0
) {
refClosest = lA;
}
else
if
(t >
1.0
) {
refClosest = lB;
}
}
Vector xdl =
xfrm
(lB).
Minus
(
xfrm
(lA));
writeLinearDimension
(
xfrm
(pt),
xfrm
(refClosest),
xfrm
(ref),
xfrm
(ref), c.
Label
(),
atan2
(xdl.
y
, xdl.
x
) /
PI
*
180.0
+
90.0
,
0.0
,
c.
GetStyle
(), c.
valA
);
break
;
}
case
Constraint::Type::
DIAMETER
: {
Entity *circle =
SK
.
GetEntity
(c.
entityA
);
Vector center =
SK
.
GetEntity
(circle->
point
[
0
])->
PointGetNum
();
Quaternion q =
SK
.
GetEntity
(circle->
normal
)->
NormalGetNum
();
Vector n = q.
RotationN
().
WithMagnitude
(
1
);
double
r = circle->
CircleGetRadiusNum
();
Vector ref = center.
Plus
(c.
disp
.
offset
);
//
Force the label into the same plane as the circle.
ref = ref.
Minus
(n.
ScaledBy
(n.
Dot
(ref) - n.
Dot
(center)));
Vector rad = ref.
Minus
(center).
WithMagnitude
(r);
if
(
/*
isRadius
*/
c.
other
) {
writeRadialDimension
(
xfrm
(center),
xfrm
(center.
Plus
(rad)),
xfrm
(ref), c.
Label
(), c.
GetStyle
(), c.
valA
);
}
else
{
writeDiametricDimension
(
xfrm
(center.
Minus
(rad)),
xfrm
(center.
Plus
(rad)),
xfrm
(ref), c.
Label
(), c.
GetStyle
(), c.
valA
);
}
break
;
}
case
Constraint::Type::
ANGLE
: {
Entity *a =
SK
.
GetEntity
(c.
entityA
);
Entity *b =
SK
.
GetEntity
(c.
entityB
);
Vector a0 = a->
VectorGetStartPoint
();
Vector b0 = b->
VectorGetStartPoint
();
Vector da = a->
VectorGetNum
();
Vector db = b->
VectorGetNum
();
if
(
/*
otherAngle
*/
c.
other
) {
a0 = a0.
Plus
(da);
da = da.
ScaledBy
(-
1
);
}
bool
skew =
false
;
Vector ref = c.
disp
.
offset
;
Vector pi =
Vector::AtIntersectionOfLines
(a0, a0.
Plus
(da), b0, b0.
Plus
(db),
&skew);
if
(!skew) ref = pi.
Plus
(c.
disp
.
offset
);
Vector norm = da.
Cross
(db);
Vector dna = norm.
Cross
(da).
WithMagnitude
(
1.0
);
double
thetaf =
acos
(da.
DirectionCosineWith
(db));
//
Calculate median
Vector m = da.
WithMagnitude
(
1.0
).
ScaledBy
(
cos
(thetaf/
2
)).
Plus
(
dna.
ScaledBy
(
sin
(thetaf/
2
)));
Vector rm = ref.
Minus
(pi);
//
Test which side we have to place an arc
if
(m.
Dot
(rm) <
0
) {
da = da.
ScaledBy
(-
1
); dna = dna.
ScaledBy
(-
1
);
db = db.
ScaledBy
(-
1
);
}
Vector bisect = da.
WithMagnitude
(
1.0
).
ScaledBy
(
cos
(thetaf /
2.0
)).
Plus
(
dna.
ScaledBy
(
sin
(thetaf /
2.0
)));
ref = pi.
Plus
(bisect.
WithMagnitude
(c.
disp
.
offset
.
Magnitude
()));
//
Get lines again to write exact line.
a0 = a->
VectorGetStartPoint
();
b0 = b->
VectorGetStartPoint
();
da = a->
VectorGetNum
();
db = b->
VectorGetNum
();
writeAngularDimension
(
xfrm
(a0),
xfrm
(a0.
Plus
(da)),
xfrm
(b0),
xfrm
(b0.
Plus
(db)),
xfrm
(ref),
xfrm
(ref), c.
Label
(), c.
GetStyle
(), c.
valA
);
break
;
}
case
Constraint::Type::
COMMENT
: {
Style *st =
SK
.
style
.
FindById
(c.
GetStyle
());
writeText
(
xfrm
(c.
disp
.
offset
), c.
Label
(),
Style::TextHeight
(c.
GetStyle
()) /
SS
.
GW
.
scale
,
st->
textAngle
, st->
textOrigin
, c.
GetStyle
());
break
;
}
default
:
//
Other types of constraints do not have a DXF dimension equivalent.
break
;
}
}
}
}
int
findDxfColor
(
const
RgbaColor &src) {
int
best =
0
;
double
minDist =
VERY_POSITIVE
;
Vector srcv =
Vector::From
(src.
redF
(), src.
greenF
(), src.
blueF
());
for
(
int
i =
1
; i <
256
; i++) {
RgbaColor dst =
RGBi
(
DRW
::dxfColors[i][
0
],
DRW
::dxfColors[i][
1
],
DRW
::dxfColors[i][
2
]);
Vector dstv =
Vector::From
(dst.
redF
(), dst.
greenF
(), dst.
blueF
());
double
dist = srcv.
Minus
(dstv).
Magnitude
();
if
(dist < minDist || best ==
0
) {
best = i;
minDist = dist;
}
}
return
best;
}
void
assignEntityDefaults
(DRW_Entity *entity, hStyle hs) {
Style *s =
Style::Get
(hs);
RgbaColor color =
Style::Color
(hs,
/*
forExport=
*/
true
);
entity->
color24
= color.
ToPackedIntBGRA
();
entity->
color
=
findDxfColor
(color);
entity->
layer
= s->
DescriptionString
();
entity->
lineType
=
DxfFileWriter::lineTypeName
(s->
stippleType
);
entity->
ltypeScale
=
Style::StippleScaleMm
(s->
h
);
entity->
setWidthMm
(
Style::WidthMm
(hs.
v
));
if
(s->
stippleType
== StipplePattern::
FREEHAND
) {
messages.
insert
(
_
(
"
freehand lines were replaced with continuous lines
"
));
}
else
if
(s->
stippleType
== StipplePattern::
ZIGZAG
) {
messages.
insert
(
_
(
"
zigzag lines were replaced with continuous lines
"
));
}
}
void
assignDimensionDefaults
(DRW_Dimension *dimension, hStyle hs) {
assignEntityDefaults
(dimension, hs);
dimension->
layer
=
"
dimensions
"
;
}
void
writeLine
(
const
Vector &p0,
const
Vector &p1, hStyle hs) {
DRW_Line line;
assignEntityDefaults
(&line, hs);
line.
basePoint
=
toCoord
(p0);
line.
secPoint
=
toCoord
(p1);
dxf->
writeLine
(&line);
}
void
writeArc
(
const
Vector &c,
double
r,
double
sa,
double
ea, hStyle hs) {
DRW_Arc arc;
assignEntityDefaults
(&arc, hs);
arc.
radious
= r;
arc.
basePoint
=
toCoord
(c);
arc.
staangle
= sa;
arc.
endangle
= ea;
dxf->
writeArc
(&arc);
}
void
writeBezierAsPwl
(SBezier *sb) {
List<Vector> lv = {};
sb->
MakePwlInto
(&lv,
SS
.
ExportChordTolMm
());
hStyle hs = { (
uint32_t
)sb->
auxA
};
DRW_Polyline polyline;
assignEntityDefaults
(&polyline, hs);
for
(
int
i =
0
; i < lv.
n
; i++) {
Vector *v = &lv[i];
DRW_Vertex *vertex =
new
DRW_Vertex
(v->
x
, v->
y
, v->
z
,
0.0
);
polyline.
vertlist
.
push_back
(vertex);
}
dxf->
writePolyline
(&polyline);
lv.
Clear
();
}
void
makeKnotsFor
(DRW_Spline *spline) {
//
QCad/LibreCAD require this for some reason.
if
(spline->
degree
==
3
) {
spline->
nknots
=
8
;
spline->
knotslist
.
push_back
(
0.0
);
spline->
knotslist
.
push_back
(
0.0
);
spline->
knotslist
.
push_back
(
0.0
);
spline->
knotslist
.
push_back
(
0.0
);
spline->
knotslist
.
push_back
(
1.0
);
spline->
knotslist
.
push_back
(
1.0
);
spline->
knotslist
.
push_back
(
1.0
);
spline->
knotslist
.
push_back
(
1.0
);
}
else
if
(spline->
degree
==
2
) {
spline->
nknots
=
6
;
spline->
knotslist
.
push_back
(
0.0
);
spline->
knotslist
.
push_back
(
0.0
);
spline->
knotslist
.
push_back
(
0.0
);
spline->
knotslist
.
push_back
(
1.0
);
spline->
knotslist
.
push_back
(
1.0
);
spline->
knotslist
.
push_back
(
1.0
);
}
else
ssassert
(
false
,
"
Unexpected degree of spline
"
);
}
void
writeSpline
(SBezier *sb) {
bool
isRational = sb->
IsRational
();
hStyle hs = { (
uint32_t
)sb->
auxA
};
DRW_Spline spline;
assignEntityDefaults
(&spline, hs);
spline.
flags
= (isRational) ?
0x04
:
0x08
;
spline.
degree
= sb->
deg
;
spline.
ncontrol
= sb->
deg
+
1
;
makeKnotsFor
(&spline);
for
(
int
i =
0
; i <= sb->
deg
; i++) {
spline.
controllist
.
push_back
(
new
DRW_Coord
(sb->
ctrl
[i].
x
, sb->
ctrl
[i].
y
, sb->
ctrl
[i].
z
));
if
(isRational) spline.
weightlist
.
push_back
(sb->
weight
[i]);
}
dxf->
writeSpline
(&spline);
}
void
writeBezier
(SBezier *sb) {
hStyle hs = { (
uint32_t
)sb->
auxA
};
Vector c;
Vector n =
Vector::From
(
0.0
,
0.0
,
1.0
);
double
r;
if
(sb->
deg
==
1
) {
//
Line
writeLine
(sb->
ctrl
[
0
], sb->
ctrl
[
1
], hs);
}
else
if
(sb->
IsInPlane
(n,
0
) && sb->
IsCircle
(n, &c, &r)) {
//
Circle perpendicular to camera
double
theta0 =
atan2
(sb->
ctrl
[
0
].
y
- c.
y
, sb->
ctrl
[
0
].
x
- c.
x
);
double
theta1 =
atan2
(sb->
ctrl
[
2
].
y
- c.
y
, sb->
ctrl
[
2
].
x
- c.
x
);
double
dtheta =
WRAP_SYMMETRIC
(theta1 - theta0,
2.0
*
PI
);
if
(dtheta <
0.0
)
swap
(theta0, theta1);
writeArc
(c, r, theta0, theta1, hs);
}
else
if
(sb->
IsRational
()) {
//
Rational bezier
//
We'd like to export rational beziers exactly, but the resulting DXF
//
files can only be read by AutoCAD; LibreCAD/QCad simply do not
//
implement the feature. So, export as piecewise linear for compatibility.
writeBezierAsPwl
(sb);
}
else
{
//
Any other curve
writeSpline
(sb);
}
}
void
writeAlignedDimension
(Vector def1, Vector def2, Vector dimp,
Vector textp,
const
std::string &text, hStyle hs,
double
actual) {
DRW_DimAligned dim;
assignDimensionDefaults
(&dim, hs);
dim.
setDef1Point
(
toCoord
(def1));
dim.
setDef2Point
(
toCoord
(def2));
dim.
setDimPoint
(
toCoord
(dimp));
dim.
setTextPoint
(
toCoord
(textp));
dim.
setText
(text);
dim.
setActualMeasurement
(actual);
dxf->
writeDimension
(&dim);
}
void
writeLinearDimension
(Vector def1, Vector def2, Vector dimp,
Vector textp,
const
std::string &text,
double
angle,
double
oblique, hStyle hs,
double
actual) {
DRW_DimLinear dim;
assignDimensionDefaults
(&dim, hs);
dim.
setDef1Point
(
toCoord
(def1));
dim.
setDef2Point
(
toCoord
(def2));
dim.
setDimPoint
(
toCoord
(dimp));
dim.
setTextPoint
(
toCoord
(textp));
dim.
setText
(text);
dim.
setAngle
(angle);
dim.
setOblique
(oblique);
dim.
setActualMeasurement
(actual);
dxf->
writeDimension
(&dim);
}
void
writeRadialDimension
(Vector center, Vector radius,
Vector textp,
const
std::string &text, hStyle hs,
double
actual) {
DRW_DimRadial dim;
assignDimensionDefaults
(&dim, hs);
dim.
setCenterPoint
(
toCoord
(center));
dim.
setDiameterPoint
(
toCoord
(radius));
dim.
setTextPoint
(
toCoord
(textp));
dim.
setText
(text);
dim.
setActualMeasurement
(actual);
dxf->
writeDimension
(&dim);
}
void
writeDiametricDimension
(Vector def1, Vector def2,
Vector textp,
const
std::string &text, hStyle hs,
double
actual) {
DRW_DimDiametric dim;
assignDimensionDefaults
(&dim, hs);
dim.
setDiameter1Point
(
toCoord
(def1));
dim.
setDiameter2Point
(
toCoord
(def2));
dim.
setTextPoint
(
toCoord
(textp));
dim.
setText
(text);
dim.
setActualMeasurement
(actual);
dxf->
writeDimension
(&dim);
}
void
writeAngularDimension
(Vector fl1, Vector fl2, Vector sl1, Vector sl2, Vector dimp,
Vector textp,
const
std::string &text, hStyle hs,
double
actual) {
DRW_DimAngular dim;
assignDimensionDefaults
(&dim, hs);
dim.
setFirstLine1
(
toCoord
(fl1));
dim.
setFirstLine2
(
toCoord
(fl2));
dim.
setSecondLine1
(
toCoord
(sl1));
dim.
setSecondLine2
(
toCoord
(sl2));
dim.
setDimPoint
(
toCoord
(dimp));
dim.
setTextPoint
(
toCoord
(textp));
dim.
setText
(text);
dim.
setActualMeasurement
(actual *
PI
/
180.0
);
dxf->
writeDimension
(&dim);
}
void
writeText
(Vector textp,
const
std::string &text,
double
height,
double
angle, Style::TextOrigin origin, hStyle hs) {
DRW_Text txt;
assignEntityDefaults
(&txt, hs);
txt.
layer
=
"
text
"
;
txt.
style
=
"
unicode
"
;
txt.
basePoint
=
toCoord
(textp);
txt.
secPoint
= txt.
basePoint
;
txt.
text
= text;
txt.
height
= height;
txt.
angle
= angle;
txt.
alignH
= DRW_Text::HCenter;
if
((
uint32_t
)origin & (
uint32_t
)Style::TextOrigin::
LEFT
) {
txt.
alignH
= DRW_Text::HLeft;
}
else
if
((
uint32_t
)origin & (
uint32_t
)Style::TextOrigin::
RIGHT
) {
txt.
alignH
= DRW_Text::HRight;
}
txt.
alignV
= DRW_Text::VMiddle;
if
((
uint32_t
)origin & (
uint32_t
)Style::TextOrigin::
TOP
) {
txt.
alignV
= DRW_Text::VTop;
}
else
if
((
uint32_t
)origin & (
uint32_t
)Style::TextOrigin::
BOT
) {
txt.
alignV
= DRW_Text::VBaseLine;
}
dxf->
writeText
(&txt);
}
};
bool
DxfFileWriter::OutputConstraints
(IdList<Constraint,hConstraint> *constraint) {
this
->
constraint
= constraint;
return
true
;
}
void
DxfFileWriter::StartFile
() {
paths.
clear
();
}
void
DxfFileWriter::Background
(RgbaColor color) {
}
void
DxfFileWriter::StartPath
(RgbaColor strokeRgb,
double
lineWidth,
bool
filled, RgbaColor fillRgb, hStyle hs)
{
BezierPath path = {};
paths.
push_back
(path);
}
void
DxfFileWriter::FinishPath
(RgbaColor strokeRgb,
double
lineWidth,
bool
filled, RgbaColor fillRgb, hStyle hs)
{
}
void
DxfFileWriter::Triangle
(STriangle *tr) {
}
void
DxfFileWriter::Bezier
(SBezier *sb) {
paths.
back
().
beziers
.
push_back
(sb);
}
void
DxfFileWriter::FinishAndCloseFile
() {
dxfRW dxf;
DxfWriteInterface interface = {};
interface.
writer
=
this
;
interface.
dxf
= &dxf;
std::stringstream stream;
dxf.
write
(stream, &interface,
DRW
::
AC1021
,
/*
bin=
*/
false
);
paths.
clear
();
constraint =
NULL
;
if
(!
WriteFile
(filename, stream.
str
())) {
Error
(
"
Couldn't write to '%s'
"
, filename.
raw
.
c_str
());
return
;
}
if
(!interface.
messages
.
empty
()) {
std::string text =
_
(
"
Some aspects of the drawing have no DXF equivalent and
"
"
were not exported:
\n
"
);
for
(
const
std::string &message : interface.
messages
) {
text +=
"
*
"
+ message +
"
\n
"
;
}
Message
(text.
c_str
());
}
}
bool
DxfFileWriter::NeedToOutput
(Constraint *c) {
switch
(c->
type
) {
case
Constraint::Type::
PT_PT_DISTANCE
:
case
Constraint::Type::
PT_LINE_DISTANCE
:
case
Constraint::Type::
DIAMETER
:
case
Constraint::Type::
ANGLE
:
case
Constraint::Type::
COMMENT
:
return
c->
IsVisible
();
default
:
//
See writeEntities().
break
;
}
return
false
;
}
const
char
*
DxfFileWriter::lineTypeName
(StipplePattern stippleType) {
switch
(stippleType) {
case
StipplePattern::
CONTINUOUS
:
return
"
CONTINUOUS
"
;
case
StipplePattern::
SHORT_DASH
:
return
"
DASHED
"
;
case
StipplePattern::
DASH
:
return
"
DASHED
"
;
case
StipplePattern::
LONG_DASH
:
return
"
DASHEDX2
"
;
case
StipplePattern::
DASH_DOT
:
return
"
DASHDOT
"
;
case
StipplePattern::
DASH_DOT_DOT
:
return
"
DIVIDE
"
;
case
StipplePattern::
DOT
:
return
"
DOT
"
;
case
StipplePattern::
FREEHAND
:
case
StipplePattern::
ZIGZAG
:
/*
no corresponding DXF line type
*/
break
;
}
return
"
CONTINUOUS
"
;
}
//
-----------------------------------------------------------------------------
//
Routines for EPS output
//
-----------------------------------------------------------------------------
static
std::string
MakeStipplePattern
(StipplePattern pattern,
double
scale,
char
delimiter,
bool
inkscapeWorkaround =
false
) {
scale /=
2.0
;
//
Inkscape ignores all elements that are exactly zero instead of drawing
//
them as dots.
double
zero = inkscapeWorkaround ?
1e-6
:
0
;
std::string result;
switch
(pattern) {
case
StipplePattern::
CONTINUOUS
:
return
"
"
;
case
StipplePattern::
SHORT_DASH
:
result =
ssprintf
(
"
%.3f_%.3f
"
, scale, scale *
2.0
);
break
;
case
StipplePattern::
DASH
:
result =
ssprintf
(
"
%.3f_%.3f
"
, scale, scale);
break
;
case
StipplePattern::
DASH_DOT
:
result =
ssprintf
(
"
%.3f_%.3f_%.6f_%.3f
"
,
scale, scale *
0.5
, zero, scale *
0.5
);
break
;
case
StipplePattern::
DASH_DOT_DOT
:
result =
ssprintf
(
"
%.3f_%.3f_%.6f_%.3f_%.6f_%.3f
"
,
scale, scale *
0.5
, zero,
scale *
0.5
, scale *
0.5
, zero);
break
;
case
StipplePattern::
DOT
:
result =
ssprintf
(
"
%.6f_%.3f
"
, zero, scale *
0.5
);
break
;
case
StipplePattern::
LONG_DASH
:
result =
ssprintf
(
"
%.3f_%.3f
"
, scale *
2.0
, scale *
0.5
);
break
;
case
StipplePattern::
FREEHAND
:
case
StipplePattern::
ZIGZAG
:
ssassert
(
false
,
"
Freehand and zigzag export not implemented
"
);
}
std::replace
(result.
begin
(), result.
end
(),
'
_
'
, delimiter);
return
result;
}
void
EpsFileWriter::StartFile
() {
fprintf
(f,
"
%%!PS-Adobe-2.0
\r\n
"
"
%%%%Creator: SolveSpace
\r\n
"
"
%%%%Title: title
\r\n
"
"
%%%%Pages: 0
\r\n
"
"
%%%%PageOrder: Ascend
\r\n
"
"
%%%%BoundingBox: 0 0 %d %d
\r\n
"
"
%%%%HiResBoundingBox: 0 0 %.3f %.3f
\r\n
"
"
%%%%EndComments
\r\n
"
"
\r\n
"
"
gsave
\r\n
"
"
\r\n
"
,
(
int
)
ceil
(
MmToPts
(ptMax.
x
- ptMin.
x
)),
(
int
)
ceil
(
MmToPts
(ptMax.
y
- ptMin.
y
)),
MmToPts
(ptMax.
x
- ptMin.
x
),
MmToPts
(ptMax.
y
- ptMin.
y
));
}
void
EpsFileWriter::Background
(RgbaColor color) {
double
width = ptMax.
x
- ptMin.
x
;
double
height = ptMax.
y
- ptMin.
y
;
fprintf
(f,
"
%.3f %.3f %.3f setrgbcolor
\r\n
"
"
newpath
\r\n
"
"
%.3f %.3f moveto
\r\n
"
"
%.3f %.3f lineto
\r\n
"
"
%.3f %.3f lineto
\r\n
"
"
%.3f %.3f lineto
\r\n
"
"
closepath
\r\n
"
"
gsave fill grestore
\r\n
"
,
color.
redF
(), color.
greenF
(), color.
blueF
(),
MmToPts
(
0
),
MmToPts
(
0
),
MmToPts
(width),
MmToPts
(
0
),
MmToPts
(width),
MmToPts
(height),
MmToPts
(
0
),
MmToPts
(height));
//
same issue with cracks, stroke it to avoid them
double
sw =
max
(width, height) /
1000
;
fprintf
(f,
"
1 setlinejoin
\r\n
"
"
1 setlinecap
\r\n
"
"
%.3f setlinewidth
\r\n
"
"
gsave stroke grestore
\r\n
"
,
MmToPts
(sw));
}
void
EpsFileWriter::StartPath
(RgbaColor strokeRgb,
double
lineWidth,
bool
filled, RgbaColor fillRgb, hStyle hs)
{
fprintf
(f,
"
newpath
\r\n
"
);
prevPt =
Vector::From
(
VERY_POSITIVE
,
VERY_POSITIVE
,
VERY_POSITIVE
);
}
void
EpsFileWriter::FinishPath
(RgbaColor strokeRgb,
double
lineWidth,
bool
filled, RgbaColor fillRgb, hStyle hs)
{
StipplePattern pattern =
Style::PatternType
(hs);
double
stippleScale =
MmToPts
(
Style::StippleScaleMm
(hs));
fprintf
(f,
"
%.3f setlinewidth
\r\n
"
"
%.3f %.3f %.3f setrgbcolor
\r\n
"
"
1 setlinejoin
\r\n
"
//
rounded
"
1 setlinecap
\r\n
"
//
rounded
"
[%s] 0 setdash
\r\n
"
"
gsave stroke grestore
\r\n
"
,
MmToPts
(lineWidth),
strokeRgb.
redF
(), strokeRgb.
greenF
(), strokeRgb.
blueF
(),
MakeStipplePattern
(pattern, stippleScale,
'
'
).
c_str
());
if
(filled) {
fprintf
(f,
"
%.3f %.3f %.3f setrgbcolor
\r\n
"
"
gsave fill grestore
\r\n
"
,
fillRgb.
redF
(), fillRgb.
greenF
(), fillRgb.
blueF
());
}
}
void
EpsFileWriter::MaybeMoveTo
(Vector st, Vector fi) {
if
(!prevPt.
Equals
(st)) {
fprintf
(f,
"
%.3f %.3f moveto
\r\n
"
,
MmToPts
(st.
x
- ptMin.
x
),
MmToPts
(st.
y
- ptMin.
y
));
}
prevPt = fi;
}
void
EpsFileWriter::Triangle
(STriangle *tr) {
fprintf
(f,
"
%.3f %.3f %.3f setrgbcolor
\r\n
"
"
newpath
\r\n
"
"
%.3f %.3f moveto
\r\n
"
"
%.3f %.3f lineto
\r\n
"
"
%.3f %.3f lineto
\r\n
"
"
closepath
\r\n
"
"
gsave fill grestore
\r\n
"
,
tr->
meta
.
color
.
redF
(), tr->
meta
.
color
.
greenF
(), tr->
meta
.
color
.
blueF
(),
MmToPts
(tr->
a
.
x
- ptMin.
x
),
MmToPts
(tr->
a
.
y
- ptMin.
y
),
MmToPts
(tr->
b
.
x
- ptMin.
x
),
MmToPts
(tr->
b
.
y
- ptMin.
y
),
MmToPts
(tr->
c
.
x
- ptMin.
x
),
MmToPts
(tr->
c
.
y
- ptMin.
y
));
//
same issue with cracks, stroke it to avoid them
double
sw =
max
(ptMax.
x
- ptMin.
x
, ptMax.
y
- ptMin.
y
) /
1000
;
fprintf
(f,
"
1 setlinejoin
\r\n
"
"
1 setlinecap
\r\n
"
"
%.3f setlinewidth
\r\n
"
"
gsave stroke grestore
\r\n
"
,
MmToPts
(sw));
}
void
EpsFileWriter::Bezier
(SBezier *sb) {
Vector c, n =
Vector::From
(
0
,
0
,
1
);
double
r;
if
(sb->
deg
==
1
) {
MaybeMoveTo
(sb->
ctrl
[
0
], sb->
ctrl
[
1
]);
fprintf
(f,
"
%.3f %.3f lineto
\r\n
"
,
MmToPts
(sb->
ctrl
[
1
].
x
- ptMin.
x
),
MmToPts
(sb->
ctrl
[
1
].
y
- ptMin.
y
));
}
else
if
(sb->
IsCircle
(n, &c, &r)) {
Vector p0 = sb->
ctrl
[
0
], p1 = sb->
ctrl
[
2
];
double
theta0 =
atan2
(p0.
y
- c.
y
, p0.
x
- c.
x
),
theta1 =
atan2
(p1.
y
- c.
y
, p1.
x
- c.
x
),
dtheta =
WRAP_SYMMETRIC
(theta1 - theta0,
2
*
PI
);
MaybeMoveTo
(p0, p1);
fprintf
(f,
"
%.3f %.3f %.3f %.3f %.3f %s
\r\n
"
,
MmToPts
(c.
x
- ptMin.
x
),
MmToPts
(c.
y
- ptMin.
y
),
MmToPts
(r),
theta0*
180
/
PI
, theta1*
180
/
PI
,
dtheta <
0
?
"
arcn
"
:
"
arc
"
);
}
else
if
(sb->
deg
==
3
&& !sb->
IsRational
()) {
MaybeMoveTo
(sb->
ctrl
[
0
], sb->
ctrl
[
3
]);
fprintf
(f,
"
%.3f %.3f %.3f %.3f %.3f %.3f curveto
\r\n
"
,
MmToPts
(sb->
ctrl
[
1
].
x
- ptMin.
x
),
MmToPts
(sb->
ctrl
[
1
].
y
- ptMin.
y
),
MmToPts
(sb->
ctrl
[
2
].
x
- ptMin.
x
),
MmToPts
(sb->
ctrl
[
2
].
y
- ptMin.
y
),
MmToPts
(sb->
ctrl
[
3
].
x
- ptMin.
x
),
MmToPts
(sb->
ctrl
[
3
].
y
- ptMin.
y
));
}
else
{
BezierAsNonrationalCubic
(sb);
}
}
void
EpsFileWriter::FinishAndCloseFile
() {
fprintf
(f,
"
\r\n
"
"
grestore
\r\n
"
"
\r\n
"
);
fclose
(f);
}
//
-----------------------------------------------------------------------------
//
Routines for PDF output, some extra complexity because we have to generate
//
a correct xref table.
//
-----------------------------------------------------------------------------
void
PdfFileWriter::StartFile
() {
if
((ptMax.
x
- ptMin.
x
) >
200
*
25.4
||
(ptMax.
y
- ptMin.
y
) >
200
*
25.4
)
{
Message
(
_
(
"
PDF page size exceeds 200 by 200 inches; many viewers may
"
"
reject this file.
"
));
}
fprintf
(f,
"
%%PDF-1.1
\r\n
"
"
%%%c%c%c%c
\r\n
"
,
0xe2
,
0xe3
,
0xcf
,
0xd3
);
xref[
1
] = (
uint32_t
)
ftell
(f);
fprintf
(f,
"
1 0 obj
\r\n
"
"
<< /Type /Catalog
\r\n
"
"
/Outlines 2 0 R
\r\n
"
"
/Pages 3 0 R
\r\n
"
"
>>
\r\n
"
"
endobj
\r\n
"
);
xref[
2
] = (
uint32_t
)
ftell
(f);
fprintf
(f,
"
2 0 obj
\r\n
"
"
<< /Type /Outlines
\r\n
"
"
/Count 0
\r\n
"
"
>>
\r\n
"
"
endobj
\r\n
"
);
xref[
3
] = (
uint32_t
)
ftell
(f);
fprintf
(f,
"
3 0 obj
\r\n
"
"
<< /Type /Pages
\r\n
"
"
/Kids [4 0 R]
\r\n
"
"
/Count 1
\r\n
"
"
>>
\r\n
"
"
endobj
\r\n
"
);
xref[
4
] = (
uint32_t
)
ftell
(f);
fprintf
(f,
"
4 0 obj
\r\n
"
"
<< /Type /Page
\r\n
"
"
/Parent 3 0 R
\r\n
"
"
/MediaBox [0 0 %.3f %.3f]
\r\n
"
"
/Contents 5 0 R
\r\n
"
"
/Resources << /ProcSet 7 0 R
\r\n
"
"
/Font << /F1 8 0 R >>
\r\n
"
"
>>
\r\n
"
"
>>
\r\n
"
"
endobj
\r\n
"
,
MmToPts
(ptMax.
x
- ptMin.
x
),
MmToPts
(ptMax.
y
- ptMin.
y
));
xref[
5
] = (
uint32_t
)
ftell
(f);
fprintf
(f,
"
5 0 obj
\r\n
"
"
<< /Length 6 0 R >>
\r\n
"
"
stream
\r\n
"
);
bodyStart = (
uint32_t
)
ftell
(f);
}
void
PdfFileWriter::FinishAndCloseFile
() {
uint32_t
bodyEnd = (
uint32_t
)
ftell
(f);
fprintf
(f,
"
endstream
\r\n
"
"
endobj
\r\n
"
);
xref[
6
] = (
uint32_t
)
ftell
(f);
fprintf
(f,
"
6 0 obj
\r\n
"
"
%d
\r\n
"
"
endobj
\r\n
"
,
bodyEnd - bodyStart);
xref[
7
] = (
uint32_t
)
ftell
(f);
fprintf
(f,
"
7 0 obj
\r\n
"
"
[/PDF /Text]
\r\n
"
"
endobj
\r\n
"
);
xref[
8
] = (
uint32_t
)
ftell
(f);
fprintf
(f,
"
8 0 obj
\r\n
"
"
<< /Type /Font
\r\n
"
"
/Subtype /Type1
\r\n
"
"
/Name /F1
\r\n
"
"
/BaseFont /Helvetica
\r\n
"
"
/Encoding /WinAnsiEncoding
\r\n
"
"
>>
\r\n
"
"
endobj
\r\n
"
);
xref[
9
] = (
uint32_t
)
ftell
(f);
fprintf
(f,
"
9 0 obj
\r\n
"
"
<< /Creator (SolveSpace)
\r\n
"
"
>>
\r\n
"
);
uint32_t
xrefStart = (
uint32_t
)
ftell
(f);
fprintf
(f,
"
xref
\r\n
"
"
0 10
\r\n
"
"
0000000000 65535 f
\r\n
"
);
int
i;
for
(i =
1
; i <=
9
; i++) {
fprintf
(f,
"
%010d %05d n
\r\n
"
, xref[i],
0
);
}
fprintf
(f,
"
\r\n
"
"
trailer
\r\n
"
"
<< /Size 10
\r\n
"
"
/Root 1 0 R
\r\n
"
"
/Info 9 0 R
\r\n
"
"
>>
\r\n
"
"
startxref
\r\n
"
"
%d
\r\n
"
"
%%%%EOF
\r\n
"
,
xrefStart);
fclose
(f);
}
void
PdfFileWriter::Background
(RgbaColor color) {
double
width = ptMax.
x
- ptMin.
x
;
double
height = ptMax.
y
- ptMin.
y
;
double
sw =
max
(width, height) /
1000
;
fprintf
(f,
"
1 J 1 j
\r\n
"
"
%.3f %.3f %.3f RG
\r\n
"
"
%.3f %.3f %.3f rg
\r\n
"
"
%.3f w
\r\n
"
"
%.3f %.3f m
\r\n
"
"
%.3f %.3f l
\r\n
"
"
%.3f %.3f l
\r\n
"
"
%.3f %.3f l
\r\n
"
"
b
\r\n
"
,
color.
redF
(), color.
greenF
(), color.
blueF
(),
color.
redF
(), color.
greenF
(), color.
blueF
(),
MmToPts
(sw),
MmToPts
(
0
),
MmToPts
(
0
),
MmToPts
(width),
MmToPts
(
0
),
MmToPts
(width),
MmToPts
(height),
MmToPts
(
0
),
MmToPts
(height));
}
void
PdfFileWriter::StartPath
(RgbaColor strokeRgb,
double
lineWidth,
bool
filled, RgbaColor fillRgb, hStyle hs)
{
StipplePattern pattern =
Style::PatternType
(hs);
double
stippleScale =
MmToPts
(
Style::StippleScaleMm
(hs));
fprintf
(f,
"
1 J 1 j
"
//
round endcaps and joins
"
%.3f w [%s] 0 d
"
"
%.3f %.3f %.3f RG
\r\n
"
,
MmToPts
(lineWidth),
MakeStipplePattern
(pattern, stippleScale,
'
'
).
c_str
(),
strokeRgb.
redF
(), strokeRgb.
greenF
(), strokeRgb.
blueF
());
if
(filled) {
fprintf
(f,
"
%.3f %.3f %.3f rg
\r\n
"
,
fillRgb.
redF
(), fillRgb.
greenF
(), fillRgb.
blueF
());
}
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL