diff options
author | Don Bright <hugh.m.bright@gmail.com> | 2011-09-12 22:40:51 (GMT) |
---|---|---|
committer | Don Bright <hugh.m.bright@gmail.com> | 2011-09-12 22:40:51 (GMT) |
commit | f5f06c8e976ca45aebea42fe8c04bf7404357ac8 (patch) | |
tree | 8d0cdd2698e7806c1b12cc49e743b561e1082961 /src/import.cc | |
parent | 007c40848db9efd704694f2e7596cabed80da50f (diff) | |
parent | 5ac9162f1c67fd21737ead11d7ebc638bf4eef5f (diff) |
merge
Merge remote branch 'upstream/visitor' into visitortests
Conflicts:
src/export.cc
src/openscad.cc
src/polyset.cc
src/transform.cc
tests/CMakeLists.txt
tests/FindGLEW.cmake
tests/csgtermtest.cc
Diffstat (limited to 'src/import.cc')
-rw-r--r-- | src/import.cc | 142 |
1 files changed, 71 insertions, 71 deletions
diff --git a/src/import.cc b/src/import.cc index 7435d4d..f64a8f6 100644 --- a/src/import.cc +++ b/src/import.cc @@ -33,39 +33,40 @@ #include "dxfdata.h" #include "dxftess.h" #include "printutils.h" -#include "openscad.h" // handle_dep() +#include "handle_dep.h" // handle_dep() + +#ifdef ENABLE_CGAL +#include "cgalutils.h" +#endif #include <QFile> #include <QRegExp> #include <QStringList> #include <sys/types.h> #include <sys/stat.h> +#include <fstream> #include <sstream> #include <assert.h> +#include <boost/assign/std/vector.hpp> +using namespace boost::assign; // bring 'operator+=()' into scope class ImportModule : public AbstractModule { public: import_type_e type; - ImportModule(import_type_e type) : type(type) { } + ImportModule(import_type_e type = TYPE_UNKNOWN) : type(type) { } virtual AbstractNode *evaluate(const Context *ctx, const ModuleInstantiation *inst) const; }; AbstractNode *ImportModule::evaluate(const Context *ctx, const ModuleInstantiation *inst) const { - ImportNode *node = new ImportNode(inst, type); - - QVector<QString> argnames; - if (this->type == TYPE_DXF) { - argnames = QVector<QString>() << "file" << "layer" << "convexity" << "origin" << "scale"; - } else { - argnames = QVector<QString>() << "file" << "convexity"; - } - QVector<Expression*> argexpr; + std::vector<std::string> argnames; + argnames += "file", "layer", "convexity", "origin", "scale"; + std::vector<Expression*> argexpr; // Map old argnames to new argnames for compatibility - QVector<QString> inst_argnames = inst->argnames; - for (int i=0; i<inst_argnames.size(); i++) { + std::vector<std::string> inst_argnames = inst->argnames; + for (size_t i=0; i<inst_argnames.size(); i++) { if (inst_argnames[i] == "filename") inst_argnames[i] = "file"; if (inst_argnames[i] == "layername") @@ -75,14 +76,24 @@ AbstractNode *ImportModule::evaluate(const Context *ctx, const ModuleInstantiati Context c(ctx); c.args(argnames, argexpr, inst_argnames, inst->argvalues); + Value v = c.lookup_variable("file"); + std::string filename = c.getAbsolutePath(v.text); + import_type_e actualtype = this->type; + if (actualtype == TYPE_UNKNOWN) { + QFileInfo fi(QString::fromStdString(filename)); + if (fi.suffix().toLower() == "stl") actualtype = TYPE_STL; + else if (fi.suffix().toLower() == "off") actualtype = TYPE_OFF; + else if (fi.suffix().toLower() == "dxf") actualtype = TYPE_DXF; + } + + ImportNode *node = new ImportNode(inst, actualtype); + node->fn = c.lookup_variable("$fn").num; node->fs = c.lookup_variable("$fs").num; node->fa = c.lookup_variable("$fa").num; - Value v = c.lookup_variable("file"); - node->filename = c.get_absolute_path(QString::fromStdString(v.text)); -// node->filename = c.get_absolute_path(QString::fromStdString(c.lookup_variable("file").text)); - node->layername = QString::fromStdString(c.lookup_variable("layer", true).text); + node->filename = filename; + node->layername = c.lookup_variable("layer", true).text; node->convexity = c.lookup_variable("convexity", true).num; if (node->convexity <= 0) @@ -100,27 +111,20 @@ AbstractNode *ImportModule::evaluate(const Context *ctx, const ModuleInstantiati return node; } -void register_builtin_import() +PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *evaluator) const { - builtin_modules["import_stl"] = new ImportModule(TYPE_STL); - builtin_modules["import_off"] = new ImportModule(TYPE_OFF); - builtin_modules["import_dxf"] = new ImportModule(TYPE_DXF); -} - -PolySet *ImportNode::evaluate_polyset(render_mode_e, class PolySetEvaluator *) const -{ - PolySet *p = new PolySet(); - p->convexity = this->convexity; + PolySet *p = NULL; if (this->type == TYPE_STL) { handle_dep(this->filename); - QFile f(this->filename); + QFile f(QString::fromStdString(this->filename)); if (!f.open(QIODevice::ReadOnly)) { - PRINTF("WARNING: Can't open import file `%s'.", this->filename.toAscii().data()); + PRINTF("WARNING: Can't open import file `%s'.", this->filename.c_str()); return p; } + p = new PolySet(); QByteArray data = f.read(5); if (data.size() == 5 && QString(data) == QString("solid")) { @@ -189,19 +193,34 @@ PolySet *ImportNode::evaluate_polyset(render_mode_e, class PolySetEvaluator *) c } } - if (this->type == TYPE_OFF) + else if (this->type == TYPE_OFF) { - PRINTF("WARNING: OFF import is not implemented yet."); +#ifdef ENABLE_CGAL + CGAL_Polyhedron poly; + std::ifstream file(this->filename.c_str()); + file >> poly; + file.close(); + + p = createPolySetFromPolyhedron(poly); +#else + PRINTF("WARNING: OFF import requires CGAL."); +#endif } - if (this->type == TYPE_DXF) + else if (this->type == TYPE_DXF) { + p = new PolySet(); DxfData dd(this->fn, this->fs, this->fa, this->filename, this->layername, this->origin_x, this->origin_y, this->scale); p->is2d = true; - dxf_tesselate(p, &dd, 0, true, false, 0); - dxf_border_to_ps(p, &dd); + dxf_tesselate(p, dd, 0, true, false, 0); + dxf_border_to_ps(p, dd); + } + else + { + PRINTF("ERROR: Unsupported file format while trying to import file '%s'", this->filename.c_str()); } + if (p) p->convexity = this->convexity; return p; } @@ -212,49 +231,30 @@ std::string ImportNode::toString() const QString text; struct stat st; memset(&st, 0, sizeof(struct stat)); - stat(this->filename.toAscii().data(), &st); + stat(this->filename.c_str(), &st); stream << this->name(); - switch (this->type) { - case TYPE_STL: - stream << "(file = \"" << this->filename << "\", " - "cache = \"" << std::hex << (int)st.st_mtime << "." << (int)st.st_size << "\", " - "convexity = " << std::dec << this->convexity << ")"; - break; - case TYPE_OFF: - stream << "(file = \"" << this->filename << "\", " - "cache = \"" << std::hex << (int)st.st_mtime << "." << (int)st.st_size << "\", " - "convexity = " << std::dec << this->convexity << ")"; - break; - case TYPE_DXF: - stream << "(file = \"" << this->filename << "\", " - "cache = \"" << std::hex << (int)st.st_mtime << "." << (int)st.st_size << "\", " - "layer = \"" << this->layername << "\", " - "origin = [ " << std::dec << this->origin_x << " " << this->origin_y << " ], " - "scale = " << this->scale << ", " - "convexity = " << this->convexity << ", " - "$fn = " << this->fn << ", $fa = " << this->fa << ", $fs = " << this->fs << ")"; - break; - default: - assert(false); - } + stream << "(file = \"" << this->filename << "\", " + "cache = \"" << std::hex << (int)st.st_mtime << "." << (int)st.st_size << "\", " + "layer = \"" << this->layername << "\", " + "origin = [ " << std::dec << this->origin_x << " " << this->origin_y << " ], " + "scale = " << this->scale << ", " + "convexity = " << this->convexity << ", " + "$fn = " << this->fn << ", $fa = " << this->fa << ", $fs = " << this->fs << ")"; return stream.str(); } std::string ImportNode::name() const { - switch (this->type) { - case TYPE_STL: - return "import_stl"; - break; - case TYPE_OFF: - return "import_off"; - break; - case TYPE_DXF: - return "import_dxf"; - break; - default: - assert(false); - } + return "import"; +} + +void register_builtin_import() +{ + builtin_modules["import_stl"] = new ImportModule(TYPE_STL); + builtin_modules["import_off"] = new ImportModule(TYPE_OFF); + builtin_modules["import_dxf"] = new ImportModule(TYPE_DXF); + builtin_modules["import"] = new ImportModule(); } + |