diff options
author | Marius Kintel <marius@kintel.net> | 2013-05-09 10:18:51 (GMT) |
---|---|---|
committer | Marius Kintel <marius@kintel.net> | 2013-05-09 10:18:51 (GMT) |
commit | f46118da22f806d05b410cced0f9b06ea00ac02c (patch) | |
tree | e42fece860be6d224744ba5401bf35e783dddd62 /src | |
parent | f8ad66c8bbb3c78548c2e4b3b930f82d4a57b345 (diff) | |
parent | ad8ba77a15efd745fe7845e085b3daefd3f58e81 (diff) |
Merge branch 'master' into issue352
Diffstat (limited to 'src')
-rw-r--r-- | src/dxfdim.cc | 4 | ||||
-rw-r--r-- | src/fileutils.cc | 35 | ||||
-rw-r--r-- | src/fileutils.h | 9 | ||||
-rw-r--r-- | src/import.cc | 3 | ||||
-rw-r--r-- | src/linearextrude.cc | 3 | ||||
-rw-r--r-- | src/mainwin.cc | 12 | ||||
-rw-r--r-- | src/module.cc | 5 | ||||
-rw-r--r-- | src/openscad.cc | 3 | ||||
-rw-r--r-- | src/rotateextrude.cc | 3 | ||||
-rw-r--r-- | src/surface.cc | 3 |
10 files changed, 71 insertions, 9 deletions
diff --git a/src/dxfdim.cc b/src/dxfdim.cc index 555ed49..66842d2 100644 --- a/src/dxfdim.cc +++ b/src/dxfdim.cc @@ -30,6 +30,7 @@ #include "dxfdata.h" #include "builtin.h" #include "printutils.h" +#include "fileutils.h" #include "evalcontext.h" #include "mathc99.h" @@ -54,7 +55,8 @@ Value builtin_dxf_dim(const Context *ctx, const EvalContext *evalctx) // See issue #217 for (size_t i = 0; i < evalctx->numArgs(); i++) { if (evalctx->getArgName(i) == "file") - filename = evalctx->getAbsolutePath(evalctx->getArgValue(i).toString()); + filename = lookup_file(evalctx->getArgValue(i).toString(), + evalctx->documentPath(), ctx->documentPath()); if (evalctx->getArgName(i) == "layer") layername = evalctx->getArgValue(i).toString(); if (evalctx->getArgName(i) == "origin") diff --git a/src/fileutils.cc b/src/fileutils.cc new file mode 100644 index 0000000..b844b4a --- /dev/null +++ b/src/fileutils.cc @@ -0,0 +1,35 @@ +#include "fileutils.h" +#include "printutils.h" + +#include <boost/filesystem.hpp> +namespace fs = boost::filesystem; +#include "boosty.h" + +/*! + Returns the absolute path to the given filename, unless it's empty. + If the file isn't found in the given path, the fallback path will be + used to be backwards compatible with <= 2013.01 (see issue #217). +*/ +std::string lookup_file(const std::string &filename, + const std::string &path, const std::string &fallbackpath) +{ + std::string resultfile; + if (!filename.empty() && !boosty::is_absolute(fs::path(filename))) { + fs::path absfile; + if (!path.empty()) absfile = boosty::absolute(fs::path(path) / filename); + fs::path absfile_fallback; + if (!fallbackpath.empty()) absfile_fallback = boosty::absolute(fs::path(fallbackpath) / filename); + + if (!fs::exists(absfile) && fs::exists(absfile_fallback)) { + resultfile = absfile_fallback.string(); + PRINTB("WARNING: Imported file (%s) found in document root instead of relative to the importing module. This behavior is deprecated", filename); + } + else { + resultfile = absfile.string(); + } + } + else { + resultfile = filename; + } + return resultfile; +} diff --git a/src/fileutils.h b/src/fileutils.h new file mode 100644 index 0000000..1f68cdb --- /dev/null +++ b/src/fileutils.h @@ -0,0 +1,9 @@ +#ifndef FILEUTILS_H_ +#define FILEUTILS_H_ + +#include <string> + +std::string lookup_file(const std::string &filename, + const std::string &path, const std::string &fallbackpath); + +#endif diff --git a/src/import.cc b/src/import.cc index bd8f830..bb44d70 100644 --- a/src/import.cc +++ b/src/import.cc @@ -33,6 +33,7 @@ #include "dxfdata.h" #include "dxftess.h" #include "printutils.h" +#include "fileutils.h" #include "handle_dep.h" // handle_dep() #ifdef ENABLE_CGAL @@ -97,7 +98,7 @@ AbstractNode *ImportModule::instantiate(const Context *ctx, const ModuleInstanti PRINT("DEPRECATED: filename= is deprecated. Please use file="); } } - std::string filename = inst->getAbsolutePath(v.isUndefined() ? "" : v.toString()); + std::string filename = lookup_file(v.isUndefined() ? "" : v.toString(), inst->path(), ctx->documentPath()); import_type_e actualtype = this->type; if (actualtype == TYPE_UNKNOWN) { std::string extraw = boosty::extension_str( fs::path(filename) ); diff --git a/src/linearextrude.cc b/src/linearextrude.cc index 4953781..bc678e6 100644 --- a/src/linearextrude.cc +++ b/src/linearextrude.cc @@ -29,6 +29,7 @@ #include "module.h" #include "evalcontext.h" #include "printutils.h" +#include "fileutils.h" #include "builtin.h" #include "PolySetEvaluator.h" #include "openscad.h" // get_fragments_from_r() @@ -74,7 +75,7 @@ AbstractNode *LinearExtrudeModule::instantiate(const Context *ctx, const ModuleI if (!file.isUndefined() && file.type() == Value::STRING) { PRINT("DEPRECATED: Support for reading files in linear_extrude will be removed in future releases. Use a child import() instead."); - node->filename = inst->getAbsolutePath(file.toString()); + node->filename = lookup_file(file.toString(), inst->path(), c.documentPath()); } // if height not given, and first argument is a number, diff --git a/src/mainwin.cc b/src/mainwin.cc index 027f72a..5f9d633 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -488,17 +488,25 @@ void MainWindow::requestOpenFile(const QString &) void MainWindow::openFile(const QString &new_filename) { + QString actual_filename = new_filename; #ifdef ENABLE_MDI if (!editor->toPlainText().isEmpty()) { - new MainWindow(new_filename); + QFileInfo fi(new_filename); + if (fi.suffix().toLower().contains(QRegExp("^(stl|off|dxf)$"))) { + actual_filename = QString(); + } + new MainWindow(actual_filename); clearCurrentOutput(); return; } #endif - setFileName(new_filename); + setFileName(actual_filename); refreshDocument(); updateRecentFiles(); + if (actual_filename.isEmpty()) { + this->editor->setPlainText(QString("import(\"%1\");\n").arg(new_filename)); + } } void diff --git a/src/module.cc b/src/module.cc index 9503f05..8b84c07 100644 --- a/src/module.cc +++ b/src/module.cc @@ -71,7 +71,10 @@ IfElseModuleInstantiation::~IfElseModuleInstantiation() /*! Returns the absolute path to the given filename, unless it's empty. - */ + + NB! This will actually search for the file, to be backwards compatible with <= 2013.01 + (see issue #217) +*/ std::string ModuleInstantiation::getAbsolutePath(const std::string &filename) const { if (!filename.empty() && !boosty::is_absolute(fs::path(filename))) { diff --git a/src/openscad.cc b/src/openscad.cc index 6a0d057..7c54762 100644 --- a/src/openscad.cc +++ b/src/openscad.cc @@ -359,7 +359,8 @@ int main(int argc, char **argv) fs::path fpath = boosty::absolute(fs::path(filename)); fs::path fparent = fpath.parent_path(); fs::current_path(fparent); - + top_ctx.setDocumentPath(fparent.string()); + AbstractNode::resetIndexCounter(); absolute_root_node = root_module->instantiate(&top_ctx, &root_inst, NULL); diff --git a/src/rotateextrude.cc b/src/rotateextrude.cc index 2f9a28b..e073a69 100644 --- a/src/rotateextrude.cc +++ b/src/rotateextrude.cc @@ -28,6 +28,7 @@ #include "module.h" #include "evalcontext.h" #include "printutils.h" +#include "fileutils.h" #include "builtin.h" #include "polyset.h" #include "visitor.h" @@ -70,7 +71,7 @@ AbstractNode *RotateExtrudeModule::instantiate(const Context *ctx, const ModuleI if (!file.isUndefined()) { PRINT("DEPRECATED: Support for reading files in rotate_extrude will be removed in future releases. Use a child import() instead."); - node->filename = inst->getAbsolutePath(file.toString()); + node->filename = lookup_file(file.toString(), inst->path(), c.documentPath()); } node->layername = layer.isUndefined() ? "" : layer.toString(); diff --git a/src/surface.cc b/src/surface.cc index b3246c1..46ddc07 100644 --- a/src/surface.cc +++ b/src/surface.cc @@ -30,6 +30,7 @@ #include "evalcontext.h" #include "builtin.h" #include "printutils.h" +#include "fileutils.h" #include "handle_dep.h" // handle_dep() #include "visitor.h" @@ -82,7 +83,7 @@ AbstractNode *SurfaceModule::instantiate(const Context *ctx, const ModuleInstant c.setVariables(args, evalctx); Value fileval = c.lookup_variable("file"); - node->filename = inst->getAbsolutePath(fileval.isUndefined() ? "" : fileval.toString()); + node->filename = lookup_file(fileval.isUndefined() ? "" : fileval.toString(), inst->path(), c.documentPath()); Value center = c.lookup_variable("center", true); if (center.type() == Value::BOOL) { |