OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::GUI::Config::Snapshot Class Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/Config/Snapshot.hpp>

Collaboration diagram for Slic3r::GUI::Config::Snapshot:

Classes

struct  VendorConfig

Public Types

enum  Reason {
  SNAPSHOT_UNKNOWN , SNAPSHOT_UPGRADE , SNAPSHOT_DOWNGRADE , SNAPSHOT_BEFORE_ROLLBACK ,
  SNAPSHOT_USER
}

Public Member Functions

 Snapshot ()
void clear ()
void load_ini (const std::string &path)
void save_ini (const std::string &path)
void export_selections (AppConfig &config) const
void export_vendor_configs (AppConfig &config) const
bool equal_to_active (const AppConfig &app_config) const
std::string format_reason () const

Public Attributes

std::string id
std::time_t time_captured
Semver slic3r_version_captured = Semver::invalid()
std::string comment
Reason reason
std::string print
std::string sla_print
std::vector< std::string > filaments
std::string sla_material
std::string printer
std::string physical_printer
std::vector< VendorConfig > vendor_configs

Detailed Description


Class Documentation

◆ Slic3r::GUI::Config::Snapshot::VendorConfig

struct Slic3r::GUI::Config::Snapshot::VendorConfig
Collaboration diagram for Slic3r::GUI::Config::Snapshot::VendorConfig:
Class Members
map< string, set< string > > models_variants_installed
string name
Version version

Member Enumeration Documentation

◆ Reason

Enumerator
SNAPSHOT_UNKNOWN 
SNAPSHOT_UPGRADE 
SNAPSHOT_DOWNGRADE 
SNAPSHOT_BEFORE_ROLLBACK 
SNAPSHOT_USER 
36 {
42 };
@ SNAPSHOT_UNKNOWN
Definition Snapshot.hpp:37
@ SNAPSHOT_BEFORE_ROLLBACK
Definition Snapshot.hpp:40
@ SNAPSHOT_UPGRADE
Definition Snapshot.hpp:38
@ SNAPSHOT_DOWNGRADE
Definition Snapshot.hpp:39
@ SNAPSHOT_USER
Definition Snapshot.hpp:41

Constructor & Destructor Documentation

◆ Snapshot()

Slic3r::GUI::Config::Snapshot::Snapshot ( )
inline
44{ clear(); }
void clear()
Definition Snapshot.cpp:57

References clear().

Here is the call graph for this function:

Member Function Documentation

◆ clear()

void Slic3r::GUI::Config::Snapshot::clear ( )
58{
59 this->id.clear();
60 this->time_captured = 0;
62 this->comment.clear();
64 this->print.clear();
65 this->sla_print.clear();
66 this->filaments.clear();
67 this->sla_material.clear();
68 this->printer.clear();
69 this->physical_printer.clear();
70}
std::string physical_printer
Definition Snapshot.hpp:77
std::string sla_material
Definition Snapshot.hpp:75
std::string sla_print
Definition Snapshot.hpp:73
std::string print
Definition Snapshot.hpp:72
Semver slic3r_version_captured
Definition Snapshot.hpp:64
std::string printer
Definition Snapshot.hpp:76
Reason reason
Definition Snapshot.hpp:67
std::string comment
Definition Snapshot.hpp:66
std::time_t time_captured
Definition Snapshot.hpp:62
std::vector< std::string > filaments
Definition Snapshot.hpp:74
static const Semver invalid()
Definition Semver.hpp:81

References comment, filaments, Slic3r::Semver::invalid(), physical_printer, print, printer, reason, sla_material, sla_print, slic3r_version_captured, SNAPSHOT_UNKNOWN, and time_captured.

Referenced by Snapshot(), and load_ini().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ equal_to_active()

