Created
June 19, 2016 20:07
-
-
Save koppi/3ae4e37422d78e46705d136a6dc41135 to your computer and use it in GitHub Desktop.
Revisions
-
koppi created this gist
Jun 19, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,153 @@ /* * Copyright 2016 Jakob Flierl (jakob.flierl "at" gmail.com) * * ngc-hole is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ngc-hole is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with stl2ngc. If not, see <http://www.gnu.org/licenses/>. */ // make LDLIBS+=`pkg-config --libs ImageMagick++` CXXFLAGS+="-std=gnu++14 `pkg-config --cflags ImageMagick++`" ngc-hole #include <Magick++.h> #include <iostream> #include <locale> #include <clocale> #include <stdarg.h> // For va_start, etc. #include <memory> // For std::unique_ptr #include <string.h> using namespace std; using namespace Magick; string fmt(const string fmt_str, ...) { int final_n, n = ((int)fmt_str.size()) * 2; string str; unique_ptr<char[]> formatted; va_list ap; while(1) { formatted.reset(new char[n]); strcpy(&formatted[0], fmt_str.c_str()); va_start(ap, fmt_str); final_n = vsnprintf(&formatted[0], n, fmt_str.c_str(), ap); va_end(ap); if (final_n < 0 || final_n >= n) n += abs(final_n - n + 1); else break; } return string(formatted.get()); } double mm2inch(double mm) { return mm / 2.54; } #define flt "%.4f" // drill a hole at x,y with zdepth and retract to zsafe afterwards // f is the z feedrate void hole(double x, double y, double ztop = 0.1, double zdepth = -1.0, double zsafe = 1.0, double f = 50.0) { cout << fmt("G0 X" flt " Y" flt, x, y) << endl; cout << fmt("Z" flt, ztop) << endl; cout << fmt("G1 Z" flt " F" flt, zdepth, f) << endl; cout << fmt("G0 Z" flt, zsafe) << endl; } // drill a hole for every colored pixel on the image void img2holes(int argc, char** argv) { Image image; InitializeMagick(*argv); image.read(argv[1]); // load the image file cout << "G20" << endl; // programming in inches //cout << "G21" << endl; // programming in millimeters (mm) //cout << "T01" << endl; // select tool nr 1 cout << "M03 S20000" << endl; // spindle on (clockwise rotation) int w = image.columns(); int h = image.rows(); int dir = 1; for (int i = 0; i <= w; ++i) { if (dir == 1) { for (int j = 0; j <= h; ++j) { ColorRGB rgb(image.pixelColor(i,h-j)); if (rgb.red() <= 0.5) { hole(i*0.1, j*0.1, mm2inch(0.1), -0.062, mm2inch(0.1)); } } dir = -1; } else { for (int j = h; j >= 0; --j) { ColorRGB rgb(image.pixelColor(i,h-j)); if (rgb.red() <= 0.5) { hole(i*0.1, j*0.1, mm2inch(0.1), -0.062, mm2inch(0.1)); } } dir = 1; } } cout << "M05" << endl; // spindle stop cout << fmt("G0 Z" flt, mm2inch(0.1)) << endl; cout << "G0 X0 Y0" << endl; cout << "M2" << endl; // LinuxCNC: end the program } void spiral(int argc, char** argv) { cout << "G20" << endl; // programming in inches //cout << "G21" << endl; // programming in millimeters (mm) //cout << "T01" << endl; // select tool nr 1 cout << "M03 S20000" << endl; // spindle on (clockwise rotation) // a hole in the center hole(0, 0, mm2inch(0.1), -0.062, mm2inch(0.1)); // some spirals for (int i = 0; i <= 3; ++i) { double off = i * M_PI / 2; for (double t = M_PI; t <= 10 * M_PI; t+=0.25) { double d = 0.05; hole(t*sin(t+off)*d, t*cos(t+off)*d, mm2inch(0.1), -0.062, mm2inch(0.1)); } } cout << "M05" << endl; // spindle stop cout << fmt("G0 Z" flt, mm2inch(0.1)) << endl; cout << "G0 X0 Y0" << endl; cout << "M2" << endl; // LinuxCNC: end the program } int main(int argc, char** argv) { // make sure, a dot is printed not comma as used in some locales setlocale(LC_ALL, ""); locale::global(locale("")); cout.imbue(locale()); // turn of scientific notation cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); // some functions spiral(argc, argv); //img2holes(argc, argv); return 0; }