1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#include "graph/hooks/export.h"
#include "graph/hooks/hooks.h"
#include "graph/proxy/node.h"
#include "export/export_mesh.h"
#include "export/export_heightmap.h"
#include <QString>
using namespace boost::python;
////////////////////////////////////////////////////////////////////////////////
template <class T>
T ScriptExportHooks::get_object(std::string name, dict kwargs, T d)
{
if (!kwargs.has_key(name))
return d;
extract<T> out(kwargs[name]);
if (!out.check())
throw AppHooks::Exception(
"Keyword argument " + name + " is of the wrong type.");
return out();
}
////////////////////////////////////////////////////////////////////////////////
Shape ScriptExportHooks::get_shape(tuple args)
{
extract<Shape> shape_(args[1]);
if (!shape_.check())
throw AppHooks::Exception(
"First argument to export task must be a fab.types.Shape");
return shape_();
}
////////////////////////////////////////////////////////////////////////////////
Bounds ScriptExportHooks::pad_bounds(Bounds b)
{
float dx = (b.xmax - b.xmin) / 20;
float dy = (b.ymax - b.ymin) / 20;
float dz = (b.zmax - b.zmin) / 20;
return Bounds(b.xmin - dx, b.ymin - dy, b.zmin - dz,
b.xmax + dx, b.ymax + dy, b.zmax + dz);
}
////////////////////////////////////////////////////////////////////////////////
object ScriptExportHooks::stl(tuple args, dict kwargs)
{
ScriptExportHooks* self = extract<ScriptExportHooks*>(args[0])();
if (self->called)
throw AppHooks::Exception(
"Cannot define multiple export tasks in a single script.");
self->called = true;
if (len(args) != 2)
throw AppHooks::Exception(
"export.stl must be called with shape as first argument.");
Shape shape = get_shape(args);
Bounds bounds = get_object("bounds", kwargs, shape.bounds);
if (get_object("pad", kwargs, true))
bounds = pad_bounds(bounds);
const QString filename = QString::fromStdString(
get_object("filename", kwargs, ""));
const float resolution = get_object("resolution", kwargs, -1);
const bool detect_features = get_object("detect_features", kwargs, false);
self->proxy->setExportWorker(new ExportMeshWorker(
shape, bounds, filename, resolution, detect_features));
return object();
}
////////////////////////////////////////////////////////////////////////////////
object ScriptExportHooks::heightmap(tuple args, dict kwargs)
{
ScriptExportHooks* self = extract<ScriptExportHooks*>(args[0])();
if (self->called)
throw AppHooks::Exception(
"Cannot define multiple export tasks in a single script.");
self->called = true;
if (len(args) != 2)
throw AppHooks::Exception(
"export.stl must be called with shape as first argument.");
Shape shape = get_shape(args);
Bounds bounds = get_object("bounds", kwargs, shape.bounds);
if (get_object("pad", kwargs, true))
bounds = pad_bounds(bounds);
const QString filename = QString::fromStdString(
get_object("filename", kwargs, ""));
const float resolution = get_object("resolution", kwargs, -1);
const float mm_per_unit = get_object("mm_per_unit", kwargs, 25.4);
self->proxy->setExportWorker(new ExportHeightmapWorker(
shape, bounds, filename, resolution, mm_per_unit));
return object();
}
|