diff options
Diffstat (limited to 'src/openscad.cc')
-rw-r--r-- | src/openscad.cc | 120 |
1 files changed, 73 insertions, 47 deletions
diff --git a/src/openscad.cc b/src/openscad.cc index 180dbdf..c4b0d5f 100644 --- a/src/openscad.cc +++ b/src/openscad.cc @@ -24,6 +24,7 @@ * */ +#include "myqhash.h" #include "openscad.h" #include "MainWindow.h" #include "node.h" @@ -32,13 +33,19 @@ #include "value.h" #include "export.h" #include "builtin.h" +#include "nodedumper.h" +#include "printutils.h" +#include "handle_dep.h" #include <string> #include <vector> +#include <fstream> #ifdef ENABLE_CGAL -#include "cgal.h" +#include "CGAL_Nef_polyhedron.h" #include <CGAL/assertions_behaviour.h> +#include "CGALEvaluator.h" +#include "PolySetCGALEvaluator.h" #endif #include <QApplication> @@ -46,7 +53,10 @@ #include <QDir> #include <QSet> #include <QSettings> +#include <QTextStream> #include <boost/program_options.hpp> +#include <sstream> + #ifdef Q_WS_MAC #include "EventFilter.h" #include "AppleEvents.h" @@ -74,9 +84,7 @@ static void version() exit(1); } -QString commandline_commands; -const char *make_command = NULL; -QSet<QString> dependencies; +std::string commandline_commands; QString currentdir; QString examplesdir; QString librarydir; @@ -84,19 +92,6 @@ QString librarydir; using std::string; using std::vector; -void handle_dep(QString filename) -{ - if (filename.startsWith("/")) - dependencies.insert(filename); - else - dependencies.insert(QDir::currentPath() + QString("/") + filename); - if (!QFile(filename).exists() && make_command) { - char buffer[4096]; - snprintf(buffer, 4096, "%s '%s'", make_command, filename.replace("'", "'\\''").toUtf8().data()); - system(buffer); // FIXME: Handle error - } -} - int main(int argc, char **argv) { int rc = 0; @@ -194,8 +189,8 @@ int main(int argc, char **argv) const vector<string> &commands = vm["D"].as<vector<string> >(); for (vector<string>::const_iterator i = commands.begin(); i != commands.end(); i++) { - commandline_commands.append(i->c_str()); - commandline_commands.append(";\n"); + commandline_commands += *i; + commandline_commands += ";\n"; } } @@ -249,6 +244,17 @@ int main(int argc, char **argv) librarydir = libdir.path(); } + // Initialize global visitors + NodeCache nodecache; + NodeDumper dumper(nodecache); + Tree tree; +#ifdef ENABLE_CGAL + // FIXME: enforce some maximum cache size (old version had 100K vertices as limit) + QHash<std::string, CGAL_Nef_polyhedron> cache; + CGALEvaluator cgalevaluator(cache, tree); + PolySetCGALEvaluator psevaluator(cgalevaluator); +#endif + if (stl_output_file || off_output_file || dxf_output_file) { if (!filename) @@ -265,9 +271,9 @@ int main(int argc, char **argv) Value zero3; zero3.type = Value::VECTOR; - zero3.vec.append(new Value(0.0)); - zero3.vec.append(new Value(0.0)); - zero3.vec.append(new Value(0.0)); + zero3.append(new Value(0.0)); + zero3.append(new Value(0.0)); + zero3.append(new Value(0.0)); root_ctx.set_variable("$vpt", zero3); root_ctx.set_variable("$vpr", zero3); @@ -283,15 +289,16 @@ int main(int argc, char **argv) fprintf(stderr, "Can't open input file `%s'!\n", filename); exit(1); } else { - QString text; + std::stringstream text; char buffer[513]; int ret; while ((ret = fread(buffer, 1, 512, fp)) > 0) { buffer[ret] = 0; - text += buffer; + text << buffer; } fclose(fp); - root_module = parse((text+commandline_commands).toAscii().data(), fileInfo.absolutePath().toLocal8Bit(), false); + text << commandline_commands; + root_module = parse(text.str().c_str(), fileInfo.absolutePath().toLocal8Bit(), false); } QDir::setCurrent(fileInfo.absolutePath()); @@ -299,41 +306,60 @@ int main(int argc, char **argv) AbstractNode::resetIndexCounter(); root_node = root_module->evaluate(&root_ctx, &root_inst); - CGAL_Nef_polyhedron *root_N; - root_N = new CGAL_Nef_polyhedron(root_node->render_cgal_nef_polyhedron()); + tree.setRoot(root_node); + CGAL_Nef_polyhedron root_N = cgalevaluator.evaluateCGALMesh(*tree.root()); QDir::setCurrent(original_path.absolutePath()); if (deps_output_file) { - fp = fopen(deps_output_file, "wt"); - if (!fp) { - fprintf(stderr, "Can't open dependencies file `%s' for writing!\n", deps_output_file); + if (!write_deps(deps_output_file, + stl_output_file ? stl_output_file : off_output_file)) { exit(1); } - fprintf(fp, "%s:", stl_output_file ? stl_output_file : off_output_file); - QSetIterator<QString> i(dependencies); - while (i.hasNext()) - fprintf(fp, " \\\n\t%s", i.next().toUtf8().data()); - fprintf(fp, "\n"); - fclose(fp); } - if (root_N->dim == 3 && !root_N->p3.is_simple()) { - fprintf(stderr, "Object isn't a valid 2-manifold! Modify your design.\n"); - exit(1); + if (stl_output_file) { + if (root_N->dim == 3 && !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 (stl_output_file) - export_stl(root_N, stl_output_file, NULL); - - if (off_output_file) - export_off(root_N, off_output_file, NULL); + if (off_output_file) { + if (root_N->dim == 3 && !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_off(&root_N, fstream, NULL); + fstream.close(); + } + } - if (dxf_output_file) - export_dxf(root_N, dxf_output_file, NULL); + 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; - delete root_N; #else fprintf(stderr, "OpenSCAD has been compiled without CGAL support!\n"); exit(1); |