diff --git a/src/Mod/Path/App/Command.cpp b/src/Mod/Path/App/Command.cpp index 95c9fe0071..a86f7b1c5a 100644 --- a/src/Mod/Path/App/Command.cpp +++ b/src/Mod/Path/App/Command.cpp @@ -281,6 +281,24 @@ Command Command::transform(const Base::Placement other) return c; } +void Command::scaleBy(double factor) +{ + for(std::map::const_iterator i = Parameters.begin(); i != Parameters.end(); ++i) { + switch (i->first[0]) { + case 'X': + case 'Y': + case 'Z': + case 'I': + case 'J': + case 'R': + case 'Q': + case 'F': + Parameters[i->first] = i->second * factor; + break; + } + } +} + // Reimplemented from base class unsigned int Command::getMemSize (void) const diff --git a/src/Mod/Path/App/Command.h b/src/Mod/Path/App/Command.h index 199d172a89..8b4f316ae9 100644 --- a/src/Mod/Path/App/Command.h +++ b/src/Mod/Path/App/Command.h @@ -58,6 +58,7 @@ namespace Path bool has(const std::string&) const; // returns true if the given string exists in the parameters Command transform(const Base::Placement); // returns a transformed copy of this command double getValue(const std::string &name) const; // returns the value of a given parameter + void scaleBy(double factor); // scales the receiver - use for imperial/metric conversions // this assumes the name is upper case inline double getParam(const std::string &name) const { diff --git a/src/Mod/Path/App/Path.cpp b/src/Mod/Path/App/Path.cpp index 179d69d83e..f9d9ba2c25 100644 --- a/src/Mod/Path/App/Path.cpp +++ b/src/Mod/Path/App/Path.cpp @@ -140,6 +140,24 @@ double Toolpath::getLength() return l; } +static void bulkAddCommand(const std::string &gcodestr, std::vector &commands, bool &inches) +{ + Command *cmd = new Command(); + cmd->setFromGCode(gcodestr); + if ("G20" == cmd->Name) { + inches = true; + delete cmd; + } else if ("G21" == cmd->Name) { + inches = false; + delete cmd; + } else { + if (inches) { + cmd->scaleBy(254); + } + commands.push_back(cmd); + } +} + void Toolpath::setFromGCode(const std::string instr) { clear(); @@ -153,48 +171,41 @@ void Toolpath::setFromGCode(const std::string instr) std::string mode = "command"; std::size_t found = str.find_first_of("(gGmM"); int last = -1; - while (found!=std::string::npos) + bool inches = false; + while (found != std::string::npos) { if (str[found] == '(') { // start of comment if ( (last > -1) && (mode == "command") ) { // before opening a comment, add the last found command - std::string gcodestr = str.substr(last,found-last); - Command *tmp = new Command(); - tmp->setFromGCode(gcodestr); - vpcCommands.push_back(tmp); + std::string gcodestr = str.substr(last, found-last); + bulkAddCommand(gcodestr, vpcCommands, inches); } mode = "comment"; last = found; - found=str.find_first_of(")",found+1); + found = str.find_first_of(")", found+1); } else if (str[found] == ')') { // end of comment - std::string gcodestr = str.substr(last,found-last+1); - Command *tmp = new Command(); - tmp->setFromGCode(gcodestr); - vpcCommands.push_back(tmp); + std::string gcodestr = str.substr(last, found-last+1); + bulkAddCommand(gcodestr, vpcCommands, inches); last = -1; - found=str.find_first_of("(gGmM",found+1); + found = str.find_first_of("(gGmM", found+1); mode = "command"; } else if (mode == "command") { // command if (last > -1) { - std::string gcodestr = str.substr(last,found-last); - Command *tmp = new Command(); - tmp->setFromGCode(gcodestr); - vpcCommands.push_back(tmp); + std::string gcodestr = str.substr(last, found-last); + bulkAddCommand(gcodestr, vpcCommands, inches); } last = found; - found=str.find_first_of("(gGmM",found+1); + found = str.find_first_of("(gGmM", found+1); } } // add the last command found, if any if (last > -1) { if (mode == "command") { std::string gcodestr = str.substr(last,std::string::npos); - Command *tmp = new Command(); - tmp->setFromGCode(gcodestr); - vpcCommands.push_back(tmp); + bulkAddCommand(gcodestr, vpcCommands, inches); } } recalculate();