Merge remote-tracking branch 'origin/master' into profile_changes_reset

This commit is contained in:
YuSanka 2018-04-12 15:58:46 +02:00
commit 3bc7580e8c
44 changed files with 2116 additions and 252 deletions

View file

@ -0,0 +1,308 @@
#include "Snapshot.hpp"
#include "../GUI/AppConfig.hpp"
#include "../Utils/Time.hpp"
#include <time.h>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/nowide/fstream.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include "../../libslic3r/libslic3r.h"
#include "../../libslic3r/Config.hpp"
#include "../../libslic3r/FileParserError.hpp"
#include "../../libslic3r/Utils.hpp"
#define SLIC3R_SNAPSHOTS_DIR "snapshots"
#define SLIC3R_SNAPSHOT_FILE "snapshot.ini"
namespace Slic3r {
namespace GUI {
namespace Config {
void Snapshot::clear()
{
this->id.clear();
this->time_captured = 0;
this->slic3r_version_captured = Semver::invalid();
this->comment.clear();
this->reason = SNAPSHOT_UNKNOWN;
this->print.clear();
this->filaments.clear();
this->printer.clear();
}
void Snapshot::load_ini(const std::string &path)
{
this->clear();
auto throw_on_parse_error = [&path](const std::string &msg) {
throw file_parser_error(std::string("Failed loading the snapshot file. Reason: ") + msg, path);
};
// Load the snapshot.ini file.
boost::property_tree::ptree tree;
try {
boost::nowide::ifstream ifs(path);
boost::property_tree::read_ini(ifs, tree);
} catch (const std::ifstream::failure &err) {
throw file_parser_error(std::string("The snapshot file cannot be loaded. Reason: ") + err.what(), path);
} catch (const std::runtime_error &err) {
throw_on_parse_error(err.what());
}
// Parse snapshot.ini
std::string group_name_vendor = "Vendor:";
std::string key_filament = "filament";
for (auto &section : tree) {
if (section.first == "snapshot") {
// Parse the common section.
for (auto &kvp : section.second) {
if (kvp.first == "id")
this->id = kvp.second.data();
else if (kvp.first == "time_captured") {
this->time_captured = Slic3r::Utils::parse_time_ISO8601Z(kvp.second.data());
if (this->time_captured == (time_t)-1)
throw_on_parse_error("invalid timestamp");
} else if (kvp.first == "slic3r_version_captured") {
auto semver = Semver::parse(kvp.second.data());
if (! semver)
throw_on_parse_error("invalid slic3r_version_captured semver");
this->slic3r_version_captured = *semver;
} else if (kvp.first == "comment") {
this->comment = kvp.second.data();
} else if (kvp.first == "reason") {
std::string rsn = kvp.second.data();
if (rsn == "upgrade")
this->reason = SNAPSHOT_UPGRADE;
else if (rsn == "downgrade")
this->reason = SNAPSHOT_DOWNGRADE;
else if (rsn == "user")
this->reason = SNAPSHOT_USER;
else
this->reason = SNAPSHOT_UNKNOWN;
}
}
} else if (section.first == "presets") {
// Load the names of the active presets.
for (auto &kvp : section.second) {
if (kvp.first == "print") {
this->print = kvp.second.data();
} else if (boost::starts_with(kvp.first, "filament")) {
int idx = 0;
if (kvp.first == "filament" || sscanf(kvp.first.c_str(), "filament_%d", &idx) == 1) {
if (int(this->filaments.size()) <= idx)
this->filaments.resize(idx + 1, std::string());
this->filaments[idx] = kvp.second.data();
}
} else if (kvp.first == "printer") {
this->printer = kvp.second.data();
}
}
} else if (boost::starts_with(section.first, group_name_vendor) && section.first.size() > group_name_vendor.size()) {
// Vendor specific section.
VendorConfig vc;
vc.name = section.first.substr(group_name_vendor.size());
for (auto &kvp : section.second) {
if (boost::starts_with(kvp.first, "model_")) {
//model:MK2S = 0.4;xxx
//model:MK3 = 0.4;xxx
} else if (kvp.first == "version" || kvp.first == "min_slic3r_version" || kvp.first == "max_slic3r_version") {
// Version of the vendor specific config bundle bundled with this snapshot.
auto semver = Semver::parse(kvp.second.data());
if (! semver)
throw_on_parse_error("invalid " + kvp.first + " format for " + section.first);
if (kvp.first == "version")
vc.version = *semver;
else if (kvp.first == "min_slic3r_version")
vc.min_slic3r_version = *semver;
else
vc.max_slic3r_version = *semver;
}
}
}
}
}
void Snapshot::save_ini(const std::string &path)
{
boost::nowide::ofstream c;
c.open(path, std::ios::out | std::ios::trunc);
c << "# " << Slic3r::header_slic3r_generated() << std::endl;
// Export the common "snapshot".
c << std::endl << "[snapshot]" << std::endl;
c << "id = " << this->id << std::endl;
c << "time_captured = " << Slic3r::Utils::format_time_ISO8601Z(this->time_captured) << std::endl;
c << "slic3r_version_captured = " << this->slic3r_version_captured.to_string() << std::endl;
c << "comment = " << this->comment << std::endl;
c << "reason = " << this->reason << std::endl;
// Export the active presets at the time of the snapshot.
c << std::endl << "[presets]" << std::endl;
c << "print = " << this->print << std::endl;
c << "filament = " << this->filaments.front() << std::endl;
for (size_t i = 1; i < this->filaments.size(); ++ i)
c << "filament_" << std::to_string(i) << " = " << this->filaments[i] << std::endl;
c << "printer = " << this->printer << std::endl;
// Export the vendor configs.
for (const VendorConfig &vc : this->vendor_configs) {
c << std::endl << "[Vendor:" << vc.name << "]" << std::endl;
c << "version = " << vc.version.to_string() << std::endl;
c << "min_slic3r_version = " << vc.min_slic3r_version.to_string() << std::endl;
c << "max_slic3r_version = " << vc.max_slic3r_version.to_string() << std::endl;
}
c.close();
}
void Snapshot::export_selections(AppConfig &config) const
{
assert(filaments.size() >= 1);
config.clear_section("presets");
config.set("presets", "print", print);
config.set("presets", "filament", filaments.front());
for (int i = 1; i < filaments.size(); ++i) {
char name[64];
sprintf(name, "filament_%d", i);
config.set("presets", name, filaments[i]);
}
config.set("presets", "printer", printer);
}
size_t SnapshotDB::load_db()
{
boost::filesystem::path snapshots_dir = SnapshotDB::create_db_dir();
m_snapshots.clear();
// Walk over the snapshot directories and load their index.
std::string errors_cummulative;
for (auto &dir_entry : boost::filesystem::directory_iterator(snapshots_dir))
if (boost::filesystem::is_directory(dir_entry.status())) {
// Try to read "snapshot.ini".
boost::filesystem::path path_ini = dir_entry.path() / SLIC3R_SNAPSHOT_FILE;
Snapshot snapshot;
try {
snapshot.load_ini(path_ini.string());
} catch (const std::runtime_error &err) {
errors_cummulative += err.what();
errors_cummulative += "\n";
continue;
}
// Check that the name of the snapshot directory matches the snapshot id stored in the snapshot.ini file.
if (dir_entry.path().filename().string() != snapshot.id) {
errors_cummulative += std::string("Snapshot ID ") + snapshot.id + " does not match the snapshot directory " + dir_entry.path().filename().string() + "\n";
continue;
}
m_snapshots.emplace_back(std::move(snapshot));
}
if (! errors_cummulative.empty())
throw std::runtime_error(errors_cummulative);
return m_snapshots.size();
}
static void copy_config_dir_single_level(const boost::filesystem::path &path_src, const boost::filesystem::path &path_dst)
{
if (! boost::filesystem::is_directory(path_dst) &&
! boost::filesystem::create_directory(path_dst))
throw std::runtime_error(std::string("Slic3r was unable to create a directory at ") + path_dst.string());
for (auto &dir_entry : boost::filesystem::directory_iterator(path_src))
if (boost::filesystem::is_regular_file(dir_entry.status()) && boost::algorithm::iends_with(dir_entry.path().filename().string(), ".ini"))
boost::filesystem::copy_file(dir_entry.path(), path_dst / dir_entry.path().filename(), boost::filesystem::copy_option::overwrite_if_exists);
}
static void delete_existing_ini_files(const boost::filesystem::path &path)
{
if (! boost::filesystem::is_directory(path))
return;
for (auto &dir_entry : boost::filesystem::directory_iterator(path))
if (boost::filesystem::is_regular_file(dir_entry.status()) && boost::algorithm::iends_with(dir_entry.path().filename().string(), ".ini"))
boost::filesystem::remove(dir_entry.path());
}
const Snapshot& SnapshotDB::make_snapshot(const AppConfig &app_config, Snapshot::Reason reason, const std::string &comment)
{
boost::filesystem::path data_dir = boost::filesystem::path(Slic3r::data_dir());
boost::filesystem::path snapshot_db_dir = SnapshotDB::create_db_dir();
// 1) Prepare the snapshot structure.
Snapshot snapshot;
// Snapshot header.
snapshot.time_captured = Slic3r::Utils::get_current_time_utc();
snapshot.id = Slic3r::Utils::format_time_ISO8601Z(snapshot.time_captured);
snapshot.slic3r_version_captured = *Semver::parse(SLIC3R_VERSION);
snapshot.comment = comment;
snapshot.reason = reason;
// Active presets at the time of the snapshot.
snapshot.print = app_config.get("presets", "print");
snapshot.filaments.emplace_back(app_config.get("presets", "filament"));
snapshot.printer = app_config.get("presets", "printer");
for (unsigned int i = 1; i < 1000; ++ i) {
char name[64];
sprintf(name, "filament_%d", i);
if (! app_config.has("presets", name))
break;
snapshot.filaments.emplace_back(app_config.get("presets", name));
}
// Vendor specific config bundles and installed printers.
// Backup the presets.
boost::filesystem::path snapshot_dir = snapshot_db_dir / snapshot.id;
for (const char *subdir : { "print", "filament", "printer", "vendor" })
copy_config_dir_single_level(data_dir / subdir, snapshot_dir / subdir);
snapshot.save_ini((snapshot_dir / "snapshot.ini").string());
m_snapshots.emplace_back(std::move(snapshot));
return m_snapshots.back();
}
void SnapshotDB::restore_snapshot(const std::string &id, AppConfig &app_config)
{
for (const Snapshot &snapshot : m_snapshots)
if (snapshot.id == id) {
this->restore_snapshot(snapshot, app_config);
return;
}
throw std::runtime_error(std::string("Snapshot with id " + id + " was not found."));
}
void SnapshotDB::restore_snapshot(const Snapshot &snapshot, AppConfig &app_config)
{
boost::filesystem::path data_dir = boost::filesystem::path(Slic3r::data_dir());
boost::filesystem::path snapshot_db_dir = SnapshotDB::create_db_dir();
boost::filesystem::path snapshot_dir = snapshot_db_dir / snapshot.id;
// Remove existing ini files and restore the ini files from the snapshot.
for (const char *subdir : { "print", "filament", "printer", "vendor" }) {
delete_existing_ini_files(data_dir / subdir);
copy_config_dir_single_level(snapshot_dir / subdir, data_dir / subdir);
}
// Update app_config from the snapshot.
snapshot.export_selections(app_config);
// Store information about the snapshot.
}
boost::filesystem::path SnapshotDB::create_db_dir()
{
boost::filesystem::path data_dir = boost::filesystem::path(Slic3r::data_dir());
boost::filesystem::path snapshots_dir = data_dir / SLIC3R_SNAPSHOTS_DIR;
for (const boost::filesystem::path &path : { data_dir, snapshots_dir }) {
boost::filesystem::path subdir = path;
subdir.make_preferred();
if (! boost::filesystem::is_directory(subdir) &&
! boost::filesystem::create_directory(subdir))
throw std::runtime_error(std::string("Slic3r was unable to create a directory at ") + subdir.string());
}
return snapshots_dir;
}
} // namespace Config
} // namespace GUI
} // namespace Slic3r

