Moved ProcessWriteRegex to stream

Made it work with gcode export
This commit is contained in:
Lars Brubaker 2017-06-29 13:02:36 -07:00
parent f74d731993
commit f09f8fd939
6 changed files with 142 additions and 78 deletions

View file

@ -0,0 +1,109 @@
/*
Copyright (c) 2015, 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.RegularExpressions;
using MatterHackers.Agg.UI;
using MatterHackers.MatterControl.SlicerConfiguration;
using MatterHackers.VectorMath;
namespace MatterHackers.MatterControl.PrinterCommunication.Io
{
public class ProcessWriteRegExStream : GCodeStreamProxy
{
static Regex getQuotedParts = new Regex(@"([""'])(\\?.)*?\1", RegexOptions.Compiled);
public ProcessWriteRegExStream(GCodeStream internalStream)
: base(internalStream)
{
}
public override string ReadLine()
{
var baseLine = base.ReadLine();
var lines = ProcessWriteRegEx(baseLine);
for (int i = 1; i < lines.Count; i++)
{
PrinterConnection.Instance.SendLineToPrinterNow(lines[i]);
}
return lines[0];
}
static string write_regex = "";
static private List<(Regex Regex, string Replacement)> WriteLineReplacements = new List<(Regex Regex, string Replacement)>();
public static List<string> ProcessWriteRegEx(string lineToWrite)
{
lock (WriteLineReplacements)
{
if (write_regex != ActiveSliceSettings.Instance.GetValue(SettingsKey.write_regex))
{
WriteLineReplacements.Clear();
string splitString = "\\n";
write_regex = ActiveSliceSettings.Instance.GetValue(SettingsKey.write_regex);
foreach (string regExLine in write_regex.Split(splitString.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
{
var matches = getQuotedParts.Matches(regExLine);
if (matches.Count == 2)
{
var search = matches[0].Value.Substring(1, matches[0].Value.Length - 2);
var replace = matches[1].Value.Substring(1, matches[1].Value.Length - 2);
WriteLineReplacements.Add((new Regex(search, RegexOptions.Compiled), replace));
}
}
}
}
var linesToWrite = new List<string>();
linesToWrite.Add(lineToWrite);
var addedLines = new List<string>();
for (int i = 0; i < linesToWrite.Count; i++)
{
foreach (var item in WriteLineReplacements)
{
var replaced = item.Regex.Replace(lineToWrite, item.Replacement);
var replacedLines = replaced.Split(',');
linesToWrite[i] = replacedLines[0];
for (int j = 1; j < replacedLines.Length; j++)
{
addedLines.Add(replacedLines[j]);
}
}
}
linesToWrite.AddRange(addedLines);
return linesToWrite;
}
}
}

View file

@ -32,27 +32,27 @@ using MatterHackers.VectorMath;
namespace MatterHackers.MatterControl.PrinterCommunication.Io
{
public class RequestTemperaturesStream : GCodeStreamProxy
{
private long nextReadTimeMs = 0;
public class RequestTemperaturesStream : GCodeStreamProxy
{
private long nextReadTimeMs = 0;
public RequestTemperaturesStream(GCodeStream internalStream)
: base(internalStream)
{
nextReadTimeMs = UiThread.CurrentTimerMs + 1000;
}
public RequestTemperaturesStream(GCodeStream internalStream)
: base(internalStream)
{
nextReadTimeMs = UiThread.CurrentTimerMs + 1000;
}
public override string ReadLine()
{
if (!PrinterConnection.Instance.WatingForPositionRead
public override string ReadLine()
{
if (!PrinterConnection.Instance.WatingForPositionRead
&& nextReadTimeMs < UiThread.CurrentTimerMs
&& PrinterConnection.Instance.PrinterIsConnected)
{
nextReadTimeMs = UiThread.CurrentTimerMs + 1000;
return "M105";
}
{
nextReadTimeMs = UiThread.CurrentTimerMs + 1000;
return "M105";
}
return base.ReadLine();
}
}
return base.ReadLine();
}
}
}

View file

@ -228,6 +228,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
private ExtrusionMultiplyerStream extrusionMultiplyerStream7 = null;
private FeedRateMultiplyerStream feedrateMultiplyerStream8 = null;
private RequestTemperaturesStream requestTemperaturesStream9 = null;
private ProcessWriteRegExStream processWriteRegExStream10 = null;
private GCodeStream totalGCodeStream = null;
@ -1856,7 +1857,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
if (lineToWrite.Trim().Length > 0)
{
// sometimes we need to send code without buffering (like when we are closing the program).
WriteRawToPrinter(ProcessWriteRegEx(lineToWrite) + "\n", lineToWrite);
WriteRawToPrinter(lineToWrite + "\n", lineToWrite);
}
}
else
@ -1870,9 +1871,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication
}
}
#region RegExProcess
Regex getQuotedParts = new Regex(@"([""'])(\\?.)*?\1", RegexOptions.Compiled);
#region ProcessRead
static Regex getQuotedParts = new Regex(@"([""'])(\\?.)*?\1", RegexOptions.Compiled);
string read_regex = "";
private List<(Regex Regex, string Replacement)> ReadLineReplacements = new List<(Regex Regex, string Replacement)>();
@ -1904,55 +1904,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication
}
#endregion // ProcessRead
#region ProcessWrite
string write_regex = "";
private List<(Regex Regex, string Replacement)> WriteLineReplacements = new List<(Regex Regex, string Replacement)>();
private string ProcessWriteRegEx(string lineToWrite)
{
if (write_regex != ActiveSliceSettings.Instance.GetValue(SettingsKey.write_regex))
{
WriteLineReplacements.Clear();
string splitString = "\\n";
write_regex = ActiveSliceSettings.Instance.GetValue(SettingsKey.write_regex);
foreach (string regExLine in write_regex.Split(splitString.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
{
var matches = getQuotedParts.Matches(regExLine);
if (matches.Count == 2)
{
var search = matches[0].Value.Substring(1, matches[0].Value.Length - 2);
var replace = matches[1].Value.Substring(1, matches[1].Value.Length - 2);
WriteLineReplacements.Add((new Regex(search, RegexOptions.Compiled), replace));
}
}
}
foreach (var item in WriteLineReplacements)
{
var replaced = item.Regex.Replace(lineToWrite, item.Replacement);
if (replaced != lineToWrite)
{
var lines = replaced.Split(',');
if (lines.Length > 1)
{
lineToWrite = lines[0];
for (int i = 1; i < lines.Length; i++)
{
SendLineToPrinterNow(lines[i]);
}
}
else
{
lineToWrite = replaced;
}
}
}
return lineToWrite;
}
#endregion
#endregion
public bool SerialPortIsAvailable(string portName)
//Check is serial port is in the list of available serial ports
{
@ -2221,7 +2172,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
private void ClearQueuedGCode()
{
loadedGCode.Clear();
WriteChecksumLineToPrinter(ProcessWriteRegEx("M110 N1"));
WriteChecksumLineToPrinter("M110 N1");
}
private void Connect_Thread()
@ -2400,7 +2351,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication
extrusionMultiplyerStream7 = new ExtrusionMultiplyerStream(babyStepsStream6);
feedrateMultiplyerStream8 = new FeedRateMultiplyerStream(extrusionMultiplyerStream7);
requestTemperaturesStream9 = new RequestTemperaturesStream(feedrateMultiplyerStream8);
totalGCodeStream = requestTemperaturesStream9;
processWriteRegExStream10 = new ProcessWriteRegExStream(requestTemperaturesStream9);
totalGCodeStream = processWriteRegExStream10;
// Get the current position of the printer any time we reset our streams
ReadPosition();
@ -2645,10 +2597,10 @@ namespace MatterHackers.MatterControl.PrinterCommunication
secondsSinceUpdateHistory = secondsSinceStartedPrint;
}
currentSentLine = currentSentLine.Trim();
// Check if there is anything in front of the ;.
if (currentSentLine.Split(';')[0].Trim().Length > 0)
{
currentSentLine = ProcessWriteRegEx(currentSentLine).Trim();
if (currentSentLine.Length > 0)
{
WriteChecksumLineToPrinter(currentSentLine);