本篇主要在qt作一個例子讀取cad文件數據,使用的開源庫是dxflib。用到dxflib的DL_CreationAdapter,DL_Dxf。DL_Dxf是用來讀取.dxf文件的類,DL_CreatinAdapter是一個容器,當DL_Dxf加載.dxf文件後,就會響應DL_CreationAdapter這個類,讀取cad數據。
咱們來看看他的工做原理:c++
//本身聲明一個類繼承DL_CreationAdapter class MyDxfFilter : public DL_CreationAdapter { //重寫虛函數來把度出來的數據保存到本身設計好的數據結構中 virtual void addLine(const DL_LineData& d); ... } void MyDxfFilter::addLine(const DL_LineData& d) { std::cout << "Line: " << d.x1 << "/" << d.y1 << " " << d.x2 << "/" << d.y2 << std::endl; } MyDxfFilter f; DL_Dxf dxf; //讀取.dxf文件 if (!dxf.in("drawing.dxf", &f)) { std::cerr << "drawing.dxf could not be opened.\n"; }
在這段程序中,當dxf.in("drawing.dxf", &f)讀取.dxf文件成功了,若是文件中有不少line,那麼f對象會一直遞歸調用虛函數接口addLine(),直到全部的line都讀完。
重這個特徵,咱們就能夠把全部咱們要讀取的數據都出來了!
直接上程序!:
entitiesDatas.hcanvas
#ifndef ENTITIESDATAS_H #define ENTITIESDATAS_H #include <QDebug> #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 <QList> #include <QString> class EntitiesDatas:public DL_CreationAdapter { public: // explicit EntitiesDatas(); virtual void EntitiesDatas::addPoint(const DL_PointData &d){ points.append(d); qDebug()<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^add point"; } virtual void EntitiesDatas::addLine(const DL_LineData &d){ lines.append(d); qDebug()<<"~~~~~~~~~~~~~~~~~~~~~~~~~~add line"; } virtual void EntitiesDatas::addArc(const DL_ArcData &d){ arcs.append(d); qDebug()<<"#######################add arc"; } virtual void EntitiesDatas::addCircle(const DL_CircleData &d){ circles.append(d); qDebug()<<"!!!!!!!!!!!!!!!!!!!!!!!!add circle"; } virtual void EntitiesDatas::addEllipse(const DL_EllipseData &d){ ellipses.append(d); qDebug()<<"%%%%%%%%%%%%%%%%%%%%%%add ellipse"; } virtual void EntitiesDatas::addText(const DL_TextData &d){ texts.append(d); qDebug()<<"add text"; } virtual void EntitiesDatas::addDimAngular(const DL_DimensionData &d, const DL_DimAngularData &d1){ qDebug()<<"***********add angular"; } // virtual EntitiesDatas::addDimLinear(const DL_DimensionData &d, const DL_DimLinearData &d1){ // qDebug()<<"___________________________add deim linear"; // } virtual void EntitiesDatas::addMText(const DL_MTextData &d){ mtexts.append(d); qDebug()<<"&&&&&&&&&&add mtext"; } virtual void EntitiesDatas::addXLine(const DL_XLineData &){ qDebug()<<"++++++++++++++add xline"; } virtual void EntitiesDatas::addRay(const DL_RayData &){ qDebug()<<"/////////////////////////////add ray"; } virtual void EntitiesDatas::addPolyline(const DL_PolylineData &d){ qDebug()<<"eeeeeeeeeeeeeeeeeeeeeeeeeee add polyline"; polylines.append(d); } virtual void EntitiesDatas::addSpline(const DL_SplineData &){ qDebug()<<".....................................add spline data"; } virtual void EntitiesDatas::addComment(const std::string &){ qDebug()<<",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,add comment"; } virtual void EntitiesDatas::addVertex(const DL_VertexData &d){ vertices.append(d); qDebug()<<"hhhhhhhhhhhhhhhhhhhhhhhh add vertex"; } virtual void EntitiesDatas::addLayer(const DL_LayerData &d){ qDebug()<<"ggggggggggggggggggg add layer"; qDebug()<<QString::fromStdString(d.name); } virtual void EntitiesDatas::addXDataString(int, const std::string &){ qDebug()<<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>add xdata string"; } virtual void EntitiesDatas::addXRecord(const std::string &){ qDebug()<<",.,.,.,.,.,.,.,.,.,.,.,.,.,.,add repcord"; } virtual void EntitiesDatas::addTrace(const DL_TraceData &){ qDebug()<<"mmmmmmmmmmmmmm add trace"; } virtual void EntitiesDatas::endEntity(){ qDebug()<<"end entity***********************"; qDebug()<<"attribute ***************** "<<this->getAttributes().getColor(); } virtual void EntitiesDatas::endSequence(){ qDebug()<<"end sequence +++++++++++++++++++++"; } virtual void EntitiesDatas::setAttributes(const DL_Attributes &attrib){ qDebug()<<"set attribute ()()()()()()()()()()()()()()()"; } virtual void EntitiesDatas::addHatch(const DL_HatchData &d){ qDebug()<<"nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn add hatch"; hatches.append(d); } virtual void EntitiesDatas::addHatchEdge(const DL_HatchEdgeData &d){ qDebug()<<"MMMMMMMMMMMMMMMMKMMMMM add hat edge data"; hatchedges.append(d); if(this->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<DL_PointData> points; QList<DL_LineData> lines;// QList<DL_ArcData> arcs; QList<DL_CircleData> circles;// QList<DL_EllipseData> ellipses; QList<DL_TextData> texts; QList<DL_DimAngularData> dimangulars; QList<DL_DimLinearData> dimlines; QList<DL_DimRadialData> radias; QList<DL_VertexData> vertices;// QList<DL_PolylineData> polylines;// QList<DL_MTextData> mtexts;// QList<DL_HatchData> hatches;// QList<DL_HatchEdgeData> hatchedges;// QList<QString> hatchcolors; }; #endif // ENTITIESDATAS_H
dxfreader.h數據結構
#ifndef DXFREADER_H #define DXFREADER_H #include <QObject> #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 <QDebug> #include "entitiesdatas.h" #include <QString> class DXFReader : public QObject { Q_OBJECT public: explicit DXFReader(QObject *parent = 0); //line Q_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); //circle Q_INVOKABLE int getCircleLength(); Q_INVOKABLE double getCirclex(int index); Q_INVOKABLE double getCircley(int index); Q_INVOKABLE double getCircleRadius(int index); //mtext Q_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 line Q_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 color Q_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.cppapp
#include "dxfreader.h" #include "math.h" #include "QtMath" DXFReader::DXFReader(QObject *parent) : QObject(parent) { //初始化第一個dxf文件 if (!dxf.in("c:/aa.dxf", &f)) { qDebug()<<"can not read datas"; }else { qDebug()<<"success"; } } //line int DXFReader::getLineLength(){ return f.lines.count(); } double DXFReader::getLinex1(int index){ return f.lines.at(index).x1; } double DXFReader::getLinex2(int index){ return f.lines.at(index).x2; } double DXFReader::getLiney1(int index){ return f.lines.at(index).y1; } double DXFReader::getLiney2(int index){ return f.lines.at(index).y2; } //circle int DXFReader::getCircleLength(){ return f.circles.count(); } double DXFReader::getCirclex(int index){ return f.circles.at(index).cx; } double DXFReader::getCircley(int index){ return f.circles.at(index).cy; } double DXFReader::getCircleRadius(int index){ return f.circles.at(index).radius; } //mtext int DXFReader::getMTextLength(){ return f.mtexts.count(); } double DXFReader::getMTextx(int index){ return f.mtexts.at(index).ipx; } double DXFReader::getMTexty(int index){ return f.mtexts.at(index).ipy; } QString DXFReader::getMTextStr(int index){ return QString::fromStdString(f.mtexts.at(index).text); } double DXFReader::getMTextSize(int index){ return f.mtexts.at(index).height*3/4; } //ploy line int DXFReader::getPolyLineLength(){ return f.polylines.count(); } double DXFReader::getVertex1x(int index){ return f.vertices.at(index*4).x; } double DXFReader::getVertex2x(int index){ return f.vertices.at(index*4+1).x; } double DXFReader::getVertex3x(int index){ return f.vertices.at(index*4+2).x; } double DXFReader::getVertex4x(int index){ return f.vertices.at(index*4+3).x; } double DXFReader::getVertex1y(int index){ return f.vertices.at(index*4).y; } double DXFReader::getVertex2y(int index){ return f.vertices.at(index*4+1).y; } double DXFReader::getVertex3y(int index){ return f.vertices.at(index*4+2).y; } double DXFReader::getVertex4y(int index){ return f.vertices.at(index*4+3).y; } //hatch edge and hatch color int DXFReader::getHatchLength(){ return f.hatchedges.count(); } double DXFReader::getHatchx(int index){ return f.hatchedges.at(index).cx; } double DXFReader::getHatchy(int index){ return f.hatchedges.at(index).cy; } QString DXFReader::getHatchColor(int index){ return f.hatchcolors.at(index); } double DXFReader::getHatchRadius(int index){ return f.hatchedges.at(index).radius; }
上面程序我是設計成暴露給qml使用的數據,一邊使用qml中的canvas來繪製cad數據
看看都出來的數據效果圖:函數
到這裏,這個實現方法有個缺點,就是使用了qml 中的canvas繪製數據,當把canvas進行縮放的時候,onpaint事件被響應,可是數據不少,因此縮放越大,消耗內存就越大,搞得有點卡頓,不過這方法的有點就是方便,省了不少時間。
關於dxflib的搭建,很簡單,只須要在qcad官網下載dxflib源碼,用qt編譯出dxflib.lib文件,而後用本身喜歡的方式放到指定目錄,就能夠使用了。this