bool Slic3r::GUI::Config::Snapshot::equal_to_active ( const AppConfig & app_config) const
274{
275 // 1) Check, whether this snapshot contains the same set of active vendors, printer models and variants
276 // as app_config.
277 {
278 std::set<std::string> matched;
279 for (const VendorConfig &vc : this->vendor_configs) {
280 auto it_vendor_models_variants = app_config.vendors().find(vc.name);
281 if (it_vendor_models_variants == app_config.vendors().end() ||
282 it_vendor_models_variants->second != vc.models_variants_installed)
283 // There are more vendors enabled in the snapshot than currently installed.
284 return false;
285 matched.insert(vc.name);
286 }
287 for (const auto &v : app_config.vendors())
288 if (matched.find(v.first) == matched.end() && ! v.second.empty())
289 // There are more vendors currently installed than enabled in the snapshot.
290 return false;
291 }
292
293 // 2) Check, whether this snapshot references the same set of ini files as the current state.
294 boost::filesystem::path data_dir = boost::filesystem::path(Slic3r::data_dir());
295 boost::filesystem::path snapshot_dir = boost::filesystem::path(Slic3r::data_dir()) / SLIC3R_SNAPSHOTS_DIR / this->id;
296 for (const char *subdir : snapshot_subdirs) {
297 boost::filesystem::path path1 = data_dir / subdir;
298 boost::filesystem::path path2 = snapshot_dir / subdir;
299 std::vector<std::string> files1, files2;
300 if (boost::filesystem::is_directory(path1))
301 for (auto &dir_entry : boost::filesystem::directory_iterator(path1))
302 if (Slic3r::is_ini_file(dir_entry))
303 files1.emplace_back(dir_entry.path().filename().string());
304 if (boost::filesystem::is_directory(path2))
305 for (auto &dir_entry : boost::filesystem::directory_iterator(path2))
306 if (Slic3r::is_ini_file(dir_entry))
307 files2.emplace_back(dir_entry.path().filename().string());
308 std::sort(files1.begin(), files1.end());
309 std::sort(files2.begin(), files2.end());
310 if (files1 != files2)
311 return false;
312 for (const std::string &filename : files1) {
313 FILE *f1 = boost::nowide::fopen((path1 / filename).string().c_str(), "rb");
314 FILE *f2 = boost::nowide::fopen((path2 / filename).string().c_str(), "rb");
315 bool same = true;
316 if (f1 && f2) {
317 char buf1[4096];
318 char buf2[4096];
319 do {
320 size_t r1 = fread(buf1, 1, 4096, f1);
321 size_t r2 = fread(buf2, 1, 4096, f2);
322 if (r1 != r2 || memcmp(buf1, buf2, r1)) {
323 same = false;
324 break;
325 }
326 } while (! feof(f1) || ! feof(f2));
327 } else
328 same = false;
329 if (f1)
330 fclose(f1);
331 if (f2)
332 fclose(f2);
333 if (! same)
334 return false;
335 }
336 }
337 return true;
338}
#define SLIC3R_SNAPSHOTS_DIR
Definition Snapshot.cpp:50
std::vector< VendorConfig > vendor_configs
Definition Snapshot.hpp:91
std::string id
Definition Snapshot.hpp:61
static constexpr auto snapshot_subdirs
Definition Snapshot.cpp:267
const std::string & data_dir()
Definition utils.cpp:365
bool is_ini_file(const boost::filesystem::directory_entry &dir_entry)
Definition utils.cpp:1197

References Slic3r::data_dir(), id, Slic3r::is_ini_file(), SLIC3R_SNAPSHOTS_DIR, Slic3r::GUI::Config::snapshot_subdirs, vendor_configs, and Slic3r::AppConfig::vendors().

Here is the call graph for this function:

◆ export_selections()

void Slic3r::GUI::Config::Snapshot::export_selections ( AppConfig & config) const
242{
243 assert(filaments.size() >= 1);
244 config.clear_section("presets");
245 config.set("presets", PRESET_PRINT_NAME, print);
246 config.set("presets", "sla_print", sla_print);
247 config.set("presets", PRESET_FILAMENT_NAME, filaments.front());
248 for (unsigned i = 1; i < filaments.size(); ++i) {
249 char name[64];
250 sprintf(name, "filament_%u", i);
251 config.set("presets", name, filaments[i]);
252 }
253 config.set("presets", "sla_material", sla_material);
254 config.set("presets", PRESET_PRINTER_NAME, printer);
255 config.set("presets", "physical_printer", physical_printer);
256}
#define PRESET_PRINTER_NAME
Definition Preset.hpp:35
#define PRESET_PRINT_NAME
Definition Preset.hpp:34
#define PRESET_FILAMENT_NAME
Definition Preset.hpp:33

