利用dflib读写cad文件
本篇主要在qt做一个例子读取cad文件数据,使用的开源库是dxflib。用到dxflib的DL_CreationAdapter,DL_Dxf。DL_Dxf是用来读取.dxf文件的类,DL_CreatinAdapter是一个容器,当DL_Dxf加载.dxf文件后,就会响应DL_CreationAdapter这个类,读取cad数据。
我们来看看他的工作原理:
//自己声明一个类继承DL_CreationAdapter
class MyDxfFilter : public DL_CreationAdapter {
//重写虚函数来把度出来的数据保存到自己设计好的数据结构中
virtual void addLine(const DL_LineData& d);
...
}
void MyDxfFilter::addLine(const DL_LineData& d) {
std::cout
在这段程序中,当dxf.in("drawing.dxf", &f)读取.dxf文件成功了,如果文件中有很多line,那么f对象会一直递归调用虚函数接口addLine(),直到所有的line都读完。
重这个特征,我们就可以把所有我们要读取的数据都出来了!
直接上程序!:
entitiesDatas.h
ifndef ENTITIESDATAS_H
define ENTITIESDATAS_H
include
include "dl_dxf.h"
include "dl_creationadapter.h"
include "dl_attributes.h"
include "dl_codes.h"
include "dl_entities.h"
include "dl_exception.h"
include "dl_global.h"
include "dl_writer.h"
include "dl_writer_ascii.h"
include
include
class EntitiesDatas:public DL_CreationAdapter
{
public:
// explicit EntitiesDatas();
virtual void EntitiesDatas::addPoint(const DL_PointData &d){ points.append(d); qDebug()>>>>>>>>>>>>>>>>>>>>>>>>>>>>add xdata string";}virtual void EntitiesDatas::addXRecord(const std::string &){ qDebug()getAttributes().getColor();}virtual void EntitiesDatas::endSequence(){ qDebug()getAttributes().getColor()==1){ QString color="# ef3312"; hatchcolors.append(color); } else if(this->getAttributes().getColor()==2){ QString color="# efef33"; hatchcolors.append(color); } else if(this->getAttributes().getColor()==3){ QString color="# 12ef33"; hatchcolors.append(color); } else { QString color="# efefef"; hatchcolors.append(color); }}
// virtual void EntitiesDatas::att
QList points;QList lines;//QList arcs;QList circles;//QList ellipses;QList texts;QList dimangulars;QList dimlines;QList radias;QList vertices;//QList polylines;//QList mtexts;//QList hatches;//QList hatchedges;//QList hatchcolors;
};
endif // ENTITIESDATAS_H
dxfreader.h
ifndef DXFREADER_H
define DXFREADER_H
include
include "entitiesdatas.h"
include "dl_dxf.h"
include "dl_creationadapter.h"
include "dl_attributes.h"
include "dl_codes.h"
include "dl_entities.h"
include "dl_exception.h"
include "dl_global.h"
include "dl_writer.h"
include "dl_writer_ascii.h"
include
include "entitiesdatas.h"
include
class DXFReader : public QObject
{
Q_OBJECT
public:
explicit DXFReader(QObject *parent = 0);
//lineQ_INVOKABLE int getLineLength();Q_INVOKABLE double getLinex1(int index);Q_INVOKABLE double getLinex2(int index);Q_INVOKABLE double getLiney1(int index);Q_INVOKABLE double getLiney2(int index);//circleQ_INVOKABLE int getCircleLength();Q_INVOKABLE double getCirclex(int index);Q_INVOKABLE double getCircley(int index);Q_INVOKABLE double getCircleRadius(int index);//mtextQ_INVOKABLE int getMTextLength();Q_INVOKABLE double getMTextx(int index);Q_INVOKABLE double getMTexty(int index);Q_INVOKABLE QString getMTextStr(int index);Q_INVOKABLE double getMTextSize(int index);//ploy lineQ_INVOKABLE int getPolyLineLength();Q_INVOKABLE double getVertex1x(int index);Q_INVOKABLE double getVertex2x(int index);Q_INVOKABLE double getVertex3x(int index);Q_INVOKABLE double getVertex4x(int index);Q_INVOKABLE double getVertex1y(int index);Q_INVOKABLE double getVertex2y(int index);Q_INVOKABLE double getVertex3y(int index);Q_INVOKABLE double getVertex4y(int index);//hatch edge and hatch colorQ_INVOKABLE int getHatchLength();Q_INVOKABLE QString getHatchColor(int index);Q_INVOKABLE double getHatchx(int index);Q_INVOKABLE double getHatchy(int index);Q_INVOKABLE double getHatchRadius(int index);
signals:
public slots:
private:
EntitiesDatas f;
DL_Dxf dxf;
};
endif // DXFREADER_H
dxfreader.cpp
include "dxfreader.h"
include "math.h"
include "QtMath"
DXFReader::DXFReader(QObject *parent) : QObject(parent)
{
//初始化第一个dxf文件
if (!dxf.in("c:/aa.dxf", &f)) {
qDebug()
上面程序我是设计成暴露给qml使用的数据,一边使用qml中的canvas来绘制cad数据
看看都出来的数据效果图:
到这里,这个实现方法有个缺点,就是使用了qml 中的canvas绘制数据,当把canvas进行缩放的时候,onpaint事件被响应,但是数据很多,所以缩放越大,消耗内存就越大,搞得有点卡顿,不过这方法的有点就是方便,省了很多时间。
关于dxflib的搭建,很简单,只需要在qcad官网下载dxflib源码,用qt编译出dxflib.lib文件,然后用自己喜欢的方式放到指定目录,就可以使用了。
关键字:cad, qt, qml, #c++#
版权声明
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!