Skip to content
Snippets Groups Projects
CADwriter.cpp 2.3 KiB
Newer Older
#include "CADwriter.h"

#include "Message.hxx"
#include "Message_PrinterOStream.hxx"

#include "STEPControl_Writer.hxx"
samuel.hayden's avatar
samuel.hayden committed
#include "IGESControl_Writer.hxx"

CADwriter::CADwriter(const std::string& savepath)
{
	const FileExt ext = determineExtension(savepath);
	switch (ext)
	{
	case FileExt::STEP:
samuel.hayden's avatar
samuel.hayden committed
	case FileExt::IGES:
		_saveType = ext;
		break;
	default:
		_saveType = FileExt::STEP;
		_savepath += ".stp";
		_errors.push(ErrorCodes::InvalidExtension);
		_errorMsgs.push("Saving as .stp by default");
		break;
	}
}

void CADwriter::write(const TopoDS_Shape& saveshape)
{
	switch (_saveType)
	{
	case STEP:
		writeSTEP(saveshape);
		return;
samuel.hayden's avatar
samuel.hayden committed
	case IGES:
		writeIGES(saveshape);
		return;
	default:
		_errors.push(ErrorCodes::UnknownError);
		_errorMsgs.push("File Extension not defined");
		return;
	}
}

FileExt CADwriter::determineExtension(const std::string& savepath)
{
	size_t extIdx = savepath.find_last_of('.');
	if (extIdx == std::string::npos)
	{
		_savepath = savepath;
		return Invalid;
	}

	std::string extstr = savepath.substr(extIdx);

	// Is a valid extension
	if (fileExtensions.find(extstr) != fileExtensions.end())
	{
		_savepath = savepath;
		return fileExtensions.at(extstr);
	}

	// Strip existing extension as it is invalid (will be saved as a stp)
	_savepath = savepath.substr(0, extIdx);
	return Invalid;
}

/* Different Export Formats */
// Write to step format
void CADwriter::writeSTEP(const TopoDS_Shape& saveshape)
{
	STEPControl_Writer writer;
	Message::DefaultMessenger()->RemovePrinters(STANDARD_TYPE(Message_PrinterOStream));
	writer.Transfer(saveshape, STEPControl_ManifoldSolidBrep);
	auto status = writer.Write(_savepath.c_str());
	if (status != IFSelect_RetDone)
	{
		_errors.push(ErrorCodes::SaveFailed);
		_errorMsgs.push("Unknown error occured while exporting");
	}
}
samuel.hayden's avatar
samuel.hayden committed

void CADwriter::writeIGES(const TopoDS_Shape& saveshape)
{
	IGESControl_Writer writer("MM", 1);
	Message::DefaultMessenger()->RemovePrinters(STANDARD_TYPE(Message_PrinterOStream));
	if (!writer.AddShape(saveshape))
	{
		_errors.push(ErrorCodes::SaveFailed);
		_errorMsgs.push("Unkown error occured while adding shape to IGES writer");
		return;
	}
	writer.ComputeModel();
	bool status = writer.Write(_savepath.c_str());
	if (status == false)
	{
		_errors.push(ErrorCodes::SaveFailed);
		_errorMsgs.push("Unknown error occured while exporting");
	}
}