Improve error handling for the dxf export.

This commit is contained in:
Roman Telezhynskyi 2021-04-28 20:06:24 +03:00
parent a2d7446308
commit 613502b392
10 changed files with 104 additions and 50 deletions

View File

@ -7,6 +7,7 @@
- [smart-pattern/valentina#122] Extend piece bounding rect. - [smart-pattern/valentina#122] Extend piece bounding rect.
- Remember last selected export format. - Remember last selected export format.
- [smart-pattern/valentina#123] Error inside Save layout dialog. - [smart-pattern/valentina#123] Error inside Save layout dialog.
- Improve error handling for the dxf export.
# Version 0.7.46 Mar 31, 2021 # Version 0.7.46 Mar 31, 2021
- Fix incorrect calculation of value for multisize measurements in Valentina. - Fix incorrect calculation of value for multisize measurements in Valentina.

View File

@ -110,6 +110,11 @@ void dx_iface::writeEntity(DRW_Entity* e){
} }
} }
std::string dx_iface::ErrorString() const
{
return dxfW->ErrorString();
}
void dx_iface::writeHeader(DRW_Header &data){ void dx_iface::writeHeader(DRW_Header &data){
//complete copy of header vars: //complete copy of header vars:
data = cData.headerC; data = cData.headerC;

View File

@ -101,6 +101,8 @@ public:
bool fileExport(bool binary); bool fileExport(bool binary);
void writeEntity(DRW_Entity* e); void writeEntity(DRW_Entity* e);
std::string ErrorString() const;
//reimplement virtual DRW_Interface functions //reimplement virtual DRW_Interface functions
//writer part, send all in class dx_data to writer //writer part, send all in class dx_data to writer
virtual void writeHeader(DRW_Header& data) override; virtual void writeHeader(DRW_Header& data) override;

View File

@ -123,55 +123,66 @@ bool dxfRW::read(DRW_Interface *interface_, bool ext){
bool dxfRW::write(DRW_Interface *interface_, DRW::Version ver, bool bin){ bool dxfRW::write(DRW_Interface *interface_, DRW::Version ver, bool bin){
bool isOk = false; 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_;
if (binFile) { try
filestr.open (fileName.c_str(), std::ios_base::out | std::ios::binary | std::ios::trunc); {
//write sentinel if (binFile) {
filestr << "AutoCAD Binary DXF\r\n" << static_cast<char>(26) << '\0'; filestr.open (fileName.c_str(), std::ios_base::out | std::ios::binary | std::ios::trunc);
writer = new dxfWriterBinary(&filestr); //write sentinel
DRW_DBG("dxfRW::read binary file\n"); filestr << "AutoCAD Binary DXF\r\n" << static_cast<char>(26) << '\0';
} else { writer = new dxfWriterBinary(&filestr);
filestr.open (fileName.c_str(), std::ios_base::out | std::ios::trunc); DRW_DBG("dxfRW::read binary file\n");
writer = new dxfWriterAscii(&filestr); } else {
std::string comm = std::string("dxfrw ") + std::string(DRW_VERSION); filestr.open (fileName.c_str(), std::ios_base::out | std::ios::trunc);
writer->writeString(999, comm); writer = new dxfWriterAscii(&filestr);
} std::string comm = std::string("dxfrw ") + std::string(DRW_VERSION);
DRW_Header header; writer->writeString(999, comm);
iface->writeHeader(header); }
writer->writeString(0, "SECTION"); DRW_Header header;
entCount =FIRSTHANDLE; iface->writeHeader(header);
header.write(writer, version);
writer->writeString(0, "ENDSEC");
if (ver > DRW::AC1009) {
writer->writeString(0, "SECTION"); writer->writeString(0, "SECTION");
writer->writeString(2, "CLASSES"); entCount =FIRSTHANDLE;
header.write(writer, version);
writer->writeString(0, "ENDSEC"); writer->writeString(0, "ENDSEC");
} if (ver > DRW::AC1009) {
writer->writeString(0, "SECTION"); writer->writeString(0, "SECTION");
writer->writeString(2, "TABLES"); writer->writeString(2, "CLASSES");
writeTables(); writer->writeString(0, "ENDSEC");
writer->writeString(0, "ENDSEC"); }
writer->writeString(0, "SECTION");
writer->writeString(2, "BLOCKS");
writeBlocks();
writer->writeString(0, "ENDSEC");
writer->writeString(0, "SECTION");
writer->writeString(2, "ENTITIES");
iface->writeEntities();
writer->writeString(0, "ENDSEC");
if (version > DRW::AC1009) {
writer->writeString(0, "SECTION"); writer->writeString(0, "SECTION");
writer->writeString(2, "OBJECTS"); writer->writeString(2, "TABLES");
writeObjects(); writeTables();
writer->writeString(0, "ENDSEC"); writer->writeString(0, "ENDSEC");
writer->writeString(0, "SECTION");
writer->writeString(2, "BLOCKS");
writeBlocks();
writer->writeString(0, "ENDSEC");
writer->writeString(0, "SECTION");
writer->writeString(2, "ENTITIES");
iface->writeEntities();
writer->writeString(0, "ENDSEC");
if (version > DRW::AC1009) {
writer->writeString(0, "SECTION");
writer->writeString(2, "OBJECTS");
writeObjects();
writer->writeString(0, "ENDSEC");
}
writer->writeString(0, "EOF");
filestr.flush();
filestr.close();
}
catch(std::ofstream::failure &writeErr)
{
errorString = writeErr.what();
delete writer;
writer = nullptr;
return isOk;
} }
writer->writeString(0, "EOF");
filestr.flush();
filestr.close();
isOk = true; isOk = true;
delete writer; delete writer;
writer = nullptr; writer = nullptr;
@ -1160,6 +1171,11 @@ bool dxfRW::writeDimension(DRW_Dimension *ent) {
return true; return true;
} }
std::string dxfRW::ErrorString() const
{
return errorString;
}
bool dxfRW::writeInsert(DRW_Insert *ent){ bool dxfRW::writeInsert(DRW_Insert *ent){
writer->writeString(0, "INSERT"); writer->writeString(0, "INSERT");
writeEntity(ent); writeEntity(ent);

View File

@ -72,6 +72,8 @@ public:
bool writeDimension(DRW_Dimension *ent); bool writeDimension(DRW_Dimension *ent);
void setEllipseParts(int parts){elParts = parts;} /*!< set parts munber when convert ellipse to polyline */ void setEllipseParts(int parts){elParts = parts;} /*!< set parts munber when convert ellipse to polyline */
std::string ErrorString() const;
private: private:
Q_DISABLE_COPY(dxfRW) Q_DISABLE_COPY(dxfRW)
/// used by read() to parse the content of the file /// used by read() to parse the content of the file
@ -144,6 +146,7 @@ private:
int currHandle; int currHandle;
std::string errorString{};
}; };
#endif // LIBDXFRW_H #endif // LIBDXFRW_H

View File

@ -138,8 +138,7 @@ bool VDxfEngine::begin(QPaintDevice *pdev)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
bool VDxfEngine::end() bool VDxfEngine::end()
{ {
const bool res = input->fileExport(m_binary); return input->fileExport(m_binary);
return res;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -611,6 +610,12 @@ void VDxfEngine::SetYScale(const qreal &yscale)
m_yscale = yscale; m_yscale = yscale;
} }
//---------------------------------------------------------------------------------------------------------------------
QString VDxfEngine::ErrorString() const
{
return QString::fromStdString(input->ErrorString());
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QT_WARNING_PUSH QT_WARNING_PUSH
QT_WARNING_DISABLE_GCC("-Wswitch-default") QT_WARNING_DISABLE_GCC("-Wswitch-default")
@ -668,7 +673,7 @@ bool VDxfEngine::ExportToAAMA(const QVector<VLayoutPiece> &details)
for(auto detail : details) for(auto detail : details)
{ {
dx_ifaceBlock *detailBlock = new dx_ifaceBlock(); auto *detailBlock = new dx_ifaceBlock();
QString blockName = detail.GetName(); QString blockName = detail.GetName();
if (m_version <= DRW::AC1009) if (m_version <= DRW::AC1009)
@ -691,7 +696,7 @@ bool VDxfEngine::ExportToAAMA(const QVector<VLayoutPiece> &details)
input->AddBlock(detailBlock); input->AddBlock(detailBlock);
DRW_Insert *insert = new DRW_Insert(); auto *insert = new DRW_Insert();
insert->name = blockName.toStdString(); insert->name = blockName.toStdString();
insert->layer = '1'; insert->layer = '1';
@ -869,7 +874,7 @@ bool VDxfEngine::ExportToASTM(const QVector<VLayoutPiece> &details)
for(auto detail : details) for(auto detail : details)
{ {
dx_ifaceBlock *detailBlock = new dx_ifaceBlock(); auto *detailBlock = new dx_ifaceBlock();
QString blockName = detail.GetName(); QString blockName = detail.GetName();
if (m_version <= DRW::AC1009) if (m_version <= DRW::AC1009)
@ -894,7 +899,7 @@ bool VDxfEngine::ExportToASTM(const QVector<VLayoutPiece> &details)
input->AddBlock(detailBlock); input->AddBlock(detailBlock);
DRW_Insert *insert = new DRW_Insert(); auto *insert = new DRW_Insert();
insert->name = blockName.toStdString(); insert->name = blockName.toStdString();
insert->layer = '1'; insert->layer = '1';

View File

@ -98,6 +98,8 @@ public:
qreal GetYScale() const; qreal GetYScale() const;
void SetYScale(const qreal &yscale); void SetYScale(const qreal &yscale);
QString ErrorString() const;
private: private:
Q_DISABLE_COPY(VDxfEngine) Q_DISABLE_COPY(VDxfEngine)
QSize size; QSize size;

View File

@ -214,6 +214,12 @@ bool VDxfPaintDevice::ExportToASTM(const QVector<VLayoutPiece> &details) const
return res; return res;
} }
//---------------------------------------------------------------------------------------------------------------------
QString VDxfPaintDevice::ErrorString() const
{
return engine->ErrorString();
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
int VDxfPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const int VDxfPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const
{ {

View File

@ -75,6 +75,8 @@ public:
bool ExportToAAMA(const QVector<VLayoutPiece> &details) const; bool ExportToAAMA(const QVector<VLayoutPiece> &details) const;
bool ExportToASTM(const QVector<VLayoutPiece> &details) const; bool ExportToASTM(const QVector<VLayoutPiece> &details) const;
QString ErrorString() const;
protected: protected:
virtual int metric(PaintDeviceMetric metric) const override; virtual int metric(PaintDeviceMetric metric) const override;
private: private:

View File

@ -301,7 +301,11 @@ void VLayoutExporter::ExportToFlatDXF(QGraphicsScene *scene, const QList<QList<Q
{ {
painter.scale(m_xScale, m_yScale); painter.scale(m_xScale, m_yScale);
scene->render(&painter, m_imageRect, m_imageRect, Qt::IgnoreAspectRatio); scene->render(&painter, m_imageRect, m_imageRect, Qt::IgnoreAspectRatio);
painter.end(); if (not painter.end())
{
qCritical() << tr("Can't create an flat dxf file.")
<< generator.ErrorString();
}
} }
RestoreTextAfterDXF(endStringPlaceholder, details); RestoreTextAfterDXF(endStringPlaceholder, details);
@ -319,7 +323,11 @@ void VLayoutExporter::ExportToAAMADXF(const QVector<VLayoutPiece> &details) cons
generator.setInsunits(VarInsunits::Millimeters);// Decided to always use mm. See issue #745 generator.setInsunits(VarInsunits::Millimeters);// Decided to always use mm. See issue #745
generator.SetXScale(m_xScale); generator.SetXScale(m_xScale);
generator.SetYScale(m_yScale); generator.SetYScale(m_yScale);
generator.ExportToAAMA(details); if (not generator.ExportToAAMA(details))
{
qCritical() << tr("Can't create an AAMA dxf file.")
<< generator.ErrorString();
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -334,7 +342,11 @@ void VLayoutExporter::ExportToASTMDXF(const QVector<VLayoutPiece> &details) cons
generator.setInsunits(VarInsunits::Millimeters);// Decided to always use mm. See issue #745 generator.setInsunits(VarInsunits::Millimeters);// Decided to always use mm. See issue #745
generator.SetXScale(m_xScale); generator.SetXScale(m_xScale);
generator.SetYScale(m_yScale); generator.SetYScale(m_yScale);
generator.ExportToASTM(details); if (not generator.ExportToASTM(details))
{
qCritical() << tr("Can't create an ASTM dxf file.")
<< generator.ErrorString();
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------