References Slic3r::AppConfig::clear_section(), filaments, physical_printer, PRESET_FILAMENT_NAME, PRESET_PRINT_NAME, PRESET_PRINTER_NAME, print, printer, Slic3r::AppConfig::set(), sla_material, and sla_print.

Here is the call graph for this function:

◆ export_vendor_configs()

void Slic3r::GUI::Config::Snapshot::export_vendor_configs ( AppConfig & config) const
259{
260 std::map<std::string, std::map<std::string, std::set<std::string>>> vendors;
261 for (const VendorConfig &vc : vendor_configs)
262 vendors[vc.name] = vc.models_variants_installed;
263 config.set_vendors(std::move(vendors));
264}

References Slic3r::AppConfig::set_vendors(), and vendor_configs.

Here is the call graph for this function:

◆ format_reason()

std::string Slic3r::GUI::Config::Snapshot::format_reason ( ) const

◆ load_ini()

void Slic3r::GUI::Config::Snapshot::load_ini ( const std::string & path)
73{
74 this->clear();
75
76 auto throw_on_parse_error = [&path](const std::string &msg) {
77 throw file_parser_error(std::string("Failed loading the snapshot file. Reason: ") + msg, path);
78 };
79
80 // Load the snapshot.ini file.
81 boost::property_tree::ptree tree;
82 try {
83 boost::nowide::ifstream ifs(path);
84 boost::property_tree::read_ini(ifs, tree);
85 } catch (const std::ifstream::failure &err) {
86 throw file_parser_error(std::string("The snapshot file cannot be loaded. Reason: ") + err.what(), path);
87 } catch (const std::runtime_error &err) {
88 throw_on_parse_error(err.what());
89 }
90
91 // Parse snapshot.ini
92 std::string group_name_vendor = "Vendor:";
93 std::string key_filament = "filament";
94 std::string key_prefix_model = "model_";
95 for (auto &section : tree) {
96 if (section.first == "snapshot") {
97 // Parse the common section.
98 for (auto &kvp : section.second) {
99 if (kvp.first == "id")
100 this->id = kvp.second.data();
101 else if (kvp.first == "time_captured") {
102 this->time_captured = Slic3r::Utils::parse_iso_utc_timestamp(kvp.second.data());
103 if (this->time_captured == (time_t)-1)
104 throw_on_parse_error("invalid timestamp");
105 } else if (kvp.first == "slic3r_version_captured") {
106 auto semver = Semver::parse(kvp.second.data());
107 if (! semver)
108 throw_on_parse_error("invalid slic3r_version_captured semver");
109 this->slic3r_version_captured = *semver;
110 } else if (kvp.first == "comment") {
111 this->comment = kvp.second.data();
112 } else if (kvp.first == "reason") {
113 std::string rsn = kvp.second.data();
114 if (rsn == "upgrade")
115 this->reason = SNAPSHOT_UPGRADE;
116 else if (rsn == "downgrade")
118 else if (rsn == "before_rollback")
120 else if (rsn == "user")
121 this->reason = SNAPSHOT_USER;
122 else
123 this->reason = SNAPSHOT_UNKNOWN;
124 }
125 }
126 } else if (section.first == "presets") {
127 // Load the names of the active presets.
128 for (auto &kvp : section.second) {
129 if (kvp.first == PRESET_PRINT_NAME) {
130 this->print = kvp.second.data();
131 } else if (kvp.first == "sla_print") {
132 this->sla_print = kvp.second.data();
133 } else if (boost::starts_with(kvp.first, PRESET_FILAMENT_NAME)) {
134 int idx = 0;
135 if (kvp.first == "filament" || sscanf(kvp.first.c_str(), "filament_%d", &idx) == 1) {
136 if (int(this->filaments.size()) <= idx)
137 this->filaments.resize(idx + 1, std::string());
138 this->filaments[idx] = kvp.second.data();
139 }
140 } else if (kvp.first == "sla_material") {
141 this->sla_material = kvp.second.data();
142 } else if (kvp.first == PRESET_PRINTER_NAME) {
143 this->printer = kvp.second.data();
144 } else if (kvp.first == "physical_printer") {
145 this->physical_printer = kvp.second.data();
146 }
147 }
148 } else if (boost::starts_with(section.first, group_name_vendor) && section.first.size() > group_name_vendor.size()) {
149 // Vendor specific section.
150 VendorConfig vc;
151 vc.name = section.first.substr(group_name_vendor.size());
152 for (auto &kvp : section.second) {
153 if (kvp.first == "version" || kvp.first == "min_slic3r_version" || kvp.first == "max_slic3r_version") {
154 // Version of the vendor specific config bundle bundled with this snapshot.
155 auto semver = Semver::parse(kvp.second.data());
156 if (! semver)
157 throw_on_parse_error("invalid " + kvp.first + " format for " + section.first);
158 if (kvp.first == "version")
159 vc.version.config_version = *semver;
160 else if (kvp.first == "min_slic3r_version")
161 vc.version.min_slic3r_version = *semver;
162 else
163 vc.version.max_slic3r_version = *semver;
164 } else if (boost::starts_with(kvp.first, key_prefix_model) && kvp.first.size() > key_prefix_model.size()) {
165 // Parse the printer variants installed for the current model.
166 auto &set_variants = vc.models_variants_installed[kvp.first.substr(key_prefix_model.size())];
167 std::vector<std::string> variants;
168 if (unescape_strings_cstyle(kvp.second.data(), variants))
169 for (auto &variant : variants)
170 set_variants.insert(std::move(variant));
171 }
172 }
173 this->vendor_configs.emplace_back(std::move(vc));
174 }
175 }
176 // Sort the vendors lexicographically.
177 std::sort(this->vendor_configs.begin(), this->vendor_configs.begin(),
178 [](const VendorConfig &cfg1, const VendorConfig &cfg2) { return cfg1.name < cfg2.name; });
179}
std::string name
Definition Snapshot.hpp:84
static boost::optional< Semver > parse(const std::string &str)
Definition Semver.hpp:63
time_t parse_iso_utc_timestamp(const std::string &str)
Definition Time.hpp:61
bool unescape_strings_cstyle(const std::string &str, std::vector< std::string > &out)
Definition Config.cpp:173

