Skip to content

Commit

Permalink
Edit custom G-codes: Improvements
Browse files Browse the repository at this point in the history
* Prepared ConfigDefs for placeholders used in EditGCodeDialog.
* Removed unused code and files
* DEBUG mode only: Added check of placeholder's existence in custom_gcode_specific_placeholders and custom_gcode_specific_config_def during the custom G-code parsing.
  • Loading branch information
YuSanka authored and lukasmatena committed Sep 13, 2023
1 parent 7dcad1a commit b8bb7f2
Show file tree
Hide file tree
Showing 17 changed files with 484 additions and 349 deletions.
4 changes: 0 additions & 4 deletions resources/custom_gcodes/before_layer_gcode

This file was deleted.

5 changes: 0 additions & 5 deletions resources/custom_gcodes/end_filament_gcode

This file was deleted.

5 changes: 0 additions & 5 deletions resources/custom_gcodes/end_gcode

This file was deleted.

4 changes: 0 additions & 4 deletions resources/custom_gcodes/layer_gcode

This file was deleted.

4 changes: 0 additions & 4 deletions resources/custom_gcodes/rw_slicing_state

This file was deleted.

5 changes: 0 additions & 5 deletions resources/custom_gcodes/start_filament_gcode

This file was deleted.

3 changes: 0 additions & 3 deletions resources/custom_gcodes/tcr_rotated_gcode

This file was deleted.

7 changes: 0 additions & 7 deletions resources/custom_gcodes/toolchange_gcode

This file was deleted.

39 changes: 0 additions & 39 deletions resources/custom_gcodes/universal

This file was deleted.

3 changes: 3 additions & 0 deletions src/libslic3r/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,8 @@ class ConfigOptionDef
// Create a default option to be inserted into a DynamicConfig.
ConfigOption* create_default_option() const;

bool is_scalar() const { return (int(this->type) & int(coVectorType)) == 0; }

template<class Archive> ConfigOption* load_option_from_archive(Archive &archive) const {
if (this->nullable) {
switch (this->type) {
Expand Down Expand Up @@ -2101,6 +2103,7 @@ class ConfigDef
out.push_back(kvp.first);
return out;
}
bool empty() { return options.empty(); }

// Iterate through all of the CLI options and write them to a stream.
std::ostream& print_cli_help(
Expand Down
28 changes: 24 additions & 4 deletions src/libslic3r/GCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,10 +1625,30 @@ std::string GCodeGenerator::placeholder_parser_process(
unsigned int current_extruder_id,
const DynamicConfig *config_override)
{
#if GET_CUSTOM_GCODE_PLACEHOLDERS
if (config_override &&
g_code_placeholders_map.find(name) == g_code_placeholders_map.end())
g_code_placeholders_map[name] = *config_override;
#ifndef NDEBUG // CHECK_CUSTOM_GCODE_PLACEHOLDERS
if (config_override) {
const auto& custom_gcode_placeholders = custom_gcode_specific_placeholders();

// 1-st check: custom G-code "name" have to be present in s_CustomGcodeSpecificOptions;
//if (custom_gcode_placeholders.count(name) > 0) {
// const auto& placeholders = custom_gcode_placeholders.at(name);
if (auto it = custom_gcode_placeholders.find(name); it != custom_gcode_placeholders.end()) {
const auto& placeholders = it->second;

for (const std::string& key : config_override->keys()) {
// 2-nd check: "key" have to be present in s_CustomGcodeSpecificOptions for "name" custom G-code ;
if (std::find(placeholders.begin(), placeholders.end(), key) == placeholders.end())
throw Slic3r::PlaceholderParserError(format("\"%s\" placeholder for \"%s\" custom G-code \n"
"needs to be added to s_CustomGcodeSpecificOptions", key.c_str(), name.c_str()));
// 3-rd check: "key" have to be present in CustomGcodeSpecificConfigDef for "key" placeholder;
if (!custom_gcode_specific_config_def.has(key))
throw Slic3r::PlaceholderParserError(format("Definition of \"%s\" placeholder \n"
"needs to be added to CustomGcodeSpecificConfigDef", key.c_str()));
}
}
else
throw Slic3r::PlaceholderParserError(format("\"%s\" custom G-code needs to be added to s_CustomGcodeSpecificOptions", name.c_str()));
}
#endif

PlaceholderParserIntegration &ppi = m_placeholder_parser_integration;
Expand Down
8 changes: 0 additions & 8 deletions src/libslic3r/GCode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ struct LayerResult {
static LayerResult make_nop_layer_result() { return {"", std::numeric_limits<coord_t>::max(), false, false, true}; }
};

#define GET_CUSTOM_GCODE_PLACEHOLDERS 0
class GCodeGenerator {

public:
Expand Down Expand Up @@ -115,13 +114,6 @@ class GCodeGenerator {
{}
~GCodeGenerator() = default;

#if GET_CUSTOM_GCODE_PLACEHOLDERS
std::map<std::string, DynamicConfig> g_code_placeholders_map;
const std::map<std::string, DynamicConfig>& get_g_code_placeholders_map() { return g_code_placeholders_map; }
const DynamicConfig& get_placeholder_parser_config() const { return m_placeholder_parser_integration.parser.config(); }
const DynamicConfig& get_placeholder_output_config() const { return m_placeholder_parser_integration.output_config; }
#endif

// throws std::runtime_exception on error,
// throws CanceledException through print->throw_if_canceled().
void do_export(Print* print, const char* path, GCodeProcessorResult* result = nullptr, ThumbnailsGeneratorCallback thumbnail_cb = nullptr);
Expand Down
40 changes: 0 additions & 40 deletions src/libslic3r/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,46 +1048,6 @@ std::string Print::export_gcode(const std::string& path_template, GCodeProcessor
std::unique_ptr<GCodeGenerator> gcode(new GCodeGenerator);
gcode->do_export(this, path.c_str(), result, thumbnail_cb);


#if GET_CUSTOM_GCODE_PLACEHOLDERS

const std::string dir = custom_gcodes_dir() +
#ifdef _WIN32
"\\";
#else
"/";
#endif

auto save_placeholders = [dir](const std::string& file_name, const DynamicConfig& config) {
try {
boost::nowide::ofstream c;
c.open(dir + file_name, std::ios::out | std::ios::trunc);
c << "# " << header_slic3r_generated() << std::endl;
auto keys = config.keys();
for (const std::string& opt_key : keys) {
const std::string type = std::to_string(int(config.optptr(opt_key)->type()));
c << opt_key << " = " << type << std::endl;
}
c.close();
}
catch (const std::ofstream::failure& err) {
throw RuntimeError(format("The %1% cannot be loaded:\n\tReason: %2%", file_name, err.what()));
}
};

// save specific placeholders
const auto& gcode_placeholders = gcode->get_g_code_placeholders_map();
for (const auto& [gcode_name, config] : gcode_placeholders)
save_placeholders(gcode_name, config);

// save universal placeholders
save_placeholders("universal", gcode->get_placeholder_parser_config());

// save placeholders for "rw_slicing_state" slicing state
save_placeholders("rw_slicing_state", gcode->get_placeholder_output_config());

#endif

if (m_conflict_result.has_value())
result->conflict_result = *m_conflict_result;

Expand Down
Loading

0 comments on commit b8bb7f2

Please sign in to comment.