-
Notifications
You must be signed in to change notification settings - Fork 0
/
geom-dxf.stanza
81 lines (67 loc) · 2.35 KB
/
geom-dxf.stanza
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
;; See license.txt for details about licensing.
defpackage geom/dxf :
import core
import collections
import math
import utils/seqable
import geom/vec
import geom/box
import geom/polygon
import geom/line-segment
import geom/polyline
import geom/bounded
import geom/poseable
import geom/assembly
defn do-print-dxf (s:OutputStream, assembly:Assembly) :
for elt in children(assembly) do :
match(elt) :
(c:PolyLine2|Polygon|Assembly) : do-print-dxf(s, xyz(mat(assembly), c))
(c) : false
defn do-print-dxf (s:OutputStream, x:Double, code:Int) :
println(s, code)
println(s, x)
defn do-print-dxf (s:OutputStream, pt:Vec2, offset:Int) :
do-print-dxf(s, x(pt), 10 + offset)
do-print-dxf(s, y(pt), 20 + offset)
do-print-dxf(s, 0.0, 30 + offset)
defn do-print-dxf (s:OutputStream, color:Vec4, start:Vec2, end:Vec2) :
println(s, "0")
println(s, "LINE")
println(s, "8")
println(s, "0")
do-print-dxf(s, start, 0)
do-print-dxf(s, end, 1)
defn do-print-dxf (s:OutputStream, color:Vec4, points:Seqable<Vec2>, close?:True|False) :
val pts = to-tuple $ points
if close? :
for [b,e] in successive-pairs-wrapped(pts) do : do-print-dxf(s, color, b, e)
else :
for [b,e] in successive-pairs(pts) do : do-print-dxf(s, color, b, e)
defn do-print-dxf (s:OutputStream, color:Vec4, contour:Contour) :
do-print-dxf(s, color, vertices(contour), true)
defn do-print-dxf-header (s:OutputStream, bounds:Box2, f:OutputStream -> False) :
println(s, "0")
println(s, "SECTION")
println(s, "2")
println(s, "ENTITIES")
f(s)
println(s, "0")
println(s, "ENDSEC")
println(s, "0")
println(s, "EOF")
defn do-print-dxf (s:OutputStream, polygon:Polygon) :
for c in contours(polygon) do :
do-print-dxf(s, color(polygon), c)
defn do-print-dxf (s:OutputStream, polyline:PolyLine2) :
for line in strokes(polyline) do :
do-print-dxf(s, color(polyline), line, false)
defn dxf (s:OutputStream, g:Assembly|PolyLine2|Polygon, bbox:Box2) :
do-print-dxf-header(s, bbox, { (
val mg = mov(xyz(-1.0 * lo(bbox)), g)
do-print-dxf(s, mg) ) } )
public defn dxf (g:Assembly|PolyLine2|Polygon, filename:String, bbox:Box2) :
val s = FileOutputStream(filename)
try : dxf(s, g, bbox)
finally : close(s)
public defn dxf (g:Assembly|PolyLine2|Polygon, filename:String, margin:Vec2) :
dxf(g, filename, fatten(xy(bounds(g)), margin))