Export to DXF Format partially added. Following entities are exported:

1. Border and title block
2. Diagram independent text items
3. Element text items
4. Conductors

Following entities are not exported right now:
1. Element components
2. Terminals
3. Maybe other things also.

Lot of debugging shall be required.

Thank you




git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@2718 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
abhishekm71
2014-01-08 16:55:16 +00:00
parent 3b0f0e1b1b
commit de10e6cf69
11 changed files with 886 additions and 1 deletions

View File

@@ -22,6 +22,7 @@
#include "diagramposition.h"
#include "qetapp.h"
#include "math.h"
#include "createdxf.h"
/**
Constructeur simple : construit une bordure en recuperant les dimensions
@@ -409,6 +410,80 @@ void BorderTitleBlock::draw(QPainter *qp, qreal x, qreal y) {
titleblock_rect_ .translate(-x, -y);
}
void BorderTitleBlock::drawDxf(int width, int height, bool keep_aspect_ratio, QString &file_path, int color) {
// Transform to DXF scale.
columns_header_height_ *= Createdxf::yScale;
rows_height_ *= Createdxf::yScale;
rows_header_width_ *= Createdxf::xScale;
columns_width_ *= Createdxf::xScale;
// dessine la case vide qui apparait des qu'il y a un entete
if (display_border_ &&
(display_columns_ ||
display_rows_)
) {
Createdxf::drawRectangle(
file_path,
double(diagram_rect_.topLeft().x()) * Createdxf::xScale,
Createdxf::sheetHeight - double(diagram_rect_.topLeft().y()) * Createdxf::yScale - columns_header_height_,
rows_header_width_,
columns_header_height_,
color
);
}
// dessine la numerotation des colonnes
if (display_border_ &&
display_columns_) {
for (int i = 1 ; i <= columns_count_ ; ++ i) {
double xCoord = diagram_rect_.topLeft().x() +
(rows_header_width_ + ((i - 1) *
columns_width_));
double yCoord = Createdxf::sheetHeight - diagram_rect_.topLeft().y() - columns_header_height_;
double recWidth = columns_width_;
double recHeight = columns_header_height_;
Createdxf::drawRectangle(file_path, xCoord, yCoord, recWidth, recHeight, color);
Createdxf::drawTextAligned(file_path, QString::number(i), xCoord,
yCoord + recHeight*0.5, recHeight*0.7, 0, 0, 1, 2, xCoord+recWidth/2, color, 0);
}
}
// dessine la numerotation des lignes
if (display_border_ && display_rows_) {
QString row_string("A");
for (int i = 1 ; i <= rows_count_ ; ++ i) {
double xCoord = diagram_rect_.topLeft().x() * Createdxf::xScale;
double yCoord = Createdxf::sheetHeight - diagram_rect_.topLeft().y()*Createdxf::yScale
- (columns_header_height_ + ((i - 1) * rows_height_))
- rows_height_;
double recWidth = rows_header_width_;
double recHeight = rows_height_;
Createdxf::drawRectangle(file_path, xCoord, yCoord, recWidth, recHeight, color);
Createdxf::drawTextAligned(file_path, row_string, xCoord,
yCoord + recHeight*0.5, recWidth*0.7, 0, 0, 1, 2, xCoord+recWidth/2, color, 0);
row_string = incrementLetters(row_string);
}
}
// render the titleblock, using the TitleBlockTemplate object
if (display_titleblock_) {
//qp -> translate(titleblock_rect_.topLeft());
titleblock_template_renderer_ -> renderDxf(titleblock_rect_, titleblock_rect_.width(), file_path, color);
//qp -> translate(-titleblock_rect_.topLeft());
}
// Transform back to QET scale
columns_header_height_ /= Createdxf::yScale;
rows_height_ /= Createdxf::yScale;
rows_header_width_ /= Createdxf::xScale;
columns_width_ /= Createdxf::xScale;
}
/**
Ajoute une colonne.
*/

View File

