离线
找到的第一份代码 https://github.com/Luckyxcj/dxflib
是一个调用 dxflib 库写 dxf 文件,
#include <QCoreApplication>
#include <QDebug>
#include "src/dl_dxf.h"
#include "src/dl_creationadapter.h"
#include "src/dl_attributes.h"
#include "src/dl_codes.h"
#include "src/dl_entities.h"
#include "src/dl_exception.h"
#include "src/dl_global.h"
#include "src/dl_writer.h"
#include "src/dl_writer_ascii.h"
#include <QList>
#include <QString>
#include <QFile>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QFile file("myfile.dxf");
if(file.exists())
{
file.remove();
qDebug()<<"DEL dxf";
}
#if 1
DL_Dxf dxf;
DL_Codes::version exportVersion = DL_Codes::AC1015;
DL_WriterA* dw = dxf.out("myfile.dxf", exportVersion);
if (dw==NULL)
{
printf("Cannot open file 'myfile.dxf' \
for writing.");
// abort function e.g. with return
}
#if 1
// int variable:
//单位
dw->dxfString(9, "$INSUNITS");
dw->dxfInt(70, 4);
// real (double, float) variable:
dw->dxfString(9, "$DIMEXE");
dw->dxfReal(40, 1.25);
// string variable:
dw->dxfString(9, "$TEXTSTYLE");
dw->dxfString(7, "Standard");
// vector variable:
dw->dxfString(9, "$LIMMIN");
dw->dxfReal(10, 0.0);
dw->dxfReal(20, 0.0);
#endif
//关闭header
dw->sectionEnd();
//写tablesection
//.1 打开tables section
dw->sectionTables();
//.2 写viewports
dxf.writeVPort(*dw);
//.3写linetypes
dw->tableLinetypes(25);
dxf.writeLinetype(*dw, DL_LinetypeData("BYBLOCK","",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("BYLAYER","",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("CONTINUOUS","",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("ACAD_ISO02W100", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("ACAD_ISO03W100", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("ACAD_ISO04W100", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("ACAD_ISO05W100", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("BORDER", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("BORDER2", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("BORDERX2", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("CENTER", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("CENTER2", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("CENTERX2", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("DASHDOT", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("DASHDOT2", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("DASHDOTX2", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("DASHED", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("DASHED2", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("DASHEDX2", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("DIVIDE", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("DIVIDE2", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("DIVIDEX2","",0,0, 0));
dxf.writeLinetype(*dw, DL_LinetypeData("DOT", "",0,0,0));
dxf.writeLinetype(*dw, DL_LinetypeData("DOT2","",0,0, 0));
dxf.writeLinetype(*dw, DL_LinetypeData("DOTX2", "",0,0,0));
dw->tableEnd();
//.4写layers 0不可以省略
int numberOfLayers = 3;
dw->tableLayers(numberOfLayers);
dxf.writeLayer(*dw,
DL_LayerData("0", 0),
DL_Attributes( std::string(""),
DL_Codes::red,
-1,
100,
"BYLAYER"
)); // default line style
dxf.writeLayer(*dw,
DL_LayerData("mainlayer", 0),
DL_Attributes());//CONTINUOUS
dxf.writeLayer(*dw,
DL_LayerData("anotherlayer", 0),
DL_Attributes());
dw->tableEnd();
//.5其他tables 不必要
// dxf.writeStyle(*dw,DL_StyleData());
//。6写dimension styles
dxf.writeDimStyle(*dw,
1,
1,
1,
1,
1);
dxf.writeBlockRecord(*dw);
#if 1
dxf.writeBlockRecord(*dw, "myblock1");
dxf.writeBlockRecord(*dw, "myblock2");
#endif
dw->tableEnd();
//.7结束tables section
dw->sectionEnd();
//写blocks section
dw->sectionBlocks();
dxf.writeBlock(*dw,
DL_BlockData("*Model_Space", 0, 0.0, 0.0, 0.0));
dxf.writeEndBlock(*dw, "*Model_Space");
dxf.writeBlock(*dw,
DL_BlockData("*Paper_Space", 0, 0.0, 0.0, 0.0));
dxf.writeEndBlock(*dw, "*Paper_Space");
dxf.writeBlock(*dw,
DL_BlockData("*Paper_Space0", 0, 0.0, 0.0, 0.0));
dxf.writeEndBlock(*dw, "*Paper_Space0");
dxf.writeBlock(*dw,
DL_BlockData("myblock1", 0, 0.0, 0.0, 0.0));
// ...
// write block entities e.g. with dxf.writeLine(), ..
// ...
dxf.writeEndBlock(*dw, "myblock1");
dxf.writeBlock(*dw,
DL_BlockData("myblock2", 0, 0.0, 0.0, 0.0));
// ...
// write block entities e.g. with dxf.writeLine(), ..
// ...
dxf.writeEndBlock(*dw, "myblock2");
dw->sectionEnd();
//写entities section
dw->sectionEntities();
// write all your entities..
dxf.writePoint(*dw,
DL_PointData(10.0, 45.0, 0.0),
DL_Attributes());//"mainlayer", 256, -1, "BYLAYER"
dxf.writeLine(*dw,
DL_LineData(25.0, 30.0, 0.0, // start point
100.0, 120.0, 0.0), // end point
//DL_Attributes("mainlayer", 256, -1, "BYLAYER"));
DL_Attributes());
dxf.writeArc(*dw,
DL_ArcData(50.0,50.0,0.0,
20,0.0,20),
DL_Attributes(std::string(""),
DL_Codes::red,
-1,
100,
"BYLAYER"
));
dxf.writeCircle(*dw,DL_CircleData(50.0,50.0,5.0,20.0),
DL_Attributes(std::string(""),
DL_Codes::red,
-1,
1,
"BYLAYER"
));
dw->sectionEnd();
//写objects section
dxf.writeObjects(*dw);
dxf.writeObjectsEnd(*dw);
//结束
dw->dxfEOF();
dw->close();
delete dw;
#endif
qDebug()<< "end"<<endl;
return a.exec();
}
离线
公司用的 dxf 解析库各种问题, 发现 qcad 用的就是 dxflib 相当完善, 所以打算强怼 dxflib 了。
离线