Fix export to DXF AAMA/ASTM on Windows with Qt 6.

This commit is contained in:
Roman Telezhynskyi 2024-01-16 20:12:41 +02:00
parent ad20f93085
commit 4f7c9fd59b

View File

@ -109,17 +109,21 @@ auto dxfRW::read(DRW_Interface *interface_, bool ext) -> bool
auto dxfRW::write(DRW_Interface *interface_, DRW::Version ver, bool bin) -> bool auto dxfRW::write(DRW_Interface *interface_, DRW::Version ver, bool bin) -> bool
{ {
bool isOk = false;
std::ofstream filestr; std::ofstream filestr;
filestr.exceptions(std::ifstream::failbit | std::ifstream::badbit);
version = ver; version = ver;
binFile = bin; binFile = bin;
iface = interface_; iface = interface_;
try
{
if (binFile) if (binFile)
{ {
filestr.open(fileName.c_str(), std::ios_base::out | std::ios::binary | std::ios::trunc); filestr.open(fileName.c_str(), std::ios_base::out | std::ios::binary | std::ios::trunc);
if (!filestr.is_open())
{
errorString = "Error opening file!";
writer.reset();
return false;
}
// write sentinel // write sentinel
filestr << "AutoCAD Binary DXF\r\n" << static_cast<char>(26) << '\0'; filestr << "AutoCAD Binary DXF\r\n" << static_cast<char>(26) << '\0';
writer = std::make_unique<dxfWriterBinary>(&filestr); writer = std::make_unique<dxfWriterBinary>(&filestr);
@ -128,10 +132,19 @@ auto dxfRW::write(DRW_Interface *interface_, DRW::Version ver, bool bin) -> bool
else else
{ {
filestr.open(fileName.c_str(), std::ios_base::out | std::ios::trunc); filestr.open(fileName.c_str(), std::ios_base::out | std::ios::trunc);
if (!filestr.is_open())
{
errorString = "Error opening file!";
writer.reset();
return false;
}
writer = std::make_unique<dxfWriterAscii>(&filestr); writer = std::make_unique<dxfWriterAscii>(&filestr);
std::string comm = std::string("dxfrw ") + std::string(DRW_VERSION); std::string const comm = std::string("dxfrw ") + std::string(DRW_VERSION);
writer->writeString(999, comm); writer->writeString(999, comm);
} }
this->header = DRW_Header(); this->header = DRW_Header();
iface->writeHeader(header); iface->writeHeader(header);
writer->writeString(0, "SECTION"); writer->writeString(0, "SECTION");
@ -167,17 +180,16 @@ auto dxfRW::write(DRW_Interface *interface_, DRW::Version ver, bool bin) -> bool
} }
writer->writeString(0, "EOF"); writer->writeString(0, "EOF");
filestr.flush(); filestr.flush();
filestr.close();
} if (filestr.fail())
catch (std::ofstream::failure &writeErr)
{ {
errorString = writeErr.what(); errorString = "Error writing to file!";
writer.reset(); return false;
return isOk;
} }
isOk = true;
filestr.close();
writer.reset(); writer.reset();
return isOk; return true;
} }
auto dxfRW::writeEntity(DRW_Entity *ent) -> bool auto dxfRW::writeEntity(DRW_Entity *ent) -> bool