2009-10-14 16:34:50 +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-10-14 16:34:50 +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-10-14 16:34:50 +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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-03-02 21:22:31 +03:00
|
|
|
#include "importnode.h"
|
|
|
|
|
2010-01-30 07:26:05 +03:00
|
|
|
#include "module.h"
|
|
|
|
#include "polyset.h"
|
|
|
|
#include "context.h"
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "dxfdata.h"
|
|
|
|
#include "dxftess.h"
|
2009-12-09 17:22:50 +03:00
|
|
|
#include "printutils.h"
|
2011-09-04 02:03:16 +04:00
|
|
|
#include "handle_dep.h" // handle_dep()
|
2009-10-14 16:34:50 +04:00
|
|
|
|
2011-09-08 00:04:59 +04:00
|
|
|
#ifdef ENABLE_CGAL
|
|
|
|
#include "cgalutils.h"
|
|
|
|
#endif
|
|
|
|
|
2009-10-14 16:34:50 +04:00
|
|
|
#include <QFile>
|
2011-08-05 04:11:20 +04:00
|
|
|
#include <QRegExp>
|
|
|
|
#include <QStringList>
|
2010-01-06 05:35:21 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2011-09-08 00:04:59 +04:00
|
|
|
#include <fstream>
|
2010-03-02 21:22:31 +03:00
|
|
|
#include <sstream>
|
2010-03-28 19:57:39 +04:00
|
|
|
#include <assert.h>
|
2011-09-03 08:10:36 +04:00
|
|
|
#include <boost/assign/std/vector.hpp>
|
|
|
|
using namespace boost::assign; // bring 'operator+=()' into scope
|
2009-10-14 16:34:50 +04:00
|
|
|
|
|
|
|
class ImportModule : public AbstractModule
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
import_type_e type;
|
2011-09-07 02:47:55 +04:00
|
|
|
ImportModule(import_type_e type = TYPE_UNKNOWN) : type(type) { }
|
2010-01-06 22:38:45 +03:00
|
|
|
virtual AbstractNode *evaluate(const Context *ctx, const ModuleInstantiation *inst) const;
|
2009-10-14 16:34:50 +04:00
|
|
|
};
|
|
|
|
|
2010-01-06 22:38:45 +03:00
|
|
|
AbstractNode *ImportModule::evaluate(const Context *ctx, const ModuleInstantiation *inst) const
|
2009-10-14 16:34:50 +04:00
|
|
|
{
|
2011-09-03 08:10:36 +04:00
|
|
|
std::vector<std::string> argnames;
|
2011-09-07 02:47:55 +04:00
|
|
|
argnames += "file", "layer", "convexity", "origin", "scale";
|
2011-09-03 08:10:36 +04:00
|
|
|
std::vector<Expression*> argexpr;
|
2009-10-14 16:34:50 +04:00
|
|
|
|
2010-01-08 06:31:06 +03:00
|
|
|
// Map old argnames to new argnames for compatibility
|
2011-09-03 08:10:36 +04:00
|
|
|
std::vector<std::string> inst_argnames = inst->argnames;
|
|
|
|
for (size_t i=0; i<inst_argnames.size(); i++) {
|
2010-01-08 06:31:06 +03:00
|
|
|
if (inst_argnames[i] == "filename")
|
|
|
|
inst_argnames[i] = "file";
|
|
|
|
if (inst_argnames[i] == "layername")
|
|
|
|
inst_argnames[i] = "layer";
|
|
|
|
}
|
|
|
|
|
2009-10-14 16:34:50 +04:00
|
|
|
Context c(ctx);
|
2010-01-08 06:31:06 +03:00
|
|
|
c.args(argnames, argexpr, inst_argnames, inst->argvalues);
|
2009-10-14 16:34:50 +04:00
|
|
|
|
2011-09-07 02:47:55 +04:00
|
|
|
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);
|
|
|
|
|
2010-01-06 05:35:21 +03:00
|
|
|
node->fn = c.lookup_variable("$fn").num;
|
|
|
|
node->fs = c.lookup_variable("$fs").num;
|
|
|
|
node->fa = c.lookup_variable("$fa").num;
|
|
|
|
|
2011-09-07 02:47:55 +04:00
|
|
|
node->filename = filename;
|
2011-09-03 08:10:36 +04:00
|
|
|
node->layername = c.lookup_variable("layer", true).text;
|
2010-01-06 14:03:35 +03:00
|
|
|
node->convexity = c.lookup_variable("convexity", true).num;
|
2009-10-14 16:34:50 +04:00
|
|
|
|
2010-01-06 05:35:21 +03:00
|
|
|
if (node->convexity <= 0)
|
|
|
|
node->convexity = 1;
|
|
|
|
|
|
|
|
Value origin = c.lookup_variable("origin", true);
|
|
|
|
node->origin_x = node->origin_y = 0;
|
|
|
|
origin.getv2(node->origin_x, node->origin_y);
|
|
|
|
|
|
|
|
node->scale = c.lookup_variable("scale", true).num;
|
|
|
|
|
|
|
|
if (node->scale <= 0)
|
|
|
|
node->scale = 1;
|
|
|
|
|
2009-10-14 16:34:50 +04:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2011-09-09 07:53:05 +04:00
|
|
|
PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *evaluator) const
|
2009-10-14 16:34:50 +04:00
|
|
|
{
|
2011-09-08 00:04:59 +04:00
|
|
|
PolySet *p = NULL;
|
2009-10-14 16:34:50 +04:00
|
|
|
|
2010-07-07 02:49:12 +04:00
|
|
|
if (this->type == TYPE_STL)
|
2009-10-14 16:34:50 +04:00
|
|
|
{
|
2011-09-04 02:03:16 +04:00
|
|
|
handle_dep(this->filename);
|
2011-09-03 08:10:36 +04:00
|
|
|
QFile f(QString::fromStdString(this->filename));
|
2009-10-14 17:22:59 +04:00
|
|
|
if (!f.open(QIODevice::ReadOnly)) {
|
2011-09-03 08:10:36 +04:00
|
|
|
PRINTF("WARNING: Can't open import file `%s'.", this->filename.c_str());
|
2009-10-14 17:22:59 +04:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2011-09-08 00:27:29 +04:00
|
|
|
p = new PolySet();
|
2009-10-14 17:22:59 +04:00
|
|
|
QByteArray data = f.read(5);
|
|
|
|
if (data.size() == 5 && QString(data) == QString("solid"))
|
2009-10-14 16:34:50 +04:00
|
|
|
{
|
2009-10-14 17:22:59 +04:00
|
|
|
int i = 0;
|
|
|
|
double vdata[3][3];
|
|
|
|
QRegExp splitre = QRegExp("\\s*(vertex)?\\s+");
|
|
|
|
f.readLine();
|
|
|
|
while (!f.atEnd())
|
|
|
|
{
|
|
|
|
QString line = QString(f.readLine()).remove("\n").remove("\r");
|
|
|
|
if (line.contains("solid") || line.contains("facet") || line.contains("endloop"))
|
|
|
|
continue;
|
|
|
|
if (line.contains("outer loop")) {
|
|
|
|
i = 0;
|
|
|
|
continue;
|
2009-10-14 16:34:50 +04:00
|
|
|
}
|
2009-10-14 17:22:59 +04:00
|
|
|
if (line.contains("vertex")) {
|
|
|
|
QStringList tokens = line.split(splitre);
|
|
|
|
bool ok[3] = { false, false, false };
|
|
|
|
if (tokens.size() == 4) {
|
|
|
|
vdata[i][0] = tokens[1].toDouble(&ok[0]);
|
|
|
|
vdata[i][1] = tokens[2].toDouble(&ok[1]);
|
|
|
|
vdata[i][2] = tokens[3].toDouble(&ok[2]);
|
|
|
|
}
|
|
|
|
if (!ok[0] || !ok[1] || !ok[2]) {
|
|
|
|
PRINTF("WARNING: Can't parse vertex line `%s'.", line.toAscii().data());
|
|
|
|
i = 10;
|
|
|
|
} else if (++i == 3) {
|
|
|
|
p->append_poly();
|
|
|
|
p->append_vertex(vdata[0][0], vdata[0][1], vdata[0][2]);
|
|
|
|
p->append_vertex(vdata[1][0], vdata[1][1], vdata[1][2]);
|
|
|
|
p->append_vertex(vdata[2][0], vdata[2][1], vdata[2][2]);
|
|
|
|
}
|
2009-10-14 16:34:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-14 17:22:59 +04:00
|
|
|
else
|
|
|
|
{
|
2010-01-03 12:32:48 +03:00
|
|
|
f.read(80-5+4);
|
2009-10-14 17:22:59 +04:00
|
|
|
while (1) {
|
2011-01-19 21:37:42 +03:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma pack(push,1)
|
|
|
|
#endif
|
2009-10-14 17:22:59 +04:00
|
|
|
struct {
|
|
|
|
float i, j, k;
|
|
|
|
float x1, y1, z1;
|
|
|
|
float x2, y2, z2;
|
|
|
|
float x3, y3, z3;
|
|
|
|
unsigned short acount;
|
2011-01-19 21:37:42 +03:00
|
|
|
}
|
|
|
|
#ifdef __GNUC__
|
|
|
|
__attribute__ ((packed))
|
|
|
|
#endif
|
|
|
|
data;
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma pack(pop)
|
|
|
|
#endif
|
|
|
|
|
2009-10-14 17:22:59 +04:00
|
|
|
if (f.read((char*)&data, sizeof(data)) != sizeof(data))
|
|
|
|
break;
|
|
|
|
p->append_poly();
|
|
|
|
p->append_vertex(data.x1, data.y1, data.z1);
|
|
|
|
p->append_vertex(data.x2, data.y2, data.z2);
|
|
|
|
p->append_vertex(data.x3, data.y3, data.z3);
|
|
|
|
}
|
|
|
|
}
|
2009-10-14 16:34:50 +04:00
|
|
|
}
|
|
|
|
|
2011-09-07 02:47:55 +04:00
|
|
|
else if (this->type == TYPE_OFF)
|
2009-10-14 16:34:50 +04:00
|
|
|
{
|
2011-09-08 00:04:59 +04:00
|
|
|
#ifdef ENABLE_CGAL
|
|
|
|
CGAL_Polyhedron poly;
|
|
|
|
std::ifstream file(this->filename.c_str());
|
|
|
|
file >> poly;
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
p = createPolySetFromPolyhedron(poly);
|
|
|
|
#else
|
|
|
|
PRINTF("WARNING: OFF import requires CGAL.");
|
|
|
|
#endif
|
2009-10-14 16:34:50 +04:00
|
|
|
}
|
|
|
|
|
2011-09-07 02:47:55 +04:00
|
|
|
else if (this->type == TYPE_DXF)
|
2010-01-06 05:35:21 +03:00
|
|
|
{
|
2011-09-08 00:04:59 +04:00
|
|
|
p = new PolySet();
|
2011-09-04 00:44:41 +04:00
|
|
|
DxfData dd(this->fn, this->fs, this->fa, this->filename, this->layername, this->origin_x, this->origin_y, this->scale);
|
2010-01-06 05:35:21 +03:00
|
|
|
p->is2d = true;
|
2011-09-01 19:03:34 +04:00
|
|
|
dxf_tesselate(p, dd, 0, true, false, 0);
|
2011-09-04 00:44:41 +04:00
|
|
|
dxf_border_to_ps(p, dd);
|
2010-01-06 05:35:21 +03:00
|
|
|
}
|
2011-09-07 02:47:55 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
PRINTF("ERROR: Unsupported file format while trying to import file '%s'", this->filename.c_str());
|
|
|
|
}
|
2010-01-06 05:35:21 +03:00
|
|
|
|
2011-09-08 00:04:59 +04:00
|
|
|
if (p) p->convexity = this->convexity;
|
2009-10-14 16:34:50 +04:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2010-03-02 21:22:31 +03:00
|
|
|
std::string ImportNode::toString() const
|
|
|
|
{
|
|
|
|
std::stringstream stream;
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
memset(&st, 0, sizeof(struct stat));
|
2011-09-03 08:10:36 +04:00
|
|
|
stat(this->filename.c_str(), &st);
|
2010-03-02 21:22:31 +03:00
|
|
|
|
2010-08-28 23:21:44 +04:00
|
|
|
stream << this->name();
|
2011-09-07 02:47:55 +04:00
|
|
|
stream << "(file = \"" << this->filename << "\", "
|
|
|
|
"cache = \"" << std::hex << (int)st.st_mtime << "." << (int)st.st_size << "\", "
|
|
|
|
"layer = \"" << this->layername << "\", "
|
2011-11-03 22:38:57 +04:00
|
|
|
"origin = [" << std::dec << this->origin_x << ", " << this->origin_y << "], "
|
2011-09-07 02:47:55 +04:00
|
|
|
"scale = " << this->scale << ", "
|
|
|
|
"convexity = " << this->convexity << ", "
|
|
|
|
"$fn = " << this->fn << ", $fa = " << this->fa << ", $fs = " << this->fs << ")";
|
2010-03-02 21:22:31 +03:00
|
|
|
|
|
|
|
return stream.str();
|
|
|
|
}
|
2010-08-28 23:21:44 +04:00
|
|
|
|
|
|
|
std::string ImportNode::name() const
|
|
|
|
{
|
2011-09-07 02:47:55 +04:00
|
|
|
return "import";
|
2010-08-28 23:21:44 +04:00
|
|
|
}
|
2011-09-07 02:47:55 +04:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|