forked from CauldronDevelopmentLLC/CAMotics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineApp.cpp
More file actions
148 lines (112 loc) · 4.88 KB
/
CommandLineApp.cpp
File metadata and controls
148 lines (112 loc) · 4.88 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 "CommandLineApp.h"
#include <gcode/machine/MachineState.h>
#include <gcode/machine/MachineLinearizer.h>
#include <gcode/machine/MachineUnitAdapter.h>
#include <gcode/machine/GCodeMachine.h>
#include <gcode/machine/JSONMachineWriter.h>
#include <cbang/config/MinConstraint.h>
#include <cbang/os/SystemUtilities.h>
#include <cbang/boost/StartInclude.h>
#include <boost/version.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <cbang/boost/EndInclude.h>
namespace io = boost::iostreams;
#if BOOST_VERSION < 104400
#define BOOST_CLOSE_HANDLE true
#else
#define BOOST_CLOSE_HANDLE io::close_handle
#endif
using namespace CAMotics;
using namespace GCode;
using namespace cb;
using namespace std;
CommandLineApp::CommandLineApp(const string &name, hasFeature_t hasFeature) :
Application(name, hasFeature) {
cmdLine.addTarget("out", out, "Output filename or '-' to write "
"to the standard output stream");
cmdLine.addTarget("force", force, "Force overwriting output file", 'f');
cmdLine.add("metric", 0, this, &CommandLineApp::metricAction,
"Output in metric units.")->setType(Option::TYPE_BOOLEAN);
cmdLine.add("imperial", 0, this, &CommandLineApp::imperialAction,
"Output in imperial units.")->setType(Option::TYPE_BOOLEAN);
cmdLine.addTarget("units", outputUnits, "Set output units.");
cmdLine.addTarget("default-units", defaultUnits,
"Units assumed at the start.");
cmdLine.addTarget("max-arc-error", maxArcError,
"The maximum allowed error, in length units, when "
"estimating arcs with line segments. Default value is "
"in mm.");
cmdLine.addTarget("linearize", linearize,
"Convert all moves to straight line movements.");
cmdLine.addTarget("json-out", jsonOut, "Output in JSON format.");
cmdLine.addTarget("json-precision", jsonPrecision,
"JSON output numerical precision.");
cmdLine.addTarget("json-location", jsonLocation,
"Output source location information in JSON.");
Option &opt = *cmdLine.add("pipe", "Specify a output file descriptor, "
"overrides the 'out' option");
opt.setType(Option::TYPE_INTEGER);
opt.setConstraint(new MinConstraint<int>(0));
}
bool CommandLineApp::_hasFeature(int feature) {
switch (feature) {
case FEATURE_SIGNAL_HANDLER: return false;
default: return Application::_hasFeature(feature);
}
}
int CommandLineApp::init(int argc, char *argv[]) {
int ret = Application::init(argc, argv);
if (ret == -1) return ret;
if (cmdLine["--pipe"].hasValue()) {
#ifdef _WIN32
typedef void *handle_t;
#else
typedef int handle_t;
#endif
handle_t pipe = (handle_t)cmdLine["--pipe"].toInteger();
stream = new io::stream<io::file_descriptor>(pipe, BOOST_CLOSE_HANDLE);
} else if (out == "-") stream = SmartPointer<ostream>::Phony(&cout);
else {
if (SystemUtilities::exists(out) && !force)
THROW("File '" << out << "' already exists");
stream = SystemUtilities::oopen(out);
}
return ret;
}
void CommandLineApp::run() {
Application::run();
stream->flush();
}
void CommandLineApp::build(GCode::MachinePipeline &pipeline) {
pipeline.add(new MachineUnitAdapter(defaultUnits, outputUnits));
if (linearize) pipeline.add(new MachineLinearizer);
if (jsonOut) pipeline.add(new JSONMachineWriter
(*stream, outputUnits, jsonLocation, 0, false, 2,
jsonPrecision));
else pipeline.add(new GCodeMachine(stream, outputUnits));
pipeline.add(new MachineState);
pipeline.set("_max_arc_error", maxArcError, outputUnits);
}
int CommandLineApp::metricAction(Option &opt) {
outputUnits = opt.toBoolean() ? GCode::Units::METRIC : GCode::Units::IMPERIAL;
return 0;
}
int CommandLineApp::imperialAction(Option &opt) {
outputUnits = opt.toBoolean() ? GCode::Units::IMPERIAL : GCode::Units::METRIC;
return 0;
}