View file

@ -0,0 +1,106 @@
#ifndef slic3r_GUI_Snapshot_
#define slic3r_GUI_Snapshot_
#include <string>
#include <vector>
#include <boost/filesystem.hpp>
#include "../Utils/Semver.hpp"
namespace Slic3r {
class AppConfig;
namespace GUI {
namespace Config {
// A snapshot contains:
// Slic3r.ini
// vendor/
// print/
// filament/
// printer/
class Snapshot
{
public:
enum Reason {
SNAPSHOT_UNKNOWN,
SNAPSHOT_UPGRADE,
SNAPSHOT_DOWNGRADE,
SNAPSHOT_USER,
};
Snapshot() { clear(); }
void clear();
void load_ini(const std::string &path);
void save_ini(const std::string &path);
// Export the print / filament / printer selections to be activated into the AppConfig.
void export_selections(AppConfig &config) const;
// ID of a snapshot should equal to the name of the snapshot directory.
// The ID contains the date/time, reason and comment to be human readable.
std::string id;
std::time_t time_captured;
// Which Slic3r version captured this snapshot?
Semver slic3r_version_captured = Semver::invalid();
// Comment entered by the user at the start of the snapshot capture.
std::string comment;
Reason reason;
// Active presets at the time of the snapshot.
std::string print;
std::vector<std::string> filaments;
std::string printer;
// Annotation of the vendor configuration stored in the snapshot.
// This information is displayed to the user and used to decide compatibility
// of the configuration stored in the snapshot with the running Slic3r version.
struct VendorConfig {
// Name of the vendor contained in this snapshot.
std::string name;
// Version of the vendor config contained in this snapshot.
Semver version = Semver::invalid();
// Minimum Slic3r version compatible with this vendor configuration.
Semver min_slic3r_version = Semver::zero();
// Maximum Slic3r version compatible with this vendor configuration, or empty.
Semver max_slic3r_version = Semver::inf();
};
// List of vendor configs contained in this snapshot.
std::vector<VendorConfig> vendor_configs;
};
class SnapshotDB
{
public:
typedef std::vector<Snapshot>::const_iterator const_iterator;
// Load the snapshot database from the snapshots directory.
// If the snapshot directory or its parent does not exist yet, it will be created.
// Returns a number of snapshots loaded.
size_t load_db();
// Create a snapshot directory, copy the vendor config bundles, user print/filament/printer profiles,
// create an index.
const Snapshot& make_snapshot(const AppConfig &app_config, Snapshot::Reason reason, const std::string &comment);
void restore_snapshot(const std::string &id, AppConfig &app_config);
void restore_snapshot(const Snapshot &snapshot, AppConfig &app_config);
const_iterator begin() const { return m_snapshots.begin(); }
const_iterator end() const { return m_snapshots.end(); }
const std::vector<Snapshot>& snapshots() const { return m_snapshots; }
private:
// Create the snapshots directory if it does not exist yet.
static boost::filesystem::path create_db_dir();
std::vector<Snapshot> m_snapshots;
};
} // namespace Config
} // namespace GUI
} // namespace Slic3r
#endif /* slic3r_GUI_Snapshot_ */

