diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/export.cc | 38 | ||||
-rw-r--r-- | src/export.h | 6 | ||||
-rw-r--r-- | src/mainwin.cc | 17 | ||||
-rw-r--r-- | src/openscad.cc | 39 |
4 files changed, 64 insertions, 36 deletions
diff --git a/src/export.cc b/src/export.cc index 40b16d5..2108469 100644 --- a/src/export.cc +++ b/src/export.cc @@ -29,6 +29,7 @@ #include <QApplication> #include <QProgressDialog> +#include <QTextStream> #include <errno.h> #ifdef ENABLE_CGAL @@ -71,9 +72,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); @@ -85,14 +87,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) { @@ -126,14 +121,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 " << vs2 << "\n"; + output << " vertex " << vs2 << "\n"; + output << " vertex " << vs3 << "\n"; + output << " endloop\n"; + output << " endfacet\n"; } } while (hc != hc_end); if (pd) { @@ -142,12 +139,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."); } diff --git a/src/export.h b/src/export.h index 444b984..d35246c 100644 --- a/src/export.h +++ b/src/export.h @@ -4,9 +4,9 @@ #ifdef ENABLE_CGAL #include "cgal.h" void cgal_nef3_to_polyset(PolySet *ps, CGAL_Nef_polyhedron *root_N); -void export_stl(class CGAL_Nef_polyhedron *root_N, QString filename, class QProgressDialog *pd); -void export_off(CGAL_Nef_polyhedron *root_N, QString filename, QProgressDialog *pd); -void export_dxf(CGAL_Nef_polyhedron *root_N, QString filename, QProgressDialog *pd); +void export_stl(class CGAL_Nef_polyhedron *root_N, class QTextStream &output, class QProgressDialog *pd); +void export_off(CGAL_Nef_polyhedron *root_N, class QTextStream &output, QProgressDialog *pd); +void export_dxf(CGAL_Nef_polyhedron *root_N, class QTextStream &output, QProgressDialog *pd); #endif #endif diff --git a/src/mainwin.cc b/src/mainwin.cc index 955921d..af220a3 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -1313,13 +1313,18 @@ void MainWindow::actionExportSTLorOFF(bool) pd->show(); QApplication::processEvents(); - if (stl_mode) - export_stl(this->root_N, stl_filename, pd); - else - export_off(this->root_N, stl_filename, pd); - - PRINTF("%s export finished.", stl_mode ? "STL" : "OFF"); + QFile file(filename); + if (!file.open(QIODevice::ReadWrite)) { + PRINTA("Can't open file \"%1\" for export", filename); + } + else { + QTextStream fstream(&file); + if (stl_mode) export_stl(this->root_N, fstream, pd); + else export_off(this->root_N, fstream, pd); + file.close() + PRINTF("%s export finished.", stl_mode ? "STL" : "OFF"); + } delete pd; clearCurrentOutput(); diff --git a/src/openscad.cc b/src/openscad.cc index 35da4d7..494d41b 100644 --- a/src/openscad.cc +++ b/src/openscad.cc @@ -318,14 +318,41 @@ int main(int argc, char **argv) fclose(fp); } - if (stl_output_file) - export_stl(&root_N, stl_output_file, NULL); + if (stl_output_file) { + QFile file(stl_output_file); + if (!file.open(QIODevice::ReadWrite)) { + PRINTA("Can't open file \"%1\" for export", stl_output_file); + } + else { + QTextStream fstream(&file); + export_stl(&root_N, fstream, NULL); + file.close(); + } + } - if (off_output_file) - export_off(&root_N, off_output_file, NULL); + if (off_output_file) { + QFile file(stl_output_file); + if (!file.open(QIODevice::ReadWrite)) { + PRINTA("Can't open file \"%1\" for export", stl_output_file); + } + else { + QTextStream fstream(&file); + export_off(&root_N, fstream, NULL); + file.close(); + } + } - if (dxf_output_file) - export_dxf(&root_N, dxf_output_file, NULL); + if (dxf_output_file) { + QFile file(stl_output_file); + if (!file.open(QIODevice::ReadWrite)) { + PRINTA("Can't open file \"%1\" for export", stl_output_file); + } + else { + QTextStream fstream(&file); + export_dxf(&root_N, fstream, NULL); + file.close(); + } + } delete root_node; #else |