-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathTool.cpp
More file actions
148 lines (100 loc) · 3.87 KB
/
Tool.cpp
File metadata and controls
148 lines (100 loc) · 3.87 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/******************************************************************************\
CAMotics is an Open-Source simulation and CAM software.
Copyright (C) 2011-2019 Joseph Coffland <joseph@cauldrondevelopment.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
\******************************************************************************/
#include "Tool.h"
#include <cbang/String.h>
#include <cbang/Exception.h>
#include <cbang/xml/Writer.h>
#include <cbang/Catch.h>
#include <cstring>
using namespace std;
using namespace cb;
using namespace GCode;
const char *Tool::VARS = "XYZABCUVWRIJQ";
Tool Tool::null;
Tool::Tool(unsigned number, unsigned pocket, Units units) :
number(number), pocket(pocket), units(units),
shape(ToolShape::TS_CYLINDRICAL), snubDiameter(0) {
Axes::clear();
memset(vars, 0, sizeof(vars));
if (units == Units::METRIC) {
setLength(10);
setRadius(1);
} else {
setLength(25.4);
setRadius(25.4 / 16);
}
}
string Tool::getSizeText() const {
double diameter = getDiameter();
double length = getLength();
if (getUnits() == Units::IMPERIAL) {
diameter /= 25.4;
length /= 25.4;
}
string s;
if (getShape() == ToolShape::TS_CONICAL)
s = String::printf("%gdeg %g", getAngle(), diameter);
else s = String::printf("%gx%g", diameter, length);
if (getUnits() == Units::METRIC) s += "mm";
else s += "in";
return s;
}
string Tool::getText() const {
if (!description.empty()) return description;
return getSizeText() + " " +
String::capitalize(String::toLower(getShape().toString()));
}
double Tool::getAngle() const {
double angle = 180.0 - 360.0 * atan(getLength() / getRadius()) / Math::PI;
return round(angle * 100) / 100;
}
void Tool::setRadiusFromAngle(double angle) {
setRadius(getLength() / tan((1 - angle / 180.0) * Math::PI / 2.0));
}
void Tool::setLengthFromAngle(double angle) {
setLength(getRadius() * tan((1 - angle / 180.0) * Math::PI / 2.0));
}
ostream &Tool::print(ostream &stream) const {
stream << "T" << number << " R" << getRadius() << " L" << getLength();
return Axes::print(stream);
}
void Tool::write(JSON::Sink &sink, bool withNumber) const {
sink.beginDict();
double scale = units == Units::IMPERIAL ? 1.0 / 25.4 : 1;
if (withNumber) sink.insert("number", number);
sink.insert("units", String::toLower(getUnits().toString()));
sink.insert("shape", String::toLower(getShape().toString()));
sink.insert("length", getLength() * scale);
sink.insert("diameter", getDiameter() * scale);
if (getShape() == ToolShape::TS_SNUBNOSE)
sink.insert("snub_diameter", getSnubDiameter() * scale);
sink.insert("description", getDescription());
sink.endDict();
}
void Tool::read(const JSON::Value &value) {
setNumber(value.getU32("number", number));
if (value.hasString("units"))
units = Units::parse(value.getString("units"));
double scale = units == Units::IMPERIAL ? 25.4 : 1;
if (value.hasString("shape"))
shape = ToolShape::parse(value.getString("shape"));
if (value.hasNumber("length"))
setLength(value.getNumber("length") * scale);
if (value.hasNumber("diameter"))
setDiameter(value.getNumber("diameter") * scale);
if (value.hasNumber("snub_diameter"))
setSnubDiameter(value.getNumber("snub_diameter") * scale);
setDescription(value.getString("description", ""));
}