-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreplicator.js
233 lines (188 loc) · 4.98 KB
/
replicator.js
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
var COLUMNS = "Columns";
var ROWS = "Rows";
var CENTERS = "Use center distance";
var SPACING;
function properties(args) {
SPACING = `Spacing (${args.preferredUnit})`;
var isMetric = args.preferredUnit === "mm";
return [
{
type: "range",
id: COLUMNS,
value: 2,
min: 1,
max: 10,
step: 1
},
{
type: "range",
id: ROWS,
value: 2,
min: 1,
max: 10,
step: 1
},
{
type: "text",
id: SPACING,
value: isMetric
? 10
: 0.5,
},
{
type: "boolean",
id: CENTERS,
value: false
}
];
}
function shallowCopy(object) {
var result = {};
for (var key in object) {
if (object.hasOwnProperty(key)) {
result[key] = object[key];
}
}
return result;
}
function offsetLine(shape, dx, dy) {
var newShape = shallowCopy(shape);
newShape.point1 = {
x: shape.point1.x + dx,
y: shape.point1.y + dy
}
newShape.point2 = {
x: shape.point2.x + dx,
y: shape.point2.y + dy
}
return newShape;
}
function offsetCenteredShape(shape, dx, dy) {
var newShape = shallowCopy(shape);
newShape.center = {
x: shape.center.x + dx,
y: shape.center.y + dy
};
return newShape;
}
function offsetShape(shape, dx, dy) {
switch (shape.type) {
case "line":
return offsetLine(shape, dx, dy);
default:
return offsetCenteredShape(shape, dx, dy);
}
}
function offsetCut(cut, dx, dy) {
var newCut = shallowCopy(cut);
if (cut.tabs) {
newCut.tabs = cut.tabs.map(function(tab) {
var newTab = shallowCopy(tab);
newTab.center = {
x: newTab.center.x + dx,
y: newTab.center.y + dy,
};
return newTab;
});
}
return newCut;
}
function getVolumesByIds(volumes, ids) {
var selectedVolumes = [];
for (var i = 0; i < volumes.length; i++) {
var volume = volumes[i];
if (ids.indexOf(volume.id) >= 0) {
selectedVolumes.push(volume);
}
}
return selectedVolumes;
}
function getVolumeBounds(volume) {
switch (volume.shape.type) {
case "line":
return {
x1: Math.min(volume.shape.point1.x, volume.shape.point2.x),
x2: Math.max(volume.shape.point1.x, volume.shape.point2.x),
y1: Math.min(volume.shape.point1.y, volume.shape.point2.y),
y2: Math.max(volume.shape.point1.y, volume.shape.point2.y)
};
case "drill":
return {
x1: volume.shape.center.x,
x2: volume.shape.center.x,
y1: volume.shape.center.y,
y2: volume.shape.center.y
};
default:
return {
x1: volume.shape.center.x - (volume.shape.width / 2),
x2: volume.shape.center.x + (volume.shape.width / 2),
y1: volume.shape.center.y - (volume.shape.height / 2),
y2: volume.shape.center.y + (volume.shape.height / 2)
};
}
}
function calculateBounds(volumes) {
var bounds = getVolumeBounds(volumes[0]);
for (var i = 1; i < volumes.length; i++) {
var volume = volumes[i];
var volumeBounds = getVolumeBounds(volumes[i]);
bounds.x1 = Math.min(bounds.x1, volumeBounds.x1);
bounds.x2 = Math.max(bounds.x2, volumeBounds.x2);
bounds.y1 = Math.min(bounds.y1, volumeBounds.y1);
bounds.y2 = Math.max(bounds.y2, volumeBounds.y2);
}
return bounds;
}
function calculateGap(bounds, spacing, useCenters, unit) {
var unitDivisor = unit === "mm"
? 25.4
: 1.0;
var gap = {
x: spacing / unitDivisor,
y: spacing / unitDivisor
};
if (!useCenters) {
gap.x += bounds.x2 - bounds.x1;
gap.y += bounds.y2 - bounds.y1;
}
return gap;
}
function executor(args, success, failure) {
var columnCount = args.params[COLUMNS];
var rowCount = args.params[ROWS];
var spacing = Number(args.params[SPACING]);
var useCenters = args.params[CENTERS];
if (!isFinite(spacing)) {
failure("Spacing must be a number.");
return;
}
var selectedVolumeIds = args.selectedVolumeIds || []
if (selectedVolumeIds.length === 0) {
failure("Select one or more shapes to replicate.");
return;
}
var selectedVolumes = getVolumesByIds(args.volumes, selectedVolumeIds);
var bounds = calculateBounds(selectedVolumes);
var gap = calculateGap(bounds, spacing, useCenters, args.preferredUnit);
var newVolumes = [];
for (var i = 0; i < selectedVolumes.length; i++) {
var volume = selectedVolumes[i];
for (var row = 0; row < rowCount; row++) {
for (var col = 0; col < columnCount; col++) {
var dx = gap.x * col;
var dy = gap.y * row;
// Giving the original volume's id will replace it, so we don't end up with a double of it.
// For other rows & columns, setting the volume id to null will cause a new volume to be created.
var newId = (row === 0 && col === 0) ? volume.id : null;
var newVolume = {
id: newId,
shape: offsetShape(volume.shape, dx, dy),
cut: offsetCut(volume.cut, dx, dy),
};
newVolumes.push(newVolume);
}
}
}
success(newVolumes);
}