From 1d47ceeaea9b5b64b7201406552737488ee6ee5c Mon Sep 17 00:00:00 2001 From: Shai Seger Date: Sun, 22 Oct 2017 17:50:52 +0300 Subject: [PATCH] implement ApplyCommand and SetCurrentTool --- src/Mod/Path/PathSimulator/App/CMakeLists.txt | 2 ++ src/Mod/Path/PathSimulator/App/PathSim.cpp | 19 ++++++++++-- src/Mod/Path/PathSimulator/App/PathSim.h | 3 +- src/Mod/Path/PathSimulator/App/PathSimPy.xml | 8 +++++ .../Path/PathSimulator/App/PathSimPyImp.cpp | 31 +++++++++++++++++-- src/Mod/Path/PathSimulator/App/VolSim.cpp | 6 ++-- src/Mod/Path/PathSimulator/App/VolSim.h | 4 ++- 7 files changed, 62 insertions(+), 11 deletions(-) diff --git a/src/Mod/Path/PathSimulator/App/CMakeLists.txt b/src/Mod/Path/PathSimulator/App/CMakeLists.txt index d9fd8eb86b..cbe0975d38 100644 --- a/src/Mod/Path/PathSimulator/App/CMakeLists.txt +++ b/src/Mod/Path/PathSimulator/App/CMakeLists.txt @@ -37,6 +37,8 @@ SET(PathSimulator_SRCS generate_from_xml(PathSimPy) +SOURCE_GROUP("Python" FILES ${Python_SRCS}) + add_library(PathSimulator SHARED ${PathSimulator_SRCS}) target_link_libraries(PathSimulator ${PathSimulator_LIBS}) diff --git a/src/Mod/Path/PathSimulator/App/PathSim.cpp b/src/Mod/Path/PathSimulator/App/PathSim.cpp index 865946ddb1..ce950db858 100644 --- a/src/Mod/Path/PathSimulator/App/PathSim.cpp +++ b/src/Mod/Path/PathSimulator/App/PathSim.cpp @@ -80,9 +80,24 @@ void PathSim::SetCurrentTool(Tool * tool) } -void PathSim::ApplyCommand(Command * cmd) +void PathSim::ApplyCommand(Base::Placement * pos, Command * cmd) { - + Point3D fromPos(*pos); + Point3D toPos(cmd->getPlacement()); + if (cmd->Name == "G0" || cmd->Name == "G1") + { + m_stock->ApplyLinearTool(fromPos, toPos, *m_tool); + } + else if (cmd->Name == "G2") + { + Point3D cent(cmd->getCenter()); + m_stock->ApplyCircularTool(fromPos, toPos, cent, *m_tool, true); + } + else if (cmd->Name == "G3") + { + Point3D cent(cmd->getCenter()); + m_stock->ApplyCircularTool(fromPos, toPos, cent, *m_tool, false); + } } diff --git a/src/Mod/Path/PathSimulator/App/PathSim.h b/src/Mod/Path/PathSimulator/App/PathSim.h index 228a66aa48..aeb05e79bc 100644 --- a/src/Mod/Path/PathSimulator/App/PathSim.h +++ b/src/Mod/Path/PathSimulator/App/PathSim.h @@ -30,7 +30,6 @@ #include #include #include -#include #include #include #include @@ -53,7 +52,7 @@ namespace PathSimulator void BeginSimulation(Part::TopoShape * stock, float resolution); void SetCurrentTool(Tool * tool); - void ApplyCommand(Command * cmd); + void ApplyCommand(Base::Placement * pos, Command * cmd); public: cStock * m_stock; diff --git a/src/Mod/Path/PathSimulator/App/PathSimPy.xml b/src/Mod/Path/PathSimulator/App/PathSimPy.xml index d6228ed39b..1449f5b20e 100644 --- a/src/Mod/Path/PathSimulator/App/PathSimPy.xml +++ b/src/Mod/Path/PathSimulator/App/PathSimPy.xml @@ -39,6 +39,14 @@ Start a simulation process on a box shape stock with given resolution\n + + + + ApplyCommand(placement, command):\n + Apply a single path command on the stock starting from placement.\n + + + Return current simulation tool. diff --git a/src/Mod/Path/PathSimulator/App/PathSimPyImp.cpp b/src/Mod/Path/PathSimulator/App/PathSimPyImp.cpp index 2b530cd2aa..2979b4c4f7 100644 --- a/src/Mod/Path/PathSimulator/App/PathSimPyImp.cpp +++ b/src/Mod/Path/PathSimulator/App/PathSimPyImp.cpp @@ -23,7 +23,11 @@ #include "PreCompiled.h" #include "Mod/Path/PathSimulator/App/PathSim.h" +#include +#include #include +#include +#include #include // inclusion of the generated files (generated out of PathSimPy.xml) @@ -65,10 +69,15 @@ PyObject* PathSimPy::BeginSimulation(PyObject * args, PyObject * kwds) return Py_None; } -PyObject* PathSimPy::SetCurrentTool(PyObject * /*args*/) +PyObject* PathSimPy::SetCurrentTool(PyObject * args) { - PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented"); - return 0; + PyObject *pObjTool; + if (!PyArg_ParseTuple(args, "O!", &(Path::ToolPy::Type), &pObjTool)) + return 0; + PathSim *sim = getPathSimPtr(); + sim->SetCurrentTool(static_cast(pObjTool)->getToolPtr()); + Py_IncRef(Py_None); + return Py_None; } PyObject* PathSimPy::GetResultMesh(PyObject * args) @@ -83,6 +92,22 @@ PyObject* PathSimPy::GetResultMesh(PyObject * args) return meshpy; } + +PyObject* PathSimPy::ApplyCommand(PyObject * args, PyObject * kwds) +{ + static char *kwlist[] = { "position", "command", NULL }; + PyObject *pObjPlace; + PyObject *pObjCmd; + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O!", kwlist, &(Base::PlacementPy::Type), &pObjPlace, &(Path::CommandPy::Type), &pObjCmd)) + return 0; + PathSim *sim = getPathSimPtr(); + Base::Placement *pos = static_cast(pObjPlace)->getPlacementPtr(); + Path::Command *cmd = static_cast(pObjCmd)->getCommandPtr(); + sim->ApplyCommand(pos, cmd); + Py_IncRef(Py_None); + return Py_None; +} + /* test script import PathSimulator sim = PathSimulator.PathSim() diff --git a/src/Mod/Path/PathSimulator/App/VolSim.cpp b/src/Mod/Path/PathSimulator/App/VolSim.cpp index 1317928366..cbe4175f6d 100644 --- a/src/Mod/Path/PathSimulator/App/VolSim.cpp +++ b/src/Mod/Path/PathSimulator/App/VolSim.cpp @@ -527,7 +527,7 @@ void cStock::ApplyLinearTool(Point3D & p1, Point3D & p2, cSimTool & tool) cupAngle = 360; // end cup - for (float r = 0.5; r <= rad; r += SIM_WALK_RES) + for (float r = 0.5f; r <= rad; r += (float)SIM_WALK_RES) { Point3D cupCirc(perpDirX * r, perpDirY * r, pi2.z); float rotang = 180 * SIM_WALK_RES / (3.1415926535 * r); @@ -583,7 +583,7 @@ void cStock::ApplyCircularTool(Point3D & p1, Point3D & p2, Point3D & cent, cSimT Point3D cupCirc; float tstep = (float)SIM_WALK_RES / rad; float t = -1; - for (float r = crad1; r <= crad2; r += SIM_WALK_RES) + for (float r = crad1; r <= crad2; r += (float)SIM_WALK_RES) { cupCirc.x = xynorm.x * r; cupCirc.y = xynorm.y * r; @@ -612,7 +612,7 @@ void cStock::ApplyCircularTool(Point3D & p1, Point3D & p2, Point3D & cent, cSimT // apply end cup xynorm.SetRotationAngleRad(ang); xynorm.Rotate(); - for (float r = 0.5; r <= rad; r += SIM_WALK_RES) + for (float r = 0.5f; r <= rad; r += (float)SIM_WALK_RES) { Point3D cupCirc(xynorm.x * r, xynorm.y * r, 0); float rotang = (float)SIM_WALK_RES / r; diff --git a/src/Mod/Path/PathSimulator/App/VolSim.h b/src/Mod/Path/PathSimulator/App/VolSim.h index accc247164..42cd117fa7 100644 --- a/src/Mod/Path/PathSimulator/App/VolSim.h +++ b/src/Mod/Path/PathSimulator/App/VolSim.h @@ -37,6 +37,8 @@ struct Point3D { Point3D() {} Point3D(float x, float y, float z) : x(x), y(y), z(z) {} + Point3D(Base::Vector3d & vec) : x(vec[0]), y(vec[1]), z(vec[2]) {} + Point3D(Base::Placement & pl) : x(pl.getPosition()[0]), y(pl.getPosition()[1]), z(pl.getPosition()[2]) {} inline void set(float px, float py, float pz) { x = px; y = py; z = pz; } inline void Add(Point3D & p) { x += p.x; y += p.y; z += p.z; } inline void Rotate() { float tx = x; x = x * cosa - y * sina; y = tx * sina + y * cosa; } @@ -167,7 +169,7 @@ public: void CreatePocket(float x, float y, float rad, float height); void ApplyLinearTool(Point3D & p1, Point3D & p2, cSimTool &tool); void ApplyCircularTool(Point3D & p1, Point3D & p2, Point3D & cent, cSimTool &tool, bool isCCW); - inline Point3D & ToInner(Point3D & p) { + inline Point3D ToInner(Point3D & p) { return Point3D((p.x - m_px) / m_res, (p.y - m_py) / m_res, p.z); }