Skip to content

Commit

Permalink
allow scripted option to be disabled & searchable
Browse files Browse the repository at this point in the history
set s_seam_position to disable if no perimeters.
add is_enabled as script function (still experimental)
  • Loading branch information
supermerill committed Dec 14, 2023
1 parent 975b5d8 commit 3c97b61
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 102 deletions.
2 changes: 2 additions & 0 deletions resources/ui_layout/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ These functions can only be called in a `set` or `reset` function. If you need t
### others
* void **ask_for_refresh**()
ask for a OPTNAME_set() after the current OPTNAME_get(), to be able to set settings.
* bool **is_enabled**(string setting_key)
Experimental. Ask if this setting is currently enabled. Dangerous, as it will be true if it's not constructed.

### to get/set the value of a custom variable
The first argument is the index of the tab setting: 0 for print settings, 1 for filament settings and 2 for printer settings.
Expand Down
5 changes: 5 additions & 0 deletions resources/ui_layout/default/print.as
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ void s_seam_position_set(string &in set_val, int idx)
}
}

bool s_seam_position_is_enabled()
{
return get_int("perimeters") > 0;
}

// s_wall_thickness
// set the perimeter_spacing & external_perimeter_spacing
// as m * 2 perimeter_spacing + n * 2 * external_perimeter_spacing = o * s_wall_thickness
Expand Down
13 changes: 12 additions & 1 deletion src/libslic3r/Preset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,21 @@ class Preset
TYPE_SLA_MATERIAL = TYPE_SLA | TYPE_MATERIAL,
TYPE_TECHNOLOGY = TYPE_FFF | TYPE_SLA,

TYPE_FREQUENT = 1 << 5,
TYPE_FREQUENT_FFF = TYPE_FFF | TYPE_FREQUENT,
TYPE_FREQUENT_SLA = TYPE_SLA | TYPE_FREQUENT,
// This type is here to support PresetConfigSubstitutions for physical printers, however it does not belong to the Preset class,
// PhysicalPrinter class is used instead.
TYPE_PHYSICAL_PRINTER = 1 << 5,
TYPE_PHYSICAL_PRINTER = 1 << 6,
};
static inline PrinterTechnology get_tech(Type type)
{
if ((type & TYPE_FFF) == TYPE_FFF)
return PrinterTechnology::ptFFF;
if ((type & TYPE_FFF) == TYPE_SLA)
return PrinterTechnology::ptSLA;
return PrinterTechnology::ptUnknown;
}
static std::string type_name(Type t);

Type type = TYPE_INVALID;
Expand Down
36 changes: 18 additions & 18 deletions src/slic3r/GUI/CreateMMUTiledCanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,31 +1113,31 @@ void CreateMMUTiledCanvas::create_main_tab(wxPanel* tab)
//group_size->append_single_option_line(option);

//line = { L("Size"), "" };
group_size->append_single_option_line(group_size->get_option("size"));
group_size->append_single_option_line(group_size->create_option_from_def("size"));
//line.append_option(Option(def, "size"));

group_size->append_single_option_line(group_size->get_option("size_px"));
group_size->append_single_option_line(group_size->create_option_from_def("size_px"));
//line.append_option(Option(def, "size_px"));

//group_size->append_line(line);

group_size->append_single_option_line(group_size->get_option("height"));
group_size->append_single_option_line(group_size->create_option_from_def("height"));

group_size->append_single_option_line(group_size->get_option("offset"));
group_size->append_single_option_line(group_size->create_option_from_def("offset"));

line = { L("Gap"), "" };

line.append_option(group_size->get_option("separation_xy"));
line.append_option(group_size->create_option_from_def("separation_xy"));

line.append_option(group_size->get_option("separation_z"));
line.append_option(group_size->create_option_from_def("separation_z"));

group_size->append_line(line);

group_size->append_single_option_line(group_size->get_option("bezel"));
group_size->append_single_option_line(group_size->get_option("border"));
group_size->append_single_option_line(group_size->create_option_from_def("bezel"));
group_size->append_single_option_line(group_size->create_option_from_def("border"));


