2009-10-18 12:50:01 +04:00
|
|
|
/*
|
|
|
|
* OpenSCAD (www.openscad.at)
|
|
|
|
* Copyright (C) 2009 Clifford Wolf <clifford@clifford.at>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2010-02-01 12:34:18 +03:00
|
|
|
* As a special exception, you have permission to link this program
|
|
|
|
* with the CGAL library and distribute executables, as long as you
|
|
|
|
* follow the requirements of the GNU GPL in regard to all of the
|
|
|
|
* software in the executable aside from CGAL.
|
|
|
|
*
|
2009-10-18 12:50:01 +04:00
|
|
|
* 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-12-09 17:25:09 +03:00
|
|
|
#include "printutils.h"
|
2010-03-01 14:05:08 +03:00
|
|
|
#include "polyset.h"
|
2010-01-30 07:26:05 +03:00
|
|
|
#include "dxfdata.h"
|
2009-10-18 12:50:01 +04:00
|
|
|
|
|
|
|
#include <QApplication>
|
2010-01-30 07:26:05 +03:00
|
|
|
#include <QProgressDialog>
|
2010-01-30 08:25:21 +03:00
|
|
|
#include <errno.h>
|
2009-10-18 12:50:01 +04:00
|
|
|
|
|
|
|
#ifdef ENABLE_CGAL
|
2010-01-30 07:26:05 +03:00
|
|
|
#include "cgal.h"
|
2009-10-18 12:50:01 +04:00
|
|
|
|
2010-01-30 10:41:55 +03:00
|
|
|
void cgal_nef3_to_polyset(PolySet *ps, CGAL_Nef_polyhedron *root_N)
|
|
|
|
{
|
|
|
|
CGAL_Polyhedron P;
|
|
|
|
root_N->p3.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;
|
|
|
|
|
|
|
|
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());
|
|
|
|
ps->append_poly();
|
|
|
|
ps->append_vertex(x1, y1, z1);
|
|
|
|
ps->append_vertex(x2, y2, z2);
|
|
|
|
ps->append_vertex(x3, y3, z3);
|
|
|
|
} while (hc != hc_end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-23 23:39:46 +03:00
|
|
|
/*!
|
|
|
|
Saves the current 3D CGAL Nef polyhedron as STL to the given absolute filename.
|
|
|
|
*/
|
2009-10-18 12:50:01 +04:00
|
|
|
void export_stl(CGAL_Nef_polyhedron *root_N, QString filename, QProgressDialog *pd)
|
|
|
|
{
|
|
|
|
CGAL_Polyhedron P;
|
2009-12-28 02:49:27 +03:00
|
|
|
root_N->p3.convert_to_Polyhedron(P);
|
2009-10-18 12:50:01 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2010-06-26 20:17:34 +04:00
|
|
|
setlocale(LC_NUMERIC, "C"); // Ensure radix is . (not ,) in output
|
|
|
|
|
2009-12-17 02:54:12 +03:00
|
|
|
FILE *f = fopen(filename.toUtf8().data(), "w");
|
2009-10-18 12:50:01 +04:00
|
|
|
if (!f) {
|
2009-12-17 02:54:12 +03:00
|
|
|
PRINTA("Can't open STL file \"%1\" for STL export: %2",
|
|
|
|
filename, QString(strerror(errno)));
|
2010-02-28 23:54:55 +03:00
|
|
|
set_output_handler(NULL, NULL);
|
2009-10-18 12:50:01 +04:00
|
|
|
return;
|
|
|
|
}
|
2010-02-25 11:35:50 +03:00
|
|
|
fprintf(f, "solid OpenSCAD_Model\n");
|
2009-10-18 12:50:01 +04:00
|
|
|
|
|
|
|
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);
|
2010-05-10 22:14:12 +04:00
|
|
|
double nlength = sqrt(nx*nx + ny*ny + nz*nz);
|
|
|
|
// Avoid generating normals for polygons with zero area
|
|
|
|
double eps = 0.000001;
|
|
|
|
if (nlength < eps) nlength = 1.0;
|
2009-10-18 12:50:01 +04:00
|
|
|
fprintf(f, " facet normal %f %f %f\n",
|
2010-05-10 22:14:12 +04:00
|
|
|
nx / nlength, ny / nlength, nz / nlength);
|
2009-10-18 12:50:01 +04:00
|
|
|
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);
|
|
|
|
if (pd) {
|
|
|
|
pd->setValue(facet_count++);
|
|
|
|
QApplication::processEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-25 11:35:50 +03:00
|
|
|
fprintf(f, "endsolid OpenSCAD_Model\n");
|
2009-10-18 12:50:01 +04:00
|
|
|
fclose(f);
|
2010-06-26 20:17:34 +04:00
|
|
|
setlocale(LC_NUMERIC, ""); // Set default locale
|
2009-10-18 12:50:01 +04:00
|
|
|
}
|
|
|
|
|
2009-10-19 23:30:41 +04:00
|
|
|
void export_off(CGAL_Nef_polyhedron*, QString, QProgressDialog*)
|
2009-10-18 12:50:01 +04:00
|
|
|
{
|
2009-10-19 23:30:41 +04:00
|
|
|
PRINTF("WARNING: OFF import is not implemented yet.");
|
2009-10-18 12:50:01 +04:00
|
|
|
}
|
|
|
|
|
2010-02-23 23:39:46 +03:00
|
|
|
/*!
|
|
|
|
Saves the current 2D CGAL Nef polyhedron as DXF to the given absolute filename.
|
|
|
|
*/
|
2010-01-06 05:04:49 +03:00
|
|
|
void export_dxf(CGAL_Nef_polyhedron *root_N, QString filename, 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)));
|
2010-02-28 23:54:55 +03:00
|
|
|
set_output_handler(NULL, NULL);
|
2010-01-06 05:04:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-27 02:55:21 +04:00
|
|
|
setlocale(LC_NUMERIC, "C"); // Ensure radix is . (not ,) in output
|
2011-01-09 19:08:34 +03:00
|
|
|
|
|
|
|
// Some importers (e.g. QCAD) needs a HEADER section specifying AutoCAD 2000 as
|
|
|
|
// the file format for LWPOLYLINE entities to work
|
|
|
|
fprintf(f, " 0\n"
|
|
|
|
"SECTION\n"
|
|
|
|
" 2\n"
|
|
|
|
"HEADER\n"
|
|
|
|
" 9\n"
|
|
|
|
"$ACADVER\n"
|
|
|
|
" 1\n"
|
|
|
|
"AC1015\n"
|
|
|
|
" 0\n"
|
|
|
|
"ENDSEC\n");
|
|
|
|
|
2010-06-27 02:55:21 +04:00
|
|
|
// 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");
|
2010-01-06 05:04:49 +03:00
|
|
|
|
|
|
|
DxfData dd(*root_N);
|
|
|
|
for (int i=0; i<dd.paths.size(); i++)
|
|
|
|
{
|
2011-01-09 19:08:34 +03:00
|
|
|
if (dd.paths[i].points.size() < 2)
|
|
|
|
// not a valid polygon
|
|
|
|
continue;
|
|
|
|
// Use the LWPOLYLINE class - this makes it easier to handle complete
|
|
|
|
// objects (as paths) in Inkscape.
|
|
|
|
fprintf(f, " 0\n");
|
|
|
|
fprintf(f, "LWPOLYLINE\n");
|
|
|
|
// Some importers (e.g. Inkscape) need a layer to be specified
|
|
|
|
fprintf(f, " 8\n");
|
|
|
|
fprintf(f, "0\n");
|
|
|
|
// number of vertices
|
|
|
|
fprintf(f, " 90\n");
|
|
|
|
fprintf(f, "%d\n", dd.paths[i].points.size());
|
|
|
|
// polygon flag (closed, ...)
|
|
|
|
fprintf(f, " 70\n");
|
|
|
|
fprintf(f, "%d\n", dd.paths[i].is_closed ? 1 : 0);
|
|
|
|
// add all points
|
|
|
|
for (int j=0; j<dd.paths[i].points.size(); j++) {
|
|
|
|
DxfData::Point *p = dd.paths[i].points[j];
|
2010-01-06 05:04:49 +03:00
|
|
|
fprintf(f, " 10\n");
|
2011-01-09 19:08:34 +03:00
|
|
|
fprintf(f, "%f\n", p->x);
|
2010-01-06 05:04:49 +03:00
|
|
|
fprintf(f, " 20\n");
|
2011-01-09 19:08:34 +03:00
|
|
|
fprintf(f, "%f\n", p->y);
|
2010-01-06 05:04:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f, " 0\n");
|
|
|
|
fprintf(f, "ENDSEC\n");
|
2010-06-27 02:55:21 +04:00
|
|
|
// 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");
|
2010-01-06 05:04:49 +03:00
|
|
|
fprintf(f, " 0\n");
|
|
|
|
fprintf(f, "EOF\n");
|
|
|
|
|
|
|
|
fclose(f);
|
2010-06-27 02:55:21 +04:00
|
|
|
setlocale(LC_NUMERIC, ""); // Set default locale
|
2010-01-06 05:04:49 +03:00
|
|
|
}
|
|
|
|
|
2009-10-18 12:50:01 +04:00
|
|
|
#endif
|
|
|
|
|