├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE ├── README.md ├── cmake └── modules │ └── FindSFML.cmake ├── deps ├── circle3d │ ├── CMakeLists.txt │ ├── circle3d.cpp │ └── circle3d.hpp └── clipper │ ├── CMakeLists.txt │ ├── License.txt │ ├── README │ ├── clipper.cpp │ └── clipper.hpp ├── nc_tools.conf ├── robot_backplot.png ├── src ├── CMakeLists.txt ├── Simulation.cpp ├── Simulation.h ├── Stock.cpp ├── Stock.h ├── Tool.cpp ├── Tool.h ├── base │ ├── CMakeLists.txt │ ├── machine_config.cpp │ ├── machine_config.h │ ├── nc_config.cpp │ ├── nc_config.h │ ├── rs274_base.cpp │ └── rs274_base.h ├── fold_adjacent.h ├── lua │ ├── CMakeLists.txt │ ├── luaconfig.cpp │ ├── luaconfig.h │ ├── state.cpp │ └── state.h ├── nc_annotate │ ├── CMakeLists.txt │ ├── annotate_curvature.cpp │ ├── annotate_engagement.cpp │ ├── rs274_annotate.cpp │ └── rs274_annotate.h ├── nc_arcfit │ ├── CMakeLists.txt │ ├── arcfit.cpp │ ├── geometry_3.cpp │ ├── geometry_3.h │ ├── rs274_arcfit.cpp │ └── rs274_arcfit.h ├── nc_backplot │ ├── CMakeLists.txt │ ├── backplot.cpp │ ├── rs274_backplot.cpp │ └── rs274_backplot.h ├── nc_bounds │ ├── CMakeLists.txt │ ├── bounds.cpp │ ├── rs274_bounds.cpp │ └── rs274_bounds.h ├── nc_contour_pocket │ ├── CMakeLists.txt │ ├── common.h │ ├── contour_drill.cpp │ ├── contour_offset.cpp │ ├── contour_pocket.cpp │ ├── contour_profile.cpp │ ├── contour_spiral.cpp │ ├── rs274_clipper_path.cpp │ ├── rs274_clipper_path.h │ └── spiral_pocket.cpp ├── nc_delay │ ├── CMakeLists.txt │ ├── delay.cpp │ ├── rs274_delay.cpp │ └── rs274_delay.h ├── nc_feedrate │ ├── CMakeLists.txt │ ├── feedrate.cpp │ ├── rs274_feedrate.cpp │ └── rs274_feedrate.h ├── nc_identity │ ├── CMakeLists.txt │ ├── identity.cpp │ ├── rs274_identity.cpp │ └── rs274_identity.h ├── nc_lathe_roughing │ ├── CMakeLists.txt │ ├── geometry.cpp │ ├── geometry.h │ ├── lathe_roughing.cpp │ ├── rs274_lathe_path.cpp │ └── rs274_lathe_path.h ├── nc_linear_ramp │ ├── CMakeLists.txt │ ├── linear_ramp.cpp │ ├── rs274_ramp.cpp │ └── rs274_ramp.h ├── nc_mill_roughing │ ├── CMakeLists.txt │ └── mill_roughing.cpp ├── nc_model │ ├── CMakeLists.txt │ ├── model.cpp │ ├── rs274_model.cpp │ └── rs274_model.h ├── nc_offset_rotational_origin │ ├── CMakeLists.txt │ ├── offset_rotational_origin.cpp │ ├── rs274_offset.cpp │ └── rs274_offset.h ├── nc_rename_axis │ ├── CMakeLists.txt │ ├── rename_axis.cpp │ ├── rs274_rename.cpp │ └── rs274_rename.h ├── nc_shortlines │ ├── CMakeLists.txt │ ├── rs274_shortlines.cpp │ ├── rs274_shortlines.h │ └── shortlines.cpp ├── nc_slice │ ├── CMakeLists.txt │ └── slice.cpp ├── nc_stock │ ├── CMakeLists.txt │ └── stock.cpp ├── nc_svgpath │ ├── CMakeLists.txt │ └── svgpath.cpp ├── nc_tooltable │ ├── CMakeLists.txt │ └── tools.cpp ├── nc_transform │ ├── CMakeLists.txt │ ├── rs274_transform.cpp │ ├── rs274_transform.h │ └── transform.cpp ├── print_exception.cpp ├── print_exception.h ├── r6.h └── throw_if.h └── test └── CMakeLists.txt /.gitignore: -------------------------------------------------------------------------------- 1 | .ycm_extra_conf.pyc 2 | build 3 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "deps/rs274ngc"] 2 | path = deps/rs274ngc 3 | url = git@github.com:mythagel/rs274ngc.git 4 | [submodule "deps/cxxcam"] 5 | path = deps/cxxcam 6 | url = git@github.com:mythagel/cxxcam.git 7 | [submodule "deps/geom"] 8 | path = deps/geom 9 | url = git@github.com:mythagel/geom.git 10 | [submodule "deps/svg_path"] 11 | path = deps/svg_path 12 | url = git@github.com:mythagel/svg_path.git 13 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6) 2 | PROJECT(nc_tools) 3 | 4 | SET(CMAKE_EXPORT_COMPILE_COMMANDS ON) 5 | 6 | ENABLE_TESTING() 7 | IF(NOT TARGET check) 8 | ADD_CUSTOM_TARGET(check COMMAND ${CMAKE_CTEST_COMMAND}) 9 | ENDIF() 10 | 11 | SET( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/" ) 12 | 13 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 14 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 15 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) 16 | 17 | ADD_SUBDIRECTORY(deps/rs274ngc) 18 | ADD_SUBDIRECTORY(deps/cxxcam) 19 | ADD_SUBDIRECTORY(deps/svg_path) 20 | ADD_SUBDIRECTORY(deps/geom) 21 | ADD_SUBDIRECTORY(deps/clipper) 22 | ADD_SUBDIRECTORY(deps/circle3d) 23 | ADD_SUBDIRECTORY(src) 24 | ADD_SUBDIRECTORY(test) 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | nc_tools 2 | ======== 3 | 4 | A collection of composable CNC/CAM GCode tools. 5 | 6 | ``` 7 | $ cat robot2.ngc | nc_backplot 8 | ``` 9 | 10 | ![robot backplot](https://raw.github.com/mythagel/backplot/master/robot_backplot.png) 11 | 12 | Implemented utilities 13 | ===================== 14 | 15 | * [nc_backplot](http://mythagel.github.io/nc_tools/nc_backplot/) 16 | * display a backplot of the incoming gcode 17 | * [nc_model](http://mythagel.github.io/nc_tools/nc_model/) 18 | * generate a 3d model of the incoming gcode, given tools and stock 19 | * [nc_rename_axis](http://mythagel.github.io/nc_tools/nc_rename_axis/) 20 | * rename an axis, a=> is delete, a=>b is rename, a<=>b is swap 21 | * nc_bounds 22 | * bounding box of gcode or .off model 23 | * nc_transform 24 | * transform / rotate stock for next operation 25 | * nc_transform -a 45 -b 45 -x -30 # rotate 45 deg around x, 45 around y, translate -30 on x 26 | * nc_tooltable 27 | * generate linuxcnc tool table from nc_tools.conf 28 | * [nc_contour_pocket](http://mythagel.github.io/nc_tools/nc_contour_pocket/) 29 | * Generate contour parallel toolpath from gcode path 30 | * [nc_stock](http://mythagel.github.io/nc_tools/nc_stock/) 31 | * Generate .off stock models 32 | * [nc_svgpath](http://mythagel.github.io/nc_tools/nc_svgpath/) 33 | * SVG path data -> gcode converter 34 | * nc_identity 35 | * gcode identity transformation - base for new tools 36 | * [nc_arcfit](http://mythagel.github.io/nc_tools/nc_arcfit/) 37 | * recover G2/G3 arcs from linear paths 38 | * nc_delay 39 | * delay the output of the gcode based on the machining time option 1x 2x, etc 40 | * nc_shortlines 41 | * split incoming gcode into short line segments 42 | * ~~cli option --arc-only~~ 43 | * nc_slice 44 | * Segments a 3d model into 2d slices for further processing 45 | * nc_contour_drill 46 | * Generate a drill pattern bounded by the input gcode path 47 | * nc_contour_offset 48 | * Generate contour parallel offsets from gcode path 49 | * nc_linear_ramp 50 | * Convert plunge into linear ramp along path 51 | 52 | 53 | ~~not implemented / not complete~~ 54 | 55 | * ~~nc_fold_moves~~ 56 | * fold adajacent G00 moves 57 | * ~~nc_pick~~ 58 | * nc_stop --at-comment blah --at-line 205 --at-tool 3 59 | * selectively pick gcode from file 60 | * e.g. within bounding box 61 | * ~~nc_inspect~~ 62 | * allow playback of the gcode 63 | * feed rate override + pause 64 | * seek forward and backward 65 | * zoom to section of timeline 66 | * ~~nc_scale~~ 67 | * scale gcode axes up or down 68 | * ~~nc_offset_rotational_origin~~ 69 | * translate to/from tool local (canonical) rotations to world rotation (e.g. rotary tables) 70 | * ~~nc_feedrate~~ 71 | * analyse / optimise feedrate of provided gcode 72 | * ~~nc_lathe_roughing~~ 73 | * G70/G71 canned cycle style lathe roughing generator 74 | * ~~nc_blueprint~~ 75 | * Generate SVG blueprint with 3 ortho + perspective view of gcode / model. 76 | * ~~nc_annotate~~ 77 | * Analyses and adds annotations to input gcode, e.g. path continuity 78 | * ~~nc_contour_spiral~~ 79 | * Generate spiral toolpaths from gcode path 80 | 81 | Generators 82 | ========== 83 | 84 | * ~~lathe_path~~ 85 | * generate lathe operations for svg path 86 | * TODO determine machining strategy for lathe operations based on different tools and back faces 87 | * ~~mill_path~~ 88 | * generate profile operation for svg path 89 | * pick from SVG file with xpath pattern 90 | * opt. scale / transform 91 | * profile / pocket 92 | -------------------------------------------------------------------------------- /deps/circle3d/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0) 2 | PROJECT(circle3d) 3 | 4 | ADD_LIBRARY(circle3d STATIC circle3d.cpp) 5 | -------------------------------------------------------------------------------- /deps/circle3d/circle3d.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // 3 | // Copyright (c) 2018 Sergio Garrido-Jurado (sgarrido2011@gmail.com) 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #include "circle3d.hpp" 24 | #include 25 | 26 | namespace circle3d 27 | { 28 | 29 | /** 30 | * @brief Basic 3D vector class 31 | */ 32 | class Vector3D 33 | { 34 | public: 35 | double x,y,z; 36 | 37 | Vector3D(double _x=0, double _y=0, double _z=0) 38 | : x(_x), y(_y), z(_z) 39 | { 40 | } 41 | 42 | double dot(Vector3D v2) 43 | { 44 | return x*v2.x + y*v2.y + z*v2.z; 45 | } 46 | 47 | Vector3D operator+(const Vector3D& v2) 48 | { 49 | return Vector3D(x+v2.x, y+v2.y, z+v2.z); 50 | } 51 | 52 | Vector3D operator-(const Vector3D& v2) 53 | { 54 | return Vector3D(x-v2.x, y-v2.y, z-v2.z); 55 | } 56 | 57 | Vector3D operator*(const double& sc) 58 | { 59 | return Vector3D(sc*x, sc*y, sc*z); 60 | } 61 | 62 | double norm() 63 | { 64 | return sqrt(dot(*this)); 65 | } 66 | 67 | }; 68 | 69 | 70 | /** 71 | * @brief estimate the center and the radious of a circle in 3D given 3 points 72 | * This is the relevant function 73 | */ 74 | static void estimate3DCircle(Vector3D p1, Vector3D p2, Vector3D p3, Vector3D &c, double &radius) 75 | { 76 | Vector3D v1 = p2-p1; 77 | Vector3D v2 = p3-p1; 78 | double v1v1, v2v2, v1v2; 79 | v1v1 = v1.dot(v1); 80 | v2v2 = v2.dot(v2); 81 | v1v2 = v1.dot(v2); 82 | 83 | float base = 0.5/(v1v1*v2v2-v1v2*v1v2); 84 | float k1 = base*v2v2*(v1v1-v1v2); 85 | float k2 = base*v1v1*(v2v2-v1v2); 86 | c = p1 + v1*k1 + v2*k2; // center 87 | 88 | radius = (c-p1).norm(); 89 | } 90 | 91 | void estimate3DCircle(Point3D p1, Point3D p2, Point3D p3, Point3D& c, double& radius) 92 | { 93 | Vector3D center; 94 | estimate3DCircle({p1.x, p1.y, p1.z}, {p2.x, p2.y, p2.z}, {p3.x, p3.y, p3.z}, center, radius); 95 | c.x = center.x; 96 | c.y = center.y; 97 | c.z = center.z; 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /deps/circle3d/circle3d.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CIRCLE3D_H 2 | #define CIRCLE3D_H 3 | 4 | namespace circle3d 5 | { 6 | 7 | struct Point3D 8 | { 9 | double x,y,z; 10 | }; 11 | 12 | void estimate3DCircle(Point3D p1, Point3D p2, Point3D p3, Point3D& c, double& radius); 13 | 14 | } 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /deps/clipper/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0) 2 | PROJECT(polyclipping) 3 | 4 | ADD_LIBRARY(polyclipping STATIC clipper.cpp) 5 | 6 | SET_TARGET_PROPERTIES(polyclipping PROPERTIES VERSION 22.0.0 SOVERSION 22 ) 7 | -------------------------------------------------------------------------------- /deps/clipper/License.txt: -------------------------------------------------------------------------------- 1 | Boost Software License - Version 1.0 - August 17th, 2003 2 | http://www.boost.org/LICENSE_1_0.txt 3 | 4 | Permission is hereby granted, free of charge, to any person or organization 5 | obtaining a copy of the software and accompanying documentation covered by 6 | this license (the "Software") to use, reproduce, display, distribute, 7 | execute, and transmit the Software, and to prepare derivative works of the 8 | Software, and to permit third-parties to whom the Software is furnished to 9 | do so, all subject to the following: 10 | 11 | The copyright notices in the Software and this entire statement, including 12 | the above license grant, this restriction and the following disclaimer, 13 | must be included in all copies of the Software, in whole or in part, and 14 | all derivative works of the Software, unless such copies or derivative 15 | works are solely in the form of machine-executable object code generated by 16 | a source language processor. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 21 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 22 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 23 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24 | DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /deps/clipper/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mythagel/nc_tools/d1c95a1d0cc7fe0399eaebcd731adf038e9919e6/deps/clipper/README -------------------------------------------------------------------------------- /deps/clipper/clipper.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mythagel/nc_tools/d1c95a1d0cc7fe0399eaebcd731adf038e9919e6/deps/clipper/clipper.cpp -------------------------------------------------------------------------------- /nc_tools.conf: -------------------------------------------------------------------------------- 1 | -- Complete general configuration for nc tools 2 | -- searched for in cwd of tool, or specified with config file 3 | -- (cwd more convenient - expected that tools will be executed from project root) 4 | -- not all configuration used by all tools BUT ONE config file for all tools with different 5 | -- sections set. 6 | 7 | default = { 8 | machine = "mill0", 9 | units = "metric" 10 | } 11 | 12 | -- Machine configuration 13 | machine = {} 14 | 15 | machine.mill0 = { 16 | type = "mill", 17 | spindle = {"100-1000", "2000-6000"}, 18 | 19 | tool_table = { 20 | [1] = { 21 | name = "1mm end mill", 22 | length = 20, 23 | diameter = 1, 24 | flute_length = 10, 25 | shank_diameter = 3.175 26 | }, 27 | [3] = { 28 | name = "8mm carbide end mill", 29 | length = 50, 30 | diameter = 8, 31 | flute_length = 20, 32 | shank_diameter = 8 33 | } 34 | } 35 | } 36 | 37 | -- lathe tool orientation 38 | -- 1 = -x +z 39 | -- 2 = -x -z 40 | -- 3 = +x -z 41 | -- 4 = +x +z 42 | -- 5 = x0 +z 43 | -- 6 = -x z0 44 | -- 7 = x0 -z 45 | -- 8 = +x z0 46 | -- 9 = 0,0 47 | machine.lathe0 = { 48 | type = "lathe", 49 | spindle = {"100-1000", "2000-6000"}, 50 | 51 | tool_table = { 52 | [1] = { 53 | name = "1mm end mill", 54 | front_angle = 0, 55 | back_angle = 0, 56 | orientation = 0 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /robot_backplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mythagel/nc_tools/d1c95a1d0cc7fe0399eaebcd731adf038e9919e6/robot_backplot.png -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") 3 | add_subdirectory(lua) 4 | add_subdirectory(base) 5 | 6 | add_subdirectory(nc_backplot) 7 | add_subdirectory(nc_model) 8 | add_subdirectory(nc_stock) 9 | add_subdirectory(nc_transform) 10 | add_subdirectory(nc_delay) 11 | add_subdirectory(nc_bounds) 12 | add_subdirectory(nc_tooltable) 13 | add_subdirectory(nc_identity) 14 | add_subdirectory(nc_offset_rotational_origin) 15 | add_subdirectory(nc_feedrate) 16 | add_subdirectory(nc_svgpath) 17 | add_subdirectory(nc_lathe_roughing) 18 | add_subdirectory(nc_rename_axis) 19 | add_subdirectory(nc_contour_pocket) 20 | add_subdirectory(nc_arcfit) 21 | add_subdirectory(nc_shortlines) 22 | add_subdirectory(nc_slice) 23 | add_subdirectory(nc_annotate) 24 | add_subdirectory(nc_linear_ramp) 25 | add_subdirectory(nc_mill_roughing) 26 | -------------------------------------------------------------------------------- /src/Simulation.cpp: -------------------------------------------------------------------------------- 1 | /* cxxcam - C++ CAD/CAM driver library. 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * Simulation.cpp 20 | * 21 | * Created on: 2013-07-11 22 | * Author: nicholas 23 | */ 24 | 25 | #include "Simulation.h" 26 | #include "geom/translate.h" 27 | #include "geom/ops.h" 28 | #include "geom/query.h" 29 | #include "fold_adjacent.h" 30 | #include 31 | #include 32 | 33 | namespace cxxcam 34 | { 35 | namespace simulation 36 | { 37 | 38 | geom::polyhedron_t sweep_tool(geom::polyhedron_t tool, const path::step& s0, const path::step& s1) 39 | { 40 | using units::length_mm; 41 | 42 | static const math::quaternion_t identity{1,0,0,0}; 43 | 44 | const auto& o0 = s0.orientation; 45 | const auto& p0 = s0.position; 46 | const auto& p1 = s1.position; 47 | 48 | if(o0 != identity) 49 | tool = geom::rotate(tool, o0.R_component_1(), o0.R_component_2(), o0.R_component_3(), o0.R_component_4()); 50 | 51 | if(distance(p0, p1) > units::length{0.000001 * units::millimeters}) 52 | { 53 | geom::polyline_t path{ { {length_mm(p0.x).value(), length_mm(p0.y).value(), length_mm(p0.z).value()}, 54 | {length_mm(p1.x).value(), length_mm(p1.y).value(), length_mm(p1.z).value()} } }; 55 | return geom::glide(tool, path); 56 | } 57 | 58 | return translate(tool, length_mm(p0.x).value(), length_mm(p0.y).value(), length_mm(p0.z).value()); 59 | } 60 | 61 | geom::polyhedron_t sweep_lathe_tool(geom::polyhedron_t tool, const path::step& s0, const path::step& s1, units::plane_angle spindle_theta) 62 | { 63 | using units::length_mm; 64 | 65 | static const math::quaternion_t identity{1,0,0,0}; 66 | 67 | const auto& o0 = s0.orientation; 68 | const auto& p0 = s0.position; 69 | const auto& p1 = s1.position; 70 | auto so = identity; 71 | so /= math::normalise(math::axis2quat(0, 0, 1, spindle_theta)); 72 | 73 | if(o0 != identity) 74 | tool = geom::rotate(tool, o0.R_component_1(), o0.R_component_2(), o0.R_component_3(), o0.R_component_4()); 75 | 76 | if(distance(p0, p1) > units::length{0.000001 * units::millimeters}) 77 | { 78 | geom::polyline_t path{ { {length_mm(p0.x).value(), length_mm(p0.y).value(), length_mm(p0.z).value()}, 79 | {length_mm(p1.x).value(), length_mm(p1.y).value(), length_mm(p1.z).value()} } }; 80 | tool = geom::glide(tool, path); 81 | } 82 | else 83 | { 84 | tool = translate(tool, length_mm(p0.x).value(), length_mm(p0.y).value(), length_mm(p0.z).value()); 85 | } 86 | 87 | return geom::rotate(tool, so.R_component_1(), so.R_component_2(), so.R_component_3(), so.R_component_4()); 88 | } 89 | 90 | Bbox bounding_box(const std::vector& steps) 91 | { 92 | if(steps.empty()) 93 | return {}; 94 | 95 | auto s0 = steps.front(); 96 | return std::accumulate(++begin(steps), end(steps), Bbox{s0.position, s0.position}, [](Bbox& b, const path::step& s0) { return b + s0.position; } ); 97 | } 98 | 99 | geom::polyhedron_t remove_material(const geom::polyhedron_t& tool, const geom::polyhedron_t& stock, const std::vector& steps) 100 | { 101 | auto fold_path = [&tool](std::vector::const_iterator begin, std::vector::const_iterator end) -> geom::polyhedron_t 102 | { 103 | std::vector tool_motion; 104 | 105 | fold_adjacent(begin, end, std::back_inserter(tool_motion), 106 | [&tool](const path::step& s0, const path::step& s1) -> geom::polyhedron_t 107 | { 108 | return sweep_tool(tool, s0, s1); 109 | }); 110 | 111 | return geom::merge(tool_motion); 112 | }; 113 | 114 | auto tool_path = fold_path(begin(steps), end(steps)); 115 | return stock - tool_path; 116 | } 117 | 118 | result_t run(const simulation_t& simulation) 119 | { 120 | result_t result; 121 | 122 | result.stock = remove_material(simulation.tool.Model(), simulation.stock.Model, simulation.steps.path); 123 | result.bounding_box = bounding_box(simulation.steps.path); 124 | 125 | return result; 126 | } 127 | 128 | } 129 | } 130 | 131 | -------------------------------------------------------------------------------- /src/Simulation.h: -------------------------------------------------------------------------------- 1 | /* cxxcam - C++ CAD/CAM driver library. 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * Simulation.h 20 | * 21 | * Created on: 2013-07-11 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef SIMULATION_H_ 26 | #define SIMULATION_H_ 27 | #include 28 | #include "cxxcam/Path.h" 29 | #include "Tool.h" 30 | #include "Stock.h" 31 | #include "cxxcam/Units.h" 32 | #include "cxxcam/Limits.h" 33 | #include "cxxcam/Bbox.h" 34 | 35 | namespace cxxcam 36 | { 37 | namespace simulation 38 | { 39 | 40 | /* 41 | * Sweep the tool along the path given, applying any needed transformations. 42 | */ 43 | geom::polyhedron_t sweep_tool(geom::polyhedron_t tool, const path::step& s0, const path::step& s1); 44 | geom::polyhedron_t sweep_lathe_tool(geom::polyhedron_t tool, const path::step& s0, const path::step& s1, units::plane_angle spindle_theta); 45 | 46 | // TODO function to iterate path and validate feedrates 47 | // TODO function to iterate path and calculate time 48 | 49 | Bbox bounding_box(const std::vector& steps); 50 | geom::polyhedron_t remove_material(const geom::polyhedron_t& tool, const geom::polyhedron_t& stock, const std::vector& steps); 51 | 52 | /* 53 | * TODO simulation::run aggregates the individual functions above into a single interface. 54 | * TODO It is too easy with the current interface to forget to set a parameter. 55 | * Add a constructor to ensure fields are set. 56 | */ 57 | struct simulation_t 58 | { 59 | path::path_t steps; 60 | 61 | Stock stock; 62 | Tool tool; 63 | }; 64 | struct result_t 65 | { 66 | Stock stock; 67 | 68 | Bbox bounding_box; 69 | }; 70 | result_t run(const simulation_t& simulation); 71 | 72 | } 73 | } 74 | 75 | #endif /* SIMULATION_H_ */ 76 | -------------------------------------------------------------------------------- /src/Stock.cpp: -------------------------------------------------------------------------------- 1 | /* cxxcam - C++ CAD/CAM driver library. 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * Stock.cpp 20 | * 21 | * Created on: 26/04/2012 22 | * Author: nicholas 23 | */ 24 | 25 | #include "Stock.h" 26 | #include 27 | #include 28 | #include "geom/io.h" 29 | #include 30 | 31 | namespace cxxcam 32 | { 33 | 34 | Stock::Stock(const geom::polyhedron_t& model) 35 | : Model(model) 36 | { 37 | } 38 | 39 | } 40 | 41 | -------------------------------------------------------------------------------- /src/Stock.h: -------------------------------------------------------------------------------- 1 | /* cxxcam - C++ CAD/CAM driver library. 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * Stock.h 20 | * 21 | * Created on: 26/04/2012 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef STOCK_H_ 26 | #define STOCK_H_ 27 | #include 28 | #include "cxxcam/Material.h" 29 | #include "geom/polyhedron.h" 30 | #include 31 | 32 | namespace cxxcam 33 | { 34 | 35 | /* 36 | * Stores a description and model of the stock from which material will be removed. 37 | * Also should reference properties on the material the stock is made out of. 38 | */ 39 | struct Stock 40 | { 41 | std::shared_ptr Material; 42 | geom::polyhedron_t Model; 43 | 44 | Stock() = default; 45 | Stock(const geom::polyhedron_t& model); 46 | }; 47 | 48 | } 49 | 50 | #endif /* STOCK_H_ */ 51 | -------------------------------------------------------------------------------- /src/Tool.cpp: -------------------------------------------------------------------------------- 1 | /* cxxcam - C++ CAD/CAM driver library. 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * Tool.cpp 20 | * 21 | * Created on: 26/04/2012 22 | * Author: nicholas 23 | */ 24 | 25 | #include "Tool.h" 26 | #include "geom/primitives.h" 27 | #include "cxxcam/Error.h" 28 | 29 | namespace cxxcam 30 | { 31 | 32 | namespace 33 | { 34 | 35 | geom::polyhedron_t make_mill_tool(const Tool::Mill& em) 36 | { 37 | using namespace geom; 38 | 39 | // TODO determine slices based on height under arc + accuracy. 40 | auto shank = make_cone( {0, 0, em.length}, {0, 0, em.cutting_length}, em.shank_diameter/2, em.shank_diameter/2, 8); 41 | 42 | switch(em.type) 43 | { 44 | case Tool::Mill::Type::End: 45 | { 46 | auto flutes = make_cone( {0, 0, em.cutting_length}, {0, 0, 0}, em.mill_diameter/2, em.mill_diameter/2, 8); 47 | return shank + flutes; 48 | } 49 | default: 50 | throw error("Unimplemented mill type."); 51 | } 52 | 53 | // Shank tapers to cutting diameter. 54 | // auto shank = make_cone(0, 0, em.cutting_length+em.shank_length, 0, 0, em.cutting_length, em.shank_diameter/2, em.cutting_diameter/2, 64); 55 | 56 | throw error("Unable to create tool geometry model"); 57 | } 58 | 59 | } 60 | 61 | Tool::Tool() 62 | : m_Name("Invalid") 63 | { 64 | } 65 | 66 | Tool::Tool(const std::string& name, const Mill& mill) 67 | : m_Name(name), m_Type(Type::Mill), m_Mill(mill), m_Model(make_mill_tool(m_Mill)) 68 | { 69 | } 70 | Tool::Tool(const std::string& name, const Lathe& lathe) 71 | : m_Name(name), m_Type(Type::Lathe), m_Lathe(lathe) 72 | { 73 | } 74 | 75 | std::string Tool::Name() const 76 | { 77 | return m_Name; 78 | } 79 | 80 | Tool::Type Tool::ToolType() const 81 | { 82 | return m_Type; 83 | } 84 | 85 | auto Tool::GetMill() const -> Mill 86 | { 87 | if(m_Type != Type::Mill) 88 | throw error("GetMill: Tool is not Mill type."); 89 | 90 | return m_Mill; 91 | } 92 | 93 | auto Tool::GetLathe() const -> Lathe 94 | { 95 | if(m_Type != Type::Lathe) 96 | throw error("GetLathe: Tool is not Lathe type."); 97 | 98 | return m_Lathe; 99 | } 100 | 101 | geom::polyhedron_t Tool::Model() const 102 | { 103 | return m_Model; 104 | } 105 | 106 | } 107 | 108 | -------------------------------------------------------------------------------- /src/Tool.h: -------------------------------------------------------------------------------- 1 | /* cxxcam - C++ CAD/CAM driver library. 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * Tool.h 20 | * 21 | * Created on: 26/04/2012 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef TOOL_H_ 26 | #define TOOL_H_ 27 | #include 28 | #include "geom/polyhedron.h" 29 | 30 | namespace cxxcam 31 | { 32 | 33 | /* 34 | * Representation of the cutting tool used to remove material from the Stock. 35 | */ 36 | class Tool 37 | { 38 | public: 39 | enum class Type 40 | { 41 | Mill, 42 | Lathe 43 | }; 44 | 45 | struct Mill 46 | { 47 | enum class Type 48 | { 49 | End, 50 | Ball, 51 | Radius, 52 | Face, 53 | FlyCutter 54 | } type; 55 | 56 | // Whether the tool is suitable for plunge cuts. 57 | bool center_cutting; 58 | 59 | int flutes; 60 | double flute_length; 61 | double core_diameter; 62 | 63 | // Used for model generation 64 | double cutting_length; 65 | double mill_diameter; 66 | double shank_diameter; 67 | double length; 68 | }; 69 | 70 | struct Lathe 71 | { 72 | 73 | }; 74 | private: 75 | std::string m_Name; 76 | Type m_Type; 77 | union 78 | { 79 | Mill m_Mill; 80 | Lathe m_Lathe; 81 | }; 82 | geom::polyhedron_t m_Model; 83 | public: 84 | Tool(); 85 | 86 | Tool(const std::string& name, const Mill& mill); 87 | Tool(const std::string& name, const Lathe& lathe); 88 | 89 | std::string Name() const; 90 | Type ToolType() const; 91 | Mill GetMill() const; 92 | Lathe GetLathe() const; 93 | 94 | geom::polyhedron_t Model() const; 95 | }; 96 | 97 | } 98 | 99 | #endif /* TOOL_H_ */ 100 | -------------------------------------------------------------------------------- /src/base/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Lua REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options system filesystem REQUIRED) 8 | 9 | include_directories( 10 | ${LUA_INCLUDE_DIR} 11 | ${Boost_INCLUDE_DIRS} 12 | ${PROJECT_SOURCE_DIR}/src 13 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 14 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 15 | ) 16 | 17 | add_library(nc_base STATIC rs274_base.cpp nc_config.cpp machine_config.cpp) 18 | target_link_libraries(nc_base 19 | ${LUA_LIBRARIES} 20 | ${Boost_LIBRARIES} 21 | nc_lua 22 | rs274ngc 23 | cxxcam 24 | ) 25 | -------------------------------------------------------------------------------- /src/base/machine_config.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * machine_config.cpp 20 | * 21 | * Created on: 2016-04-30 22 | * Author: nicholas 23 | */ 24 | 25 | #include "machine_config.h" 26 | #include "../throw_if.h" 27 | #include 28 | 29 | namespace po = boost::program_options; 30 | 31 | namespace { 32 | 33 | template 34 | class guard { 35 | private: 36 | Fn fn; 37 | bool armed; 38 | public: 39 | guard(Fn fn) : fn(fn), armed(true) {} 40 | void disarm() { armed = false; } 41 | ~guard() { if (armed) fn(); } 42 | }; 43 | template guard make_guard(Fn fn) { 44 | return { fn }; 45 | } 46 | 47 | void get_machine(lua::state& L, const std::string& name) { 48 | lua_getglobal(L, "machine"); 49 | auto pop_global = make_guard([&]{ lua_pop(L, 1); }); 50 | throw_if (!lua_istable(L, -1), "machine config missing / incorrect"); 51 | 52 | 53 | lua_getfield(L, -1, name.c_str()); 54 | auto pop_machine_field = make_guard([&]{ lua_pop(L, 1); }); 55 | throw_if(!lua_istable(L, -1), "named machine config missing"); 56 | 57 | lua_remove(L, -2); 58 | pop_global.disarm(); 59 | pop_machine_field.disarm(); 60 | } 61 | 62 | } 63 | 64 | namespace machine_config { 65 | 66 | po::options_description base_options() { 67 | po::options_description options("base options"); 68 | options.add_options() 69 | ("config", po::value()->default_value(""), "Configuration file") 70 | ("machine", po::value(), "Machine configuration") 71 | ; 72 | 73 | return options; 74 | } 75 | 76 | std::string default_machine(nc_config& config) { 77 | auto& L = config.state(); 78 | 79 | lua_getglobal(L, "default"); 80 | auto pop_global = make_guard([&]{ lua_pop(L, 1); }); 81 | if (lua_isnil(L, -1)) return "__default__"; 82 | throw_if (!lua_istable(L, -1), "default config missing / incorrect"); 83 | 84 | lua_getfield(L, -1, "machine"); 85 | auto pop_machine_field = make_guard([&]{ lua_pop(L, 1); }); 86 | throw_if(!lua_isstring(L, -1), "machine default string missing / incorrect"); 87 | 88 | return lua_tostring(L, -1); 89 | } 90 | 91 | units default_units(nc_config& config) { 92 | auto& L = config.state(); 93 | 94 | lua_getglobal(L, "default"); 95 | auto pop_global = make_guard([&]{ lua_pop(L, 1); }); 96 | if (lua_isnil(L, -1)) return units::metric; 97 | throw_if (!lua_istable(L, -1), "default config missing / incorrect"); 98 | 99 | lua_getfield(L, -1, "units"); 100 | auto pop_units_field = make_guard([&]{ lua_pop(L, 1); }); 101 | throw_if(!lua_isstring(L, -1), "units string missing / incorrect"); 102 | 103 | if(strcmp(lua_tostring(L, -1), "metric") == 0) 104 | return units::metric; 105 | if(strcmp(lua_tostring(L, -1), "imperial") == 0) 106 | return units::imperial; 107 | 108 | throw std::runtime_error("unrecognised units"); 109 | } 110 | units machine_units(nc_config& config, const std::string& machine) { 111 | auto& L = config.state(); 112 | 113 | if (machine == "__default__") 114 | return units::metric; 115 | 116 | get_machine(L, machine); 117 | auto pop_machine_field = make_guard([&]{ lua_pop(L, 1); }); 118 | 119 | lua_getfield(L, -1, "units"); 120 | auto pop_units_field = make_guard([&]{ lua_pop(L, 1); }); 121 | 122 | if (!lua_isnil(L, -1)) { 123 | throw_if(!lua_isstring(L, -1), "units string missing / incorrect"); 124 | 125 | if(strcmp(lua_tostring(L, -1), "metric") == 0) 126 | return units::metric; 127 | if(strcmp(lua_tostring(L, -1), "imperial") == 0) 128 | return units::imperial; 129 | 130 | throw std::runtime_error("unrecognised machine type"); 131 | } else { 132 | return default_units(config); 133 | } 134 | } 135 | 136 | machine_type get_machine_type(nc_config& config, const std::string& machine) { 137 | auto& L = config.state(); 138 | 139 | if (machine == "__default__") 140 | return machine_type::mill; 141 | 142 | get_machine(L, machine); 143 | auto pop_machine_field = make_guard([&]{ lua_pop(L, 1); }); 144 | 145 | lua_getfield(L, -1, "type"); 146 | auto pop_type_field = make_guard([&]{ lua_pop(L, 1); }); 147 | throw_if(!lua_isstring(L, -1), "machine type string missing / incorrect"); 148 | 149 | 150 | if(strcmp(lua_tostring(L, -1), "mill") == 0) 151 | return machine_type::mill; 152 | if(strcmp(lua_tostring(L, -1), "lathe") == 0) 153 | return machine_type::lathe; 154 | 155 | throw std::runtime_error("unrecognised machine type"); 156 | } 157 | 158 | bool get_tool(nc_config& config, unsigned id, const std::string& machine, mill_tool& tool) { 159 | auto& L = config.state(); 160 | 161 | if (machine == "__default__") 162 | return false; 163 | 164 | get_machine(L, machine); 165 | auto pop_machine_field = make_guard([&]{ lua_pop(L, 1); }); 166 | 167 | lua_getfield(L, -1, "tool_table"); 168 | auto pop_tool_table = make_guard([&]{ lua_pop(L, 1); }); 169 | throw_if (!lua_istable(L, -1), "tool_table missing / incorrect"); 170 | 171 | lua_pushinteger(L, id); 172 | lua_gettable(L, -2); 173 | auto pop_tool_entry = make_guard([&]{ lua_pop(L, 1); }); 174 | if (lua_isnil(L, -1)) return false; 175 | throw_if (!lua_istable(L, -1), "tool entry incorrect"); 176 | 177 | lua_getfield(L, -1, "name"); 178 | if(lua_isstring(L, -1)) 179 | tool.name = lua_tostring(L, -1); 180 | lua_pop(L, 1); 181 | 182 | lua_getfield(L, -1, "length"); 183 | if(lua_isnumber(L, -1)) 184 | tool.length = lua_tonumber(L, -1); 185 | lua_pop(L, 1); 186 | 187 | lua_getfield(L, -1, "diameter"); 188 | if(lua_isnumber(L, -1)) 189 | tool.diameter = lua_tonumber(L, -1); 190 | lua_pop(L, 1); 191 | 192 | lua_getfield(L, -1, "flutes"); 193 | if(lua_isnumber(L, -1)) 194 | tool.flutes = lua_tonumber(L, -1); 195 | lua_pop(L, 1); 196 | 197 | lua_getfield(L, -1, "flute_length"); 198 | if(lua_isnumber(L, -1)) 199 | tool.flute_length = lua_tonumber(L, -1); 200 | lua_pop(L, 1); 201 | 202 | lua_getfield(L, -1, "shank_diameter"); 203 | if(lua_isnumber(L, -1)) 204 | tool.shank_diameter = lua_tonumber(L, -1); 205 | lua_pop(L, 1); 206 | 207 | return true; 208 | } 209 | 210 | bool get_tool(nc_config& config, unsigned id, const std::string& machine, lathe_tool& tool) { 211 | auto& L = config.state(); 212 | get_machine(L, machine); 213 | auto pop_machine_field = make_guard([&]{ lua_pop(L, 1); }); 214 | 215 | return false; 216 | } 217 | 218 | } 219 | -------------------------------------------------------------------------------- /src/base/machine_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * machine_config.h 20 | * 21 | * Created on: 2016-04-30 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef MACHINE_CONFIG_H_ 26 | #define MACHINE_CONFIG_H_ 27 | #include 28 | #include "nc_config.h" 29 | #include 30 | 31 | namespace machine_config { 32 | 33 | boost::program_options::options_description base_options(); 34 | 35 | std::string default_machine(nc_config& config); 36 | 37 | enum class units { 38 | metric, 39 | imperial 40 | }; 41 | units default_units(nc_config& config); 42 | units machine_units(nc_config& config, const std::string& machine); 43 | 44 | enum class machine_type { 45 | mill, 46 | lathe 47 | }; 48 | machine_type get_machine_type(nc_config& config, const std::string& machine); 49 | 50 | struct mill_tool { 51 | std::string name; 52 | double length = 0.0; 53 | double diameter = 0.0; 54 | unsigned flutes = 0; 55 | double flute_length = 0.0; 56 | double shank_diameter = 0.0; 57 | }; 58 | struct lathe_tool { 59 | std::string name; 60 | }; 61 | 62 | bool get_tool(nc_config& config, unsigned id, const std::string& machine, mill_tool& tool); 63 | bool get_tool(nc_config& config, unsigned id, const std::string& machine, lathe_tool& tool); 64 | 65 | } 66 | 67 | #endif /* MACHINE_CONFIG_H_ */ 68 | -------------------------------------------------------------------------------- /src/base/nc_config.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * nc_config.cpp 20 | * 21 | * Created on: 2016-02-09 22 | * Author: nicholas 23 | */ 24 | 25 | #include "nc_config.h" 26 | #include 27 | #include 28 | #include 29 | 30 | namespace fs = boost::filesystem; 31 | 32 | namespace { 33 | 34 | std::string find_config_file(const std::string& conf) { 35 | if(!conf.empty()) 36 | return conf; 37 | 38 | /* Search cwd then parents for nc_tools.conf */ 39 | auto cwd = fs::current_path(); 40 | while(!cwd.empty()) { 41 | auto candidate = cwd / "nc_tools.conf"; 42 | if(exists(candidate)) 43 | return candidate.native(); 44 | cwd = cwd.parent_path(); 45 | } 46 | return {}; 47 | } 48 | 49 | } 50 | 51 | nc_config::nc_config(const std::string& conf) 52 | { 53 | auto config = find_config_file(conf); 54 | try { 55 | if(!config.empty() && luaL_dofile(L, config.c_str())) { 56 | std::string ex = lua_tostring(L, -1); 57 | lua_pop(L, 1); 58 | throw std::runtime_error(ex); 59 | } 60 | } catch(const std::exception& ex) { 61 | std::cerr << ex.what() << "\n"; 62 | if(!conf.empty()) 63 | throw; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/base/nc_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * nc_config.h 20 | * 21 | * Created on: 2016-02-09 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef NC_CONFIG_H_ 26 | #define NC_CONFIG_H_ 27 | #include "lua/state.h" 28 | #include 29 | 30 | struct nc_config 31 | { 32 | protected: 33 | lua::state L; 34 | public: 35 | nc_config(const std::string& conf = {}); 36 | nc_config(nc_config&) = delete; 37 | nc_config& operator=(nc_config&) = delete; 38 | 39 | inline lua::state& state() { return L; } 40 | }; 41 | 42 | #endif /* NC_CONFIG_H_ */ 43 | -------------------------------------------------------------------------------- /src/base/rs274_base.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_base.h 20 | * 21 | * Created on: 2015-05-26 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_BASE_H_ 26 | #define RS274_BASE_H_ 27 | #include "rs274ngc.hh" 28 | #include "cxxcam/Position.h" 29 | #include "cxxcam/Math.h" 30 | #include "lua/state.h" 31 | #include 32 | #include "nc_config.h" 33 | #include 34 | 35 | std::string str(const block_t& block); 36 | 37 | class rs274_base : public rs274ngc 38 | { 39 | public: 40 | rs274_base(boost::program_options::variables_map& vm); 41 | virtual ~rs274_base(); 42 | private: 43 | virtual void interp_init(); 44 | 45 | protected: 46 | mutable nc_config config; 47 | std::string machine_id; 48 | 49 | Plane _active_plane = Plane::XY; 50 | int _active_slot = 1; 51 | double _feed_rate = 0.0; 52 | int _flood = 0; 53 | int _mist = 0; 54 | Units _length_unit_type = Units::Metric; 55 | Motion _motion_mode = Motion::Continuous; 56 | 57 | Position probe_pos; 58 | Position origin_pos; 59 | Position program_pos; 60 | 61 | double _spindle_speed; 62 | Direction _spindle_turning; 63 | double _spindle_theta; 64 | double _traverse_rate; 65 | 66 | cxxcam::Position convert(const Position& p) const; 67 | double spindle_delta_theta(const cxxcam::units::length& motion_length) const; 68 | void apply_spindle_delta(double delta_theta); 69 | private: 70 | 71 | virtual void offset_origin(const Position& pos); 72 | virtual void units(Units u); 73 | virtual void rapid_rate(double rate); 74 | virtual void rapid(const Position& pos); 75 | virtual void _rapid(const Position& pos) =0; 76 | virtual void feed_rate(double rate); 77 | virtual void feed_reference(FeedReference reference); 78 | virtual void motion_mode(Motion mode); 79 | virtual void plane(Plane pl); 80 | virtual void cutter_radius_comp(double radius); 81 | virtual void cutter_radius_comp_start(Side direction); 82 | virtual void cutter_radius_comp_stop(); 83 | virtual void speed_feed_sync_start(); 84 | virtual void speed_feed_sync_stop(); 85 | virtual void arc(double end0, double end1, double axis0, double axis1, int rotation, double end_point, double a, double b, double c); 86 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation) =0; 87 | virtual void linear(const Position& pos); 88 | virtual void _linear(const Position& pos) =0; 89 | virtual void probe(const Position& pos); 90 | virtual void dwell(double seconds); 91 | virtual void spindle_start_clockwise(); 92 | virtual void spindle_start_counterclockwise(); 93 | virtual void spindle_speed(double r); 94 | virtual void spindle_stop(); 95 | virtual void spindle_orient(double orientation, Direction direction); 96 | virtual void tool_length_offset(double length); 97 | virtual void tool_change(int slot); 98 | virtual void tool_select(int i); 99 | virtual void axis_clamp(Axis axis); 100 | virtual void comment(const char *s); 101 | virtual void feed_override_disable(); 102 | virtual void speed_override_disable(); 103 | virtual void feed_override_enable(); 104 | virtual void speed_override_enable(); 105 | virtual void coolant_flood_off(); 106 | virtual void coolant_flood_on(); 107 | virtual void message(const char *s); 108 | virtual void coolant_mist_off(); 109 | virtual void coolant_mist_on(); 110 | virtual void pallet_shuttle(); 111 | virtual void probe_off(); 112 | virtual void probe_on(); 113 | virtual void axis_unclamp(Axis axis); 114 | virtual void program_stop(); 115 | virtual void program_optional_stop(); 116 | virtual void program_end(); 117 | virtual double feed_rate() const; 118 | virtual bool coolant_flood() const; 119 | virtual Units units() const; 120 | virtual bool coolant_mist() const; 121 | virtual Motion motion_mode() const; 122 | virtual void get_parameter_filename(char* filename, size_t max_size) const; 123 | virtual Plane plane() const; 124 | virtual Position current_position() const; 125 | virtual Position probe_position() const; 126 | virtual double probe_value() const; 127 | virtual bool queue_empty() const; 128 | virtual double spindle_speed() const; 129 | virtual Direction spindle_state() const; 130 | virtual void spindle_mode(double); 131 | virtual double spindle_mode() const; 132 | virtual int tool_slot() const; 133 | virtual unsigned int tool_max() const; 134 | virtual Tool tool(int pocket) const; 135 | virtual double rapid_rate() const; 136 | virtual void block_end(const block_t& block); 137 | }; 138 | 139 | #endif /* RS274_BASE_H_ */ 140 | -------------------------------------------------------------------------------- /src/fold_adjacent.h: -------------------------------------------------------------------------------- 1 | /* cxxcam - C++ CAD/CAM driver library. 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * fold_adjacent.h 20 | * 21 | * Created on: 2013-07-19 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef FOLD_ADJACENT_H_ 26 | #define FOLD_ADJACENT_H_ 27 | #include 28 | 29 | template 30 | OutputIt fold_adjacent(InputIt first, InputIt last, OutputIt d_first, BinaryOperation op) 31 | { 32 | if (first == last) 33 | return d_first; 34 | 35 | auto acc = *first; 36 | while (++first != last) 37 | { 38 | auto val = *first; 39 | *++d_first = op(acc, val); 40 | acc = std::move(val); 41 | } 42 | return ++d_first; 43 | } 44 | 45 | #endif /* FOLD_ADJACENT_H_ */ 46 | -------------------------------------------------------------------------------- /src/lua/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Lua REQUIRED) 7 | 8 | include_directories( 9 | ${LUA_INCLUDE_DIR} 10 | ) 11 | 12 | add_library(nc_lua STATIC luaconfig.cpp state.cpp) 13 | target_link_libraries(nc_lua 14 | ${LUA_LIBRARIES} 15 | ) 16 | -------------------------------------------------------------------------------- /src/lua/luaconfig.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * luaconfig.cpp 20 | * 21 | * Created on: 2015-06-02 22 | * Author: nicholas 23 | */ 24 | 25 | #include "luaconfig.h" 26 | #include 27 | #include 28 | 29 | namespace lua { 30 | 31 | config::config() { 32 | } 33 | 34 | bool config::get(const char* table, const char* field, std::string& value) { 35 | lua_getglobal(L, table); 36 | lua_getfield(L, -1, field); 37 | if (lua_isnil(L, -1)) { 38 | lua_pop(L, 2); 39 | return false; 40 | } 41 | if (!lua_isstring(L, -1)) { 42 | lua_pop(L, 2); 43 | throw std::runtime_error("Expected string value"); 44 | } 45 | value = lua_tostring(L, -1); 46 | lua_pop(L, 2); 47 | return true; 48 | } 49 | 50 | config::~config() { 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/lua/luaconfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * luaconfig.h 20 | * 21 | * Created on: 2015-06-02 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef LUACONFIG_H_ 26 | #define LUACONFIG_H_ 27 | #include 28 | #include "state.h" 29 | 30 | namespace lua { 31 | 32 | /* Add functions to read value from lua table 33 | * at creation creates a lua state and runs config file 34 | * allows querying of configuration tables as 35 | * OPTIONAL table name, followed by value as type. 36 | * */ 37 | class config { 38 | private: 39 | state L; 40 | public: 41 | config(); 42 | bool get(const char* table, const char* field, std::string& value); 43 | ~config(); 44 | }; 45 | 46 | } 47 | 48 | #endif /* LUACONFIG_H_ */ 49 | -------------------------------------------------------------------------------- /src/lua/state.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * state.cpp 20 | * 21 | * Created on: 2015-06-03 22 | * Author: nicholas 23 | */ 24 | 25 | #include "state.h" 26 | #include 27 | #include 28 | #include "../throw_if.h" 29 | 30 | namespace lua { 31 | 32 | state::state() 33 | : L(luaL_newstate()) { 34 | throw_if(!L, "Unable to create lua state"); 35 | } 36 | state::state(state&& s) 37 | : L(nullptr) { 38 | using std::swap; 39 | swap(L, s.L); 40 | } 41 | state& state::operator=(state&& s) { 42 | using std::swap; 43 | swap(L, s.L); 44 | return *this; 45 | } 46 | state::operator lua_State*() { 47 | return L; 48 | } 49 | state::~state() { 50 | if(L) lua_close(L); 51 | } 52 | 53 | size_t table_size(state& L) { 54 | size_t count = 0; 55 | 56 | auto t = lua_gettop(L); 57 | lua_pushnil(L); 58 | while(lua_next(L, t)) { 59 | ++count; 60 | lua_pop(L, 1); 61 | } 62 | lua_pop(L, 1); 63 | return count; 64 | 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/lua/state.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * state.h 20 | * 21 | * Created on: 2015-06-03 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef LUASTATE_H_ 26 | #define LUASTATE_H_ 27 | #include 28 | #include 29 | 30 | namespace lua { 31 | 32 | struct state { 33 | lua_State* L; 34 | state(); 35 | state(const state&) = delete; 36 | state& operator=(const state&) = delete; 37 | state(state&& s); 38 | state& operator=(state&&); 39 | operator lua_State*(); 40 | ~state(); 41 | }; 42 | 43 | size_t table_size(state& L); 44 | 45 | } 46 | 47 | #endif /* LUASTATE_H_ */ 48 | -------------------------------------------------------------------------------- /src/nc_annotate/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Threads REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 8 | FIND_PACKAGE(Lua REQUIRED) 9 | 10 | include_directories( 11 | ${Boost_INCLUDE_DIRS} 12 | ${LUA_INCLUDE_DIR} 13 | ${PROJECT_SOURCE_DIR}/src 14 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 15 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 16 | ${PROJECT_SOURCE_DIR}/deps/geom/include 17 | ${PROJECT_SOURCE_DIR}/deps/clipper 18 | ${PROJECT_SOURCE_DIR}/deps/circle3d 19 | ) 20 | 21 | add_executable(nc_annotate_curvature annotate_curvature.cpp rs274_annotate.cpp ../print_exception.cpp) 22 | target_link_libraries(nc_annotate_curvature 23 | ${CMAKE_THREAD_LIBS_INIT} 24 | ${Boost_LIBRARIES} 25 | ${LUA_LIBRARIES} 26 | rs274ngc 27 | nc_base 28 | circle3d 29 | ) 30 | 31 | add_executable(nc_annotate_engagement annotate_engagement.cpp rs274_annotate.cpp ../print_exception.cpp) 32 | target_link_libraries(nc_annotate_engagement 33 | ${CMAKE_THREAD_LIBS_INIT} 34 | ${Boost_LIBRARIES} 35 | ${LUA_LIBRARIES} 36 | rs274ngc 37 | nc_base 38 | geom 39 | circle3d 40 | polyclipping 41 | ) 42 | -------------------------------------------------------------------------------- /src/nc_annotate/annotate_curvature.cpp: -------------------------------------------------------------------------------- 1 | #include "rs274_annotate.h" 2 | #include "rs274ngc_return.hh" 3 | #include 4 | #include "print_exception.h" 5 | #include "base/machine_config.h" 6 | #include "circle3d.hpp" 7 | #include "../r6.h" 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | namespace po = boost::program_options; 15 | 16 | class rs274_annotate_curvature : public rs274_annotate 17 | { 18 | private: 19 | void process_point(const cxxcam::math::point_3& p, bool rapid) 20 | { 21 | using cxxcam::units::length_mm; 22 | 23 | if (rapid) { 24 | // TODO flush annotations? 25 | points.clear(); 26 | return; 27 | } 28 | 29 | points.push_back(p); 30 | 31 | if (points.size() >= 3) { 32 | 33 | if (points.size() > 3) 34 | points.erase(begin(points)); 35 | 36 | circle3d::Point3D p1 = {length_mm(points[0].x).value(), length_mm(points[0].y).value(), length_mm(points[0].z).value()}; 37 | circle3d::Point3D p2 = {length_mm(points[1].x).value(), length_mm(points[1].y).value(), length_mm(points[1].z).value()}; 38 | circle3d::Point3D p3 = {length_mm(points[2].x).value(), length_mm(points[2].y).value(), length_mm(points[2].z).value()}; 39 | circle3d::Point3D c; 40 | double radius; 41 | circle3d::estimate3DCircle(p1, p2, p3, c, radius); 42 | 43 | double curve = 1/radius; 44 | 45 | if (isnan(curve) == false) { 46 | std::ostringstream ss; 47 | ss << "R" << r6(curve); // Radius of curvature 48 | push_annotation(ss.str()); 49 | } 50 | } 51 | } 52 | 53 | std::vector points; 54 | 55 | public: 56 | rs274_annotate_curvature(boost::program_options::variables_map& vm) 57 | : rs274_annotate(vm) {} 58 | virtual ~rs274_annotate_curvature() = default; 59 | }; 60 | 61 | int main(int argc, char* argv[]) { 62 | po::options_description options("nc_annotate_curvature"); 63 | std::vector args(argv, argv + argc); 64 | args.erase(begin(args)); 65 | 66 | options.add(machine_config::base_options()); 67 | options.add_options() 68 | ("help,h", "display this help and exit") 69 | ; 70 | 71 | try { 72 | po::variables_map vm; 73 | store(po::command_line_parser(args).options(options).run(), vm); 74 | 75 | if(vm.count("help")) { 76 | std::cout << options << "\n"; 77 | return 0; 78 | } 79 | notify(vm); 80 | 81 | rs274_annotate_curvature annotate(vm); 82 | 83 | std::string line; 84 | while(std::getline(std::cin, line)) { 85 | int status; 86 | 87 | status = annotate.read(line.c_str()); 88 | if(status != RS274NGC_OK) { 89 | if(status != RS274NGC_EXECUTE_FINISH) { 90 | std::cerr << "Error reading line!: \n"; 91 | return status; 92 | } 93 | } 94 | 95 | status = annotate.execute(); 96 | if(status != RS274NGC_OK) 97 | return status; 98 | } 99 | } catch(const po::error& e) { 100 | print_exception(e); 101 | std::cout << options << "\n"; 102 | return 1; 103 | } catch(const std::exception& e) { 104 | print_exception(e); 105 | return 1; 106 | } 107 | 108 | return 0; 109 | } 110 | -------------------------------------------------------------------------------- /src/nc_annotate/rs274_annotate.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_annotate.cpp 20 | * 21 | * Created on: 2019-05-15 22 | * Author: nicholas 23 | */ 24 | 25 | #include "rs274_annotate.h" 26 | #include 27 | #include 28 | #include "cxxcam/Path.h" 29 | #include 30 | 31 | void rs274_annotate::_rapid(const Position& pos) { 32 | using namespace cxxcam::path; 33 | 34 | auto steps = expand_linear(convert(program_pos), convert(pos), {}, 0).path; 35 | for (auto& step : steps) { 36 | auto& p = step.position; 37 | process_point(p, true); 38 | } 39 | } 40 | 41 | void rs274_annotate::_arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation) { 42 | using namespace cxxcam::path; 43 | 44 | auto steps = expand_arc(convert(program_pos), convert(end), convert(center), (rotation < 0 ? ArcDirection::Clockwise : ArcDirection::CounterClockwise), plane, std::abs(rotation), {}, 1).path; 45 | for (auto& step : steps) { 46 | auto& p = step.position; 47 | process_point(p, false); 48 | } 49 | } 50 | 51 | void rs274_annotate::_linear(const Position& pos) { 52 | using namespace cxxcam::path; 53 | 54 | auto steps = expand_linear(convert(program_pos), convert(pos), {}, 0).path; 55 | for (auto& step : steps) { 56 | auto& p = step.position; 57 | process_point(p, false); 58 | } 59 | } 60 | 61 | void rs274_annotate::block_end(const block_t& block) { 62 | std::cout << str(block) << "\n"; 63 | for (auto& anno : annotations_) 64 | std::cout << " (" << anno << ")\n"; 65 | 66 | annotations_.clear(); 67 | } 68 | 69 | void rs274_annotate::push_annotation(const std::string& annotation) { 70 | annotations_.push_back(annotation); 71 | } 72 | 73 | rs274_annotate::rs274_annotate(boost::program_options::variables_map& vm) 74 | : rs274_base(vm) { 75 | } 76 | 77 | -------------------------------------------------------------------------------- /src/nc_annotate/rs274_annotate.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_annotate.h 20 | * 21 | * Created on: 2019-05-15 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_ANNOTATE_H_ 26 | #define RS274_ANNOTATE_H_ 27 | #include "base/rs274_base.h" 28 | #include 29 | #include 30 | 31 | class rs274_annotate : public rs274_base 32 | { 33 | private: 34 | virtual void _rapid(const Position& pos); 35 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation); 36 | virtual void _linear(const Position& pos); 37 | virtual void block_end(const block_t& block); 38 | 39 | std::vector annotations_; 40 | 41 | protected: 42 | virtual void process_point(const cxxcam::math::point_3& p, bool rapid) =0; 43 | void push_annotation(const std::string& annotation); 44 | 45 | public: 46 | rs274_annotate(boost::program_options::variables_map& vm); 47 | virtual ~rs274_annotate() = default; 48 | }; 49 | 50 | #endif /* RS274_SHORTLINES_H_ */ 51 | -------------------------------------------------------------------------------- /src/nc_arcfit/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Threads REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 8 | FIND_PACKAGE(Lua REQUIRED) 9 | 10 | include_directories( 11 | ${Boost_INCLUDE_DIRS} 12 | ${LUA_INCLUDE_DIR} 13 | ${PROJECT_SOURCE_DIR}/src 14 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 15 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 16 | ) 17 | 18 | add_executable(nc_arcfit arcfit.cpp rs274_arcfit.cpp geometry_3.cpp ../print_exception.cpp) 19 | target_link_libraries(nc_arcfit 20 | ${CMAKE_THREAD_LIBS_INIT} 21 | ${Boost_LIBRARIES} 22 | ${LUA_LIBRARIES} 23 | rs274ngc 24 | nc_base 25 | cxxcam 26 | ) 27 | -------------------------------------------------------------------------------- /src/nc_arcfit/arcfit.cpp: -------------------------------------------------------------------------------- 1 | #include "rs274_arcfit.h" 2 | #include "rs274ngc_return.hh" 3 | #include 4 | #include "print_exception.h" 5 | #include "../throw_if.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include "base/machine_config.h" 11 | 12 | namespace po = boost::program_options; 13 | 14 | int main(int argc, char* argv[]) { 15 | po::options_description options("nc_arcfit"); 16 | std::vector args(argv, argv + argc); 17 | args.erase(begin(args)); 18 | 19 | /* TODO values used as defaults should have explicit units. 20 | * values passed as parameters should use named units 21 | * */ 22 | options.add(machine_config::base_options()); 23 | options.add_options() 24 | ("help,h", "display this help and exit") 25 | ("chord_height,c", po::value()->default_value(0.1), "Chord height tolerance") 26 | ("radius_dev,r", po::value()->default_value(0.1), "Radius deviation tolerance") 27 | ("planar_dev,p", po::value()->default_value(1e-6), "Planar deviation tolerance") 28 | ("theta_min,t", po::value()->default_value(3.14/16.0), "Minimum arc theta") 29 | ; 30 | 31 | try { 32 | po::variables_map vm; 33 | store(po::command_line_parser(args).options(options).run(), vm); 34 | 35 | if(vm.count("help")) { 36 | std::cout << options << "\n"; 37 | return 0; 38 | } 39 | notify(vm); 40 | 41 | double chord_height_tolerance = vm["chord_height"].as(); 42 | double point_deviation = vm["radius_dev"].as(); 43 | double planar_tolerance = vm["planar_dev"].as(); 44 | double theta_minimum = vm["theta_min"].as(); 45 | 46 | rs274_arcfit arcfit(vm, chord_height_tolerance, point_deviation, planar_tolerance, theta_minimum); 47 | 48 | std::string line; 49 | while(std::getline(std::cin, line)) { 50 | int status; 51 | 52 | status = arcfit.read(line.c_str()); 53 | if(status != RS274NGC_OK) { 54 | if(status != RS274NGC_EXECUTE_FINISH) { 55 | std::cerr << "Error reading line!: \n"; 56 | std::cout << line <<"\n"; 57 | return status; 58 | } 59 | } 60 | 61 | status = arcfit.execute(); 62 | if(status != RS274NGC_OK) 63 | return status; 64 | } 65 | 66 | if (arcfit.read("M2") == RS274NGC_OK) 67 | arcfit.execute(); 68 | 69 | } catch(const po::error& e) { 70 | print_exception(e); 71 | std::cout << options << "\n"; 72 | return 1; 73 | } catch(const std::exception& e) { 74 | print_exception(e); 75 | return 1; 76 | } 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /src/nc_arcfit/geometry_3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * geometry_3.cpp 20 | * 21 | * Created on: 2016-04-11 22 | * Author: nicholas 23 | */ 24 | 25 | #include "geometry_3.h" 26 | #include 27 | 28 | namespace geometry_3 { 29 | 30 | vector_3 point_3::operator-(const point_3& p) const { 31 | return {x-p.x, y-p.y, z-p.z}; 32 | } 33 | 34 | double distance(const point_3& p0, const point_3& p1) { 35 | return std::sqrt((p0.x-p1.x)*(p0.x-p1.x) + (p0.y-p1.y)*(p0.y-p1.y) + (p0.z-p1.z)*(p0.z-p1.z)); 36 | } 37 | 38 | vector_3 vector_3::operator-(const vector_3& v) const { 39 | return {x - v.x, y - v.y, z - v.z}; 40 | } 41 | 42 | vector_3 vector_3::operator+(const vector_3& v) const { 43 | return {x - v.x, y - v.y, z - v.z}; 44 | } 45 | 46 | vector_3 cross(const vector_3& a, const vector_3& b) { 47 | return {(a.y*b.z) - (a.z*b.y), (a.z*b.x) - (a.x*b.z), (a.x*b.y) - (a.y*b.x)}; 48 | } 49 | 50 | vector_3 normalise(const vector_3& v) { 51 | auto scale = std::sqrt((v.x*v.x) + (v.y*v.y) + (v.z*v.z)); 52 | if(scale == 0.0) 53 | return {0, 0, 0}; 54 | 55 | return {v.x/scale, v.y/scale, v.z/scale}; 56 | } 57 | 58 | bool collinear(const point_3& a, const point_3& b, const point_3& c, double tolerance) { 59 | auto AB = std::abs(distance(a, b)); 60 | auto AC = std::abs(distance(a, c)); 61 | auto BC = std::abs(distance(b, c)); 62 | return std::abs((AB+BC) - AC) < tolerance; 63 | } 64 | 65 | vector_3 plane(const point_3& a, const point_3& b, const point_3& c) { 66 | auto ab = b-a; 67 | auto ac = c-a; 68 | return normalise(cross(ab, ac)); 69 | } 70 | 71 | double chord_height(const point_3& a, const point_3& b, double r) { 72 | auto l = std::abs(distance(a, b)) / 2.0; 73 | return r - std::sqrt((r*r) - (l*l)); 74 | } 75 | 76 | /* assumes arc is described in the xy plane; when calculating other planes, map to XY 77 | * */ 78 | boost::optional circle_center(point_3 p0, point_3 p1, point_3 p2) { 79 | if (collinear(p0, p1, p2)) return {}; 80 | point_3 center; 81 | 82 | auto perpendicular = [](const point_3& p0, const point_3& p1, double tolerance = 1e-9) { 83 | auto a = p1 - p0; 84 | return std::abs(a.x) < tolerance || std::abs(a.y) < tolerance; 85 | }; 86 | 87 | if(perpendicular(p0, p1)) 88 | std::swap(p1, p2); 89 | 90 | auto a = p1 - p0; 91 | auto b = p2 - p1; 92 | 93 | // check if a.x == 0 or b.x == 0 94 | auto ma = a.y/a.x; 95 | auto mb = b.y/b.x; 96 | 97 | center.x = (ma*mb*(p0.y - p2.y) + mb*(p0.x + p1.x) - ma*(p1.x + p2.x)) / (2*(mb-ma)); 98 | center.y = (-1.0/ma) * (center.x - ((p0.x+p1.x)/2.0)) + ((p0.y + p1.y)/2.0); 99 | center.z = p0.z; 100 | return center; 101 | } 102 | 103 | } 104 | 105 | -------------------------------------------------------------------------------- /src/nc_arcfit/geometry_3.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * geometry_3.h 20 | * 21 | * Created on: 2016-04-11 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef GEOMETRY_3_H_ 26 | #define GEOMETRY_3_H_ 27 | #include 28 | #include 29 | #include 30 | 31 | namespace geometry_3 { 32 | 33 | struct point_3; 34 | struct vector_3; 35 | 36 | struct point_3 { 37 | double x; 38 | double y; 39 | double z; 40 | 41 | vector_3 operator-(const point_3& p) const; 42 | }; 43 | 44 | struct line_3 { 45 | point_3 a; 46 | point_3 b; 47 | }; 48 | 49 | struct vector_3 { 50 | double x; 51 | double y; 52 | double z; 53 | 54 | vector_3 operator-(const vector_3& v) const; 55 | vector_3 operator+(const vector_3& v) const; 56 | }; 57 | 58 | double distance(const point_3& p0, const point_3& p1); 59 | 60 | // NOTE: points must represent an ordered polyline 61 | bool collinear(const point_3& a, const point_3& b, const point_3& c, double tolerance = 1e-9); 62 | 63 | vector_3 cross(const vector_3& a, const vector_3& b); 64 | vector_3 normalise(const vector_3& v); 65 | 66 | vector_3 plane(const point_3& a, const point_3& b, const point_3& c); 67 | 68 | double chord_height(const point_3& a, const point_3& b, double r); 69 | 70 | boost::optional circle_center(point_3 p0, point_3 p1, point_3 p2); 71 | 72 | inline std::ostream& operator<<(std::ostream& os, const point_3& p) { 73 | os << "{" << p.x << "," << p.y << "," << p.z << "}"; 74 | return os; 75 | } 76 | 77 | inline double theta(const geometry_3::point_3& p, const geometry_3::point_3& center) { 78 | auto t = std::atan((p.y - center.y) / (p.x - center.x)); 79 | if (t < 0) t += 6.28318530718; 80 | return t; 81 | }; 82 | 83 | } 84 | 85 | #endif /* GEOMETRY_3_H_ */ 86 | -------------------------------------------------------------------------------- /src/nc_arcfit/rs274_arcfit.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_arcfit.h 20 | * 21 | * Created on: 2016-04-11 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_ARCFIT_H_ 26 | #define RS274_ARCFIT_H_ 27 | #include "base/rs274_base.h" 28 | #include "geometry_3.h" 29 | #include 30 | #include 31 | 32 | class rs274_arcfit : public rs274_base 33 | { 34 | private: 35 | struct block_point { 36 | block_t block; 37 | geometry_3::point_3 p0; 38 | geometry_3::point_3 p; 39 | }; 40 | boost::optional point_; 41 | geometry_3::point_3 to_point_3(const cxxcam::Position& pos); 42 | 43 | // state machine 44 | enum class State { 45 | indeterminate, 46 | collecting_points 47 | } state_; 48 | struct Arc { 49 | std::vector points; 50 | geometry_3::vector_3 plane; 51 | double r; 52 | geometry_3::point_3 center; 53 | int dir; 54 | double arc_theta; 55 | 56 | enum { 57 | radiusDelta, 58 | chordHeight 59 | } minimise = radiusDelta; 60 | } arc_; 61 | double chord_height_tolerance; 62 | double point_deviation; 63 | double planar_tolerance; 64 | double theta_minimum; 65 | 66 | void reset(); 67 | void push(const block_point& point); 68 | void flush(bool all = false); 69 | 70 | virtual void _rapid(const Position& pos); 71 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation); 72 | virtual void _linear(const Position& pos); 73 | virtual void block_end(const block_t& block); 74 | virtual void program_end(); 75 | 76 | public: 77 | rs274_arcfit(boost::program_options::variables_map& vm, double chord_height_tolerance, double point_deviation, double planar_tolerance, double theta_minimum); 78 | 79 | virtual ~rs274_arcfit() = default; 80 | }; 81 | 82 | #endif /* RS274_ARCFIT_H_ */ 83 | -------------------------------------------------------------------------------- /src/nc_backplot/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(SFML 2 COMPONENTS system window graphics audio REQUIRED) 7 | FIND_PACKAGE(OpenGL REQUIRED) 8 | FIND_PACKAGE(OpenSceneGraph 3.2.0 REQUIRED osgViewer osgGA osgUtil) 9 | FIND_PACKAGE(Threads REQUIRED) 10 | FIND_PACKAGE(Lua REQUIRED) 11 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 12 | 13 | include_directories( 14 | ${Boost_INCLUDE_DIRS} 15 | ${SFML_INCLUDE_DIR} 16 | ${OPENGL_INCLUDE_DIR} 17 | ${OPENSCENEGRAPH_INCLUDE_DIRS} 18 | ${LUA_INCLUDE_DIR} 19 | ${PROJECT_SOURCE_DIR}/src 20 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 21 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 22 | ${PROJECT_SOURCE_DIR}/deps/geom/include 23 | ) 24 | 25 | add_executable(nc_backplot backplot.cpp rs274_backplot.cpp ../print_exception.cpp) 26 | target_link_libraries(nc_backplot 27 | ${Boost_LIBRARIES} 28 | ${SFML_LIBRARIES} 29 | ${OPENGL_LIBRARIES} 30 | ${OPENSCENEGRAPH_LIBRARIES} 31 | ${CMAKE_THREAD_LIBS_INIT} 32 | ${LUA_LIBRARIES} 33 | rs274ngc 34 | cxxcam 35 | geom 36 | nc_base 37 | ) 38 | -------------------------------------------------------------------------------- /src/nc_backplot/rs274_backplot.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_backplot.cpp 20 | * 21 | * Created on: 2014-12-31 22 | * Author: nicholas 23 | */ 24 | 25 | #include "rs274_backplot.h" 26 | #include 27 | #include 28 | #include 29 | #include "base/machine_config.h" 30 | 31 | void rs274_backplot::pushBackplot(osg::Geode* geode, const std::vector& steps, bool cut) { 32 | auto geom = new osg::Geometry(); 33 | auto units = machine_config::machine_units(config, machine_id); 34 | 35 | auto vertices = new osg::Vec3Array; 36 | vertices->reserve(steps.size()); 37 | for(auto& step : steps) 38 | { 39 | using cxxcam::units::length_mm; 40 | using cxxcam::units::length_inch; 41 | auto& p = step.position; 42 | 43 | switch (units) { 44 | case machine_config::units::metric: 45 | vertices->push_back({static_cast(length_mm{p.x}.value()), static_cast(length_mm{p.y}.value()), static_cast(length_mm{p.z}.value())}); 46 | break; 47 | case machine_config::units::imperial: 48 | vertices->push_back({static_cast(length_inch{p.x}.value()), static_cast(length_inch{p.y}.value()), static_cast(length_inch{p.z}.value())}); 49 | break; 50 | default: 51 | throw std::logic_error("Unhandled units"); 52 | } 53 | } 54 | geom->setVertexArray(vertices); 55 | 56 | auto colors = new osg::Vec4Array; 57 | if(cut) 58 | colors->push_back({0.0f,1.0f,0.0f,1.0f}); 59 | else 60 | colors->push_back({1.0f,0.0f,0.0f,1.0f}); 61 | geom->setColorArray(colors, osg::Array::BIND_OVERALL); 62 | 63 | auto normals = new osg::Vec3Array; 64 | normals->push_back({0.0f,0.0f,1.0f}); 65 | geom->setNormalArray(normals, osg::Array::BIND_OVERALL); 66 | 67 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,0,vertices->size())); 68 | 69 | geode->addDrawable(geom); 70 | } 71 | 72 | void rs274_backplot::_rapid(const Position& pos) 73 | { 74 | auto steps = cxxcam::path::expand_linear(convert(program_pos), convert(pos), {}, -1).path; 75 | pushBackplot(geode, steps, false); 76 | } 77 | 78 | void rs274_backplot::_arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation) 79 | { 80 | using namespace cxxcam::path; 81 | auto steps = expand_arc(convert(program_pos), convert(end), convert(center), (rotation < 0 ? ArcDirection::Clockwise : ArcDirection::CounterClockwise), plane, std::abs(rotation), {}).path; 82 | pushBackplot(geode, steps, true); 83 | } 84 | 85 | void rs274_backplot::_linear(const Position& pos) 86 | { 87 | auto steps = cxxcam::path::expand_linear(convert(program_pos), convert(pos), {}, -1).path; 88 | pushBackplot(geode, steps, true); 89 | } 90 | 91 | rs274_backplot::rs274_backplot(boost::program_options::variables_map& vm, osg::Group* parent) 92 | : rs274_base(vm), geode(new osg::Geode) 93 | { 94 | parent->addChild(geode); 95 | } 96 | -------------------------------------------------------------------------------- /src/nc_backplot/rs274_backplot.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_backplot.h 20 | * 21 | * Created on: 2014-12-31 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_BACKPLOT_H_ 26 | #define RS274_BACKPLOT_H_ 27 | #include "base/rs274_base.h" 28 | #include 29 | #include "cxxcam/Position.h" 30 | #include "cxxcam/Path.h" 31 | 32 | class rs274_backplot : public rs274_base 33 | { 34 | private: 35 | osg::Geode* geode; 36 | void pushBackplot(osg::Geode* geode, const std::vector& steps, bool cut); 37 | 38 | virtual void _rapid(const Position& pos); 39 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation); 40 | virtual void _linear(const Position& pos); 41 | 42 | public: 43 | rs274_backplot(boost::program_options::variables_map& vm, osg::Group* parent); 44 | virtual ~rs274_backplot() = default; 45 | }; 46 | 47 | #endif /* RS274_BACKPLOT_H_ */ 48 | -------------------------------------------------------------------------------- /src/nc_bounds/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Threads REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 8 | FIND_PACKAGE(Lua REQUIRED) 9 | 10 | include_directories( 11 | ${Boost_INCLUDE_DIRS} 12 | ${LUA_INCLUDE_DIR} 13 | ${PROJECT_SOURCE_DIR}/src 14 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 15 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 16 | ${PROJECT_SOURCE_DIR}/deps/geom/include 17 | ) 18 | 19 | add_executable(nc_bounds bounds.cpp rs274_bounds.cpp ../print_exception.cpp) 20 | target_link_libraries(nc_bounds 21 | ${CMAKE_THREAD_LIBS_INIT} 22 | ${Boost_LIBRARIES} 23 | ${LUA_LIBRARIES} 24 | rs274ngc 25 | nc_base 26 | geom 27 | ) 28 | -------------------------------------------------------------------------------- /src/nc_bounds/bounds.cpp: -------------------------------------------------------------------------------- 1 | #include "rs274_bounds.h" 2 | #include "rs274ngc_return.hh" 3 | #include 4 | #include "print_exception.h" 5 | #include "geom/polyhedron.h" 6 | #include "geom/query.h" 7 | #include "../throw_if.h" 8 | #include "../r6.h" 9 | #include "base/machine_config.h" 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | namespace po = boost::program_options; 17 | 18 | std::ostream& operator<<(std::ostream& os, const geom::query::bbox_3& b) { 19 | os << "min: {" << r6(b.min.x) << ", " << r6(b.min.y) << ", " << r6(b.min.z) <<"} max: {" << r6(b.max.x) << ", " << r6(b.max.y) << ", " << r6(b.max.z) <<"}"; 20 | return os; 21 | } 22 | 23 | int main(int argc, char* argv[]) { 24 | po::options_description options("nc_bounds"); 25 | std::vector args(argv, argv + argc); 26 | args.erase(begin(args)); 27 | 28 | options.add(machine_config::base_options()); 29 | options.add_options() 30 | ("help,h", "display this help and exit") 31 | ("cut,c", "track cuts in gcode") 32 | ("rapid,r", "track rapids in gcode") 33 | ("model,m", "calculate bounding box of model") 34 | ; 35 | 36 | try { 37 | po::variables_map vm; 38 | store(po::command_line_parser(args).options(options).run(), vm); 39 | 40 | if(vm.count("help")) { 41 | std::cout << options << "\n"; 42 | return 0; 43 | } 44 | notify(vm); 45 | 46 | if (vm.count("model")) { 47 | geom::polyhedron_t model; 48 | throw_if(!(std::cin >> geom::format::off >> model), "Unable to read model from file"); 49 | 50 | auto bounding_box = geom::bounding_box(model); 51 | std::cout << bounding_box << "\n"; 52 | 53 | } else { 54 | bool cut = vm.count("cut"); 55 | bool rapid = vm.count("rapid"); 56 | if(! (cut || rapid)) 57 | cut = true; 58 | rs274_bounds bounding_box(vm, cut, rapid); 59 | 60 | std::string line; 61 | while(std::getline(std::cin, line)) { 62 | int status; 63 | 64 | status = bounding_box.read(line.c_str()); 65 | if(status != RS274NGC_OK) { 66 | if(status != RS274NGC_EXECUTE_FINISH) { 67 | std::cerr << "Error reading line!: \n"; 68 | std::cout << line <<"\n"; 69 | return status; 70 | } 71 | } 72 | 73 | status = bounding_box.execute(); 74 | if(status != RS274NGC_OK) 75 | return status; 76 | } 77 | 78 | std::cout << bounding_box.bounding_box() << "\n"; 79 | } 80 | } catch(const po::error& e) { 81 | print_exception(e); 82 | std::cout << options << "\n"; 83 | return 1; 84 | } catch(const std::exception& e) { 85 | print_exception(e); 86 | return 1; 87 | } 88 | 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /src/nc_bounds/rs274_bounds.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_bounds.cpp 20 | * 21 | * Created on: 2015-07-24 22 | * Author: nicholas 23 | */ 24 | 25 | #include "rs274_bounds.h" 26 | #include 27 | #include 28 | #include "throw_if.h" 29 | #include "fold_adjacent.h" 30 | #include 31 | #include "cxxcam/Path.h" 32 | 33 | cxxcam::math::point_3 pos2point(const cxxcam::Position& pos) { 34 | return {pos.X, pos.Y, pos.Z}; 35 | } 36 | 37 | // TODO fix bbox calc = init to first point 38 | void rs274_bounds::_rapid(const Position& pos) { 39 | if (track_rapid) { 40 | if (!first_point) { 41 | bbox.min = bbox.max = pos2point(convert(program_pos)); 42 | first_point = true; 43 | } 44 | bbox += pos2point(convert(program_pos)); 45 | bbox += pos2point(convert(pos)); 46 | } 47 | } 48 | 49 | void rs274_bounds::_arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation) { 50 | using namespace cxxcam::path; 51 | if (track_cut) { 52 | auto steps = expand_arc(convert(program_pos), convert(end), convert(center), (rotation < 0 ? ArcDirection::Clockwise : ArcDirection::CounterClockwise), plane, std::abs(rotation), {}).path; 53 | 54 | for (auto& step : steps) { 55 | if (!first_point) { 56 | bbox.min = bbox.max = step.position; 57 | first_point = true; 58 | } 59 | bbox += step.position; 60 | } 61 | } 62 | } 63 | 64 | 65 | void rs274_bounds::_linear(const Position& pos) { 66 | using namespace cxxcam::path; 67 | if (track_cut) { 68 | auto steps = expand_linear(convert(program_pos), convert(pos), {}, -1).path; 69 | 70 | for (auto& step : steps) { 71 | if (!first_point) { 72 | bbox.min = bbox.max = step.position; 73 | first_point = true; 74 | } 75 | bbox += step.position; 76 | } 77 | } 78 | } 79 | 80 | rs274_bounds::rs274_bounds(boost::program_options::variables_map& vm, bool cut, bool rapid) 81 | : rs274_base(vm), first_point(false), track_cut(cut), track_rapid(rapid) { 82 | } 83 | 84 | cxxcam::Bbox rs274_bounds::bounding_box() const { 85 | return bbox; 86 | } 87 | -------------------------------------------------------------------------------- /src/nc_bounds/rs274_bounds.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_bounds.h 20 | * 21 | * Created on: 2015-07-24 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_BOUNDS_H_ 26 | #define RS274_BOUNDS_H_ 27 | #include "base/rs274_base.h" 28 | #include "cxxcam/Bbox.h" 29 | 30 | class rs274_bounds : public rs274_base 31 | { 32 | private: 33 | bool first_point; 34 | cxxcam::Bbox bbox; 35 | bool track_cut; 36 | bool track_rapid; 37 | 38 | virtual void _rapid(const Position& pos); 39 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation); 40 | virtual void _linear(const Position& pos); 41 | 42 | public: 43 | rs274_bounds(boost::program_options::variables_map& vm, bool cut, bool rapid); 44 | 45 | cxxcam::Bbox bounding_box() const; 46 | 47 | virtual ~rs274_bounds() = default; 48 | }; 49 | 50 | #endif /* RS274_BOUNDS_H_ */ 51 | -------------------------------------------------------------------------------- /src/nc_contour_pocket/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 7 | FIND_PACKAGE(Lua REQUIRED) 8 | 9 | include_directories( 10 | ${Boost_INCLUDE_DIRS} 11 | ${LUA_INCLUDE_DIR} 12 | ${PROJECT_SOURCE_DIR}/src 13 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 14 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 15 | ${PROJECT_SOURCE_DIR}/deps/clipper 16 | ) 17 | 18 | add_executable(nc_contour_offset contour_offset.cpp rs274_clipper_path.cpp ../print_exception.cpp) 19 | target_link_libraries(nc_contour_offset 20 | ${Boost_LIBRARIES} 21 | ${LUA_LIBRARIES} 22 | rs274ngc 23 | cxxcam 24 | nc_base 25 | polyclipping 26 | ) 27 | 28 | add_executable(nc_contour_pocket contour_pocket.cpp rs274_clipper_path.cpp ../print_exception.cpp) 29 | target_link_libraries(nc_contour_pocket 30 | ${Boost_LIBRARIES} 31 | ${LUA_LIBRARIES} 32 | rs274ngc 33 | cxxcam 34 | nc_base 35 | polyclipping 36 | ) 37 | 38 | add_executable(nc_spiral_pocket spiral_pocket.cpp rs274_clipper_path.cpp ../print_exception.cpp) 39 | target_link_libraries(nc_spiral_pocket 40 | ${Boost_LIBRARIES} 41 | ${LUA_LIBRARIES} 42 | rs274ngc 43 | cxxcam 44 | nc_base 45 | polyclipping 46 | ) 47 | 48 | add_executable(nc_contour_spiral contour_spiral.cpp rs274_clipper_path.cpp ../print_exception.cpp) 49 | target_link_libraries(nc_contour_spiral 50 | ${Boost_LIBRARIES} 51 | ${LUA_LIBRARIES} 52 | rs274ngc 53 | cxxcam 54 | nc_base 55 | polyclipping 56 | ) 57 | 58 | add_executable(nc_contour_drill contour_drill.cpp rs274_clipper_path.cpp ../print_exception.cpp) 59 | target_link_libraries(nc_contour_drill 60 | ${Boost_LIBRARIES} 61 | ${LUA_LIBRARIES} 62 | rs274ngc 63 | cxxcam 64 | nc_base 65 | polyclipping 66 | ) 67 | 68 | add_executable(nc_contour_profile contour_profile.cpp rs274_clipper_path.cpp ../print_exception.cpp) 69 | target_link_libraries(nc_contour_profile 70 | ${Boost_LIBRARIES} 71 | ${LUA_LIBRARIES} 72 | rs274ngc 73 | cxxcam 74 | nc_base 75 | polyclipping 76 | ) 77 | -------------------------------------------------------------------------------- /src/nc_contour_pocket/common.h: -------------------------------------------------------------------------------- 1 | /* Copyright (C) 2016 Nicholas Gill 2 | * 3 | * This program is free software: you can redistribute it and/or modify 4 | * it under the terms of the GNU General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or 6 | * (at your option) any later version. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | * 16 | * common.h 17 | * 18 | * Created on: 2016-08-29 19 | * Author: nicholas 20 | */ 21 | 22 | #ifndef COMMON_H_ 23 | #define COMMON_H_ 24 | #include 25 | #include 26 | 27 | namespace geometry { 28 | 29 | struct point_2 30 | { 31 | double x; 32 | double y; 33 | 34 | point_2 operator-(const point_2& p) const { 35 | return {x-p.x, y-p.y}; 36 | } 37 | point_2 operator+(const point_2& p) const { 38 | return {x+p.x, y+p.y}; 39 | } 40 | point_2 operator*(double t) const { 41 | return {x*t, y*t}; 42 | } 43 | }; 44 | inline double distance(const point_2& a, const point_2& b) { 45 | return std::sqrt(std::pow(b.x - a.x, 2) + std::pow(b.y - a.y, 2)); 46 | } 47 | point_2 lerp(const point_2& p0, const point_2& p1, double t) { 48 | return { (1-t)*p0.x + t*p1.x, (1-t)*p0.y + t*p1.y }; 49 | } 50 | 51 | struct vector_2 52 | { 53 | double x; 54 | double y; 55 | }; 56 | inline double dot(const vector_2& a, const vector_2& b) { 57 | return a.x * b.x + a.y * b.y; 58 | } 59 | 60 | struct line_segment_2 61 | { 62 | point_2 a; 63 | point_2 b; 64 | }; 65 | 66 | inline double squared_length(const line_segment_2& l) { 67 | return std::pow(l.b.x - l.a.x, 2) + std::pow(l.b.y - l.a.y, 2); 68 | } 69 | 70 | inline point_2 centroid(const std::vector& polygon) { 71 | point_2 c = {0, 0}; 72 | double A = 0; 73 | auto it = begin(polygon); 74 | for (unsigned i = 0; i < polygon.size(); ++i) { 75 | auto p = *it++; 76 | if (it == end(polygon)) 77 | it = begin(polygon); 78 | auto p1 = *it; 79 | c.x += (p.x + p1.x) * ((p.x * p1.y) - (p1.x * p.y)); 80 | c.y += (p.y + p1.y) * ((p.x * p1.y) - (p1.x * p.y)); 81 | A += ((p.x * p1.y) - (p1.x * p.y)); 82 | } 83 | 84 | A /= 2.0; 85 | c.x /= 6*A; 86 | c.y /= 6*A; 87 | return c; 88 | } 89 | 90 | inline double area(const std::vector& polygon) { 91 | // Shoelace 92 | double area = 0; 93 | for (unsigned i = 0; i < polygon.size(); ++i) { 94 | double j = (i+1) % polygon.size(); 95 | area += polygon[i].x * polygon[j].y; 96 | area -= polygon[i].y * polygon[j].x; 97 | } 98 | return area/2; 99 | } 100 | 101 | inline point_2 nearest_point(const line_segment_2& l, const point_2& p) { 102 | auto l2 = squared_length(l); 103 | if (l2 == 0) return l.a; 104 | auto s1 = p - l.a; 105 | auto s2 = l.b - l.a; 106 | auto t = std::max(0.0, std::min(1.0, dot({s1.x, s1.y}, {s2.x, s2.y}) / l2)); 107 | return l.a + (l.b - l.a) * t; 108 | } 109 | 110 | template 111 | struct maybe { 112 | bool valid; 113 | T value; 114 | 115 | maybe() : valid(false) {} 116 | maybe(T v) : valid(true), value(v) {} 117 | 118 | explicit operator bool() const { return valid; } 119 | maybe& operator= (T v) { 120 | valid = true; 121 | value = v; 122 | return *this; 123 | } 124 | T operator*() const { 125 | if(!valid) 126 | throw std::logic_error("Value not valid."); 127 | return value; 128 | } 129 | }; 130 | 131 | maybe intersects (const line_segment_2& l1, const line_segment_2& l2) { 132 | auto s1 = l1.b - l1.a; 133 | auto s2 = l2.b - l2.a; 134 | 135 | vector_2 ortho{s2.y, -s2.x}; 136 | auto denom = dot({s1.x, s1.y}, ortho); 137 | if (std::abs(denom) < 0.000001) 138 | return {}; 139 | 140 | auto u = l1.a - l2.a; 141 | auto s = ( s2.x * u.y - s2.y * u.x) / denom; 142 | auto t = (-s1.y * u.x + s1.x * u.y) / denom; 143 | 144 | if (s >= 0 && s <= 1 && t >= 0 && t <= 1) 145 | return { l2.a + (s2 * t) }; 146 | 147 | return {}; 148 | } 149 | 150 | maybe intersects (const line_segment_2& l1, const std::vector& polygon) { 151 | auto it = begin(polygon); 152 | auto p0 = *it++; 153 | while (it != end(polygon)) { 154 | auto p1 = *it++; 155 | if (auto p = intersects(l1, line_segment_2{p0, p1})) 156 | return p; 157 | p0 = p1; 158 | } 159 | return {}; 160 | } 161 | 162 | } 163 | 164 | #endif /* COMMON_H_ */ 165 | -------------------------------------------------------------------------------- /src/nc_contour_pocket/contour_offset.cpp: -------------------------------------------------------------------------------- 1 | #include "rs274ngc_return.hh" 2 | #include 3 | #include "print_exception.h" 4 | #include "../throw_if.h" 5 | #include "rs274_clipper_path.h" 6 | #include 7 | #include "clipper.hpp" 8 | #include "base/machine_config.h" 9 | #include 10 | #include "../r6.h" 11 | #include "common.h" 12 | 13 | namespace po = boost::program_options; 14 | namespace cl = ClipperLib; 15 | using namespace geometry; 16 | 17 | std::vector> contour_offset(const ClipperLib::Paths& paths, double tool_offset, bool climb) { 18 | double scale = 10e12; 19 | 20 | auto unscale_point = [&](const cl::IntPoint& p) -> point_2 { 21 | return {static_cast(p.X)/scale, static_cast(p.Y)/scale}; 22 | }; 23 | 24 | cl::ClipperOffset co; 25 | co.AddPaths(paths, cl::jtRound, cl::etClosedPolygon); 26 | co.ArcTolerance = 0.1 * scale; 27 | 28 | std::vector>> toolpaths_levels; 29 | point_2 current_point = {0, 0}; 30 | 31 | double offset = 0.0; 32 | while (offset <= 0) { 33 | 34 | toolpaths_levels.emplace_back(); 35 | auto& toolpaths = toolpaths_levels.back(); 36 | 37 | cl::Paths solution; 38 | co.Execute(solution, offset * scale); 39 | 40 | if(solution.empty()) 41 | break; 42 | 43 | for(auto& path : solution) { 44 | 45 | auto orient_path = [&climb](cl::Path& path) { 46 | bool CCW = Orientation(path); 47 | if (climb && !CCW) 48 | ReversePath(path); 49 | else if (!climb && CCW) 50 | ReversePath(path); 51 | }; 52 | 53 | orient_path(path); 54 | 55 | auto it_near = std::min_element(begin(path), end(path), 56 | [&](const cl::IntPoint& l, const cl::IntPoint& r) -> bool { 57 | return std::abs(distance(unscale_point(l), current_point)) < std::abs(distance(unscale_point(r), current_point)); 58 | }); 59 | double dist = distance(unscale_point(*it_near), current_point); 60 | std::rotate(begin(path), it_near, end(path)); 61 | 62 | // next path closes to start point of this path 63 | current_point = unscale_point(path.front()); 64 | 65 | toolpaths.emplace_back(); 66 | auto& toolpath = toolpaths.back(); 67 | 68 | // cannot be reversed this way because path is continuous... 69 | for(auto& point : path) { 70 | auto p = unscale_point(point); 71 | toolpath.push_back(p); 72 | } 73 | // Close path 74 | toolpath.push_back(unscale_point(path.front())); 75 | } 76 | 77 | offset -= tool_offset; 78 | } 79 | 80 | std::reverse(begin(toolpaths_levels), end(toolpaths_levels)); 81 | std::vector> toolpaths; 82 | for (auto& level : toolpaths_levels) 83 | toolpaths.insert(toolpaths.end(), begin(level), end(level)); 84 | 85 | return toolpaths; 86 | } 87 | 88 | int main(int argc, char* argv[]) { 89 | po::options_description options("nc_contour_offset"); 90 | std::vector args(argv, argv + argc); 91 | args.erase(begin(args)); 92 | 93 | options.add(machine_config::base_options()); 94 | options.add_options() 95 | ("help,h", "display this help and exit") 96 | ("tool_r,r", po::value()->required(), "Tool radius") 97 | ("stepover,s", po::value()->default_value(0.9), "Tool stepover 0.0 - 1.0") 98 | ("feedrate,f", po::value()->required(), "Feedrate") 99 | ("retract_z,t", po::value(), "Z Tool retract") 100 | ; 101 | 102 | try { 103 | po::variables_map vm; 104 | store(po::command_line_parser(args).options(options).run(), vm); 105 | 106 | if(vm.count("help")) { 107 | std::cout << options << "\n"; 108 | return 0; 109 | } 110 | notify(vm); 111 | 112 | rs274_clipper_path nc_path(vm); 113 | double tool_offset = vm["tool_r"].as() * 2 * vm["stepover"].as(); 114 | double feedrate = vm["feedrate"].as(); 115 | 116 | auto process_path = [&] (ClipperLib::Paths paths, double z) { 117 | 118 | // Offset path inwards by tool radius. 119 | { 120 | double offset = -vm["tool_r"].as(); 121 | double scale = 10e12; 122 | 123 | cl::ClipperOffset co; 124 | co.AddPaths(paths, cl::jtRound, cl::etClosedPolygon); 125 | co.ArcTolerance = 0.1 * scale; 126 | co.Execute(paths, offset * scale); 127 | } 128 | 129 | const auto offset_paths = contour_offset(paths, tool_offset, false); 130 | 131 | for (auto& path : offset_paths) { 132 | 133 | bool rapid_to_first = true; 134 | if (rapid_to_first) { 135 | auto p = path.front(); 136 | std::cout << "G0 X" << r6(p.x) << " Y" << r6(p.y) << "\n"; 137 | std::cout << "G1 Z" << r6(z) << " F" << r6(feedrate/2) << "\n"; 138 | rapid_to_first = false; 139 | } 140 | 141 | for(auto& p : path) { 142 | std::cout << " X" << r6(p.x) << " Y" << r6(p.y) << " Z" << r6(z) << "\n"; 143 | } 144 | 145 | if (vm.count("retract_z")) { 146 | double retract_z = vm["retract_z"].as(); 147 | std::cout << "G0 z" << r6(retract_z) << "\n"; 148 | } 149 | } 150 | std::cout << "\n"; 151 | }; 152 | 153 | // TODO read default init line from nc_tools.conf 154 | // nc_path.read("G18"); 155 | // nc_path.execute(); 156 | 157 | nc_path.set_callback(process_path); 158 | 159 | std::string line; 160 | while(std::getline(std::cin, line)) { 161 | int status; 162 | 163 | status = nc_path.read(line.c_str()); 164 | if(status != RS274NGC_OK) { 165 | if(status != RS274NGC_EXECUTE_FINISH) { 166 | std::cerr << "Error reading line!: \n"; 167 | std::cout << line <<"\n"; 168 | return status; 169 | } 170 | } 171 | 172 | status = nc_path.execute(); 173 | if(status != RS274NGC_OK) 174 | return status; 175 | } 176 | 177 | if (nc_path.read("M2") == RS274NGC_OK) 178 | nc_path.execute(); 179 | 180 | } catch(const po::error& e) { 181 | print_exception(e); 182 | std::cout << options << "\n"; 183 | return 1; 184 | } catch(const std::exception& e) { 185 | print_exception(e); 186 | return 1; 187 | } 188 | } 189 | 190 | -------------------------------------------------------------------------------- /src/nc_contour_pocket/contour_spiral.cpp: -------------------------------------------------------------------------------- 1 | #include "rs274ngc_return.hh" 2 | #include 3 | #include "print_exception.h" 4 | #include "../throw_if.h" 5 | #include "rs274_clipper_path.h" 6 | #include 7 | #include "clipper.hpp" 8 | #include "base/machine_config.h" 9 | #include 10 | #include "../r6.h" 11 | #include "common.h" 12 | 13 | namespace po = boost::program_options; 14 | namespace cl = ClipperLib; 15 | using namespace geometry; 16 | 17 | struct Node { 18 | cl::Paths polygon; 19 | std::vector children; 20 | }; 21 | 22 | void fold_path (const cl::PolyNode* node, cl::Paths& paths) { 23 | paths.push_back(node->Contour); 24 | for (auto& child : node->Childs) 25 | fold_path(child, paths); 26 | } 27 | 28 | void offset_node(Node& node, double offset, double scale, unsigned level = 0) { 29 | if (node.polygon.empty()) 30 | return; 31 | 32 | cl::ClipperOffset co; 33 | co.AddPaths(node.polygon, cl::jtRound, cl::etClosedPolygon); 34 | co.ArcTolerance = 0.025 * scale; 35 | 36 | cl::Paths solution; 37 | co.Execute(solution, offset * scale); 38 | if (solution.empty()) 39 | return; 40 | 41 | // Reduce the number of points in the result 42 | cl::CleanPolygons(solution, 1e-5*scale); 43 | 44 | { 45 | cl::Clipper clipper; 46 | clipper.AddPaths(solution, cl::ptSubject, true); 47 | 48 | cl::PolyTree pt; 49 | clipper.Execute(cl::ctUnion, pt); 50 | 51 | node.children.reserve(pt.Childs.size()); 52 | for (auto& child : pt.Childs) { 53 | node.children.emplace_back(); 54 | Node& cn = node.children.back(); 55 | 56 | fold_path(child, cn.polygon); 57 | offset_node(cn, offset, scale, level+1); 58 | } 59 | } 60 | } 61 | 62 | void segment(Node node, std::vector& nodes) { 63 | Node flat; 64 | 65 | Node* curr = &node; 66 | while (curr) { 67 | if (curr->children.size() == 1) { 68 | flat.children.push_back({ curr->polygon, {} }); 69 | curr = &curr->children.back(); 70 | } else if (curr->children.size() == 0) { 71 | flat.children.push_back({ curr->polygon, {} }); 72 | nodes.push_back(flat); 73 | curr = nullptr; 74 | } else { 75 | flat.children.push_back({ curr->polygon, {} }); 76 | nodes.push_back(flat); 77 | 78 | for (auto& node : curr->children) { 79 | segment(node, nodes); 80 | } 81 | curr = nullptr; 82 | } 83 | } 84 | } 85 | 86 | // debug 87 | void output_node(const Node& node, double z, double scale, unsigned level, bool children = true) { 88 | auto unscale_point = [&](const cl::IntPoint& p) -> point_2 { 89 | return {static_cast(p.X)/scale, static_cast(p.Y)/scale}; 90 | }; 91 | 92 | for (auto& path : node.polygon) { 93 | bool rapid_to_first = true; 94 | for (auto& point : path) { 95 | auto p = unscale_point(point); 96 | if (rapid_to_first) { 97 | std::cout << "G0 X" << r6(p.x) << " Y" << r6(p.y) << "\n"; 98 | std::cout << "G1 Z" << r6(z+level) << " F" << r6(50) << "\n"; 99 | rapid_to_first = false; 100 | } 101 | std::cout << " X" << r6(p.x) << " Y" << r6(p.y) << "\n"; 102 | } 103 | auto p = unscale_point(*path.begin()); 104 | std::cout << " X" << r6(p.x) << " Y" << r6(p.y) << "\n"; 105 | } 106 | 107 | if (children) { 108 | for (auto& child : node.children) { 109 | output_node(child, z, scale, level+1); 110 | } 111 | } 112 | } 113 | 114 | int main(int argc, char* argv[]) { 115 | po::options_description options("nc_contour_spiral"); 116 | std::vector args(argv, argv + argc); 117 | args.erase(begin(args)); 118 | 119 | options.add(machine_config::base_options()); 120 | options.add_options() 121 | ("help,h", "display this help and exit") 122 | ("tool_r,r", po::value()->required(), "Tool radius") 123 | ("stepover,s", po::value()->default_value(0.9), "Tool stepover 0.0 - 1.0") 124 | ("feedrate,f", po::value()->required(), "Feedrate") 125 | ; 126 | 127 | try { 128 | po::variables_map vm; 129 | store(po::command_line_parser(args).options(options).run(), vm); 130 | 131 | if(vm.count("help")) { 132 | std::cout << options << "\n"; 133 | return 0; 134 | } 135 | notify(vm); 136 | 137 | rs274_clipper_path nc_path(vm); 138 | double tool_offset = vm["tool_r"].as() * 2 * vm["stepover"].as(); 139 | double feedrate = vm["feedrate"].as(); 140 | 141 | auto process_path = [&] (cl::Paths paths, double z) { 142 | 143 | // Offset path inwards by tool radius. 144 | { 145 | double offset = -vm["tool_r"].as(); 146 | double scale = nc_path.scale(); 147 | 148 | cl::ClipperOffset co; 149 | co.AddPaths(paths, cl::jtRound, cl::etClosedPolygon); 150 | co.ArcTolerance = 0.025 * scale; 151 | co.Execute(paths, offset * scale); 152 | } 153 | 154 | Node root; 155 | root.polygon = paths; 156 | offset_node(root, -tool_offset, nc_path.scale()); 157 | 158 | std::vector segments; 159 | segment(root, segments); 160 | 161 | for (auto& node : segments) { 162 | std::reverse(begin(node.children), end(node.children)); 163 | fprintf(stderr, "node.children.size(): %d\n", node.children.size()); 164 | // TODO modify segments into spiral paths by interpolating between levels 165 | for (unsigned i = 0; i < node.children.size(); ++i) { 166 | if (i == 0) { 167 | output_node(node.children[i], z, nc_path.scale(), 0); 168 | } else { 169 | std::vector path; 170 | } 171 | } 172 | } 173 | 174 | std::cout << "\n"; 175 | }; 176 | 177 | // TODO read default init line from nc_tools.conf 178 | // nc_path.read("G18"); 179 | // nc_path.execute(); 180 | 181 | nc_path.set_callback(process_path); 182 | 183 | std::string line; 184 | while(std::getline(std::cin, line)) { 185 | int status; 186 | 187 | status = nc_path.read(line.c_str()); 188 | if(status != RS274NGC_OK) { 189 | if(status != RS274NGC_EXECUTE_FINISH) { 190 | std::cerr << "Error reading line!: \n"; 191 | std::cout << line <<"\n"; 192 | return status; 193 | } 194 | } 195 | 196 | status = nc_path.execute(); 197 | if(status != RS274NGC_OK) 198 | return status; 199 | } 200 | 201 | if (nc_path.read("M2") == RS274NGC_OK) 202 | nc_path.execute(); 203 | 204 | } catch(const po::error& e) { 205 | print_exception(e); 206 | std::cout << options << "\n"; 207 | return 1; 208 | } catch(const std::exception& e) { 209 | print_exception(e); 210 | return 1; 211 | } 212 | } 213 | 214 | -------------------------------------------------------------------------------- /src/nc_contour_pocket/rs274_clipper_path.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_clipper_path.cpp 20 | * 21 | * Created on: 2016-04-07 22 | * Author: nicholas 23 | */ 24 | 25 | #include "rs274_clipper_path.h" 26 | #include "cxxcam/Path.h" 27 | #include "base/machine_config.h" 28 | 29 | using namespace ClipperLib; 30 | 31 | 32 | void rs274_clipper_path::_rapid(const Position& pos) { 33 | if (new_path_ && std::abs(pos.z - program_pos.z) > 0) { 34 | if (! path_.empty()) { 35 | new_path_(path_, program_pos.z); 36 | path_.clear(); 37 | } 38 | } 39 | 40 | if (path_.empty() || !path_.back().empty()) 41 | path_.emplace_back(); 42 | } 43 | void rs274_clipper_path::_arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation) { 44 | if (plane.z != 1) 45 | throw std::runtime_error("Arc must exist in XY plane"); 46 | if (std::abs(end.z - program_pos.z) > 0) 47 | throw std::runtime_error("Helix not supported in path"); 48 | 49 | using namespace cxxcam::path; 50 | auto steps = expand_arc(convert(program_pos), convert(end), convert(center), (rotation < 0 ? ArcDirection::Clockwise : ArcDirection::CounterClockwise), plane, std::abs(rotation), {}).path; 51 | 52 | if (path_.empty()) 53 | path_.emplace_back(); 54 | for (auto& step : steps) { 55 | auto& p = step.position; 56 | path_.back().push_back(scale_point(p)); 57 | } 58 | } 59 | 60 | 61 | void rs274_clipper_path::_linear(const Position& pos) { 62 | if (_active_plane != Plane::XY) 63 | throw std::runtime_error("Path must be described in XY plane"); 64 | if (std::abs(pos.z - program_pos.z) > 0) 65 | throw std::runtime_error("Path must be 2d"); 66 | 67 | auto steps = cxxcam::path::expand_linear(convert(program_pos), convert(pos), {}, -1).path; 68 | 69 | if (path_.empty()) 70 | path_.emplace_back(); 71 | for (auto& step : steps) { 72 | auto& p = step.position; 73 | path_.back().push_back(scale_point(p)); 74 | } 75 | } 76 | 77 | void rs274_clipper_path::program_end() { 78 | if (new_path_ && ! path_.empty()) { 79 | new_path_(path_, program_pos.z); 80 | path_.clear(); 81 | } 82 | } 83 | 84 | rs274_clipper_path::rs274_clipper_path(boost::program_options::variables_map& vm) 85 | : rs274_base(vm) { 86 | } 87 | 88 | IntPoint rs274_clipper_path::scale_point(const cxxcam::math::point_3& p) const { 89 | using cxxcam::units::length_mm; 90 | using cxxcam::units::length_inch; 91 | 92 | switch (machine_config::machine_units(config, machine_id)) { 93 | case machine_config::units::metric: 94 | return IntPoint(length_mm(p.x).value() * scale(), length_mm(p.y).value() * scale()); 95 | case machine_config::units::imperial: 96 | return IntPoint(length_inch(p.x).value() * scale(), length_inch(p.y).value() * scale()); 97 | default: 98 | throw std::logic_error("Unhandled units"); 99 | } 100 | } 101 | 102 | Paths rs274_clipper_path::path() const { 103 | return path_; 104 | } 105 | 106 | void rs274_clipper_path::set_callback(std::function new_path) { 107 | new_path_ = new_path; 108 | } 109 | -------------------------------------------------------------------------------- /src/nc_contour_pocket/rs274_clipper_path.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_clipper_path.h 20 | * 21 | * Created on: 2016-04-07 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_CLIPPER_PATH_H_ 26 | #define RS274_CLIPPER_PATH_H_ 27 | #include "base/rs274_base.h" 28 | #include "cxxcam/Math.h" 29 | #include "clipper.hpp" 30 | #include 31 | 32 | class rs274_clipper_path : public rs274_base 33 | { 34 | private: 35 | 36 | virtual void _rapid(const Position&); 37 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation); 38 | virtual void _linear(const Position& pos); 39 | virtual void program_end(); 40 | 41 | ClipperLib::Paths path_; 42 | std::function new_path_; 43 | public: 44 | rs274_clipper_path(boost::program_options::variables_map& vm); 45 | 46 | ClipperLib::Paths path() const; 47 | void set_callback(std::function new_path); 48 | 49 | ClipperLib::IntPoint scale_point(const cxxcam::math::point_3& p) const; 50 | double scale() const { 51 | return 10e12; 52 | } 53 | 54 | virtual ~rs274_clipper_path() = default; 55 | }; 56 | 57 | #endif /* RS274_CLIPPER_PATH_H_ */ 58 | -------------------------------------------------------------------------------- /src/nc_delay/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Threads REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 8 | FIND_PACKAGE(Lua REQUIRED) 9 | 10 | include_directories( 11 | ${Boost_INCLUDE_DIRS} 12 | ${LUA_INCLUDE_DIR} 13 | ${PROJECT_SOURCE_DIR}/src 14 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 15 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 16 | ) 17 | 18 | add_executable(nc_delay delay.cpp rs274_delay.cpp ../print_exception.cpp) 19 | target_link_libraries(nc_delay 20 | ${CMAKE_THREAD_LIBS_INIT} 21 | ${Boost_LIBRARIES} 22 | ${LUA_LIBRARIES} 23 | rs274ngc 24 | nc_base 25 | ) 26 | -------------------------------------------------------------------------------- /src/nc_delay/delay.cpp: -------------------------------------------------------------------------------- 1 | #include "rs274_delay.h" 2 | #include "rs274ngc_return.hh" 3 | #include 4 | #include "print_exception.h" 5 | #include "base/machine_config.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | namespace po = boost::program_options; 13 | 14 | int main(int argc, char* argv[]) { 15 | po::options_description options("nc_delay"); 16 | std::vector args(argv, argv + argc); 17 | args.erase(begin(args)); 18 | 19 | options.add(machine_config::base_options()); 20 | options.add_options() 21 | ("help,h", "display this help and exit") 22 | ("scale,s", po::value()->default_value(1.0), "feed rate scale factor") 23 | ("measure,m", "measure only") 24 | ; 25 | 26 | try { 27 | po::variables_map vm; 28 | store(po::command_line_parser(args).options(options).run(), vm); 29 | 30 | if(vm.count("help")) { 31 | std::cout << options << "\n"; 32 | return 0; 33 | } 34 | notify(vm); 35 | 36 | rs274_delay delayer(vm, vm["scale"].as(), vm.count("measure")); 37 | 38 | std::string line; 39 | while(std::getline(std::cin, line)) { 40 | int status; 41 | 42 | status = delayer.read(line.c_str()); 43 | if(status != RS274NGC_OK) { 44 | if(status != RS274NGC_EXECUTE_FINISH) { 45 | std::cerr << "Error reading line!: \n"; 46 | std::cout << line <<"\n"; 47 | return status; 48 | } 49 | } 50 | 51 | status = delayer.execute(); 52 | if(status != RS274NGC_OK) 53 | return status; 54 | std::cout << line << "\n"; 55 | } 56 | 57 | auto duration = delayer.cut_duration(); 58 | auto hours = std::chrono::duration_cast(duration); 59 | auto minutes = std::chrono::duration_cast(duration - hours); 60 | auto seconds = std::chrono::duration_cast(duration - (hours + minutes)); 61 | auto milliseconds = std::chrono::duration_cast(duration - (hours + minutes + seconds)); 62 | if (hours.count() > 0) std::cerr << hours.count() << " h "; 63 | if (minutes.count() > 0) std::cerr << minutes.count() << " m "; 64 | if (seconds.count() > 0) std::cerr << seconds.count() << " s "; 65 | if (milliseconds.count() > 0) std::cerr << milliseconds.count() << " ms "; 66 | std::cerr << "\n"; 67 | 68 | } catch(const po::error& e) { 69 | print_exception(e); 70 | std::cout << options << "\n"; 71 | return 1; 72 | } catch(const std::exception& e) { 73 | print_exception(e); 74 | return 1; 75 | } 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /src/nc_delay/rs274_delay.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_delay.cpp 20 | * 21 | * Created on: 2015-07-24 22 | * Author: nicholas 23 | */ 24 | 25 | #include "rs274_delay.h" 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include "cxxcam/Path.h" 32 | #include "../throw_if.h" 33 | 34 | double rs274_delay::motion_duration_s(const cxxcam::units::length& motion_length) const { 35 | using namespace cxxcam; 36 | 37 | throw_if(_feed_rate == 0, "Zero feed rate"); 38 | 39 | auto feed_rate = [&] { 40 | if(_length_unit_type == Units::Metric) { 41 | return units::velocity{ (_feed_rate*scale) * units::millimeters_per_minute }; 42 | } else { 43 | return units::velocity{ (_feed_rate*scale) * units::inches_per_minute }; 44 | } 45 | }(); 46 | 47 | auto time = units::time{ motion_length / feed_rate }; 48 | // TODO is a static assert that time is represented as seconds necessary? 49 | return time.value(); 50 | } 51 | 52 | void rs274_delay::_rapid(const Position&) { 53 | using namespace cxxcam; 54 | 55 | //auto length = path::length_linear(convert(program_pos), convert(pos)); 56 | // TODO rapid rate? 57 | } 58 | 59 | void rs274_delay::_arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation) { 60 | using namespace cxxcam; 61 | 62 | auto length = path::length_arc(convert(program_pos), convert(end), convert(center), (rotation < 0 ? path::ArcDirection::Clockwise : path::ArcDirection::CounterClockwise), plane, std::abs(rotation)); 63 | auto time_delta = std::chrono::duration(motion_duration_s(length)); 64 | duration += time_delta; 65 | if (!measure_only) 66 | std::this_thread::sleep_for(time_delta); 67 | } 68 | 69 | 70 | void rs274_delay::_linear(const Position& pos) { 71 | using namespace cxxcam; 72 | 73 | auto length = path::length_linear(convert(program_pos), convert(pos)); 74 | auto time_delta = std::chrono::duration(motion_duration_s(length)); 75 | duration += time_delta; 76 | if (!measure_only) 77 | std::this_thread::sleep_for(time_delta); 78 | } 79 | 80 | rs274_delay::rs274_delay(boost::program_options::variables_map& vm, double scale, bool measure_only) 81 | : rs274_base(vm), scale(scale > 0 ? scale : 1), measure_only(measure_only) { 82 | } 83 | 84 | std::chrono::duration rs274_delay::cut_duration() { 85 | return duration; 86 | } 87 | -------------------------------------------------------------------------------- /src/nc_delay/rs274_delay.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_delay.h 20 | * 21 | * Created on: 2015-07-24 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_DELAY_H_ 26 | #define RS274_DELAY_H_ 27 | #include "base/rs274_base.h" 28 | #include 29 | 30 | class rs274_delay : public rs274_base 31 | { 32 | private: 33 | double scale; 34 | bool measure_only; 35 | std::chrono::duration duration; 36 | double motion_duration_s(const cxxcam::units::length& motion_length) const; 37 | 38 | virtual void _rapid(const Position& pos); 39 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation); 40 | virtual void _linear(const Position& pos); 41 | 42 | public: 43 | rs274_delay(boost::program_options::variables_map& vm, double scale, bool measure_only); 44 | std::chrono::duration cut_duration(); 45 | virtual ~rs274_delay() = default; 46 | }; 47 | 48 | #endif /* RS274_DELAY_H_ */ 49 | -------------------------------------------------------------------------------- /src/nc_feedrate/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Threads REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 8 | FIND_PACKAGE(Lua REQUIRED) 9 | 10 | include_directories( 11 | ${Boost_INCLUDE_DIRS} 12 | ${LUA_INCLUDE_DIR} 13 | ${PROJECT_SOURCE_DIR}/src 14 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 15 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 16 | ${PROJECT_SOURCE_DIR}/deps/geom/include 17 | ) 18 | 19 | add_executable(nc_feedrate feedrate.cpp rs274_feedrate.cpp ../Simulation.cpp ../Tool.cpp ../Stock.cpp ../print_exception.cpp) 20 | target_link_libraries(nc_feedrate 21 | ${CMAKE_THREAD_LIBS_INIT} 22 | ${Boost_LIBRARIES} 23 | ${LUA_LIBRARIES} 24 | rs274ngc 25 | cxxcam 26 | geom 27 | nc_base 28 | ) 29 | -------------------------------------------------------------------------------- /src/nc_feedrate/feedrate.cpp: -------------------------------------------------------------------------------- 1 | #include "rs274_feedrate.h" 2 | #include "rs274ngc_return.hh" 3 | #include 4 | #include "print_exception.h" 5 | #include "base/machine_config.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | namespace po = boost::program_options; 13 | 14 | int main(int argc, char* argv[]) { 15 | po::options_description options("nc_feedrate"); 16 | std::vector args(argv, argv + argc); 17 | args.erase(begin(args)); 18 | 19 | options.add(machine_config::base_options()); 20 | options.add_options() 21 | ("help,h", "display this help and exit") 22 | ("stock", po::value()->required(), "Stock model file") 23 | ("tool", po::value(), "Default tool") 24 | ; 25 | 26 | try { 27 | po::variables_map vm; 28 | store(po::command_line_parser(args).options(options).run(), vm); 29 | 30 | if(vm.count("help")) { 31 | std::cout << options << "\n"; 32 | return 0; 33 | } 34 | notify(vm); 35 | 36 | rs274_feedrate rate(vm, vm["stock"].as()); 37 | 38 | if(vm.count("tool")) { 39 | std::stringstream s; 40 | s << "M06 T" << vm["tool"].as(); 41 | rate.read(s.str().c_str()); 42 | rate.execute(); 43 | } 44 | 45 | std::string line; 46 | while(std::getline(std::cin, line)) { 47 | int status; 48 | 49 | status = rate.read(line.c_str()); 50 | if(status != RS274NGC_OK) { 51 | if(status != RS274NGC_EXECUTE_FINISH) { 52 | std::cerr << "Error reading line!: \n"; 53 | std::cerr << line <<"\n"; 54 | return status; 55 | } 56 | } 57 | 58 | status = rate.execute(); 59 | if(status != RS274NGC_OK) 60 | return status; 61 | std::cout << line << "\n"; 62 | } 63 | 64 | } catch(const po::error& e) { 65 | print_exception(e); 66 | std::cout << options << "\n"; 67 | return 1; 68 | } catch(const std::exception& e) { 69 | print_exception(e); 70 | return 1; 71 | } 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /src/nc_feedrate/rs274_feedrate.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_feedrate.h 20 | * 21 | * Created on: 2016-02-22 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_FEEDRATE_H_ 26 | #define RS274_FEEDRATE_H_ 27 | #include "base/rs274_base.h" 28 | #include "cxxcam/Position.h" 29 | #include "geom/polyhedron.h" 30 | #include 31 | #include 32 | #include "base/machine_config.h" 33 | 34 | namespace cxxcam { 35 | namespace path { 36 | struct step; 37 | } 38 | } 39 | 40 | class rs274_feedrate : public rs274_base 41 | { 42 | private: 43 | geom::polyhedron_t _model; 44 | geom::polyhedron_t _toolmodel; 45 | geom::polyhedron_t _tool_shank; 46 | struct { 47 | machine_config::mill_tool mill; 48 | machine_config::lathe_tool lathe; 49 | } _tool; 50 | 51 | double chip_load(const cxxcam::path::step& s0, const cxxcam::path::step& s1, double spindle_step); 52 | 53 | virtual void _rapid(const Position& pos); 54 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation); 55 | virtual void _linear(const Position& pos); 56 | virtual void tool_change(int slot); 57 | 58 | public: 59 | rs274_feedrate(boost::program_options::variables_map& vm, const std::string& stock_filename); 60 | 61 | virtual ~rs274_feedrate() = default; 62 | }; 63 | 64 | #endif /* RS274_MODEL_H_ */ 65 | -------------------------------------------------------------------------------- /src/nc_identity/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Threads REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 8 | FIND_PACKAGE(Lua REQUIRED) 9 | 10 | include_directories( 11 | ${Boost_INCLUDE_DIRS} 12 | ${LUA_INCLUDE_DIR} 13 | ${PROJECT_SOURCE_DIR}/src 14 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 15 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 16 | ) 17 | 18 | add_executable(nc_identity identity.cpp rs274_identity.cpp ../print_exception.cpp) 19 | target_link_libraries(nc_identity 20 | ${CMAKE_THREAD_LIBS_INIT} 21 | ${Boost_LIBRARIES} 22 | ${LUA_LIBRARIES} 23 | rs274ngc 24 | nc_base 25 | cxxcam 26 | ) 27 | -------------------------------------------------------------------------------- /src/nc_identity/identity.cpp: -------------------------------------------------------------------------------- 1 | #include "rs274_identity.h" 2 | #include "rs274ngc_return.hh" 3 | #include 4 | #include "print_exception.h" 5 | #include "../throw_if.h" 6 | #include "base/machine_config.h" 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | namespace po = boost::program_options; 13 | 14 | int main(int argc, char* argv[]) { 15 | po::options_description options("nc_identity"); 16 | std::vector args(argv, argv + argc); 17 | args.erase(begin(args)); 18 | 19 | options.add(machine_config::base_options()); 20 | options.add_options() 21 | ("help,h", "display this help and exit") 22 | ; 23 | 24 | try { 25 | po::variables_map vm; 26 | store(po::command_line_parser(args).options(options).run(), vm); 27 | 28 | if(vm.count("help")) { 29 | std::cout << options << "\n"; 30 | return 0; 31 | } 32 | notify(vm); 33 | 34 | rs274_identity identity(vm); 35 | 36 | std::string line; 37 | while(std::getline(std::cin, line)) { 38 | int status; 39 | 40 | status = identity.read(line.c_str()); 41 | if(status != RS274NGC_OK) { 42 | if(status != RS274NGC_EXECUTE_FINISH) { 43 | std::cerr << "Error reading line!: \n"; 44 | std::cout << line <<"\n"; 45 | return status; 46 | } 47 | } 48 | 49 | status = identity.execute(); 50 | if(status != RS274NGC_OK) 51 | return status; 52 | } 53 | 54 | } catch(const po::error& e) { 55 | print_exception(e); 56 | std::cout << options << "\n"; 57 | return 1; 58 | } catch(const std::exception& e) { 59 | print_exception(e); 60 | return 1; 61 | } 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /src/nc_identity/rs274_identity.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_identity.cpp 20 | * 21 | * Created on: 2015-11-12 22 | * Author: nicholas 23 | */ 24 | 25 | #include "rs274_identity.h" 26 | #include 27 | 28 | void rs274_identity::_rapid(const Position&) { 29 | } 30 | 31 | void rs274_identity::_arc(const Position&, const Position&, const cxxcam::math::vector_3&, int) { 32 | } 33 | 34 | 35 | void rs274_identity::_linear(const Position&) { 36 | } 37 | 38 | void rs274_identity::block_end(const block_t& block) { 39 | std::cout << str(block) << "\n"; 40 | } 41 | rs274_identity::rs274_identity(boost::program_options::variables_map& vm) 42 | : rs274_base(vm) { 43 | } 44 | 45 | -------------------------------------------------------------------------------- /src/nc_identity/rs274_identity.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_identity.h 20 | * 21 | * Created on: 2015-11-12 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_IDENTITY_H_ 26 | #define RS274_IDENTITY_H_ 27 | #include "base/rs274_base.h" 28 | 29 | class rs274_identity : public rs274_base 30 | { 31 | private: 32 | 33 | virtual void _rapid(const Position& pos); 34 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation); 35 | virtual void _linear(const Position& pos); 36 | 37 | virtual void block_end(const block_t& block); 38 | 39 | public: 40 | rs274_identity(boost::program_options::variables_map& vm); 41 | 42 | virtual ~rs274_identity() = default; 43 | }; 44 | 45 | #endif /* RS274_IDENTITY_H_ */ 46 | -------------------------------------------------------------------------------- /src/nc_lathe_roughing/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Threads REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 8 | FIND_PACKAGE(Lua REQUIRED) 9 | 10 | include_directories( 11 | ${Boost_INCLUDE_DIRS} 12 | ${LUA_INCLUDE_DIR} 13 | ${PROJECT_SOURCE_DIR}/src 14 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 15 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 16 | ${PROJECT_SOURCE_DIR}/deps/clipper 17 | ) 18 | 19 | add_executable(nc_lathe_roughing lathe_roughing.cpp geometry.cpp rs274_lathe_path.cpp ../print_exception.cpp) 20 | target_link_libraries(nc_lathe_roughing 21 | ${CMAKE_THREAD_LIBS_INIT} 22 | ${Boost_LIBRARIES} 23 | ${LUA_LIBRARIES} 24 | rs274ngc 25 | cxxcam 26 | nc_base 27 | polyclipping 28 | ) 29 | -------------------------------------------------------------------------------- /src/nc_lathe_roughing/geometry.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * geometry.cpp 20 | * 21 | * Created on: 2016-03-25 22 | * Author: nicholas 23 | */ 24 | 25 | #include "geometry.h" 26 | 27 | boost::optional intersects (const line_segment_2& l1, const line_segment_2& l2) { 28 | auto s1 = l1.b - l1.a; 29 | auto s2 = l2.b - l2.a; 30 | 31 | vector_2 ortho{s2.z, -s2.x}; 32 | auto denom = dot({s1.x, s1.z}, ortho); 33 | if (std::abs(denom) < 0.000001) 34 | return {}; 35 | 36 | auto u = l1.a - l2.a; 37 | auto s = ( s2.x * u.z - s2.z * u.x) / denom; 38 | auto t = (-s1.z * u.x + s1.x * u.z) / denom; 39 | 40 | if (s >= 0 && s <= 1 && t >= 0 && t <= 1) 41 | return { l1.a + (s1 * t) }; 42 | 43 | return {}; 44 | } 45 | -------------------------------------------------------------------------------- /src/nc_lathe_roughing/geometry.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * geometry.h 20 | * 21 | * Created on: 2016-03-25 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef GEOMETRY_H_ 26 | #define GEOMETRY_H_ 27 | #include 28 | #include 29 | 30 | struct point_2 31 | { 32 | double x; 33 | double z; 34 | 35 | point_2 operator-(const point_2& p) const { 36 | return {x-p.x, z-p.z}; 37 | } 38 | point_2 operator+(const point_2& p) const { 39 | return {x+p.x, z+p.z}; 40 | } 41 | point_2 operator*(double t) const { 42 | return {x*t, z*t}; 43 | } 44 | }; 45 | inline double distance(const point_2& a, const point_2& b) { 46 | return std::sqrt(std::pow(b.x - a.x, 2) + std::pow(b.z - a.z, 2)); 47 | } 48 | 49 | struct vector_2 50 | { 51 | double x; 52 | double z; 53 | }; 54 | inline double dot(const vector_2& a, const vector_2& b) { 55 | return a.x * b.x + a.z * b.z; 56 | } 57 | 58 | struct line_segment_2 59 | { 60 | point_2 a; 61 | point_2 b; 62 | }; 63 | 64 | boost::optional intersects(const line_segment_2& l1, const line_segment_2& l2); 65 | 66 | #endif /* GEOMETRY_H_ */ 67 | -------------------------------------------------------------------------------- /src/nc_lathe_roughing/rs274_lathe_path.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_path.cpp 20 | * 21 | * Created on: 2015-11-12 22 | * Author: nicholas 23 | */ 24 | 25 | #include "rs274_lathe_path.h" 26 | #include "cxxcam/Path.h" 27 | #include "base/machine_config.h" 28 | 29 | using namespace ClipperLib; 30 | 31 | void rs274_path::_rapid(const Position&) { 32 | if (!path_.empty()) { 33 | throw std::runtime_error("Rapid within profile disallowed"); 34 | } else { 35 | using cxxcam::units::length_mm; 36 | using cxxcam::units::length_inch; 37 | 38 | auto p = convert(program_pos); 39 | switch (machine_config::machine_units(config, machine_id)) { 40 | case machine_config::units::metric: 41 | start_point_ = IntPoint(length_mm(p.X).value() * scale(), length_mm(p.Z).value() * scale()); 42 | break; 43 | case machine_config::units::imperial: 44 | start_point_ = IntPoint(length_inch(p.X).value() * scale(), length_inch(p.Z).value() * scale()); 45 | break; 46 | default: 47 | throw std::logic_error("Unhandled units"); 48 | } 49 | } 50 | } 51 | 52 | void rs274_path::_arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation) { 53 | if (plane.y != 1) 54 | throw std::runtime_error("Arc must exist in XZ plane"); 55 | if (std::abs(end.y - program_pos.y) > 0) 56 | throw std::runtime_error("Helix not supported in path"); 57 | if (std::abs(rotation) > 1) 58 | throw std::runtime_error("Single rotation only in path"); 59 | 60 | using namespace cxxcam::path; 61 | auto steps = expand_arc(convert(program_pos), convert(end), convert(center), (rotation < 0 ? ArcDirection::Clockwise : ArcDirection::CounterClockwise), plane, std::abs(rotation), {}).path; 62 | 63 | for (auto& step : steps) { 64 | auto& p = step.position; 65 | path_.push_back(scale_point(p)); 66 | } 67 | } 68 | 69 | void rs274_path::_linear(const Position& pos) { 70 | if (_active_plane != Plane::XZ) 71 | throw std::runtime_error("Path must be described in XZ plane"); 72 | if (std::abs(pos.y - program_pos.y) > 0) 73 | throw std::runtime_error("Path must be 2d"); 74 | 75 | auto steps = cxxcam::path::expand_linear(convert(program_pos), convert(pos), {}, -1).path; 76 | 77 | for (auto& step : steps) { 78 | auto& p = step.position; 79 | path_.push_back(scale_point(p)); 80 | } 81 | } 82 | 83 | rs274_path::rs274_path(boost::program_options::variables_map& vm) 84 | : rs274_base(vm) { 85 | } 86 | 87 | IntPoint rs274_path::scale_point(const cxxcam::math::point_3& p) const { 88 | using cxxcam::units::length_mm; 89 | using cxxcam::units::length_inch; 90 | 91 | switch (machine_config::machine_units(config, machine_id)) { 92 | case machine_config::units::metric: 93 | return IntPoint(length_mm(p.x).value() * scale(), length_mm(p.z).value() * scale()); 94 | case machine_config::units::imperial: 95 | return IntPoint(length_inch(p.x).value() * scale(), length_inch(p.z).value() * scale()); 96 | default: 97 | throw std::logic_error("Unhandled units"); 98 | } 99 | } 100 | 101 | Path rs274_path::path() const { 102 | return path_; 103 | } 104 | -------------------------------------------------------------------------------- /src/nc_lathe_roughing/rs274_lathe_path.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_path.h 20 | * 21 | * Created on: 2016-03-24 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_PATH_H_ 26 | #define RS274_PATH_H_ 27 | #include "base/rs274_base.h" 28 | #include "geometry.h" 29 | #include "clipper.hpp" 30 | 31 | class rs274_path : public rs274_base 32 | { 33 | private: 34 | 35 | virtual void _rapid(const Position&); 36 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation); 37 | virtual void _linear(const Position& pos); 38 | 39 | ClipperLib::IntPoint start_point_; 40 | 41 | ClipperLib::Path path_; 42 | public: 43 | rs274_path(boost::program_options::variables_map& vm); 44 | 45 | ClipperLib::IntPoint start() const { return start_point_; } 46 | ClipperLib::Path path() const; 47 | 48 | ClipperLib::IntPoint scale_point(const cxxcam::math::point_3& p) const; 49 | double scale() const { 50 | return 10e12; 51 | } 52 | 53 | virtual ~rs274_path() = default; 54 | }; 55 | 56 | #endif /* RS274_PATH_H_ */ 57 | -------------------------------------------------------------------------------- /src/nc_linear_ramp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Threads REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 8 | FIND_PACKAGE(Lua REQUIRED) 9 | 10 | include_directories( 11 | ${Boost_INCLUDE_DIRS} 12 | ${LUA_INCLUDE_DIR} 13 | ${PROJECT_SOURCE_DIR}/src 14 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 15 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 16 | ) 17 | 18 | add_executable(nc_linear_ramp linear_ramp.cpp rs274_ramp.cpp ../print_exception.cpp) 19 | target_link_libraries(nc_linear_ramp 20 | ${CMAKE_THREAD_LIBS_INIT} 21 | ${Boost_LIBRARIES} 22 | ${LUA_LIBRARIES} 23 | rs274ngc 24 | nc_base 25 | cxxcam 26 | ) 27 | -------------------------------------------------------------------------------- /src/nc_linear_ramp/linear_ramp.cpp: -------------------------------------------------------------------------------- 1 | #include "rs274_ramp.h" 2 | #include "rs274ngc_return.hh" 3 | #include 4 | #include "print_exception.h" 5 | #include "../throw_if.h" 6 | #include "base/machine_config.h" 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | namespace po = boost::program_options; 13 | 14 | int main(int argc, char* argv[]) { 15 | po::options_description options("nc_linear_ramp"); 16 | std::vector args(argv, argv + argc); 17 | args.erase(begin(args)); 18 | 19 | options.add(machine_config::base_options()); 20 | options.add_options() 21 | ("help,h", "display this help and exit") 22 | ("angle,a", po::value()->default_value(5.0), "Ramp angle") 23 | ("length,l", po::value()->default_value(10.0), "Ramp length") 24 | ; 25 | 26 | try { 27 | po::variables_map vm; 28 | store(po::command_line_parser(args).options(options).run(), vm); 29 | 30 | if(vm.count("help")) { 31 | std::cout << options << "\n"; 32 | return 0; 33 | } 34 | notify(vm); 35 | 36 | rs274_ramp ramp(vm); 37 | 38 | std::string line; 39 | while(std::getline(std::cin, line)) { 40 | int status; 41 | 42 | status = ramp.read(line.c_str()); 43 | if(status != RS274NGC_OK) { 44 | if(status != RS274NGC_EXECUTE_FINISH) { 45 | std::cerr << "Error reading line!: \n"; 46 | std::cout << line <<"\n"; 47 | return status; 48 | } 49 | } 50 | 51 | status = ramp.execute(); 52 | if(status != RS274NGC_OK) 53 | return status; 54 | } 55 | 56 | } catch(const po::error& e) { 57 | print_exception(e); 58 | std::cout << options << "\n"; 59 | return 1; 60 | } catch(const std::exception& e) { 61 | print_exception(e); 62 | return 1; 63 | } 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /src/nc_linear_ramp/rs274_ramp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_ramp.cpp 20 | * 21 | * Created on: 2019-10-04 22 | * Author: nicholas 23 | */ 24 | 25 | #include "rs274_ramp.h" 26 | #include 27 | #include "cxxcam/Path.h" 28 | #include "base/machine_config.h" 29 | #include "../r6.h" 30 | #include "../throw_if.h" 31 | #include "../fold_adjacent.h" 32 | #include 33 | 34 | void rs274_ramp::_rapid(const Position&) { 35 | } 36 | 37 | void rs274_ramp::_arc(const Position&, const Position&, const cxxcam::math::vector_3&, int) { 38 | throw_if(m_ramping, "Arc not handled while ramping"); 39 | } 40 | 41 | void rs274_ramp::_linear(const Position& pos) { 42 | using namespace cxxcam::path; 43 | using cxxcam::units::length_mm; 44 | using cxxcam::units::length_inch; 45 | 46 | // detect plunge movement 47 | if (std::abs(pos.x - program_pos.x) < 1e9 && 48 | std::abs(pos.y - program_pos.y) < 1e9 && 49 | (program_pos.z - pos.z) > 0) 50 | { 51 | throw_if(m_ramping, "Plunge while ramping not supported"); 52 | 53 | m_ramping = true; 54 | m_start = convert(program_pos); 55 | m_ramp = convert(pos); 56 | m_rampPath.clear(); 57 | } 58 | 59 | if (m_ramping) { 60 | throw_if (std::abs((convert(pos).Z - m_ramp.Z).value()) > 1e9, "Ramping on 3D path not supported."); 61 | 62 | m_rampPath.push_back(convert(pos)); 63 | 64 | auto deg2rad = [](double d) { return (d / 180.0) * PI; }; 65 | 66 | if (length_mm(ramp_path_length()).value() >= m_length) { 67 | 68 | // Hit or exceeded target length, output ramping 69 | 70 | std::vector ramp_steps; 71 | // TODO trim to exact target length 72 | { 73 | cxxcam::Position prev = m_rampPath.front(); 74 | for (auto& pos : m_rampPath) { 75 | auto steps = expand_linear(prev, pos, {}, 5).path; 76 | ramp_steps.insert(end(ramp_steps), begin(steps), end(steps)); 77 | prev = pos; 78 | } 79 | } 80 | 81 | auto Z = m_start.Z; 82 | int step_index = 0; 83 | int direction = 1; 84 | 85 | cxxcam::path::step prev = ramp_steps.front(); 86 | cxxcam::units::length dist; 87 | 88 | // TODO lift needed only for non-centercutting tools 89 | // at angles which would foul the center of the cutter. 90 | // TODO Calculate lift 91 | cxxcam::units::length h(0 * cxxcam::units::millimeter); 92 | 93 | while (Z > m_ramp.Z) { 94 | auto step = ramp_steps[step_index]; 95 | auto step_delta = cxxcam::math::distance(prev.position, step.position); 96 | 97 | auto z = (std::sin(deg2rad(m_angle)) * step_delta); 98 | if (z.value() < 0) 99 | z = -z; 100 | Z -= z; 101 | if (Z <= m_ramp.Z) 102 | Z = m_ramp.Z; 103 | 104 | step.position.z = Z; 105 | 106 | output_point(step.position); 107 | 108 | dist += step_delta; 109 | prev = ramp_steps[step_index]; 110 | 111 | step_index += direction; 112 | if (static_cast(step_index) == ramp_steps.size() || step_index == -1) { 113 | direction = -direction; 114 | step_index += direction*2; 115 | Z += h; 116 | } 117 | } 118 | 119 | while (true) { 120 | auto step = ramp_steps[step_index]; 121 | output_point(step.position); 122 | 123 | if (direction == 1) 124 | step_index -= direction; // Reversed 125 | else 126 | step_index += direction; 127 | if (static_cast(step_index) == ramp_steps.size() || step_index == -1) 128 | break; 129 | } 130 | 131 | { 132 | cxxcam::Position prev = m_rampPath.front(); 133 | for (auto& pos : m_rampPath) { 134 | auto steps = expand_linear(prev, pos, {}, -1).path; 135 | for (auto& step : steps) { 136 | auto& p = step.position; 137 | output_point(p); 138 | } 139 | prev = pos; 140 | } 141 | } 142 | 143 | m_ramping = false; 144 | } 145 | } 146 | } 147 | 148 | cxxcam::units::length rs274_ramp::ramp_path_length() { 149 | using namespace cxxcam; 150 | std::vector lengths; 151 | fold_adjacent(begin(m_rampPath), end(m_rampPath), std::back_inserter(lengths), cxxcam::path::length_linear); 152 | return std::accumulate(begin(lengths), end(lengths), cxxcam::units::length{}); 153 | } 154 | 155 | void rs274_ramp::output_point(const cxxcam::math::point_3& p) const { 156 | using cxxcam::units::length_mm; 157 | using cxxcam::units::length_inch; 158 | 159 | std::cout << "G1"; 160 | 161 | switch (machine_config::machine_units(config, machine_id)) { 162 | case machine_config::units::metric: 163 | std::cout << " X" << r6(length_mm(p.x).value()); 164 | std::cout << " Y" << r6(length_mm(p.y).value()); 165 | std::cout << " Z" << r6(length_mm(p.z).value()); 166 | break; 167 | case machine_config::units::imperial: 168 | std::cout << " X" << r6(length_inch(p.x).value()); 169 | std::cout << " Y" << r6(length_inch(p.y).value()); 170 | std::cout << " Z" << r6(length_inch(p.z).value()); 171 | break; 172 | default: 173 | throw std::logic_error("Unhandled units"); 174 | } 175 | 176 | std::cout << " F" << _feed_rate; 177 | std::cout << "\n"; 178 | } 179 | 180 | void rs274_ramp::block_end(const block_t& block) { 181 | if (m_ramping == false) { 182 | std::cout << str(block) << "\n"; 183 | } 184 | } 185 | rs274_ramp::rs274_ramp(boost::program_options::variables_map& vm) 186 | : rs274_base(vm) { 187 | m_angle = vm["angle"].as(); 188 | m_length = vm["length"].as(); 189 | m_ramping = false; 190 | 191 | throw_if(m_length == 0, "Zero ramp length"); 192 | } 193 | 194 | -------------------------------------------------------------------------------- /src/nc_linear_ramp/rs274_ramp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_ramp.h 20 | * 21 | * Created on: 2019-10-04 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_RAMP_H_ 26 | #define RS274_RAMP_H_ 27 | #include "base/rs274_base.h" 28 | 29 | class rs274_ramp : public rs274_base 30 | { 31 | private: 32 | 33 | virtual void _rapid(const Position& pos); 34 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation); 35 | virtual void _linear(const Position& pos); 36 | 37 | cxxcam::units::length ramp_path_length(); 38 | 39 | virtual void block_end(const block_t& block); 40 | 41 | void output_point(const cxxcam::math::point_3& p) const; 42 | private: 43 | double m_angle; 44 | double m_length; 45 | 46 | bool m_ramping; 47 | std::vector m_rampPath; 48 | cxxcam::Position m_start; 49 | cxxcam::Position m_ramp; 50 | 51 | public: 52 | rs274_ramp(boost::program_options::variables_map& vm); 53 | 54 | virtual ~rs274_ramp() = default; 55 | }; 56 | 57 | #endif /* RS274_RAMP_H_ */ 58 | -------------------------------------------------------------------------------- /src/nc_mill_roughing/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Threads REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 8 | FIND_PACKAGE(Lua REQUIRED) 9 | 10 | include_directories( 11 | ${Boost_INCLUDE_DIRS} 12 | ${LUA_INCLUDE_DIR} 13 | ${PROJECT_SOURCE_DIR}/src 14 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 15 | ${PROJECT_SOURCE_DIR}/deps/geom/include 16 | ${PROJECT_SOURCE_DIR}/deps/clipper 17 | ) 18 | 19 | add_executable(nc_mill_roughing mill_roughing.cpp ../print_exception.cpp) 20 | target_link_libraries(nc_mill_roughing 21 | ${CMAKE_THREAD_LIBS_INIT} 22 | ${Boost_LIBRARIES} 23 | ${LUA_LIBRARIES} 24 | nc_base 25 | geom 26 | polyclipping 27 | ) 28 | -------------------------------------------------------------------------------- /src/nc_model/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Threads REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 8 | FIND_PACKAGE(Lua REQUIRED) 9 | 10 | include_directories( 11 | ${Boost_INCLUDE_DIRS} 12 | ${LUA_INCLUDE_DIR} 13 | ${PROJECT_SOURCE_DIR}/src 14 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 15 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 16 | ${PROJECT_SOURCE_DIR}/deps/geom/include 17 | ) 18 | 19 | add_executable(nc_model model.cpp rs274_model.cpp ../Simulation.cpp ../Tool.cpp ../Stock.cpp ../print_exception.cpp) 20 | target_link_libraries(nc_model 21 | ${CMAKE_THREAD_LIBS_INIT} 22 | ${Boost_LIBRARIES} 23 | ${LUA_LIBRARIES} 24 | rs274ngc 25 | cxxcam 26 | geom 27 | nc_base 28 | ) 29 | -------------------------------------------------------------------------------- /src/nc_model/model.cpp: -------------------------------------------------------------------------------- 1 | #include "rs274_model.h" 2 | #include "rs274ngc_return.hh" 3 | #include 4 | #include "print_exception.h" 5 | #include "base/machine_config.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | namespace po = boost::program_options; 13 | 14 | int main(int argc, char* argv[]) { 15 | po::options_description options("nc_model"); 16 | std::vector args(argv, argv + argc); 17 | args.erase(begin(args)); 18 | 19 | options.add(machine_config::base_options()); 20 | options.add_options() 21 | ("help,h", "display this help and exit") 22 | ("stock", po::value()->required(), "Stock model file") 23 | ("tool", po::value(), "Default tool") 24 | ; 25 | 26 | try { 27 | po::variables_map vm; 28 | store(po::command_line_parser(args).options(options).run(), vm); 29 | 30 | if(vm.count("help")) { 31 | std::cout << options << "\n"; 32 | return 0; 33 | } 34 | notify(vm); 35 | 36 | rs274_model modeler(vm, vm["stock"].as()); 37 | 38 | if(vm.count("tool")) { 39 | std::stringstream s; 40 | s << "M06 T" << vm["tool"].as(); 41 | modeler.read(s.str().c_str()); 42 | modeler.execute(); 43 | } 44 | 45 | std::string line; 46 | while(std::getline(std::cin, line)) { 47 | int status; 48 | 49 | status = modeler.read(line.c_str()); 50 | if(status != RS274NGC_OK) { 51 | if(status != RS274NGC_EXECUTE_FINISH) { 52 | std::cerr << "Error reading line!: \n"; 53 | std::cerr << line <<"\n"; 54 | return status; 55 | } 56 | } 57 | 58 | status = modeler.execute(); 59 | if(status != RS274NGC_OK) 60 | return status; 61 | std::cerr << line << "\n"; 62 | } 63 | 64 | std::cout << geom::format::off << modeler.model(); 65 | } catch(const po::error& e) { 66 | print_exception(e); 67 | std::cout << options << "\n"; 68 | return 1; 69 | } catch(const std::exception& e) { 70 | print_exception(e); 71 | return 1; 72 | } 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /src/nc_model/rs274_model.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_model.h 20 | * 21 | * Created on: 2015-05-22 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_MODEL_H_ 26 | #define RS274_MODEL_H_ 27 | #include "base/rs274_base.h" 28 | #include "cxxcam/Position.h" 29 | #include "geom/polyhedron.h" 30 | #include 31 | #include 32 | 33 | class rs274_model : public rs274_base 34 | { 35 | private: 36 | geom::polyhedron_t _model; 37 | geom::polyhedron_t _tool; 38 | std::vector _toolpath; 39 | unsigned _steps_per_revolution = 360; 40 | bool _lathe = false; 41 | 42 | virtual void _rapid(const Position& pos); 43 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation); 44 | virtual void _linear(const Position& pos); 45 | virtual void tool_change(int slot); 46 | virtual void dwell(double seconds); 47 | 48 | public: 49 | rs274_model(boost::program_options::variables_map& vm, const std::string& stock_filename); 50 | 51 | geom::polyhedron_t model(); 52 | 53 | virtual ~rs274_model() = default; 54 | }; 55 | 56 | #endif /* RS274_MODEL_H_ */ 57 | -------------------------------------------------------------------------------- /src/nc_offset_rotational_origin/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Threads REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 8 | FIND_PACKAGE(Lua REQUIRED) 9 | 10 | include_directories( 11 | ${Boost_INCLUDE_DIRS} 12 | ${LUA_INCLUDE_DIR} 13 | ${PROJECT_SOURCE_DIR}/src 14 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 15 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 16 | ) 17 | 18 | add_executable(nc_offset_rotational_origin offset_rotational_origin.cpp rs274_offset.cpp ../print_exception.cpp) 19 | target_link_libraries(nc_offset_rotational_origin 20 | ${CMAKE_THREAD_LIBS_INIT} 21 | ${Boost_LIBRARIES} 22 | ${LUA_LIBRARIES} 23 | rs274ngc 24 | nc_base 25 | cxxcam 26 | ) 27 | -------------------------------------------------------------------------------- /src/nc_offset_rotational_origin/offset_rotational_origin.cpp: -------------------------------------------------------------------------------- 1 | #include "rs274_offset.h" 2 | #include "rs274ngc_return.hh" 3 | #include 4 | #include "print_exception.h" 5 | #include "../throw_if.h" 6 | #include "base/machine_config.h" 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | namespace po = boost::program_options; 13 | 14 | int main(int argc, char* argv[]) { 15 | po::options_description options("nc_offset_rotational_origin"); 16 | std::vector args(argv, argv + argc); 17 | args.erase(begin(args)); 18 | 19 | options.add(machine_config::base_options()); 20 | options.add_options() 21 | ("help,h", "display this help and exit") 22 | ("from-tool,t", "From - Tool zero as rotational origin") 23 | ("from-x,x", po::value(), "From - X axis rotational origin") 24 | ("from-y,y", po::value(), "From - Y axis rotational origin") 25 | ("from-z,z", po::value(), "From - Z axis rotational origin") 26 | ("to-tool,T", "To - Tool zero as rotational origin") 27 | ("to-x,X", po::value(), "To - X axis rotational origin") 28 | ("to-y,Y", po::value(), "To - Y axis rotational origin") 29 | ("to-z,Z", po::value(), "To - Z axis rotational origin") 30 | ; 31 | 32 | try { 33 | po::variables_map vm; 34 | store(po::command_line_parser(args).options(options).run(), vm); 35 | 36 | if(vm.count("help")) { 37 | std::cout << options << "\n"; 38 | return 0; 39 | } 40 | notify(vm); 41 | 42 | rotational_origin from; 43 | rotational_origin to; 44 | 45 | if(vm.count("from-tool")) 46 | from = {}; 47 | else 48 | from = { vm["from-x"].as(), vm["from-y"].as(), vm["from-z"].as() }; 49 | 50 | if(vm.count("to-tool")) 51 | from = rotational_origin(); 52 | else 53 | from = { vm["to-x"].as(), vm["to-y"].as(), vm["to-z"].as() }; 54 | 55 | 56 | rs274_offset offset(vm, from, to); 57 | 58 | std::string line; 59 | while(std::getline(std::cin, line)) { 60 | int status; 61 | 62 | status = offset.read(line.c_str()); 63 | if(status != RS274NGC_OK) { 64 | if(status != RS274NGC_EXECUTE_FINISH) { 65 | std::cerr << "Error reading line!: \n"; 66 | return status; 67 | } 68 | } 69 | 70 | status = offset.execute(); 71 | if(status != RS274NGC_OK) 72 | return status; 73 | } 74 | 75 | } catch(const po::error& e) { 76 | print_exception(e); 77 | std::cout << options << "\n"; 78 | return 1; 79 | } catch(const std::exception& e) { 80 | print_exception(e); 81 | return 1; 82 | } 83 | 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /src/nc_offset_rotational_origin/rs274_offset.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_offset.cpp 20 | * 21 | * Created on: 2015-11-12 22 | * Author: nicholas 23 | */ 24 | 25 | #include "rs274_offset.h" 26 | #include 27 | #include 28 | #include 29 | 30 | std::string r6(double v) { 31 | std::ostringstream ss; 32 | ss << std::fixed << std::setprecision(6) << v; 33 | auto s = ss.str(); 34 | 35 | s.erase(s.find_last_not_of('0') + 1, std::string::npos); 36 | if(s.back() == '.') s.pop_back(); 37 | return s; 38 | } 39 | 40 | void rs274_offset::_rapid(const Position&) { 41 | } 42 | 43 | void rs274_offset::_arc(const Position&, const Position&, const cxxcam::math::vector_3&, int) { 44 | } 45 | 46 | 47 | void rs274_offset::_linear(const Position&) { 48 | } 49 | 50 | void rs274_offset::block_end(const block_t&) { 51 | char line[256]; 52 | line_text(line, 256); 53 | std::cout << line << "\n"; 54 | } 55 | 56 | rs274_offset::rs274_offset(boost::program_options::variables_map& vm, const rotational_origin& from, const rotational_origin& to) 57 | : rs274_base(vm), from_(from), to_(to) { 58 | } 59 | 60 | -------------------------------------------------------------------------------- /src/nc_offset_rotational_origin/rs274_offset.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_offset.h 20 | * 21 | * Created on: 2015-11-12 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_OFFSET_H_ 26 | #define RS274_OFFSET_H_ 27 | #include "base/rs274_base.h" 28 | #include "cxxcam/Bbox.h" 29 | 30 | /* allow ABC to be redefined around arbitary axes? 31 | * Or enforce AX BY CZ correlation 32 | * AXBYCZ requires only 3 points for axis center 33 | * 34 | * otherwise 3 vectors are required to define axis + center. 35 | * 36 | * initially enforce AXBYCZ 37 | * */ 38 | 39 | struct rotational_origin 40 | { 41 | bool tool; 42 | double x; 43 | double y; 44 | double z; 45 | 46 | rotational_origin() 47 | : tool(true) 48 | {} 49 | rotational_origin(double x, double y, double z) 50 | : tool(false), x(x), y(y), z(z) 51 | {} 52 | }; 53 | 54 | class rs274_offset : public rs274_base 55 | { 56 | private: 57 | rotational_origin from_; 58 | rotational_origin to_; 59 | 60 | virtual void _rapid(const Position& pos); 61 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation); 62 | virtual void _linear(const Position& pos); 63 | virtual void block_end(const block_t& block); 64 | 65 | public: 66 | rs274_offset(boost::program_options::variables_map& vm, const rotational_origin& from, const rotational_origin& to); 67 | 68 | virtual ~rs274_offset() = default; 69 | }; 70 | 71 | #endif /* RS274_OFFSET_H_ */ 72 | -------------------------------------------------------------------------------- /src/nc_rename_axis/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Threads REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 8 | FIND_PACKAGE(Lua REQUIRED) 9 | 10 | include_directories( 11 | ${Boost_INCLUDE_DIRS} 12 | ${LUA_INCLUDE_DIR} 13 | ${PROJECT_SOURCE_DIR}/src 14 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 15 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 16 | ) 17 | 18 | add_executable(nc_rename_axis rename_axis.cpp rs274_rename.cpp ../print_exception.cpp) 19 | target_link_libraries(nc_rename_axis 20 | ${CMAKE_THREAD_LIBS_INIT} 21 | ${Boost_LIBRARIES} 22 | ${LUA_LIBRARIES} 23 | rs274ngc 24 | nc_base 25 | cxxcam 26 | ) 27 | -------------------------------------------------------------------------------- /src/nc_rename_axis/rename_axis.cpp: -------------------------------------------------------------------------------- 1 | #include "rs274_rename.h" 2 | #include "rs274ngc_return.hh" 3 | #include 4 | #include "print_exception.h" 5 | #include "../throw_if.h" 6 | #include "base/machine_config.h" 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | namespace po = boost::program_options; 13 | 14 | int main(int argc, char* argv[]) { 15 | po::options_description options("nc_rename_axis"); 16 | std::vector args(argv, argv + argc); 17 | args.erase(begin(args)); 18 | 19 | options.add(machine_config::base_options()); 20 | options.add_options() 21 | ("help,h", "display this help and exit") 22 | ("delete,d", po::value(), "delete axis [XYZABC]") 23 | ("swap,s", po::value(), "swap axes [XYZABC][XYZABC]") 24 | ; 25 | 26 | try { 27 | auto parsed = po::command_line_parser(args).options(options).run(); 28 | 29 | po::variables_map vm; 30 | store(parsed, vm); 31 | 32 | if(vm.count("help")) { 33 | std::cout << options << "\n"; 34 | return 0; 35 | } 36 | 37 | std::vector mods; 38 | auto is_none = [](AxisModification::Axis a) { return a == AxisModification::axis_None; }; 39 | for (auto& option : parsed.options) { 40 | if (option.string_key == "delete") { 41 | auto value = boost::lexical_cast(option.value[0]); 42 | if(is_none(value.from) || !is_none(value.to)) 43 | throw std::runtime_error("Invalid axis specification for delete option"); 44 | mods.push_back(value); 45 | } else if (option.string_key == "swap") { 46 | auto value = boost::lexical_cast(option.value[0]); 47 | if(is_none(value.from) || is_none(value.to)) 48 | throw std::runtime_error("Invalid axis specification for swap option"); 49 | mods.push_back(value); 50 | } 51 | } 52 | 53 | rs274_rename rename(vm, mods); 54 | 55 | std::string line; 56 | while(std::getline(std::cin, line)) { 57 | int status; 58 | 59 | status = rename.read(line.c_str()); 60 | if(status != RS274NGC_OK) { 61 | if(status != RS274NGC_EXECUTE_FINISH) { 62 | std::cerr << "Error reading line!: \n"; 63 | std::cout << line <<"\n"; 64 | return status; 65 | } 66 | } 67 | 68 | status = rename.execute(); 69 | if(status != RS274NGC_OK) 70 | return status; 71 | } 72 | 73 | } catch(const po::error& e) { 74 | print_exception(e); 75 | std::cout << options << "\n"; 76 | return 1; 77 | } catch(const std::exception& e) { 78 | print_exception(e); 79 | return 1; 80 | } 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /src/nc_rename_axis/rs274_rename.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_rename.cpp 20 | * 21 | * Created on: 2016-04-01 22 | * Author: nicholas 23 | */ 24 | 25 | #include "rs274_rename.h" 26 | #include 27 | #include 28 | 29 | AxisModification::Axis AxisModification::map(char c) { 30 | switch(c) { 31 | case 'x': 32 | case 'X': 33 | return axis_X; 34 | case 'y': 35 | case 'Y': 36 | return axis_Y; 37 | case 'z': 38 | case 'Z': 39 | return axis_Z; 40 | case 'a': 41 | case 'A': 42 | return axis_A; 43 | case 'b': 44 | case 'B': 45 | return axis_B; 46 | case 'c': 47 | case 'C': 48 | return axis_C; 49 | case 'i': 50 | case 'I': 51 | return axis_I; 52 | case 'j': 53 | case 'J': 54 | return axis_J; 55 | case 'k': 56 | case 'K': 57 | return axis_K; 58 | case '-': 59 | return axis_None; 60 | } 61 | throw std::runtime_error("Unrecognised axis"); 62 | } 63 | 64 | void apply_mod(AxisModification a, block_t& block) { 65 | maybe empty; 66 | auto axis = [&block, &empty](AxisModification::Axis a) -> maybe& { 67 | switch (a) { 68 | case AxisModification::axis_None: 69 | return empty; 70 | 71 | case AxisModification::axis_X: 72 | return block.x; 73 | case AxisModification::axis_Y: 74 | return block.y; 75 | case AxisModification::axis_Z: 76 | return block.z; 77 | 78 | case AxisModification::axis_A: 79 | return block.a; 80 | case AxisModification::axis_B: 81 | return block.b; 82 | case AxisModification::axis_C: 83 | return block.c; 84 | 85 | case AxisModification::axis_I: 86 | return block.i; 87 | case AxisModification::axis_J: 88 | return block.j; 89 | case AxisModification::axis_K: 90 | return block.k; 91 | } 92 | throw std::logic_error("Unrecognised axis"); 93 | }; 94 | 95 | using std::swap; 96 | swap(axis(a.from), axis(a.to)); 97 | } 98 | void rs274_rename::apply_mods(block_t& block) const { 99 | for (auto& mod : mods) 100 | apply_mod(mod, block); 101 | } 102 | 103 | void rs274_rename::_rapid(const Position&) { 104 | } 105 | 106 | void rs274_rename::_arc(const Position&, const Position&, const cxxcam::math::vector_3&, int) { 107 | } 108 | 109 | 110 | void rs274_rename::_linear(const Position&) { 111 | } 112 | 113 | void rs274_rename::block_end(const block_t& b) { 114 | using std::swap; 115 | block_t block = b; 116 | 117 | enum 118 | { 119 | G_0 = 0, 120 | G_1 = 10, 121 | G_2 = 20, 122 | G_3 = 30, 123 | G_83 = 830 124 | }; 125 | auto is_motion = [](int motion_to_be){ 126 | return motion_to_be == G_0 || 127 | motion_to_be == G_1 || 128 | motion_to_be == G_2 || 129 | motion_to_be == G_3 || 130 | motion_to_be == G_83; 131 | }; 132 | 133 | if (is_motion(block.motion_to_be)) 134 | apply_mods(block); 135 | 136 | // TODO validate block is still valid after mods - e.g. no axis words with movement 137 | // must maintain non-axis words, e.g. feedrate 138 | 139 | std::cout << str(block) << "\n"; 140 | } 141 | rs274_rename::rs274_rename(boost::program_options::variables_map& vm, const std::vector& mods) 142 | : rs274_base(vm), mods(mods) { 143 | } 144 | 145 | -------------------------------------------------------------------------------- /src/nc_rename_axis/rs274_rename.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_rename.h 20 | * 21 | * Created on: 2016-04-01 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_RENAME_H_ 26 | #define RS274_RENAME_H_ 27 | #include "base/rs274_base.h" 28 | #include 29 | #include 30 | 31 | struct AxisModification 32 | { 33 | enum Axis 34 | { 35 | axis_None, 36 | axis_X, 37 | axis_Y, 38 | axis_Z, 39 | axis_A, 40 | axis_B, 41 | axis_C, 42 | axis_I, 43 | axis_J, 44 | axis_K, 45 | }; 46 | static Axis map(char c); 47 | 48 | Axis from = axis_None; 49 | Axis to = axis_None; 50 | }; 51 | inline std::istream& operator>>(std::istream& is, AxisModification& a) 52 | { 53 | char c; 54 | if(is >> c) a.from = AxisModification::map(c); 55 | if(is.peek() != EOF && is >> c) a.to = AxisModification::map(c); 56 | return is; 57 | } 58 | 59 | class rs274_rename : public rs274_base 60 | { 61 | private: 62 | std::vector mods; 63 | void apply_mods(block_t& block) const; 64 | 65 | virtual void _rapid(const Position& pos); 66 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation); 67 | virtual void _linear(const Position& pos); 68 | 69 | virtual void block_end(const block_t& block); 70 | 71 | public: 72 | rs274_rename(boost::program_options::variables_map& vm, const std::vector& mods); 73 | 74 | virtual ~rs274_rename() = default; 75 | }; 76 | 77 | #endif /* RS274_RENAME_H_ */ 78 | -------------------------------------------------------------------------------- /src/nc_shortlines/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Threads REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 8 | FIND_PACKAGE(Lua REQUIRED) 9 | 10 | include_directories( 11 | ${Boost_INCLUDE_DIRS} 12 | ${LUA_INCLUDE_DIR} 13 | ${PROJECT_SOURCE_DIR}/src 14 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 15 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 16 | ) 17 | 18 | add_executable(nc_shortlines shortlines.cpp rs274_shortlines.cpp ../print_exception.cpp) 19 | target_link_libraries(nc_shortlines 20 | ${CMAKE_THREAD_LIBS_INIT} 21 | ${Boost_LIBRARIES} 22 | ${LUA_LIBRARIES} 23 | rs274ngc 24 | nc_base 25 | ) 26 | -------------------------------------------------------------------------------- /src/nc_shortlines/rs274_shortlines.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_shortlines.cpp 20 | * 21 | * Created on: 2016-04-06 22 | * Author: nicholas 23 | */ 24 | 25 | #include "rs274_shortlines.h" 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include "cxxcam/Path.h" 32 | #include "../throw_if.h" 33 | #include "../r6.h" 34 | #include 35 | #include "base/machine_config.h" 36 | 37 | void rs274_shortlines::_rapid(const Position& pos) { 38 | using namespace cxxcam::path; 39 | 40 | auto steps = expand_linear(convert(program_pos), convert(pos), {}, 10).path; 41 | for (auto& step : steps) { 42 | auto& p = step.position; 43 | output_point(p, true); 44 | } 45 | } 46 | 47 | void rs274_shortlines::_arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation) { 48 | using namespace cxxcam::path; 49 | 50 | auto steps = expand_arc(convert(program_pos), convert(end), convert(center), (rotation < 0 ? ArcDirection::Clockwise : ArcDirection::CounterClockwise), plane, std::abs(rotation), {}, 10).path; 51 | for (auto& step : steps) { 52 | auto& p = step.position; 53 | output_point(p, false); 54 | } 55 | } 56 | 57 | 58 | void rs274_shortlines::_linear(const Position& pos) { 59 | using namespace cxxcam::path; 60 | 61 | auto steps = expand_linear(convert(program_pos), convert(pos), {}, 10).path; 62 | for (auto& step : steps) { 63 | auto& p = step.position; 64 | output_point(p, false); 65 | } 66 | } 67 | 68 | void rs274_shortlines::output_point(const cxxcam::math::point_3& p, bool rapid) const { 69 | using cxxcam::units::length_mm; 70 | using cxxcam::units::length_inch; 71 | 72 | if (rapid) 73 | std::cout << "G0"; 74 | else 75 | std::cout << "G1"; 76 | 77 | switch (machine_config::machine_units(config, machine_id)) { 78 | case machine_config::units::metric: 79 | std::cout << " X" << r6(length_mm(p.x).value()); 80 | std::cout << " Y" << r6(length_mm(p.y).value()); 81 | std::cout << " Z" << r6(length_mm(p.z).value()); 82 | break; 83 | case machine_config::units::imperial: 84 | std::cout << " X" << r6(length_inch(p.x).value()); 85 | std::cout << " Y" << r6(length_inch(p.y).value()); 86 | std::cout << " Z" << r6(length_inch(p.z).value()); 87 | break; 88 | default: 89 | throw std::logic_error("Unhandled units"); 90 | } 91 | 92 | if (!rapid) 93 | std::cout << " F" << _feed_rate; 94 | std::cout << "\n"; 95 | } 96 | 97 | rs274_shortlines::rs274_shortlines(boost::program_options::variables_map& vm) 98 | : rs274_base(vm) { 99 | } 100 | 101 | -------------------------------------------------------------------------------- /src/nc_shortlines/rs274_shortlines.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_shortlines.h 20 | * 21 | * Created on: 2016-04-06 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_SHORTLINES_H_ 26 | #define RS274_SHORTLINES_H_ 27 | #include "base/rs274_base.h" 28 | 29 | class rs274_shortlines : public rs274_base 30 | { 31 | private: 32 | virtual void _rapid(const Position& pos); 33 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation); 34 | virtual void _linear(const Position& pos); 35 | 36 | void output_point(const cxxcam::math::point_3& p, bool rapid) const; 37 | 38 | public: 39 | rs274_shortlines(boost::program_options::variables_map& vm); 40 | virtual ~rs274_shortlines() = default; 41 | }; 42 | 43 | #endif /* RS274_SHORTLINES_H_ */ 44 | -------------------------------------------------------------------------------- /src/nc_shortlines/shortlines.cpp: -------------------------------------------------------------------------------- 1 | #include "rs274_shortlines.h" 2 | #include "rs274ngc_return.hh" 3 | #include 4 | #include "print_exception.h" 5 | #include "base/machine_config.h" 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | namespace po = boost::program_options; 12 | 13 | int main(int argc, char* argv[]) { 14 | po::options_description options("nc_shortlines"); 15 | std::vector args(argv, argv + argc); 16 | args.erase(begin(args)); 17 | 18 | options.add(machine_config::base_options()); 19 | options.add_options() 20 | ("help,h", "display this help and exit") 21 | ; 22 | 23 | try { 24 | po::variables_map vm; 25 | store(po::command_line_parser(args).options(options).run(), vm); 26 | 27 | if(vm.count("help")) { 28 | std::cout << options << "\n"; 29 | return 0; 30 | } 31 | notify(vm); 32 | 33 | rs274_shortlines shortlines(vm); 34 | 35 | std::string line; 36 | while(std::getline(std::cin, line)) { 37 | int status; 38 | 39 | status = shortlines.read(line.c_str()); 40 | if(status != RS274NGC_OK) { 41 | if(status != RS274NGC_EXECUTE_FINISH) { 42 | std::cerr << "Error reading line!: \n"; 43 | return status; 44 | } 45 | } 46 | 47 | status = shortlines.execute(); 48 | if(status != RS274NGC_OK) 49 | return status; 50 | } 51 | } catch(const po::error& e) { 52 | print_exception(e); 53 | std::cout << options << "\n"; 54 | return 1; 55 | } catch(const std::exception& e) { 56 | print_exception(e); 57 | return 1; 58 | } 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /src/nc_slice/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Threads REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 8 | FIND_PACKAGE(Lua REQUIRED) 9 | 10 | include_directories( 11 | ${Boost_INCLUDE_DIRS} 12 | ${LUA_INCLUDE_DIR} 13 | ${PROJECT_SOURCE_DIR}/src 14 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 15 | ${PROJECT_SOURCE_DIR}/deps/geom/include 16 | ${PROJECT_SOURCE_DIR}/deps/clipper 17 | ) 18 | 19 | add_executable(nc_slice slice.cpp ../print_exception.cpp) 20 | target_link_libraries(nc_slice 21 | ${CMAKE_THREAD_LIBS_INIT} 22 | ${Boost_LIBRARIES} 23 | ${LUA_LIBRARIES} 24 | nc_base 25 | geom 26 | polyclipping 27 | ) 28 | -------------------------------------------------------------------------------- /src/nc_slice/slice.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "print_exception.h" 3 | #include "geom/polyhedron.h" 4 | #include "geom/query.h" 5 | #include "geom/primitives.h" 6 | #include "geom/ops.h" 7 | #include "../throw_if.h" 8 | #include "../r6.h" 9 | #include "base/machine_config.h" 10 | #include "clipper.hpp" 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | namespace po = boost::program_options; 19 | namespace cl = ClipperLib; 20 | 21 | int main(int argc, char* argv[]) { 22 | po::options_description options("nc_slice"); 23 | std::vector args(argv, argv + argc); 24 | args.erase(begin(args)); 25 | 26 | options.add(machine_config::base_options()); 27 | options.add_options() 28 | ("help,h", "display this help and exit") 29 | ("feedrate,f", po::value()->required(), "Feed rate") 30 | ("stepdown,d", po::value()->required(), "Z Stepdown") 31 | ("stock", po::value(), "Stock model file") 32 | ("offset,o", po::value(), "Generate stock via offset") 33 | ("nonmonotonic", "Allow slices to be non-monotonic") 34 | ; 35 | 36 | try { 37 | po::variables_map vm; 38 | store(po::command_line_parser(args).options(options).run(), vm); 39 | 40 | if(vm.count("help")) { 41 | std::cout << options << "\n"; 42 | return 0; 43 | } 44 | notify(vm); 45 | 46 | double stepdown = vm["stepdown"].as(); 47 | double f = vm["feedrate"].as(); 48 | bool monotonic = vm.count("nonmonotonic") == 0; 49 | 50 | double scale = 10e12; 51 | auto scale_point = [scale] (const geom::polygon_t::point& point){ 52 | return cl::IntPoint(point.x * scale, point.y * scale); 53 | }; 54 | auto unscale_point = [scale](const cl::IntPoint& p) -> geom::polygon_t::point { 55 | return {static_cast(p.X)/scale, static_cast(p.Y)/scale}; 56 | }; 57 | auto scale_paths = [scale_point] (const std::vector& polygons) { 58 | cl::Paths paths; 59 | for (auto& polygon : polygons) { 60 | for (auto& poly : polygon.polygons) { 61 | paths.emplace_back(); 62 | for (auto& p : poly.points) 63 | paths.back().push_back(scale_point(p)); 64 | } 65 | } 66 | return paths; 67 | }; 68 | 69 | geom::polyhedron_t model; 70 | throw_if(!(std::cin >> geom::format::off >> model), "Unable to read model from file"); 71 | 72 | geom::polyhedron_t stock; 73 | bool has_stock_file = vm.count("stock"); 74 | if (has_stock_file) { 75 | std::ifstream is(vm["stock"].as()); 76 | throw_if(!(is >> geom::format::off >> stock), "Unable to read stock from file"); 77 | } 78 | 79 | cl::Paths stock_offset; 80 | bool has_stock_offset = vm.count("offset"); 81 | if (has_stock_offset) 82 | { 83 | stock_offset = scale_paths(geom::projection_xy(model)); 84 | 85 | cl::ClipperOffset co; 86 | co.AddPaths(stock_offset, cl::jtRound, cl::etClosedPolygon); 87 | co.ArcTolerance = 0.1 * scale; 88 | co.Execute(stock_offset, vm["offset"].as() * scale); 89 | } 90 | 91 | auto bbox = geom::bounding_box(model); 92 | double model_z = bbox.max.z - bbox.min.z; 93 | 94 | const unsigned n_steps = [&] { 95 | unsigned steps = std::abs(std::ceil(model_z / stepdown)); 96 | return steps == 0 ? 1 : steps; 97 | }(); 98 | const double step_z = model_z / n_steps; 99 | 100 | double z0 = bbox.max.z; 101 | for (unsigned step = 0; step < n_steps; ++step, z0 -= step_z) { 102 | double slice_z0 = monotonic ? bbox.max.z : z0; 103 | double z1 = z0 - step_z; 104 | 105 | auto slice_bounds = geom::make_box({bbox.min.x, bbox.min.y, z1}, {bbox.max.x, bbox.max.y, slice_z0}); 106 | auto slice = geom::projection_xy(model * slice_bounds); 107 | 108 | // If stock (model or offset) has been provided, calculate the slice of material to be removed 109 | if (has_stock_file || has_stock_offset) { 110 | 111 | cl::Clipper clpr; 112 | clpr.AddPaths(scale_paths(slice), cl::ptClip, true); 113 | 114 | if (has_stock_file) { 115 | auto stock_slice = geom::projection_xy(stock * slice_bounds); 116 | clpr.AddPaths(scale_paths(stock_slice), cl::ptSubject, true); 117 | } 118 | else if (has_stock_offset) { 119 | clpr.AddPaths(stock_offset, cl::ptSubject, true); 120 | } 121 | 122 | cl::Paths solution; 123 | clpr.Execute(cl::ctDifference, solution); 124 | 125 | slice.clear(); 126 | for (auto path : solution) { 127 | geom::polygon_tree_t poly; 128 | poly.polygons.emplace_back(); 129 | for (auto point : path) { 130 | poly.polygons.back().points.push_back(unscale_point(point)); 131 | } 132 | slice.push_back(poly); 133 | } 134 | } 135 | 136 | auto output_slice = [&](const geom::polygon_t& path) { 137 | bool rapid_to_first = true; 138 | for (auto& p : path.points) { 139 | if (rapid_to_first) { 140 | std::cout << "G00 X" << r6(p.x) << " Y" << r6(p.y) << " Z" << r6(z1) << "\n"; 141 | rapid_to_first = false; 142 | } 143 | std::cout << "G01 X" << r6(p.x) << " Y" << r6(p.y) << " Z" << r6(z1) << " F" << r6(f) << "\n"; 144 | } 145 | std::cout << "G01 X" << r6(path.points.front().x) << " Y" << r6(path.points.front().y) << " Z" << r6(z1) << " F" << r6(f) << "\n"; 146 | }; 147 | 148 | for (auto& polygon : slice) 149 | { 150 | for (auto& p : polygon.polygons) 151 | output_slice(p); 152 | } 153 | } 154 | 155 | } catch(const po::error& e) { 156 | print_exception(e); 157 | std::cout << options << "\n"; 158 | return 1; 159 | } catch(const std::exception& e) { 160 | print_exception(e); 161 | return 1; 162 | } 163 | 164 | return 0; 165 | } 166 | -------------------------------------------------------------------------------- /src/nc_stock/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 7 | 8 | include_directories( 9 | ${Boost_INCLUDE_DIRS} 10 | ${PROJECT_SOURCE_DIR}/src 11 | ${PROJECT_SOURCE_DIR}/deps/geom/include 12 | ) 13 | 14 | add_executable(nc_stock stock.cpp ../print_exception.cpp) 15 | target_link_libraries(nc_stock 16 | ${Boost_LIBRARIES} 17 | geom 18 | ) 19 | -------------------------------------------------------------------------------- /src/nc_stock/stock.cpp: -------------------------------------------------------------------------------- 1 | #include "geom/polyhedron.h" 2 | #include "geom/primitives.h" 3 | #include 4 | #include 5 | #include "print_exception.h" 6 | #include 7 | #include 8 | #include "../throw_if.h" 9 | 10 | namespace po = boost::program_options; 11 | 12 | int main(int argc, char* argv[]) { 13 | po::options_description options("nc_stock"); 14 | std::vector args(argv, argv + argc); 15 | args.erase(begin(args)); 16 | 17 | options.add_options() 18 | ("help,h", "display this help and exit") 19 | ("box", "Box stock shape") 20 | ("cylinder", "Cylinder stock shape") 21 | ("x0,X", po::value()->default_value(0.0), "X0 dimension") 22 | ("y0,Y", po::value()->default_value(0.0), "Y0 dimension") 23 | ("z0,Z", po::value()->default_value(0.0), "Z0 dimension") 24 | ("x1,x", po::value()->required(), "X1 dimension") 25 | ("y1,y", po::value()->required(), "Y1 dimension") 26 | ("z1,z", po::value()->required(), "Z1 dimension") 27 | ("radius,r", po::value(), "Cylinder radius") 28 | ("segments,S", po::value()->default_value(64), "Cylinder segments") 29 | ; 30 | 31 | try { 32 | po::variables_map vm; 33 | store(po::command_line_parser(args).options(options).run(), vm); 34 | 35 | if(vm.count("help")) { 36 | std::cout << options << "\n"; 37 | return 0; 38 | } 39 | notify(vm); 40 | 41 | if(vm.count("box")) { 42 | double x0 = vm["x0"].as(); 43 | double y0 = vm["y0"].as(); 44 | double z0 = vm["z0"].as(); 45 | double x1 = vm["x1"].as(); 46 | double y1 = vm["y1"].as(); 47 | double z1 = vm["z1"].as(); 48 | auto stock = geom::make_box({x0, y0, z0}, {x1, y1, z1}); 49 | std::cout << geom::format::off << stock; 50 | } else if(vm.count("cylinder")) { 51 | throw_if(!vm.count("radius"), "radius required for cylinder stock"); 52 | 53 | double x0 = vm["x0"].as(); 54 | double y0 = vm["y0"].as(); 55 | double z0 = vm["z0"].as(); 56 | double x1 = vm["x1"].as(); 57 | double y1 = vm["y1"].as(); 58 | double z1 = vm["z1"].as(); 59 | double r = vm["radius"].as(); 60 | double s = vm["segments"].as(); 61 | auto stock = geom::make_cone({x0, y0, z0}, {x1, y1, z1}, r, r, s); 62 | std::cout << geom::format::off << stock; 63 | } else { 64 | std::cerr << "must specify shape\n"; 65 | return 1; 66 | } 67 | } catch(const po::error& e) { 68 | print_exception(e); 69 | std::cout << options << "\n"; 70 | return 1; 71 | } catch(const std::exception& e) { 72 | print_exception(e); 73 | return 1; 74 | } 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /src/nc_svgpath/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 7 | 8 | include_directories( 9 | ${Boost_INCLUDE_DIRS} 10 | ${PROJECT_SOURCE_DIR}/src 11 | ${PROJECT_SOURCE_DIR}/deps/svg_path/include 12 | ) 13 | 14 | add_executable(nc_svgpath svgpath.cpp ../print_exception.cpp) 15 | target_link_libraries(nc_svgpath 16 | ${Boost_LIBRARIES} 17 | svg_path 18 | ) 19 | -------------------------------------------------------------------------------- /src/nc_tooltable/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Threads REQUIRED) 7 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 8 | FIND_PACKAGE(Lua REQUIRED) 9 | 10 | include_directories( 11 | ${Boost_INCLUDE_DIRS} 12 | ${LUA_INCLUDE_DIR} 13 | ${PROJECT_SOURCE_DIR}/src 14 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 15 | ) 16 | 17 | add_executable(nc_tooltable tools.cpp ../print_exception.cpp) 18 | target_link_libraries(nc_tooltable 19 | ${CMAKE_THREAD_LIBS_INIT} 20 | ${Boost_LIBRARIES} 21 | ${LUA_LIBRARIES} 22 | cxxcam 23 | nc_base 24 | ) 25 | -------------------------------------------------------------------------------- /src/nc_tooltable/tools.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "print_exception.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "cxxcam/GCodeLine.h" 10 | #include 11 | #include 12 | #include "base/nc_config.h" 13 | #include "base/machine_config.h" 14 | 15 | namespace po = boost::program_options; 16 | 17 | /* http://wiki.linuxcnc.org/cgi-bin/wiki.pl?ToolTable 18 | * ; - opening semicolon, no data following 19 | * T - tool number, 0-99999 (you can have a large number of tools in inventory) 20 | * P - pocket number, 1-99999 (tool table has a lower number of entries, currently 56.) 21 | * X..W - tool offset on specified axis - floating-point 22 | * D - tool diameter - floating-point 23 | * I - front angle (lathe only) - floating-point 24 | * J - back angle (lathe only) - floating-point 25 | * Q - tool orientation (lathe only) - integer, 0-9 26 | * ; - beginning of comment or remark - text */ 27 | int main(int argc, char* argv[]) { 28 | po::options_description options("nc_tooltable"); 29 | std::vector args(argv, argv + argc); 30 | args.erase(begin(args)); 31 | 32 | options.add(machine_config::base_options()); 33 | options.add_options() 34 | ("help,h", "display this help and exit") 35 | ; 36 | 37 | try { 38 | po::variables_map vm; 39 | store(po::command_line_parser(args).options(options).run(), vm); 40 | 41 | if(vm.count("help")) { 42 | std::cout << options << "\n"; 43 | return 0; 44 | } 45 | notify(vm); 46 | 47 | nc_config config(vm["config"].as()); 48 | std::string machine_id; 49 | if (vm.count("machine")) 50 | machine_id = vm["machine"].as(); 51 | else 52 | machine_id = machine_config::default_machine(config); 53 | auto type = machine_config::get_machine_type(config, machine_id); 54 | 55 | std::cout << ";\n"; 56 | for(unsigned slot = 0; slot < 56; ++slot) { 57 | using namespace cxxcam::gcode; 58 | using namespace machine_config; 59 | 60 | Line line; 61 | line += {Word::T, static_cast(slot)}; 62 | line += {Word::P, static_cast(slot)}; 63 | 64 | switch (type) { 65 | case machine_type::mill: { 66 | mill_tool tool; 67 | if (!get_tool(config, slot, machine_id, tool)) 68 | continue; 69 | 70 | line += {Word::D, tool.diameter}; 71 | line.Comment(tool.name); 72 | break; 73 | } 74 | case machine_type::lathe: { 75 | // TODO 76 | break; 77 | } 78 | } 79 | 80 | std::copy(line.begin(), line.end(), std::ostream_iterator(std::cout, " ")); 81 | if(!line.Comment().empty()) 82 | std::cout << "; " << line.Comment(); 83 | std::cout << "\n"; 84 | } 85 | 86 | } catch(const po::error& e) { 87 | print_exception(e); 88 | std::cout << options << "\n"; 89 | return 1; 90 | } catch(const std::exception& e) { 91 | print_exception(e); 92 | return 1; 93 | } 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /src/nc_transform/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF(UNIX) 3 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 | ENDIF() 5 | 6 | FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED) 7 | FIND_PACKAGE(Lua REQUIRED) 8 | 9 | include_directories( 10 | ${Boost_INCLUDE_DIRS} 11 | ${LUA_INCLUDE_DIR} 12 | ${PROJECT_SOURCE_DIR}/src 13 | ${PROJECT_SOURCE_DIR}/deps/rs274ngc/include 14 | ${PROJECT_SOURCE_DIR}/deps/geom/include 15 | ${PROJECT_SOURCE_DIR}/deps/cxxcam/include 16 | ) 17 | 18 | add_executable(nc_transform transform.cpp rs274_transform.cpp ../print_exception.cpp) 19 | target_link_libraries(nc_transform 20 | ${Boost_LIBRARIES} 21 | ${LUA_LIBRARIES} 22 | rs274ngc 23 | nc_base 24 | geom 25 | cxxcam 26 | ) 27 | -------------------------------------------------------------------------------- /src/nc_transform/rs274_transform.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_transform.cpp 20 | * 21 | * Created on: 2016-07-23 22 | * Author: nicholas 23 | */ 24 | 25 | #include "rs274_transform.h" 26 | #include 27 | 28 | void rs274_transform::_rapid(const Position&) { 29 | } 30 | 31 | void rs274_transform::_arc(const Position&, const Position&, const cxxcam::math::vector_3&, int) { 32 | } 33 | 34 | 35 | void rs274_transform::_linear(const Position&) { 36 | } 37 | 38 | rs274_transform::rs274_transform(boost::program_options::variables_map& vm) 39 | : rs274_base(vm) { 40 | } 41 | 42 | -------------------------------------------------------------------------------- /src/nc_transform/rs274_transform.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * rs274_transform.h 20 | * 21 | * Created on: 2016-07-23 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef RS274_TRANSFORM_H_ 26 | #define RS274_TRANSFORM_H_ 27 | #include "base/rs274_base.h" 28 | 29 | class rs274_transform : public rs274_base 30 | { 31 | private: 32 | 33 | virtual void _rapid(const Position& pos); 34 | virtual void _arc(const Position& end, const Position& center, const cxxcam::math::vector_3& plane, int rotation); 35 | virtual void _linear(const Position& pos); 36 | 37 | public: 38 | rs274_transform(boost::program_options::variables_map& vm); 39 | 40 | virtual ~rs274_transform() = default; 41 | }; 42 | 43 | #endif /* RS274_TRANSFORM_H_ */ 44 | -------------------------------------------------------------------------------- /src/nc_transform/transform.cpp: -------------------------------------------------------------------------------- 1 | #include "geom/polyhedron.h" 2 | #include "geom/translate.h" 3 | #include 4 | #include 5 | #include "print_exception.h" 6 | #include 7 | #include 8 | #include "../throw_if.h" 9 | #include "cxxcam/Math.h" 10 | #include "rs274_transform.h" 11 | 12 | namespace po = boost::program_options; 13 | using namespace cxxcam; 14 | 15 | // TODO add options to align by bounding box, e.g. top at z0, xy0 at center or corners 16 | int main(int argc, char* argv[]) { 17 | po::options_description options("nc_transform"); 18 | std::vector args(argv, argv + argc); 19 | args.erase(begin(args)); 20 | 21 | options.add_options() 22 | ("help,h", "display this help and exit") 23 | ("translate_x,x", po::value(), "Translate along x axis") 24 | ("translate_y,y", po::value(), "Translate along y axis") 25 | ("translate_z,z", po::value(), "Translate along z axis") 26 | ("rotate_x,a", po::value(), "Rotate around x axis") 27 | ("rotate_y,b", po::value(), "Rotate around y axis") 28 | ("rotate_z,c", po::value(), "Rotate around z axis") 29 | ("model,m", "transform model") 30 | ; 31 | 32 | try { 33 | auto parsed = po::command_line_parser(args).options(options).run(); 34 | 35 | po::variables_map vm; 36 | store(parsed, vm); 37 | 38 | if(vm.count("help")) { 39 | std::cout << options << "\n"; 40 | return 0; 41 | } 42 | 43 | if (vm.count("model")) { 44 | geom::polyhedron_t model; 45 | throw_if(!(std::cin >> geom::format::off >> model), "Unable to read model from file"); 46 | 47 | // apply transformations in order 48 | for (auto& option : parsed.options) { 49 | if(option.string_key == "model") 50 | continue; 51 | 52 | auto value = boost::lexical_cast(option.value[0]); 53 | if(option.string_key == "translate_x") { 54 | model = geom::translate(model, value, 0, 0); 55 | } else if(option.string_key == "translate_y") { 56 | model = geom::translate(model, 0, value, 0); 57 | } else if(option.string_key == "translate_z") { 58 | model = geom::translate(model, 0, 0, value); 59 | } else if(option.string_key == "rotate_x") { 60 | auto theta = units::plane_angle{value * units::degrees}; 61 | auto q = math::normalise(math::axis2quat(1, 0, 0, theta)); 62 | model = geom::rotate(model, q.R_component_1(), q.R_component_2(), q.R_component_3(), q.R_component_4()); 63 | } else if(option.string_key == "rotate_y") { 64 | auto theta = units::plane_angle{value * units::degrees}; 65 | auto q = math::normalise(math::axis2quat(0, 1, 0, theta)); 66 | model = geom::rotate(model, q.R_component_1(), q.R_component_2(), q.R_component_3(), q.R_component_4()); 67 | } else if(option.string_key == "rotate_z") { 68 | auto theta = units::plane_angle{value * units::degrees}; 69 | auto q = math::normalise(math::axis2quat(0, 0, 1, theta)); 70 | model = geom::rotate(model, q.R_component_1(), q.R_component_2(), q.R_component_3(), q.R_component_4()); 71 | } 72 | } 73 | 74 | std::cout << model; 75 | } else { 76 | // TODO store transformations / rotations in list 77 | // create rs274 transformer with transformations, go. 78 | } 79 | 80 | } catch(const po::error& e) { 81 | print_exception(e); 82 | std::cout << options << "\n"; 83 | return 1; 84 | } catch(const std::exception& e) { 85 | print_exception(e); 86 | return 1; 87 | } 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /src/print_exception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void print_exception(const std::exception& e, int level) 4 | { 5 | std::cerr << std::string(level, ' ') << "error: " << e.what() << '\n'; 6 | try 7 | { 8 | std::rethrow_if_nested(e); 9 | } 10 | catch(const std::exception& e) 11 | { 12 | print_exception(e, level+1); 13 | } 14 | catch(...) 15 | { 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /src/print_exception.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void print_exception(const std::exception& e, int level = 0); 4 | -------------------------------------------------------------------------------- /src/r6.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * r6.h 20 | * 21 | * Created on: 2016-03-04 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef R6_H_ 26 | #define R6_H_ 27 | #include 28 | #include 29 | #include 30 | 31 | inline std::string r6(double v) { 32 | std::ostringstream ss; 33 | ss << std::fixed << std::setprecision(6) << v; 34 | auto s = ss.str(); 35 | 36 | s.erase(s.find_last_not_of('0') + 1, std::string::npos); 37 | if(s.back() == '.') s.pop_back(); 38 | return s; 39 | } 40 | 41 | #endif /* R6_H_ */ 42 | -------------------------------------------------------------------------------- /src/throw_if.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Nicholas Gill 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | * throw_if.h 20 | * 21 | * Created on: 2015-06-04 22 | * Author: nicholas 23 | */ 24 | 25 | #ifndef THROW_IF_H_ 26 | #define THROW_IF_H_ 27 | #include 28 | #include 29 | 30 | inline void throw_if(bool cond, const std::string& msg) { 31 | if(cond) 32 | throw std::runtime_error(msg); 33 | } 34 | 35 | #endif /* THROW_IF_H_ */ 36 | -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #ADD_TEST(${testname} "${MAKE_CURRENT_BINARY_DIR}/${testname}") 2 | --------------------------------------------------------------------------------