View file

@ -0,0 +1,136 @@
#include "Version.hpp"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/nowide/fstream.hpp>
#include "../../libslic3r/libslic3r.h"
#include "../../libslic3r/Config.hpp"
namespace Slic3r {
namespace GUI {
namespace Config {
static boost::optional<Semver> s_current_slic3r_semver = Semver::parse(SLIC3R_VERSION);
bool Version::is_current_slic3r_supported() const
{
return this->is_slic3r_supported(*s_current_slic3r_semver);
}
inline char* left_trim(char *c)
{
for (; *c == ' ' || *c == '\t'; ++ c);
return c;
}
inline char* right_trim(char *start)
{
char *end = start + strlen(start) - 1;
for (; end >= start && (*end == ' ' || *end == '\t'); -- end);
*(++ end) = 0;
return end;
}
inline std::string unquote_value(char *value, char *end, const std::string &path, int idx_line)
{
std::string svalue;
if (value == end) {
// Empty string is a valid string.
} else if (*value == '"') {
if (++ value < -- end || *end != '"')
throw file_parser_error("String not enquoted correctly", path, idx_line);
*end = 0;
if (! unescape_string_cstyle(value, svalue))
throw file_parser_error("Invalid escape sequence inside a quoted string", path, idx_line);
}
return svalue;
}
inline std::string unquote_version_comment(char *value, char *end, const std::string &path, int idx_line)
{
std::string svalue;
if (value == end) {
// Empty string is a valid string.
} else if (*value == '"') {
if (++ value < -- end || *end != '"')
throw file_parser_error("Version comment not enquoted correctly", path, idx_line);
*end = 0;
if (! unescape_string_cstyle(value, svalue))
throw file_parser_error("Invalid escape sequence inside a quoted version comment", path, idx_line);
}
return svalue;
}
size_t Index::load(const std::string &path)
{
m_configs.clear();
boost::nowide::ifstream ifs(path);
std::string line;
size_t idx_line = 0;
Version ver;
while (std::getline(ifs, line)) {
++ idx_line;
// Skip the initial white spaces.
char *key = left_trim(const_cast<char*>(line.data()));
// Right trim the line.
char *end = right_trim(key);
// Keyword may only contain alphanumeric characters. Semantic version may in addition contain "+.-".
char *key_end = key;
bool maybe_semver = false;
for (;; ++ key) {
if (strchr("+.-", *key) != nullptr)
maybe_semver = true;
else if (! std::isalnum(*key))
break;
}
if (*key != 0 && *key != ' ' && *key != '\t' && *key != '=')
throw file_parser_error("Invalid keyword or semantic version", path, idx_line);
*key_end = 0;
boost::optional<Semver> semver;
if (maybe_semver)
semver = Semver::parse(key);
char *value = left_trim(key_end);
if (*value == '=') {
if (semver)
throw file_parser_error("Key cannot be a semantic version", path, idx_line);
// Verify validity of the key / value pair.
std::string svalue = unquote_value(left_trim(++ value), end, path, idx_line);
if (key == "min_sic3r_version" || key == "max_slic3r_version") {
if (! svalue.empty())
semver = Semver::parse(key);
if (! semver)
throw file_parser_error(std::string(key) + " must referece a valid semantic version", path, idx_line);
if (key == "min_sic3r_version")
ver.min_slic3r_version = *semver;
else
ver.max_slic3r_version = *semver;
} else {
// Ignore unknown keys, as there may come new keys in the future.
}
}
if (! semver)
throw file_parser_error("Invalid semantic version", path, idx_line);
ver.config_version = *semver;
ver.comment = (end <= key_end) ? "" : unquote_version_comment(value, end, path, idx_line);
m_configs.emplace_back(ver);
}
return m_configs.size();
}
Index::const_iterator Index::recommended() const
{
int idx = -1;
const_iterator highest = m_configs.end();
for (const_iterator it = this->begin(); it != this->end(); ++ it)
if (it->is_current_slic3r_supported() &&
(highest == this->end() || highest->max_slic3r_version < it->max_slic3r_version))
highest = it;
return highest;
}
} // namespace Config
} // namespace GUI
} // namespace Slic3r

