2013-03-24 10:48:39 +00:00
|
|
|
/********************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* This file is part of IfcOpenShell. *
|
|
|
|
|
* *
|
|
|
|
|
* IfcOpenShell is free software: you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the Lesser GNU General Public License as published by *
|
|
|
|
|
* the Free Software Foundation, either version 3.0 of the License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* IfcOpenShell 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 *
|
|
|
|
|
* Lesser GNU General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the Lesser GNU General Public License *
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
|
* *
|
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
|
|
/********************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* This started as a brief example of how IfcOpenShell can be interfaced from *
|
|
|
|
|
* within a C++ context, it has since then evolved into a fullfledged command *
|
|
|
|
|
* line application that is able to convert geometry in an IFC files into *
|
|
|
|
|
* several tesselated and topological output formats. *
|
|
|
|
|
* *
|
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "../ifcconvert/ColladaSerializer.h"
|
|
|
|
|
#include "../ifcconvert/IgesSerializer.h"
|
|
|
|
|
#include "../ifcconvert/StepSerializer.h"
|
|
|
|
|
#include "../ifcconvert/WavefrontObjSerializer.h"
|
2015-02-06 13:07:50 +00:00
|
|
|
#include "../ifcconvert/XmlSerializer.h"
|
2015-07-26 14:26:13 +00:00
|
|
|
#include "../ifcconvert/SvgSerializer.h"
|
|
|
|
|
|
2016-03-07 22:37:36 +02:00
|
|
|
#include "../ifcgeom/IfcGeomIterator.h"
|
2013-03-24 10:48:39 +00:00
|
|
|
|
2015-11-23 15:52:12 +02:00
|
|
|
#include <IGESControl_Controller.hxx>
|
2016-01-25 13:30:33 +01:00
|
|
|
#include <Standard_Version.hxx>
|
2015-11-23 15:52:12 +02:00
|
|
|
|
2016-03-07 22:37:36 +02:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
#include <boost/optional/optional_io.hpp>
|
|
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <set>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
2016-02-07 12:28:05 +02:00
|
|
|
#if USE_VLD
|
2016-02-07 00:30:08 +02:00
|
|
|
#include <vld.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-03-07 22:37:36 +02:00
|
|
|
const std::string DEFAULT_EXTENSION = "obj";
|
2016-03-20 12:48:20 +02:00
|
|
|
const std::string TEMP_FILE_EXTENSION = ".tmp";
|
2013-03-24 10:48:39 +00:00
|
|
|
|
2016-03-07 22:37:36 +02:00
|
|
|
void print_version()
|
|
|
|
|
{
|
|
|
|
|
/// @todo Why cerr used for info prints? Change to cout.
|
|
|
|
|
std::cerr << "IfcOpenShell " << IfcSchema::Identifier << " IfcConvert " << IFCOPENSHELL_VERSION << std::endl;
|
2013-03-24 10:48:39 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-11 10:05:33 +02:00
|
|
|
void print_usage(bool suggest_help = true)
|
2016-03-07 22:37:36 +02:00
|
|
|
{
|
|
|
|
|
std::cerr << "Usage: IfcConvert [options] <input.ifc> [<output>]" << "\n"
|
|
|
|
|
<< "\n"
|
|
|
|
|
<< "Converts the geometry in an IFC file into one of the following formats:" << "\n"
|
|
|
|
|
<< " .obj WaveFront OBJ (a .mtl file is also created)" << "\n"
|
|
|
|
|
#ifdef WITH_OPENCOLLADA
|
|
|
|
|
<< " .dae Collada Digital Assets Exchange" << "\n"
|
2013-11-22 08:23:11 +00:00
|
|
|
#endif
|
2016-03-07 22:37:36 +02:00
|
|
|
<< " .stp STEP Standard for the Exchange of Product Data" << "\n"
|
|
|
|
|
<< " .igs IGES Initial Graphics Exchange Specification" << "\n"
|
|
|
|
|
<< " .xml XML Property definitions and decomposition tree" << "\n"
|
|
|
|
|
<< " .svg SVG Scalable Vector Graphics (2D floor plan)" << "\n"
|
|
|
|
|
<< "\n"
|
2016-03-11 10:05:33 +02:00
|
|
|
<< "If no output filename given, <input>." + DEFAULT_EXTENSION + " will be used as the output file.\n";
|
|
|
|
|
if (suggest_help) {
|
|
|
|
|
std::cerr << "\nRun 'IfcConvert --help' for more information.";
|
|
|
|
|
}
|
|
|
|
|
std::cerr << std::endl;
|
2016-03-07 22:37:36 +02:00
|
|
|
}
|
|
|
|
|
|
2016-03-11 10:05:33 +02:00
|
|
|
void print_options(const boost::program_options::options_description& options)
|
2016-03-07 22:37:36 +02:00
|
|
|
{
|
2016-03-11 10:05:33 +02:00
|
|
|
std::cerr << "\n" << options;
|
2016-03-07 22:37:36 +02:00
|
|
|
std::cerr << std::endl;
|
2013-03-24 10:48:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string change_extension(const std::string& fn, const std::string& ext) {
|
|
|
|
|
std::string::size_type dot = fn.find_last_of('.');
|
|
|
|
|
if (dot != std::string::npos) {
|
|
|
|
|
return fn.substr(0,dot+1) + ext;
|
|
|
|
|
} else {
|
|
|
|
|
return fn + "." + ext;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-18 22:14:05 +02:00
|
|
|
bool file_exists(const std::string& filename)
|
|
|
|
|
{
|
|
|
|
|
/// @todo Windows Unicode support
|
|
|
|
|
std::ifstream file(filename.c_str());
|
|
|
|
|
return file.good();
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 12:48:20 +02:00
|
|
|
bool rename_file(const std::string& old_filename, const std::string& new_filename)
|
|
|
|
|
{
|
|
|
|
|
// Whether or not rename() replaces an existing file is implementation-specific,
|
|
|
|
|
// so remove() possible existing file always.
|
|
|
|
|
/// @todo Windows Unicode support
|
|
|
|
|
std::remove(new_filename.c_str());
|
|
|
|
|
return std::rename(old_filename.c_str(), new_filename.c_str()) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-31 14:10:38 +00:00
|
|
|
static std::stringstream log_stream;
|
|
|
|
|
void write_log();
|
|
|
|
|
|
2013-03-24 10:48:39 +00:00
|
|
|
int main(int argc, char** argv) {
|
2016-03-11 10:05:33 +02:00
|
|
|
boost::program_options::options_description generic_options("Command line options");
|
2013-03-24 10:48:39 +00:00
|
|
|
generic_options.add_options()
|
2016-03-07 22:37:36 +02:00
|
|
|
("help,h", "display usage information")
|
2013-03-24 10:48:39 +00:00
|
|
|
("version", "display version information")
|
2016-03-18 22:14:05 +02:00
|
|
|
("verbose,v", "more verbose output")
|
|
|
|
|
("yes,y", "answer 'yes' automatically to possible confirmation queries (e.g overwriting an existing output file)");
|
2013-03-24 10:48:39 +00:00
|
|
|
|
|
|
|
|
boost::program_options::options_description fileio_options;
|
|
|
|
|
fileio_options.add_options()
|
|
|
|
|
("input-file", boost::program_options::value<std::string>(), "input IFC file")
|
|
|
|
|
("output-file", boost::program_options::value<std::string>(), "output geometry file");
|
|
|
|
|
|
2016-03-10 21:15:50 +02:00
|
|
|
std::vector<std::string> entity_vector, names;
|
2016-03-10 16:09:39 +02:00
|
|
|
double deflection_tolerance;
|
2016-03-11 10:05:33 +02:00
|
|
|
boost::program_options::options_description geom_options("Geometry options");
|
2013-03-24 10:48:39 +00:00
|
|
|
geom_options.add_options()
|
2015-04-22 09:15:48 +00:00
|
|
|
("plan",
|
|
|
|
|
"Specifies whether to include curves in the output result. Typically "
|
|
|
|
|
"these are representations of type Plan or Axis. Excluded by default.")
|
|
|
|
|
("model",
|
2015-07-26 17:58:08 +00:00
|
|
|
"Specifies whether to include surfaces and solids in the output result. "
|
2015-04-22 09:15:48 +00:00
|
|
|
"Typically these are representations of type Body or Facetation. "
|
|
|
|
|
"Included by default.")
|
2015-02-05 11:55:25 +00:00
|
|
|
("weld-vertices",
|
|
|
|
|
"Specifies whether vertices are welded, meaning that the coordinates "
|
|
|
|
|
"vector will only contain unique xyz-triplets. This results in a "
|
|
|
|
|
"manifold mesh which is useful for modelling applications, but might "
|
|
|
|
|
"result in unwanted shading artefacts in rendering applications.")
|
|
|
|
|
("use-world-coords",
|
|
|
|
|
"Specifies whether to apply the local placements of building elements "
|
|
|
|
|
"directly to the coordinates of the representation mesh rather than "
|
|
|
|
|
"to represent the local placement in the 4x3 matrix, which will in that "
|
|
|
|
|
"case be the identity matrix.")
|
|
|
|
|
("convert-back-units",
|
|
|
|
|
"Specifies whether to convert back geometrical output back to the "
|
|
|
|
|
"unit of measure in which it is defined in the IFC file. Default is "
|
|
|
|
|
"to use meters.")
|
|
|
|
|
("sew-shells",
|
|
|
|
|
"Specifies whether to sew the faces of IfcConnectedFaceSets together. "
|
|
|
|
|
"This is a potentially time consuming operation, but guarantees a "
|
|
|
|
|
"consistent orientation of surface normals, even if the faces are not "
|
|
|
|
|
"properly oriented in the IFC file.")
|
2016-01-25 13:30:33 +01:00
|
|
|
#if OCC_VERSION_HEX < 0x60900
|
|
|
|
|
// In Open CASCADE version prior to 6.9.0 boolean operations with multiple
|
|
|
|
|
// arguments where not introduced yet and a work-around was implemented to
|
|
|
|
|
// subtract multiple openings as a single compound. This hack is obsolete
|
|
|
|
|
// for newer versions of Open CASCADE.
|
2015-02-05 11:55:25 +00:00
|
|
|
("merge-boolean-operands",
|
|
|
|
|
"Specifies whether to merge all IfcOpeningElement operands into a single "
|
|
|
|
|
"operand before applying the subtraction operation. This may "
|
|
|
|
|
"introduce a performance improvement at the risk of failing, in "
|
|
|
|
|
"which case the subtraction is applied one-by-one.")
|
2016-01-25 13:30:33 +01:00
|
|
|
#endif
|
2015-02-05 11:55:25 +00:00
|
|
|
("disable-opening-subtractions",
|
|
|
|
|
"Specifies whether to disable the boolean subtraction of "
|
|
|
|
|
"IfcOpeningElement Representations from their RelatingElements.")
|
2015-10-07 17:33:30 +02:00
|
|
|
("enable-layerset-slicing",
|
|
|
|
|
"Specifies whether to enable the slicing of products according "
|
|
|
|
|
"to their associated IfcMaterialLayerSet.")
|
2015-02-05 11:55:25 +00:00
|
|
|
("include",
|
2016-03-07 22:37:36 +02:00
|
|
|
"Specifies that the entities listed after --entities or --names are to be included")
|
2015-02-05 11:55:25 +00:00
|
|
|
("exclude",
|
2016-03-07 22:37:36 +02:00
|
|
|
"Specifies that the entities listed after --entities or --names are to be excluded")
|
|
|
|
|
("entities", boost::program_options::value< std::vector<std::string> >(&entity_vector)->multitoken(),
|
2015-02-05 11:55:25 +00:00
|
|
|
"A list of entities that should be included in or excluded from the "
|
2016-03-07 22:37:36 +02:00
|
|
|
"geometrical output, depending on whether --exclude or --include is specified. "
|
2016-03-10 21:15:50 +02:00
|
|
|
"Defaults to IfcOpeningElement and IfcSpace to be excluded. SVG output defaults "
|
|
|
|
|
"to IfcSpace to be included."
|
2016-03-07 22:37:36 +02:00
|
|
|
"The names are handled case-insensitively. Cannot be placed right before input file argument.")
|
2016-03-10 21:15:50 +02:00
|
|
|
("names", boost::program_options::value< std::vector<std::string> >(&names)->multitoken(),
|
2016-03-07 22:37:36 +02:00
|
|
|
"A list of names or wildcard patterns that should be included in or excluded from the "
|
|
|
|
|
"geometrical output, depending on whether --exclude or --include is specified. "
|
2016-03-10 21:15:50 +02:00
|
|
|
"The names are handled case-sensitively. Cannot be placed right before input file argument.")
|
2016-03-07 22:37:36 +02:00
|
|
|
("no-normals",
|
|
|
|
|
"Disables computation of normals. Saves time and file size and is useful "
|
|
|
|
|
"in instances where you're going to recompute normals for the exported "
|
|
|
|
|
"model in other modelling application in any case.")
|
2016-03-10 16:09:39 +02:00
|
|
|
("deflection-tolerance", boost::program_options::value<double>(&deflection_tolerance),
|
2016-03-17 00:25:33 +02:00
|
|
|
"Sets the deflection tolerance of the mesher, 1e-3 by default if not specified.")
|
|
|
|
|
("generate-uvs",
|
|
|
|
|
"Generates UVs (texture coordinates) by using simple box projection. Requires normals. "
|
|
|
|
|
"Not guaranteed to work properly if used with --weld-vertices.");
|
2016-03-07 22:37:36 +02:00
|
|
|
|
|
|
|
|
std::string bounds;
|
2016-03-11 10:05:33 +02:00
|
|
|
boost::program_options::options_description serializer_options("Serialization options");
|
2016-03-07 22:37:36 +02:00
|
|
|
serializer_options.add_options()
|
|
|
|
|
("bounds", boost::program_options::value<std::string>(&bounds),
|
|
|
|
|
"Specifies the bounding rectangle, for example 512x512, to which the "
|
|
|
|
|
"output will be scaled. Only used when converting to SVG.")
|
2016-03-10 12:01:04 +02:00
|
|
|
("use-element-names",
|
|
|
|
|
"Use entity names instead of unique IDs for naming elements upon serialization. "
|
|
|
|
|
"Applicable for OBJ, DAE, and SVG output.")
|
|
|
|
|
("use-element-guids",
|
|
|
|
|
"Use entity GUIDs instead of unique IDs for naming elements upon serialization. "
|
|
|
|
|
"Applicable for OBJ, DAE, and SVG output.")
|
|
|
|
|
("use-material-names",
|
|
|
|
|
"Use material names instead of unique IDs for naming materials upon serialization. "
|
|
|
|
|
"Applicable for OBJ and DAE output.")
|
2016-03-10 14:39:42 +02:00
|
|
|
("center-model",
|
2016-03-17 23:25:53 +02:00
|
|
|
"Centers the elements upon serialization by applying the center point of "
|
|
|
|
|
"all placements as an offset. Applicable for OBJ and DAE output.");
|
2016-03-07 22:37:36 +02:00
|
|
|
|
2013-03-24 10:48:39 +00:00
|
|
|
boost::program_options::options_description cmdline_options;
|
2016-03-07 22:37:36 +02:00
|
|
|
cmdline_options.add(generic_options).add(fileio_options).add(geom_options).add(serializer_options);
|
2013-03-24 10:48:39 +00:00
|
|
|
|
|
|
|
|
boost::program_options::positional_options_description positional_options;
|
|
|
|
|
positional_options.add("input-file", 1);
|
|
|
|
|
positional_options.add("output-file", 1);
|
|
|
|
|
|
|
|
|
|
boost::program_options::variables_map vmap;
|
2014-03-22 13:12:22 +00:00
|
|
|
try {
|
|
|
|
|
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
|
|
|
|
|
options(cmdline_options).positional(positional_options).run(), vmap);
|
|
|
|
|
} catch (const boost::program_options::unknown_option& e) {
|
|
|
|
|
std::cerr << "[Error] Unknown option '" << e.get_option_name() << "'" << std::endl << std::endl;
|
2016-03-07 22:37:36 +02:00
|
|
|
print_usage();
|
|
|
|
|
return 1;
|
2014-11-04 12:45:20 +00:00
|
|
|
} catch (...) {
|
|
|
|
|
// Catch other errors such as invalid command line syntax
|
2016-03-07 22:37:36 +02:00
|
|
|
print_usage();
|
|
|
|
|
return 1;
|
2014-03-22 13:12:22 +00:00
|
|
|
}
|
2013-03-24 10:48:39 +00:00
|
|
|
boost::program_options::notify(vmap);
|
|
|
|
|
|
2016-03-07 22:37:36 +02:00
|
|
|
print_version();
|
|
|
|
|
|
|
|
|
|
if (vmap.count("version")) {
|
|
|
|
|
return 0;
|
|
|
|
|
} else if (vmap.count("help")) {
|
2016-03-11 10:05:33 +02:00
|
|
|
print_usage(false);
|
|
|
|
|
print_options(generic_options.add(geom_options).add(serializer_options));
|
2016-03-07 22:37:36 +02:00
|
|
|
return 0;
|
|
|
|
|
} else if (!vmap.count("input-file")) {
|
|
|
|
|
std::cerr << "[Error] Input file not specified" << std::endl;
|
|
|
|
|
print_usage();
|
|
|
|
|
return 1;
|
2015-02-05 11:55:25 +00:00
|
|
|
} else if (vmap.count("include") && vmap.count("exclude")) {
|
2016-03-07 22:37:36 +02:00
|
|
|
std::cerr << "[Error] --include and --exclude can not be specified together" << std::endl;
|
2016-03-11 10:05:33 +02:00
|
|
|
print_options(geom_options);
|
2015-02-05 11:55:25 +00:00
|
|
|
return 1;
|
2013-03-24 10:48:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bool verbose = vmap.count("verbose") != 0;
|
|
|
|
|
const bool weld_vertices = vmap.count("weld-vertices") != 0;
|
|
|
|
|
const bool use_world_coords = vmap.count("use-world-coords") != 0;
|
|
|
|
|
const bool convert_back_units = vmap.count("convert-back-units") != 0;
|
|
|
|
|
const bool sew_shells = vmap.count("sew-shells") != 0;
|
2016-01-25 13:30:33 +01:00
|
|
|
#if OCC_VERSION_HEX < 0x60900
|
2013-03-24 10:48:39 +00:00
|
|
|
const bool merge_boolean_operands = vmap.count("merge-boolean-operands") != 0;
|
2016-01-25 13:30:33 +01:00
|
|
|
#endif
|
2013-12-27 09:37:51 +00:00
|
|
|
const bool disable_opening_subtractions = vmap.count("disable-opening-subtractions") != 0;
|
2015-07-26 14:26:13 +00:00
|
|
|
bool include_entities = vmap.count("include") != 0;
|
2015-04-22 09:15:48 +00:00
|
|
|
const bool include_plan = vmap.count("plan") != 0;
|
|
|
|
|
const bool include_model = vmap.count("model") != 0 || (!include_plan);
|
2015-10-07 17:33:30 +02:00
|
|
|
const bool enable_layerset_slicing = vmap.count("enable-layerset-slicing") != 0;
|
2016-03-10 12:01:04 +02:00
|
|
|
const bool use_element_names = vmap.count("use-element-names") != 0;
|
|
|
|
|
const bool use_element_guids = vmap.count("use-element-guids") != 0 ;
|
|
|
|
|
const bool use_material_names = vmap.count("use-material-names") != 0;
|
2016-03-10 13:29:22 +02:00
|
|
|
const bool no_normals = vmap.count("no-normals") != 0 ;
|
2016-03-10 14:39:42 +02:00
|
|
|
bool center_model = vmap.count("center-model") != 0 ;
|
2016-03-10 16:46:20 +02:00
|
|
|
const bool generate_uvs = vmap.count("generate-uvs") != 0 ;
|
2016-03-10 16:09:39 +02:00
|
|
|
const bool deflection_tolerance_specified = vmap.count("deflection-tolerance") != 0 ;
|
2015-10-07 17:33:30 +02:00
|
|
|
|
2015-07-26 14:26:13 +00:00
|
|
|
boost::optional<int> bounding_width, bounding_height;
|
|
|
|
|
if (vmap.count("bounds") == 1) {
|
|
|
|
|
int w, h;
|
|
|
|
|
if (sscanf(bounds.c_str(), "%ux%u", &w, &h) == 2) {
|
|
|
|
|
bounding_width = w;
|
|
|
|
|
bounding_height = h;
|
|
|
|
|
} else {
|
|
|
|
|
std::cerr << "[Error] Invalid use of --bounds" << std::endl;
|
2016-03-11 10:05:33 +02:00
|
|
|
print_options(serializer_options);
|
2015-07-26 14:26:13 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-24 10:48:39 +00:00
|
|
|
|
|
|
|
|
// Gets the set ifc types to be ignored from the command line.
|
2016-03-07 22:37:36 +02:00
|
|
|
std::set<std::string> entities(entity_vector.begin(), entity_vector.end());
|
|
|
|
|
|
2013-03-24 10:48:39 +00:00
|
|
|
const std::string input_filename = vmap["input-file"].as<std::string>();
|
2016-03-18 22:33:03 +02:00
|
|
|
if (!file_exists(input_filename)) {
|
|
|
|
|
std::cerr << "[Error] Input file '" << input_filename << "' does not exist" << std::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 10:48:39 +00:00
|
|
|
// If no output filename is specified a Wavefront OBJ file will be output
|
|
|
|
|
// to maintain backwards compatibility with the obsolete IfcObj executable.
|
|
|
|
|
const std::string output_filename = vmap.count("output-file") == 1
|
|
|
|
|
? vmap["output-file"].as<std::string>()
|
2015-07-26 14:26:13 +00:00
|
|
|
: change_extension(input_filename, DEFAULT_EXTENSION);
|
2013-03-24 10:48:39 +00:00
|
|
|
|
|
|
|
|
if (output_filename.size() < 5) {
|
2016-03-18 22:33:03 +02:00
|
|
|
std::cerr << "[Error] Invalid or unsupported output file '" << output_filename << "' given" << std::endl;
|
2016-03-07 22:37:36 +02:00
|
|
|
print_usage();
|
2013-03-24 10:48:39 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-18 22:14:05 +02:00
|
|
|
if (file_exists(output_filename) && !vmap.count("yes")) {
|
|
|
|
|
std::string answer;
|
|
|
|
|
std::cout << "A file '" << output_filename << "' already exists. Overwrite the existing file?" << std::endl;
|
|
|
|
|
std::cin >> answer;
|
|
|
|
|
if (!boost::iequals(answer, "yes") && !boost::iequals(answer, "y")) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 12:48:20 +02:00
|
|
|
std::string output_temp_filename = output_filename + TEMP_FILE_EXTENSION;
|
|
|
|
|
|
2015-02-06 13:07:50 +00:00
|
|
|
std::string output_extension = output_filename.substr(output_filename.size()-4);
|
2015-10-05 13:34:43 +02:00
|
|
|
boost::to_lower(output_extension);
|
2015-02-06 13:07:50 +00:00
|
|
|
|
2016-03-10 21:15:50 +02:00
|
|
|
// If no entity or names filters are specified these are the defaults to skip from output
|
|
|
|
|
if (entities.empty() && names.empty()) {
|
2016-03-07 22:37:36 +02:00
|
|
|
entities.insert("IfcSpace");
|
2015-07-26 14:26:13 +00:00
|
|
|
if (output_extension == ".svg") {
|
|
|
|
|
include_entities = true;
|
|
|
|
|
} else {
|
2016-03-07 22:37:36 +02:00
|
|
|
entities.insert("IfcOpeningElement");
|
2015-07-26 14:26:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-06 13:07:50 +00:00
|
|
|
Logger::SetOutput(&std::cout, &log_stream);
|
|
|
|
|
Logger::Verbosity(verbose ? Logger::LOG_NOTICE : Logger::LOG_ERROR);
|
|
|
|
|
|
|
|
|
|
if (output_extension == ".xml") {
|
|
|
|
|
int exit_code = 1;
|
|
|
|
|
try {
|
2016-03-20 12:48:20 +02:00
|
|
|
XmlSerializer s(output_temp_filename);
|
2015-02-06 13:07:50 +00:00
|
|
|
IfcParse::IfcFile f;
|
|
|
|
|
if (!f.Init(input_filename)) {
|
2016-03-20 12:48:20 +02:00
|
|
|
Logger::Message(Logger::LOG_ERROR, "Unable to parse input file '" + input_filename + "'");
|
2015-02-06 13:07:50 +00:00
|
|
|
} else {
|
|
|
|
|
s.setFile(&f);
|
|
|
|
|
s.finalize();
|
2016-03-20 12:48:20 +02:00
|
|
|
rename_file(output_temp_filename, output_filename);
|
2015-02-06 13:07:50 +00:00
|
|
|
exit_code = 0;
|
|
|
|
|
}
|
|
|
|
|
} catch (...) {}
|
|
|
|
|
write_log();
|
|
|
|
|
return exit_code;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-19 12:14:36 +00:00
|
|
|
IfcGeom::IteratorSettings settings;
|
2016-03-07 22:37:36 +02:00
|
|
|
/// @todo Make APPLY_DEFAULT_MATERIALS configurable? Quickly tested setting this to false and using obj exporter caused the program to crash and burn.
|
2014-12-19 12:14:36 +00:00
|
|
|
settings.set(IfcGeom::IteratorSettings::APPLY_DEFAULT_MATERIALS, true);
|
|
|
|
|
settings.set(IfcGeom::IteratorSettings::USE_WORLD_COORDS, use_world_coords);
|
|
|
|
|
settings.set(IfcGeom::IteratorSettings::WELD_VERTICES, weld_vertices);
|
|
|
|
|
settings.set(IfcGeom::IteratorSettings::SEW_SHELLS, sew_shells);
|
|
|
|
|
settings.set(IfcGeom::IteratorSettings::CONVERT_BACK_UNITS, convert_back_units);
|
2016-01-25 13:30:33 +01:00
|
|
|
#if OCC_VERSION_HEX < 0x60900
|
2014-12-19 12:14:36 +00:00
|
|
|
settings.set(IfcGeom::IteratorSettings::FASTER_BOOLEANS, merge_boolean_operands);
|
2016-01-25 13:30:33 +01:00
|
|
|
#endif
|
2014-12-19 12:14:36 +00:00
|
|
|
settings.set(IfcGeom::IteratorSettings::DISABLE_OPENING_SUBTRACTIONS, disable_opening_subtractions);
|
2015-04-22 09:15:48 +00:00
|
|
|
settings.set(IfcGeom::IteratorSettings::INCLUDE_CURVES, include_plan);
|
|
|
|
|
settings.set(IfcGeom::IteratorSettings::EXCLUDE_SOLIDS_AND_SURFACES, !include_model);
|
2015-10-07 17:33:30 +02:00
|
|
|
settings.set(IfcGeom::IteratorSettings::APPLY_LAYERSETS, enable_layerset_slicing);
|
2016-03-10 12:01:04 +02:00
|
|
|
settings.set(IfcGeom::IteratorSettings::USE_ELEMENT_NAMES, use_element_names);
|
|
|
|
|
settings.set(IfcGeom::IteratorSettings::USE_ELEMENT_GUIDS, use_element_guids);
|
|
|
|
|
settings.set(IfcGeom::IteratorSettings::USE_MATERIAL_NAMES, use_material_names);
|
2016-03-10 13:29:22 +02:00
|
|
|
settings.set(IfcGeom::IteratorSettings::NO_NORMALS, no_normals);
|
2016-03-10 14:39:42 +02:00
|
|
|
settings.set(IfcGeom::IteratorSettings::CENTER_MODEL, center_model);
|
2016-03-10 16:46:20 +02:00
|
|
|
settings.set(IfcGeom::IteratorSettings::GENERATE_UVS, generate_uvs);
|
2016-03-10 16:09:39 +02:00
|
|
|
if (deflection_tolerance_specified) {
|
|
|
|
|
settings.set_deflection_tolerance(deflection_tolerance);
|
|
|
|
|
}
|
2013-03-24 10:48:39 +00:00
|
|
|
|
|
|
|
|
GeometrySerializer* serializer;
|
|
|
|
|
if (output_extension == ".obj") {
|
2016-03-28 11:32:29 +03:00
|
|
|
// Do not use temp file for MTL as it's such a small file.
|
|
|
|
|
const std::string mtl_filename = change_extension(output_filename, "mtl");
|
2013-03-24 10:48:39 +00:00
|
|
|
if (!use_world_coords) {
|
2013-12-31 14:10:38 +00:00
|
|
|
Logger::Message(Logger::LOG_NOTICE, "Using world coords when writing WaveFront OBJ files");
|
2014-12-19 12:14:36 +00:00
|
|
|
settings.set(IfcGeom::IteratorSettings::USE_WORLD_COORDS, true);
|
2013-03-24 10:48:39 +00:00
|
|
|
}
|
2016-03-28 11:32:29 +03:00
|
|
|
serializer = new WaveFrontOBJSerializer(output_temp_filename, mtl_filename, settings);
|
2013-11-22 08:23:11 +00:00
|
|
|
#ifdef WITH_OPENCOLLADA
|
2013-03-24 10:48:39 +00:00
|
|
|
} else if (output_extension == ".dae") {
|
2016-03-20 12:48:20 +02:00
|
|
|
serializer = new ColladaSerializer(output_temp_filename, settings);
|
2013-11-22 08:23:11 +00:00
|
|
|
#endif
|
2013-03-24 10:48:39 +00:00
|
|
|
} else if (output_extension == ".stp") {
|
2016-03-20 12:48:20 +02:00
|
|
|
serializer = new StepSerializer(output_temp_filename, settings);
|
2013-03-24 10:48:39 +00:00
|
|
|
} else if (output_extension == ".igs") {
|
2016-03-10 16:46:20 +02:00
|
|
|
IGESControl_Controller::Init(); // work around Open Cascade bug
|
2016-03-20 12:48:20 +02:00
|
|
|
serializer = new IgesSerializer(output_temp_filename, settings);
|
2015-07-26 14:26:13 +00:00
|
|
|
} else if (output_extension == ".svg") {
|
|
|
|
|
settings.set(IfcGeom::IteratorSettings::DISABLE_TRIANGULATION, true);
|
2016-03-20 12:48:20 +02:00
|
|
|
serializer = new SvgSerializer(output_temp_filename, settings);
|
2015-07-26 14:26:13 +00:00
|
|
|
if (bounding_width && bounding_height) {
|
2016-03-07 22:37:36 +02:00
|
|
|
static_cast<SvgSerializer*>(serializer)->setBoundingRectangle(*bounding_width, *bounding_height);
|
2015-07-26 14:26:13 +00:00
|
|
|
}
|
2013-03-24 10:48:39 +00:00
|
|
|
} else {
|
2016-03-17 00:25:33 +02:00
|
|
|
Logger::Message(Logger::LOG_ERROR, "Unknown output filename extension '" + output_extension + "'");
|
2013-12-31 14:10:38 +00:00
|
|
|
write_log();
|
2016-03-07 22:37:36 +02:00
|
|
|
print_usage();
|
2013-03-24 10:48:39 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 23:25:53 +02:00
|
|
|
const bool is_tesselated = serializer->isTesselated(); // isTesselated() doesn't change at run-time
|
|
|
|
|
if (!is_tesselated) {
|
2015-02-23 22:01:21 +00:00
|
|
|
if (weld_vertices) {
|
2016-03-17 00:25:33 +02:00
|
|
|
Logger::Message(Logger::LOG_NOTICE, "Weld vertices setting ignored when writing non-tesselated output");
|
2015-02-23 22:01:21 +00:00
|
|
|
}
|
2016-03-17 00:25:33 +02:00
|
|
|
if (generate_uvs) {
|
|
|
|
|
Logger::Message(Logger::LOG_NOTICE, "Generate UVs setting ignored when writing non-tesselated output");
|
|
|
|
|
}
|
2016-03-17 23:25:53 +02:00
|
|
|
if (center_model) {
|
|
|
|
|
Logger::Message(Logger::LOG_NOTICE, "Center model setting ignored when writing non-tesselated output");
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:37:36 +02:00
|
|
|
settings.set(IfcGeom::IteratorSettings::DISABLE_TRIANGULATION, true);
|
2015-02-23 22:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:37:36 +02:00
|
|
|
IfcGeom::Iterator<real_t> context_iterator(settings, input_filename);
|
2014-12-19 12:14:36 +00:00
|
|
|
|
2015-02-05 11:55:25 +00:00
|
|
|
try {
|
|
|
|
|
if (include_entities) {
|
|
|
|
|
context_iterator.includeEntities(entities);
|
2016-03-10 21:15:50 +02:00
|
|
|
context_iterator.include_entity_names(names);
|
2015-02-05 11:55:25 +00:00
|
|
|
} else {
|
|
|
|
|
context_iterator.excludeEntities(entities);
|
2016-03-10 21:15:50 +02:00
|
|
|
context_iterator.exclude_entity_names(names);
|
2015-02-05 11:55:25 +00:00
|
|
|
}
|
|
|
|
|
} catch (const IfcParse::IfcException& e) {
|
|
|
|
|
std::cout << "[Error] " << e.what() << std::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 10:48:39 +00:00
|
|
|
if (!serializer->ready()) {
|
2016-03-18 22:33:03 +02:00
|
|
|
Logger::Message(Logger::LOG_ERROR, "Unable to open output '" + output_filename + "' file for writing");
|
2013-12-31 14:10:38 +00:00
|
|
|
write_log();
|
2013-03-24 10:48:39 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-31 14:03:06 +00:00
|
|
|
time_t start,end;
|
|
|
|
|
time(&start);
|
2015-02-05 11:55:25 +00:00
|
|
|
|
2015-04-22 09:15:48 +00:00
|
|
|
if (!context_iterator.initialize()) {
|
2016-03-18 22:33:03 +02:00
|
|
|
Logger::Message(Logger::LOG_ERROR, "Unable to parse input file '" + input_filename + "' or no geometrical entities found");
|
2013-12-31 14:10:38 +00:00
|
|
|
write_log();
|
2013-03-24 10:48:39 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 11:35:32 +02:00
|
|
|
serializer->setFile(context_iterator.getFile());
|
2015-07-26 14:26:13 +00:00
|
|
|
|
2013-12-31 14:10:38 +00:00
|
|
|
if (convert_back_units) {
|
2016-03-07 22:37:36 +02:00
|
|
|
serializer->setUnitNameAndMagnitude(context_iterator.getUnitName(), static_cast<float>(context_iterator.getUnitMagnitude()));
|
2013-12-31 14:10:38 +00:00
|
|
|
} else {
|
|
|
|
|
serializer->setUnitNameAndMagnitude("METER", 1.0f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serializer->writeHeader();
|
|
|
|
|
|
2013-03-24 10:48:39 +00:00
|
|
|
int old_progress = -1;
|
2016-03-27 21:17:51 +02:00
|
|
|
|
|
|
|
|
if (center_model) {
|
|
|
|
|
double* offset = serializer->settings().offset;
|
|
|
|
|
gp_XYZ center = (context_iterator.bounds_min() + context_iterator.bounds_max()) * 0.5;
|
|
|
|
|
offset[0] = -center.X();
|
|
|
|
|
offset[1] = -center.Y();
|
|
|
|
|
offset[2] = -center.Z();
|
|
|
|
|
std::stringstream msg;
|
|
|
|
|
msg << "Using model offset (" << offset[0] << "," << offset[1] << "," << offset[2] << ")";
|
|
|
|
|
Logger::Message(Logger::LOG_NOTICE, msg.str());
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 10:48:39 +00:00
|
|
|
Logger::Status("Creating geometry...");
|
|
|
|
|
|
2015-02-06 13:07:50 +00:00
|
|
|
// The functions IfcGeom::Iterator::get() and IfcGeom::Iterator::next()
|
|
|
|
|
// wrap an iterator of all geometrical products in the Ifc file.
|
|
|
|
|
// IfcGeom::Iterator::get() returns an IfcGeom::TriangulationElement or
|
|
|
|
|
// -BRepElement pointer, based on current settings. (see IfcGeomIterator.h
|
|
|
|
|
// for definition) IfcGeom::Iterator::next() is used to poll whether more
|
|
|
|
|
// geometrical entities are available. None of these functions throw
|
|
|
|
|
// exceptions, neither for parsing errors or geometrical errors. Upon
|
|
|
|
|
// calling next() the entity to be returned has already been processed, a
|
|
|
|
|
// true return value guarantees that a successfully processed product is
|
|
|
|
|
// available.
|
2016-03-27 21:17:51 +02:00
|
|
|
size_t num_created = 0;
|
|
|
|
|
|
2013-03-24 10:48:39 +00:00
|
|
|
do {
|
2016-03-27 21:17:51 +02:00
|
|
|
IfcGeom::Element<real_t> *geom_object = context_iterator.get();
|
|
|
|
|
if (is_tesselated) {
|
|
|
|
|
serializer->write(static_cast<const IfcGeom::TriangulationElement<real_t>*>(geom_object));
|
2013-03-24 10:48:39 +00:00
|
|
|
} else {
|
2016-03-27 21:17:51 +02:00
|
|
|
serializer->write(static_cast<const IfcGeom::BRepElement<real_t>*>(geom_object));
|
2013-03-24 10:48:39 +00:00
|
|
|
}
|
2016-03-07 22:37:36 +02:00
|
|
|
const int progress = context_iterator.progress() / 2;
|
|
|
|
|
if (old_progress != progress) Logger::ProgressBar(progress);
|
|
|
|
|
old_progress = progress;
|
2016-03-27 21:17:51 +02:00
|
|
|
} while (++num_created, context_iterator.next());
|
2013-03-24 10:48:39 +00:00
|
|
|
|
2016-03-27 21:17:51 +02:00
|
|
|
Logger::Status("\rDone creating geometry (" + boost::lexical_cast<std::string>(num_created) +
|
2016-03-07 22:37:36 +02:00
|
|
|
" objects) ");
|
|
|
|
|
|
2016-03-27 21:17:51 +02:00
|
|
|
serializer->finalize();
|
2013-03-24 10:48:39 +00:00
|
|
|
delete serializer;
|
|
|
|
|
|
2016-03-28 11:47:02 +03:00
|
|
|
// Renaming might fail (e.g. maybe the existing file was open in a viewer application)
|
|
|
|
|
// Do not remove the temp file as user can salvage the conversion result from it.
|
|
|
|
|
bool successful = rename_file(output_temp_filename, output_filename);
|
|
|
|
|
if (!successful) {
|
|
|
|
|
Logger::Message(Logger::LOG_ERROR, "Unable to write output file '" + output_filename + "");
|
2016-03-20 12:48:20 +02:00
|
|
|
}
|
2013-03-24 10:48:39 +00:00
|
|
|
|
2013-12-31 14:10:38 +00:00
|
|
|
write_log();
|
2013-03-24 10:48:39 +00:00
|
|
|
|
|
|
|
|
time(&end);
|
2015-02-06 13:07:50 +00:00
|
|
|
|
2016-03-07 22:37:36 +02:00
|
|
|
int seconds = (int)difftime(end, start);
|
2016-03-27 21:17:51 +02:00
|
|
|
std::stringstream msg;
|
|
|
|
|
int minutes = seconds / 60;
|
|
|
|
|
seconds = seconds % 60;
|
|
|
|
|
msg << "\nConversion took";
|
|
|
|
|
if (minutes > 0) {
|
|
|
|
|
msg << " " << minutes << " minute";
|
|
|
|
|
if (minutes > 1) {
|
|
|
|
|
msg << "s";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
msg << " " << seconds << " second";
|
|
|
|
|
if (seconds > 1) {
|
|
|
|
|
msg << "s";
|
|
|
|
|
}
|
|
|
|
|
Logger::Status(msg.str());
|
2015-02-06 13:07:50 +00:00
|
|
|
|
2016-03-28 11:47:02 +03:00
|
|
|
return successful ? 0 : 1;
|
2013-12-31 14:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void write_log() {
|
|
|
|
|
std::string log = log_stream.str();
|
|
|
|
|
if (!log.empty()) {
|
2016-03-07 22:37:36 +02:00
|
|
|
std::cerr << "\n" << "Log:\n" << log << std::endl;
|
2013-12-31 14:10:38 +00:00
|
|
|
}
|
2016-03-07 22:37:36 +02:00
|
|
|
}
|