diff options
author | clifford <clifford@b57f626f-c46c-0410-a088-ec61d464b74c> | 2009-07-10 15:09:43 (GMT) |
---|---|---|
committer | clifford <clifford@b57f626f-c46c-0410-a088-ec61d464b74c> | 2009-07-10 15:09:43 (GMT) |
commit | edc627bcc9884e340d2a7b2bb2bec09f811844a0 (patch) | |
tree | e10c3f6b914007d1199112059bdbf3fcacb770d2 | |
parent | e66b2cc8c65b7b4eca6b6f767759c3c9a1575490 (diff) |
Clifford Wolf:
Added STL export
git-svn-id: http://svn.clifford.at/openscad/trunk@53 b57f626f-c46c-0410-a088-ec61d464b74c
-rw-r--r-- | example.scad | 3 | ||||
-rw-r--r-- | mainwin.cc | 93 |
2 files changed, 94 insertions, 2 deletions
diff --git a/example.scad b/example.scad index f91d22f..193ec73 100644 --- a/example.scad +++ b/example.scad @@ -1,6 +1,7 @@ module test001() { + $fn = 100; function r_from_dia(d) = d / 2; module rotcy(rot, r, h) { @@ -80,7 +81,7 @@ module test005() cylinder(h = 200, r=10); } translate([0 0 200]) - cylinder(h = 80, r1 = 120, r2 = 0); + cylinder(h = 80, r1 = 120, r2 = 1); } } @@ -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; } |