View file

@ -0,0 +1,75 @@
#ifndef slic3r_GUI_ConfigIndex_
#define slic3r_GUI_ConfigIndex_
#include <string>
#include <vector>
#include "../../libslic3r/FileParserError.hpp"
#include "../Utils/Semver.hpp"
namespace Slic3r {
namespace GUI {
namespace Config {
// Configuration bundle version.
struct Version
{
// Version of this config.
Semver config_version = Semver::invalid();
// Minimum Slic3r version, for which this config is applicable.
Semver min_slic3r_version = Semver::zero();
// Maximum Slic3r version, for which this config is recommended.
// Slic3r should read older configuration and upgrade to a newer format,
// but likely there has been a better configuration published, using the new features.
Semver max_slic3r_version = Semver::inf();
// Single comment line.
std::string comment;
bool is_slic3r_supported(const Semver &slicer_version) const { return slicer_version.in_range(min_slic3r_version, max_slic3r_version); }
bool is_current_slic3r_supported() const;
};
// Index of vendor specific config bundle versions and Slic3r compatibilities.
// The index is being downloaded from the internet, also an initial version of the index
// is contained in the Slic3r installation.
//
// The index has a simple format:
//
// min_sic3r_version =
// max_slic3r_version =
// config_version "comment"
// config_version "comment"
// ...
// min_slic3r_version =
// max_slic3r_version =
// config_version comment
// config_version comment
// ...
//
// The min_slic3r_version, max_slic3r_version keys are applied to the config versions below,
// empty slic3r version means an open interval.
class Index
{
public:
typedef std::vector<Version>::const_iterator const_iterator;
// Read a config index file in the simple format described in the Index class comment.
// Throws Slic3r::file_parser_error and the standard std file access exceptions.
size_t load(const std::string &path);
const_iterator begin() const { return m_configs.begin(); }
const_iterator end() const { return m_configs.end(); }
const std::vector<Version>& configs() const { return m_configs; }
// Finds a recommended config to be installed for the current Slic3r version.
// Returns configs().end() if such version does not exist in the index. This shall never happen
// if the index is valid.
const_iterator recommended() const;
private:
std::vector<Version> m_configs;
};
} // namespace Config
} // namespace GUI
} // namespace Slic3r
#endif /* slic3r_GUI_ConfigIndex_ */

View file

