summaryrefslogtreecommitdiff
path: root/mainwin.cc
diff options
context:
space:
mode:
Diffstat (limited to 'mainwin.cc')
-rw-r--r--mainwin.cc93
1 files changed, 92 insertions, 1 deletions
diff --git a/mainwin.cc b/mainwin.cc
index 0905d66..79aa0a9 100644
--- a/mainwin.cc
+++ b/mainwin.cc
@@ -644,7 +644,98 @@ void MainWindow::actionDisplayCSGProducts()
void MainWindow::actionExportSTL()
{
current_win = this;
- PRINTA("Function %1 is not implemented yet!", QString(__PRETTY_FUNCTION__));
+
+ if (!root_N) {
+ PRINT("Nothing to export! Try building first (press F6).");
+ current_win = NULL;
+ return;
+ }
+
+ if (!root_N->is_simple()) {
+ PRINT("Object isn't a single polyeder or otherwise invalid! Modify your design..");
+ current_win = NULL;
+ return;
+ }
+
+ QString stl_filename = QFileDialog::getSaveFileName(this, "Export STL File", "", "STL Files (*.stl)");
+ if (stl_filename.isEmpty()) {
+ PRINT("No filename specified. STL export aborted.");
+ current_win = NULL;
+ return;
+ }
+
+ CGAL_Polyhedron P;
+ root_N->convert_to_Polyhedron(P);
+
+ typedef CGAL_Polyhedron::Vertex Vertex;
+ typedef CGAL_Polyhedron::Vertex_const_iterator VCI;
+ typedef CGAL_Polyhedron::Facet_const_iterator FCI;
+ typedef CGAL_Polyhedron::Halfedge_around_facet_const_circulator HFCC;
+
+ FILE *f = fopen(stl_filename.toAscii().data(), "w");
+ if (!f) {
+ PRINT("Can't open STL file for STL export.");
+ current_win = NULL;
+ return;
+ }
+ fprintf(f, "solid\n");
+
+ QProgressDialog *pd = new QProgressDialog("Exporting object to STL file...",
+ QString(), 0, root_N->number_of_facets() + 1);
+ pd->setValue(0);
+ pd->setAutoClose(false);
+ pd->show();
+ QApplication::processEvents();
+
+ int facet_count = 0;
+ for (FCI fi = P.facets_begin(); fi != P.facets_end(); ++fi) {
+ HFCC hc = fi->facet_begin();
+ HFCC hc_end = hc;
+ Vertex v1, v2, v3;
+ v1 = *VCI((hc++)->vertex());
+ v3 = *VCI((hc++)->vertex());
+ do {
+ v2 = v3;
+ v3 = *VCI((hc++)->vertex());
+ double x1 = CGAL::to_double(v1.point().x());
+ double y1 = CGAL::to_double(v1.point().y());
+ double z1 = CGAL::to_double(v1.point().z());
+ double x2 = CGAL::to_double(v2.point().x());
+ double y2 = CGAL::to_double(v2.point().y());
+ double z2 = CGAL::to_double(v2.point().z());
+ double x3 = CGAL::to_double(v3.point().x());
+ double y3 = CGAL::to_double(v3.point().y());
+ double z3 = CGAL::to_double(v3.point().z());
+ QString vs1, vs2, vs3;
+ vs1.sprintf("%f %f %f", x1, y1, z1);
+ vs2.sprintf("%f %f %f", x2, y2, z2);
+ vs3.sprintf("%f %f %f", x3, y3, z3);
+ if (vs1 != vs2 && vs1 != vs3 && vs2 != vs3) {
+
+ double nx = (y1-y2)*(z1-z3) - (z1-z2)*(y1-y3);
+ double ny = (z1-z2)*(x1-x3) - (x1-x2)*(z1-z3);
+ double nz = (x1-x2)*(y1-y3) - (y1-y2)*(x1-x3);
+ double n_scale = 1 / sqrt(nx*nx + ny*ny + nz*nz);
+ fprintf(f, " facet normal %f %f %f\n",
+ nx * n_scale, ny * n_scale, nz * n_scale);
+ 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");
+ }
+ } while (hc != hc_end);
+ pd->setValue(facet_count++);
+ QApplication::processEvents();
+ }
+
+ fprintf(f, "endsolid\n");
+ fclose(f);
+
+ PRINT("STL export finished.");
+
+ delete pd;
current_win = NULL;
}
contact: Jan Huwald // Impressum