DXF - Cleaning up scaling

This commit is contained in:
David Varley
2020-09-22 12:16:32 +10:00
parent 4d7ece07ab
commit bcc5846662
3 changed files with 226 additions and 146 deletions

View File

@@ -33,7 +33,6 @@ Createdxf::Createdxf()
{
}
Createdxf::~Createdxf()
{
}
@@ -255,26 +254,6 @@ void Createdxf::dxfEnd(const QString& fileName)
}
}
/**
@brief Createdxf::drawCircle
draw circle in qt format
@param fileName
@param center
@param radius
@param colour
*/
void Createdxf::drawCircle(
const QString& fileName,
QPointF centre,
double radius,
int colour)
{
qreal x = centre.x() * xScale;
qreal y = sheetHeight - centre.y() * yScale;
qreal r = radius * xScale;
drawCircle(fileName,r,x,y,colour);
}
/**
@brief Createdxf::drawCircle
draw circle in dxf format
@@ -465,25 +444,6 @@ int Createdxf::dxfColor(QPen pen) {
return Createdxf::dxfColor(pen.color());
}
/**
@brief Createdxf::drawLine
Convenience function to draw line
@param filepath
@param line
@param colorcode
*/
void Createdxf::drawLine(
const QString &filepath,
const QLineF &line,
const int &colorcode)
{
drawLine(filepath, line.p1().x() * xScale,
sheetHeight - (line.p1().y() * yScale),
line.p2().x() * xScale,
sheetHeight - (line.p2().y() * yScale),
colorcode);
}
void Createdxf::drawArcEllipse(
const QString &file_path,
qreal x,
@@ -631,26 +591,7 @@ void Createdxf::drawArcEllipse(
}
}
/**
@brief Createdxf::drawEllipse
Conveniance function for draw ellipse
@param filepath
@param rect
@param colorcode
*/
void Createdxf::drawEllipse(
const QString &filepath,
const QRectF &rect,
const int &colorcode)
{
drawArcEllipse(
filepath,
rect.topLeft().x() * xScale,
sheetHeight - (rect.topLeft().y() * yScale),
rect.width() * xScale,
rect.height() * yScale,
0, 360, 0, 0, 0, colorcode);
}
/**
@brief Createdxf::drawRectangle
@@ -675,45 +616,11 @@ void Createdxf::drawRectangle (
drawPolyline(fileName,poly,colour,true);
}
static QRectF scaleRect(QRectF rect)
{
QRectF ro(rect.bottomLeft().x() * Createdxf::xScale,
Createdxf::sheetHeight - (rect.bottomLeft().y() * Createdxf::yScale),
rect.width() * Createdxf::xScale,
rect.height() * Createdxf::yScale);
return ro;
}
/**
@brief Createdxf::drawRectangle
Convenience function for draw rectangle
@param filepath
@param rect
@param colorcode
*/
void Createdxf::drawRectangle(
const QString &filepath,
const QRectF &rect,
const int &colorcode) {
//QPolygonF poly(scaleRect(rect));
QPolygonF poly(rect);
drawPolyline(filepath,poly,colorcode);
}
/**
@brief Createdxf::drawPolygon
Convenience function for draw polygon
@param filepath
@param poly
@param colorcode
*/
void Createdxf::drawPolygon(
const QString &filepath,
const QPolygonF &poly,
const int &colorcode)
{
drawPolyline(filepath,poly,colorcode);
}
/**
@brief Createdxf::drawArc
draw arc in dx format
@@ -788,7 +695,7 @@ void Createdxf::drawText(
double height,
double rotation,
int colour,
double xScale)
double xScaleW)
{
if (!fileName.isEmpty()) {
QFile file(fileName);
@@ -800,7 +707,7 @@ void Createdxf::drawText(
errorFileOpen.exec();
} else {
QTextStream To_Dxf(&file);
// Draw the circle
// Draw the text
To_Dxf << 0 << "\r\n";
To_Dxf << "TEXT" << "\r\n";
To_Dxf << 8 << "\r\n";
@@ -816,7 +723,7 @@ void Createdxf::drawText(
To_Dxf << 40 << "\r\n";
To_Dxf << height << "\r\n"; // Text Height
To_Dxf << 41 << "\r\n";
To_Dxf << xScale << "\r\n"; // X Scale
To_Dxf << xScaleW << "\r\n"; // X Scale
To_Dxf << 1 << "\r\n";
To_Dxf << text << "\r\n"; // Text Value
To_Dxf << 50 << "\r\n";
@@ -840,7 +747,7 @@ void Createdxf::drawTextAligned(
int hAlign,
int vAlign,
double xAlign,
double xScale,
double xScaleW,
int colour)
{
if (!fileName.isEmpty()) {
@@ -869,7 +776,7 @@ void Createdxf::drawTextAligned(
To_Dxf << 40 << "\r\n";
To_Dxf << height << "\r\n"; // Text Height
To_Dxf << 41 << "\r\n";
To_Dxf << xScale << "\r\n"; // X Scale
To_Dxf << xScaleW << "\r\n"; // X Scale
To_Dxf << 1 << "\r\n";
To_Dxf << text << "\r\n"; // Text Value
To_Dxf << 50 << "\r\n";
@@ -982,3 +889,154 @@ void Createdxf::drawPolyline(const QString &filepath,
}
}
}
/* ================================================
* Majority of calls above here are must be passed
* parameters pre=scaled to DXF units
* Calls below use Qt scaling, and re-scale them to DXF
* ================================================
*/
/**
@brief Createdxf::drawCircle
draw circle in qt format
@param fileName
@param center
@param radius
@param colour
*/
void Createdxf::drawCircle(
const QString& fileName,
QPointF centre,
double radius,
int colour)
{
qreal x = centre.x() * xScale;
qreal y = sheetHeight - centre.y() * yScale;
qreal r = radius * xScale;
drawCircle(fileName,r,x,y,colour);
}
/**
@brief Createdxf::drawLine
Convenience function to draw line
@param filepath
@param line
@param colorcode
*/
void Createdxf::drawLine(
const QString &filepath,
const QLineF &line,
const int &colorcode)
{
drawLine(filepath, line.p1().x() * xScale,
sheetHeight - (line.p1().y() * yScale),
line.p2().x() * xScale,
sheetHeight - (line.p2().y() * yScale),
colorcode);
}
/**
@brief Createdxf::drawEllipse
Conveniance function for draw ellipse
@param filepath
@param rect
@param colorcode
*/
void Createdxf::drawEllipse(
const QString &filepath,
const QRectF &rect,
const int &colorcode)
{
drawArcEllipse(
filepath,
rect.topLeft().x() * xScale,
sheetHeight - (rect.topLeft().y() * yScale),
rect.width() * xScale,
rect.height() * yScale,
0, 360, 0, 0, 0, colorcode);
}
/**
@brief Createdxf::drawRectangle
Convenience function for draw rectangle
@param filepath
@param rect
@param colorcode
*/
void Createdxf::drawRectangle(
const QString &filepath,
const QRectF &rect,
const int &colorcode) {
//QPolygonF poly(scaleRect(rect));
QPolygonF poly(rect);
drawPolyline(filepath,poly,colorcode);
}
/**
@brief Createdxf::drawPolygon
Convenience function for draw polygon
@param filepath
@param poly
@param colorcode
*/
void Createdxf::drawPolygon(
const QString &filepath,
const QPolygonF &poly,
const int &colorcode)
{
drawPolyline(filepath,poly,colorcode);
}
/**
@brief Createdxf::drawText
draw simple text in dxf format without any alignment specified
@param fileName
@param text
@param point
@param height
@param rotation
@param colour
@param xScaleW=1
*/
void Createdxf::drawText(
const QString& fileName,
const QString& text,
QPointF point,
double height,
double rotation,
int colour,
double xScaleW)
{
qreal x = point.x() * xScale;
qreal y = sheetHeight - (point.y() * yScale);
drawText(fileName,text,x,y,height * yScale,rotation,colour,xScaleW);
}
void Createdxf::drawArcEllipse(
const QString &file_path,
QRectF rect,
qreal startAngle,
qreal spanAngle,
QPointF hotspot,
qreal rotation_angle,
const int &colorcode)
{
qreal x = rect.x() * xScale;
qreal y = sheetHeight - rect.y() * yScale;
qreal w = rect.width() * xScale;
qreal h = rect.height() * yScale;
qreal hotspot_x = hotspot.x() * xScale;
qreal hotspot_y = sheetHeight - hotspot.y() * yScale;
drawArcEllipse(file_path,x,y,w,h,startAngle,spanAngle,hotspot_x,hotspot_y,rotation_angle,colorcode);
}
/*
* Utility functions
*/
static QRectF scaleRect(QRectF rect)
{
QRectF ro(rect.bottomLeft().x() * Createdxf::xScale,
Createdxf::sheetHeight - (rect.bottomLeft().y() * Createdxf::yScale),
rect.width() * Createdxf::xScale,
rect.height() * Createdxf::yScale);
return ro;
}

