From 8ce46922f9cb240ca355a0401bfa00d09380b184 Mon Sep 17 00:00:00 2001 From: Markus Lampert Date: Thu, 16 Jan 2020 22:53:39 -0800 Subject: [PATCH] Fix Path.Length calculation. --- src/Mod/Path/App/Command.cpp | 4 ++-- src/Mod/Path/App/Command.h | 6 +++--- src/Mod/Path/App/Path.cpp | 2 +- src/Mod/Path/PathTests/TestPathCore.py | 9 +++++++++ 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Mod/Path/App/Command.cpp b/src/Mod/Path/App/Command.cpp index 3c7e9195b4ef..6403f26eeaac 100644 --- a/src/Mod/Path/App/Command.cpp +++ b/src/Mod/Path/App/Command.cpp @@ -60,7 +60,7 @@ Command::~Command() // New methods -Placement Command::getPlacement (void) const +Placement Command::getPlacement (const Base::Vector3d pos) const { static const std::string x = "X"; static const std::string y = "Y"; @@ -68,7 +68,7 @@ Placement Command::getPlacement (void) const static const std::string a = "A"; static const std::string b = "B"; static const std::string c = "C"; - Vector3d vec(getParam(x),getParam(y),getParam(z)); + Vector3d vec(getParam(x, pos.x),getParam(y, pos.y),getParam(z, pos.z)); Rotation rot; rot.setYawPitchRoll(getParam(a),getParam(b),getParam(c)); Placement plac(vec,rot); diff --git a/src/Mod/Path/App/Command.h b/src/Mod/Path/App/Command.h index 8b4f316ae9eb..850f5393ce31 100644 --- a/src/Mod/Path/App/Command.h +++ b/src/Mod/Path/App/Command.h @@ -49,7 +49,7 @@ namespace Path virtual void Restore(Base::XMLReader &/*reader*/); // specific methods - Base::Placement getPlacement (void) const; // returns a placement from the x,y,z,a,b,c parameters + Base::Placement getPlacement (const Base::Vector3d pos = Base::Vector3d()) const; // returns a placement from the x,y,z,a,b,c parameters Base::Vector3d getCenter (void) const; // returns a 3d vector from the i,j,k parameters void setCenter(const Base::Vector3d&, bool clockwise=true); // sets the center coordinates and the command name std::string toGCode (int precision=6, bool padzero=true) const; // returns a GCode string representation of the command @@ -61,9 +61,9 @@ namespace Path 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 { + inline double getParam(const std::string &name, double fallback = 0.0) const { auto it = Parameters.find(name); - return it==Parameters.end()?0.0:it->second; + return it==Parameters.end() ? fallback : it->second; } // attributes diff --git a/src/Mod/Path/App/Path.cpp b/src/Mod/Path/App/Path.cpp index bf6b2af172f6..c802a61768f3 100644 --- a/src/Mod/Path/App/Path.cpp +++ b/src/Mod/Path/App/Path.cpp @@ -129,7 +129,7 @@ double Toolpath::getLength() Vector3d next; for(std::vector::const_iterator it = vpcCommands.begin();it!=vpcCommands.end();++it) { std::string name = (*it)->Name; - next = (*it)->getPlacement().getPosition(); + next = (*it)->getPlacement(last).getPosition(); if ( (name == "G0") || (name == "G00") || (name == "G1") || (name == "G01") ) { // straight line l += (next - last).Length(); diff --git a/src/Mod/Path/PathTests/TestPathCore.py b/src/Mod/Path/PathTests/TestPathCore.py index efa31840a733..96c5c0de379f 100644 --- a/src/Mod/Path/PathTests/TestPathCore.py +++ b/src/Mod/Path/PathTests/TestPathCore.py @@ -155,3 +155,12 @@ def test20(self): self.assertEqual(len(table.Tools), 2) self.assertEqual(str(table.Tools), '{1: Tool 12.7mm Drill Bit, 2: Tool my other tool}' ) + + def test50(self): + """Test Path.Length calculation""" + commands = [] + commands.append(Path.Command("G1",{"X":1})) + commands.append(Path.Command("G1",{"Y":1})) + path = Path.Path(commands) + + self.assertEqual(path.Length, 2)