//group_size->append_single_option_line(group_size->get_option("bump"));
//group_size->append_single_option_line(group_size->create_option_from_def("bump"));

group_size->activate([]() {}, wxALIGN_RIGHT);
group_size->reload_config();
Expand All @@ -1156,23 +1156,23 @@ void CreateMMUTiledCanvas::create_main_tab(wxPanel* tab)
};
group_colors->title_width = 15;

group_colors->append_single_option_line(group_colors->get_option("spool_colors"));
group_colors->append_single_option_line(group_colors->create_option_from_def("spool_colors"));

//line = { L("Separation"), "" };
//line.append_option(group_colors->get_option("near_color"));
//line.append_option(group_colors->get_option("color_comp"));
//line.append_option(group_colors->create_option_from_def("near_color"));
//line.append_option(group_colors->create_option_from_def("color_comp"));
//group_colors->append_line(line);
group_colors->append_single_option_line(group_colors->get_option("near_color"));
group_colors->append_single_option_line(group_colors->create_option_from_def("near_color"));

group_colors->append_single_option_line(group_colors->get_option("color_comp"));
group_colors->append_single_option_line(group_colors->create_option_from_def("color_comp"));

group_colors->append_single_option_line(group_colors->get_option("order_dark"));
group_colors->append_single_option_line(group_colors->create_option_from_def("order_dark"));

group_colors->append_single_option_line(group_colors->get_option("original"));
group_colors->append_single_option_line(group_colors->create_option_from_def("original"));

group_colors->append_single_option_line(group_colors->get_option("extruders"));
group_colors->append_single_option_line(group_colors->create_option_from_def("extruders"));

group_colors->append_single_option_line(group_colors->get_option("background_color"));
group_colors->append_single_option_line(group_colors->create_option_from_def("background_color"));

//line = { "", "" };
//line.full_width = 1;
Expand Down
11 changes: 9 additions & 2 deletions src/slic3r/GUI/Field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,16 @@ class Field {
// This is used to avoid recursive invocation of the field change/update by wxWidgets.
bool m_disable_change_event {false};
bool m_is_modified_value {false};
bool m_is_nonsys_value {true};
bool m_is_nonsys_value{true};

/// Copy of ConfigOption for deduction purposes
const ConfigOptionDef m_opt {ConfigOptionDef()};
const t_config_option_key m_opt_id;//! {""};
int m_opt_idx = 0;

// for saving state
bool m_is_enable{true};

double opt_height{ 0.0 };
bool parent_is_custom_ctrl{ false };

Expand All @@ -127,7 +130,11 @@ class Field {
virtual void disable() = 0;

/// Fires the enable or disable function, based on the input.
inline void toggle(bool en) { en ? enable() : disable(); }
inline void toggle(bool en) {
m_is_enable = en;
en ? enable() : disable();
}
inline bool is_enabled() const { return m_is_enable; }

virtual wxString get_tooltip_text(const wxString& default_string);
// hack via richtooltip that are also hacked
Expand Down
1 change: 1 addition & 0 deletions src/slic3r/GUI/GUI_ObjectSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "GUI_App.hpp"
#include "wxExtensions.hpp"
#include "Plater.hpp"
#include "ScriptExecutor.hpp"
#include "libslic3r/PresetBundle.hpp"
#include "libslic3r/Model.hpp"

Expand Down
25 changes: 17 additions & 8 deletions src/slic3r/GUI/OptionsGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,15 @@ void OptionsGroup::update_script_presets(bool init) {
}
}
}
bool ConfigOptionsGroup::has_option_def(const std::string &opt_key)
{
return this->m_options.find(opt_key) != this->m_options.end();
}
const Option *ConfigOptionsGroup::get_option_def(const std::string &opt_key)
{
auto it = this->m_options.find(opt_key);
return it == m_options.end() ? nullptr : &it->second;
}

