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
|
#ifdef SLIC3RXS
#include <xsinit.h>
namespace Slic3r {
REGISTER_CLASS(ExPolygon, "ExPolygon");
REGISTER_CLASS(ExPolygonCollection, "ExPolygon::Collection");
REGISTER_CLASS(ExtrusionMultiPath, "ExtrusionMultiPath");
REGISTER_CLASS(ExtrusionPath, "ExtrusionPath");
REGISTER_CLASS(ExtrusionLoop, "ExtrusionLoop");
// there is no ExtrusionLoop::Collection or ExtrusionEntity::Collection
REGISTER_CLASS(ExtrusionEntityCollection, "ExtrusionPath::Collection");
REGISTER_CLASS(ExtrusionSimulator, "ExtrusionSimulator");
REGISTER_CLASS(Filler, "Filler");
REGISTER_CLASS(Flow, "Flow");
REGISTER_CLASS(CoolingBuffer, "GCode::CoolingBuffer");
REGISTER_CLASS(GCode, "GCode");
REGISTER_CLASS(GCodePreviewData, "GCode::PreviewData");
REGISTER_CLASS(GCodeSender, "GCode::Sender");
REGISTER_CLASS(Layer, "Layer");
REGISTER_CLASS(SupportLayer, "Layer::Support");
REGISTER_CLASS(LayerRegion, "Layer::Region");
REGISTER_CLASS(Line, "Line");
REGISTER_CLASS(Linef3, "Linef3");
REGISTER_CLASS(PerimeterGenerator, "Layer::PerimeterGenerator");
REGISTER_CLASS(PlaceholderParser, "GCode::PlaceholderParser");
REGISTER_CLASS(Polygon, "Polygon");
REGISTER_CLASS(Polyline, "Polyline");
REGISTER_CLASS(PolylineCollection, "Polyline::Collection");
REGISTER_CLASS(Print, "Print");
REGISTER_CLASS(PrintObject, "Print::Object");
REGISTER_CLASS(PrintRegion, "Print::Region");
REGISTER_CLASS(Model, "Model");
REGISTER_CLASS(ModelMaterial, "Model::Material");
REGISTER_CLASS(ModelObject, "Model::Object");
REGISTER_CLASS(ModelVolume, "Model::Volume");
REGISTER_CLASS(ModelInstance, "Model::Instance");
REGISTER_CLASS(MotionPlanner, "MotionPlanner");
REGISTER_CLASS(BoundingBox, "Geometry::BoundingBox");
REGISTER_CLASS(BoundingBoxf, "Geometry::BoundingBoxf");
REGISTER_CLASS(BoundingBoxf3, "Geometry::BoundingBoxf3");
REGISTER_CLASS(BridgeDetector, "BridgeDetector");
REGISTER_CLASS(Point, "Point");
REGISTER_CLASS(Point3, "Point3");
REGISTER_CLASS(Pointf, "Pointf");
REGISTER_CLASS(Pointf3, "Pointf3");
REGISTER_CLASS(DynamicPrintConfig, "Config");
REGISTER_CLASS(StaticPrintConfig, "Config::Static");
REGISTER_CLASS(PrintObjectConfig, "Config::PrintObject");
REGISTER_CLASS(PrintRegionConfig, "Config::PrintRegion");
REGISTER_CLASS(GCodeConfig, "Config::GCode");
REGISTER_CLASS(PrintConfig, "Config::Print");
REGISTER_CLASS(FullPrintConfig, "Config::Full");
REGISTER_CLASS(Surface, "Surface");
REGISTER_CLASS(SurfaceCollection, "Surface::Collection");
REGISTER_CLASS(PrintObjectSupportMaterial, "Print::SupportMaterial2");
REGISTER_CLASS(TriangleMesh, "TriangleMesh");
REGISTER_CLASS(AppConfig, "GUI::AppConfig");
REGISTER_CLASS(GLShader, "GUI::_3DScene::GLShader");
REGISTER_CLASS(GLVolume, "GUI::_3DScene::GLVolume");
REGISTER_CLASS(GLVolumeCollection, "GUI::_3DScene::GLVolume::Collection");
REGISTER_CLASS(Preset, "GUI::Preset");
REGISTER_CLASS(PresetCollection, "GUI::PresetCollection");
REGISTER_CLASS(PresetBundle, "GUI::PresetBundle");
REGISTER_CLASS(TabIface, "GUI::Tab");
REGISTER_CLASS(PresetUpdater, "PresetUpdater");
REGISTER_CLASS(AppController, "AppController");
REGISTER_CLASS(PrintController, "PrintController");
REGISTER_CLASS(PrintHost, "PrintHost");
SV* ConfigBase__as_hash(ConfigBase* THIS)
{
HV* hv = newHV();
for (auto &key : THIS->keys())
(void)hv_store(hv, key.c_str(), key.length(), ConfigBase__get(THIS, key), 0);
return newRV_noinc((SV*)hv);
}
SV* ConfigBase__get(ConfigBase* THIS, const t_config_option_key &opt_key)
{
ConfigOption *opt = THIS->option(opt_key, false);
return (opt == nullptr) ?
&PL_sv_undef :
ConfigOption_to_SV(*opt, *THIS->def()->get(opt_key));
}
SV* ConfigOption_to_SV(const ConfigOption &opt, const ConfigOptionDef &def)
{
switch (def.type) {
case coFloat:
case coPercent:
return newSVnv(static_cast<const ConfigOptionFloat*>(&opt)->value);
case coFloats:
case coPercents:
{
auto optv = static_cast<const ConfigOptionFloats*>(&opt);
AV* av = newAV();
av_fill(av, optv->values.size()-1);
for (const double &v : optv->values)
av_store(av, &v - optv->values.data(), newSVnv(v));
return newRV_noinc((SV*)av);
}
case coInt:
return newSViv(static_cast<const ConfigOptionInt*>(&opt)->value);
case coInts:
{
auto optv = static_cast<const ConfigOptionInts*>(&opt);
AV* av = newAV();
av_fill(av, optv->values.size()-1);
for (const int &v : optv->values)
av_store(av, &v - optv->values.data(), newSViv(v));
return newRV_noinc((SV*)av);
}
case coString:
{
auto optv = static_cast<const ConfigOptionString*>(&opt);
// we don't serialize() because that would escape newlines
return newSVpvn_utf8(optv->value.c_str(), optv->value.length(), true);
}
case coStrings:
{
auto optv = static_cast<const ConfigOptionStrings*>(&opt);
AV* av = newAV();
av_fill(av, optv->values.size()-1);
for (const std::string &v : optv->values)
av_store(av, &v - optv->values.data(), newSVpvn_utf8(v.c_str(), v.length(), true));
return newRV_noinc((SV*)av);
}
case coPoint:
return perl_to_SV_clone_ref(static_cast<const ConfigOptionPoint*>(&opt)->value);
case coPoints:
{
auto optv = static_cast<const ConfigOptionPoints*>(&opt);
AV* av = newAV();
av_fill(av, optv->values.size()-1);
for (const Pointf &v : optv->values)
av_store(av, &v - optv->values.data(), perl_to_SV_clone_ref(v));
return newRV_noinc((SV*)av);
}
case coBool:
return newSViv(static_cast<const ConfigOptionBool*>(&opt)->value ? 1 : 0);
case coBools:
{
auto optv = static_cast<const ConfigOptionBools*>(&opt);
AV* av = newAV();
av_fill(av, optv->values.size()-1);
for (size_t i = 0; i < optv->values.size(); ++ i)
av_store(av, i, newSViv(optv->values[i] ? 1 : 0));
return newRV_noinc((SV*)av);
}
default:
std::string serialized = opt.serialize();
return newSVpvn_utf8(serialized.c_str(), serialized.length(), true);
}
}
SV* ConfigBase__get_at(ConfigBase* THIS, const t_config_option_key &opt_key, size_t i)
{
ConfigOption* opt = THIS->option(opt_key, false);
if (opt == nullptr)
return &PL_sv_undef;
const ConfigOptionDef* def = THIS->def()->get(opt_key);
switch (def->type) {
case coFloats:
case coPercents:
return newSVnv(static_cast<ConfigOptionFloats*>(opt)->get_at(i));
case coInts:
return newSViv(static_cast<ConfigOptionInts*>(opt)->get_at(i));
case coStrings:
{
// we don't serialize() because that would escape newlines
const std::string &val = static_cast<ConfigOptionStrings*>(opt)->get_at(i);
return newSVpvn_utf8(val.c_str(), val.length(), true);
}
case coPoints:
return perl_to_SV_clone_ref(static_cast<ConfigOptionPoints*>(opt)->get_at(i));
case coBools:
return newSViv(static_cast<ConfigOptionBools*>(opt)->get_at(i) ? 1 : 0);
default:
return &PL_sv_undef;
}
}
bool ConfigBase__set(ConfigBase* THIS, const t_config_option_key &opt_key, SV* value)
{
ConfigOption* opt = THIS->option(opt_key, true);
if (opt == nullptr)
CONFESS("Trying to set non-existing option");
const ConfigOptionDef* def = THIS->def()->get(opt_key);
if (opt->type() != def->type)
CONFESS("Option type is different from the definition");
switch (def->type) {
case coFloat:
if (!looks_like_number(value))
return false;
static_cast<ConfigOptionFloat*>(opt)->value = SvNV(value);
break;
case coFloats:
{
std::vector<double> &values = static_cast<ConfigOptionFloats*>(opt)->values;
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
values.clear();
values.reserve(len);
for (size_t i = 0; i < len; ++ i) {
SV** elem = av_fetch(av, i, 0);
if (elem == NULL || !looks_like_number(*elem)) return false;
values.emplace_back(SvNV(*elem));
}
break;
}
case coPercents:
{
std::vector<double> &values = static_cast<ConfigOptionPercents*>(opt)->values;
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
values.clear();
values.reserve(len);
for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0);
if (elem == NULL || !looks_like_number(*elem)) return false;
values.emplace_back(SvNV(*elem));
}
break;
}
case coInt:
if (!looks_like_number(value)) return false;
static_cast<ConfigOptionInt*>(opt)->value = SvIV(value);
break;
case coInts:
{
std::vector<int> &values = static_cast<ConfigOptionInts*>(opt)->values;
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
values.clear();
values.reserve(len);
for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0);
if (elem == NULL || !looks_like_number(*elem)) return false;
values.emplace_back(SvIV(*elem));
}
break;
}
case coString:
static_cast<ConfigOptionString*>(opt)->value = std::string(SvPV_nolen(value), SvCUR(value));
break;
case coStrings:
{
std::vector<std::string> &values = static_cast<ConfigOptionStrings*>(opt)->values;
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
values.clear();
values.reserve(len);
for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0);
if (elem == NULL) return false;
values.emplace_back(std::string(SvPV_nolen(*elem), SvCUR(*elem)));
}
break;
}
case coPoint:
return from_SV_check(value, &static_cast<ConfigOptionPoint*>(opt)->value);
case coPoints:
{
std::vector<Pointf> &values = static_cast<ConfigOptionPoints*>(opt)->values;
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
values.clear();
values.reserve(len);
for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0);
Pointf point;
if (elem == NULL || !from_SV_check(*elem, &point)) return false;
values.emplace_back(point);
}
break;
}
case coBool:
static_cast<ConfigOptionBool*>(opt)->value = SvTRUE(value);
break;
case coBools:
{
std::vector<unsigned char> &values = static_cast<ConfigOptionBools*>(opt)->values;
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
values.clear();
values.reserve(len);
for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0);
if (elem == NULL) return false;
values.emplace_back(SvTRUE(*elem));
}
break;
}
default:
if (! opt->deserialize(std::string(SvPV_nolen(value))))
return false;
}
return true;
}
/* This method is implemented as a workaround for this typemap bug:
https://rt.cpan.org/Public/Bug/Display.html?id=94110 */
bool ConfigBase__set_deserialize(ConfigBase* THIS, const t_config_option_key &opt_key, SV* str)
{
size_t len;
const char * c = SvPV(str, len);
std::string value(c, len);
return THIS->set_deserialize(opt_key, value);
}
void ConfigBase__set_ifndef(ConfigBase* THIS, const t_config_option_key &opt_key, SV* value, bool deserialize)
{
if (THIS->has(opt_key))
return;
if (deserialize)
ConfigBase__set_deserialize(THIS, opt_key, value);
else
ConfigBase__set(THIS, opt_key, value);
}
bool StaticConfig__set(StaticConfig* THIS, const t_config_option_key &opt_key, SV* value)
{
const ConfigOptionDef* optdef = THIS->def()->get(opt_key);
if (optdef->shortcut.empty())
return ConfigBase__set(THIS, opt_key, value);
for (const t_config_option_key &key : optdef->shortcut)
if (! StaticConfig__set(THIS, key, value))
return false;
return true;
}
SV* to_AV(ExPolygon* expolygon)
{
const unsigned int num_holes = expolygon->holes.size();
AV* av = newAV();
av_extend(av, num_holes); // -1 +1
av_store(av, 0, perl_to_SV_ref(expolygon->contour));
for (unsigned int i = 0; i < num_holes; i++) {
av_store(av, i+1, perl_to_SV_ref(expolygon->holes[i]));
}
return newRV_noinc((SV*)av);
}
SV* to_SV_pureperl(const ExPolygon* expolygon)
{
const unsigned int num_holes = expolygon->holes.size();
AV* av = newAV();
av_extend(av, num_holes); // -1 +1
av_store(av, 0, to_SV_pureperl(&expolygon->contour));
for (unsigned int i = 0; i < num_holes; i++) {
av_store(av, i+1, to_SV_pureperl(&expolygon->holes[i]));
}
return newRV_noinc((SV*)av);
}
void from_SV(SV* expoly_sv, ExPolygon* expolygon)
{
AV* expoly_av = (AV*)SvRV(expoly_sv);
const unsigned int num_polygons = av_len(expoly_av)+1;
expolygon->holes.resize(num_polygons-1);
SV** polygon_sv = av_fetch(expoly_av, 0, 0);
from_SV(*polygon_sv, &expolygon->contour);
for (unsigned int i = 0; i < num_polygons-1; i++) {
polygon_sv = av_fetch(expoly_av, i+1, 0);
from_SV(*polygon_sv, &expolygon->holes[i]);
}
}
void from_SV_check(SV* expoly_sv, ExPolygon* expolygon)
{
if (sv_isobject(expoly_sv) && (SvTYPE(SvRV(expoly_sv)) == SVt_PVMG)) {
if (!sv_isa(expoly_sv, perl_class_name(expolygon)) && !sv_isa(expoly_sv, perl_class_name_ref(expolygon)))
CONFESS("Not a valid %s object", perl_class_name(expolygon));
// a XS ExPolygon was supplied
*expolygon = *(ExPolygon *)SvIV((SV*)SvRV( expoly_sv ));
} else {
// a Perl arrayref was supplied
from_SV(expoly_sv, expolygon);
}
}
void from_SV(SV* line_sv, Line* THIS)
{
AV* line_av = (AV*)SvRV(line_sv);
from_SV_check(*av_fetch(line_av, 0, 0), &THIS->a);
from_SV_check(*av_fetch(line_av, 1, 0), &THIS->b);
}
void from_SV_check(SV* line_sv, Line* THIS)
{
if (sv_isobject(line_sv) && (SvTYPE(SvRV(line_sv)) == SVt_PVMG)) {
if (!sv_isa(line_sv, perl_class_name(THIS)) && !sv_isa(line_sv, perl_class_name_ref(THIS)))
CONFESS("Not a valid %s object", perl_class_name(THIS));
*THIS = *(Line*)SvIV((SV*)SvRV( line_sv ));
} else {
from_SV(line_sv, THIS);
}
}
SV* to_AV(Line* THIS)
{
AV* av = newAV();
av_extend(av, 1);
av_store(av, 0, perl_to_SV_ref(THIS->a));
av_store(av, 1, perl_to_SV_ref(THIS->b));
return newRV_noinc((SV*)av);
}
SV* to_SV_pureperl(const Line* THIS)
{
AV* av = newAV();
av_extend(av, 1);
av_store(av, 0, to_SV_pureperl(&THIS->a));
av_store(av, 1, to_SV_pureperl(&THIS->b));
return newRV_noinc((SV*)av);
}
void from_SV(SV* poly_sv, MultiPoint* THIS)
{
AV* poly_av = (AV*)SvRV(poly_sv);
const unsigned int num_points = av_len(poly_av)+1;
THIS->points.resize(num_points);
for (unsigned int i = 0; i < num_points; i++) {
SV** point_sv = av_fetch(poly_av, i, 0);
from_SV_check(*point_sv, &THIS->points[i]);
}
}
void from_SV_check(SV* poly_sv, MultiPoint* THIS)
{
if (sv_isobject(poly_sv) && (SvTYPE(SvRV(poly_sv)) == SVt_PVMG)) {
*THIS = *(MultiPoint*)SvIV((SV*)SvRV( poly_sv ));
} else {
from_SV(poly_sv, THIS);
}
}
SV* to_AV(MultiPoint* THIS)
{
const unsigned int num_points = THIS->points.size();
AV* av = newAV();
if (num_points > 0) av_extend(av, num_points-1);
for (unsigned int i = 0; i < num_points; i++) {
av_store(av, i, perl_to_SV_ref(THIS->points[i]));
}
return newRV_noinc((SV*)av);
}
SV* to_SV_pureperl(const MultiPoint* THIS)
{
const unsigned int num_points = THIS->points.size();
AV* av = newAV();
if (num_points > 0) av_extend(av, num_points-1);
for (unsigned int i = 0; i < num_points; i++) {
av_store(av, i, to_SV_pureperl(&THIS->points[i]));
}
return newRV_noinc((SV*)av);
}
void from_SV_check(SV* poly_sv, Polygon* THIS)
{
if (sv_isobject(poly_sv) && !sv_isa(poly_sv, perl_class_name(THIS)) && !sv_isa(poly_sv, perl_class_name_ref(THIS)))
CONFESS("Not a valid %s object", perl_class_name(THIS));
from_SV_check(poly_sv, (MultiPoint*)THIS);
}
void from_SV_check(SV* poly_sv, Polyline* THIS)
{
if (!sv_isa(poly_sv, perl_class_name(THIS)) && !sv_isa(poly_sv, perl_class_name_ref(THIS)))
CONFESS("Not a valid %s object", perl_class_name(THIS));
from_SV_check(poly_sv, (MultiPoint*)THIS);
}
SV* to_SV_pureperl(const Point* THIS)
{
AV* av = newAV();
av_fill(av, 1);
av_store(av, 0, newSViv(THIS->x));
av_store(av, 1, newSViv(THIS->y));
return newRV_noinc((SV*)av);
}
void from_SV(SV* point_sv, Point* point)
{
AV* point_av = (AV*)SvRV(point_sv);
// get a double from Perl and round it, otherwise
// it would get truncated
point->x = lrint(SvNV(*av_fetch(point_av, 0, 0)));
point->y = lrint(SvNV(*av_fetch(point_av, 1, 0)));
}
void from_SV_check(SV* point_sv, Point* point)
{
if (sv_isobject(point_sv) && (SvTYPE(SvRV(point_sv)) == SVt_PVMG)) {
if (!sv_isa(point_sv, perl_class_name(point)) && !sv_isa(point_sv, perl_class_name_ref(point)))
CONFESS("Not a valid %s object (got %s)", perl_class_name(point), HvNAME(SvSTASH(SvRV(point_sv))));
*point = *(Point*)SvIV((SV*)SvRV( point_sv ));
} else {
from_SV(point_sv, point);
}
}
SV* to_SV_pureperl(const Pointf* point)
{
AV* av = newAV();
av_fill(av, 1);
av_store(av, 0, newSVnv(point->x));
av_store(av, 1, newSVnv(point->y));
return newRV_noinc((SV*)av);
}
bool from_SV(SV* point_sv, Pointf* point)
{
AV* point_av = (AV*)SvRV(point_sv);
SV* sv_x = *av_fetch(point_av, 0, 0);
SV* sv_y = *av_fetch(point_av, 1, 0);
if (!looks_like_number(sv_x) || !looks_like_number(sv_y)) return false;
point->x = SvNV(sv_x);
point->y = SvNV(sv_y);
return true;
}
bool from_SV_check(SV* point_sv, Pointf* point)
{
if (sv_isobject(point_sv) && (SvTYPE(SvRV(point_sv)) == SVt_PVMG)) {
if (!sv_isa(point_sv, perl_class_name(point)) && !sv_isa(point_sv, perl_class_name_ref(point)))
CONFESS("Not a valid %s object (got %s)", perl_class_name(point), HvNAME(SvSTASH(SvRV(point_sv))));
*point = *(Pointf*)SvIV((SV*)SvRV( point_sv ));
return true;
} else {
return from_SV(point_sv, point);
}
}
void from_SV_check(SV* surface_sv, Surface* THIS)
{
if (!sv_isa(surface_sv, perl_class_name(THIS)) && !sv_isa(surface_sv, perl_class_name_ref(THIS)))
CONFESS("Not a valid %s object", perl_class_name(THIS));
// a XS Surface was supplied
*THIS = *(Surface *)SvIV((SV*)SvRV( surface_sv ));
}
SV* to_SV(TriangleMesh* THIS)
{
SV* sv = newSV(0);
sv_setref_pv( sv, perl_class_name(THIS), (void*)THIS );
return sv;
}
}
#endif
|