Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
PilotMatt committed Feb 26, 2024
1 parent a857816 commit a79dcfe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
63 changes: 46 additions & 17 deletions src/slic3r/Utils/PrintagoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,8 @@ bool PrintagoDirector::ProcessPrintagoCommand(const PrintagoCommand& cmd)
PBJob::configFiles["filament"] = localFilamentConf;
PBJob::configFiles["print"] = localPrintConf;

wxFileName localOverridePrintConf;
if (!overridePrintConfUrl.empty()) {
wxFileName localOverridePrintConf;
if (!SavePrintagoFile(overridePrintConfUrl, localOverridePrintConf)) {
PostErrorMessage(printerId, wxString::Format("%s:%s", action, PBJob::serverStateStr()), originalCommand,
"override config download failed", true);
Expand Down Expand Up @@ -873,9 +873,11 @@ bool PrintagoDirector::ProcessPrintagoCommand(const PrintagoCommand& cmd)

void PrintagoDirector::OverridePrintSettings()
{
auto fullOptionsList = Preset::print_options();
auto fullOptionsList = Preset::print_options();
std::vector<std::string> noOverrideKeys = PBJob::PrintSettingsNotToOverride();
std::vector<std::string> layerHeightKeys = {"layer_height", "initial_layer_height", "independent_support_layer_height"};
std::vector<std::string> layerHeightKeys = {"layer_height", "initial_layer_height", "independent_support_layer_height" };
std::vector<std::string> nameKeys = {"name", "print_settings_id" };


std::vector<std::string> optionsToUse;
std::copy_if(fullOptionsList.begin(), fullOptionsList.end(), std::back_inserter(optionsToUse),
Expand All @@ -886,24 +888,35 @@ void PrintagoDirector::OverridePrintSettings()
wxFileName printSettingsPath = PBJob::configFiles["print"].GetFullPath();
wxFileName overrideSettingsPath = PBJob::configFiles["print_override"].GetFullPath();
wxFileName printerSettingsPath = PBJob::configFiles["printer"].GetFullPath();
wxString newProfileName = printSettingsPath.GetName() + "-printago-" + PBJob::jobId.Right(6);

// Load JSON data from files
std::ifstream printSettingsFile(printSettingsPath.GetFullPath());
json printSettings;
printSettingsFile >> printSettings;
printSettingsFile.close();

std::ifstream printerSettingsFile(printerSettingsPath.GetFullPath());
json printerSettings;
printerSettingsFile >> printerSettings;
printerSettingsFile.close();

auto min_layer_height = printerSettings["min_layer_height"].get<double>();
auto max_layer_height = printerSettings["max_layer_height"].get<double>();
auto nozzle_diameter = printerSettings["nozzle_diameter"].get<double>();
double nozzle_diameter = 0.0;
double min_layer_height = 0.0;
double max_layer_height = 0.0;

if (printerSettings.contains("nozzle_diameter") && !printerSettings["nozzle_diameter"].empty()) {
nozzle_diameter = std::stod(printerSettings["nozzle_diameter"][0].get<std::string>());
}

if (printerSettings.contains("min_layer_height") && !printerSettings["min_layer_height"].empty()) {
min_layer_height = std::stod(printerSettings["min_layer_height"][0].get<std::string>());
}

if (printerSettings.contains("max_layer_height") && !printerSettings["max_layer_height"].empty()) {
max_layer_height = std::stod(printerSettings["max_layer_height"][0].get<std::string>());
}

if (max_layer_height == 0.0) {
max_layer_height = nozzle_diameter * 0.75;
max_layer_height = nozzle_diameter * 0.75; // Apply the default calculation if max_layer_height is not set
}

std::ifstream overrideSettingsFile(overrideSettingsPath.GetFullPath());
Expand All @@ -917,18 +930,17 @@ void PrintagoDirector::OverridePrintSettings()
if (printSettings.find(key) != printSettings.end()) {
// Check if the key is one of the layer height keys
if (std::find(layerHeightKeys.begin(), layerHeightKeys.end(), key) != layerHeightKeys.end()) {
// Convert the override value to double (assuming all layer heights are double)
double overrideValue = overrideSettings[key].get<double>();

std::string overrideValueStr = overrideSettings[key].get<std::string>();
double overrideValue = std::stod(overrideValueStr);
// Clamp the override value to be within the min and max layer height
if (overrideValue < min_layer_height) {
overrideValue = min_layer_height;
} else if (overrideValue > max_layer_height) {
overrideValue = max_layer_height;
}

// Set the clamped value
printSettings[key] = overrideValue;
printSettings[key] = FormatDoubleToString(overrideValue);
} else {
// For keys not related to layer height, simply override the value
printSettings[key] = overrideSettings[key];
Expand All @@ -937,15 +949,26 @@ void PrintagoDirector::OverridePrintSettings()
}
}

printSettings["name"] = newProfileName.ToStdString();
printSettings["print_settings_id"] = newProfileName.ToStdString();

wxFileName newPrintSettingsPath(printSettingsPath);
newPrintSettingsPath.SetName(printSettingsPath.GetName() + "_" + PBJob::jobId.Right(6));
newPrintSettingsPath.SetName(newProfileName);
wxString z = newPrintSettingsPath.GetFullPath();
std::ofstream outFile(newPrintSettingsPath.GetFullPath());
outFile << printSettings.dump();
outFile.close();
outFile << printSettings.dump(2);

PBJob::configFiles["print"] = newPrintSettingsPath;
}

std::string PrintagoDirector::FormatDoubleToString(double value, int precision)
{
std::ostringstream out;
out << std::fixed << std::setprecision(precision) << value;
return out.str();
}


//needs UI thread.
void PrintagoDirector::ImportPrintagoConfigs()
{
Expand Down Expand Up @@ -1431,6 +1454,12 @@ void PrintagoDirector::OnPrintJobSent(wxString printerId, bool success)
const json command_copy = PBJob::command;
const wxString printerId_copy = PBJob::printerId;

if(!PBJob::configFiles["print_override"].GetFullPath().IsEmpty()) {
//when we overrode the print settings, we replaced the ["print"] file with a new one.
//this should already be selected in the UI, but we need to re-select it here to ensure we delete the right one.
wxGetApp().get_tab(Preset::TYPE_PRINT)->select_preset(PBJob::configFiles["print"].GetName().ToStdString());
wxGetApp().get_tab(Preset::TYPE_PRINT)->select_preset(PBJob::configFiles["print"].GetName().ToStdString(), true);
}
PBJob::UnblockJobProcessing(); // unblock before notifying the client of the success.

PostSuccessMessage(printerId_copy, "start_print_bbl", command_copy, wxString::Format("print sent to: %s", printerId_copy));
Expand Down
3 changes: 1 addition & 2 deletions src/slic3r/Utils/PrintagoServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ namespace Slic3r {
static constexpr short PRINTAGO_PORT = 33647;
void printago_ws_error(beefy::error_code ec, char const* what);

class PrintagoDirector;

//``````````````````````````````````````````````````
//------------------PrintagoSession------------------
//``````````````````````````````````````````````````
Expand Down Expand Up @@ -226,6 +224,7 @@ class PrintagoDirector
json GetConfigByName(wxString configType, wxString configName);
json GetCompatPrinterConfigNames(std::string printer_type);
void OverridePrintSettings();
std::string FormatDoubleToString(double value, int precision = 2);
void ImportPrintagoConfigs();
void SetPrintagoConfigs();

Expand Down

0 comments on commit a79dcfe

Please sign in to comment.