Allow referencing a vector without explicitly specify the extruder id

This commit is contained in:
Noisyfox
2025-09-28 10:51:40 +08:00
parent 7de52de353
commit 81978dc558

View File

@@ -1093,11 +1093,33 @@ namespace client
return; return;
assert(opt.opt->is_vector()); assert(opt.opt->is_vector());
if (! opt.has_index())
ctx->throw_exception("Referencing a vector variable when scalar is expected", opt.it_range);
const ConfigOptionVectorBase* vec = static_cast<const ConfigOptionVectorBase*>(opt.opt); const ConfigOptionVectorBase* vec = static_cast<const ConfigOptionVectorBase*>(opt.opt);
if (vec->empty()) if (vec->empty())
ctx->throw_exception("Indexing an empty vector variable", opt.it_range); ctx->throw_exception("Indexing an empty vector variable", opt.it_range);
if (!opt.has_index()) {
// Allow omitting extruder id when referencing vectors
switch (opt.opt->type()) {
case coFloats: {
const ConfigOptionFloatsNullable* opt_floatsnullable = static_cast<const ConfigOptionFloatsNullable *>(opt.opt);
if (opt_floatsnullable) {
if (opt_floatsnullable->size() == 1) { // old version
output.set_d(static_cast<const ConfigOptionFloatsNullable*>(opt.opt)->get_at(0));
} else {
output.set_d(static_cast<const ConfigOptionFloatsNullable*>(opt.opt)->get_at(ctx->get_extruder_id()));
}
} else {
const ConfigOptionFloats* opt_floats = static_cast<const ConfigOptionFloats*>(opt.opt);
if (opt_floats->size() == 1) { // old version
output.set_d(static_cast<const ConfigOptionFloats*>(opt.opt)->get_at(0));
} else {
output.set_d(static_cast<const ConfigOptionFloats*>(opt.opt)->get_at(ctx->get_extruder_id()));
}
}
break;
}
default: ctx->throw_exception("Referencing a vector variable when scalar is expected", opt.it_range);
}
} else {
size_t idx = (opt.index < 0) ? 0 : (opt.index >= int(vec->size())) ? 0 : size_t(opt.index); size_t idx = (opt.index < 0) ? 0 : (opt.index >= int(vec->size())) ? 0 : size_t(opt.index);
if (vec->is_nil(idx)) if (vec->is_nil(idx))
ctx->throw_exception("Trying to reference an undefined (nil) element of vector of optional values", opt.it_range); ctx->throw_exception("Trying to reference an undefined (nil) element of vector of optional values", opt.it_range);
@@ -1113,6 +1135,7 @@ namespace client
ctx->throw_exception("Unsupported vector variable type", opt.it_range); ctx->throw_exception("Unsupported vector variable type", opt.it_range);
} }
} }
}
static void check_writable(const MyContext *ctx, OptWithPos &opt) { static void check_writable(const MyContext *ctx, OptWithPos &opt) {
if (! opt.writable) if (! opt.writable)