bool ConfigOptionsGroup::has_option(const std::string& opt_key, int opt_index /*= -1*/)
{
Expand All @@ -614,17 +623,17 @@ bool ConfigOptionsGroup::has_option(const std::string& opt_key, int opt_index /*
return m_opt_map.find(opt_id) != m_opt_map.end();
}

Option ConfigOptionsGroup::get_option(const std::string& opt_key, int opt_index /*= -1*/)
Option ConfigOptionsGroup::create_option_from_def(const std::string &opt_key, int opt_index /*= -1*/)
{
if (!m_config->has(opt_key)) {
std::cerr << "No " << opt_key << " in ConfigOptionsGroup config.\n";
}
if (!m_config->has(opt_key)) {
std::cerr << "No " << opt_key << " in ConfigOptionsGroup config.\n";
}

std::string opt_id = opt_index == -1 ? opt_key : opt_key + "#" + std::to_string(opt_index);
std::pair<std::string, int> pair(opt_key, opt_index);
m_opt_map.emplace(opt_id, pair);
std::string opt_id = opt_index == -1 ? opt_key : opt_key + "#" + std::to_string(opt_index);
std::pair<std::string, int> pair(opt_key, opt_index);
m_opt_map.emplace(opt_id, pair);

return Option(*m_config->def()->get(opt_key), opt_id);
return Option(*m_config->def()->get(opt_key), opt_id);
}

void ConfigOptionsGroup::register_to_search(const std::string& opt_key, const ConfigOptionDef& option_def, int opt_index /*= -1*/, bool reset)
Expand Down
12 changes: 8 additions & 4 deletions src/slic3r/GUI/OptionsGroup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,16 @@ class ConfigOptionsGroup: public OptionsGroup {
void set_config(DynamicPrintConfig* config) {
m_config = config; m_modelconfig = nullptr;
}
bool has_option(const std::string& opt_key, int opt_index = -1);
// more like "create option from def"
Option get_option(const std::string& opt_key, int opt_index = -1);

bool has_option_def(const std::string &opt_key);
const Option* get_option_def(const std::string &opt_key);
//these 'has' and 'get' are about m_opt_map and not m_options. it's the option + id
bool has_option(const std::string &opt_key, int opt_index = -1);
// more like "create option from def" (old "get_option")
Option create_option_from_def(const std::string& opt_key, int opt_index = -1);
void register_to_search(const std::string& opt_key, const ConfigOptionDef& option_def, int opt_index, bool reset);
Option get_option_and_register(const std::string& opt_key, int opt_index = -1) {
Option opt = get_option(opt_key, opt_index);
Option opt = create_option_from_def(opt_key, opt_index);
if(m_use_custom_ctrl) // fill group and category values just for options from Settings Tab
register_to_search(opt_key, opt.opt, opt_index, true);
return opt;
Expand Down
16 changes: 8 additions & 8 deletions src/slic3r/GUI/PhysicalPrinterDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ void PhysicalPrinterDialog::build_printhost_settings(ConfigOptionsGroup* m_optgr
};

// Set a wider width for a better alignment
Option option = m_optgroup->get_option("print_host");
Option option = m_optgroup->create_option_from_def("print_host");
option.opt.width = Field::def_width_wider();
Line host_line = m_optgroup->create_single_option_line(option);
host_line.append_widget(printhost_browse);
Expand All @@ -405,11 +405,11 @@ void PhysicalPrinterDialog::build_printhost_settings(ConfigOptionsGroup* m_optgr

m_optgroup->append_single_option_line("printhost_authorization_type");

option = m_optgroup->get_option("printhost_apikey");
option = m_optgroup->create_option_from_def("printhost_apikey");
option.opt.width = Field::def_width_wider();
m_optgroup->append_single_option_line(option);

option = m_optgroup->get_option("printhost_port");
option = m_optgroup->create_option_from_def("printhost_port");
option.opt.width = Field::def_width_wider();
Line port_line = m_optgroup->create_single_option_line(option);
port_line.append_widget(print_host_printers);
Expand All @@ -427,7 +427,7 @@ void PhysicalPrinterDialog::build_printhost_settings(ConfigOptionsGroup* m_optgr
m_optgroup->append_single_option_line(option);
}

option = m_optgroup->get_option("printhost_client_cert");
option = m_optgroup->create_option_from_def("printhost_client_cert");
option.opt.width = Field::def_width_wider();
Line client_cert_line = m_optgroup->create_single_option_line(option);

Expand Down Expand Up @@ -466,14 +466,14 @@ void PhysicalPrinterDialog::build_printhost_settings(ConfigOptionsGroup* m_optgr
};
m_optgroup->append_line(clientcert_hint);

option = m_optgroup->get_option("printhost_client_cert_password");
option = m_optgroup->create_option_from_def("printhost_client_cert_password");
option.opt.width = Field::def_width_wider();
m_optgroup->append_single_option_line(option);

const auto ca_file_hint = _u8L("HTTPS CA file is optional. It is only needed if you use HTTPS with a self-signed certificate.");

if (Http::ca_file_supported()) {
option = m_optgroup->get_option("printhost_cafile");
option = m_optgroup->create_option_from_def("printhost_cafile");
option.opt.width = Field::def_width_wider();
Line cafile_line = m_optgroup->create_single_option_line(option);

Expand Down Expand Up @@ -525,13 +525,13 @@ void PhysicalPrinterDialog::build_printhost_settings(ConfigOptionsGroup* m_optgr
}

for (const std::string& opt_key : std::vector<std::string>{ "printhost_user", "printhost_password" }) {
option = m_optgroup->get_option(opt_key);
option = m_optgroup->create_option_from_def(opt_key);
option.opt.width = Field::def_width_wider();
m_optgroup->append_single_option_line(option);
}

#ifdef WIN32
option = m_optgroup->get_option("printhost_ssl_ignore_revoke");
option = m_optgroup->create_option_from_def("printhost_ssl_ignore_revoke");
option.opt.width = Field::def_width_wider();
m_optgroup->append_single_option_line(option);
#endif
Expand Down
8 changes: 4 additions & 4 deletions src/slic3r/GUI/Plater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,14 @@ void FreqChangedParams::init()
};

std::vector<PageShp> pages;
if(tab_print != nullptr) pages = tab_print->create_pages("freq_fff.ui");
if(tab_print != nullptr) pages = tab_print->create_pages("freq_fff.ui", -1, Preset::Type::TYPE_FREQUENT_FFF);
if (!pages.empty()) {
m_og->set_config(config);
m_og->hide_labels();
m_og->m_on_change = Tab::set_or_add(m_og->m_on_change, [tab_print, this](t_config_option_key opt_key, boost::any value)
//m_og->m_on_change = [tab_print, this](t_config_option_key opt_key, boost::any value)
{
Option opt = this->m_og->get_option(opt_key);
Option opt = this->m_og->create_option_from_def(opt_key);
if (!opt.opt.is_script) {
tab_print->update_dirty();
tab_print->reload_config();
Expand Down Expand Up @@ -470,14 +470,14 @@ void FreqChangedParams::init()
// Frequently changed parameters for SLA_technology
tab_print = wxGetApp().get_tab(Preset::TYPE_SLA_PRINT);
pages.clear();
if (tab_print != nullptr) pages = tab_print->create_pages("freq_sla.ui");
if (tab_print != nullptr) pages = tab_print->create_pages("freq_sla.ui", -1, Preset::Type::TYPE_FREQUENT_SLA);
if (!pages.empty()) {
std::shared_ptr<ConfigOptionsGroup> m_og_sla = m_og_other[ptSLA] = std::make_shared<ConfigOptionsGroup>(m_parent, "");
m_og_sla->set_config(config);
m_og_sla->hide_labels();
m_og_sla->m_on_change = Tab::set_or_add(m_og_sla->m_on_change, [tab_print, this](t_config_option_key opt_key, boost::any value)
{
Option opt = this->m_og_other[ptSLA]->get_option(opt_key);
Option opt = this->m_og_other[ptSLA]->create_option_from_def(opt_key);
if (!opt.opt.is_script) {
tab_print->update_dirty();
tab_print->reload_config();
Expand Down
31 changes: 31 additions & 0 deletions src/slic3r/GUI/ScriptExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,15 @@ void as_ask_for_refresh()
current_script->request_refresh();
}

bool as_is_enabled(std::string &key)
{
Page *selected_page;
Field *f = current_script->tab()->get_field(selected_page, key, -1);
if (!f)
return true;
return f->is_enabled();
}

//function to reset a field
void as_back_initial_value(std::string& key) {
current_script->add_to_reset(key);
Expand Down Expand Up @@ -672,6 +681,7 @@ void ScriptContainer::init(const std::string& tab_key, Tab* tab)
m_script_engine.get()->RegisterGlobalFunction("void back_initial_value(string &in)", WRAP_FN(as_back_initial_value), AngelScript::asCALL_GENERIC);
m_script_engine.get()->RegisterGlobalFunction("void back_custom_initial_value(int, string &in)", WRAP_FN(as_back_custom_initial_value), AngelScript::asCALL_GENERIC);
m_script_engine.get()->RegisterGlobalFunction("void ask_for_refresh()", WRAP_FN(as_ask_for_refresh), AngelScript::asCALL_GENERIC);
m_script_engine.get()->RegisterGlobalFunction("bool is_enabled(string &in)", WRAP_FN(as_is_enabled), AngelScript::asCALL_GENERIC);

#else
m_script_engine.get()->RegisterGlobalFunction("void print(string &in)", AngelScript::asFUNCTION(as_print), AngelScript::asCALL_CDECL);
Expand Down Expand Up @@ -702,6 +712,7 @@ void ScriptContainer::init(const std::string& tab_key, Tab* tab)
m_script_engine.get()->RegisterGlobalFunction("void back_initial_value(string &in)", AngelScript::asFUNCTION(as_back_initial_value), AngelScript::asCALL_CDECL);
m_script_engine.get()->RegisterGlobalFunction("void back_custom_initial_value(int, string &in)", AngelScript::asFUNCTION(as_back_custom_initial_value), AngelScript::asCALL_CDECL);
m_script_engine.get()->RegisterGlobalFunction("void ask_for_refresh()", AngelScript::asFUNCTION(as_ask_for_refresh), AngelScript::asCALL_CDECL);
m_script_engine.get()->RegisterGlobalFunction("bool is_enabled(string &in)", AngelScript::asFUNCTION(as_is_enabled), AngelScript::asCALL_CDECL);
#endif
}

Expand Down Expand Up @@ -1086,6 +1097,26 @@ void ScriptContainer::refresh(const ConfigOptionDef& def, boost::any value)
m_currently_reset.erase(it);
}

bool ScriptContainer::call_script_function_is_enable(const ConfigOptionDef &def)
{
std::string func_name = ("bool " + def.opt_key + "_is_enabled()");
AngelScript::asIScriptFunction *func = m_script_module->GetFunctionByDecl(func_name.c_str());
if (func == nullptr) {
// default true
return true;
}
AngelScript::asIScriptContext *ctx = m_script_engine->CreateContext();
if (ctx == nullptr) {
BOOST_LOG_TRIVIAL(error) << "Error, can't create script context for function '" << func_name << "'";
return true;
}
ctx->Prepare(func);
// exec
/*int res = */ ctx->Execute();
uint8_t ret = ctx->GetReturnByte();
return ret != 0;
}

//TODO find a way to use the depends_on to add the same lock & points as real configoption in the gui

} }//namespace Slic3r Gui
Expand Down
1 change: 1 addition & 0 deletions src/slic3r/GUI/ScriptExecutor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ScriptContainer
bool call_script_function_reset(const ConfigOptionDef& def);
//void call_script_function_refresh(const std::string& def_id);
boost::any call_script_function_get_value(const ConfigOptionDef& def);
bool call_script_function_is_enable(const ConfigOptionDef &def);
};

} } //namespace Slic3r Gui
Expand Down
Loading

0 comments on commit 3c97b61

Please sign in to comment.