diff options
Diffstat (limited to 'src/export.cc')
-rw-r--r-- | src/export.cc | 122 |
1 files changed, 55 insertions, 67 deletions
diff --git a/src/export.cc b/src/export.cc index 8e0ab16..1f9046e 100644 --- a/src/export.cc +++ b/src/export.cc @@ -30,6 +30,7 @@ #include <QApplication> #include <QProgressDialog> +#include <QTextStream> #include <errno.h> #ifdef ENABLE_CGAL @@ -72,9 +73,10 @@ void cgal_nef3_to_polyset(PolySet *ps, CGAL_Nef_polyhedron *root_N) } /*! - Saves the current 3D CGAL Nef polyhedron as STL to the given absolute filename. + Saves the current 3D CGAL Nef polyhedron as STL to the given file. + The file must be open. */ -void export_stl(CGAL_Nef_polyhedron *root_N, QString filename, QProgressDialog *pd) +void export_stl(CGAL_Nef_polyhedron *root_N, QTextStream &output, QProgressDialog *pd) { CGAL_Polyhedron P; root_N->p3.convert_to_Polyhedron(P); @@ -86,14 +88,7 @@ void export_stl(CGAL_Nef_polyhedron *root_N, QString filename, QProgressDialog * setlocale(LC_NUMERIC, "C"); // Ensure radix is . (not ,) in output - FILE *f = fopen(filename.toUtf8().data(), "w"); - if (!f) { - PRINTA("Can't open STL file \"%1\" for STL export: %2", - filename, QString(strerror(errno))); - set_output_handler(NULL, NULL); - return; - } - fprintf(f, "solid OpenSCAD_Model\n"); + output << "solid OpenSCAD_Model\n"; int facet_count = 0; for (FCI fi = P.facets_begin(); fi != P.facets_end(); ++fi) { @@ -127,14 +122,16 @@ void export_stl(CGAL_Nef_polyhedron *root_N, QString filename, QProgressDialog * // Avoid generating normals for polygons with zero area double eps = 0.000001; if (nlength < eps) nlength = 1.0; - fprintf(f, " facet normal %f %f %f\n", - nx / nlength, ny / nlength, nz / nlength); - fprintf(f, " outer loop\n"); - fprintf(f, " vertex %s\n", vs1.toAscii().data()); - fprintf(f, " vertex %s\n", vs2.toAscii().data()); - fprintf(f, " vertex %s\n", vs3.toAscii().data()); - fprintf(f, " endloop\n"); - fprintf(f, " endfacet\n"); + output << " facet normal " + << nx / nlength << " " + << ny / nlength << " " + << nz / nlength << "\n"; + output << " outer loop\n"; + output << " vertex " << vs1 << "\n"; + output << " vertex " << vs2 << "\n"; + output << " vertex " << vs3 << "\n"; + output << " endloop\n"; + output << " endfacet\n"; } } while (hc != hc_end); if (pd) { @@ -143,12 +140,11 @@ void export_stl(CGAL_Nef_polyhedron *root_N, QString filename, QProgressDialog * } } - fprintf(f, "endsolid OpenSCAD_Model\n"); - fclose(f); + output << "endsolid OpenSCAD_Model\n"; setlocale(LC_NUMERIC, ""); // Set default locale } -void export_off(CGAL_Nef_polyhedron*, QString, QProgressDialog*) +void export_off(CGAL_Nef_polyhedron*, QTextStream&, QProgressDialog*) { PRINTF("WARNING: OFF import is not implemented yet."); } @@ -156,29 +152,20 @@ void export_off(CGAL_Nef_polyhedron*, QString, QProgressDialog*) /*! Saves the current 2D CGAL Nef polyhedron as DXF to the given absolute filename. */ -void export_dxf(CGAL_Nef_polyhedron *root_N, QString filename, QProgressDialog *) +void export_dxf(CGAL_Nef_polyhedron *root_N, QTextStream &output, QProgressDialog *) { - FILE *f = fopen(filename.toUtf8().data(), "w"); - if (!f) { - PRINTA("Can't open DXF file \"%1\" for DXF export: %2", - filename, QString(strerror(errno))); - set_output_handler(NULL, NULL); - return; - } - setlocale(LC_NUMERIC, "C"); // Ensure radix is . (not ,) in output // Some importers (e.g. Inkscape) needs a BLOCKS section to be present - fprintf(f, " 0\n" - "SECTION\n" - " 2\n" - "BLOCKS\n" - " 0\n" - "ENDSEC\n"); - - fprintf(f, " 0\n" - "SECTION\n" - " 2\n" - "ENTITIES\n"); + output << " 0\n" + << "SECTION\n" + << " 2\n" + << "BLOCKS\n" + << " 0\n" + << "ENDSEC\n" + << " 0\n" + << "SECTION\n" + << " 2\n" + << "ENTITIES\n"; DxfData dd(*root_N); for (int i=0; i<dd.paths.size(); i++) @@ -190,37 +177,38 @@ void export_dxf(CGAL_Nef_polyhedron *root_N, QString filename, QProgressDialog * double y1 = p1->y; double x2 = p2->x; double y2 = p2->y; - fprintf(f, " 0\n"); - fprintf(f, "LINE\n"); + output << " 0\n" + << "LINE\n"; // Some importers (e.g. Inkscape) needs a layer to be specified - fprintf(f, " 8\n"); - fprintf(f, "0\n"); - fprintf(f, " 10\n"); - fprintf(f, "%f\n", x1); - fprintf(f, " 11\n"); - fprintf(f, "%f\n", x2); - fprintf(f, " 20\n"); - fprintf(f, "%f\n", y1); - fprintf(f, " 21\n"); - fprintf(f, "%f\n", y2); + output << " 8\n" + << "0\n" + << " 10\n" + << x1 << "\n" + << " 11\n" + << x2 << "\n" + << " 20\n" + << y1 << "\n" + << " 21\n" + << y2 << "\n"; } } + + output << " 0\n" + << "ENDSEC\n"; - fprintf(f, " 0\n"); - fprintf(f, "ENDSEC\n"); // Some importers (e.g. Inkscape) needs an OBJECTS section with a DICTIONARY entry - fprintf(f, " 0\n" - "SECTION\n" - " 2\n" - "OBJECTS\n" - " 0\n" - "DICTIONARY\n" - " 0\n" - "ENDSEC\n"); - fprintf(f, " 0\n"); - fprintf(f, "EOF\n"); - - fclose(f); + output << " 0\n" + << "SECTION\n" + << " 2\n" + << "OBJECTS\n" + << " 0\n" + << "DICTIONARY\n" + << " 0\n" + << "ENDSEC\n"; + + output << " 0\n" + <<"EOF\n"; + setlocale(LC_NUMERIC, ""); // Set default locale } |