References clear(), comment, Slic3r::GUI::Config::Version::config_version, filaments, Slic3r::GUI::Config::Version::max_slic3r_version, Slic3r::GUI::Config::Version::min_slic3r_version, Slic3r::GUI::Config::Snapshot::VendorConfig::models_variants_installed, Slic3r::GUI::Config::Snapshot::VendorConfig::name, Slic3r::Semver::parse(), Slic3r::Utils::parse_iso_utc_timestamp(), physical_printer, PRESET_FILAMENT_NAME, PRESET_PRINT_NAME, PRESET_PRINTER_NAME, print, printer, reason, sla_material, sla_print, slic3r_version_captured, SNAPSHOT_BEFORE_ROLLBACK, SNAPSHOT_DOWNGRADE, SNAPSHOT_UNKNOWN, SNAPSHOT_UPGRADE, SNAPSHOT_USER, time_captured, Slic3r::unescape_strings_cstyle(), vendor_configs, and Slic3r::GUI::Config::Snapshot::VendorConfig::version.

Here is the call graph for this function:

◆ save_ini()

void Slic3r::GUI::Config::Snapshot::save_ini ( const std::string & path)
199{
200 boost::nowide::ofstream c;
201 c.open(path, std::ios::out | std::ios::trunc);
202 c << "# " << Slic3r::header_slic3r_generated() << std::endl;
203
204 // Export the common "snapshot".
205 c << std::endl << "[snapshot]" << std::endl;
206 c << "id = " << this->id << std::endl;
207 c << "time_captured = " << Slic3r::Utils::iso_utc_timestamp(this->time_captured) << std::endl;
208 c << "slic3r_version_captured = " << this->slic3r_version_captured.to_string() << std::endl;
209 c << "comment = " << this->comment << std::endl;
210 c << "reason = " << reason_string(this->reason) << std::endl;
211
212 // Export the active presets at the time of the snapshot.
213 c << std::endl << "[presets]" << std::endl;
214 c << "print = " << this->print << std::endl;
215 c << "sla_print = " << this->sla_print << std::endl;
216 c << "filament = " << this->filaments.front() << std::endl;
217 for (size_t i = 1; i < this->filaments.size(); ++ i)
218 c << "filament_" << std::to_string(i) << " = " << this->filaments[i] << std::endl;
219 c << "sla_material = " << this->sla_material << std::endl;
220 c << "printer = " << this->printer << std::endl;
221 c << "physical_printer = " << this->physical_printer << std::endl;
222
223 // Export the vendor configs.
224 for (const VendorConfig &vc : this->vendor_configs) {
225 c << std::endl << "[Vendor:" << vc.name << "]" << std::endl;
226 c << "version = " << vc.version.config_version.to_string() << std::endl;
227 c << "min_slic3r_version = " << vc.version.min_slic3r_version.to_string() << std::endl;
228 c << "max_slic3r_version = " << vc.version.max_slic3r_version.to_string() << std::endl;
229 // Export installed printer models and their variants.
230 for (const auto &model : vc.models_variants_installed) {
231 if (model.second.size() == 0)
232 continue;
233 const std::vector<std::string> variants(model.second.begin(), model.second.end());
234 const auto escaped = escape_strings_cstyle(variants);
235 c << "model_" << model.first << " = " << escaped << std::endl;
236 }
237 }
238 c.close();
239}
static std::string reason_string(const Snapshot::Reason reason)
Definition Snapshot.cpp:181
std::string iso_utc_timestamp()
Definition Time.hpp:56
std::string header_slic3r_generated()
Definition utils.cpp:1470
std::string escape_strings_cstyle(const std::vector< std::string > &strs)
Definition Config.cpp:99

