I think this should fix issue #217

felipesanches-svg
Marius Kintel 2013-04-05 01:30:09 -04:00
parent c7f83aa3e3
commit 1b8b7aa5fa
18 changed files with 2080 additions and 42 deletions

View File

@ -49,6 +49,9 @@ Value builtin_dxf_dim(const Context *ctx, const std::vector<std::string> &argnam
double yorigin = 0;
double scale = 1;
// FIXME: We don't lookup the file relative to where this function was instantiated
// since the path is only available for ModuleInstantiations, not function expressions.
// See isse #217
for (size_t i = 0; i < argnames.size() && i < args.size(); i++) {
if (argnames[i] == "file")
filename = ctx->getAbsolutePath(args[i].toString());
@ -63,9 +66,16 @@ Value builtin_dxf_dim(const Context *ctx, const std::vector<std::string> &argnam
}
std::stringstream keystream;
fs::path filepath(filename);
uintmax_t filesize = -1;
time_t lastwritetime = -1;
if (fs::exists(filepath) && fs::is_regular_file(filepath)) {
filesize = fs::file_size(filepath);
lastwritetime = fs::last_write_time(filepath);
}
keystream << filename << "|" << layername << "|" << name << "|" << xorigin
<< "|" << yorigin <<"|" << scale << "|" << fs::last_write_time(filename)
<< "|" << fs::file_size(filename);
<< "|" << yorigin <<"|" << scale << "|" << lastwritetime
<< "|" << filesize;
std::string key = keystream.str();
if (dxf_dim_cache.find(key) != dxf_dim_cache.end())
return dxf_dim_cache.find(key)->second;
@ -133,6 +143,9 @@ Value builtin_dxf_cross(const Context *ctx, const std::vector<std::string> &argn
double yorigin = 0;
double scale = 1;
// FIXME: We don't lookup the file relative to where this function was instantiated
// since the path is only available for ModuleInstantiations, not function expressions.
// See isse #217
for (size_t i = 0; i < argnames.size() && i < args.size(); i++) {
if (argnames[i] == "file")
filename = ctx->getAbsolutePath(args[i].toString());

View File

@ -80,7 +80,7 @@ AbstractNode *ImportModule::evaluate(const Context *ctx, const ModuleInstantiati
c.args(argnames, argexpr, inst_argnames, inst->argvalues);
Value v = c.lookup_variable("file");
std::string filename = c.getAbsolutePath(v.isUndefined() ? "" : v.toString());
std::string filename = inst->getAbsolutePath(v.isUndefined() ? "" : v.toString());
import_type_e actualtype = this->type;
if (actualtype == TYPE_UNKNOWN) {
std::string extraw = boosty::extension_str( fs::path(filename) );

View File

@ -75,7 +75,7 @@ AbstractNode *LinearExtrudeModule::evaluate(const Context *ctx, const ModuleInst
if (!file.isUndefined()) {
PRINT("DEPRECATED: Support for reading files in linear_extrude will be removed in future releases. Use a child import() instead.");
node->filename = c.getAbsolutePath(file.toString());
node->filename = inst->getAbsolutePath(file.toString());
}
// if height not given, and first argument is a number,

View File

@ -32,6 +32,9 @@
#include "function.h"
#include "printutils.h"
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#include "boosty.h"
#include <boost/foreach.hpp>
#include <sstream>
#include <sys/stat.h>
@ -67,6 +70,19 @@ IfElseModuleInstantiation::~IfElseModuleInstantiation()
BOOST_FOREACH (ModuleInstantiation *v, else_children) delete v;
}
/*!
Returns the absolute path to the given filename, unless it's empty.
*/
std::string ModuleInstantiation::getAbsolutePath(const std::string &filename) const
{
if (!filename.empty() && !boosty::is_absolute(fs::path(filename))) {
return boosty::absolute(fs::path(this->modpath) / filename).string();
}
else {
return filename;
}
}
std::string ModuleInstantiation::dump(const std::string &indent) const
{
std::stringstream dump;
@ -152,15 +168,15 @@ AbstractNode *Module::evaluate(const Context *ctx, const ModuleInstantiation *in
c.functions_p = &functions;
c.modules_p = &modules;
if (!usedlibs.empty())
c.usedlibs_p = &usedlibs;
else
c.usedlibs_p = NULL;
BOOST_FOREACH(const std::string &var, assignments_var) {
c.set_variable(var, assignments.at(var)->evaluate(&c));
}
}
AbstractNode *node = new AbstractNode(inst);
for (size_t i = 0; i < children.size(); i++) {

View File

@ -10,7 +10,7 @@
class ModuleInstantiation
{
public:
ModuleInstantiation(const std::string &name = "")
ModuleInstantiation(const std::string &name = "")
: ctx(NULL),
tag_root(false), tag_highlight(false), tag_background(false), modname(name) { }
virtual ~ModuleInstantiation();
@ -19,6 +19,10 @@ public:
class AbstractNode *evaluate(const class Context *ctx) const;
std::vector<AbstractNode*> evaluateChildren(const Context *ctx = NULL) const;
void setPath(const std::string &path) { this->modpath = path; }
const std::string &path() const { return this->modpath; }
std::string getAbsolutePath(const std::string &filename) const;
const std::string &name() const { return this->modname; }
bool isBackground() const { return this->tag_background; }
bool isHighlight() const { return this->tag_highlight; }
@ -35,6 +39,7 @@ public:
bool tag_background;
protected:
std::string modname;
std::string modpath;
friend class Module;
};
@ -61,6 +66,7 @@ class Module : public AbstractModule
public:
Module() : is_handling_dependencies(false) { }
virtual ~Module();
virtual AbstractNode *evaluate(const Context *ctx, const ModuleInstantiation *inst) const;
virtual std::string dump(const std::string &indent, const std::string &name) const;

View File

@ -58,6 +58,12 @@ int lexerlex(void);
std::vector<Module*> module_stack;
Module *currmodule;
extern void lexerdestroy();
extern FILE *lexerin;
extern const char *parser_input_buffer;
const char *parser_input_buffer;
std::string parser_source_path;
class ArgContainer {
public:
std::string argname;
@ -77,6 +83,7 @@ public:
class Value *value;
class Expression *expr;
class ModuleInstantiation *inst;
std::vector<ModuleInstantiation*> *instvec;
class IfElseModuleInstantiation *ifelse;
class ArgContainer *arg;
class ArgsContainer *args;
@ -117,8 +124,8 @@ public:
%type <inst> module_instantiation
%type <ifelse> if_statement
%type <ifelse> ifelse_statement
%type <inst> children_instantiation
%type <inst> module_instantiation_list
%type <instvec> children_instantiation
%type <instvec> module_instantiation_list
%type <inst> single_module_instantiation
%type <args> arguments_call
@ -182,9 +189,9 @@ statement:
/* Will return a dummy parent node with zero or more children */
children_instantiation:
module_instantiation {
$$ = new ModuleInstantiation();
$$ = new std::vector<ModuleInstantiation*>;
if ($1) {
$$->children.push_back($1);
$$->push_back($1);
}
} |
'{' module_instantiation_list '}' {
@ -196,14 +203,14 @@ if_statement:
$$ = new IfElseModuleInstantiation();
$$->argnames.push_back("");
$$->argexpr.push_back($3);
$$->setPath(parser_source_path);
if ($$) {
$$->children = $5->children;
$$->children = *$5;
} else {
for (size_t i = 0; i < $5->children.size(); i++)
delete $5->children[i];
for (size_t i = 0; i < $5->size(); i++)
delete (*$5)[i];
}
$5->children.clear();
delete $5;
} ;
@ -214,12 +221,11 @@ ifelse_statement:
if_statement TOK_ELSE children_instantiation {
$$ = $1;
if ($$) {
$$->else_children = $3->children;
$$->else_children = *$3;
} else {
for (size_t i = 0; i < $3->children.size(); i++)
delete $3->children[i];
for (size_t i = 0; i < $3->size(); i++)
delete (*$3)[i];
}
$3->children.clear();
delete $3;
} ;
@ -246,12 +252,11 @@ module_instantiation:
single_module_instantiation children_instantiation {
$$ = $1;
if ($$) {
$$->children = $2->children;
$$->children = *$2;
} else {
for (size_t i = 0; i < $2->children.size(); i++)
delete $2->children[i];
for (size_t i = 0; i < $2->size(); i++)
delete (*$2)[i];
}
$2->children.clear();
delete $2;
} |
ifelse_statement {
@ -260,12 +265,12 @@ module_instantiation:
module_instantiation_list:
/* empty */ {
$$ = new ModuleInstantiation();
$$ = new std::vector<ModuleInstantiation*>;
} |
module_instantiation_list module_instantiation {
$$ = $1;
if ($$) {
if ($2) $$->children.push_back($2);
if ($2) $$->push_back($2);
} else {
delete $2;
}
@ -276,6 +281,7 @@ single_module_instantiation:
$$ = new ModuleInstantiation($1);
$$->argnames = $3->argnames;
$$->argexpr = $3->argexpr;
$$->setPath(parser_source_path);
free($1);
delete $3;
}
@ -536,18 +542,12 @@ void yyerror (char const *s)
currmodule = NULL;
}
extern void lexerdestroy();
extern FILE *lexerin;
extern const char *parser_input_buffer;
const char *parser_input_buffer;
std::string parser_source_path;
Module *parse(const char *text, const char *path, int debug)
{
lexerin = NULL;
parser_error_pos = -1;
parser_input_buffer = text;
parser_source_path = std::string(path);
parser_source_path = boosty::absolute(std::string(path)).string();
module_stack.clear();
Module *rootmodule = currmodule = new Module();

View File

@ -71,7 +71,7 @@ AbstractNode *RotateExtrudeModule::evaluate(const Context *ctx, const ModuleInst
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 = c.getAbsolutePath(file.toString());
node->filename = inst->getAbsolutePath(file.toString());
}
node->layername = layer.isUndefined() ? "" : layer.toString();

View File

@ -83,7 +83,7 @@ AbstractNode *SurfaceModule::evaluate(const Context *ctx, const ModuleInstantiat
c.args(argnames, argexpr, inst->argnames, inst->argvalues);
Value fileval = c.lookup_variable("file");
node->filename = c.getAbsolutePath(fileval.isUndefined() ? "" : fileval.toString());
node->filename = inst->getAbsolutePath(fileval.isUndefined() ? "" : fileval.toString());
Value center = c.lookup_variable("center", true);
if (center.type() == Value::BOOL) {

View File

@ -0,0 +1,3 @@
use <localfiles_dir/localfiles_module.scad>
localfiles_module();

View File

@ -0,0 +1,2 @@
0 1
2 3

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,10 @@
module localfiles_module()
{
linear_extrude(h=100) import("localfile.dxf");
translate([-250,0,0]) linear_extrude(file="localfile.dxf");
translate([0,350,0]) rotate_extrude(file="localfile.dxf");
translate([250,0,0]) scale([200,200,50]) surface("localfile.dat");
// This is not supported:
// echo(dxf_dim(file="localfile.dxf", name="localfile"));
}

View File

@ -769,12 +769,15 @@ list(APPEND ECHO_FILES ${FUNCTION_FILES}
list(APPEND DUMPTEST_FILES ${MINIMAL_FILES} ${FEATURES_FILES} ${EXAMPLE_FILES})
list(APPEND DUMPTEST_FILES ${CMAKE_SOURCE_DIR}/../testdata/scad/misc/escape-test.scad
${CMAKE_SOURCE_DIR}/../testdata/scad/misc/include-tests.scad
${CMAKE_SOURCE_DIR}/../testdata/scad/misc/use-tests.scad)
${CMAKE_SOURCE_DIR}/../testdata/scad/misc/use-tests.scad
${CMAKE_SOURCE_DIR}/../testdata/scad/misc/localfiles-test.scad)
list(APPEND CGALPNGTEST_FILES ${FEATURES_FILES} ${SCAD_DXF_FILES} ${EXAMPLE_FILES})
list(APPEND CGALPNGTEST_FILES ${CMAKE_SOURCE_DIR}/../testdata/scad/misc/include-tests.scad
${CMAKE_SOURCE_DIR}/../testdata/scad/misc/use-tests.scad
${CMAKE_SOURCE_DIR}/../testdata/scad/bugs/transform-nan-inf-tests.scad)
${CMAKE_SOURCE_DIR}/../testdata/scad/bugs/transform-nan-inf-tests.scad
${CMAKE_SOURCE_DIR}/../testdata/scad/misc/localfiles-test.scad)
list(APPEND OPENCSGTEST_FILES ${CGALPNGTEST_FILES})
list(APPEND OPENCSGTEST_FILES ${CMAKE_SOURCE_DIR}/../testdata/scad/bugs/bbox-transform-bug.scad)
list(APPEND OPENCSGTEST_FILES ${CMAKE_SOURCE_DIR}/../testdata/scad/bugs/intersection-prune-test.scad)

View File

@ -118,7 +118,6 @@ int main(int argc, char **argv)
exit(1);
}
fs::current_path(original_path);
std::ofstream outfile;
outfile.open(outfilename);
outfile << dumpstdstr << "\n";
@ -127,21 +126,22 @@ int main(int argc, char **argv)
delete root_node;
delete root_module;
fs::current_path(original_path);
root_module = parsefile(outfilename);
if (!root_module) {
fprintf(stderr, "Error: Unable to read back dumped file\n");
exit(1);
}
if (fs::path(filename).has_parent_path()) {
fs::current_path(fs::path(filename).parent_path());
}
AbstractNode::resetIndexCounter();
root_node = root_module->evaluate(&root_ctx, &root_inst);
tree.setRoot(root_node);
if (fs::path(outfilename).has_parent_path()) {
fs::current_path(fs::path(outfilename).parent_path());
}
string readbackstr = dumptree(tree, *root_node);
if (dumpstdstr != readbackstr) {
fprintf(stderr, "Error: Readback is different from original dump:\n");

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

View File

@ -0,0 +1,17 @@
group() {
linear_extrude(height = 100, center = false, convexity = 1, $fn = 0, $fa = 12, $fs = 2) {
import(file = "localfiles_dir/localfile.dxf", layer = "", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2);
}
multmatrix([[1, 0, 0, -250], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) {
linear_extrude(file = "localfiles_dir/localfile.dxf", layer = "", origin = [0, 0], scale = 1, height = 100, center = false, convexity = 1, $fn = 0, $fa = 12, $fs = 2);
}
multmatrix([[1, 0, 0, 0], [0, 1, 0, 350], [0, 0, 1, 0], [0, 0, 0, 1]]) {
rotate_extrude(file = "localfiles_dir/localfile.dxf", layer = "", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2);
}
multmatrix([[1, 0, 0, 250], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) {
multmatrix([[200, 0, 0, 0], [0, 200, 0, 0], [0, 0, 50, 0], [0, 0, 0, 1]]) {
surface(file = "localfiles_dir/localfile.dat", center = false);
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB