Skip to content

Commit

Permalink
Feature/zbottom warn if negative z on z bottom (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
k3ldar committed Dec 25, 2023
1 parent 9b3ae39 commit 3c891b9
Show file tree
Hide file tree
Showing 55 changed files with 353 additions and 186 deletions.
20 changes: 18 additions & 2 deletions src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ dotnet_diagnostic.S3267.severity = none
# IDE1006: Naming Styles
dotnet_diagnostic.IDE1006.severity = none
csharp_indent_labels = one_less_than_current
csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_using_directive_placement = outside_namespace:error
csharp_prefer_simple_using_statement = true:warning
csharp_prefer_braces = true:silent
csharp_style_namespace_declarations = block_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
Expand All @@ -25,6 +25,11 @@ dotnet_diagnostic.IDE0066.severity = none
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_expression_bodied_local_functions = false:silent
csharp_style_prefer_switch_expression = false:silent
csharp_space_around_binary_operators = before_and_after
dotnet_diagnostic.IDE0301.severity = none
dotnet_diagnostic.IDE0305.severity = none
dotnet_diagnostic.IDE0300.severity = none
dotnet_diagnostic.IDE0290.severity = none

[*.{cs,vb}]
#### Naming styles ####
Expand Down Expand Up @@ -81,3 +86,14 @@ dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_auto_properties = true:silent
dotnet_style_object_initializer = true:suggestion
dotnet_style_prefer_collection_expression = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_compound_assignment = true:suggestion
dotnet_style_prefer_simplified_interpolation = true:suggestion
48 changes: 48 additions & 0 deletions src/GCAAnalyser/Analyzers/AnalyzeZ.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using GSendShared;
using GSendShared.Abstractions;

namespace GSendAnalyzer.Analyzers
{
internal class AnalyzeZ : IGCodeAnalyzer
{
private const int MinimumLayerCount = 2;

public int Order => Int32.MinValue;

public void Analyze(string fileName, IGCodeAnalyses gCodeAnalyses)
{
ArgumentNullException.ThrowIfNull(gCodeAnalyses);


decimal homeZ = gCodeAnalyses.AllCommands.Count > 0 ? gCodeAnalyses.AllCommands.Max(c => c.CurrentZ) : 0;

List<IGCodeCommand> homeZCommands = gCodeAnalyses.AllCommands.Where(c => c.CurrentZ < homeZ).ToList();
decimal safeZ = homeZCommands.Count > 0 ? homeZCommands.Max(c => c.CurrentZ) : 0;

List<IGCodeCommand> layerCommands = gCodeAnalyses.AllSpecificCommands(Constants.CharZ).Where(c =>
c.CommandValue < safeZ &&
(
!c.Attributes.HasFlag(CommandAttributes.SafeZ) &&
!c.Attributes.HasFlag(CommandAttributes.HomeZ) &&
!c.Attributes.HasFlag(CommandAttributes.StartProgram)
)
)
.DistinctBy(c => c.CurrentZ)
.OrderByDescending(c => c.CurrentZ)
.ToList();

if (layerCommands.Count > MinimumLayerCount)
{
if (layerCommands[0].CommandValue > 0)
{
gCodeAnalyses.ZBottom = layerCommands[0].CommandValue > layerCommands[^1].CommandValue;

if (gCodeAnalyses.ZBottom.HasValue && gCodeAnalyses.ZBottom.Value && layerCommands.Exists(c => c.CommandValue < 0) && gCodeAnalyses is GCodeAnalyses analysis)
analysis.AddWarning(GSend.Language.Resources.WarnBitBelowSpoilboard);
}
else
gCodeAnalyses.ZBottom = layerCommands[0].CommandValue < layerCommands[^1].CommandValue;
}
}
}
}
2 changes: 2 additions & 0 deletions src/GCAAnalyser/GCodeAnalyses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ private void RecursivelyRetrieveAllCommands(List<IGCodeCommand> Result, IReadOnl

public decimal SafeZ { get; set; }

public bool? ZBottom { get; set; } = null;

public UnitOfMeasurement UnitOfMeasurement { get; set; }

public decimal TotalDistance { get; set; }
Expand Down
81 changes: 81 additions & 0 deletions src/GCATests/GSendAnalyzerTests/AnalyzeZTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Diagnostics.CodeAnalysis;

using GSendAnalyzer.Analyzers;
using GSendAnalyzer.Internal;

using GSendShared;

using GSendTests.Mocks;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GSendTests.GSendAnalyzerTests
{
[ExcludeFromCodeCoverage]
[TestClass]
public sealed class AnalyzeZTests
{
private const string GCodeZTop = "G0Z30.000\r\nG1Z-2.000F250.0\r\nG1Z-4.000F250.0\r\nG1Z-6.000F250.0\r\nG1Z-8.000F250.0\r\nG1Z-10.000F250.0\r\nG1Z-12.000F250.0\r\nG1Z-14.000F250.0\r\nG1Z-16.000F250.0\r\nG1Z-18.000F250.0\r\nG0Z22.000";
private const string GCodeZBottom = "G0Z30.000\r\nG1Z14.300F250.0\r\nG1Z12.300F250.0\r\nG1Z10.300F250.0\r\nG1Z8.300F250.0\r\nG1Z6.300F250.0\r\nG1Z4.300F250.0\r\nG1Z2.300F250.0\r\nG1Z0.300F250.0\r\nG1Z-1.700F250.0";
private const string GCodeZUndetermined = "G0Z30.000\r\nG1Z-2.000F250.0\r\nG1Z-4.000F250.0";

[TestMethod]
public void Construct_ValidInstance_Success()
{
AnalyzeZ sut = new();
Assert.IsNotNull(sut);
Assert.AreEqual(Int32.MinValue, sut.Order);
}

[TestMethod]
public void Validate_ZTop_Success()
{
AnalyzeZ sut = new();
GCodeParser gCodeParser = new(new MockPluginClassesService(), new MockSubprograms());
IGCodeAnalyses analyses = gCodeParser.Parse(GCodeZTop);

sut.Analyze(String.Empty, analyses);

Assert.IsFalse(analyses.ZBottom);
}

[TestMethod]
public void Validate_ZBottom_Success()
{
AnalyzeZ sut = new();
GCodeParser gCodeParser = new(new MockPluginClassesService(), new MockSubprograms());
IGCodeAnalyses analyses = gCodeParser.Parse(GCodeZBottom);

sut.Analyze(String.Empty, analyses);

Assert.IsTrue(analyses.ZBottom);
}

[TestMethod]
public void Validate_ZUndetermined_Success()
{
AnalyzeZ sut = new();
GCodeParser gCodeParser = new(new MockPluginClassesService(), new MockSubprograms());
IGCodeAnalyses analyses = gCodeParser.Parse(GCodeZUndetermined);

sut.Analyze(String.Empty, analyses);

Assert.IsNull(analyses.ZBottom);
}

[TestMethod]
public void Validate_ZBottom_GoesBelowBed_CreatesWarning_Success()
{
AnalyzeZ sut = new();
GCodeParser gCodeParser = new(new MockPluginClassesService(), new MockSubprograms());
IGCodeAnalyses analyses = gCodeParser.Parse(GCodeZBottom);

sut.Analyze(String.Empty, analyses);

Assert.IsTrue(analyses.ZBottom);
Assert.AreEqual(1, analyses.Warnings.Count);
Assert.AreEqual("Validate the bit does not go below the spoilboard when using Z Bottom and having a negative Z value", analyses.Warnings[0]);
}
}
}
2 changes: 1 addition & 1 deletion src/GCATests/Mocks/MockGCodeAnalyses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace GSendTests.Mocks
internal class MockGCodeAnalyses : IGCodeAnalyses
{
public IReadOnlyList<IGCodeCommand> Commands => new List<IGCodeCommand>();

public decimal SafeZ { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public decimal HomeZ { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public bool? ZBottom { get; set; } = null;
public decimal TotalDistance { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public TimeSpan TotalTime { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public UnitOfMeasurement UnitOfMeasurement { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
Expand Down
16 changes: 8 additions & 8 deletions src/GCSDB/Providers/GSendDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public IReadOnlyList<ISpindleTime> SpindleTimeGet(long machineId)
{
List<MachineSpindleTimeDataRow> spindleTime = _spindleTimeTable.Select(m => m.MachineId == machineId).ToList();

List<ISpindleTime> Result = new();
List<ISpindleTime> Result = [];

foreach (MachineSpindleTimeDataRow row in spindleTime)
{
Expand All @@ -270,7 +270,7 @@ public IJobProfile JobProfileGet(long jobId)

public IReadOnlyList<IJobProfile> JobProfilesGet()
{
List<IJobProfile> Result = new();
List<IJobProfile> Result = [];

foreach (JobProfileDataRow profile in _jobProfileTable.Select())
{
Expand Down Expand Up @@ -336,7 +336,7 @@ public ulong JobProfileGetNextSerialNumber(IJobProfile jobProfile)

public IReadOnlyList<IToolProfile> ToolsGet()
{
List<IToolProfile> Result = new();
List<IToolProfile> Result = [];

foreach (ToolDatabaseDataRow toolProfile in _toolDatabaseTable.Select())
{
Expand Down Expand Up @@ -410,11 +410,11 @@ public void ServiceAdd(MachineServiceModel service)

public IReadOnlyList<MachineServiceModel> ServicesGet(long machineId)
{
List<MachineServiceModel> Result = new();
List<MachineServiceModel> Result = [];

foreach (var service in _machineServiceTable.Select(m => m.MachineId == machineId).OrderByDescending(m => m.ServiceDate))
{
Dictionary<long, string> serviceItems = new();
Dictionary<long, string> serviceItems = [];

foreach (long serviceItem in service.Items)
serviceItems.Add(serviceItem, _serviceItemsTable.Select(serviceItem).Name);
Expand All @@ -428,7 +428,7 @@ public IReadOnlyList<MachineServiceModel> ServicesGet(long machineId)

public List<ServiceItemModel> ServiceItemsGet(MachineType machineType)
{
List<ServiceItemModel> serviceItems = new();
List<ServiceItemModel> serviceItems = [];

foreach (var serviceItem in _serviceItemsTable.Select().Where(si => !si.IsDeleted))
{
Expand Down Expand Up @@ -456,7 +456,7 @@ private static IToolProfile CreateToolDatabaseModelFromToolDatabaseDataRow(ToolD
if (toolDatabaseDataRow == null)
return null;

List<ToolUsageHistoryModel> history = new();
List<ToolUsageHistoryModel> history = [];

toolDatabaseDataRow.ToolHistory.ForEach(th => history.Add(new ToolUsageHistoryModel(th.LastChanged, th.UsageMinutes)));

Expand Down Expand Up @@ -508,7 +508,7 @@ private MachineDataRow ConvertFromIMachineToMachineDataRow(IMachine machine)

private List<IMachine> InternalGetMachines()
{
List<IMachine> Result = new();
List<IMachine> Result = [];

foreach (MachineDataRow machineDataRow in _machineDataRow.Select())
{
Expand Down
6 changes: 3 additions & 3 deletions src/GCSDB/Tables/JobProfileDataRowDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public List<JobProfileDataRow> InitialData(ushort version)
if (version > 1)
return null;

return new List<JobProfileDataRow>()
{
return
[
new() { JobName = "(Default)", JobDescription = "Default job" },
};
];
}
}
}
2 changes: 1 addition & 1 deletion src/GCSDB/Tables/MachineDataRowDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal sealed class MachineDataRowDefaults : ITableDefaults<MachineDataRow>

public List<MachineDataRow> InitialData(ushort version)
{
return new List<MachineDataRow>();
return [];
}
}
}
2 changes: 1 addition & 1 deletion src/GCSDB/Tables/MachineServiceDataRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal sealed class MachineServiceDataRow : TableRowDefinition

public MachineServiceDataRow()
{
_items = new();
_items = [];
_items.Changed += ObservableDataChanged;
}

Expand Down
6 changes: 3 additions & 3 deletions src/GCSDB/Tables/ServiceItemsDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public List<ServiceItemsDataRow> InitialData(ushort version)
{
if (version == 1)
{
return new List<ServiceItemsDataRow>()
{
return
[
new() { Name = "Clean Machine/Enclosure", IsDaily = true, IsMinor = true, IsMajor = true },
new() { Name = "Inspect Bits for Damage", IsDaily = true, IsMinor = true, IsMajor = true },
new() { Name = "Inspect Wires and Connections", IsDaily = true, IsMinor = true, IsMajor = true },
Expand All @@ -36,7 +36,7 @@ public List<ServiceItemsDataRow> InitialData(ushort version)
new() { Name = "Test individual Limit Switches", IsDaily = false, IsMinor = false, IsMajor = true },
new() { Name = "Validate Gantry is Square", IsDaily = false, IsMinor = false, IsMajor = true },
new() { Name = "Verify screws/bolts correctly tightened", IsDaily = false, IsMinor = false, IsMajor = true },
};
];
}

return [];
Expand Down
2 changes: 1 addition & 1 deletion src/GCSDB/Tables/ToolDatabaseDataRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal sealed class ToolDatabaseDataRow : TableRowDefinition

public ToolDatabaseDataRow()
{
_toolHistory = new ObservableList<ToolUsageHistory>();
_toolHistory = [];
_toolHistory.Changed += ObservableDataChanged;
}

Expand Down
8 changes: 4 additions & 4 deletions src/GCSDB/Tables/ToolDatabaseDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public List<ToolDatabaseDataRow> InitialData(ushort version)
{
if (version == 1)
{
return new List<ToolDatabaseDataRow>()
{
return
[
new () { ToolName = "(Default)", Description = "Default profile for no tool selected"}
};
];
}

return new List<ToolDatabaseDataRow>();
return [];
}
}
}
2 changes: 1 addition & 1 deletion src/GCSDesktop/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public partial class FormMain : BaseForm, IPluginHost, IOnlineStatusUpdate
private readonly GSendWebSocket _clientWebSocket;
private readonly object _lockObject = new();
private readonly CancellationTokenRegistration _cancellationTokenRegistration;
private readonly Dictionary<long, ListViewItem> _machines = new();
private readonly Dictionary<long, ListViewItem> _machines = [];
private IMachine _selectedMachine = null;
private readonly IPluginHelper _pluginHelper;
private long _machineHashCombined = 0;
Expand Down
Loading

0 comments on commit 3c891b9

Please sign in to comment.