Skip to content

Commit

Permalink
Merge pull request #37 from avmaisak/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
avmaisak committed Jun 10, 2020
2 parents dcd8520 + fc040fa commit b2c2024
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 66 deletions.
21 changes: 18 additions & 3 deletions src/Gcode.Utils/Gcode.Utils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<PackageTags>gcode 3d-printing reprap reprap-3d-printer marlin marlin-firmware repitier repitier-firmware json json-parsing gcode-json cura kisslicer slic3r simplify3d</PackageTags>
<RepositoryType>git</RepositoryType>
<Copyright>Anton Maisak</Copyright>
<AssemblyVersion>0.2.0.20</AssemblyVersion>
<AssemblyVersion>0.2.0.21</AssemblyVersion>
<PackageReleaseNotes></PackageReleaseNotes>
<Version>0.2.20</Version>
<FileVersion>0.2.0.20</FileVersion>
<Version>0.2.21</Version>
<FileVersion>0.2.0.21</FileVersion>
<PackageIconUrl></PackageIconUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageIcon>logo.png</PackageIcon>
Expand Down Expand Up @@ -48,4 +48,19 @@
<PackageReference Include="LibBase" Version="1.0.6" />
</ItemGroup>

<ItemGroup>
<Compile Update="Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

</Project>
33 changes: 14 additions & 19 deletions src/Gcode.Utils/GcodeCrc.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Gcode.Utils.Entity;
using Gcode.Utils.Entity;

