/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc.
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
namespace UVtools.Core.GCode
{
public class GCodeCommand
{
///
/// Gets or sets if this command is enabled
///
public bool Enabled { get; set; } = true;
///
/// Gets or sets the command name
///
public string Command { get; set; }
///
/// Gets or sets the arguments for this command
///
public string Arguments { get; set; }
///
/// Gets or sets the comment
///
public string Comment { get; set; }
public GCodeCommand() { }
public GCodeCommand(string command, string arguments = null, string comment = null, bool enabled = true)
{
Enabled = enabled;
Command = command;
Arguments = arguments;
Comment = comment;
}
public void Reset(string command, string arguments, string comment, bool enabled = true)
{
Enabled = enabled;
Command = command;
Arguments = arguments;
Comment = comment;
}
public void Reset(string command, string arguments)
{
Command = command;
Arguments = arguments;
}
public void Reset(string command)
{
Command = command;
}
public string ToString(bool showComment, bool showTailComma = true, string overrideComment = null)
{
var result = Command;
if (!string.IsNullOrWhiteSpace(Arguments))
result += $" {Arguments}";
if (result[0] == ';') return result;
var comment = string.IsNullOrWhiteSpace(overrideComment) ? Comment : overrideComment;
if (showComment && !string.IsNullOrWhiteSpace(comment))
{
result += $";{comment}";
}
else if (showTailComma)
{
result += ';';
}
return result;
}
public string ToString(bool showComment, bool showTailComma = true, params object[] args) =>
string.Format(ToString(showComment, showTailComma), args);
public string ToStringOverrideComment(string comment, params object[] args) => ToStringOverrideComment(true, true, comment, args);
public string ToStringOverrideComment(bool showComment, bool showTailComma, string comment, params object[] args) =>
string.Format(ToString(showComment, showTailComma, comment), args);
public string ToString(params object[] args) => ToString(true, true, args);
public string ToStringWithoutComments() => ToString(false, false);
public string ToStringWithoutComments(params object[] args) => ToString(false, false, args);
public override string ToString()
{
var result = Command;
if (!string.IsNullOrWhiteSpace(Arguments))
result += $" {Arguments}";
if (!string.IsNullOrWhiteSpace(Comment))
result += $";{Comment}";
return result;
}
protected bool Equals(GCodeCommand other)
{
return Command == other.Command;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((GCodeCommand) obj);
}
public override int GetHashCode()
{
return (Command != null ? Command.GetHashCode() : 0);
}
}
}