2018-11-13 18:43:27 -08:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright (c) 2016, Lars Brubaker
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
|
list of conditions and the following disclaimer.
|
|
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
The views and conclusions contained in the software and documentation are those
|
|
|
|
|
|
of the authors and should not be interpreted as representing official policies,
|
|
|
|
|
|
either expressed or implied, of the FreeBSD Project.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Text;
|
2019-05-18 07:25:00 -07:00
|
|
|
|
using MatterHackers.Agg;
|
2018-11-13 18:43:27 -08:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl.SlicerConfiguration.MappingClasses
|
|
|
|
|
|
{
|
|
|
|
|
|
public class MapStartGCode : InjectGCodeCommands
|
|
|
|
|
|
{
|
2019-05-18 07:25:00 -07:00
|
|
|
|
private readonly bool escapeNewlineCharacters;
|
2018-11-13 18:43:27 -08:00
|
|
|
|
|
2019-06-26 12:43:44 -07:00
|
|
|
|
public MapStartGCode(bool escapeNewlineCharacters)
|
2018-11-13 18:43:27 -08:00
|
|
|
|
{
|
|
|
|
|
|
this.escapeNewlineCharacters = escapeNewlineCharacters;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-26 12:43:44 -07:00
|
|
|
|
public override string Resolve(string value, PrinterSettings settings)
|
2018-11-13 18:43:27 -08:00
|
|
|
|
{
|
2019-06-26 12:43:44 -07:00
|
|
|
|
System.Diagnostics.Debugger.Break();
|
2018-11-13 18:43:27 -08:00
|
|
|
|
|
2019-06-26 12:43:44 -07:00
|
|
|
|
var newStartGCode = new StringBuilder();
|
2018-11-13 18:43:27 -08:00
|
|
|
|
|
2019-06-26 12:43:44 -07:00
|
|
|
|
//foreach (string line in PreStartGCode(Slicer.ExtrudersUsed))
|
|
|
|
|
|
//{
|
|
|
|
|
|
// newStartGCode.Append(line + "\n");
|
|
|
|
|
|
//}
|
2018-11-13 18:43:27 -08:00
|
|
|
|
|
2019-06-26 12:43:44 -07:00
|
|
|
|
value = base.Resolve(value, settings);
|
|
|
|
|
|
|
|
|
|
|
|
newStartGCode.Append(settings.ReplaceMacroValues(value));
|
|
|
|
|
|
|
|
|
|
|
|
//foreach (string line in PostStartGCode(Slicer.ExtrudersUsed))
|
|
|
|
|
|
//{
|
|
|
|
|
|
// newStartGCode.Append("\n");
|
|
|
|
|
|
// newStartGCode.Append(line);
|
|
|
|
|
|
//}
|
2018-11-13 18:43:27 -08:00
|
|
|
|
|
2019-06-26 12:43:44 -07:00
|
|
|
|
if (escapeNewlineCharacters)
|
|
|
|
|
|
{
|
|
|
|
|
|
return newStartGCode.ToString().Replace("\n", "\\n");
|
2018-11-13 18:43:27 -08:00
|
|
|
|
}
|
2019-06-26 12:43:44 -07:00
|
|
|
|
|
|
|
|
|
|
return newStartGCode.ToString();
|
2018-11-13 18:43:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-26 12:43:44 -07:00
|
|
|
|
public List<string> PreStartGCode(PrinterSettings settings, List<bool> extrudersUsed)
|
2018-11-13 18:43:27 -08:00
|
|
|
|
{
|
2019-06-26 12:43:44 -07:00
|
|
|
|
string startGCode = settings.GetValue(SettingsKey.start_gcode);
|
2019-06-15 07:51:26 -07:00
|
|
|
|
string[] startGCodeLines = startGCode.Split(new string[] { "\\n" }, StringSplitOptions.RemoveEmptyEntries);
|
2018-11-13 18:43:27 -08:00
|
|
|
|
|
2019-05-18 07:25:00 -07:00
|
|
|
|
var preStartGCode = new List<string>
|
|
|
|
|
|
{
|
|
|
|
|
|
"; automatic settings before start_gcode"
|
|
|
|
|
|
};
|
2019-06-15 07:51:26 -07:00
|
|
|
|
AddDefaultIfNotPresent(preStartGCode, "G21", startGCodeLines, "set units to millimeters");
|
|
|
|
|
|
AddDefaultIfNotPresent(preStartGCode, "M107", startGCodeLines, "fan off");
|
2019-06-26 12:43:44 -07:00
|
|
|
|
double bed_temperature = settings.GetValue<double>(SettingsKey.bed_temperature);
|
2018-11-13 18:43:27 -08:00
|
|
|
|
if (bed_temperature > 0)
|
|
|
|
|
|
{
|
2019-03-04 16:43:29 -08:00
|
|
|
|
string setBedTempString = string.Format("M140 S{0}", bed_temperature);
|
2019-06-15 07:51:26 -07:00
|
|
|
|
AddDefaultIfNotPresent(preStartGCode, setBedTempString, startGCodeLines, "start heating the bed");
|
2018-11-13 18:43:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-26 12:43:44 -07:00
|
|
|
|
int numberOfHeatedExtruders = settings.Helpers.HotendCount();
|
2018-11-13 18:43:27 -08:00
|
|
|
|
|
|
|
|
|
|
// Start heating all the extruder that we are going to use.
|
2019-06-15 07:51:26 -07:00
|
|
|
|
for (int hotendIndex = 0; hotendIndex < numberOfHeatedExtruders; hotendIndex++)
|
2018-11-13 18:43:27 -08:00
|
|
|
|
{
|
2019-06-15 07:51:26 -07:00
|
|
|
|
if (extrudersUsed.Count > hotendIndex
|
|
|
|
|
|
&& extrudersUsed[hotendIndex])
|
2018-11-13 18:43:27 -08:00
|
|
|
|
{
|
2019-06-26 12:43:44 -07:00
|
|
|
|
double materialTemperature = settings.Helpers.ExtruderTargetTemperature(hotendIndex);
|
2018-11-13 18:43:27 -08:00
|
|
|
|
if (materialTemperature != 0)
|
|
|
|
|
|
{
|
2019-06-15 07:51:26 -07:00
|
|
|
|
string setTempString = "M104 T{0} S{1}".FormatWith(hotendIndex, materialTemperature);
|
|
|
|
|
|
AddDefaultIfNotPresent(preStartGCode, setTempString, startGCodeLines, $"start heating T{hotendIndex}");
|
2018-11-13 18:43:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we need to wait for the heaters to heat up before homing then set them to M109 (heat and wait).
|
2019-06-26 12:43:44 -07:00
|
|
|
|
if (settings.GetValue<bool>(SettingsKey.heat_extruder_before_homing))
|
2018-11-13 18:43:27 -08:00
|
|
|
|
{
|
2019-06-15 07:51:26 -07:00
|
|
|
|
for (int hotendIndex = 0; hotendIndex < numberOfHeatedExtruders; hotendIndex++)
|
2018-11-13 18:43:27 -08:00
|
|
|
|
{
|
2019-06-15 07:51:26 -07:00
|
|
|
|
if (extrudersUsed.Count > hotendIndex
|
|
|
|
|
|
&& extrudersUsed[hotendIndex])
|
2018-11-13 18:43:27 -08:00
|
|
|
|
{
|
2019-06-26 12:43:44 -07:00
|
|
|
|
double materialTemperature = settings.Helpers.ExtruderTargetTemperature(hotendIndex);
|
2018-11-13 18:43:27 -08:00
|
|
|
|
if (materialTemperature != 0)
|
|
|
|
|
|
{
|
2019-06-15 07:51:26 -07:00
|
|
|
|
string setTempString = "M109 T{0} S{1}".FormatWith(hotendIndex, materialTemperature);
|
|
|
|
|
|
AddDefaultIfNotPresent(preStartGCode, setTempString, startGCodeLines, $"wait for T{hotendIndex }");
|
2018-11-13 18:43:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-05-18 07:25:00 -07:00
|
|
|
|
// If we have bed temp and the start gcode specifies to finish heating the extruders,
|
|
|
|
|
|
// make sure we also finish heating the bed. This preserves legacy expectation.
|
|
|
|
|
|
if (bed_temperature > 0
|
|
|
|
|
|
&& startGCode.Contains("M109"))
|
2019-05-17 18:09:55 -07:00
|
|
|
|
{
|
|
|
|
|
|
string setBedTempString = string.Format("M190 S{0}", bed_temperature);
|
2019-06-15 07:51:26 -07:00
|
|
|
|
AddDefaultIfNotPresent(preStartGCode, setBedTempString, startGCodeLines, "wait for bed temperature to be reached");
|
2019-05-17 18:09:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-05-18 07:25:00 -07:00
|
|
|
|
SwitchToFirstActiveExtruder(extrudersUsed, preStartGCode);
|
|
|
|
|
|
preStartGCode.Add("; settings from start_gcode");
|
|
|
|
|
|
|
2018-11-13 18:43:27 -08:00
|
|
|
|
return preStartGCode;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-26 12:43:44 -07:00
|
|
|
|
public List<string> PostStartGCode(PrinterSettings settings, List<bool> extrudersUsed)
|
2019-03-04 16:43:29 -08:00
|
|
|
|
{
|
2019-06-26 12:43:44 -07:00
|
|
|
|
string startGCode = settings.GetValue(SettingsKey.start_gcode);
|
2019-06-15 07:51:26 -07:00
|
|
|
|
string[] startGCodeLines = startGCode.Split(new string[] { "\\n" }, StringSplitOptions.RemoveEmptyEntries);
|
2019-03-04 16:43:29 -08:00
|
|
|
|
|
2019-05-18 07:25:00 -07:00
|
|
|
|
var postStartGCode = new List<string>
|
|
|
|
|
|
{
|
|
|
|
|
|
"; automatic settings after start_gcode"
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2019-06-26 12:43:44 -07:00
|
|
|
|
double bed_temperature = settings.GetValue<double>(SettingsKey.bed_temperature);
|
2019-05-18 07:25:00 -07:00
|
|
|
|
if (bed_temperature > 0
|
|
|
|
|
|
&& !startGCode.Contains("M109"))
|
|
|
|
|
|
{
|
|
|
|
|
|
string setBedTempString = string.Format("M190 S{0}", bed_temperature);
|
2019-06-15 07:51:26 -07:00
|
|
|
|
AddDefaultIfNotPresent(postStartGCode, setBedTempString, startGCodeLines, "wait for bed temperature to be reached");
|
2019-05-18 07:25:00 -07:00
|
|
|
|
}
|
2019-03-04 16:43:29 -08:00
|
|
|
|
|
2019-06-26 12:43:44 -07:00
|
|
|
|
int numberOfHeatedExtruders = settings.GetValue<int>(SettingsKey.extruder_count);
|
2019-03-04 16:43:29 -08:00
|
|
|
|
// wait for them to finish
|
2019-06-15 07:51:26 -07:00
|
|
|
|
for (int hotendIndex = 0; hotendIndex < numberOfHeatedExtruders; hotendIndex++)
|
2019-03-04 16:43:29 -08:00
|
|
|
|
{
|
2019-06-15 07:51:26 -07:00
|
|
|
|
if (hotendIndex < extrudersUsed.Count
|
|
|
|
|
|
&& extrudersUsed[hotendIndex])
|
2019-03-04 16:43:29 -08:00
|
|
|
|
{
|
2019-06-26 12:43:44 -07:00
|
|
|
|
double materialTemperature = settings.Helpers.ExtruderTargetTemperature(hotendIndex);
|
2019-03-04 16:43:29 -08:00
|
|
|
|
if (materialTemperature != 0)
|
|
|
|
|
|
{
|
2019-06-15 07:51:26 -07:00
|
|
|
|
if (!(hotendIndex == 0 && LineStartsWith(startGCodeLines, "M109 S"))
|
|
|
|
|
|
&& !LineStartsWith(startGCodeLines, $"M109 T{hotendIndex} S"))
|
|
|
|
|
|
{
|
|
|
|
|
|
// always heat the extruders that are used beyond extruder 0
|
|
|
|
|
|
postStartGCode.Add($"M109 T{hotendIndex} S{materialTemperature} ; Finish heating T{hotendIndex}");
|
|
|
|
|
|
}
|
2019-03-04 16:43:29 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-05-18 07:25:00 -07:00
|
|
|
|
SwitchToFirstActiveExtruder(extrudersUsed, postStartGCode);
|
2019-06-15 07:51:26 -07:00
|
|
|
|
AddDefaultIfNotPresent(postStartGCode, "G90", startGCodeLines, "use absolute coordinates");
|
2019-03-04 16:43:29 -08:00
|
|
|
|
postStartGCode.Add(string.Format("{0} ; {1}", "G92 E0", "reset the expected extruder position"));
|
2019-06-15 07:51:26 -07:00
|
|
|
|
AddDefaultIfNotPresent(postStartGCode, "M82", startGCodeLines, "use absolute distance for extrusion");
|
2019-03-04 16:43:29 -08:00
|
|
|
|
|
|
|
|
|
|
return postStartGCode;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-05-18 07:25:00 -07:00
|
|
|
|
private void SwitchToFirstActiveExtruder(List<bool> extrudersUsed, List<string> preStartGCode)
|
2018-11-13 18:43:27 -08:00
|
|
|
|
{
|
|
|
|
|
|
// make sure we are on the first active extruder
|
|
|
|
|
|
for (int extruderIndex = 0; extruderIndex < extrudersUsed.Count; extruderIndex++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (extrudersUsed[extruderIndex])
|
|
|
|
|
|
{
|
|
|
|
|
|
// set the active extruder to the first one that will be printing
|
2019-03-04 12:17:55 -08:00
|
|
|
|
preStartGCode.Add("T{0} ; {1}".FormatWith(extruderIndex, "set the active extruder to {0}".FormatWith(extruderIndex)));
|
|
|
|
|
|
// we have set the active extruder so don't set it to any other extruder
|
|
|
|
|
|
break;
|
2018-11-13 18:43:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|