Fixed issue #463. Wrong export to DXF format.

(grafted from a2df4c627d50fcd0225cc63d924c065063af326f)

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2016-03-27 19:00:08 +03:00
parent 0325e0a441
commit c4ca41aa83
7 changed files with 137 additions and 74 deletions

View File

@ -14,6 +14,7 @@
- [#385] Add 'Open Recent' option in Tape.exe, 'File' dropdown menu.
# Version 0.4.4
- [#463] Wrong export to DXF format.
- Fixed issue with deleting detail nodes.
- [#458] Issue with segment of curve.
- Fixed disappearing curve if start and finish points of a segment are equal.

View File

@ -693,6 +693,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
{
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i));
@ -701,7 +706,25 @@ void MainWindowsNoGUI::DxfFile(const QString &name, int i) const
VDxfPaintDevice generator;
generator.setFileName(name);
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;
painter.begin(&generator);
scenes.at(i)->render(&painter, paper->rect(), paper->rect(), Qt::IgnoreAspectRatio);
@ -709,6 +732,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
{

View File

@ -114,6 +114,14 @@ bool VDxfEngine::begin(QPaintDevice *pdev)
dw->dxfString(9, "$INSUNITS");
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");
dateTime.chop(1);// we need hundredths of a second
dw->dxfString(9, "$TDCREATE");
@ -256,7 +264,9 @@ void VDxfEngine::drawPath(const QPainterPath &path)
for (int i=0; i < polygon.count(); ++i)
{
dxf->writeVertex(*dw, DL_VertexData(polygon.at(i).x(), getSize().height() - polygon.at(i).y(), 0, 0));
dxf->writeVertex(*dw,
DL_VertexData(FromPixel(polygon.at(i).x(), varInsunits),
FromPixel(getSize().height() - polygon.at(i).y(), varInsunits), 0, 0));
}
dxf->writePolylineEnd(*dw);
@ -268,17 +278,16 @@ void VDxfEngine::drawLines(const QLineF * lines, int lineCount)
{
for (int i = 0; i < lineCount; ++i)
{
QPointF p1 = matrix.map(lines[i].p1());
QPointF p2 = matrix.map(lines[i].p2());
const QPointF p1 = matrix.map(lines[i].p1());
const QPointF p2 = matrix.map(lines[i].p2());
dxf->writeLine(
*dw,
DL_LineData(p1.x(), // start point
getSize().height() - p1.y(),
0.0,
p2.x(), // end point
getSize().height() - p2.y(),
0.0),
dxf->writeLine(*dw,
DL_LineData(FromPixel(p1.x(), varInsunits), // start point
FromPixel(getSize().height() - p1.y(), varInsunits),
FromPixel(0.0, varInsunits),
FromPixel(p2.x(), varInsunits), // end point
FromPixel(getSize().height() - p2.y(), varInsunits),
FromPixel(0.0, varInsunits)),
DL_Attributes("0", getPenColor(), -1, getPenStyle(), 1.0));
}
}
@ -305,8 +314,10 @@ void VDxfEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawM
for (int i = 0; i < pointCount; ++i)
{
QPointF p = matrix.map(points[i]);
dxf->writeVertex(*dw, DL_VertexData(p.x(), getSize().height() - p.y(), 0, 0));
const QPointF p = matrix.map(points[i]);
dxf->writeVertex(*dw,
DL_VertexData(FromPixel(p.x(), varInsunits),
FromPixel(getSize().height() - p.y(), varInsunits), 0, 0));
}
dxf->writePolylineEnd(*dw);
@ -321,8 +332,8 @@ void VDxfEngine::drawPolygon(const QPoint *points, int pointCount, QPaintEngine:
//---------------------------------------------------------------------------------------------------------------------
void VDxfEngine::drawEllipse(const QRectF & rect)
{
QRectF newRect = matrix.mapRect(rect);
double rotationAngle = atan(matrix.m12()/matrix.m11());
const QRectF newRect = matrix.mapRect(rect);
const double rotationAngle = atan(matrix.m12()/matrix.m11());
double majorX, majorY; // distanse between center and endpoint of the major axis
double ratio; // ratio of minor axis to major axis
@ -342,16 +353,16 @@ void VDxfEngine::drawEllipse(const QRectF & rect)
// major axis * sin(rotation angle) * y-scale-factor
ratio = rect.height()/rect.width();
}
dxf->writeEllipse(
*dw,
DL_EllipseData(newRect.center().x(), // center X
getSize().height() - newRect.center().y(), // center Y
0, // center Z
majorX,
majorY,
0,
ratio,
0,6.28 // startangle and endangle of ellipse in rad
dxf->writeEllipse(*dw,
DL_EllipseData(FromPixel(newRect.center().x(), varInsunits), // center X
FromPixel(getSize().height() - newRect.center().y(), varInsunits), // center Y
FromPixel(0, varInsunits), // center Z
FromPixel(majorX, varInsunits),
FromPixel(majorY, varInsunits),
FromPixel(0, varInsunits),
FromPixel(majorY, varInsunits),
0, 6.28 // startangle and endangle of ellipse in rad
),
DL_Attributes("0", getPenColor(), -1, getPenStyle(), 1.0));
}
@ -365,19 +376,18 @@ void VDxfEngine::drawEllipse(const QRect & rect)
//---------------------------------------------------------------------------------------------------------------------
void VDxfEngine::drawTextItem(const QPointF & p, const QTextItem & textItem)
{
QPointF startPoint = matrix.map(p);
double rotationAngle = atan(matrix.m12()/matrix.m11());
const QPointF startPoint = matrix.map(p);
const double rotationAngle = atan(matrix.m12()/matrix.m11());
const QFont f = textItem.font();
int textSize = f.pixelSize() == -1 ? f.pointSize() : f.pixelSize();
dxf->writeText(
*dw,
DL_TextData(startPoint.x(),
getSize().height() - startPoint.y(),
0,
startPoint.x(),
getSize().height() - startPoint.y(),
0,
const int textSize = f.pixelSize() == -1 ? f.pointSize() : f.pixelSize();
dxf->writeText(*dw,
DL_TextData(FromPixel(startPoint.x(), varInsunits),
FromPixel(getSize().height() - startPoint.y(), varInsunits),
FromPixel(0, varInsunits),
FromPixel(startPoint.x(), varInsunits),
FromPixel(getSize().height() - startPoint.y(), varInsunits),
FromPixel(0, varInsunits),
textSize * matrix.m11(),
1, // relative X scale factor
0, // flag (0 = default, 2 = Backwards, 4 = Upside down)
@ -419,13 +429,13 @@ void VDxfEngine::setSize(const QSize &value)
}
//---------------------------------------------------------------------------------------------------------------------
int VDxfEngine::getResolution() const
double VDxfEngine::getResolution() const
{
return resolution;
}
//---------------------------------------------------------------------------------------------------------------------
void VDxfEngine::setResolution(int value)
void VDxfEngine::setResolution(double value)
{
Q_ASSERT(not isActive());
resolution = value;
@ -554,3 +564,27 @@ void VDxfEngine::setInsunits(const VarInsunits &var)
Q_ASSERT(not isActive());
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;
void setSize(const QSize &value);
int getResolution() const;
void setResolution(int value);
double getResolution() const;
void setResolution(double value);
QString getFileName() const;
void setFileName(const QString &value);
@ -74,7 +74,7 @@ public:
private:
Q_DISABLE_COPY(VDxfEngine)
QSize size;
int resolution;
double resolution;
QString fileName;
QMatrix matrix;
DL_Dxf* dxf;
@ -82,6 +82,7 @@ private:
VarMeasurement varMeasurement;
VarInsunits varInsunits;
double FromPixel(double pix, const VarInsunits &unit) const Q_REQUIRED_RESULT;
};
#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();
}
//---------------------------------------------------------------------------------------------------------------------
void VDxfPaintDevice::setResolution(int dpi)
void VDxfPaintDevice::setResolution(double dpi)
{
engine->setResolution(dpi);
}
@ -123,9 +123,9 @@ int VDxfPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const
case QPaintDevice::PdmHeight:
return engine->getSize().height();
case QPaintDevice::PdmDpiX:
return engine->getResolution();
return static_cast<int>(engine->getResolution());
case QPaintDevice::PdmDpiY:
return engine->getResolution();
return static_cast<int>(engine->getResolution());
case QPaintDevice::PdmHeightMM:
return qRound(engine->getSize().height() * 25.4 / engine->getResolution());
case QPaintDevice::PdmWidthMM:
@ -133,9 +133,9 @@ int VDxfPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const
case QPaintDevice::PdmNumColors:
return static_cast<int>(0xffffffff);
case QPaintDevice::PdmPhysicalDpiX:
return engine->getResolution();
return static_cast<int>(engine->getResolution());
case QPaintDevice::PdmPhysicalDpiY:
return engine->getResolution();
return static_cast<int>(engine->getResolution());
#if QT_VERSION > QT_VERSION_CHECK(5, 0, 2)
case QPaintDevice::PdmDevicePixelRatio:
return 1;

View File

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

View File

@ -588,23 +588,23 @@ void RestoreOverrideCursor(const QString & pixmapPath);
extern const qreal PrintDPI;
double ToPixel(double val, const Unit &unit);
double FromPixel(double pix, const Unit &unit);
double ToPixel(double val, const Unit &unit) Q_REQUIRED_RESULT;
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);
QStringList SupportedLocales();
QStringList AllGroupNames();
QStringList SupportedLocales() Q_REQUIRED_RESULT;
QStringList AllGroupNames() Q_REQUIRED_RESULT;
QString StrippedName(const QString &fullFileName);
QString RelativeMPath(const QString &patternPath, const QString &absoluteMPath);
QString AbsoluteMPath(const QString &patternPath, const QString &relativeMPath);
QString StrippedName(const QString &fullFileName) Q_REQUIRED_RESULT;
QString RelativeMPath(const QString &patternPath, const QString &absoluteMPath) Q_REQUIRED_RESULT;
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;
static inline bool VFuzzyComparePossibleNulls(double p1, double p2) Q_REQUIRED_RESULT;
static inline bool VFuzzyComparePossibleNulls(double p1, double p2)