<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 <OdaCommon.h>

#include <StaticRxObject.h>
#include <RxInit.h>
#include <RxDynamicModule.h>
#include <DynamicLinker.h>
#include <DgDatabase.h>
#include <RxDynamicModule.h>

#include <ExSystemServices.h>
#include <ExDgnServices.h>
#include <ExDgnHostAppServices.h>

#include <DgGiContext.h>
#include <DgGsManager.h>

#include "file_processor_dgn.h"
#include "geometry_dumper_dgn.h"
#include "vectorise_device_dgn.h"

#include <DgLine.h>      // This file puts OdDgLine3d in the output file
using namespace repo::manipulator::modelconvertor::odaHelper;

class StubDeviceModuleText : public OdGsBaseModule
{
private:
	GeometryCollector *collector;
	OdGeMatrix3d        m_matTransform;
	const std::map<OdDbStub*, double>* m_pMapDeviations;

public:
	void init(GeometryCollector* const collector)
	{
		this->collector = collector;
	}
protected:
	OdSmartPtr<OdGsBaseVectorizeDevice> createDeviceObject()
	{
		return OdRxObjectImpl<VectoriseDeviceDgn, OdGsBaseVectorizeDevice>::createObject();
	}
	OdSmartPtr<OdGsViewImpl> createViewObject()
	{
		OdSmartPtr<OdGsViewImpl> pP = OdRxObjectImpl<GeometryDumperDgn, OdGsViewImpl>::createObject();
		((GeometryDumperDgn*)pP.get())->init(collector);
		return pP;
	}
	OdSmartPtr<OdGsBaseVectorizeDevice> createBitmapDeviceObject()
	{
		return OdSmartPtr<OdGsBaseVectorizeDevice>();
	}
	OdSmartPtr<OdGsViewImpl> createBitmapViewObject()
	{
		return OdSmartPtr<OdGsViewImpl>();
	}
};
ODRX_DEFINE_PSEUDO_STATIC_MODULE(StubDeviceModuleText);

class RepoDgnServices : public OdExDgnSystemServices, public OdExDgnHostAppServices
{
protected:
	ODRX_USING_HEAP_OPERATORS(OdExDgnSystemServices);
};

