Skip to content

Commit

Permalink
Editing of Custom G-code: Partially automation to get a list of place…
Browse files Browse the repository at this point in the history
…holders for custom gcodes

+ Added missed resources
  • Loading branch information
YuSanka committed Aug 29, 2023
1 parent 59af551 commit 55d5921
Show file tree
Hide file tree
Showing 20 changed files with 444 additions and 113 deletions.
4 changes: 4 additions & 0 deletions resources/custom_gcodes/before_layer_gcode
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# generated by PrusaSlicer 2.6.0-rc1 on 2023-06-15 at 13:29:08 UTC
layer_num = 2
layer_z = 1
max_layer_z = 1
5 changes: 5 additions & 0 deletions resources/custom_gcodes/end_filament_gcode
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# generated by PrusaSlicer 2.6.0-rc1 on 2023-06-15 at 13:29:09 UTC
filament_extruder_id = 2
layer_num = 2
layer_z = 1
max_layer_z = 1
5 changes: 5 additions & 0 deletions resources/custom_gcodes/end_gcode
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# generated by PrusaSlicer 2.6.0-rc1 on 2023-06-15 at 13:29:09 UTC
filament_extruder_id = 2
layer_num = 2
layer_z = 1
max_layer_z = 1
4 changes: 4 additions & 0 deletions resources/custom_gcodes/layer_gcode
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# generated by PrusaSlicer 2.6.0-rc1 on 2023-06-15 at 13:29:09 UTC
layer_num = 2
layer_z = 1
max_layer_z = 1
4 changes: 4 additions & 0 deletions resources/custom_gcodes/rw_slicing_state
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# generated by PrusaSlicer 2.6.0-rc1 on 2023-06-15 at 13:29:09 UTC
e_restart_extra = 16385
e_retracted = 16385
position = 16385
5 changes: 5 additions & 0 deletions resources/custom_gcodes/start_filament_gcode
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# generated by PrusaSlicer 2.6.0-rc1 on 2023-06-15 at 13:29:09 UTC
filament_extruder_id = 2
layer_num = 2
layer_z = 1
max_layer_z = 1
3 changes: 3 additions & 0 deletions resources/custom_gcodes/tcr_rotated_gcode
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# generated by PrusaSlicer 2.6.0-rc1 on 2023-06-14 at 11:40:58 UTC
deretraction_from_wipe_tower_generator = 3
toolchange_gcode = 3
7 changes: 7 additions & 0 deletions resources/custom_gcodes/toolchange_gcode
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# generated by PrusaSlicer 2.6.0-rc1 on 2023-06-14 at 11:40:58 UTC
layer_num = 2
layer_z = 1
max_layer_z = 1
next_extruder = 2
previous_extruder = 2
toolchange_z = 1
39 changes: 39 additions & 0 deletions resources/custom_gcodes/universal
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# generated by PrusaSlicer 2.6.0-rc1 on 2023-06-15 at 13:29:09 UTC
current_extruder = 2
current_object_idx = 2
day = 2
extruded_volume = 16385
extruded_volume_total = 1
extruded_weight = 16385
extruded_weight_total = 1
filament_preset = 16387
first_layer_print_convex_hull = 16390
first_layer_print_max = 16385
first_layer_print_min = 16385
first_layer_print_size = 16385
has_single_extruder_multi_material_priming = 8
has_wipe_tower = 8
hour = 2
initial_extruder = 2
initial_tool = 2
input_filename = 3
input_filename_base = 3
is_extruder_used = 16392
minute = 2
month = 2
num_extruders = 2
num_instances = 2
num_objects = 2
physical_printer_preset = 3
print_bed_max = 16385
print_bed_min = 16385
print_bed_size = 16385
print_preset = 3
printer_preset = 3
scale = 16387
second = 2
timestamp = 3
total_layer_count = 2
total_toolchanges = 2
year = 2
zhop = 1
21 changes: 21 additions & 0 deletions resources/icons/scalar_param.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions resources/icons/vector_filament_param.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions resources/icons/vector_param.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/PrusaSlicer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ bool CLI::setup(int argc, char **argv)
set_var_dir((path_resources / "icons").string());
set_local_dir((path_resources / "localization").string());
set_sys_shapes_dir((path_resources / "shapes").string());
set_custom_gcodes_dir((path_resources / "custom_gcodes").string());

// Parse all command line options into a DynamicConfig.
// If any option is unsupported, print usage and abort immediately.
Expand Down
6 changes: 6 additions & 0 deletions src/libslic3r/GCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,12 @@ 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;
#endif

PlaceholderParserIntegration &ppi = m_placeholder_parser_integration;
try {
ppi.update_from_gcodewriter(m_writer);
Expand Down
9 changes: 9 additions & 0 deletions src/libslic3r/GCode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ struct LayerResult {
};

class GCodeGenerator {
#define GET_CUSTOM_GCODE_PLACEHOLDERS 1

public:
GCodeGenerator() :
m_origin(Vec2d::Zero()),
Expand All @@ -97,6 +99,13 @@ 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: 40 additions & 0 deletions src/libslic3r/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,46 @@ 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
5 changes: 5 additions & 0 deletions src/libslic3r/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ const std::string& sys_shapes_dir();
// Return a full path to the custom shapes gallery directory.
std::string custom_shapes_dir();

// Set a path with shapes gallery files.
void set_custom_gcodes_dir(const std::string &path);
// Return a full path to the system shapes gallery directory.
const std::string& custom_gcodes_dir();

// Set a path with preset files.
void set_data_dir(const std::string &path);
// Return a full path to the GUI resource files.
Expand Down
12 changes: 12 additions & 0 deletions src/libslic3r/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ const std::string& sys_shapes_dir()
return g_sys_shapes_dir;
}

static std::string g_custom_gcodes_dir;

void set_custom_gcodes_dir(const std::string &dir)
{
g_custom_gcodes_dir = dir;
}

const std::string& custom_gcodes_dir()
{
return g_custom_gcodes_dir;
}

// Translate function callback, to call wxWidgets translate function to convert non-localized UTF8 string to a localized one.
Slic3r::I18N::translate_fn_type Slic3r::I18N::translate_fn = nullptr;

Expand Down
Loading

0 comments on commit 55d5921

Please sign in to comment.