Experimental support for exporting the compiled CSG tree as a text file

stl_dim
Marius Kintel 2011-10-19 17:35:14 +02:00
parent 5dd4decc91
commit 038767f6a6
4 changed files with 124 additions and 72 deletions

View File

@ -115,6 +115,7 @@ private slots:
void actionExportSTL();
void actionExportOFF();
void actionExportDXF();
void actionExportCSG();
void actionExportImage();
void actionFlushCaches();

View File

@ -178,6 +178,7 @@
<addaction name="designActionExportSTL"/>
<addaction name="designActionExportOFF"/>
<addaction name="designActionExportDXF"/>
<addaction name="designActionExportCSG"/>
<addaction name="designActionExportImage"/>
<addaction name="designActionFlushCaches"/>
</widget>
@ -657,6 +658,11 @@
<string>Export as Image...</string>
</property>
</action>
<action name="designActionExportCSG">
<property name="text">
<string>Export as CSG...</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

View File

@ -283,6 +283,7 @@ MainWindow::MainWindow(const QString &filename)
connect(this->designActionExportSTL, SIGNAL(triggered()), this, SLOT(actionExportSTL()));
connect(this->designActionExportOFF, SIGNAL(triggered()), this, SLOT(actionExportOFF()));
connect(this->designActionExportDXF, SIGNAL(triggered()), this, SLOT(actionExportDXF()));
connect(this->designActionExportCSG, SIGNAL(triggered()), this, SLOT(actionExportCSG()));
connect(this->designActionExportImage, SIGNAL(triggered()), this, SLOT(actionExportImage()));
connect(this->designActionFlushCaches, SIGNAL(triggered()), this, SLOT(actionFlushCaches()));
@ -1474,6 +1475,38 @@ void MainWindow::actionExportDXF()
#endif /* ENABLE_CGAL */
}
void MainWindow::actionExportCSG()
{
setCurrentOutput();
if (!this->root_node) {
PRINT("Nothing to export. Please try compiling first...");
clearCurrentOutput();
return;
}
QString csg_filename = QFileDialog::getSaveFileName(this, "Export CSG File",
this->fileName.isEmpty() ? "Untitled.csg" : QFileInfo(this->fileName).baseName()+".csg",
"CSG Files (*.csg)");
if (csg_filename.isEmpty()) {
PRINTF("No filename specified. CSG export aborted.");
clearCurrentOutput();
return;
}
std::ofstream fstream(csg_filename.toUtf8());
if (!fstream.is_open()) {
PRINTA("Can't open file \"%s\" for export", csg_filename);
}
else {
fstream << this->tree.getString(*this->root_node) << "\n";
fstream.close();
PRINTF("CSG export finished.");
}
clearCurrentOutput();
}
void MainWindow::actionExportImage()
{
QImage img = this->glview->grabFrameBuffer();

View File

@ -70,7 +70,7 @@ namespace po = boost::program_options;
static void help(const char *progname)
{
fprintf(stderr, "Usage: %s [ { -s stl_file | -o off_file | -x dxf_file } [ -d deps_file ] ]\\\n"
fprintf(stderr, "Usage: %s [ { -o output_file } [ -d deps_file ] ]\\\n"
"%*s[ -m make_command ] [ -D var=val [..] ] filename\n",
progname, int(strlen(progname))+8, "");
exit(1);
@ -126,18 +126,14 @@ int main(int argc, char **argv)
QCoreApplication::setApplicationName("OpenSCAD");
const char *filename = NULL;
const char *stl_output_file = NULL;
const char *off_output_file = NULL;
const char *dxf_output_file = NULL;
const char *output_file = NULL;
const char *deps_output_file = NULL;
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "help message")
("version,v", "print the version")
("s,s", po::value<string>(), "stl-file")
("o,o", po::value<string>(), "off-file")
("x,x", po::value<string>(), "dxf-file")
("o,o", po::value<string>(), "out-file")
("d,d", po::value<string>(), "deps-file")
("m,m", po::value<string>(), "makefile")
("D,D", po::value<vector<string> >(), "var=val");
@ -159,20 +155,10 @@ int main(int argc, char **argv)
if (vm.count("help")) help(argv[0]);
if (vm.count("version")) version();
if (vm.count("s")) {
if (stl_output_file || off_output_file || dxf_output_file)
help(argv[0]);
stl_output_file = vm["s"].as<string>().c_str();
}
if (vm.count("o")) {
if (stl_output_file || off_output_file || dxf_output_file)
help(argv[0]);
off_output_file = vm["o"].as<string>().c_str();
}
if (vm.count("x")) {
if (stl_output_file || off_output_file || dxf_output_file)
help(argv[0]);
dxf_output_file = vm["x"].as<string>().c_str();
// FIXME: Allow for multiple output files?
if (output_file) help(argv[0]);
output_file = vm["o"].as<string>().c_str();
}
if (vm.count("d")) {
if (deps_output_file)
@ -253,10 +239,24 @@ int main(int argc, char **argv)
PolySetCGALEvaluator psevaluator(cgalevaluator);
#endif
if (stl_output_file || off_output_file || dxf_output_file)
if (output_file)
{
if (!filename)
help(argv[0]);
const char *stl_output_file = NULL;
const char *off_output_file = NULL;
const char *dxf_output_file = NULL;
const char *csg_output_file = NULL;
QString suffix = QFileInfo(output_file).suffix().toLower();
if (suffix == "stl") stl_output_file = output_file;
else if (suffix == "off") off_output_file = output_file;
else if (suffix == "dxf") dxf_output_file = output_file;
else if (suffix == "csg") csg_output_file = output_file;
else {
fprintf(stderr, "Unknown suffix for output file %s\n", output_file);
exit(1);
}
if (!filename) help(argv[0]);
#ifdef ENABLE_CGAL
Context root_ctx;
@ -304,68 +304,80 @@ int main(int argc, char **argv)
AbstractNode::resetIndexCounter();
root_node = root_module->evaluate(&root_ctx, &root_inst);
tree.setRoot(root_node);
CGAL_Nef_polyhedron root_N = cgalevaluator.evaluateCGALMesh(*tree.root());
QDir::setCurrent(original_path.absolutePath());
if (deps_output_file) {
if (!write_deps(deps_output_file,
stl_output_file ? stl_output_file : off_output_file)) {
exit(1);
}
}
if (stl_output_file) {
if (root_N.dim != 3) {
fprintf(stderr, "Current top level object is not a 3D object.\n");
exit(1);
}
if (!root_N.p3->is_simple()) {
fprintf(stderr, "Object isn't a valid 2-manifold! Modify your design.\n");
exit(1);
}
std::ofstream fstream(stl_output_file);
if (csg_output_file) {
QDir::setCurrent(original_path.absolutePath());
std::ofstream fstream(csg_output_file);
if (!fstream.is_open()) {
PRINTF("Can't open file \"%s\" for export", stl_output_file);
PRINTF("Can't open file \"%s\" for export", csg_output_file);
}
else {
export_stl(&root_N, fstream, NULL);
fstream << tree.getString(*root_node) << "\n";
fstream.close();
}
}
else {
CGAL_Nef_polyhedron root_N = cgalevaluator.evaluateCGALMesh(*tree.root());
QDir::setCurrent(original_path.absolutePath());
if (deps_output_file) {
if (!write_deps(deps_output_file,
stl_output_file ? stl_output_file : off_output_file)) {
exit(1);
}
}
if (off_output_file) {
if (root_N.dim != 3) {
fprintf(stderr, "Current top level object is not a 3D object.\n");
exit(1);
if (stl_output_file) {
if (root_N.dim != 3) {
fprintf(stderr, "Current top level object is not a 3D object.\n");
exit(1);
}
if (!root_N.p3->is_simple()) {
fprintf(stderr, "Object isn't a valid 2-manifold! Modify your design.\n");
exit(1);
}
std::ofstream fstream(stl_output_file);
if (!fstream.is_open()) {
PRINTF("Can't open file \"%s\" for export", stl_output_file);
}
else {
export_stl(&root_N, fstream, NULL);
fstream.close();
}
}
if (!root_N.p3->is_simple()) {
fprintf(stderr, "Object isn't a valid 2-manifold! Modify your design.\n");
exit(1);
if (off_output_file) {
if (root_N.dim != 3) {
fprintf(stderr, "Current top level object is not a 3D object.\n");
exit(1);
}
if (!root_N.p3->is_simple()) {
fprintf(stderr, "Object isn't a valid 2-manifold! Modify your design.\n");
exit(1);
}
std::ofstream fstream(off_output_file);
if (!fstream.is_open()) {
PRINTF("Can't open file \"%s\" for export", off_output_file);
}
else {
export_off(&root_N, fstream, NULL);
fstream.close();
}
}
std::ofstream fstream(off_output_file);
if (!fstream.is_open()) {
PRINTF("Can't open file \"%s\" for export", off_output_file);
}
else {
export_off(&root_N, fstream, NULL);
fstream.close();
if (dxf_output_file) {
std::ofstream fstream(dxf_output_file);
if (!fstream.is_open()) {
PRINTF("Can't open file \"%s\" for export", dxf_output_file);
}
else {
export_dxf(&root_N, fstream, NULL);
fstream.close();
}
}
}
if (dxf_output_file) {
std::ofstream fstream(dxf_output_file);
if (!fstream.is_open()) {
PRINTF("Can't open file \"%s\" for export", dxf_output_file);
}
else {
export_dxf(&root_N, fstream, NULL);
fstream.close();
}
}
delete root_node;
#else
fprintf(stderr, "OpenSCAD has been compiled without CGAL support!\n");