Merge pull request #1037 from openscad/issue949

Use FileContext when evaluating $vp{rtd} variables (fixes #949).
master
Marius Kintel 2014-12-01 10:56:32 -05:00
commit d6d814586c
3 changed files with 48 additions and 36 deletions

View File

@ -938,6 +938,7 @@ void MainWindow::compileDone(bool didchange)
const char *callslot; const char *callslot;
if (didchange) { if (didchange) {
instantiateRoot(); instantiateRoot();
updateCamera();
callslot = afterCompileSlot; callslot = afterCompileSlot;
} }
else { else {
@ -1525,34 +1526,27 @@ void MainWindow::updateCamera()
double rz = cam.object_rot.z(); double rz = cam.object_rot.z();
double d = cam.viewer_distance; double d = cam.viewer_distance;
ModuleContext mc(&top_ctx, NULL); double x, y, z;
mc.initializeModule(*root_module); const Value vpr = root_module->lookup_variable("$vpr");
if (vpr.getVec3(x, y, z)) {
rx = x;
ry = y;
rz = z;
camera_set = true;
}
BOOST_FOREACH(const Assignment &a, root_module->scope.assignments) { const Value vpt = root_module->lookup_variable("$vpt");
double x, y, z; if (vpt.getVec3(x, y, z)) {
if ("$vpr" == a.first) { tx = x;
const Value vpr = a.second.get()->evaluate(&mc); ty = y;
if (vpr.getVec3(x, y, z)) { tz = z;
rx = x; camera_set = true;
ry = y; }
rz = z;
camera_set = true; const Value vpd = root_module->lookup_variable("$vpd");
} if (vpd.type() == Value::NUMBER) {
} else if ("$vpt" == a.first) { d = vpd.toDouble();
const Value vpt = a.second.get()->evaluate(&mc); camera_set = true;
if (vpt.getVec3(x, y, z)) {
tx = x;
ty = y;
tz = z;
camera_set = true;
}
} else if ("$vpd" == a.first) {
const Value vpd = a.second.get()->evaluate(&mc);
if (vpd.type() == Value::NUMBER) {
d = vpd.toDouble();
camera_set = true;
}
}
} }
if (camera_set) { if (camera_set) {
@ -1613,8 +1607,6 @@ void MainWindow::compileTopLevelDocument()
this->root_module = parse(fulltext.c_str(), this->root_module = parse(fulltext.c_str(),
this->fileName.isEmpty() ? "" : this->fileName.isEmpty() ? "" :
QFileInfo(this->fileName).absolutePath().toLocal8Bit(), false); QFileInfo(this->fileName).absolutePath().toLocal8Bit(), false);
updateCamera();
} }
void MainWindow::checkAutoReload() void MainWindow::checkAutoReload()

View File

@ -238,6 +238,11 @@ std::string Module::dump(const std::string &indent, const std::string &name) con
return dump.str(); return dump.str();
} }
FileModule::~FileModule()
{
delete context;
}
void FileModule::registerUse(const std::string path) { void FileModule::registerUse(const std::string path) {
std::string extraw = boosty::extension_str(fs::path(path)); std::string extraw = boosty::extension_str(fs::path(path));
std::string ext = boost::algorithm::to_lower_copy(extraw); std::string ext = boost::algorithm::to_lower_copy(extraw);
@ -346,19 +351,31 @@ bool FileModule::handleDependencies()
return somethingchanged; return somethingchanged;
} }
AbstractNode *FileModule::instantiate(const Context *ctx, const ModuleInstantiation *inst, EvalContext *evalctx) const AbstractNode *FileModule::instantiate(const Context *ctx, const ModuleInstantiation *inst, EvalContext *evalctx)
{ {
assert(evalctx == NULL); assert(evalctx == NULL);
FileContext c(*this, ctx);
c.initializeModule(*this); delete context;
context = new FileContext(*this, ctx);
context->initializeModule(*this);
// FIXME: Set document path to the path of the module // FIXME: Set document path to the path of the module
#if 0 && DEBUG #if 0 && DEBUG
c.dump(this, inst); c.dump(this, inst);
#endif #endif
AbstractNode *node = new AbstractNode(inst); AbstractNode *node = new AbstractNode(inst);
std::vector<AbstractNode *> instantiatednodes = this->scope.instantiateChildren(&c); std::vector<AbstractNode *> instantiatednodes = this->scope.instantiateChildren(context);
node->children.insert(node->children.end(), instantiatednodes.begin(), instantiatednodes.end()); node->children.insert(node->children.end(), instantiatednodes.begin(), instantiatednodes.end());
return node; return node;
} }
Value FileModule::lookup_variable(const std::string &name) const
{
if (!context) {
return Value::undefined;
}
return context->lookup_variable(name, true);
}

View File

@ -99,8 +99,8 @@ private:
class FileModule : public Module class FileModule : public Module
{ {
public: public:
FileModule() : is_handling_dependencies(false) {} FileModule() : context(NULL), is_handling_dependencies(false) {}
virtual ~FileModule() {} virtual ~FileModule();
void setModulePath(const std::string &path) { this->path = path; } void setModulePath(const std::string &path) { this->path = path; }
const std::string &modulePath() const { return this->path; } const std::string &modulePath() const { return this->path; }
@ -108,14 +108,17 @@ public:
void registerInclude(const std::string &localpath, const std::string &fullpath); void registerInclude(const std::string &localpath, const std::string &fullpath);
bool includesChanged() const; bool includesChanged() const;
bool handleDependencies(); bool handleDependencies();
virtual AbstractNode *instantiate(const Context *ctx, const ModuleInstantiation *inst, EvalContext *evalctx = NULL) const; virtual AbstractNode *instantiate(const Context *ctx, const ModuleInstantiation *inst, EvalContext *evalctx = NULL);
bool hasIncludes() const { return !this->includes.empty(); } bool hasIncludes() const { return !this->includes.empty(); }
bool usesLibraries() const { return !this->usedlibs.empty(); } bool usesLibraries() const { return !this->usedlibs.empty(); }
bool isHandlingDependencies() const { return this->is_handling_dependencies; } bool isHandlingDependencies() const { return this->is_handling_dependencies; }
Value lookup_variable(const std::string &name) const;
typedef boost::unordered_set<std::string> ModuleContainer; typedef boost::unordered_set<std::string> ModuleContainer;
ModuleContainer usedlibs; ModuleContainer usedlibs;
private: private:
/** Reference to retain the context that was used in the last evaluation */
class FileContext *context;
struct IncludeFile { struct IncludeFile {
std::string filename; std::string filename;
bool valid; bool valid;