@ -194,8 +194,8 @@ void GLIndexedVertexArray::render(
const float GLVolume::SELECTED_COLOR[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
const float GLVolume::HOVER_COLOR[4] = { 0.4f, 0.9f, 0.1f, 1.0f };
const float GLVolume::OUTSIDE_COLOR[4] = { 0.75f, 0.0f, 0.75f, 1.0f };
const float GLVolume::SELECTED_OUTSIDE_COLOR[4] = { 1.0f, 0.0f, 1.0f, 1.0f };
const float GLVolume::OUTSIDE_COLOR[4] = { 0.0f, 0.38f, 0.8f, 1.0f };
const float GLVolume::SELECTED_OUTSIDE_COLOR[4] = { 0.19f, 0.58f, 1.0f, 1.0f };
void GLVolume::set_render_color(float r, float g, float b, float a)
{
@ -627,6 +627,8 @@ void GLVolumeCollection::update_outside_state(const DynamicPrintConfig* config,
BoundingBox bed_box_2D = get_extents(Polygon::new_scale(opt->values));
BoundingBoxf3 print_volume(Pointf3(unscale(bed_box_2D.min.x), unscale(bed_box_2D.min.y), 0.0), Pointf3(unscale(bed_box_2D.max.x), unscale(bed_box_2D.max.y), config->opt_float("max_print_height")));
// Allow the objects to protrude below the print bed
print_volume.min.z = -1e10;
for (GLVolume* volume : this->volumes)
{
@ -642,20 +644,25 @@ void GLVolumeCollection::update_outside_state(const DynamicPrintConfig* config,
std::vector<double> GLVolumeCollection::get_current_print_zs() const
{
// Collect layer top positions of all volumes.
std::vector<double> print_zs;
for (GLVolume *vol : this->volumes)
{
for (coordf_t z : vol->print_zs)
{
double round_z = (double)round(z * 100000.0f) / 100000.0f;
if (std::find(print_zs.begin(), print_zs.end(), round_z) == print_zs.end())
print_zs.push_back(round_z);
}
}
append(print_zs, vol->print_zs);
std::sort(print_zs.begin(), print_zs.end());
// Replace intervals of layers with similar top positions with their average value.
int n = int(print_zs.size());
int k = 0;
for (int i = 0; i < n;) {
int j = i + 1;
coordf_t zmax = print_zs[i] + EPSILON;
for (; j < n && print_zs[j] <= zmax; ++ j) ;
print_zs[k ++] = (j > i + 1) ? (0.5 * (print_zs[i] + print_zs[j - 1])) : print_zs[i];
i = j;
}
if (k < n)
print_zs.erase(print_zs.begin() + k, print_zs.end());
return print_zs;
}
@ -2039,6 +2046,8 @@ void _3DScene::_load_gcode_extrusion_paths(const GCodePreviewData& preview_data,
return path.width;
case GCodePreviewData::Extrusion::Feedrate:
return path.feedrate;
case GCodePreviewData::Extrusion::VolumetricRate:
return path.feedrate * (float)path.mm3_per_mm;
case GCodePreviewData::Extrusion::Tool:
return (float)path.extruder_id;
}
@ -2053,11 +2062,13 @@ void _3DScene::_load_gcode_extrusion_paths(const GCodePreviewData& preview_data,
case GCodePreviewData::Extrusion::FeatureType:
return data.get_extrusion_role_color((ExtrusionRole)(int)value);
case GCodePreviewData::Extrusion::Height:
return data.get_extrusion_height_color(value);
return data.get_height_color(value);
case GCodePreviewData::Extrusion::Width:
return data.get_extrusion_width_color(value);
return data.get_width_color(value);
case GCodePreviewData::Extrusion::Feedrate:
return data.get_extrusion_feedrate_color(value);
return data.get_feedrate_color(value);
case GCodePreviewData::Extrusion::VolumetricRate:
return data.get_volumetric_rate_color(value);
case GCodePreviewData::Extrusion::Tool:
{
static GCodePreviewData::Color color;
@ -2337,7 +2348,7 @@ bool _3DScene::_travel_paths_by_feedrate(const GCodePreviewData& preview_data, G
// creates a new volume for each feedrate
for (Feedrate& feedrate : feedrates)
{
GLVolume* volume = new GLVolume(preview_data.get_extrusion_feedrate_color(feedrate.value).rgba);
GLVolume* volume = new GLVolume(preview_data.get_feedrate_color(feedrate.value).rgba);
if (volume == nullptr)
return false;
else

View file

@ -1,6 +1,7 @@
#include "GUI.hpp"
#include <assert.h>
#include <cmath>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem.hpp>
@ -37,6 +38,7 @@
#include <wx/sizer.h>
#include <wx/combo.h>
#include <wx/window.h>
#include <wx/settings.h>
#include "wxExtensions.hpp"
@ -174,6 +176,8 @@ wxFrame *g_wxMainFrame = nullptr;
wxNotebook *g_wxTabPanel = nullptr;
AppConfig *g_AppConfig = nullptr;
PresetBundle *g_PresetBundle= nullptr;
wxColour g_color_label_modified;
wxColour g_color_label_sys;
std::vector<Tab *> g_tabs_list;
@ -182,9 +186,22 @@ wxLocale* g_wxLocale;
std::shared_ptr<ConfigOptionsGroup> m_optgroup;
double m_brim_width = 0.0;
static void init_label_colours()
{
auto luma = get_colour_approx_luma(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
if (luma >= 128) {
g_color_label_modified = wxColour(253, 88, 0);
g_color_label_sys = wxColour(26, 132, 57);
} else {
g_color_label_modified = wxColour(253, 111, 40);
g_color_label_sys = wxColour(115, 220, 103);
}
}
void set_wxapp(wxApp *app)
{
g_wxApp = app;
init_label_colours();
}
void set_main_frame(wxFrame *main_frame)
@ -514,12 +531,25 @@ wxApp* get_app(){
return g_wxApp;
}
wxColour* get_modified_label_clr(){
return new wxColour(253, 88, 0);
const wxColour& get_modified_label_clr() {
return g_color_label_modified;
}
wxColour* get_sys_label_clr(){
return new wxColour(26, 132, 57);
const wxColour& get_sys_label_clr() {
return g_color_label_sys;
}
unsigned get_colour_approx_luma(const wxColour &colour)
{
double r = colour.Red();
double g = colour.Green();
double b = colour.Blue();
return std::round(std::sqrt(
r * r * .241 +
g * g * .691 +
b * b * .068
));
}
void create_combochecklist(wxComboCtrl* comboCtrl, std::string text, std::string items, bool initial_value)
@ -692,4 +722,49 @@ ConfigOptionsGroup* get_optgroup()
return m_optgroup.get();
}
wxWindow* export_option_creator(wxWindow* parent)
{
wxPanel* panel = new wxPanel(parent, -1);
wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
wxCheckBox* cbox = new wxCheckBox(panel, wxID_HIGHEST + 1, L("Export print config"));
sizer->AddSpacer(5);
sizer->Add(cbox, 0, wxEXPAND | wxALL | wxALIGN_CENTER_VERTICAL, 5);
panel->SetSizer(sizer);
sizer->SetSizeHints(panel);
return panel;
}
void add_export_option(wxFileDialog* dlg, const std::string& format)
{
if ((dlg != nullptr) && (format == "AMF") || (format == "3MF"))
{
if (dlg->SupportsExtraControl())
dlg->SetExtraControlCreator(export_option_creator);
}
}
int get_export_option(wxFileDialog* dlg)
{
if (dlg != nullptr)
{
wxWindow* wnd = dlg->GetExtraControl();
if (wnd != nullptr)
{
wxPanel* panel = dynamic_cast<wxPanel*>(wnd);
if (panel != nullptr)
{
wxWindow* child = panel->FindWindow(wxID_HIGHEST + 1);
if (child != nullptr)
{
wxCheckBox* cbox = dynamic_cast<wxCheckBox*>(child);
if (cbox != nullptr)
return cbox->IsChecked() ? 1 : 0;
}
}
}
}
return 0;
}
} }

View file

@ -18,6 +18,7 @@ class wxArrayLong;
class wxColour;
class wxBoxSizer;
class wxFlexGridSizer;
class wxFileDialog;
namespace Slic3r {
@ -78,8 +79,10 @@ void set_preset_bundle(PresetBundle *preset_bundle);
AppConfig* get_app_config();
wxApp* get_app();
wxColour* get_modified_label_clr();
wxColour* get_sys_label_clr();
const wxColour& get_modified_label_clr();
const wxColour& get_sys_label_clr();
unsigned get_colour_approx_luma(const wxColour &colour);
void add_debug_menu(wxMenuBar *menu, int event_language_change);
@ -131,6 +134,8 @@ void add_frequently_changed_parameters(wxWindow* parent, wxBoxSizer* sizer, wxFl
ConfigOptionsGroup* get_optgroup();
void add_export_option(wxFileDialog* dlg, const std::string& format);
int get_export_option(wxFileDialog* dlg);
}
}

View file

@ -334,14 +334,14 @@ void Tab::update_changed_ui()
bool is_modified_value = true;
std::string sys_icon = /*wxMSW ? */"sys_lock.png"/* : "lock.png"*/;
std::string icon = /*wxMSW ? */"action_undo.png"/* : "arrow_undo.png"*/;
wxColour& color = *get_sys_label_clr();
wxColour color = get_sys_label_clr();
if (find(m_sys_options.begin(), m_sys_options.end(), opt_key) == m_sys_options.end()) {
is_nonsys_value = true;
sys_icon = m_nonsys_btn_icon;
if(find(m_dirty_options.begin(), m_dirty_options.end(), opt_key) == m_dirty_options.end())
color = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
else
color = *get_modified_label_clr();
color = get_modified_label_clr();
}
if (find(m_dirty_options.begin(), m_dirty_options.end(), opt_key) == m_dirty_options.end())
{
@ -455,9 +455,9 @@ void Tab::update_changed_tree_ui()
break;
}
if (sys_page)
m_treectrl->SetItemTextColour(cur_item, *get_sys_label_clr());
m_treectrl->SetItemTextColour(cur_item, get_sys_label_clr());
else if (modified_page)
m_treectrl->SetItemTextColour(cur_item, *get_modified_label_clr());
m_treectrl->SetItemTextColour(cur_item, get_modified_label_clr());
else
m_treectrl->SetItemTextColour(cur_item, wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));

View file

@ -36,16 +36,20 @@ struct Http::priv
::curl_slist *headerlist;
std::string buffer;
size_t limit;
bool cancel;
std::thread io_thread;
Http::CompleteFn completefn;
Http::ErrorFn errorfn;
Http::ProgressFn progressfn;
priv(const std::string &url);
~priv();
static bool ca_file_supported(::CURL *curl);
static size_t writecb(void *data, size_t size, size_t nmemb, void *userp);
static int xfercb(void *userp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
static int xfercb_legacy(void *userp, double dltotal, double dlnow, double ultotal, double ulnow);
std::string curl_error(CURLcode curlcode);
std::string body_size_error();
void http_perform();
@ -55,7 +59,8 @@ Http::priv::priv(const std::string &url) :
curl(::curl_easy_init()),
form(nullptr),
form_end(nullptr),
headerlist(nullptr)
headerlist(nullptr),
cancel(false)
{
if (curl == nullptr) {
throw std::runtime_error(std::string("Could not construct Curl object"));
@ -112,6 +117,24 @@ size_t Http::priv::writecb(void *data, size_t size, size_t nmemb, void *userp)
return realsize;
}
int Http::priv::xfercb(void *userp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{
auto self = static_cast<priv*>(userp);
bool cb_cancel = false;
if (self->progressfn) {
Progress progress(dltotal, dlnow, ultotal, ulnow);
self->progressfn(progress, cb_cancel);
}
return self->cancel || cb_cancel;
}
int Http::priv::xfercb_legacy(void *userp, double dltotal, double dlnow, double ultotal, double ulnow)
{
return xfercb(userp, dltotal, dlnow, ultotal, ulnow);
}
std::string Http::priv::curl_error(CURLcode curlcode)
{
return (boost::format("%1% (%2%)")
@ -132,6 +155,16 @@ void Http::priv::http_perform()
::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writecb);
::curl_easy_setopt(curl, CURLOPT_WRITEDATA, static_cast<void*>(this));
::curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
#if LIBCURL_VERSION_MAJOR >= 7 && LIBCURL_VERSION_MINOR >= 32
::curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xfercb);
::curl_easy_setopt(curl, CURLOPT_XFERINFODATA, static_cast<void*>(this));
(void)xfercb_legacy; // prevent unused function warning
#else
::curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, xfercb);
::curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, static_cast<void*>(this));
#endif
#ifndef NDEBUG
::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
#endif
@ -149,16 +182,16 @@ void Http::priv::http_perform()
::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_status);
if (res != CURLE_OK) {
std::string error;
if (res == CURLE_WRITE_ERROR) {
error = std::move(body_size_error());
} else {
error = std::move(curl_error(res));
};
if (errorfn) {
errorfn(std::move(buffer), std::move(error), http_status);
if (res == CURLE_ABORTED_BY_CALLBACK) {
Progress dummyprogress(0, 0, 0, 0);
bool cancel = true;
if (progressfn) { progressfn(dummyprogress, cancel); }
}
else if (res == CURLE_WRITE_ERROR) {
if (errorfn) { errorfn(std::move(buffer), std::move(body_size_error()), http_status); }
} else {
if (errorfn) { errorfn(std::move(buffer), std::move(curl_error(res)), http_status); }
};
} else {
if (completefn) {
completefn(std::move(buffer), http_status);
@ -258,6 +291,12 @@ Http& Http::on_error(ErrorFn fn)
return *this;
}
Http& Http::on_progress(ProgressFn fn)
{
if (p) { p->progressfn = std::move(fn); }
return *this;
}
Http::Ptr Http::perform()
{
auto self = std::make_shared<Http>(std::move(*this));
@ -277,6 +316,11 @@ void Http::perform_sync()
if (p) { p->http_perform(); }
}
void Http::cancel()
{
if (p) { p->cancel = true; }
}
Http Http::get(std::string url)
{
return std::move(Http{std::move(url)});
@ -297,5 +341,16 @@ bool Http::ca_file_supported()
return res;
}
std::ostream& operator<<(std::ostream &os, const Http::Progress &progress)
{
os << "Http::Progress("
<< "dltotal = " << progress.dltotal
<< ", dlnow = " << progress.dlnow
<< ", ultotal = " << progress.ultotal
<< ", ulnow = " << progress.ulnow
<< ")";
return os;
}
}

View file

@ -14,9 +14,22 @@ class Http : public std::enable_shared_from_this<Http> {
private:
struct priv;
public:
struct Progress
{
size_t dltotal;
size_t dlnow;
size_t ultotal;
size_t ulnow;
Progress(size_t dltotal, size_t dlnow, size_t ultotal, size_t ulnow) :
dltotal(dltotal), dlnow(dlnow), ultotal(ultotal), ulnow(ulnow)
{}
};
typedef std::shared_ptr<Http> Ptr;
typedef std::function<void(std::string /* body */, unsigned /* http_status */)> CompleteFn;
typedef std::function<void(std::string /* body */, std::string /* error */, unsigned /* http_status */)> ErrorFn;
typedef std::function<void(Progress, bool& /* cancel */)> ProgressFn;
Http(Http &&other);
@ -37,9 +50,11 @@ public:
Http& on_complete(CompleteFn fn);
Http& on_error(ErrorFn fn);
Http& on_progress(ProgressFn fn);
Ptr perform();
void perform_sync();
void cancel();
static bool ca_file_supported();
private:
@ -48,6 +63,8 @@ private:
std::unique_ptr<priv> p;
};
std::ostream& operator<<(std::ostream &, const Http::Progress &);
}

View file

@ -1,10 +1,11 @@
#include "OctoPrint.hpp"
#include <iostream>
#include <algorithm>
#include <boost/format.hpp>
#include <wx/frame.h>
#include <wx/event.h>
#include <wx/progdlg.h>
#include "libslic3r/PrintConfig.hpp"
#include "slic3r/GUI/GUI.hpp"
@ -39,36 +40,53 @@ bool OctoPrint::test(wxString &msg) const
return res;
}
void OctoPrint::send_gcode(int windowId, int completeEvt, int errorEvt, const std::string &filename, bool print) const
bool OctoPrint::send_gcode(const std::string &filename, bool print) const
{
enum { PROGRESS_RANGE = 1000 };
const auto errortitle = _(L("Error while uploading to the OctoPrint server"));
wxProgressDialog progress_dialog(
_(L("OctoPrint upload")),
_(L("Sending G-code file to the OctoPrint server...")),
PROGRESS_RANGE, nullptr, wxPD_AUTO_HIDE | wxPD_APP_MODAL | wxPD_CAN_ABORT);
progress_dialog.Pulse();
wxString test_msg;
if (!test(test_msg)) {
auto errormsg = wxString::Format("%s: %s", errortitle, test_msg);
GUI::show_error(&progress_dialog, std::move(errormsg));
return false;
}
bool res = true;
auto http = Http::post(std::move(make_url("api/files/local")));
set_auth(http);
http.form_add("print", print ? "true" : "false")
.form_add_file("file", filename)
.on_complete([=](std::string body, unsigned status) {
wxWindow *window = wxWindow::FindWindowById(windowId);
if (window == nullptr) { return; }
wxCommandEvent* evt = new wxCommandEvent(completeEvt);
evt->SetString(_(L("G-code file successfully uploaded to the OctoPrint server")));
evt->SetInt(100);
wxQueueEvent(window, evt);
.on_complete([&](std::string body, unsigned status) {
progress_dialog.Update(PROGRESS_RANGE);
})
.on_error([=](std::string body, std::string error, unsigned status) {
wxWindow *window = wxWindow::FindWindowById(windowId);
if (window == nullptr) { return; }
wxCommandEvent* evt_complete = new wxCommandEvent(completeEvt);
evt_complete->SetInt(100);
wxQueueEvent(window, evt_complete);
wxCommandEvent* evt_error = new wxCommandEvent(errorEvt);
evt_error->SetString(wxString::Format("%s: %s",
_(L("Error while uploading to the OctoPrint server")),
format_error(error, status)));
wxQueueEvent(window, evt_error);
.on_error([&](std::string body, std::string error, unsigned status) {
auto errormsg = wxString::Format("%s: %s", errortitle, format_error(error, status));
GUI::show_error(&progress_dialog, std::move(errormsg));
res = false;
})
.perform();
.on_progress([&](Http::Progress progress, bool &cancel) {
if (cancel) {
// Upload was canceled
res = false;
} else if (progress.ultotal > 0) {
int value = PROGRESS_RANGE * progress.ulnow / progress.ultotal;
cancel = !progress_dialog.Update(std::min(value, PROGRESS_RANGE - 1)); // Cap the value to prevent premature dialog closing
} else {
cancel = !progress_dialog.Pulse();
}
})
.perform_sync();
return res;
}
void OctoPrint::set_auth(Http &http) const

View file

@ -17,7 +17,7 @@ public:
OctoPrint(DynamicPrintConfig *config);
bool test(wxString &curl_msg) const;
void send_gcode(int windowId, int completeEvt, int errorEvt, const std::string &filename, bool print = false) const;
bool send_gcode(const std::string &filename, bool print = false) const;
private:
std::string host;
std::string apikey;

View file

@ -0,0 +1,114 @@
#ifndef slic3r_Semver_hpp_
#define slic3r_Semver_hpp_
#include <string>
#include <cstring>
#include <boost/optional.hpp>
#include <boost/format.hpp>
#include "semver/semver.h"
namespace Slic3r {
class Semver
{
public:
struct Major { const int i; Major(int i) : i(i) {} };
struct Minor { const int i; Minor(int i) : i(i) {} };
struct Patch { const int i; Patch(int i) : i(i) {} };
static boost::optional<Semver> parse(const std::string &str)
{
semver_t ver;
if (::semver_parse(str.c_str(), &ver) == 0) {
return Semver(ver);
} else {
return boost::none;
}
}
static const Semver zero()
{
static semver_t ver = { 0, 0, 0, nullptr, nullptr };
return Semver(ver);
}
static const Semver inf()
{
static semver_t ver = { std::numeric_limits<int>::max(), std::numeric_limits<int>::max(), std::numeric_limits<int>::max(), nullptr, nullptr };
return Semver(ver);
}
static const Semver invalid()
{
static semver_t ver = { -1, 0, 0, nullptr, nullptr };
return Semver(ver);
}
Semver(Semver &&other) { *this = std::move(other); }
Semver(const Semver &other) { *this = other; }
Semver &operator=(Semver &&other)
{
ver = other.ver;
other.ver.major = other.ver.minor = other.ver.patch = 0;
other.ver.metadata = other.ver.prerelease = nullptr;
return *this;
}
Semver &operator=(const Semver &other)
{
::semver_free(&ver);
ver = other.ver;
if (other.ver.metadata != nullptr) { std::strcpy(ver.metadata, other.ver.metadata); }
if (other.ver.prerelease != nullptr) { std::strcpy(ver.prerelease, other.ver.prerelease); }
return *this;
}
~Semver() { ::semver_free(&ver); }
// Comparison
bool operator<(const Semver &b) const { return ::semver_compare(ver, b.ver) == -1; }
bool operator<=(const Semver &b) const { return ::semver_compare(ver, b.ver) <= 0; }
bool operator==(const Semver &b) const { return ::semver_compare(ver, b.ver) == 0; }
bool operator!=(const Semver &b) const { return ::semver_compare(ver, b.ver) != 0; }
bool operator>=(const Semver &b) const { return ::semver_compare(ver, b.ver) >= 0; }
bool operator>(const Semver &b) const { return ::semver_compare(ver, b.ver) == 1; }
// We're using '&' instead of the '~' operator here as '~' is unary-only:
// Satisfies patch if Major and minor are equal.
bool operator&(const Semver &b) const { return ::semver_satisfies_patch(ver, b.ver); }
bool operator^(const Semver &b) const { return ::semver_satisfies_caret(ver, b.ver); }
bool in_range(const Semver &low, const Semver &high) const { return low <= *this && *this <= high; }
// Conversion
std::string to_string() const {
auto res = (boost::format("%1%.%2%.%3%") % ver.major % ver.minor % ver.patch).str();
if (ver.prerelease != nullptr) { res += '-'; res += ver.prerelease; }
if (ver.metadata != nullptr) { res += '+'; res += ver.metadata; }
return res;
}
// Arithmetics
Semver& operator+=(const Major &b) { ver.major += b.i; return *this; }
Semver& operator+=(const Minor &b) { ver.minor += b.i; return *this; }
Semver& operator+=(const Patch &b) { ver.patch += b.i; return *this; }
Semver& operator-=(const Major &b) { ver.major -= b.i; return *this; }
Semver& operator-=(const Minor &b) { ver.minor -= b.i; return *this; }
Semver& operator-=(const Patch &b) { ver.patch -= b.i; return *this; }
Semver operator+(const Major &b) const { Semver res(*this); return res += b; }
Semver operator+(const Minor &b) const { Semver res(*this); return res += b; }
Semver operator+(const Patch &b) const { Semver res(*this); return res += b; }
Semver operator-(const Major &b) const { Semver res(*this); return res -= b; }
Semver operator-(const Minor &b) const { Semver res(*this); return res -= b; }
Semver operator-(const Patch &b) const { Semver res(*this); return res -= b; }
private:
semver_t ver;
Semver(semver_t ver) : ver(ver) {}
};
}
#endif

View file

@ -0,0 +1,63 @@
#include "Time.hpp"
namespace Slic3r {
namespace Utils {
time_t parse_time_ISO8601Z(const std::string &sdate)
{
int y, M, d, h, m;
float s;
if (sscanf(sdate.c_str(), "%d-%d-%dT%d:%d:%fZ", &y, &M, &d, &h, &m, &s) != 6)
return (time_t)-1;
struct tm tms;
tms.tm_year = y - 1900; // Year since 1900
tms.tm_mon = M - 1; // 0-11
tms.tm_mday = d; // 1-31
tms.tm_hour = h; // 0-23
tms.tm_min = m; // 0-59
tms.tm_sec = (int)s; // 0-61 (0-60 in C++11)
return mktime(&tms);
}
std::string format_time_ISO8601Z(time_t time)
{
struct tm tms;
#ifdef WIN32
gmtime_s(time, &tms);
#else
gmtime_r(&tms, time);
#endif
char buf[128];
sprintf(buf, "%d-%d-%dT%d:%d:%fZ",
tms.tm_year + 1900
tms.tm_mon + 1
tms.tm_mday
tms.tm_hour
tms.tm_min
tms.tm_sec);
return buf;
}
time_t get_current_time_utc()
{
#ifdef WIN32
SYSTEMTIME st;
::GetSystemTime(&st);
std::tm tm;
tm.tm_sec = st.wSecond;
tm.tm_min = st.wMinute;
tm.tm_hour = st.wHour;
tm.tm_mday = st.wDay;
tm.tm_mon = st.wMonth - 1;
tm.tm_year = st.wYear - 1900;
tm.tm_isdst = -1;
return mktime(&tm);
#else
return gmtime();
#endif
}
}; // namespace Utils
}; // namespace Slic3r
#endif /* slic3r_Utils_Time_hpp_ */

View file

@ -0,0 +1,22 @@
#ifndef slic3r_Utils_Time_hpp_
#define slic3r_Utils_Time_hpp_
#include <string>
#include <time.h>
namespace Slic3r {
namespace Utils {
// Utilities to convert an UTC time_t to/from an ISO8601 time format,
// useful for putting timestamps into file and directory names.
// Returns (time_t)-1 on error.
extern time_t parse_time_ISO8601Z(const std::string &s);
extern std::string format_time_ISO8601Z(time_t time);
// There is no gmtime() on windows.
time_t get_current_time_utc();
}; // namespace Utils
}; // namespace Slic3r
#endif /* slic3r_Utils_Time_hpp_ */