File size: 16,545 Bytes
985c397 | 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2014 Yorik van Havre <yorik@uncreated.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include <App/Application.h>
#include <Base/Console.h>
#include <Base/Reader.h>
#include <Base/Stream.h>
#include <Base/Writer.h>
#include <Mod/CAM/App/PathSegmentWalker.h>
#include "Path.h"
using namespace Path;
using namespace Base;
TYPESYSTEM_SOURCE(Path::Toolpath, Base::Persistence)
Toolpath::Toolpath()
{}
Toolpath::Toolpath(const Toolpath& otherPath)
: vpcCommands(otherPath.vpcCommands.size())
, center(otherPath.center)
{
*this = otherPath;
recalculate();
}
Toolpath::~Toolpath()
{
clear();
}
Toolpath& Toolpath::operator=(const Toolpath& otherPath)
{
if (this == &otherPath) {
return *this;
}
clear();
vpcCommands.resize(otherPath.vpcCommands.size());
int i = 0;
for (std::vector<Command*>::const_iterator it = otherPath.vpcCommands.begin();
it != otherPath.vpcCommands.end();
++it, i++) {
vpcCommands[i] = new Command(**it);
}
center = otherPath.center;
recalculate();
return *this;
}
void Toolpath::clear()
{
for (std::vector<Command*>::iterator it = vpcCommands.begin(); it != vpcCommands.end(); ++it) {
delete (*it);
}
vpcCommands.clear();
recalculate();
}
void Toolpath::addCommand(const Command& Cmd)
{
Command* tmp = new Command(Cmd);
vpcCommands.push_back(tmp);
recalculate();
}
void Toolpath::insertCommand(const Command& Cmd, int pos)
{
if (pos == -1) {
addCommand(Cmd);
}
else if (pos <= static_cast<int>(vpcCommands.size())) {
Command* tmp = new Command(Cmd);
vpcCommands.insert(vpcCommands.begin() + pos, tmp);
}
else {
throw Base::IndexError("Index not in range");
}
recalculate();
}
void Toolpath::deleteCommand(int pos)
{
if (pos == -1) {
// delete(*vpcCommands.rbegin()); // causes crash
vpcCommands.pop_back();
}
else if (pos <= static_cast<int>(vpcCommands.size())) {
vpcCommands.erase(vpcCommands.begin() + pos);
}
else {
throw Base::IndexError("Index not in range");
}
recalculate();
}
double Toolpath::getLength()
{
if (vpcCommands.empty()) {
return 0;
}
double l = 0;
Vector3d last(0, 0, 0);
Vector3d next;
for (std::vector<Command*>::const_iterator it = vpcCommands.begin(); it != vpcCommands.end();
++it) {
std::string name = (*it)->Name;
next = (*it)->getPlacement(last).getPosition();
if ((name == "G0") || (name == "G00") || (name == "G1") || (name == "G01")) {
// straight line
l += (next - last).Length();
last = next;
}
else if ((name == "G2") || (name == "G02") || (name == "G3") || (name == "G03")) {
// arc
Vector3d center = (*it)->getCenter();
double radius = (last - center).Length();
double angle = (next - center).GetAngle(last - center);
l += angle * radius;
last = next;
}
}
return l;
}
double Toolpath::getCycleTime(double hFeed, double vFeed, double hRapid, double vRapid)
{
// check the feedrates are set
if ((hFeed == 0) || (vFeed == 0)) {
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
"User parameter:BaseApp/Preferences/Mod/CAM"
);
if (!hGrp->GetBool("WarningsSuppressAllSpeeds", true)) {
Base::Console().warning("Feed Rate Error: Check Tool Controllers have Feed Rates");
}
return 0;
}
if (hRapid == 0) {
hRapid = hFeed;
}
if (vRapid == 0) {
vRapid = vFeed;
}
if (vpcCommands.empty()) {
return 0;
}
double l = 0;
double time = 0;
bool verticalMove = false;
Vector3d last(0, 0, 0);
Vector3d next;
for (std::vector<Command*>::const_iterator it = vpcCommands.begin(); it != vpcCommands.end();
++it) {
std::string name = (*it)->Name;
float feedrate = (*it)->getParam("F");
l = 0;
verticalMove = false;
feedrate = hFeed;
next = (*it)->getPlacement(last).getPosition();
if (last.z != next.z) {
verticalMove = true;
feedrate = vFeed;
}
if ((name == "G0") || (name == "G00")) {
// Rapid Move
l += (next - last).Length();
feedrate = hRapid;
if (verticalMove) {
feedrate = vRapid;
}
}
else if ((name == "G1") || (name == "G01")) {
// Feed Move
l += (next - last).Length();
}
else if ((name == "G2") || (name == "G02") || (name == "G3") || (name == "G03")) {
// Arc Move
Vector3d center = (*it)->getCenter();
double radius = (last - center).Length();
double angle = (next - center).GetAngle(last - center);
l += angle * radius;
}
time += l / feedrate;
last = next;
}
return time;
}
class BoundBoxSegmentVisitor: public PathSegmentVisitor
{
public:
BoundBoxSegmentVisitor()
{}
void g0(
int id,
const Base::Vector3d& last,
const Base::Vector3d& next,
const std::deque<Base::Vector3d>& pts
) override
{
(void)id;
processPt(last);
processPts(pts);
processPt(next);
}
void g1(
int id,
const Base::Vector3d& last,
const Base::Vector3d& next,
const std::deque<Base::Vector3d>& pts
) override
{
(void)id;
processPt(last);
processPts(pts);
processPt(next);
}
void g23(
int id,
const Base::Vector3d& last,
const Base::Vector3d& next,
const std::deque<Base::Vector3d>& pts,
const Base::Vector3d& center
) override
{
(void)id;
(void)center;
processPt(last);
processPts(pts);
processPt(next);
}
void g8x(
int id,
const Base::Vector3d& last,
const Base::Vector3d& next,
const std::deque<Base::Vector3d>& pts,
const std::deque<Base::Vector3d>& p,
const std::deque<Base::Vector3d>& q
) override
{
(void)id;
(void)q; // always within the bounds of p
processPt(last);
processPts(pts);
processPts(p);
processPt(next);
}
void g38(int id, const Base::Vector3d& last, const Base::Vector3d& next) override
{
(void)id;
processPt(last);
processPt(next);
}
Base::BoundBox3d bb;
private:
void processPts(const std::deque<Base::Vector3d>& pts)
{
for (std::deque<Base::Vector3d>::const_iterator it = pts.begin(); pts.end() != it; ++it) {
processPt(*it);
}
}
void processPt(const Base::Vector3d& pt)
{
bb.MaxX = std::max(bb.MaxX, pt.x);
bb.MinX = std::min(bb.MinX, pt.x);
bb.MaxY = std::max(bb.MaxY, pt.y);
bb.MinY = std::min(bb.MinY, pt.y);
bb.MaxZ = std::max(bb.MaxZ, pt.z);
bb.MinZ = std::min(bb.MinZ, pt.z);
}
};
Base::BoundBox3d Toolpath::getBoundBox() const
{
BoundBoxSegmentVisitor visitor;
PathSegmentWalker walker(*this);
walker.walk(visitor, Vector3d(0, 0, 0));
return visitor.bb;
}
static void bulkAddCommand(const std::string& gcodestr, std::vector<Command*>& 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(25.4);
}
commands.push_back(cmd);
}
}
void Toolpath::setFromGCode(const std::string instr)
{
clear();
// remove comments
// boost::regex e("\\(.*?\\)");
// std::string str = boost::regex_replace(instr, e, "");
std::string str(instr);
// split input string by () or G or M commands
std::string mode = "command";
std::size_t found = str.find_first_of("(gGmM");
int last = -1;
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);
bulkAddCommand(gcodestr, vpcCommands, inches);
}
mode = "comment";
last = found;
found = str.find_first_of(')', found + 1);
}
else if (str[found] == ')') {
// end of comment
std::string gcodestr = str.substr(last, found - last + 1);
bulkAddCommand(gcodestr, vpcCommands, inches);
last = -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);
bulkAddCommand(gcodestr, vpcCommands, inches);
}
last = found;
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);
bulkAddCommand(gcodestr, vpcCommands, inches);
}
}
recalculate();
}
std::string Toolpath::toGCode() const
{
std::string result;
for (std::vector<Command*>::const_iterator it = vpcCommands.begin(); it != vpcCommands.end();
++it) {
result += (*it)->toGCode();
result += "\n";
}
return result;
}
void Toolpath::recalculate() // recalculates the path cache
{
if (vpcCommands.empty()) {
return;
}
// TODO recalculate the KDL stuff. At the moment, this is unused.
#if 0
// delete the old and create a new one
if(pcPath)
delete (pcPath);
pcPath = new KDL::Path_Composite();
KDL::Path *tempPath;
KDL::Frame Last;
try {
// handle the first waypoint differently
bool first=true;
for(std::vector<Command*>::const_iterator it = vpcCommands.begin();it!=vpcCommands.end();++it) {
if(first){
Last = toFrame((*it)->getPlacement());
first = false;
}else{
Base::Placement p = (*it)->getPlacement();
KDL::Frame Next = toFrame(p);
std::string name = (*it)->Name;
Vector3d zaxis(0,0,1);
if ( (name == "G0") || (name == "G1") || (name == "G01") ) {
// line segment
tempPath = new KDL::Path_Line(Last, Next, new KDL::RotationalInterpolation_SingleAxis(), 1.0, true);
pcPath->Add(tempPath);
Last = Next;
} else if ( (name == "G2") || (name == "G02") ) {
// clockwise arc
Vector3d fcenter = (*it)->getCenter();
KDL::Vector center(fcenter.x,fcenter.y,fcenter.z);
Vector3d fnorm;
p.getRotation().multVec(zaxis,fnorm);
KDL::Vector norm(fnorm.x,fnorm.y,fnorm.z);
Vector3d fstart = toPlacement(Last).getPosition();
Vector3d fend = toPlacement(Last).getPosition();
Rotation frot(fstart-fcenter,fend-fcenter);
double q0,q1,q2,q3;
frot.getValue(q0,q1,q2,q3);
KDL::Rotation rot;
rot.Quaternion(q0,q1,q2,q3);
tempPath = new KDL::Path_Circle(Last, center, norm, rot, 0.0, new KDL::RotationalInterpolation_SingleAxis(), 1.0, true);
pcPath->Add(tempPath);
Last = Next;
}
}
}
} catch (KDL::Error &e) {
throw Base::RuntimeError(e.Description());
}
#endif
}
// reimplemented from base class
unsigned int Toolpath::getMemSize() const
{
return toGCode().size();
}
void Toolpath::setCenter(const Base::Vector3d& c)
{
center = c;
recalculate();
}
static void saveCenter(Writer& writer, const Base::Vector3d& center)
{
writer.Stream() << writer.ind() << "<Center x=\"" << center.x << "\" y=\"" << center.y
<< "\" z=\"" << center.z << "\"/>" << std::endl;
}
void Toolpath::Save(Writer& writer) const
{
if (writer.isForceXML()) {
writer.Stream() << writer.ind() << "<Path count=\"" << getSize() << "\" version=\""
<< SchemaVersion << "\">" << std::endl;
writer.incInd();
saveCenter(writer, center);
for (unsigned int i = 0; i < getSize(); i++) {
vpcCommands[i]->Save(writer);
}
writer.decInd();
}
else {
writer.Stream() << writer.ind() << "<Path file=\""
<< writer.addFile((writer.ObjectName + ".nc").c_str(), this)
<< "\" version=\"" << SchemaVersion << "\">" << std::endl;
writer.incInd();
saveCenter(writer, center);
writer.decInd();
}
writer.Stream() << writer.ind() << "</Path>" << std::endl;
}
void Toolpath::SaveDocFile(Base::Writer& writer) const
{
if (toGCode().empty()) {
return;
}
writer.Stream() << toGCode();
}
void Toolpath::Restore(XMLReader& reader)
{
reader.readElement("Path");
std::string file(reader.getAttribute<const char*>("file"));
if (!file.empty()) {
// initiate a file read
reader.addFile(file.c_str(), this);
}
}
// The previous implementation read the file word-by-word, merging all content into a single string.
// This caused GCode commands and annotations to be split and parsed incorrectly.
// The new implementation reads the file line-by-line, ensuring each command and its annotations are
// handled correctly. void Toolpath::RestoreDocFile(Base::Reader& reader)
// {
// std::string gcode;
// std::string line;
// while (reader >> line) {
// gcode += line;
// gcode += " ";
// }
// setFromGCode(gcode);
// }
void Toolpath::addCommandNoRecalc(const Command& Cmd)
{
Command* tmp = new Command(Cmd);
vpcCommands.push_back(tmp);
// No recalculate here
}
void Toolpath::RestoreDocFile(Base::Reader& reader)
{
std::string line;
while (std::getline(reader.getStream(), line)) {
if (!line.empty()) {
Command cmd;
cmd.setFromGCode(line);
addCommandNoRecalc(cmd);
}
}
recalculate(); // Only once, after all commands are loaded
}
|