<commit_before>/**
*  Copyright (C) 2018 3D Repo Ltd
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU Affero General Public License as
*  published by the Free Software Foundation, either version 3 of the
*  License, or (at your option) any later version.
*
*  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 Affero General Public License for more details.
*
*  You should have received a copy of the GNU Affero General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <boost/filesystem.hpp>
#include <OdPlatformSettings.h>

#include "vectorise_device_rvt.h"
#include "data_processor_rvt.h"
#include "helper_functions.h"
#include "../../../../lib/repo_utils.h"

using namespace repo::manipulator::modelconvertor::odaHelper;

//These metadata params are crashing application when we're trying to get them; Report to ODA
const std::set<std::string> PROBLEMATIC_PARAMS = {
	"ROOF_SLOPE",
	"RBS_PIPE_SIZE_MAXIMUM",
	"RBS_PIPE_SIZE_MINIMUM",
	"RBS_SYSTEM_CLASSIFICATION_PARAM",
	"RBS_ELECTRICAL_DATA"
};

//These metadata params are not of interest to users. Do not read.
const std::set<std::string> IGNORE_PARAMS = {
	"RENDER APPEARANCE",
	"RENDER APPEARANCE PROPERTIES"
};

bool DataProcessorRvt::ignoreParam(const std::string& param)
{
	auto paramUpper = param;
	std::transform(paramUpper.begin(), paramUpper.end(), paramUpper.begin(), ::toupper);
	return PROBLEMATIC_PARAMS.find(param) != PROBLEMATIC_PARAMS.end() || IGNORE_PARAMS.find(paramUpper) != IGNORE_PARAMS.end();
}

std::string DataProcessorRvt::getElementName(OdBmElementPtr element, uint64_t id)
{
	std::string elName(convertToStdString(element->getElementName()));
	if (!elName.empty())
		elName.append("_");

	elName.append(std::to_string(id));

	return elName;
}


std::string DataProcessorRvt::determineTexturePath(const std::string& inputPath)
{
	// Try to extract one valid paths if multiple paths are provided
	auto pathStr = inputPath.substr(0, inputPath.find("|", 0));
	std::replace(pathStr.begin(), pathStr.end(), '\\', '/');
	auto texturePath = boost::filesystem::path(pathStr).make_preferred();
	if (repo::lib::doesFileExist(texturePath))
		return texturePath.generic_string();

	// Try to apply absolute path
	char* env = std::getenv(RVT_TEXTURES_ENV_VARIABLE);
	if (env == nullptr)
		return std::string();

	auto absolutePath = boost::filesystem::absolute(texturePath, env);
	if (repo::lib::doesFileExist(absolutePath))
		return absolutePath.generic_string();

	// Sometimes the texture path has subdirectories like "./mat/1" remove it and see if we can find it.
	auto altPath = boost::filesystem::absolute(texturePath.leaf(), env);
	if (repo::lib::doesFileExist(altPath))
		return altPath.generic_string();

	repoDebug << "Failed to find: " << texturePath;
	return std::string();
}

std::string DataProcessorRvt::translateMetadataValue(
	const OdTfVariant& val, 
	OdBmLabelUtilsPEPtr labelUtils, 
	OdBmParamDefPtr paramDef, 
	OdBmDatabase* database, 
	OdBm::BuiltInParameterDefinition::Enum param)
{
	std::string strOut;
	switch (val.type()) {
	case OdTfVariant::kString:
		strOut = convertToStdString(val.getString());
		break;
	case OdTfVariant::kBool:
		strOut = std::to_string(val.getBool());
		break;
	case OdTfVariant::kInt8:
		strOut = std::to_string(val.getInt8());
		break;
	case OdTfVariant::kInt16:
		strOut = std::to_string(val.getInt16());
		break;
	case OdTfVariant::kInt32:
		if (paramDef->getParameterType() == OdBm::ParameterType::YesNo)
			(val.getInt32()) ? strOut = "Yes" : strOut = "No";
		else
			strOut = std::to_string(val.getInt32());
		break;
	case OdTfVariant::kInt64:
		strOut = std::to_string(val.getInt64());
		break;
	case OdTfVariant::kDouble:
		strOut = convertToStdString(labelUtils->format(*database->getUnits(), paramDef->getUnitType(), val.getDouble(), false));
		break;
	case OdTfVariant::kDbStubPtr:
			OdDbStub* stub = val.getDbStubPtr();
			if (stub)
			{
				OdBmObjectId rawValue = OdBmObjectId(stub);
				if (param == OdBm::BuiltInParameter::ELEM_CATEGORY_PARAM || param == OdBm::BuiltInParameter::ELEM_CATEGORY_PARAM_MT)
				{
					OdDbHandle hdl = rawValue.getHandle();
					if (OdBmObjectId::isRegularHandle(hdl))
					{
						strOut = std::to_string((OdUInt64)hdl);
					}
					else
					{
						OdBm::BuiltInCategory::Enum builtInValue = static_cast<OdBm::BuiltInCategory::Enum>((OdUInt64)rawValue.getHandle());
						strOut = convertToStdString(OdBm::BuiltInCategory(builtInValue).toString());
					}
				}
				else
				{
					strOut = std::to_string((OdUInt64)rawValue.getHandle());
				}
			}
	}

	return strOut;
}

void DataProcessorRvt::init(GeometryCollector* geoColl, OdBmDatabasePtr database)
{
	this->collector = geoColl;
	this->database = database;
	//getCameras(database);
	forEachBmDBView(database, [&](OdBmDBViewPtr pDBView) { hiddenElementsViewRejection(pDBView); });

	establishProjectTranslation(database);
}

DataProcessorRvt::DataProcessorRvt() :
	meshesCount(0)
{
}

void DataProcessorRvt::beginViewVectorization()
{
	DataProcessor::beginViewVectorization();
	setEyeToOutputTransform(getEyeToWorldTransform());
}<commit_msg>ISSUE #332 read element name if available instead of just writing out the handle ID<commit_after>/**
*  Copyright (C) 2018 3D Repo Ltd
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU Affero General Public License as
*  published by the Free Software Foundation, either version 3 of the
*  License, or (at your option) any later version.
*
*  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 Affero General Public License for more details.
*
*  You should have received a copy of the GNU Affero General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <boost/filesystem.hpp>
#include <OdPlatformSettings.h>

#include "vectorise_device_rvt.h"
#include "data_processor_rvt.h"
#include "helper_functions.h"
#include "../../../../lib/repo_utils.h"

using namespace repo::manipulator::modelconvertor::odaHelper;

//These metadata params are crashing application when we're trying to get them; Report to ODA
const std::set<std::string> PROBLEMATIC_PARAMS = {
	"ROOF_SLOPE",
	"RBS_PIPE_SIZE_MAXIMUM",
	"RBS_PIPE_SIZE_MINIMUM",
	"RBS_SYSTEM_CLASSIFICATION_PARAM",
	"RBS_ELECTRICAL_DATA"
};

//These metadata params are not of interest to users. Do not read.
const std::set<std::string> IGNORE_PARAMS = {
	"RENDER APPEARANCE",
	"RENDER APPEARANCE PROPERTIES"
};

bool DataProcessorRvt::ignoreParam(const std::string& param)
{
	auto paramUpper = param;
	std::transform(paramUpper.begin(), paramUpper.end(), paramUpper.begin(), ::toupper);
	return PROBLEMATIC_PARAMS.find(param) != PROBLEMATIC_PARAMS.end() || IGNORE_PARAMS.find(paramUpper) != IGNORE_PARAMS.end();
}

std::string DataProcessorRvt::getElementName(OdBmElementPtr element, uint64_t id)
{
	std::string elName(convertToStdString(element->getElementName()));
	if (!elName.empty())
		elName.append("_");

	elName.append(std::to_string(id));

	return elName;
}


std::string DataProcessorRvt::determineTexturePath(const std::string& inputPath)
{
	// Try to extract one valid paths if multiple paths are provided
	auto pathStr = inputPath.substr(0, inputPath.find("|", 0));
	std::replace(pathStr.begin(), pathStr.end(), '\\', '/');
	auto texturePath = boost::filesystem::path(pathStr).make_preferred();
	if (repo::lib::doesFileExist(texturePath))
		return texturePath.generic_string();

	// Try to apply absolute path
	char* env = std::getenv(RVT_TEXTURES_ENV_VARIABLE);
	if (env == nullptr)
		return std::string();

	auto absolutePath = boost::filesystem::absolute(texturePath, env);
	if (repo::lib::doesFileExist(absolutePath))
		return absolutePath.generic_string();

	// Sometimes the texture path has subdirectories like "./mat/1" remove it and see if we can find it.
	auto altPath = boost::filesystem::absolute(texturePath.leaf(), env);
	if (repo::lib::doesFileExist(altPath))
		return altPath.generic_string();

	repoDebug << "Failed to find: " << texturePath;
	return std::string();
}

std::string DataProcessorRvt::translateMetadataValue(
	const OdTfVariant& val, 
	OdBmLabelUtilsPEPtr labelUtils, 
	OdBmParamDefPtr paramDef, 
	OdBmDatabase* database, 
	OdBm::BuiltInParameterDefinition::Enum param)
{
	std::string strOut;
	switch (val.type()) {
	case OdTfVariant::kString:
		strOut = convertToStdString(val.getString());
		break;
	case OdTfVariant::kBool:
		strOut = std::to_string(val.getBool());
		break;
	case OdTfVariant::kInt8:
		strOut = std::to_string(val.getInt8());
		break;
	case OdTfVariant::kInt16:
		strOut = std::to_string(val.getInt16());
		break;
	case OdTfVariant::kInt32:
		if (paramDef->getParameterType() == OdBm::ParameterType::YesNo)
			(val.getInt32()) ? strOut = "Yes" : strOut = "No";
		else
			strOut = std::to_string(val.getInt32());
		break;
	case OdTfVariant::kInt64:
		strOut = std::to_string(val.getInt64());
		break;
	case OdTfVariant::kDouble:
		strOut = convertToStdString(labelUtils->format(*database->getUnits(), paramDef->getUnitType(), val.getDouble(), false));
		break;
	case OdTfVariant::kDbStubPtr:
			OdDbStub* stub = val.getDbStubPtr();
			if (stub)
			{
				OdBmObjectId rawValue = OdBmObjectId(stub);
				if (param == OdBm::BuiltInParameter::ELEM_CATEGORY_PARAM || param == OdBm::BuiltInParameter::ELEM_CATEGORY_PARAM_MT)
				{
					OdDbHandle hdl = rawValue.getHandle();
					if (OdBmObjectId::isRegularHandle(hdl))
					{
						strOut = std::to_string((OdUInt64)hdl);
					}
					else
					{
						OdBm::BuiltInCategory::Enum builtInValue = static_cast<OdBm::BuiltInCategory::Enum>((OdUInt64)rawValue.getHandle());
						strOut = convertToStdString(OdBm::BuiltInCategory(builtInValue).toString());
					}
				}
				else
				{
					OdBmElementPtr elem = database->getObjectId(rawValue.getHandle()).safeOpenObject();
					if (elem->getElementName() == OdString::kEmpty)
						strOut = std::to_string((OdUInt64)rawValue.getHandle());
					else
						strOut = convertToStdString(elem->getElementName());
				}
			}
	}

	return strOut;
}

void DataProcessorRvt::init(GeometryCollector* geoColl, OdBmDatabasePtr database)
{
	this->collector = geoColl;
	this->database = database;
	//getCameras(database);
	forEachBmDBView(database, [&](OdBmDBViewPtr pDBView) { hiddenElementsViewRejection(pDBView); });

	establishProjectTranslation(database);
}

DataProcessorRvt::DataProcessorRvt() :
	meshesCount(0)
{
}

void DataProcessorRvt::beginViewVectorization()
{
	DataProcessor::beginViewVectorization();
	setEyeToOutputTransform(getEyeToWorldTransform());
}
{'fraction_non_alphanumeric': 0.1316703216374269, 'fraction_numerical': 0.006487573099415204, 'mean_word_length': 5.476331360946745, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1223206', 'n_tokens_mistral': 3982, 'n_tokens_neox': 3654, 'n_words': 815}