2009-07-17 03:11:08 +04:00
|
|
|
/*
|
2011-01-21 04:21:09 +03:00
|
|
|
* OpenSCAD (www.openscad.org)
|
|
|
|
* Copyright (C) 2009-2011 Clifford Wolf <clifford@clifford.at> and
|
|
|
|
* Marius Kintel <marius@kintel.net>
|
2009-07-17 03:11:08 +04:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2010-02-01 12:34:18 +03:00
|
|
|
* As a special exception, you have permission to link this program
|
|
|
|
* with the CGAL library and distribute executables, as long as you
|
|
|
|
* follow the requirements of the GNU GPL in regard to all of the
|
|
|
|
* software in the executable aside from CGAL.
|
|
|
|
*
|
2009-07-17 03:11:08 +04:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-09-30 05:11:28 +04:00
|
|
|
#include "linearextrudenode.h"
|
2010-03-02 21:22:31 +03:00
|
|
|
|
2010-01-30 07:26:05 +03:00
|
|
|
#include "module.h"
|
2013-04-09 08:28:16 +04:00
|
|
|
#include "evalcontext.h"
|
2009-12-09 17:22:50 +03:00
|
|
|
#include "printutils.h"
|
2013-05-09 14:12:58 +04:00
|
|
|
#include "fileutils.h"
|
2010-01-30 07:26:05 +03:00
|
|
|
#include "builtin.h"
|
2013-12-28 02:48:15 +04:00
|
|
|
#include "calc.h"
|
2013-12-22 10:43:08 +04:00
|
|
|
#include "polyset.h"
|
2011-10-11 03:01:51 +04:00
|
|
|
#include "mathc99.h"
|
2009-07-17 03:11:08 +04:00
|
|
|
|
2010-03-02 21:22:31 +03:00
|
|
|
#include <sstream>
|
2011-09-03 08:10:36 +04:00
|
|
|
#include <boost/assign/std/vector.hpp>
|
|
|
|
using namespace boost::assign; // bring 'operator+=()' into scope
|
2009-07-23 20:00:38 +04:00
|
|
|
|
2012-07-15 05:57:41 +04:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
2011-09-30 05:11:28 +04:00
|
|
|
class LinearExtrudeModule : public AbstractModule
|
2009-07-17 03:11:08 +04:00
|
|
|
{
|
|
|
|
public:
|
2011-09-30 05:11:28 +04:00
|
|
|
LinearExtrudeModule() { }
|
2013-04-20 01:52:01 +04:00
|
|
|
virtual AbstractNode *instantiate(const Context *ctx, const ModuleInstantiation *inst, const EvalContext *evalctx) const;
|
2009-07-17 03:11:08 +04:00
|
|
|
};
|
|
|
|
|
2013-04-20 01:52:01 +04:00
|
|
|
AbstractNode *LinearExtrudeModule::instantiate(const Context *ctx, const ModuleInstantiation *inst, const EvalContext *evalctx) const
|
2009-07-17 03:11:08 +04:00
|
|
|
{
|
2011-09-30 05:11:28 +04:00
|
|
|
LinearExtrudeNode *node = new LinearExtrudeNode(inst);
|
2009-07-17 03:11:08 +04:00
|
|
|
|
2013-04-19 02:34:14 +04:00
|
|
|
AssignmentList args;
|
2014-03-23 22:38:28 +04:00
|
|
|
args += Assignment("file"), Assignment("layer"), Assignment("height"), Assignment("origin"), Assignment("scale"), Assignment("center"), Assignment("twist"), Assignment("slices");
|
2009-07-17 03:11:08 +04:00
|
|
|
|
|
|
|
Context c(ctx);
|
2013-04-19 02:34:14 +04:00
|
|
|
c.setVariables(args, evalctx);
|
2009-07-17 03:11:08 +04:00
|
|
|
|
2012-03-28 02:05:00 +04:00
|
|
|
node->fn = c.lookup_variable("$fn").toDouble();
|
|
|
|
node->fs = c.lookup_variable("$fs").toDouble();
|
|
|
|
node->fa = c.lookup_variable("$fa").toDouble();
|
2009-07-17 03:11:08 +04:00
|
|
|
|
|
|
|
Value file = c.lookup_variable("file");
|
2009-07-20 21:45:39 +04:00
|
|
|
Value layer = c.lookup_variable("layer", true);
|
|
|
|
Value height = c.lookup_variable("height", true);
|
|
|
|
Value convexity = c.lookup_variable("convexity", true);
|
|
|
|
Value origin = c.lookup_variable("origin", true);
|
|
|
|
Value scale = c.lookup_variable("scale", true);
|
|
|
|
Value center = c.lookup_variable("center", true);
|
2009-07-24 22:18:56 +04:00
|
|
|
Value twist = c.lookup_variable("twist", true);
|
|
|
|
Value slices = c.lookup_variable("slices", true);
|
2009-07-17 03:11:08 +04:00
|
|
|
|
2013-05-09 08:20:06 +04:00
|
|
|
if (!file.isUndefined() && file.type() == Value::STRING) {
|
2014-03-03 07:09:10 +04:00
|
|
|
printDeprecation("DEPRECATED: Support for reading files in linear_extrude will be removed in future releases. Use a child import() instead.");
|
2013-05-09 14:12:58 +04:00
|
|
|
node->filename = lookup_file(file.toString(), inst->path(), c.documentPath());
|
2011-09-07 02:11:00 +04:00
|
|
|
}
|
2010-02-25 13:21:11 +03:00
|
|
|
|
2012-05-28 23:32:24 +04:00
|
|
|
// if height not given, and first argument is a number,
|
|
|
|
// then assume it should be the height.
|
2012-07-11 05:52:38 +04:00
|
|
|
if (c.lookup_variable("height").isUndefined() &&
|
2013-05-09 08:20:06 +04:00
|
|
|
evalctx->numArgs() > 0 &&
|
2014-06-04 14:04:21 +04:00
|
|
|
evalctx->getArgName(0) == "") {
|
|
|
|
const Value &val = evalctx->getArgValue(0);
|
|
|
|
if (val.type() == Value::NUMBER) height = val;
|
2012-07-08 00:30:04 +04:00
|
|
|
}
|
2012-05-28 23:32:24 +04:00
|
|
|
|
2012-03-28 02:05:00 +04:00
|
|
|
node->layername = layer.isUndefined() ? "" : layer.toString();
|
2013-06-15 20:48:09 +04:00
|
|
|
node->height = 100;
|
|
|
|
height.getDouble(node->height);
|
2012-03-28 02:05:00 +04:00
|
|
|
node->convexity = (int)convexity.toDouble();
|
|
|
|
origin.getVec2(node->origin_x, node->origin_y);
|
2013-05-10 20:01:50 +04:00
|
|
|
node->scale_x = node->scale_y = 1;
|
|
|
|
scale.getDouble(node->scale_x);
|
|
|
|
scale.getDouble(node->scale_y);
|
|
|
|
scale.getVec2(node->scale_x, node->scale_y);
|
2009-07-20 21:45:39 +04:00
|
|
|
|
2012-03-28 02:05:00 +04:00
|
|
|
if (center.type() == Value::BOOL)
|
|
|
|
node->center = center.toBool();
|
2009-07-17 03:11:08 +04:00
|
|
|
|
2013-06-15 20:48:09 +04:00
|
|
|
if (node->height <= 0) node->height = 0;
|
2009-07-17 03:11:08 +04:00
|
|
|
|
|
|
|
if (node->convexity <= 0)
|
|
|
|
node->convexity = 1;
|
|
|
|
|
2013-05-10 20:01:50 +04:00
|
|
|
if (node->scale_x < 0) node->scale_x = 0;
|
|
|
|
if (node->scale_y < 0) node->scale_y = 0;
|
2009-07-20 21:45:39 +04:00
|
|
|
|
2014-06-27 00:43:12 +04:00
|
|
|
if (slices.type() == Value::NUMBER) node->slices = (int)slices.toDouble();
|
|
|
|
|
2012-03-28 02:05:00 +04:00
|
|
|
if (twist.type() == Value::NUMBER) {
|
|
|
|
node->twist = twist.toDouble();
|
2014-05-19 19:49:41 +04:00
|
|
|
if (node->twist != 0.0) {
|
2014-06-27 00:43:12 +04:00
|
|
|
if (node->slices == 0) {
|
2014-05-19 19:49:41 +04:00
|
|
|
node->slices = (int)fmax(2, fabs(Calc::get_fragments_from_r(node->height,
|
|
|
|
node->fn, node->fs, node->fa) * node->twist / 360));
|
|
|
|
}
|
|
|
|
node->has_twist = true;
|
2009-07-24 22:18:56 +04:00
|
|
|
}
|
|
|
|
}
|
2014-06-27 00:43:12 +04:00
|
|
|
node->slices = std::max(node->slices, 1);
|
2009-07-24 22:18:56 +04:00
|
|
|
|
2011-09-03 08:10:36 +04:00
|
|
|
if (node->filename.empty()) {
|
2013-04-20 01:52:01 +04:00
|
|
|
std::vector<AbstractNode *> instantiatednodes = inst->instantiateChildren(evalctx);
|
|
|
|
node->children.insert(node->children.end(), instantiatednodes.begin(), instantiatednodes.end());
|
2010-01-06 15:16:56 +03:00
|
|
|
}
|
|
|
|
|
2009-07-17 03:11:08 +04:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2011-09-30 05:11:28 +04:00
|
|
|
std::string LinearExtrudeNode::toString() const
|
2010-03-02 21:22:31 +03:00
|
|
|
{
|
|
|
|
std::stringstream stream;
|
|
|
|
|
2011-09-12 05:46:13 +04:00
|
|
|
stream << this->name() << "(";
|
|
|
|
if (!this->filename.empty()) { // Ignore deprecated parameters if empty
|
2012-07-15 23:29:14 +04:00
|
|
|
fs::path path((std::string)this->filename);
|
2011-09-12 05:46:13 +04:00
|
|
|
stream <<
|
2011-11-07 06:59:40 +04:00
|
|
|
"file = " << this->filename << ", "
|
|
|
|
"layer = " << QuotedString(this->layername) << ", "
|
2011-11-06 06:25:59 +04:00
|
|
|
"origin = [" << this->origin_x << ", " << this->origin_y << "], "
|
2012-07-15 05:57:41 +04:00
|
|
|
#ifndef OPENSCAD_TESTING
|
|
|
|
// timestamp is needed for caching, but disturbs the test framework
|
|
|
|
<< "timestamp = " << (fs::exists(path) ? fs::last_write_time(path) : 0) << ", "
|
|
|
|
#endif
|
|
|
|
;
|
2011-09-12 05:46:13 +04:00
|
|
|
}
|
|
|
|
stream <<
|
2010-03-02 21:22:31 +03:00
|
|
|
"height = " << std::dec << this->height << ", "
|
|
|
|
"center = " << (this->center?"true":"false") << ", "
|
|
|
|
"convexity = " << this->convexity;
|
|
|
|
|
|
|
|
if (this->has_twist) {
|
2014-06-27 00:43:12 +04:00
|
|
|
stream << ", twist = " << this->twist;
|
|
|
|
}
|
|
|
|
if (this->slices > 1) {
|
|
|
|
stream << ", slices = " << this->slices;
|
2010-03-02 21:22:31 +03:00
|
|
|
}
|
2013-05-10 20:01:50 +04:00
|
|
|
stream << ", scale = [" << this->scale_x << ", " << this->scale_y << "]";
|
2010-03-02 21:22:31 +03:00
|
|
|
stream << ", $fn = " << this->fn << ", $fa = " << this->fa << ", $fs = " << this->fs << ")";
|
|
|
|
|
|
|
|
return stream.str();
|
|
|
|
}
|
2011-11-06 21:37:12 +04:00
|
|
|
|
|
|
|
void register_builtin_dxf_linear_extrude()
|
|
|
|
{
|
|
|
|
Builtins::init("dxf_linear_extrude", new LinearExtrudeModule());
|
|
|
|
Builtins::init("linear_extrude", new LinearExtrudeModule());
|
|
|
|
}
|