Export UI parameters as JSON file.

This commit is contained in:
Torsten Paul 2015-01-12 17:09:52 +01:00
parent 7c089c6ccd
commit f1e79f19e2
4 changed files with 110 additions and 0 deletions

View file

@ -421,6 +421,7 @@ SOURCES += src/version_check.cc \
src/calc.cc \
src/export.cc \
src/export_png.cc \
src/export_params.cc \
src/import.cc \
src/renderer.cc \
src/colormap.cc \

View file

@ -4,6 +4,7 @@
#include "Tree.h"
#include "Camera.h"
#include "memory.h"
#include "module.h"
#ifdef ENABLE_CGAL
@ -31,6 +32,7 @@ void export_svg(const class Polygon2d &poly, std::ostream &output);
void export_png(const CGAL_Nef_polyhedron *root_N, Camera &c, std::ostream &output);
void export_png_with_opencsg(Tree &tree, Camera &c, std::ostream &output);
void export_png_with_throwntogether(Tree &tree, Camera &c, std::ostream &output);
void export_parameter(std::ostream &output, FileModule *fileModule);
#endif // ENABLE_CGAL

93
src/export_params.cc Normal file
View file

@ -0,0 +1,93 @@
/*
* OpenSCAD (www.openscad.org)
* Copyright (C) 2009-2015 Clifford Wolf <clifford@clifford.at> and
* Marius Kintel <marius@kintel.net>
*
* 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.
*
* 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.
*
* 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
*
*/
#include <boost/foreach.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include "export.h"
#include "expression.h"
#include "modcontext.h"
namespace pt = boost::property_tree;
static const std::string valueTypeToString(const Value::ValueType type)
{
switch (type) {
case Value::BOOL:
return "BOOL";
case Value::NUMBER:
return "NUMBER";
case Value::STRING:
return "STRING";
case Value::VECTOR:
return "VECTOR";
case Value::RANGE:
return "RANGE";
case Value::UNDEFINED:
return "UNDEFINED";
}
assert(false && "unhandled value type");
}
void export_parameter(std::ostream &output, FileModule *fileModule)
{
pt::ptree pt;
pt::ptree parameter_array;
ModuleContext ctx;
BOOST_FOREACH(Assignment assignment, fileModule->scope.assignments)
{
const Annotation *param = assignment.annotation("Parameter");
if (!param) {
continue;
}
const ValuePtr defaultValue = assignment.second.get()->evaluate(&ctx);
if (defaultValue->type() == Value::UNDEFINED) {
continue;
}
pt::ptree parameter;
parameter.push_back(std::make_pair("name", assignment.first));
parameter.push_back(std::make_pair("value", defaultValue->toString()));
parameter.push_back(std::make_pair("type", valueTypeToString(defaultValue->type())));
const Annotation *desc = assignment.annotation("Description");
if (desc) {
const ValuePtr v = desc->evaluate(&ctx, "text");
if (v->type() == Value::STRING) {
parameter.push_back(std::make_pair("description", v->toString()));
}
}
parameter_array.push_back(std::make_pair("", parameter));
}
pt.add_child("parameters", parameter_array);
pt::write_json(output, pt);
}

View file

@ -344,6 +344,7 @@ int cmdline(const char *deps_output_file, const std::string &filename, Camera &c
const char *ast_output_file = NULL;
const char *term_output_file = NULL;
const char *echo_output_file = NULL;
const char *params_output_file = NULL;
std::string suffix = boosty::extension_str( output_file );
boost::algorithm::to_lower( suffix );
@ -358,6 +359,7 @@ int cmdline(const char *deps_output_file, const std::string &filename, Camera &c
else if (suffix == ".ast") ast_output_file = output_file;
else if (suffix == ".term") term_output_file = output_file;
else if (suffix == ".echo") echo_output_file = output_file;
else if (suffix == ".params") params_output_file = output_file;
else {
PRINTB("Unknown suffix for output file %s\n", output_file);
return 1;
@ -425,6 +427,18 @@ int cmdline(const char *deps_output_file, const std::string &filename, Camera &c
fstream.close();
}
}
else if (params_output_file) {
fs::current_path(original_path);
std::ofstream fstream(params_output_file);
if (!fstream.is_open()) {
PRINTB("Can't open file \"%s\" for export", params_output_file);
}
else {
fs::current_path(fparent); // Force exported filenames to be relative to document path
export_parameter(fstream, root_module);
fstream.close();
}
}
else if (ast_output_file) {
fs::current_path(original_path);
std::ofstream fstream(ast_output_file);