BlinkenArea - GitList
Repositories
Blog
Wiki
dxfngc
Code
Commits
Branches
Tags
Search
Tree:
e231ced
Branches
Tags
master
dxfngc
src
cmdparser.cpp
configuration of precision
Stefan Schuermans
commited
e231ced
at 2013-01-26 18:55:56
cmdparser.cpp
Blame
History
Raw
/* drawing (DXF) to G-code (NGC) converter * Copyright 2013 Stefan Schuermans <stefan@schuermans.info> * Copyleft: CC-BY-SA http://creativecommons.org/licenses/by-sa/3.0/ */ #include <fstream> #include <iostream> #include <math.h> #include <sstream> #include <string> #include "cmdparser.h" #include "drawing.h" #include "filename.h" #include "gcode.h" #include "settings.h" /** * @brief process cmd command * @param[in] strm stream to read command arguments from * @return if processing command was successful */ bool CmdParser::procCmd_cmd(std::istream &strm) { // skip whitespace and use rest of line as custom command strm >> std::ws; std::string cmd; getline(strm, cmd); // add custom command to G-code mGCode.appendCustom(cmd); return true; } /** * @brief process cut command * @param[in] strm stream to read command arguments from * @return if processing command was successful */ bool CmdParser::procCmd_cut(std::istream &strm) { // get arguments std::string layerName; strm >> layerName; if (strm.fail()) { std::cerr << "missing layer name" << std::endl; return false; } // get layer Drawing::Layers::const_iterator layer = mDrawing.mLayers.find(layerName); if (layer == mDrawing.mLayers.end()) { std::cerr << "layer \"" << layerName << "\" not found" << std::endl; return false; } // convert layer to G-code layer->second.toGCode(mSettings, mGCode); return true; } /** * @brief process read_dxf command * @param[in] strm stream to read command arguments from * @return if processing command was successful */ bool CmdParser::procCmd_read_dxf(std::istream &strm) { // get arguments std::string fileName; strm >> fileName; if (strm.fail()) { std::cerr << "missing DXF file name" << std::endl; return false; } // re-base DXF file name fileName = filename_rebase(fileName, mBaseDir); // read DXF file if (!mDrawing.loadDxf(fileName)) { std::cerr << "could not read DXF file \"" << fileName << "\"" << std::endl; return false; } return true; } /** * @brief process set_base_z command * @param[in] strm stream to read command arguments from * @return if processing command was successful */ bool CmdParser::procCmd_set_base_z(std::istream &strm) { // get arguments double z; strm >> z; if (strm.fail()) { std::cerr << "missing z coordinate" << std::endl; return false; } // update settings mSettings.base_z = z; return true; } /** * @brief process set_cut_z command * @param[in] strm stream to read command arguments from * @return if processing command was successful */ bool CmdParser::procCmd_set_cut_z(std::istream &strm) { // get arguments double z; strm >> z; if (strm.fail()) { std::cerr << "missing z coordinate" << std::endl; return false; } // update settings mSettings.cut_z = z; return true; } /** * @brief process set_cut_z_step command * @param[in] strm stream to read command arguments from * @return if processing command was successful */ bool CmdParser::procCmd_set_cut_z_step(std::istream &strm) { // get arguments and check them double z; strm >> z; if (strm.fail()) { std::cerr << "missing z value" << std::endl; return false; } if (z <= 0.0) { std::cerr << "invalid z cut step value (" << z << ")" << std::endl; return false; } // update settings mSettings.cut_z_step = z; return true; } /** * @brief process set_feed_drill command * @param[in] strm stream to read command arguments from * @return if processing command was successful */ bool CmdParser::procCmd_set_feed_drill(std::istream &strm) { // get arguments and check them double feed; strm >> feed; if (strm.fail()) { std::cerr << "missing feed rate" << std::endl; return false; } if (feed <= 0.0) { std::cerr << "invalid feed rate (" << feed << ")" << std::endl; return false; } // update settings mSettings.feed_drill = feed; return true; } /** * @brief process set_feed_mill command * @param[in] strm stream to read command arguments from * @return if processing command was successful */ bool CmdParser::procCmd_set_feed_mill(std::istream &strm) { // get arguments and check them double feed; strm >> feed; if (strm.fail()) { std::cerr << "missing feed rate" << std::endl; return false; } if (feed <= 0.0) { std::cerr << "invalid feed rate (" << feed << ")" << std::endl; return false; } // update settings mSettings.feed_mill = feed; return true; } /** * @brief process set_move_z command * @param[in] strm stream to read command arguments from * @return if processing command was successful */ bool CmdParser::procCmd_set_move_z(std::istream &strm) { // get arguments double z; strm >> z; if (strm.fail()) { std::cerr << "missing z coordinate" << std::endl; return false; } // update settings mSettings.move_z = z; return true; } /** * @brief process set_precision command * @param[in] strm stream to read command arguments from * @return if processing command was successful */ bool CmdParser::procCmd_set_precision(std::istream &strm) { // get arguments and check them double precision; strm >> precision; if (strm.fail()) { std::cerr << "missing precision" << std::endl; return false; } if (precision < 1.0e-8 || precision > 1.0) { std::cerr << "invalid precision (" << precision << ")" << std::endl; return false; } // update settings mSettings.precision = precision; return true; } /** * @brief process set_tool_diameter command * @param[in] strm stream to read command arguments from * @return if processing command was successful */ bool CmdParser::procCmd_set_tool_diameter(std::istream &strm) { // get arguments and check them double diameter; strm >> diameter; if (strm.fail()) { std::cerr << "missing tool diameter" << std::endl; return false; } if (diameter < 0.0) { std::cerr << "invalid tool diameter (" << diameter << ")" << std::endl; return false; } // update settings mSettings.tool_diameter = diameter; return true; } /** * @brief process write_ngc command * @param[in] strm stream to read command arguments from * @return if processing command was successful */ bool CmdParser::procCmd_write_ngc(std::istream &strm) { // get arguments std::string fileName; strm >> fileName; if (strm.fail()) { std::cerr << "missing NGC file name" << std::endl; return false; } // re-base NGC file name fileName = filename_rebase(fileName, mBaseDir); // calculate number of digits after decimal point unsigned int digits = (unsigned int)ceil(-log10(mSettings.precision)); // write NGC file if (!mGCode.toFile(fileName, digits)) { std::cerr << "could not write NGC file \"" << fileName << "\"" << std::endl; return false; } return true; } /** * @brief process command from line of text * @param[in] strLine line containing command to process * @return if processing command was successful */ bool CmdParser::procLine(const std::string &strLine) { std::stringstream strm(strLine); // read command std::string cmd; strm >> cmd; if (strm.fail()) return true; // ignore empty lines // commands if (cmd == "cmd") return procCmd_cmd(strm); else if (cmd == "cut") return procCmd_cut(strm); else if (cmd == "read_dxf") return procCmd_read_dxf(strm); else if (cmd == "set_base_z") return procCmd_set_base_z(strm); else if (cmd == "set_cut_z") return procCmd_set_cut_z(strm); else if (cmd == "set_cut_z_step") return procCmd_set_cut_z_step(strm); else if (cmd == "set_feed_drill") return procCmd_set_feed_drill(strm); else if (cmd == "set_feed_mill") return procCmd_set_feed_mill(strm); else if (cmd == "set_move_z") return procCmd_set_move_z(strm); else if (cmd == "set_precision") return procCmd_set_precision(strm); else if (cmd == "set_tool_diameter") return procCmd_set_tool_diameter(strm); else if (cmd == "write_ngc") return procCmd_write_ngc(strm); else { std::cerr << "unknown command \"" << cmd << "\"" << std::endl; return false; } } /** * @brief process commands from a stream * @param[in] strm stream to process * @param[in] if processing stream was successful * @return if processing commands was successful */ bool CmdParser::procStream(std::istream &strm) { unsigned int lineNo = 0; while (strm.good()) { // read line, remove comment std::string line; std::getline(strm, line); std::string::size_type pos = line.find_first_of("#;"); if (pos != std::string::npos) line.erase(pos); lineNo++; // process line if (!procLine(line)) { std::cerr << "error processing command in line " << lineNo << std::endl; return false; } } return true; } /** * @brief process commands from a file * @param[in] strFileName name of file to process * @param[in] if processing the file was successful * @return if processing commands was successful */ bool CmdParser::procFile(const std::string &strFileName) { std::ifstream ifstrm(strFileName.c_str(), std::ios::in); if (!ifstrm.is_open()) { std::cerr << "could not open \"" << strFileName << "\" for reading" << std::endl; return false; } mBaseDir = filename_get_dir(strFileName); bool ret = procStream(ifstrm); mBaseDir.clear(); return ret; }