mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-11-01 21:21:10 -06:00
Improved error handling when importing configuration from a G-code.
This commit is contained in:
parent
f7334f58d3
commit
2ac981e422
5 changed files with 47 additions and 15 deletions
|
|
@ -312,7 +312,14 @@ void ConfigBase::load(const std::string &file)
|
|||
void ConfigBase::load_from_gcode(const std::string &file)
|
||||
{
|
||||
// 1) Read a 64k block from the end of the G-code.
|
||||
boost::nowide::ifstream ifs(file);
|
||||
boost::nowide::ifstream ifs(file);
|
||||
{
|
||||
const char slic3r_gcode_header[] = "; generated by Slic3r ";
|
||||
std::string firstline;
|
||||
std::getline(ifs, firstline);
|
||||
if (strncmp(slic3r_gcode_header, firstline.c_str(), strlen(slic3r_gcode_header)) != 0)
|
||||
throw std::exception("Not a Slic3r generated g-code.");
|
||||
}
|
||||
ifs.seekg(0, ifs.end);
|
||||
auto file_length = ifs.tellg();
|
||||
auto data_length = std::min<std::fstream::streampos>(65535, file_length);
|
||||
|
|
@ -325,6 +332,7 @@ void ConfigBase::load_from_gcode(const std::string &file)
|
|||
char *data_start = data.data();
|
||||
// boost::nowide::ifstream seems to cook the text data somehow, so less then the 64k of characters may be retrieved.
|
||||
char *end = data_start + strlen(data.data());
|
||||
size_t num_key_value_pairs = 0;
|
||||
for (;;) {
|
||||
// Extract next line.
|
||||
for (-- end; end > data_start && (*end == '\r' || *end == '\n'); -- end);
|
||||
|
|
@ -362,11 +370,17 @@ void ConfigBase::load_from_gcode(const std::string &file)
|
|||
break;
|
||||
try {
|
||||
this->set_deserialize(key, value);
|
||||
++ num_key_value_pairs;
|
||||
} catch (UnknownOptionException & /* e */) {
|
||||
// ignore
|
||||
}
|
||||
end = start;
|
||||
}
|
||||
if (num_key_value_pairs < 90) {
|
||||
char msg[80];
|
||||
sprintf(msg, "Suspiciously low number of configuration values extracted: %d", num_key_value_pairs);
|
||||
throw std::exception(msg);
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigBase::save(const std::string &file) const
|
||||
|
|
|
|||
|
|
@ -39,7 +39,14 @@
|
|||
%name{setenv} void setenv_();
|
||||
double min_object_distance();
|
||||
%name{_load} void load(std::string file);
|
||||
%name{_load_from_gcode} void load_from_gcode(std::string file);
|
||||
%name{_load_from_gcode} void load_from_gcode(std::string input_file)
|
||||
%code%{
|
||||
try {
|
||||
THIS->load_from_gcode(input_file);
|
||||
} catch (std::exception& e) {
|
||||
croak("Error exracting configuration from a g-code %s:\n%s\n", input_file.c_str(), e.what());
|
||||
}
|
||||
%};
|
||||
%name{_save} void save(std::string file);
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue