ENABLE_GCODE_VIEWER -> Drag and drop .gcode files into gcode viewer

This commit is contained in:
enricoturri1966 2020-08-11 14:23:47 +02:00
parent 5882c121cc
commit 5a0e048079
4 changed files with 76 additions and 66 deletions

View File

@ -1283,7 +1283,7 @@ void GCode::_do_export(Print& print, FILE* file, ThumbnailsGeneratorCallback thu
// adds tags for time estimators
#if ENABLE_GCODE_VIEWER
if (print.config().remaining_times.value)
_writeln(file, GCodeProcessor::First_M73_Output_Placeholder_Tag);
_writeln(file, GCodeProcessor::First_Line_M73_Placeholder_Tag);
#else
if (print.config().remaining_times.value) {
_writeln(file, GCodeTimeEstimator::Normal_First_M73_Output_Placeholder_Tag);
@ -1587,7 +1587,7 @@ void GCode::_do_export(Print& print, FILE* file, ThumbnailsGeneratorCallback thu
// adds tags for time estimators
#if ENABLE_GCODE_VIEWER
if (print.config().remaining_times.value)
_writeln(file, GCodeProcessor::Last_M73_Output_Placeholder_Tag);
_writeln(file, GCodeProcessor::Last_Line_M73_Placeholder_Tag);
#else
if (print.config().remaining_times.value) {
_writeln(file, GCodeTimeEstimator::Normal_Last_M73_Output_Placeholder_Tag);
@ -1620,7 +1620,9 @@ void GCode::_do_export(Print& print, FILE* file, ThumbnailsGeneratorCallback thu
_write_format(file, "; total filament cost = %.1lf\n", print.m_print_statistics.total_cost);
if (print.m_print_statistics.total_toolchanges > 0)
_write_format(file, "; total toolchanges = %i\n", print.m_print_statistics.total_toolchanges);
#if !ENABLE_GCODE_VIEWER
#if ENABLE_GCODE_VIEWER
_writeln(file, GCodeProcessor::Estimated_Printing_Time_Placeholder_Tag);
#else
_write_format(file, "; estimated printing time (normal mode) = %s\n", m_normal_time_estimator.get_time_dhms().c_str());
if (m_silent_time_estimator_enabled)
_write_format(file, "; estimated printing time (silent mode) = %s\n", m_silent_time_estimator.get_time_dhms().c_str());

View File

@ -29,8 +29,9 @@ const std::string GCodeProcessor::Color_Change_Tag = "Color change";
const std::string GCodeProcessor::Pause_Print_Tag = "Pause print";
const std::string GCodeProcessor::Custom_Code_Tag = "Custom gcode";
const std::string GCodeProcessor::First_M73_Output_Placeholder_Tag = "; _GP_FIRST_M73_OUTPUT_PLACEHOLDER";
const std::string GCodeProcessor::Last_M73_Output_Placeholder_Tag = "; _GP_LAST_M73_OUTPUT_PLACEHOLDER";
const std::string GCodeProcessor::First_Line_M73_Placeholder_Tag = "; _GP_FIRST_LINE_M73_PLACEHOLDER";
const std::string GCodeProcessor::Last_Line_M73_Placeholder_Tag = "; _GP_LAST_LINE_M73_PLACEHOLDER";
const std::string GCodeProcessor::Estimated_Printing_Time_Placeholder_Tag = "; _GP_ESTIMATED_PRINTING_TIME_PLACEHOLDER";
static bool is_valid_extrusion_role(int value)
{
@ -338,16 +339,29 @@ void GCodeProcessor::TimeProcessor::post_process(const std::string& filename)
// replace placeholder lines with the proper final value
auto process_placeholders = [&](const std::string& gcode_line) {
std::string ret;
// remove trailing '\n'
std::string line = gcode_line.substr(0, gcode_line.length() - 1);
if (line == First_M73_Output_Placeholder_Tag || line == Last_M73_Output_Placeholder_Tag) {
std::string ret;
if (line == First_Line_M73_Placeholder_Tag || line == Last_Line_M73_Placeholder_Tag) {
for (size_t i = 0; i < static_cast<size_t>(ETimeMode::Count); ++i) {
const TimeMachine& machine = machines[i];
if (machine.enabled) {
ret += format_line_M73(machine.line_m73_mask.c_str(),
(line == First_M73_Output_Placeholder_Tag) ? 0 : 100,
(line == First_M73_Output_Placeholder_Tag) ? time_in_minutes(machines[i].time) : 0);
(line == First_Line_M73_Placeholder_Tag) ? 0 : 100,
(line == First_Line_M73_Placeholder_Tag) ? time_in_minutes(machines[i].time) : 0);
}
}
}
else if (line == Estimated_Printing_Time_Placeholder_Tag) {
for (size_t i = 0; i < static_cast<size_t>(ETimeMode::Count); ++i) {
const TimeMachine& machine = machines[i];
if (machine.enabled) {
char buf[128];
sprintf(buf, "; estimated printing time (%s mode) = %s\n",
(static_cast<ETimeMode>(i) == ETimeMode::Normal) ? "normal" : "silent",
get_time_dhms(machine.time).c_str());
ret += buf;
}
}
}
@ -407,18 +421,6 @@ void GCodeProcessor::TimeProcessor::post_process(const std::string& filename)
write_string(export_line);
}
for (size_t i = 0; i < static_cast<size_t>(ETimeMode::Count); ++i) {
const TimeMachine& machine = machines[i];
ETimeMode mode = static_cast<ETimeMode>(i);
if (machine.enabled) {
char line[128];
sprintf(line, "; estimated printing time (%s mode) = %s\n",
(mode == ETimeMode::Normal) ? "normal" : "silent",
get_time_dhms(machine.time).c_str());
export_line += line;
}
}
if (!export_line.empty())
write_string(export_line);
@ -870,12 +872,10 @@ void GCodeProcessor::process_tags(const std::string& comment)
// width tag
pos = comment.find(Width_Tag);
if (pos != comment.npos) {
try
{
try {
m_width = std::stof(comment.substr(pos + Width_Tag.length()));
}
catch (...)
{
catch (...) {
BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Width (" << comment << ").";
}
return;
@ -884,12 +884,10 @@ void GCodeProcessor::process_tags(const std::string& comment)
// height tag
pos = comment.find(Height_Tag);
if (pos != comment.npos) {
try
{
try {
m_height = std::stof(comment.substr(pos + Height_Tag.length()));
}
catch (...)
{
catch (...) {
BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Height (" << comment << ").";
}
return;
@ -899,8 +897,7 @@ void GCodeProcessor::process_tags(const std::string& comment)
pos = comment.find(Color_Change_Tag);
if (pos != comment.npos) {
pos = comment.find_last_of(",T");
try
{
try {
unsigned char extruder_id = (pos == comment.npos) ? 0 : static_cast<unsigned char>(std::stoi(comment.substr(pos + 1)));
m_extruder_colors[extruder_id] = static_cast<unsigned char>(m_extruder_offsets.size()) + m_cp_color.counter; // color_change position in list of color for preview
@ -915,8 +912,7 @@ void GCodeProcessor::process_tags(const std::string& comment)
process_custom_gcode_time(CustomGCode::ColorChange);
}
catch (...)
{
catch (...) {
BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Color_Change (" << comment << ").";
}
@ -1115,24 +1111,20 @@ bool GCodeProcessor::process_simplify3d_tags(const std::string& comment)
size_t w_start = data.find(w_tag);
size_t w_end = data.find_first_of(' ', w_start);
if (h_start != data.npos) {
try
{
try {
std::string test = data.substr(h_start + 1, (h_end != data.npos) ? h_end - h_start - 1 : h_end);
m_height = std::stof(data.substr(h_start + 1, (h_end != data.npos) ? h_end - h_start - 1 : h_end));
}
catch (...)
{
catch (...) {
BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Height (" << comment << ").";
}
}
if (w_start != data.npos) {
try
{
try {
std::string test = data.substr(w_start + 1, (w_end != data.npos) ? w_end - w_start - 1 : w_end);
m_width = std::stof(data.substr(w_start + 1, (w_end != data.npos) ? w_end - w_start - 1 : w_end));
}
catch (...)
{
catch (...) {
BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Width (" << comment << ").";
}
}
@ -1218,12 +1210,10 @@ bool GCodeProcessor::process_ideamaker_tags(const std::string& comment)
tag = "WIDTH:";
pos = comment.find(tag);
if (pos != comment.npos) {
try
{
try {
m_width = std::stof(comment.substr(pos + tag.length()));
}
catch (...)
{
catch (...) {
BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Width (" << comment << ").";
}
return true;
@ -1233,12 +1223,10 @@ bool GCodeProcessor::process_ideamaker_tags(const std::string& comment)
tag = "HEIGHT:";
pos = comment.find(tag);
if (pos != comment.npos) {
try
{
try {
m_height = std::stof(comment.substr(pos + tag.length()));
}
catch (...)
{
catch (...) {
BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Height (" << comment << ").";
}
return true;
@ -1871,8 +1859,7 @@ void GCodeProcessor::process_T(const GCodeReader::GCodeLine& line)
void GCodeProcessor::process_T(const std::string& command)
{
if (command.length() > 1) {
try
{
try {
unsigned char id = static_cast<unsigned char>(std::stoi(command.substr(1)));
if (m_extruder_id != id) {
unsigned char extruders_count = static_cast<unsigned char>(m_extruder_offsets.size());
@ -1895,8 +1882,7 @@ void GCodeProcessor::process_T(const std::string& command)
store_move_vertex(EMoveType::Tool_change);
}
}
catch (...)
{
catch (...) {
BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid toolchange (" << command << ").";
}
}

View File

@ -72,8 +72,9 @@ namespace Slic3r {
static const std::string Color_Change_Tag;
static const std::string Pause_Print_Tag;
static const std::string Custom_Code_Tag;
static const std::string First_M73_Output_Placeholder_Tag;
static const std::string Last_M73_Output_Placeholder_Tag;
static const std::string First_Line_M73_Placeholder_Tag;
static const std::string Last_Line_M73_Placeholder_Tag;
static const std::string Estimated_Printing_Time_Placeholder_Tag;
private:
using AxisCoords = std::array<float, 4>;

View File

@ -1349,20 +1349,44 @@ private:
Plater *plater;
static const std::regex pattern_drop;
#if ENABLE_GCODE_VIEWER
static const std::regex pattern_gcode_drop;
#endif // ENABLE_GCODE_VIEWER
};
const std::regex PlaterDropTarget::pattern_drop(".*[.](stl|obj|amf|3mf|prusa)", std::regex::icase);
#if ENABLE_GCODE_VIEWER
const std::regex PlaterDropTarget::pattern_gcode_drop(".*[.](gcode)", std::regex::icase);
#endif // ENABLE_GCODE_VIEWER
bool PlaterDropTarget::OnDropFiles(wxCoord x, wxCoord y, const wxArrayString &filenames)
{
std::vector<fs::path> paths;
for (const auto &filename : filenames) {
fs::path path(into_path(filename));
if (std::regex_match(path.string(), pattern_drop)) {
paths.push_back(std::move(path));
} else {
#if ENABLE_GCODE_VIEWER
if (wxGetApp().mainframe->get_mode() == MainFrame::EMode::GCodeViewer) {
for (const auto& filename : filenames) {
fs::path path(into_path(filename));
if (std::regex_match(path.string(), pattern_gcode_drop))
paths.push_back(std::move(path));
}
if (paths.size() > 1) {
wxMessageDialog((wxWindow*)plater, _L("Only one gcode file at a time can be opened."), wxString(SLIC3R_APP_NAME) + " - " + _L("Open G-code file"), wxCLOSE | wxICON_WARNING | wxCENTRE).ShowModal();
return false;
}
else if (paths.size() == 1) {
plater->load_gcode(from_path(paths.front()));
return true;
}
}
#endif // ENABLE_GCODE_VIEWER
for (const auto &filename : filenames) {
fs::path path(into_path(filename));
if (std::regex_match(path.string(), pattern_drop))
paths.push_back(std::move(path));
else
return false;
}
wxString snapshot_label;
@ -1390,13 +1414,10 @@ bool PlaterDropTarget::OnDropFiles(wxCoord x, wxCoord y, const wxArrayString &fi
// because right now the plater is not cleared, we set the project file (from the latest imported .3mf or .amf file)
// only if not set yet
// if res is empty no data has been loaded
if (!res.empty() && plater->get_project_filename().empty())
{
for (std::vector<fs::path>::const_reverse_iterator it = paths.rbegin(); it != paths.rend(); ++it)
{
if (!res.empty() && plater->get_project_filename().empty()) {
for (std::vector<fs::path>::const_reverse_iterator it = paths.rbegin(); it != paths.rend(); ++it) {
std::string filename = (*it).filename().string();
if (boost::algorithm::iends_with(filename, ".3mf") || boost::algorithm::iends_with(filename, ".amf"))
{
if (boost::algorithm::iends_with(filename, ".3mf") || boost::algorithm::iends_with(filename, ".amf")) {
plater->set_project_filename(from_path(*it));
break;
}