Fixed issue #463. Wrong export to DXF format.

--HG--
branch : release
This commit is contained in:
Roman Telezhynskyi 2016-03-27 18:51:13 +03:00
parent cb5623f937
commit 9fdc18ca27
7 changed files with 148 additions and 91 deletions

View File

@ -1,4 +1,5 @@
# Version 0.4.4 # Version 0.4.4
- [#463] Wrong export to DXF format.
- Fixed issue with deleting detail nodes. - Fixed issue with deleting detail nodes.
- [#458] Issue with segment of curve. - [#458] Issue with segment of curve.
- Fixed disappearing curve if start and finish points of a segment are equal. - Fixed disappearing curve if start and finish points of a segment are equal.

View File

@ -697,6 +697,11 @@ void MainWindowsNoGUI::ObjFile(const QString &name, int i) const
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
#if defined(Q_CC_GNU)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-default"
#endif
void MainWindowsNoGUI::DxfFile(const QString &name, int i) const void MainWindowsNoGUI::DxfFile(const QString &name, int i) const
{ {
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i)); QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i));
@ -705,7 +710,25 @@ void MainWindowsNoGUI::DxfFile(const QString &name, int i) const
VDxfPaintDevice generator; VDxfPaintDevice generator;
generator.setFileName(name); generator.setFileName(name);
generator.setSize(paper->rect().size().toSize()); generator.setSize(paper->rect().size().toSize());
generator.setResolution(static_cast<int>(PrintDPI)); generator.setResolution(PrintDPI);
switch (*pattern->GetPatternUnit())
{
case Unit::Cm:
generator.setInsunits(VarInsunits::Centimeters);
break;
case Unit::Mm:
generator.setInsunits(VarInsunits::Millimeters);
break;
case Unit::Inch:
generator.setInsunits(VarInsunits::Inches);
break;
case Unit::Px:
case Unit::LAST_UNIT_DO_NOT_USE:
Q_UNREACHABLE();
break;
}
QPainter painter; QPainter painter;
painter.begin(&generator); painter.begin(&generator);
scenes.at(i)->render(&painter, paper->rect(), paper->rect(), Qt::IgnoreAspectRatio); scenes.at(i)->render(&painter, paper->rect(), paper->rect(), Qt::IgnoreAspectRatio);
@ -713,6 +736,10 @@ void MainWindowsNoGUI::DxfFile(const QString &name, int i) const
} }
} }
#if defined(Q_CC_GNU)
#pragma GCC diagnostic pop
#endif
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QVector<QImage> MainWindowsNoGUI::AllSheets() const QVector<QImage> MainWindowsNoGUI::AllSheets() const
{ {

View File

@ -114,6 +114,14 @@ bool VDxfEngine::begin(QPaintDevice *pdev)
dw->dxfString(9, "$INSUNITS"); dw->dxfString(9, "$INSUNITS");
dw->dxfInt(70, static_cast<int>(varInsunits)); dw->dxfInt(70, static_cast<int>(varInsunits));
dw->dxfString(9, "$DIMSCALE");
dw->dxfReal(40, 1.0);
// Official documentation says that initial value is 1.0, however LibreCAD has trouble if not set this value
// explicitly.
dw->dxfString(9, "$DIMLFAC");
dw->dxfReal(40, 1.0);
QString dateTime = QDateTime::currentDateTime().toString("yyyyMMdd.HHmmsszzz"); QString dateTime = QDateTime::currentDateTime().toString("yyyyMMdd.HHmmsszzz");
dateTime.chop(1);// we need hundredths of a second dateTime.chop(1);// we need hundredths of a second
dw->dxfString(9, "$TDCREATE"); dw->dxfString(9, "$TDCREATE");
@ -252,14 +260,13 @@ void VDxfEngine::drawPath(const QPainterPath &path)
for (int i=1; i < polygon.count(); i++) for (int i=1; i < polygon.count(); i++)
{ {
dxf->writeLine( dxf->writeLine(*dw,
*dw, DL_LineData(FromPixel(polygon.at(i-1).x(), varInsunits), // start point
DL_LineData(polygon.at(i-1).x(), // start point FromPixel(getSize().height() - polygon.at(i-1).y(), varInsunits),
getSize().height() - polygon.at(i-1).y(), FromPixel(0.0, varInsunits),
0.0, FromPixel(polygon.at(i).x(), varInsunits), // end point
polygon.at(i).x(), // end point FromPixel(getSize().height() - polygon.at(i).y(), varInsunits),
getSize().height() - polygon.at(i).y(), FromPixel(0.0, varInsunits)),
0.0),
DL_Attributes("0", getPenColor(), -1, getPenStyle(), 1.0)); DL_Attributes("0", getPenColor(), -1, getPenStyle(), 1.0));
} }
} }
@ -270,17 +277,16 @@ void VDxfEngine::drawLines(const QLineF * lines, int lineCount)
{ {
for (int i = 0; i < lineCount; i++) for (int i = 0; i < lineCount; i++)
{ {
QPointF p1 = matrix.map(lines[i].p1()); const QPointF p1 = matrix.map(lines[i].p1());
QPointF p2 = matrix.map(lines[i].p2()); const QPointF p2 = matrix.map(lines[i].p2());
dxf->writeLine( dxf->writeLine(*dw,
*dw, DL_LineData(FromPixel(p1.x(), varInsunits), // start point
DL_LineData(p1.x(), // start point FromPixel(getSize().height() - p1.y(), varInsunits),
getSize().height() - p1.y(), FromPixel(0.0, varInsunits),
0.0, FromPixel(p2.x(), varInsunits), // end point
p2.x(), // end point FromPixel(getSize().height() - p2.y(), varInsunits),
getSize().height() - p2.y(), FromPixel(0.0, varInsunits)),
0.0),
DL_Attributes("0", getPenColor(), -1, getPenStyle(), 1.0)); DL_Attributes("0", getPenColor(), -1, getPenStyle(), 1.0));
} }
} }
@ -298,17 +304,16 @@ void VDxfEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawM
for (int i = 1; i < pointCount; i++) for (int i = 1; i < pointCount; i++)
{ {
QPointF p1 = matrix.map(points[i-1]); const QPointF p1 = matrix.map(points[i-1]);
QPointF p2 = matrix.map(points[i]); const QPointF p2 = matrix.map(points[i]);
dxf->writeLine( dxf->writeLine(*dw,
*dw, DL_LineData(FromPixel(p1.x(), varInsunits), // start point
DL_LineData(p1.x(), // start point FromPixel(getSize().height() - p1.y(), varInsunits),
getSize().height() - p1.y(), FromPixel(0.0, varInsunits),
0.0, FromPixel(p2.x(), varInsunits), // end point
p2.x(), // end point FromPixel(getSize().height() - p2.y(), varInsunits),
getSize().height() - p2.y(), FromPixel(0.0, varInsunits)),
0.0),
DL_Attributes("0", getPenColor(), -1, getPenStyle(), 1.0)); DL_Attributes("0", getPenColor(), -1, getPenStyle(), 1.0));
} }
} }
@ -322,8 +327,8 @@ void VDxfEngine::drawPolygon(const QPoint *points, int pointCount, QPaintEngine:
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VDxfEngine::drawEllipse(const QRectF & rect) void VDxfEngine::drawEllipse(const QRectF & rect)
{ {
QRectF newRect = matrix.mapRect(rect); const QRectF newRect = matrix.mapRect(rect);
double rotationAngle = atan(matrix.m12()/matrix.m11()); const double rotationAngle = atan(matrix.m12()/matrix.m11());
double majorX, majorY; // distanse between center and endpoint of the major axis double majorX, majorY; // distanse between center and endpoint of the major axis
double ratio; // ratio of minor axis to major axis double ratio; // ratio of minor axis to major axis
@ -343,15 +348,15 @@ void VDxfEngine::drawEllipse(const QRectF & rect)
// major axis * sin(rotation angle) * y-scale-factor // major axis * sin(rotation angle) * y-scale-factor
ratio = rect.height()/rect.width(); ratio = rect.height()/rect.width();
} }
dxf->writeEllipse(
*dw, dxf->writeEllipse(*dw,
DL_EllipseData(newRect.center().x(), // center X DL_EllipseData(FromPixel(newRect.center().x(), varInsunits), // center X
getSize().height() - newRect.center().y(), // center Y FromPixel(getSize().height() - newRect.center().y(), varInsunits), // center Y
0, // center Z FromPixel(0, varInsunits), // center Z
majorX, FromPixel(majorX, varInsunits),
majorY, FromPixel(majorY, varInsunits),
0, FromPixel(0, varInsunits),
ratio, FromPixel(majorY, varInsunits),
0, 6.28 // startangle and endangle of ellipse in rad 0, 6.28 // startangle and endangle of ellipse in rad
), ),
DL_Attributes("0", getPenColor(), -1, getPenStyle(), 1.0)); DL_Attributes("0", getPenColor(), -1, getPenStyle(), 1.0));
@ -366,19 +371,18 @@ void VDxfEngine::drawEllipse(const QRect & rect)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VDxfEngine::drawTextItem(const QPointF & p, const QTextItem & textItem) void VDxfEngine::drawTextItem(const QPointF & p, const QTextItem & textItem)
{ {
QPointF startPoint = matrix.map(p); const QPointF startPoint = matrix.map(p);
double rotationAngle = atan(matrix.m12()/matrix.m11()); const double rotationAngle = atan(matrix.m12()/matrix.m11());
const QFont f = textItem.font(); const QFont f = textItem.font();
int textSize = f.pixelSize() == -1 ? f.pointSize() : f.pixelSize(); const int textSize = f.pixelSize() == -1 ? f.pointSize() : f.pixelSize();
dxf->writeText( dxf->writeText(*dw,
*dw, DL_TextData(FromPixel(startPoint.x(), varInsunits),
DL_TextData(startPoint.x(), FromPixel(getSize().height() - startPoint.y(), varInsunits),
getSize().height() - startPoint.y(), FromPixel(0, varInsunits),
0, FromPixel(startPoint.x(), varInsunits),
startPoint.x(), FromPixel(getSize().height() - startPoint.y(), varInsunits),
getSize().height() - startPoint.y(), FromPixel(0, varInsunits),
0,
textSize * matrix.m11(), textSize * matrix.m11(),
1, // relative X scale factor 1, // relative X scale factor
0, // flag (0 = default, 2 = Backwards, 4 = Upside down) 0, // flag (0 = default, 2 = Backwards, 4 = Upside down)
@ -420,13 +424,13 @@ void VDxfEngine::setSize(const QSize &value)
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
int VDxfEngine::getResolution() const double VDxfEngine::getResolution() const
{ {
return resolution; return resolution;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VDxfEngine::setResolution(int value) void VDxfEngine::setResolution(double value)
{ {
Q_ASSERT(not isActive()); Q_ASSERT(not isActive());
resolution = value; resolution = value;
@ -555,3 +559,27 @@ void VDxfEngine::setInsunits(const VarInsunits &var)
Q_ASSERT(not isActive()); Q_ASSERT(not isActive());
varInsunits = var; varInsunits = var;
} }
//---------------------------------------------------------------------------------------------------------------------
#if defined(Q_CC_GNU)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-default"
#endif
double VDxfEngine::FromPixel(double pix, const VarInsunits &unit) const
{
switch (unit)
{
case VarInsunits::Millimeters:
return pix / resolution * 25.4;
case VarInsunits::Centimeters:
return pix / resolution * 25.4 / 10.0;
case VarInsunits::Inches:
return pix / resolution;
}
return 0;
}
#if defined(Q_CC_GNU)
#pragma GCC diagnostic pop
#endif

View File

@ -59,8 +59,8 @@ public:
QSize getSize() const; QSize getSize() const;
void setSize(const QSize &value); void setSize(const QSize &value);
int getResolution() const; double getResolution() const;
void setResolution(int value); void setResolution(double value);
QString getFileName() const; QString getFileName() const;
void setFileName(const QString &value); void setFileName(const QString &value);
@ -74,7 +74,7 @@ public:
private: private:
Q_DISABLE_COPY(VDxfEngine) Q_DISABLE_COPY(VDxfEngine)
QSize size; QSize size;
int resolution; double resolution;
QString fileName; QString fileName;
QMatrix matrix; QMatrix matrix;
DL_Dxf* dxf; DL_Dxf* dxf;
@ -82,6 +82,7 @@ private:
VarMeasurement varMeasurement; VarMeasurement varMeasurement;
VarInsunits varInsunits; VarInsunits varInsunits;
double FromPixel(double pix, const VarInsunits &unit) const Q_REQUIRED_RESULT;
}; };
#endif // VDXFENGINE_H #endif // VDXFENGINE_H

View File

@ -88,13 +88,13 @@ void VDxfPaintDevice::setSize(const QSize &size)
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
int VDxfPaintDevice::getResolution() const double VDxfPaintDevice::getResolution() const
{ {
return engine->getResolution(); return engine->getResolution();
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VDxfPaintDevice::setResolution(int dpi) void VDxfPaintDevice::setResolution(double dpi)
{ {
engine->setResolution(dpi); engine->setResolution(dpi);
} }
@ -123,9 +123,9 @@ int VDxfPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const
case QPaintDevice::PdmHeight: case QPaintDevice::PdmHeight:
return engine->getSize().height(); return engine->getSize().height();
case QPaintDevice::PdmDpiX: case QPaintDevice::PdmDpiX:
return engine->getResolution(); return static_cast<int>(engine->getResolution());
case QPaintDevice::PdmDpiY: case QPaintDevice::PdmDpiY:
return engine->getResolution(); return static_cast<int>(engine->getResolution());
case QPaintDevice::PdmHeightMM: case QPaintDevice::PdmHeightMM:
return qRound(engine->getSize().height() * 25.4 / engine->getResolution()); return qRound(engine->getSize().height() * 25.4 / engine->getResolution());
case QPaintDevice::PdmWidthMM: case QPaintDevice::PdmWidthMM:
@ -133,9 +133,9 @@ int VDxfPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const
case QPaintDevice::PdmNumColors: case QPaintDevice::PdmNumColors:
return static_cast<int>(0xffffffff); return static_cast<int>(0xffffffff);
case QPaintDevice::PdmPhysicalDpiX: case QPaintDevice::PdmPhysicalDpiX:
return engine->getResolution(); return static_cast<int>(engine->getResolution());
case QPaintDevice::PdmPhysicalDpiY: case QPaintDevice::PdmPhysicalDpiY:
return engine->getResolution(); return static_cast<int>(engine->getResolution());
#if QT_VERSION > QT_VERSION_CHECK(5, 0, 2) #if QT_VERSION > QT_VERSION_CHECK(5, 0, 2)
case QPaintDevice::PdmDevicePixelRatio: case QPaintDevice::PdmDevicePixelRatio:
return 1; return 1;

View File

@ -48,8 +48,8 @@ public:
QSize getSize(); QSize getSize();
void setSize(const QSize &size); void setSize(const QSize &size);
int getResolution() const; double getResolution() const;
void setResolution(int dpi); void setResolution(double dpi);
void setMeasurement(const VarMeasurement &var); void setMeasurement(const VarMeasurement &var);
void setInsunits(const VarInsunits &var); void setInsunits(const VarInsunits &var);

View File

@ -582,22 +582,22 @@ void RestoreOverrideCursor(const QString & pixmapPath);
extern const qreal PrintDPI; extern const qreal PrintDPI;
double ToPixel(double val, const Unit &unit); double ToPixel(double val, const Unit &unit) Q_REQUIRED_RESULT;
double FromPixel(double pix, const Unit &unit); double FromPixel(double pix, const Unit &unit) Q_REQUIRED_RESULT;
qreal UnitConvertor(qreal value, const Unit &from, const Unit &to); qreal UnitConvertor(qreal value, const Unit &from, const Unit &to) Q_REQUIRED_RESULT;
void CheckFactor(qreal &oldFactor, const qreal &Newfactor); void CheckFactor(qreal &oldFactor, const qreal &Newfactor);
QStringList SupportedLocales(); QStringList SupportedLocales() Q_REQUIRED_RESULT;
QStringList AllGroupNames(); QStringList AllGroupNames() Q_REQUIRED_RESULT;
QString StrippedName(const QString &fullFileName); QString StrippedName(const QString &fullFileName) Q_REQUIRED_RESULT;
QString RelativeMPath(const QString &patternPath, const QString &absoluteMPath); QString RelativeMPath(const QString &patternPath, const QString &absoluteMPath) Q_REQUIRED_RESULT;
QString AbsoluteMPath(const QString &patternPath, const QString &relativeMPath); QString AbsoluteMPath(const QString &patternPath, const QString &relativeMPath) Q_REQUIRED_RESULT;
QSharedPointer<QPrinter> DefaultPrinter(); QSharedPointer<QPrinter> DefaultPrinter() Q_REQUIRED_RESULT;
QPixmap darkenPixmap(const QPixmap &pixmap); QPixmap darkenPixmap(const QPixmap &pixmap) Q_REQUIRED_RESULT;
#endif // DEF_H #endif // DEF_H