namespace Gcode.Utils
{
Expand All @@ -26,30 +25,26 @@ public static class GcodeCrc
/// <returns></returns>
public static int FrameCrc(this GcodeCommandFrame gcodeCommandFrame)
{
if (gcodeCommandFrame.N <= 0) throw new Exception("Frame line number expected (>0)");

var f = gcodeCommandFrame.ToString();
var check = 0;

foreach (var ch in f) check ^= ch & 0xff;

check ^= 32;

return check;
if (gcodeCommandFrame.N <= 0) throw new GcodeException(Resources.FrameLineNumExpected);
return SetCheckSum(gcodeCommandFrame.ToString());
}
// ReSharper disable once UnusedMember.Global
public static int FrameCrc(this string gcodeCommandFrame)
{
var gcode = GcodeParser.ToGCode(gcodeCommandFrame);
if (gcode.N <= 0) throw new Exception("Frame line number expected (>0)");

var f = gcodeCommandFrame;
if (gcode.N <= 0) throw new GcodeException(Resources.FrameLineNumExpected);
return SetCheckSum(gcodeCommandFrame);
}
/// <summary>
/// Set CheckSum.
/// </summary>
/// <param name="rawFrame"></param>
/// <returns></returns>
private static int SetCheckSum(string rawFrame)
{
var check = 0;

foreach (var ch in f) check ^= ch & 0xff;

foreach (var ch in rawFrame) check ^= ch & 0xff;
check ^= 32;

return check;
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/Gcode.Utils/GcodeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Gcode.Utils
{
/// <summary>
/// GcodeException.
/// </summary>
public class GcodeException : Exception {
public GcodeException(string message): base (message: message) {}
}
}
40 changes: 12 additions & 28 deletions src/Gcode.Utils/GcodeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ namespace Gcode.Utils
/// </summary>
public static class GcodeParser
{
#region private
private static readonly CultureInfo Culture = CultureInfo.InvariantCulture;
private static string _rawFrame;
private const string CommentChar = ";";
/// <summary>
/// To Gcode CommandFrame
/// </summary>
Expand All @@ -27,31 +25,21 @@ private static GcodeCommandFrame ToGcodeCommandFrame(IEnumerable<KeyValuePair<st
var gcodeCommandFrame = new GcodeCommandFrame();
foreach (var frameSegment in frameSegments)
{
//команда
var key = frameSegment.Key;
//значение
var value = frameSegment.Value;
//получить свойство кадра
var fieldInfo = gcodeCommandFrame.GetType().GetProperty(key);
var fieldInfo = gcodeCommandFrame.GetType().GetProperty(frameSegment.Key);
//свойство есть
if (fieldInfo == null) continue;

//получить информацию свойства поля кадра
var fileldInfoType = fieldInfo.PropertyType;
// тип универсален, и универсальный тип - Nullable
if (!fileldInfoType.IsGenericType || fileldInfoType.GetGenericTypeDefinition() != typeof(Nullable<>)) continue;
//объект назначения
var obj = gcodeCommandFrame;
//значение поля
var fieldValue = value;
//свойства поля кадра
fileldInfoType = fileldInfoType.GetGenericArguments()[0];
//указание значения сегмента кадра
fieldInfo.SetValue(obj, Convert.ChangeType(fieldValue, fileldInfoType, Culture));
fieldInfo.SetValue(gcodeCommandFrame, Convert.ChangeType(frameSegment.Value, fileldInfoType, Culture));
}
return gcodeCommandFrame;
}
#endregion
/// <summary>
/// To GCode
/// </summary>
Expand Down Expand Up @@ -80,7 +68,7 @@ public static GcodeCommandFrame ToGCode(string raw)
//содержит комментарий
if (_rawFrame.ContainsGcodeComment())
{
var r = _rawFrame.Split(CommentChar);
var r = _rawFrame.Split(Resources.CommentChar);
if (r.Length == 2)
{
_rawFrame = r[0].Trim();
Expand Down Expand Up @@ -122,27 +110,23 @@ public static string ToStringCommand(this GcodeCommandFrame gcodeCommandFrame, b
var isNotEmpty = !string.IsNullOrWhiteSpace(objProp.Value?.Trim());

if (!isNotEmpty) continue;
if (objProp.Key != "Comment")
if (objProp.Key != Resources.CommentTag)
{
var commandKey = objProp.Key;

if (addedLines == 0) commandSeparator = string.Empty;

if (objProp.Key == "CheckSum" && !string.IsNullOrWhiteSpace(objProp.Value)) commandKey = "*";
if (objProp.Key == Resources.CheckSumTag && !string.IsNullOrWhiteSpace(objProp.Value)) commandKey = "*";

var cmdFrameSegmentStr = $"{commandSeparator}{commandKey}{objProp.Value}";

cmdSegmentStringBuilder.Append(cmdFrameSegmentStr);
addedLines++;
}
else
{
if (!ignoreComments) commentString = objProp.Value;
}
else if (!ignoreComments) commentString = objProp.Value;
}

var res = !string.IsNullOrWhiteSpace(commentString) ? $"{cmdSegmentStringBuilder} ;{commentString}" : cmdSegmentStringBuilder.ToString();
return res.Trim();
return (!string.IsNullOrWhiteSpace(commentString) ? $"{cmdSegmentStringBuilder} ;{commentString}" : cmdSegmentStringBuilder.ToString()).Trim();
}
/// <summary>
/// ToJson
Expand All @@ -154,15 +138,15 @@ public static string GcodeToJson(this string raw)
const string objJsonStart = "{";
const string objJsonEnd = "}";

if (_rawFrame.IsGcodeComment() || _rawFrame.IsNullOrErrorFrame()) return $"{{Comment:{raw.Replace(";", null).Replace("\r", null).Trim()}}}";
if (_rawFrame.IsGcodeComment() || _rawFrame.IsNullOrErrorFrame()) return $"{{{Resources.CommentTag}:{raw.Replace(";", null).Replace("\r", null).Trim()}}}";

var commentString = string.Empty;

if (_rawFrame.ContainsGcodeComment())
{
var arr = _rawFrame.Split(";");
_rawFrame = arr[0].Trim();
commentString = $",\"Comment\":\"{arr[1].Trim().Replace("\r", null)}\"";
commentString = $",\"{Resources.CommentTag}\":\"{arr[1].Trim().Replace("\r", null)}\"";
}

var segments = _rawFrame.ToKeyValuePair();
Expand Down Expand Up @@ -254,7 +238,7 @@ public static string NormalizeGcodeRawFrame(this string raw)
public static bool ContainsGcodeComment(this string raw)
{
_rawFrame = raw.Trim();
return !_rawFrame.StartsWith(CommentChar) && _rawFrame.Contains(CommentChar);
return !_rawFrame.StartsWith(Resources.CommentChar) && _rawFrame.Contains(Resources.CommentChar);
}
/// <summary>
/// IsComment
Expand All @@ -264,7 +248,7 @@ public static bool ContainsGcodeComment(this string raw)
public static bool IsGcodeComment(this string raw)
{
_rawFrame = raw.Trim();
return !_rawFrame.IsNullOrErrorFrame() && _rawFrame.StartsWith(CommentChar);
return !_rawFrame.IsNullOrErrorFrame() && _rawFrame.StartsWith(Resources.CommentChar);
}
/// <summary>
/// Is Null Or Error Frame
Expand All @@ -284,7 +268,7 @@ public static bool IsNullOrErrorFrame(this string raw)
public static bool IsEmptyComment(this string raw)
{
_rawFrame = raw.Trim();
return _rawFrame.Length == 1 && _rawFrame == CommentChar;
return _rawFrame.Length == 1 && _rawFrame == Resources.CommentChar;
}

/// <summary>
Expand Down
99 changes: 99 additions & 0 deletions src/Gcode.Utils/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b2c2024

Please sign in to comment.