电子工程师技术服务社区
公告
登录
|
注册
首页
技术问答
厂商活动
正点原子
板卡试用
资源库
下载
文章
社区首页
文章
通过dxflib库读取DXF文件
分 享
扫描二维码分享
通过dxflib库读取DXF文件
dxflib
Qt
linux
大斯
关注
发布时间: 2020-04-16
丨
阅读: 6518
# 前言 进入正文之前,我要先嘚吧嘚两句。关于如何在qt中读取cad文件,我真是查资料查的头都大了(哎,要不是自己水平有限。。苦笑),网上的大多数资料都是在windows下读取cad文件,要不就是代码不完整。总之就是看的蓝瘦。所以就整理了一下资料,将代码记录到这里,供其他的小伙伴参考。当然 ,如果哪位小伙伴有其他的开源库更好用的话可以在评论区告诉我哦。感恩!!! ## DXF文件分析 其实在做这个之前我也不了解DXF文件的结构,所以在看dxflib库的时候就看不懂,下图是一个良心博主自己画的架构图,我拿来给大家参考一下: ![](https://cf01.ickimg.com/bbsimages/202004/a19ad058daef011f7560e821ce32ff11.png) 可以看到,dxf的线,圆等实体要么在block中,要么在entities中,这两者的区别就是block只有在后面被insert之后才真正的加入到图像中,否则就只是一个单独的东西,可以不加理会。 至于dxf文件的格式,其实理解起来也不难。给大家一个参考地址 [dxf格式说明](http://docs.autodesk.com/ACD/2011/CHS/filesDXF/WSfacf1429558a55de185c428100849a0ab7-5df0.htm "dxf格式说明") 如果大家只是读取dxf文件的话,也不用了解太多,像实体、属性、层、段基本概念了解一下就可以了,但是如果你是要写dxf文件,那么你还是有必要去了解一下dxf文件的格式。 ## dxflib库的使用 我们用到了一个开源库dxflib,这是一个用来读写dxf文件的c++库,当读写dxf文件的时候,dxflib分析文件并且挑用用户自己定义的函数来添加实体、层等数据,而且,dxflib并不保存任何实体或者信息,它只是从dxf文件中分析可以识别的实体以及其他的对象。所以,如果你需要绘制dxf文件的话,你可以将它读取到的信息保存到容器中,然后进行绘制。本文中未做绘制部分,所以就将它的信息打印出来了。 【dxflib库的使用】[dxflib库的使用](https://www.cnblogs.com/liuyunfeifei/archive/2012/11/29/2794490.html "dxflib库的使用") 【dxflib库的下载】[dxflib库下载](http://www.qcad.org/en/dxflib-downloads "dxflib库下载") 简单说一下我们的思路,我们在dxflib库中主要用到的是DL_CreationAdapter,DL_Dxf,DL_Dxf是用来读取.dxf文件的类,DL_CreatinAdapter是一个容器,当DL_Dxf加载.dxf文件后,就会响应DL_CreationAdapter这个类,读取cad数据。所以我们应该声明一个类继承DL_CreationAdapter,然后重写虚函数吧读出来的数据打印出来或者保存到容器中。 **好了 我们直接来看代码吧!!!** ## 代码设计 大家下载了dxflib库后直接把里面的文件拷贝到自己的工程中去就可以,然后新建Test_CreationClass类继承于DL_CreationAdapter,重写虚函数addLine()等。 Test_CreationClass.h文件 ```cpp #ifndef TEST_CREATIONCLASS_H #define TEST_CREATIONCLASS_H #include "dl_creationadapter.h" /@@** * This class takes care of the entities read from the file. * Usually such a class would probably store the entities. * this one just prints some information about them to stdout. * * @author Andrew Mustun */ class Test_CreationClass : public DL_CreationAdapter { public: Test_CreationClass(); virtual void addLayer(const DL_LayerData& data); virtual void addPoint(const DL_PointData& data); virtual void addLine(const DL_LineData& data); virtual void addArc(const DL_ArcData& data); virtual void addCircle(const DL_CircleData& data); virtual void addPolyline(const DL_PolylineData& data); virtual void addVertex(const DL_VertexData& data); virtual void add3dFace(const DL_3dFaceData& data); void printAttributes(); }; #endif ``` Test_CreationClass.cpp文件 ```cpp #include "test_creationclass.h" #include
#include
/@@** * Default constructor. */ Test_CreationClass::Test_CreationClass() {} /@@** * Sample implementation of the method which handles layers. */ void Test_CreationClass::addLayer(const DL_LayerData& data) { printf("LAYER: %s flags: %d\n", data.name.c_str(), data.flags); printAttributes(); } /@@** * Sample implementation of the method which handles point entities. */ void Test_CreationClass::addPoint(const DL_PointData& data) { printf("POINT (%6.3f, %6.3f, %6.3f)\n", data.x, data.y, data.z); printAttributes(); } /@@** * Sample implementation of the method which handles line entities. */ void Test_CreationClass::addLine(const DL_LineData& data) { printf("LINE (%6.3f, %6.3f, %6.3f) (%6.3f, %6.3f, %6.3f)\n", data.x1, data.y1, data.z1, data.x2, data.y2, data.z2); printAttributes(); } /@@** * Sample implementation of the method which handles arc entities. */ void Test_CreationClass::addArc(const DL_ArcData& data) { printf("ARC (%6.3f, %6.3f, %6.3f) %6.3f, %6.3f, %6.3f\n", data.cx, data.cy, data.cz, data.radius, data.angle1, data.angle2); printAttributes(); } /@@** * Sample implementation of the method which handles circle entities. */ void Test_CreationClass::addCircle(const DL_CircleData& data) { printf("CIRCLE (%6.3f, %6.3f, %6.3f) %6.3f\n", data.cx, data.cy, data.cz, data.radius); printAttributes(); } /@@** * Sample implementation of the method which handles polyline entities. */ void Test_CreationClass::addPolyline(const DL_PolylineData& data) { printf("POLYLINE \n"); printf("flags: %d\n", (int)data.flags); printAttributes(); } /@@** * Sample implementation of the method which handles vertices. */ void Test_CreationClass::addVertex(const DL_VertexData& data) { printf("VERTEX (%6.3f, %6.3f, %6.3f) %6.3f\n", data.x, data.y, data.z, data.bulge); printAttributes(); } void Test_CreationClass::add3dFace(const DL_3dFaceData& data) { printf("3DFACE\n"); for (int i=0; i<4; i++) { printf(" corner %d: %6.3f %6.3f %6.3f\n", i, data.x[i], data.y[i], data.z[i]); } printAttributes(); } void Test_CreationClass::printAttributes() { printf(" Attributes: Layer: %s, ", attributes.getLayer().c_str()); printf(" Color: "); if (attributes.getColor()==256) { printf("BYLAYER"); } else if (attributes.getColor()==0) { printf("BYBLOCK"); } else { printf("%d", attributes.getColor()); } printf(" Width: "); if (attributes.getWidth()==-1) { printf("BYLAYER"); } else if (attributes.getWidth()==-2) { printf("BYBLOCK"); } else if (attributes.getWidth()==-3) { printf("DEFAULT"); } else { printf("%d", attributes.getWidth()); } printf(" Type: %s\n", attributes.getLinetype().c_str()); } ``` main文件中大家直接调用testReading函数就可以了 ```cpp void testReading(char* file) { // Load DXF file into memory: std::cout << "Reading file " << file << "...\n"; Test_CreationClass* creationClass = new Test_CreationClass(); DL_Dxf* dxf = new DL_Dxf(); if (!dxf->in(file, creationClass)) { // if file open failed std::cerr << file << " could not be opened.\n"; return; } delete dxf; delete creationClass; } ``` 大家运行一下代码应该可以得到dxf文件的数据,运行结果如下: ![](https://cf01.ickimg.com/bbsimages/202004/4211ef4455e209d7fc1db19bc72e2709.png) 需要注意的是,如果大家的工程和build文件夹不在一起的话,可能会提示你.dxf文件找不到,你需要把等待读取的.dxf文件放到build文件夹中。 最后附上代码地址:https://github.com/sixsmall/dxflib-dxf.git
原创作品,未经权利人授权禁止转载。详情见
转载须知
。
举报文章
点赞
(
1
)
大斯
关注
评论
(0)
登录后可评论,请
登录
或
注册
相关文章推荐
MK-米客方德推出工业级存储卡
Beetle ESP32 C3 蓝牙数据收发
Beetle ESP32 C3 wifi联网获取实时天气信息
开箱测评Beetle ESP32-C3 (RISC-V芯片)模块
正点原子数控电源DP100测评
DP100试用评测-----开箱+初体验
Beetle ESP32 C3环境搭建
【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏之二
X
你的打赏是对原创作者最大的认可
请选择打赏IC币的数量,一经提交无法退回 !
100IC币
500IC币
1000IC币
自定义
IC币
确定
X
提交成功 ! 谢谢您的支持
返回
我要举报该内容理由
×
广告及垃圾信息
抄袭或未经授权
其它举报理由
请输入您举报的理由(50字以内)
取消
提交