From 95b198a5aaa3dfa6b20eab9a7d8b82d7dc14eafa Mon Sep 17 00:00:00 2001 From: jaseg Date: Mon, 25 Jan 2021 14:55:13 +0100 Subject: Gerber and SVG export working --- src/out_gerber.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/out_gerber.cpp (limited to 'src/out_gerber.cpp') diff --git a/src/out_gerber.cpp b/src/out_gerber.cpp new file mode 100644 index 0000000..c320184 --- /dev/null +++ b/src/out_gerber.cpp @@ -0,0 +1,99 @@ +/* + * This file is part of gerbolyze, a vector image preprocessing toolchain + * Copyright (C) 2021 Jan Sebastian Götte + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +using namespace gerbolyze; +using namespace std; + +SimpleGerberOutput::SimpleGerberOutput(ostream &out, bool only_polys, int digits_int, int digits_frac, d2p offset) + : StreamPolygonSink(out, only_polys), + m_digits_int(digits_int), + m_digits_frac(digits_frac), + m_offset(offset) +{ + assert(1 <= digits_int && digits_int <= 9); + assert(0 <= digits_frac && digits_frac <= 9); + m_gerber_scale = round(pow(10, m_digits_frac)); +} + +void SimpleGerberOutput::header_impl(d2p origin, d2p size) { + m_offset[0] += origin[0]; + m_offset[1] += origin[1]; + m_width = size[0] - origin[0]; + m_height = size[1] - origin[1]; + + if (pow(10, m_digits_int-1) < max(m_width, m_height)) { + cerr << "Warning: Input has bounding box too large for " << m_digits_int << "." << m_digits_frac << " gerber resolution!" << endl; + } + + m_out << "%FSLAX" << m_digits_int << m_digits_frac << "Y" << m_digits_int << m_digits_frac << "*%" << endl; + m_out << "%MOMM*%" << endl; + m_out << "%LPD*%" << endl; + m_out << "G01*" << endl; + m_out << "%ADD10C,0.050000*%" << endl; + m_out << "D10*" << endl; +} + +SimpleGerberOutput& SimpleGerberOutput::operator<<(GerberPolarityToken pol) { + if (pol == GRB_POL_DARK) { + m_out << "%LPD*%" << endl; + } else if (pol == GRB_POL_CLEAR) { + m_out << "%LPC*%" << endl; + } else { + assert(false); + } + + return *this; +} +SimpleGerberOutput& SimpleGerberOutput::operator<<(const Polygon &poly) { + if (poly.size() < 3) { + cerr << "Warning: " << poly.size() << "-element polygon passed to SimpleGerberOutput" << endl; + return *this; + } + + /* NOTE: Clipper and gerber both have different fixed-point scales. We get points in double mm. */ + double x = round((poly[0][0] + m_offset[0]) * m_gerber_scale); + double y = round((m_height - poly[0][1] + m_offset[1]) * m_gerber_scale); + m_out << "G36*" << endl; + m_out << "X" << setw(m_digits_int + m_digits_frac) << setfill('0') << (long long int)x + << "Y" << setw(m_digits_int + m_digits_frac) << setfill('0') << (long long int)y + << "D02*" << endl; + m_out << "G01*" << endl; + for (size_t i=1; i