利用dxflib读写cad文件

作者: admin 分类: C++,CAD,dxflib 发布时间: 2019-09-14 21:44

本篇主要在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 << "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.h

#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.cpp

#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文件,然后用自己喜欢的方式放到指定目录,就可以使用了。

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

标签云