forked from KitWallace/openscad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolid.xq
82 lines (78 loc) · 2.8 KB
/
solid.xq
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
declare variable $local:db := concat("/db/",substring-after(system:get-module-load-path(),"/db/"),"/");
declare variable $local:site := "http://dmccooey.com/polyhedra/";
declare variable $local:scad-main := concat($local:db,"solid-main.scad");
declare function local:get-solid($id) {
let $url := concat($local:site,$id,".txt")
let $doc := httpclient:get(xs:anyURI($url),false(),())/httpclient:body
let $doc := util:unescape-uri($doc,"UTF-8")
let $lines := tokenize($doc," ")
let $name := $lines[1] (: bug - actually can be multiple lines :)
let $vars :=
for $vline in $lines[starts-with(.,"C")]
let $parts := tokenize($vline," = ")
let $name := $parts[1]
let $value := normalize-space($parts[2])
where matches($value,"^(\d|\.)+$") (: only constant values, not formula :)
return
element var {
element name {$name},
element value {$value}
}
let $points :=
for $pline in $lines[starts-with(.,"V")]
let $parts := tokenize($pline," = ")
let $xyz := replace(replace($parts[2],"\(",""),"\)","")
return
element point {$xyz}
let $faces :=
for $fline in $lines[starts-with(.,"{")]
let $list := substring-after(substring-before($fline,"}"),"{")
return
element face {$list}
return
element solid {
element id {$id},
element name {$name},
element vars {$vars},
element points {$points},
element faces {$faces}
}
};
declare function local:solid_to_openscad($solid) {
string-join(
(
concat (
"// source: ", concat($local:site,$solid/id,".html")),
"// generated by http://kitwallace.co.uk/3d/solid.xq",
concat('Name = "',$solid/name,'";'),
for $var in $solid/vars/var
return concat ($var/name, " = ", $var/value, ";"),
concat("points = [ ",
string-join(
for $point in $solid/points/point
return concat("[",$point,"]")
,", ")
, "];"),
concat("faces = [ ",
string-join(
for $face in $solid/faces/face
return concat("[",$face,"]")
,", ")
, "];"),
"// --------------------------------- ",
""
)," "
)
};
let $id:= request:get-parameter("id",())
let $solid := local:get-solid($id)
let $openscad := if($solid)
then
(local:solid_to_openscad($solid),
util:binary-to-string(util:binary-doc($local:scad-main))
)
else ()
let $serialize := util:declare-option("exist:serialize", "format=text media-type=text/text")
let $header := response:set-header('content-disposition', concat("attachment; filename=",$id,".scad"))
return
$openscad