From f40f7ef072927cd9ea18104e66217c31cfd810db Mon Sep 17 00:00:00 2001 From: Marius Kintel Date: Wed, 7 Sep 2011 00:47:55 +0200 Subject: [PATCH] Added support for an untyped import() module, deprecating the explicitly typed import_*() ones --- src/context.cc | 11 +++++- src/import.cc | 94 ++++++++++++++++++++---------------------------- src/importnode.h | 1 + 3 files changed, 49 insertions(+), 57 deletions(-) diff --git a/src/context.cc b/src/context.cc index 0100234c..b4983f62 100644 --- a/src/context.cc +++ b/src/context.cc @@ -153,9 +153,18 @@ AbstractNode *Context::evaluate_module(const ModuleInstantiation &inst) const if (m == builtin_modules["dxf_linear_extrude"]) { PRINTF("DEPRECATED: The dxf_linear_extrude() module will be removed in future releases. Use a linear_extrude() instead."); } - if (m == builtin_modules["dxf_rotate_extrude"]) { + else if (m == builtin_modules["dxf_rotate_extrude"]) { PRINTF("DEPRECATED: The dxf_rotate_extrude() module will be removed in future releases. Use a rotate_extrude() instead."); } + else if (m == builtin_modules["import_stl"]) { + PRINTF("DEPRECATED: The import_stl() module will be removed in future releases. Use import() instead."); + } + else if (m == builtin_modules["import_dxf"]) { + PRINTF("DEPRECATED: The import_dxf() module will be removed in future releases. Use import() instead."); + } + else if (m == builtin_modules["import_off"]) { + PRINTF("DEPRECATED: The import_off() module will be removed in future releases. Use import() instead."); + } return m->evaluate(this, &inst); } if (this->usedlibs_p) { diff --git a/src/import.cc b/src/import.cc index 447c139b..17ad3f2d 100644 --- a/src/import.cc +++ b/src/import.cc @@ -49,20 +49,14 @@ 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); - std::vector argnames; - if (this->type == TYPE_DXF) { - argnames += "file", "layer", "convexity", "origin", "scale"; - } else { - argnames += "file", "convexity"; - } + argnames += "file", "layer", "convexity", "origin", "scale"; std::vector argexpr; // Map old argnames to new argnames for compatibility @@ -77,13 +71,23 @@ 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.getAbsolutePath(v.text); -// node->filename = c.getAbsolutePath(c.lookup_variable("file").text); + node->filename = filename; node->layername = c.lookup_variable("layer", true).text; node->convexity = c.lookup_variable("convexity", true).num; @@ -102,13 +106,6 @@ AbstractNode *ImportModule::evaluate(const Context *ctx, const ModuleInstantiati return node; } -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); -} - PolySet *ImportNode::evaluate_polyset(render_mode_e, class PolySetEvaluator *) const { PolySet *p = new PolySet(); @@ -191,18 +188,22 @@ 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."); } - if (this->type == TYPE_DXF) + else if (this->type == TYPE_DXF) { 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); } + else + { + PRINTF("ERROR: Unsupported file format while trying to import file '%s'", this->filename.c_str()); + } return p; } @@ -217,46 +218,27 @@ std::string ImportNode::toString() const 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(); +} + diff --git a/src/importnode.h b/src/importnode.h index 3fcdb37c..49e9f169 100644 --- a/src/importnode.h +++ b/src/importnode.h @@ -5,6 +5,7 @@ #include "visitor.h" enum import_type_e { + TYPE_UNKNOWN, TYPE_STL, TYPE_OFF, TYPE_DXF