References comment, Slic3r::escape_strings_cstyle(), filaments, Slic3r::header_slic3r_generated(), Slic3r::Utils::iso_utc_timestamp(), physical_printer, print, printer, reason, Slic3r::GUI::Config::reason_string(), sla_material, sla_print, slic3r_version_captured, time_captured, and vendor_configs.

Here is the call graph for this function:

Member Data Documentation

◆ comment

std::string Slic3r::GUI::Config::Snapshot::comment

Referenced by clear(), load_ini(), and save_ini().

◆ filaments

std::vector<std::string> Slic3r::GUI::Config::Snapshot::filaments

◆ id

std::string Slic3r::GUI::Config::Snapshot::id

Referenced by equal_to_active().

◆ physical_printer

std::string Slic3r::GUI::Config::Snapshot::physical_printer

◆ print

std::string Slic3r::GUI::Config::Snapshot::print

◆ printer

std::string Slic3r::GUI::Config::Snapshot::printer

◆ reason

Reason Slic3r::GUI::Config::Snapshot::reason

Referenced by clear(), load_ini(), and save_ini().

◆ sla_material

std::string Slic3r::GUI::Config::Snapshot::sla_material

◆ sla_print

std::string Slic3r::GUI::Config::Snapshot::sla_print

◆ slic3r_version_captured

Semver Slic3r::GUI::Config::Snapshot::slic3r_version_captured = Semver::invalid()

Referenced by clear(), load_ini(), and save_ini().

◆ time_captured

std::time_t Slic3r::GUI::Config::Snapshot::time_captured

Referenced by clear(), load_ini(), and save_ini().

◆ vendor_configs

std::vector<VendorConfig> Slic3r::GUI::Config::Snapshot::vendor_configs

The documentation for this class was generated from the following files:
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/Config/Snapshot.hpp
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/Config/Snapshot.cpp