View File

@@ -69,6 +69,15 @@ class Createdxf
qreal rotation_angle,
const int &colorcode);
static void drawArcEllipse(
const QString &file_path,
QRectF rect,
qreal startAngle,
qreal spanAngle,
QPointF hotspot,
qreal rotation_angle,
const int &colorcode);
static void drawEllipse (const QString &filepath,
const QRectF &rect,
const int &colorcode);
@@ -110,6 +119,14 @@ class Createdxf
double,
int,
double xScale=1.0);
static void drawText(
const QString&,
const QString&,
QPointF,
double,
double,
int,
double xScale=1.0);
static void drawTextAligned(
const QString& fileName,
const QString& text,

View File

@@ -495,9 +495,6 @@ void ExportDialog::generateDxf(
qreal elem_pos_x = elmt -> pos().x();
qreal elem_pos_y = elmt -> pos().y();// - (diagram -> margin / 2);
qreal hotspot_x = (elem_pos_x) * Createdxf::xScale;
qreal hotspot_y = Createdxf::sheetHeight - (elem_pos_y) * Createdxf::yScale;
ElementPictureFactory::primitives primitives = ElementPictureFactory::instance()->getPrimitives(elmt->location());
for(QGraphicsSimpleTextItem *text : primitives.m_texts)
@@ -506,29 +503,26 @@ void ExportDialog::generateDxf(
if (fontSize < 0)
fontSize = text->font().pixelSize();
fontSize *= Createdxf::yScale;
qreal x = elem_pos_x + text->pos().x();
qreal y = elem_pos_y + text->pos().y();
x *= Createdxf::xScale;
y = Createdxf::sheetHeight - (y * Createdxf::yScale);
qreal angle = text -> rotation() + rotation_angle;
qreal angler = angle * M_PI/180;
int xdir = -sin(angler);
int ydir = -cos(angler);
QPointF transformed_point = rotation_transformed(x, y, hotspot_x, hotspot_y, rotation_angle);
QPointF transformed_point = rotation_transformed(x, y, elem_pos_x, elem_pos_y, -rotation_angle);
x = transformed_point.x() - ydir * fontSize * 0.5;
y = transformed_point.y() + xdir * fontSize * 0.5;
y = transformed_point.y() - xdir * fontSize * 0.5;
QStringList lines = text->text().split('\n');
qreal offset = fontSize * 1.6;
for (QString line : lines)
{
if (line.size() > 0 && line != "_" ) {
Createdxf::drawText(file_path, line, x, y, fontSize, 360 - angle, 0, 0.72);
Createdxf::drawText(file_path, line, QPointF(x, y), fontSize, 360 - angle, 0, 0.72);
}
x += offset * xdir;
y += offset * ydir;
y -= offset * ydir;
}
}
@@ -567,13 +561,15 @@ void ExportDialog::generateDxf(
{
if (arc.size() == 0)
continue;
qreal x = (elem_pos_x + arc.at(0)) * Createdxf::xScale;
qreal y = Createdxf::sheetHeight - (elem_pos_y + arc.at(1)) * Createdxf::yScale;
qreal w = arc.at(2) * Createdxf::xScale;
qreal h = arc.at(3) * Createdxf::yScale;
qreal x = (elem_pos_x + arc.at(0));
qreal y = (elem_pos_y + arc.at(1));
qreal w = arc.at(2);
qreal h = arc.at(3);
qreal startAngle = arc.at(4);
qreal spanAngle = arc .at(5);
Createdxf::drawArcEllipse(file_path, x, y, w, h, startAngle, spanAngle, hotspot_x, hotspot_y, rotation_angle, 0);
QRectF r(x,y,w,h);
QPointF hotspot(elem_pos_x,elem_pos_y);
Createdxf::drawArcEllipse(file_path, r, startAngle, spanAngle, hotspot, rotation_angle, 0);
}
if (epw -> exportProperties().draw_terminals) {
// Draw terminals
@@ -602,31 +598,32 @@ void ExportDialog::generateDxf(
Createdxf::drawPolyline(file_path,poly,0);
//Draw conductor text item
ConductorTextItem *textItem = cond -> textItem();
if (textItem) {
qreal fontSize = textItem -> font().pointSizeF();
if (fontSize < 0)
fontSize = textItem -> font().pixelSize();
fontSize *= Createdxf::yScale;
qreal angle = textItem -> rotation();
qreal angler = angle * M_PI/180;
int xdir = -sin(angler);
int ydir = -cos(angler);
qreal x = (cond->pos().x() + textItem -> pos().x()) * Createdxf::xScale
qreal x = (cond->pos().x() + textItem -> pos().x())
+ xdir * fontSize * 1.8
- ydir * fontSize;
qreal y = Createdxf::sheetHeight - ((cond->pos().y() + textItem -> pos().y()) * Createdxf::yScale)
+ ydir * fontSize * 1.8
+ xdir * fontSize * 0.9;
qreal y = (cond->pos().y() + textItem -> pos().y())
- ydir * fontSize * 1.8
- xdir * fontSize * 0.9;
QStringList lines = textItem->toPlainText().split('\n');
qreal offset = fontSize * 1.6;
foreach (QString line, lines) {
if (line.size() > 0 && line != "_" )
Createdxf::drawText(file_path, line, x, y, fontSize, 360-angle, 0, 0.72 );
Createdxf::drawText(file_path, line, QPointF(x, y), fontSize, 360-angle, 0, 0.72 );
x += offset * xdir;
y += offset * ydir;
y -= offset * ydir;
}
}
// Draw the junctions
QList<QPointF> junctions_list = cond->junctions();
if (!junctions_list.isEmpty()) {
@@ -641,26 +638,34 @@ void ExportDialog::generateDxf(
qreal fontSize = dti -> font().pointSizeF();
if (fontSize < 0)
fontSize = dti -> font().pixelSize();
fontSize *= Createdxf::yScale;
qreal angle = dti -> rotation();
QGraphicsItem *parent = dti->parentItem();
while (parent) {
angle += parent->rotation();
parent = parent->parentItem();
}
qreal angler = angle * M_PI/180;
int xdir = -sin(angler);
int ydir = -cos(angler);
qreal x = (dti->scenePos().x()) * Createdxf::xScale
qreal x = (dti->scenePos().x())
+ xdir * fontSize * 1.8
- ydir * fontSize;
qreal y = Createdxf::sheetHeight - (dti->scenePos().y() * Createdxf::yScale)
+ ydir * fontSize * 1.8
+ xdir * fontSize * 0.9;
qreal y = dti->scenePos().y()
- ydir * fontSize * 1.8
- xdir * fontSize * 0.9;
QStringList lines = dti -> toPlainText().split('\n');
qreal offset = fontSize * 1.6;
foreach (QString line, lines) {
if (line.size() > 0 && line != "_" )
Createdxf::drawText(file_path, line, x, y, fontSize, 360-angle, 0, 0.72 );
Createdxf::drawText(file_path, line, QPointF(x, y), fontSize, 360-angle, 0, 0.72 );
x += offset * xdir;
y += offset * ydir;
y -= offset * ydir;
}
}
Createdxf::dxfEnd(file_path);
saveReloadDiagramParameters(diagram, false);