Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GCodeCommand.cs « GCode « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e74fb4d90bef0a8a54871e03105ed4e67f40da78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 *                     GNU AFFERO GENERAL PUBLIC LICENSE
 *                       Version 3, 19 November 2007
 *  Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 *  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
    {
        /// <summary>
        /// Gets or sets if this command is enabled
        /// </summary>
        public bool Enabled { get; set; } = true;

        /// <summary>
        /// Gets or sets the command name
        /// </summary>
        public string Command { get; set; }

        /// <summary>
        /// Gets or sets the arguments for this command
        /// </summary>
        public string Arguments { get; set; }

        /// <summary>
        /// Gets or sets the comment
        /// </summary>
        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 Set(string command, string arguments, string comment, bool enabled = true)
        {
            Enabled = enabled;
            Command = command;
            Arguments = arguments;
            Comment = comment;
        }

        public void Set(string command, string arguments)
        {
            Command = command;
            Arguments = arguments;
        }

        public void Set(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);
        }
    }
}