repo::manipulator::modelconvertor::odaHelper::FileProcessorDgn::~FileProcessorDgn()
{
}
int FileProcessorDgn::readFile() {

	int   nRes = 0;               // Return value for the function
	OdStaticRxObject<RepoDgnServices> svcs;
	OdString fileSource = file.c_str();
	OdString strStlFilename = OdString::kEmpty;


	/**********************************************************************/
	/* Initialize Runtime Extension environment                           */
	/**********************************************************************/
	odrxInitialize(&svcs);
	odgsInitialize();


	try
	{
		/**********************************************************************/
		/* Initialize Teigha for .dgn files                                               */
		/**********************************************************************/
		::odrxDynamicLinker()->loadModule(L"TG_Db", false);

		OdDgDatabasePtr pDb = svcs.readFile(fileSource);

		if (!pDb.isNull())
		{

			const ODCOLORREF* refColors = OdDgColorTable::currentPalette(pDb);
			ODGSPALETTE pPalCpy;
			pPalCpy.insert(pPalCpy.begin(), refColors, refColors + 256);


			OdDgElementId elementId = pDb->getActiveModelId();
			if (elementId.isNull())
			{
				elementId = pDb->getDefaultModelId();
				pDb->setActiveModelId(elementId);
			}
			OdDgElementId elementActId = pDb->getActiveModelId();
			OdDgModelPtr pModel = elementId.safeOpenObject();
			ODCOLORREF background = pModel->getBackground();

			OdDgElementId vectorizedViewId;
			OdDgViewGroupPtr pViewGroup = pDb->getActiveViewGroupId().openObject();
			if (pViewGroup.isNull())
			{
				//  Some files can have invalid id for View Group. Try to get & use a valid (recommended) View Group object.
				pViewGroup = pDb->recommendActiveViewGroupId().openObject();
				if (pViewGroup.isNull())
				{
					// Add View group
					pModel->createViewGroup();
					pModel->fitToView();
					pViewGroup = pDb->recommendActiveViewGroupId().openObject();<commit_msg>ISSUE #291 - Merge with staging<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 <OdaCommon.h>

#include <StaticRxObject.h>
#include <RxInit.h>
#include <RxDynamicModule.h>
#include <DynamicLinker.h>
#include <DgDatabase.h>
#include <RxDynamicModule.h>

#include <ExSystemServices.h>
#include <ExDgnServices.h>
#include <ExDgnHostAppServices.h>

#include <DgGiContext.h>
#include <DgGsManager.h>

#include "file_processor_dgn.h"
#include "geometry_dumper_dgn.h"
#include "vectorise_device_dgn.h"
#include "helper_functions.h"

#include <DgLine.h>      // This file puts OdDgLine3d in the output file
using namespace repo::manipulator::modelconvertor::odaHelper;

class StubDeviceModuleText : public OdGsBaseModule
{
private:
	GeometryCollector *collector;
	OdGeMatrix3d        m_matTransform;
	const std::map<OdDbStub*, double>* m_pMapDeviations;

public:
	void init(GeometryCollector* const collector)
	{
		this->collector = collector;
	}
protected:
	OdSmartPtr<OdGsBaseVectorizeDevice> createDeviceObject()
	{
		return OdRxObjectImpl<VectoriseDeviceDgn, OdGsBaseVectorizeDevice>::createObject();
	}
	OdSmartPtr<OdGsViewImpl> createViewObject()
	{
		OdSmartPtr<OdGsViewImpl> pP = OdRxObjectImpl<GeometryDumperDgn, OdGsViewImpl>::createObject();
		((GeometryDumperDgn*)pP.get())->init(collector);
		return pP;
	}
	OdSmartPtr<OdGsBaseVectorizeDevice> createBitmapDeviceObject()
	{
		return OdSmartPtr<OdGsBaseVectorizeDevice>();
	}
	OdSmartPtr<OdGsViewImpl> createBitmapViewObject()
	{
		return OdSmartPtr<OdGsViewImpl>();
	}
};
ODRX_DEFINE_PSEUDO_STATIC_MODULE(StubDeviceModuleText);

class RepoDgnServices : public OdExDgnSystemServices, public OdExDgnHostAppServices
{
protected:
	ODRX_USING_HEAP_OPERATORS(OdExDgnSystemServices);
};

repo::manipulator::modelconvertor::odaHelper::FileProcessorDgn::~FileProcessorDgn()
{
}
int FileProcessorDgn::readFile() {

	int   nRes = 0;               // Return value for the function
	OdStaticRxObject<RepoDgnServices> svcs;
	OdString fileSource = file.c_str();
	OdString strStlFilename = OdString::kEmpty;


	/**********************************************************************/
	/* Initialize Runtime Extension environment                           */
	/**********************************************************************/
	odrxInitialize(&svcs);
	odgsInitialize();


	try
	{
		/**********************************************************************/
		/* Initialize Teigha for .dgn files                                               */
		/**********************************************************************/
		::odrxDynamicLinker()->loadModule(L"TG_Db", false);

		OdDgDatabasePtr pDb = svcs.readFile(fileSource);

		if (!pDb.isNull())
		{

			const ODCOLORREF* refColors = OdDgColorTable::currentPalette(pDb);
			ODGSPALETTE pPalCpy;
			pPalCpy.insert(pPalCpy.begin(), refColors, refColors + 256);

			OdDgElementId elementId = pDb->getActiveModelId();

			if (elementId.isValid()) {
				OdDgModelPtr viewElement = elementId.openObject(OdDg::kForRead);
				auto activeViewName = convertToStdString(viewElement->getName());
				if (viewElement->getType() != OdDgModel::Type::kDesignModel || activeViewName == "Title")
				{
					OdDgElementIteratorPtr pModelIter = pDb->getModelTable()->createIterator();
					for (; !pModelIter->done(); pModelIter->step())
					{
						OdDgModelPtr elem = pModelIter->item().openObject();
						auto viewName = convertToStdString(elem->getName());
						if (!elem.isNull() && elem->getType() == OdDgModel::Type::kDesignModel &&
							viewName != "Title") {
							repoInfo << "Setting active view to: " << viewName;
							pDb->setActiveModelId(pModelIter->item());
							elementId = pModelIter->item();
							break;
						}
					}
				}
			}

			if (elementId.isNull())
			{
				elementId = pDb->getDefaultModelId();
				pDb->setActiveModelId(elementId);
			}
			OdDgElementId elementActId = pDb->getActiveModelId();
			OdDgModelPtr pModel = elementId.safeOpenObject();
			ODCOLORREF background = pModel->getBackground();

			OdDgElementId vectorizedViewId;
			OdDgViewGroupPtr pViewGroup = pDb->getActiveViewGroupId().openObject();
			if (pViewGroup.isNull())
			{
				//  Some files can have invalid id for View Group. Try to get & use a valid (recommended) View Group object.
				pViewGroup = pDb->recommendActiveViewGroupId().openObject();
				if (pViewGroup.isNull())
				{
					// Add View group
					pModel->createViewGroup();
					pModel->fitToView();
					pViewGroup = pDb->recommendActiveViewGroupId().openObject();
{'fraction_non_alphanumeric': 0.16353521570273957, 'fraction_numerical': 0.002834050593051328, 'mean_word_length': 4.725961538461538, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4943981', 'n_tokens_mistral': 3319, 'n_tokens_neox': 2931, 'n_words': 658}