@@ -50,6 +50,7 @@ class BorderTitleBlock : public QObject {
static qreal minRowsHeight();
void draw(QPainter *, qreal = 0.0, qreal = 0.0);
void drawDxf(int, int, bool, QString &, int);
// methods to get dimensions
// columns

534
sources/createdxf.cpp Normal file
View File

@@ -0,0 +1,534 @@
#include "createdxf.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QString>
const double Createdxf::sheetWidth = 4000;
const double Createdxf::sheetHeight = 2700;
double Createdxf::xScale = 1;
double Createdxf::yScale = 1;
Createdxf::Createdxf()
{
}
Createdxf::~Createdxf()
{
}
/* Header section of every DXF file.*/
void Createdxf::dxfBegin (QString fileName)
{
// Creation of an output stream object in text mode.
// Header section of every dxf file.
if (!fileName.isEmpty()) {
QFile file(fileName);
if (!file.open(QFile::WriteOnly)) {
// error message
QMessageBox errorFileOpen;
errorFileOpen.setIcon(QMessageBox::Warning);
errorFileOpen.setText("Error: "+fileName+" Could Not be Opened.");
errorFileOpen.setInformativeText("Close all Files and Try Again.");
errorFileOpen.exec();
exit(0);
} else {
QTextStream To_Dxf(&file);
To_Dxf << 999 << "\r\n";
To_Dxf << "QET" << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << "SECTION" << "\r\n";
To_Dxf << 2 << "\r\n";
To_Dxf << "HEADER" << "\r\n";
To_Dxf << 9 << "\r\n";
To_Dxf << "$ACADVER" << "\r\n";
To_Dxf << 1 << "\r\n";
To_Dxf << "AC1006" << "\r\n";
To_Dxf << 9 << "\r\n";
To_Dxf << "$INSBASE" << "\r\n";
To_Dxf << 10 << "\r\n";
To_Dxf << "0.0" << "\r\n";
To_Dxf << 20 << "\r\n";
To_Dxf << "0.0" << "\r\n";
To_Dxf << 30 << "\r\n";
To_Dxf << "0.0" << "\r\n";
To_Dxf << 9 << "\r\n";
To_Dxf << "$EXTMIN" << "\r\n";
To_Dxf << 10 << "\r\n";
To_Dxf << "0.0" << "\r\n";
To_Dxf << 20 << "\r\n";
To_Dxf << "0.0" << "\r\n";
To_Dxf << 9 << "\r\n";
To_Dxf << "$EXTMAX" << "\r\n";
To_Dxf << 10 << "\r\n";
To_Dxf << "4000.0" << "\r\n";
To_Dxf << 20 << "\r\n";
To_Dxf << "4000.0" << "\r\n";
To_Dxf << 9 << "\r\n";
To_Dxf << "$LIMMIN" << "\r\n";
To_Dxf << 10 << "\r\n";
To_Dxf << "0.0" << "\r\n";
To_Dxf << 20 << "\r\n";
To_Dxf << "0.0" << "\r\n";
To_Dxf << 9 << "\r\n";
To_Dxf << "$LIMMAX" << "\r\n";
To_Dxf << 10 << "\r\n";
To_Dxf << "4000.0" << "\r\n";
To_Dxf << 20 << "\r\n";
To_Dxf << "4000.0" << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << "ENDSEC" << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << "SECTION" << "\r\n";
To_Dxf << 2 << "\r\n";
To_Dxf << "TABLES" << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << "TABLE" << "\r\n";
To_Dxf << 2 << "\r\n";
To_Dxf << "VPORT" << "\r\n";
To_Dxf << 70 << "\r\n";
To_Dxf << 1 << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << "VPORT" << "\r\n";
To_Dxf << 2 << "\r\n";
To_Dxf << "*ACTIVE" << "\r\n";
To_Dxf << 70 << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << 10 << "\r\n";
To_Dxf << 0.0 << "\r\n";
To_Dxf << 20 << "\r\n";
To_Dxf << 0.0 << "\r\n";
To_Dxf << 11 << "\r\n";
To_Dxf << 1.0 << "\r\n";
To_Dxf << 21 << "\r\n";
To_Dxf << 1.0 << "\r\n";
To_Dxf << 12 << "\r\n";
To_Dxf << 2000 << "\r\n";
To_Dxf << 22 << "\r\n";
To_Dxf << 1350 << "\r\n";
To_Dxf << 13 << "\r\n";
To_Dxf << 0.0 << "\r\n";
To_Dxf << 23 << "\r\n";
To_Dxf << 0.0 << "\r\n";
To_Dxf << 14 << "\r\n";
To_Dxf << 1.0 << "\r\n";
To_Dxf << 24 << "\r\n";
To_Dxf << 1.0 << "\r\n";
To_Dxf << 15 << "\r\n";
To_Dxf << 0.0 << "\r\n";
To_Dxf << 25 << "\r\n";
To_Dxf << 0.0 << "\r\n";
To_Dxf << 16 << "\r\n";
To_Dxf << 0.0 << "\r\n";
To_Dxf << 26 << "\r\n";
To_Dxf << 0.0 << "\r\n";
To_Dxf << 36 << "\r\n";
To_Dxf << 1.0 << "\r\n";
To_Dxf << 17 << "\r\n";
To_Dxf << 0.0 << "\r\n";
To_Dxf << 27 << "\r\n";
To_Dxf << 0.0 << "\r\n";
To_Dxf << 37 << "\r\n";
To_Dxf << 0.0 << "\r\n";
To_Dxf << 40 << "\r\n";
To_Dxf << 2732.5 << "\r\n";
To_Dxf << 41 << "\r\n";
To_Dxf << 2.558 << "\r\n";
To_Dxf << 42 << "\r\n";
To_Dxf << 50.0 << "\r\n";
To_Dxf << 43 << "\r\n";
To_Dxf << 0.0 << "\r\n";
To_Dxf << 44 << "\r\n";
To_Dxf << 0.0 << "\r\n";
To_Dxf << 50 << "\r\n";
To_Dxf << 0.0 << "\r\n";
To_Dxf << 51 << "\r\n";
To_Dxf << 0.0 << "\r\n";
To_Dxf << 71 << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << 72 << "\r\n";
To_Dxf << 100 << "\r\n";
To_Dxf << 73 << "\r\n";
To_Dxf << 1 << "\r\n";
To_Dxf << 74 << "\r\n";
To_Dxf << 1 << "\r\n";
To_Dxf << 75 << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << 76 << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << 77 << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << 78 << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << "ENDTAB" << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << "TABLE" << "\r\n";
To_Dxf << 2 << "\r\n";
To_Dxf << "LTYPE" << "\r\n";
To_Dxf << 70 << "\r\n";
To_Dxf << 1 << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << "LTYPE" << "\r\n";
To_Dxf << 2 << "\r\n";
To_Dxf << "CONTINUOUS" << "\r\n";
To_Dxf << 70 << "\r\n";
To_Dxf << 64 << "\r\n";
To_Dxf << 3 << "\r\n";
To_Dxf << "Solid Line" << "\r\n";
To_Dxf << 72 << "\r\n";
To_Dxf << 65 << "\r\n";
To_Dxf << 73 << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << 40 << "\r\n";
To_Dxf << 0.00 << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << "ENDTAB" << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << "ENDSEC" << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << "SECTION" << "\r\n";
To_Dxf << 2 << "\r\n";
To_Dxf << "BLOCKS" << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << "ENDSEC" << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << "SECTION" << "\r\n";
To_Dxf << 2 << "\r\n";
To_Dxf << "ENTITIES" << "\r\n";
file.close();
}
}
}
/* End Section of every DXF File*/
void Createdxf::dxfEnd (QString fileName)
{
// Creation of an output stream object in text mode.
if (!fileName.isEmpty()) {
QFile file(fileName);
if (!file.open(QFile::Append)) {
// error message
QMessageBox errorFileOpen;
errorFileOpen.setText("Error: File "+fileName+" was not written correctly.");
errorFileOpen.setInformativeText("Close all Files and Re-Run");
errorFileOpen.exec();
} else {
QTextStream To_Dxf(&file);
To_Dxf << 0 << "\r\n";
To_Dxf << "ENDSEC" << "\r\n";
To_Dxf << 0 << "\r\n";
To_Dxf << "EOF";
file.close();
}
}
}
/* draw circle in dxf format*/
void Createdxf::drawCircle (QString fileName, double radius, double x, double y, int colour)
{
if (!fileName.isEmpty()) {
QFile file(fileName);
if (!file.open(QFile::Append)) {
// error message
QMessageBox errorFileOpen;
errorFileOpen.setText("Error: File "+fileName+" was not written correctly.");
errorFileOpen.setInformativeText("Close all Files and Re-Run");
errorFileOpen.exec();
} else {
QTextStream To_Dxf(&file);
// Draw the circle
To_Dxf << 0 << "\r\n";
To_Dxf << "CIRCLE" << "\r\n";
To_Dxf << 8 << "\r\n";
To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
To_Dxf << 62 << "\r\n";
To_Dxf << colour << "\r\n"; // Colour Code
To_Dxf << 10 << "\r\n"; // XYZ is the Center point of circle
To_Dxf << x << "\r\n"; // X in UCS (User Coordinate System)coordinates
To_Dxf << 20 << "\r\n";
To_Dxf << y << "\r\n"; // Y in UCS (User Coordinate System)coordinates
To_Dxf << 30 << "\r\n";
To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
To_Dxf << 40 << "\r\n";
To_Dxf << radius << "\r\n"; // radius of circle
file.close();
}
}
}
/* draw line in DXF Format*/
void Createdxf::drawLine (QString fileName, double x1, double y1, double x2, double y2, int colour)
{
if (!fileName.isEmpty()) {
QFile file(fileName);
if (!file.open(QFile::Append)) {
// error message
QMessageBox errorFileOpen;
errorFileOpen.setText("Error: File "+fileName+" was not written correctly.");
errorFileOpen.setInformativeText("Close all Files and Re-Run");
errorFileOpen.exec();
} else {
QTextStream To_Dxf(&file);
// Draw the Line
To_Dxf << 0 << "\r\n";
To_Dxf << "LINE" << "\r\n";
To_Dxf << 8 << "\r\n";
To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
To_Dxf << 62 << "\r\n";
To_Dxf << colour << "\r\n"; // Colour Code
To_Dxf << 10 << "\r\n";
To_Dxf << x1 << "\r\n"; // X in UCS (User Coordinate System)coordinates
To_Dxf << 20 << "\r\n";
To_Dxf << y1 << "\r\n"; // Y in UCS (User Coordinate System)coordinates
To_Dxf << 30 << "\r\n";
To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
To_Dxf << 11 << "\r\n";
To_Dxf << x2 << "\r\n"; // X in UCS (User Coordinate System)coordinates
To_Dxf << 21 << "\r\n";
To_Dxf << y2 << "\r\n"; // Y in UCS (User Coordinate System)coordinates
To_Dxf << 31 << "\r\n";
To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
file.close();
}
}
}
/* draw rectangle in dxf format */
void Createdxf::drawRectangle (QString fileName, double x1, double y1, double width, double height, int colour)
{
if (!fileName.isEmpty()) {
QFile file(fileName);
if (!file.open(QFile::Append)) {
// error message
QMessageBox errorFileOpen;
errorFileOpen.setText("Error: File "+fileName+" was not written correctly.");
errorFileOpen.setInformativeText("Close all Files and Re-Run");
errorFileOpen.exec();
} else {
QTextStream To_Dxf(&file);
// Draw the Rectangle
To_Dxf << 0 << "\r\n";
To_Dxf << "LINE" << "\r\n";
To_Dxf << 8 << "\r\n";
To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
To_Dxf << 62 << "\r\n";
To_Dxf << colour << "\r\n"; // Colour Code
To_Dxf << 10 << "\r\n";
To_Dxf << x1 << "\r\n"; // X in UCS (User Coordinate System)coordinates
To_Dxf << 20 << "\r\n";
To_Dxf << y1 << "\r\n"; // Y in UCS (User Coordinate System)coordinates
To_Dxf << 30 << "\r\n";
To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
To_Dxf << 11 << "\r\n";
To_Dxf << x1+width << "\r\n"; // X in UCS (User Coordinate System)coordinates
To_Dxf << 21 << "\r\n";
To_Dxf << y1 << "\r\n"; // Y in UCS (User Coordinate System)coordinates
To_Dxf << 31 << "\r\n";
To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
To_Dxf << 0 << "\r\n";
To_Dxf << "LINE" << "\r\n";
To_Dxf << 8 << "\r\n";
To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
To_Dxf << 62 << "\r\n";
To_Dxf << colour << "\r\n"; // Colour Code
To_Dxf << 10 << "\r\n";
To_Dxf << x1 << "\r\n"; // X in UCS (User Coordinate System)coordinates
To_Dxf << 20 << "\r\n";
To_Dxf << y1 << "\r\n"; // Y in UCS (User Coordinate System)coordinates
To_Dxf << 30 << "\r\n";
To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
To_Dxf << 11 << "\r\n";
To_Dxf << x1 << "\r\n"; // X in UCS (User Coordinate System)coordinates
To_Dxf << 21 << "\r\n";
To_Dxf << y1+height << "\r\n"; // Y in UCS (User Coordinate System)coordinates
To_Dxf << 31 << "\r\n";
To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
To_Dxf << 0 << "\r\n";
To_Dxf << "LINE" << "\r\n";
To_Dxf << 8 << "\r\n";
To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
To_Dxf << 62 << "\r\n";
To_Dxf << colour << "\r\n"; // Colour Code
To_Dxf << 10 << "\r\n";
To_Dxf << x1+width << "\r\n"; // X in UCS (User Coordinate System)coordinates
To_Dxf << 20 << "\r\n";
To_Dxf << y1 << "\r\n"; // Y in UCS (User Coordinate System)coordinates
To_Dxf << 30 << "\r\n";
To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
To_Dxf << 11 << "\r\n";
To_Dxf << x1+width << "\r\n"; // X in UCS (User Coordinate System)coordinates
To_Dxf << 21 << "\r\n";
To_Dxf << y1+height << "\r\n"; // Y in UCS (User Coordinate System)coordinates
To_Dxf << 31 << "\r\n";
To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
To_Dxf << 0 << "\r\n";
To_Dxf << "LINE" << "\r\n";
To_Dxf << 8 << "\r\n";
To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
To_Dxf << 62 << "\r\n";
To_Dxf << colour << "\r\n"; // Colour Code
To_Dxf << 10 << "\r\n";
To_Dxf << x1 << "\r\n"; // X in UCS (User Coordinate System)coordinates
To_Dxf << 20 << "\r\n";
To_Dxf << y1+height << "\r\n"; // Y in UCS (User Coordinate System)coordinates
To_Dxf << 30 << "\r\n";
To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
To_Dxf << 11 << "\r\n";
To_Dxf << x1+width << "\r\n"; // X in UCS (User Coordinate System)coordinates
To_Dxf << 21 << "\r\n";
To_Dxf << y1+height << "\r\n"; // Y in UCS (User Coordinate System)coordinates
To_Dxf << 31 << "\r\n";
To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
file.close();
}
}
}
/* draw arc in dx format */
void Createdxf::drawArc(QString fileName,double x,double y,double rad,double startAngle,double endAngle,int color)
{
if (!fileName.isEmpty()) {
QFile file(fileName);
if (!file.open(QFile::Append)) {
// error message
QMessageBox errorFileOpen;
errorFileOpen.setText("Error: File "+fileName+" was not written correctly.");
errorFileOpen.setInformativeText("Close all Files and Re-Run");
errorFileOpen.exec();
} else {
QTextStream To_Dxf(&file);
// Draw the arc
To_Dxf << 0 << "\r\n";
To_Dxf << "ARC" << "\r\n";
To_Dxf << 8 << "\r\n";
To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
To_Dxf << 62 << "\r\n";
To_Dxf << color << "\r\n"; // Colour Code
To_Dxf << 10 << "\r\n"; // XYZ is the Center point of circle
To_Dxf << x << "\r\n"; // X in UCS (User Coordinate System)coordinates
To_Dxf << 20 << "\r\n";
To_Dxf << y << "\r\n"; // Y in UCS (User Coordinate System)coordinates
To_Dxf << 30 << "\r\n";
To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
To_Dxf << 40 << "\r\n";
To_Dxf << rad << "\r\n"; // radius of arc
To_Dxf << 50 << "\r\n";
To_Dxf << startAngle<< "\r\n"; // start angle
To_Dxf << 51 << "\r\n";
To_Dxf << endAngle << "\r\n"; // end angle
file.close();
}
}
}
/* draw simple text in dxf format without any alignment specified */
void Createdxf::drawText(QString fileName, QString text,double x, double y, double height, double rotation, int colour)
{
if (!fileName.isEmpty()) {
QFile file(fileName);
if (!file.open(QFile::Append)) {
// error message
QMessageBox errorFileOpen;
errorFileOpen.setText("Error: File "+fileName+" was not written correctly.");
errorFileOpen.setInformativeText("Close all Files and Re-Run");
errorFileOpen.exec();
} else {
QTextStream To_Dxf(&file);
// Draw the circle
To_Dxf << 0 << "\r\n";
To_Dxf << "TEXT" << "\r\n";
To_Dxf << 8 << "\r\n";
To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
To_Dxf << 62 << "\r\n";
To_Dxf << colour << "\r\n"; // Colour Code
To_Dxf << 10 << "\r\n"; // XYZ
To_Dxf << x << "\r\n"; // X in UCS (User Coordinate System)coordinates
To_Dxf << 20 << "\r\n";
To_Dxf << y << "\r\n"; // Y in UCS (User Coordinate System)coordinates
To_Dxf << 30 << "\r\n";
To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
To_Dxf << 40 << "\r\n";
To_Dxf << height << "\r\n"; // Text Height
To_Dxf << 1 << "\r\n";
To_Dxf << text << "\r\n"; // Text Value
To_Dxf << 50 << "\r\n";
To_Dxf << rotation << "\r\n"; // Text Rotation
file.close();
}
}
}
/* draw aligned text in DXF Format */
void Createdxf::drawTextAligned(QString fileName, QString text,double x, double y, double height, double rotation, double oblique,int hAlign, int vAlign, double xAlign,int colour,
float scale)
{
if (!fileName.isEmpty()) {
QFile file(fileName);
if (!file.open(QFile::Append)) {
// error message
QMessageBox errorFileOpen;
errorFileOpen.setText("Error: File "+fileName+" was not written correctly.");
errorFileOpen.setInformativeText("Close all Files and Re-Run");
errorFileOpen.exec();
} else {
QTextStream To_Dxf(&file);
// Draw the circle
To_Dxf << 0 << "\r\n";
To_Dxf << "TEXT" << "\r\n";
To_Dxf << 8 << "\r\n";
To_Dxf << 0 << "\r\n"; // Layer number (default layer in autocad)
To_Dxf << 62 << "\r\n";
To_Dxf << colour << "\r\n"; // Colour Code
To_Dxf << 10 << "\r\n"; // XYZ
To_Dxf << x << "\r\n"; // X in UCS (User Coordinate System)coordinates
To_Dxf << 20 << "\r\n";
To_Dxf << y << "\r\n"; // Y in UCS (User Coordinate System)coordinates
To_Dxf << 30 << "\r\n";
To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
To_Dxf << 40 << "\r\n";
To_Dxf << height << "\r\n"; // Text Height
To_Dxf << 1 << "\r\n";
To_Dxf << text << "\r\n"; // Text Value
To_Dxf << 50 << "\r\n";
To_Dxf << rotation << "\r\n"; // Text Rotation
To_Dxf << 51 << "\r\n";
To_Dxf << oblique << "\r\n"; // Text Obliqueness
To_Dxf << 72 << "\r\n";
// If "Fit to width", then check if width of text < width specified then change it "center align"
if (hAlign == 5) {
int xDiff = xAlign - x;
if (text.length() < xDiff/height) {
hAlign = 1;
xAlign = (x+xAlign) / 2;
}
}
To_Dxf << hAlign << "\r\n"; // Text Horizontal Alignment
To_Dxf << 73 << "\r\n";
To_Dxf << vAlign << "\r\n"; // Text Vertical Alignment
if ((hAlign) || (vAlign)) { // Enter Second Point
To_Dxf << 11 << "\r\n"; // XYZ
To_Dxf << xAlign << "\r\n"; // X in UCS (User Coordinate System)coordinates
To_Dxf << 21 << "\r\n";
To_Dxf << y << "\r\n"; // Y in UCS (User Coordinate System)coordinates
To_Dxf << 31 << "\r\n";
To_Dxf << 0.0 << "\r\n"; // Z in UCS (User Coordinate System)coordinates
}
file.close();
}
}
}

32
sources/createdxf.h Normal file
View File

@@ -0,0 +1,32 @@
#ifndef CREATEDXF_H
#define CREATEDXF_H
#include <QString>
#include <QtCore>
#include <QtGui>
/* This class exports the project to DXF Format */
class Createdxf
{
public:
Createdxf();
~Createdxf();
static void dxfBegin (QString);
static void dxfEnd(QString);
// you can add more functions to create more drawings.
static void drawCircle(QString,double,double,double,int);
static void drawArc(QString,double x,double y,double rad,double startAngle,double endAngle,int color);
static void drawDonut(QString,double,double,double,int);
static void drawRectangle(QString,double,double,double,double,int);
static void drawLine(QString,double,double,double,double,int);
static void drawText(QString,QString,double,double,double,double,int);
static void drawTextAligned(QString fileName, QString text,double x, double y, double height, double rotation, double oblique,int hAlign, int vAlign, double xAlign, int colour,
float scale = 0);
static const double sheetWidth;
static const double sheetHeight;
static double xScale;
static double yScale;
};
#endif // CREATEDXF_H

View File

@@ -22,6 +22,16 @@
#include "qetmessagebox.h"
#include "exportpropertieswidget.h"
#include "qetdiagrameditor.h"
#include "createdxf.h"
#include "conductorsegment.h"
#include "qetgraphicsitem/conductor.h"
#include "qetgraphicsitem/diagramtextitem.h"
#include "qetgraphicsitem/conductortextitem.h"
#include "qetgraphicsitem/customelement.h"
#include "qetgraphicsitem/elementtextitem.h"
#include "qetgraphicsitem/ghostelement.h"
#include "qetgraphicsitem/independenttextitem.h"
#include "qetgraphicsitem/diagramimageitem.h"
/**
Constructeur
@@ -350,6 +360,108 @@ void ExportDialog::generateSvg(Diagram *diagram, int width, int height, bool kee
saveReloadDiagramParameters(diagram, false);
}
/**
Exporte le schema en DXF
@param diagram Schema a exporter en DXF
@param width Largeur de l'export DXF
@param height Hauteur de l'export DXF
@param keep_aspect_ratio True pour conserver le ratio, false sinon
@param io_device Peripherique de sortie pour le code DXF (souvent : un fichier)
*/
void ExportDialog::generateDxf(Diagram *diagram, int width, int height, bool keep_aspect_ratio, QString &file_path) {
project_ -> setFilePath(file_path);
width -= 2*Diagram::margin;
height -= 2*Diagram::margin;
Createdxf::xScale = Createdxf::sheetWidth / double(width);
Createdxf::yScale = Createdxf::sheetHeight / double(height);
Createdxf::dxfBegin(file_path);
//Add project elements (lines, rectangles, circles, texts) to dxf file
Createdxf::drawRectangle(file_path, 0, 0, double(width)*Createdxf::xScale, double(height)*Createdxf::yScale, 0);
diagram -> border_and_titleblock.drawDxf(width, height, keep_aspect_ratio, file_path, 0);
// Build the lists of elements.
QList<Element *> list_elements;
QList<Conductor *> list_conductors;
QList<DiagramTextItem *> list_texts;
QList<DiagramImageItem *> list_images;
// Determine les elements a "XMLiser"
foreach(QGraphicsItem *qgi, diagram -> items()) {
if (Element *elmt = qgraphicsitem_cast<Element *>(qgi)) {
list_elements << elmt;
} else if (Conductor *f = qgraphicsitem_cast<Conductor *>(qgi)) {
list_conductors << f;
} else if (IndependentTextItem *iti = qgraphicsitem_cast<IndependentTextItem *>(qgi)) {
list_texts << iti;
} else if (DiagramImageItem *dii = qgraphicsitem_cast<DiagramImageItem *>(qgi)) {
list_images << dii;
}
}
//Draw elements
foreach(Element *elmt, list_elements) {
qreal hot_spot_x = elmt -> pos().x();// + elmt -> hotspot().x();
qreal hot_spot_y = elmt -> pos().y();// + elmt -> hotspot().y();
QList<ElementTextItem *> elmt_text = elmt -> texts();
foreach(ElementTextItem *dti, elmt_text) {
qreal fontSize = dti -> font().pointSizeF();
if (fontSize < 0)
fontSize = dti -> font().pixelSize();
fontSize *= Createdxf::yScale;
qreal x = hot_spot_x + dti -> pos().x();
x *= Createdxf::xScale;
qreal y = hot_spot_y + dti -> pos().y();
y = Createdxf::sheetHeight - (y * Createdxf::yScale) - fontSize*1.05;
Createdxf::drawText(file_path, dti -> toPlainText(), x, y, fontSize, dti -> rotationAngle(), 0 );
}
}
//Draw conductors
foreach(Conductor *cond, list_conductors) {
foreach(ConductorSegment *segment, cond -> segmentsList()) {
qreal x1 = (segment -> firstPoint().x()) * Createdxf::xScale;
qreal y1 = Createdxf::sheetHeight - (segment -> firstPoint().y() * Createdxf::yScale);
qreal x2 = (segment -> secondPoint().x()) * Createdxf::xScale;
qreal y2 = Createdxf::sheetHeight - (segment -> secondPoint().y() * Createdxf::yScale);
Createdxf::drawLine(file_path, x1, y1, x2, y2, 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 x = (textItem -> pos().x()) * Createdxf::xScale;
qreal y = Createdxf::sheetHeight - (textItem -> pos().y() * Createdxf::yScale) - fontSize*1.05;
Createdxf::drawText(file_path, textItem -> toPlainText(), x, y, fontSize, textItem -> rotationAngle(), 0 );
}
}
//Draw text items
foreach(DiagramTextItem *dti, list_texts) {
qreal fontSize = dti -> font().pointSizeF();
if (fontSize < 0)
fontSize = dti -> font().pixelSize();
fontSize *= Createdxf::yScale;
qreal x = (dti -> pos().x()) * Createdxf::xScale;
qreal y = Createdxf::sheetHeight - (dti -> pos().y() * Createdxf::yScale) - fontSize*1.05;
Createdxf::drawText(file_path, dti -> toPlainText(), x, y, fontSize, dti -> rotationAngle(), 0 );
}
Createdxf::dxfEnd(file_path);
}
/**
Slot effectuant les exports apres la validation du dialogue.
*/
@@ -456,6 +568,14 @@ void ExportDialog::exportDiagram(ExportDiagramLine *diagram_line) {
diagram_line -> keep_ratio -> isChecked(),
target_file
);
} else if (format_acronym == "DXF") {
generateDxf(
diagram_line -> diagram,
diagram_line -> width -> value(),
diagram_line -> height -> value(),
diagram_line -> keep_ratio -> isChecked(),
diagram_path
);
} else {
QImage image = generateImage(
diagram_line -> diagram,

View File

@@ -84,6 +84,7 @@ class ExportDialog : public QDialog {
QWidget *initDiagramsListPart();
void saveReloadDiagramParameters(Diagram *, bool = true);
void generateSvg(Diagram *, int, int, bool, QIODevice &);
void generateDxf(Diagram *, int, int, bool, QString &);
QImage generateImage(Diagram *, int, int, bool);
void exportDiagram(ExportDiagramLine *);
qreal diagramRatio(Diagram *);

View File

@@ -143,6 +143,7 @@ void ExportPropertiesWidget::build() {
format -> addItem(tr("JPEG (*.jpg)"), "JPG");
format -> addItem(tr("Bitmap (*.bmp)"), "BMP");
format -> addItem(tr("SVG (*.svg)"), "SVG");
format -> addItem(tr("DXF (*.dxf)"), "DXF");
hboxLayout1 -> addStretch();
vboxLayout -> addLayout(hboxLayout1);

View File

@@ -19,6 +19,7 @@
#include "qet.h"
#include "qetapp.h"
#include "nameslist.h"
#include "createdxf.h"
// uncomment the line below to get more debug information
//#define TITLEBLOCK_TEMPLATE_DEBUG
@@ -1257,6 +1258,55 @@ void TitleBlockTemplate::render(QPainter &painter, const DiagramContext &diagram
}
}
/**
Render the titleblock in DXF.
@param diagram_context Diagram context to use to generate the titleblock strings
@param titleblock_width Width of the titleblock to render
*/
void TitleBlockTemplate::renderDxf(QRectF &title_block_rect, const DiagramContext &diagram_context,
int titleblock_width, QString &file_path, int color) const {
QList<int> widths = columnsWidth(titleblock_width);
// draw the titleblock border
double xCoord = title_block_rect.topLeft().x();
double yCoord = Createdxf::sheetHeight - title_block_rect.bottomLeft().y()*Createdxf::yScale;
double recWidth = title_block_rect.width() * Createdxf::xScale;
double recHeight = title_block_rect.height() * Createdxf::yScale;
Createdxf::drawRectangle(file_path, xCoord, yCoord, recWidth, recHeight, color);
// run through each individual cell
for (int j = 0 ; j < rows_heights_.count() ; ++ j) {
for (int i = 0 ; i < columns_width_.count() ; ++ i) {
if (cells_[i][j] -> spanner_cell || cells_[i][j] -> cell_type == TitleBlockCell::EmptyCell) continue;
// calculate the border rect of the current cell
double x = lengthRange(0, cells_[i][j] -> num_col, widths);
double y = lengthRange(0, cells_[i][j] -> num_row, rows_heights_);
int row_span = 0, col_span = 0;
if (cells_[i][j] -> span_state != TitleBlockCell::Disabled) {
row_span = cells_[i][j] -> applied_row_span;
col_span = cells_[i][j] -> applied_col_span;
}
double w = lengthRange(cells_[i][j] -> num_col, cells_[i][j] -> num_col + 1 + col_span, widths);
double h = lengthRange(cells_[i][j] -> num_row, cells_[i][j] -> num_row + 1 + row_span, rows_heights_);
x = xCoord + x*Createdxf::xScale;
h *= Createdxf::yScale;
y = yCoord + recHeight - h - y*Createdxf::yScale;
w *= Createdxf::xScale;
Createdxf::drawRectangle(file_path, x, y, w, h, color);
if (cells_[i][j] -> type() == TitleBlockCell::TextCell) {
QString final_text = finalTextForCell(*cells_[i][j], diagram_context);
renderTextCellDxf(file_path, final_text, *cells_[i][j], x, y, w, h, color);
}
}
}
}
/**
Render a titleblock cell.
@param painter Painter to use to render the titleblock
@@ -1295,6 +1345,9 @@ void TitleBlockTemplate::renderCell(QPainter &painter, const TitleBlockCell &cel
painter.drawRect(cell_rect);
}
/**
@param cell A cell from this template
@param diagram_context Diagram context to use to generate the final text for the given cell
@@ -1389,6 +1442,65 @@ void TitleBlockTemplate::renderTextCell(QPainter &painter, const QString &text,
painter.drawText(cell_rect, cell.alignment, text);
}
void TitleBlockTemplate::renderTextCellDxf(QString &file_path, const QString &text,
const TitleBlockCell &cell,
qreal x, qreal y, qreal w, qreal h, int color) const {
if (text.isEmpty()) return;
QFont text_font = TitleBlockTemplate::fontForCell(cell);
double textHeight = text_font.pointSizeF();
if (textHeight < 0)
textHeight = text_font.pixelSize();
qreal x2 = x + w;
int vAlign = 0;
int hAlign = 0;
bool hALigned = false;
if ( cell.alignment & Qt::AlignRight ) {
hAlign = 2;
hALigned = true;
} else if ( cell.alignment & Qt::AlignHCenter ) {
hAlign = 1;
hALigned = true;
x2 = x + w/2;
} else if ( cell.alignment & Qt::AlignJustify ) {
hAlign = 5;
hALigned = true;
}
if ( cell.alignment & Qt::AlignTop ) {
vAlign = 3;
y += h - textHeight*Createdxf::yScale;
if (!hALigned)
x2 = x;
} else if ( cell.alignment & Qt::AlignVCenter ) {
vAlign = 2;
y += h/2;
if (!hALigned)
x2 = x;
} else if ( cell.alignment & Qt::AlignBottom ) {}
//painter.setFont(text_font);
if (cell.hadjust) {
QFontMetricsF font_metrics(text_font);
QRectF font_rect = font_metrics.boundingRect(QRect(-10000, -10000, 10000, 10000), cell.alignment, text);
if (font_rect.width()*Createdxf::xScale > w) {
qreal ratio = qreal(w) / qreal(font_rect.width()*Createdxf::xScale);
textHeight *= ratio;
}
}
Createdxf::drawTextAligned(file_path, text, x,
y, textHeight*Createdxf::yScale, 0, 0, hAlign, vAlign, x2, color, 0);
}
/**
Set the spanner_cell attribute of every cell to 0.
*/

View File

@@ -97,6 +97,7 @@ class TitleBlockTemplate : public QObject {
QPixmap bitmapLogo(const QString &) const;
void render(QPainter &, const DiagramContext &, int) const;
void renderDxf(QRectF &, const DiagramContext &, int, QString &, int) const;
void renderCell(QPainter &, const TitleBlockCell &, const DiagramContext &, const QRect &) const;
QString toString() const;
void applyCellSpans();
@@ -133,6 +134,7 @@ class TitleBlockTemplate : public QObject {
QString finalTextForCell(const TitleBlockCell &, const DiagramContext &) const;
QString interpreteVariables(const QString &, const DiagramContext &) const;
void renderTextCell(QPainter &, const QString &, const TitleBlockCell &, const QRectF &) const;
void renderTextCellDxf(QString &, const QString &, const TitleBlockCell &, qreal, qreal, qreal, qreal, int) const;
// attributes
private:

View File

@@ -76,6 +76,12 @@ void TitleBlockTemplateRenderer::render(QPainter *provided_painter, int titleblo
}
}
void TitleBlockTemplateRenderer::renderDxf(QRectF &title_block_rect, int titleblock_width, QString &file_path, int color) {
if (!titleblock_template_) return;
titleblock_template_ -> renderDxf(title_block_rect, context_, titleblock_width, file_path, color);
}
/**
Renders the titleblock to the internal QPicture
@param titleblock_width Width of the titleblock to render

View File

@@ -31,6 +31,7 @@ class TitleBlockTemplateRenderer : public QObject {
void setContext(const DiagramContext &context);
int height() const;
void render(QPainter *, int);
void renderDxf(QRectF &, int, QString &, int);
void invalidateRenderedTemplate();
void setUseCache(bool);
bool useCache() const;