Newer
Older
#include "CADwriter.h"
#include "Message.hxx"
#include "Message_PrinterOStream.hxx"
#include "STEPControl_Writer.hxx"
CADwriter::CADwriter(const std::string& savepath)
{
const FileExt ext = determineExtension(savepath);
switch (ext)
{
case FileExt::STEP:
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;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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");
}
}
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");
}
}