-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathformat-options.ts
173 lines (147 loc) Β· 6.05 KB
/
format-options.ts
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
namespace MakerJsPlayground.FormatOptions {
class BaseOptions {
constructor(public format: MakerJsPlaygroundExport.ExportFormat, public formatTitle: string, public div: HTMLDivElement, public model: MakerJs.IModel) {
}
$(selector: string) {
return this.div.querySelector(selector);
}
$checked(selector: string) {
const select = this.$(selector) as HTMLInputElement;
return select.checked;
}
$number(selector: string) {
const select = this.$(selector) as HTMLInputElement;
if (makerjs.isNumber(select.valueAsNumber)) {
return select.valueAsNumber;
}
return +select.value;
}
$selectValue(selector: string) {
const select = this.$(selector) as HTMLSelectElement;
return select.value;
}
addAccuracy(selector: string, options: MakerJs.exporter.IExportOptions) {
const accuracy = +this.$selectValue(selector);
if (accuracy >= 0) {
options.accuracy = accuracy;
}
}
getOptionObject(): MakerJs.exporter.IExportOptions {
throw 'not implemented';
}
validate() {
return true;
}
}
class DxfOptions extends BaseOptions {
constructor(format: MakerJsPlaygroundExport.ExportFormat, formatTitle: string, div: HTMLDivElement, model: MakerJs.IModel) {
super(format, formatTitle, div, model);
// TODO:
// inspect model to see if it contains units
// show unit picker if it does not
}
getOptionObject() {
const options: MakerJs.exporter.IDXFRenderOptions = {
usePOLYLINE: this.$checked('#dxf-usepolyline')
};
this.addAccuracy('#dxf-accuracy', options);
return options;
}
}
class SvgOptions extends BaseOptions {
getOptionObject() {
const options: MakerJs.exporter.ISVGRenderOptions = {
svgAttrs: { xmlns: "http://www.w3.org/2000/svg" }
};
this.addAccuracy('#svg-accuracy', options);
return options;
}
}
class SvgPathDataOptions extends BaseOptions {
getOptionObject() {
const options: MakerJs.exporter.ISVGPathDataRenderOptions = {
byLayers: false,
fillRule: this.$selectValue('#svgpathdata-fillrule') as 'evenodd' | 'nonzero',
origin: this.$selectValue('#svgpathdata-origin') === 'zero' ? [0, 0] : undefined
// TODO: Layer order
};
this.addAccuracy('#svgpathdata-accuracy', options);
return options;
}
}
class JsonOptions extends BaseOptions {
getOptionObject() {
const options: MakerJs.exporter.IJsonExportOptions = {
indentation: +this.$selectValue('#json-indent')
};
this.addAccuracy('#json-accuracy', options);
return options;
}
}
class JscadScriptOptions extends BaseOptions {
getOptionObject() {
const extrude = this.$number('#openjscad-extrusion');
if (extrude <= 0) {
//show the UI
return null;
} else {
//hide the ui
}
const options: MakerJs.exporter.IJscadScriptOptions = {
extrude,
functionName: this.$selectValue('#openjscad-name'),
indent: this.$number('#openjscad-indent'),
maxArcFacet: +this.$selectValue('#openjscad-facetsize')
};
this.addAccuracy('#openjscad-accuracy', options);
return options;
}
}
class StlOptions extends BaseOptions {
constructor(format: MakerJsPlaygroundExport.ExportFormat, formatTitle: string, div: HTMLDivElement, model: MakerJs.IModel) {
super(format, formatTitle, div, model);
//modelToExport.exporterOptions['toJscadCSG'])
// TODO:
// inspect model to see if it contains exporterOptions.layerOptions
// then disable extrude
}
getOptionObject() {
const options: MakerJs.exporter.IJscadCsgOptions = {
maxArcFacet: +this.$selectValue('#stl-facetsize'),
extrude: this.$number('#stl-extrude')
};
return options;
}
}
class PdfOptions extends BaseOptions {
getOptionObject() {
const options: MakerJs.exporter.IPDFRenderOptions = {
origin: [
+this.$selectValue('#pdf-leftmargin') * 72,
+this.$selectValue('#pdf-topmargin') * 72
]
};
return options;
}
}
let classes: { [format: number]: typeof BaseOptions } = {};
classes[MakerJsPlaygroundExport.ExportFormat.Dxf] = DxfOptions;
classes[MakerJsPlaygroundExport.ExportFormat.Json] = JsonOptions;
classes[MakerJsPlaygroundExport.ExportFormat.OpenJsCad] = JscadScriptOptions;
classes[MakerJsPlaygroundExport.ExportFormat.Pdf] = PdfOptions;
classes[MakerJsPlaygroundExport.ExportFormat.Stl] = StlOptions;
classes[MakerJsPlaygroundExport.ExportFormat.Svg] = SvgOptions;
classes[MakerJsPlaygroundExport.ExportFormat.SvgPathData] = SvgPathDataOptions;
export var current: BaseOptions;
export function activateOption(format: MakerJsPlaygroundExport.ExportFormat, formatTitle: string, model: MakerJs.IModel) {
const formatId = MakerJsPlaygroundExport.ExportFormat[format];
//deselect all
const all = document.querySelectorAll(`.download-option`);
for (let i = 0; i < all.length; i++) all[i].classList.remove('selected');
//select current
const div = document.querySelector(`.download-option[data-format="${formatId}"]`) as HTMLDivElement;
div.classList.add('selected');
let formatClass = classes[format];
current = new formatClass(format, formatTitle, div, model);
}
}