C语言库学习(四):用libredwg解析cad的dwg文件——第一版(qt)

开始

首先下载LibreDWG的源代码,
git clone https://github.com/LibreDWG/libredwg.git
下载完成后,把src文件夹和include文件夹复制到qt工程目录下
jsmn文件夹是空的,也需要下载
git clone https://github.com/zserge/jsmn

第一次失败的尝试

第一次用libredwg-0.10.1.2969-win32.zip里的.a静态库,但是编译的时候出错,猜测可能是编译器版本不一样,既然编译好的库不能用,就只能自己编译源代码了

编译源代码

C语言库学习(四):用libredwg解析cad的dwg文件——第一版(qt)_第1张图片
添加新文件,把下载到的LibreDWG/src下的所有.c.h和有用的文件全部添加到工程中,新建一个窗口,一个QPushButton
C语言库学习(四):用libredwg解析cad的dwg文件——第一版(qt)_第2张图片

引用头文件、编写测试代码

#define restrict
#include "include/dwg.h"
void Widget::add_line (double x1, double x2, double y1, double y2)
{
    // Make something with that
    qDebug()<<"//add_line"<<x1;
    qDebug()<<","<<y1;
    qDebug()<<","<<x2;
    qDebug()<<","<<y2;
    QPainter painter(&pix);
    painter.setPen(Qt::red);
    painter.drawLine(x1,y1,x2,y2);
    update();
}

void Widget::add_circle (double x, double y, double R)
{
    // Make something with that
    qDebug()<<"//add_circle"<<x;
    qDebug()<<","<<y;
    qDebug()<<","<<R;
    QPainter painter(&pix);
    painter.setPen(Qt::blue);
    painter.drawEllipse(x,y,R,R);
    update();
}

int Widget::load_dwg (char *filename, unsigned int opts)
{
    BITCODE_BL i;
    int success;
    Dwg_Data dwg;

    memset (&dwg, 0, sizeof (Dwg_Data));
    dwg.opts = opts;
    success = dwg_read_file (filename, &dwg);
    for (i = 0; i < dwg.num_objects; i++)
    {
        Dwg_Entity_LINE *line;
        Dwg_Entity_CIRCLE *circle;
        Dwg_Entity_TEXT *text;
        //qDebug()<<"//typeINT:"<
        switch (dwg.object[i].type)
        {
        case DWG_TYPE_LINE:
            line = dwg.object[i].tio.entity->tio.LINE;
            qDebug()<<"//type:line";
            add_line (line->start.x, line->end.x, line->start.y, line->end.y);
            break;
        case DWG_TYPE_CIRCLE:
            circle = dwg.object[i].tio.entity->tio.CIRCLE;
            qDebug()<<"//type:DWG_TYPE_CIRCLE";
            add_circle (circle->center.x, circle->center.y, circle->radius);
            break;
        case DWG_TYPE_TEXT:
            text = dwg.object[i].tio.entity->tio.TEXT;
            qDebug()<<"//type:DWG_TYPE_TEXT";
            add_text (text->insertion_pt.x, text->insertion_pt.y,
                      text->text_value);
            break;
        default:
            break;
        }
    }
    dwg_free (&dwg);
    return success;
}

完善其他的代码

头文件(widget.h中)

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    static void setx(double x);
    void add_line (double x1, double y1, double x2, double y2);
    int load_dwg (char *filename, unsigned int opts);
    void add_circle (double x, double y, double R);

protected:
    void paintEvent(QPaintEvent *event);

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
    QPixmap pix;
};

#endif // WIDGET_H

重写paintEvent(widget.cpp中)


void Widget::paintEvent(QPaintEvent *event)
{
    QPainter paint(this);
    paint.drawPixmap(0,0,pix);
}

qpushbutton鼠标点击(widget.cpp中)


void Widget::on_pushButton_clicked()
{
    qDebug()<<"start";
    QString fileName = QFileDialog::getSaveFileName(this, tr("Excel file"), "./",
                                                    tr("Excel Files (*.dwg)"));
    if (fileName.isEmpty())
        return;
    pix = QPixmap(size());
    pix.fill(Qt::white);
    qDebug()<<"filename:"<<fileName;
    load_dwg(fileName.toUtf8().data(),0);
}

运行结果

C语言库学习(四):用libredwg解析cad的dwg文件——第一版(qt)_第3张图片

需要改进的地方

  1. 屏幕的(0,0)坐标是在左上角,而CAD绘图的(0,0)坐标在左下角,需要把上下对称一下,用屏幕高度-x坐标就可以
  2. 现在只能识别直线和圆,需要添加更多图形识别,根据LibreDWG的定义如下:

dwg文件中存在的对象和实体类型

/**
 Object and Entity types that exist in dwg-files.
 */
typedef enum DWG_OBJECT_TYPE
{
  DWG_TYPE_UNUSED = 0x00,
  DWG_TYPE_TEXT = 0x01,
  DWG_TYPE_ATTRIB = 0x02,
  DWG_TYPE_ATTDEF = 0x03,
  DWG_TYPE_BLOCK = 0x04,
  DWG_TYPE_ENDBLK = 0x05,
  DWG_TYPE_SEQEND = 0x06,
  DWG_TYPE_INSERT = 0x07,
  DWG_TYPE_MINSERT = 0x08,
  /* DWG_TYPE_ = 0x09, */
  DWG_TYPE_VERTEX_2D = 0x0a,
  DWG_TYPE_VERTEX_3D = 0x0b,
  DWG_TYPE_VERTEX_MESH = 0x0c,
  DWG_TYPE_VERTEX_PFACE = 0x0d,
  DWG_TYPE_VERTEX_PFACE_FACE = 0x0e,
  DWG_TYPE_POLYLINE_2D = 0x0f,
  DWG_TYPE_POLYLINE_3D = 0x10,
  DWG_TYPE_ARC = 0x11,
  DWG_TYPE_CIRCLE = 0x12,
  DWG_TYPE_LINE = 0x13,
  DWG_TYPE_DIMENSION_ORDINATE = 0x14,
  DWG_TYPE_DIMENSION_LINEAR = 0x15,
  DWG_TYPE_DIMENSION_ALIGNED = 0x16,
  DWG_TYPE_DIMENSION_ANG3PT = 0x17,
  DWG_TYPE_DIMENSION_ANG2LN = 0x18,
  DWG_TYPE_DIMENSION_RADIUS = 0x19,
  DWG_TYPE_DIMENSION_DIAMETER = 0x1A,
  DWG_TYPE_POINT = 0x1b,
  DWG_TYPE__3DFACE = 0x1c,
  DWG_TYPE_POLYLINE_PFACE = 0x1d,
  DWG_TYPE_POLYLINE_MESH = 0x1e,
  DWG_TYPE_SOLID = 0x1f,
  DWG_TYPE_TRACE = 0x20,
  DWG_TYPE_SHAPE = 0x21,
  DWG_TYPE_VIEWPORT = 0x22,
  DWG_TYPE_ELLIPSE = 0x23,
  DWG_TYPE_SPLINE = 0x24,
  DWG_TYPE_REGION = 0x25,
  DWG_TYPE__3DSOLID = 0x26,
  DWG_TYPE_BODY = 0x27,
  DWG_TYPE_RAY = 0x28,
  DWG_TYPE_XLINE = 0x29,
  DWG_TYPE_DICTIONARY = 0x2a,
  DWG_TYPE_OLEFRAME = 0x2b,
  DWG_TYPE_MTEXT = 0x2c,
  DWG_TYPE_LEADER = 0x2d,
  DWG_TYPE_TOLERANCE = 0x2e,
  DWG_TYPE_MLINE = 0x2f,
  DWG_TYPE_BLOCK_CONTROL = 0x30,
  DWG_TYPE_BLOCK_HEADER = 0x31,
  DWG_TYPE_LAYER_CONTROL = 0x32,
  DWG_TYPE_LAYER = 0x33,
  DWG_TYPE_STYLE_CONTROL = 0x34,
  DWG_TYPE_STYLE = 0x35,
  /* DWG_TYPE_ = 0x36, */
  /* DWG_TYPE_ = 0x37, */
  DWG_TYPE_LTYPE_CONTROL = 0x38,
  DWG_TYPE_LTYPE = 0x39,
  /* DWG_TYPE_ = 0x3a, */
  /* DWG_TYPE_ = 0x3b, */
  DWG_TYPE_VIEW_CONTROL = 0x3c,
  DWG_TYPE_VIEW = 0x3d,
  DWG_TYPE_UCS_CONTROL = 0x3e,
  DWG_TYPE_UCS = 0x3f,
  DWG_TYPE_VPORT_CONTROL = 0x40,
  DWG_TYPE_VPORT = 0x41,
  DWG_TYPE_APPID_CONTROL = 0x42,
  DWG_TYPE_APPID = 0x43,
  DWG_TYPE_DIMSTYLE_CONTROL = 0x44,
  DWG_TYPE_DIMSTYLE = 0x45,
  DWG_TYPE_VPORT_ENTITY_CONTROL = 0x46,
  DWG_TYPE_VPORT_ENTITY_HEADER = 0x47,
  DWG_TYPE_GROUP = 0x48,
  DWG_TYPE_MLINESTYLE = 0x49,
  DWG_TYPE_OLE2FRAME = 0x4a,
  DWG_TYPE_DUMMY = 0x4b,
  DWG_TYPE_LONG_TRANSACTION = 0x4c,
  DWG_TYPE_LWPOLYLINE = 0x4d, /* ?? */
  DWG_TYPE_HATCH = 0x4e,
  DWG_TYPE_XRECORD = 0x4f,
  DWG_TYPE_PLACEHOLDER = 0x50,
  DWG_TYPE_VBA_PROJECT = 0x51,
  DWG_TYPE_LAYOUT = 0x52,

  DWG_TYPE_PROXY_ENTITY = 0x1f2, /* 498 */
  DWG_TYPE_PROXY_OBJECT = 0x1f3, /* 499 */

  /* non-fixed types > 500. not stored as type, but as fixedtype */

  DWG_TYPE_ACDSRECORD = 0x1ff + 1,
  DWG_TYPE_ACDSSCHEMA,
  DWG_TYPE_ACMECOMMANDHISTORY,
  DWG_TYPE_ACMESCOPE,
  DWG_TYPE_ACMESTATEMGR,
  DWG_TYPE_ACSH_BOX_CLASS,
  DWG_TYPE_ACSH_EXTRUSION_CLASS,
  DWG_TYPE_ACSH_HISTORY_CLASS,
  DWG_TYPE_ACSH_REVOLVE_CLASS,
  DWG_TYPE_ACSH_SPHERE_CLASS,
  DWG_TYPE_ACSH_SWEEP_CLASS,
  DWG_TYPE_ANNOTSCALEOBJECTCONTEXTDATA,
  DWG_TYPE_ARCALIGNEDTEXT,
  DWG_TYPE_ARC_DIMENSION,
  DWG_TYPE_ASSOC2DCONSTRAINTGROUP,
  DWG_TYPE_ASSOCACTION,
  DWG_TYPE_ASSOCALIGNEDDIMACTIONBODY,
  DWG_TYPE_ASSOCDEPENDENCY,
  DWG_TYPE_ASSOCGEOMDEPENDENCY,
  DWG_TYPE_ASSOCNETWORK,
  DWG_TYPE_ASSOCOSNAPPOINTREFACTIONPARAM,
  DWG_TYPE_ASSOCPERSSUBENTMANAGER,
  DWG_TYPE_ASSOCPLANESURFACEACTIONBODY,
  DWG_TYPE_ASSOCVERTEXACTIONPARAM,
  DWG_TYPE_CAMERA,
  DWG_TYPE_CELLSTYLEMAP,
  DWG_TYPE_DATALINK,
  DWG_TYPE_DATATABLE,
  DWG_TYPE_DBCOLOR,
  DWG_TYPE_DETAILVIEWSTYLE,
  DWG_TYPE_DICTIONARYVAR,
  DWG_TYPE_DICTIONARYWDFLT,
  DWG_TYPE_DIMASSOC,
  DWG_TYPE_DOCUMENTOPTIONS,
  DWG_TYPE_DYNAMICBLOCKPURGEPREVENTER,
  DWG_TYPE_EVALUATION_GRAPH,
  DWG_TYPE_FIELD,
  DWG_TYPE_FIELDLIST,
  DWG_TYPE_GEODATA,
  DWG_TYPE_GEOMAPIMAGE,
  DWG_TYPE_GEOPOSITIONMARKER,
  DWG_TYPE_HELIX,
  DWG_TYPE_IDBUFFER,
  DWG_TYPE_IMAGE,
  DWG_TYPE_IMAGEDEF,
  DWG_TYPE_IMAGEDEF_REACTOR,
  DWG_TYPE_LAYER_FILTER,
  DWG_TYPE_LAYER_INDEX,
  DWG_TYPE_LAYOUTPRINTCONFIG,
  DWG_TYPE_LIGHT,
  DWG_TYPE_LIGHTLIST,
  DWG_TYPE_MATERIAL,
  DWG_TYPE_MESH,
  DWG_TYPE_MLEADERSTYLE,
  DWG_TYPE_MULTILEADER,
  DWG_TYPE_NAVISWORKSMODELDEF,
  DWG_TYPE_NPOCOLLECTION,
  DWG_TYPE_OBJECTCONTEXTDATA,
  DWG_TYPE_OBJECT_PTR,
  DWG_TYPE_PERSSUBENTMANAGER,
  DWG_TYPE_PLOTSETTINGS,
  DWG_TYPE_POINTCLOUD,
  DWG_TYPE_RASTERVARIABLES,
  DWG_TYPE_RENDERENVIRONMENT,
  DWG_TYPE_RENDERGLOBAL,
  DWG_TYPE_MENTALRAYRENDERSETTINGS,
  DWG_TYPE_RAPIDRTRENDERENVIRONMENT,
  DWG_TYPE_RAPIDRTRENDERSETTINGS,
  DWG_TYPE_RTEXT,
  DWG_TYPE_SCALE,
  DWG_TYPE_SECTIONVIEWSTYLE,
  DWG_TYPE_SORTENTSTABLE,
  DWG_TYPE_SPATIAL_FILTER,
  DWG_TYPE_SPATIAL_INDEX,
  DWG_TYPE_SUN,
  DWG_TYPE_SUNSTUDY,
  DWG_TYPE_SURFACE, // or seperate subtypes? DXF has only SURFACE
  DWG_TYPE_PLANESURFACE,
  DWG_TYPE_EXTRUDEDSURFACE,
  DWG_TYPE_LOFTEDSURFACE,
  DWG_TYPE_REVOLVEDSURFACE,
  DWG_TYPE_SWEPTSURFACE,
  DWG_TYPE_TABLE,
  DWG_TYPE_TABLECONTENT,
  DWG_TYPE_TABLEGEOMETRY,
  DWG_TYPE_TABLESTYLE,
  DWG_TYPE_UNDERLAY, /* not separate DGN,DWF,PDF types */
  DWG_TYPE_UNDERLAYDEFINITION, /* not separate DGN,DWF,PDF types */
  DWG_TYPE_VISUALSTYLE,
  DWG_TYPE_WIPEOUT,
  DWG_TYPE_WIPEOUTVARIABLES,
  DWG_TYPE_XREFPANELOBJECT,
  // after 1.0 add new types here for binary compat

  DWG_TYPE_FREED       = 0xfffd,
  DWG_TYPE_UNKNOWN_ENT = 0xfffe,
  DWG_TYPE_UNKNOWN_OBJ = 0xffff,
} Dwg_Object_Type;

通用实体属性

typedef struct _dwg_object_entity
{
  BITCODE_BL objid; /*
  union
  {
    Dwg_Entity_UNUSED *UNUSED;
    Dwg_Entity_TEXT *TEXT;
    Dwg_Entity_ATTRIB *ATTRIB;
    Dwg_Entity_ATTDEF *ATTDEF;
    Dwg_Entity_BLOCK *BLOCK;
    Dwg_Entity_ENDBLK *ENDBLK;
    Dwg_Entity_SEQEND *SEQEND;
    Dwg_Entity_INSERT *INSERT;
    Dwg_Entity_MINSERT *MINSERT;
    Dwg_Entity_VERTEX_2D *VERTEX_2D;
    Dwg_Entity_VERTEX_3D *VERTEX_3D;
    Dwg_Entity_VERTEX_MESH *VERTEX_MESH;
    Dwg_Entity_VERTEX_PFACE *VERTEX_PFACE;
    Dwg_Entity_VERTEX_PFACE_FACE *VERTEX_PFACE_FACE;
    Dwg_Entity_POLYLINE_2D *POLYLINE_2D;
    Dwg_Entity_POLYLINE_3D *POLYLINE_3D;
    Dwg_Entity_ARC *ARC;
    Dwg_Entity_CIRCLE *CIRCLE;
    Dwg_Entity_LINE *LINE;
    Dwg_DIMENSION_common *DIMENSION_common;
    Dwg_Entity_DIMENSION_ORDINATE *DIMENSION_ORDINATE;
    Dwg_Entity_DIMENSION_LINEAR *DIMENSION_LINEAR;
    Dwg_Entity_DIMENSION_ALIGNED *DIMENSION_ALIGNED;
    Dwg_Entity_DIMENSION_ANG3PT *DIMENSION_ANG3PT;
    Dwg_Entity_DIMENSION_ANG2LN *DIMENSION_ANG2LN;
    Dwg_Entity_DIMENSION_RADIUS *DIMENSION_RADIUS;
    Dwg_Entity_DIMENSION_DIAMETER *DIMENSION_DIAMETER;
    Dwg_Entity_POINT *POINT;
    Dwg_Entity__3DFACE *_3DFACE;
    Dwg_Entity_POLYLINE_PFACE *POLYLINE_PFACE;
    Dwg_Entity_POLYLINE_MESH *POLYLINE_MESH;
    Dwg_Entity_SOLID *SOLID;
    Dwg_Entity_TRACE *TRACE;
    Dwg_Entity_SHAPE *SHAPE;
    Dwg_Entity_VIEWPORT *VIEWPORT;
    Dwg_Entity_ELLIPSE *ELLIPSE;
    Dwg_Entity_SPLINE *SPLINE;
    Dwg_Entity_3DSOLID *_3DSOLID;
    Dwg_Entity_REGION *REGION;
    Dwg_Entity_BODY *BODY;
    Dwg_Entity_RAY *RAY;
    Dwg_Entity_XLINE *XLINE;
    Dwg_Entity_OLEFRAME *OLEFRAME;
    Dwg_Entity_MTEXT *MTEXT;
    Dwg_Entity_LEADER *LEADER;
    Dwg_Entity_TOLERANCE *TOLERANCE;
    Dwg_Entity_MLINE *MLINE;
    Dwg_Entity_OLE2FRAME *OLE2FRAME;
    Dwg_Entity_HATCH *HATCH;

    Dwg_Entity_CAMERA *CAMERA;
    Dwg_Entity_GEOPOSITIONMARKER *GEOPOSITIONMARKER;
    Dwg_Entity_HELIX *HELIX;
    Dwg_Entity_IMAGE *IMAGE;
    Dwg_Entity_LIGHT *LIGHT;
    Dwg_Entity_LWPOLYLINE *LWPOLYLINE;
    Dwg_Entity_MULTILEADER *MULTILEADER;
    Dwg_Entity_PROXY_ENTITY *PROXY_ENTITY;
    Dwg_Entity_PROXY_LWPOLYLINE *PROXY_LWPOLYLINE;
    Dwg_Entity_SURFACE *SURFACE;
    Dwg_Entity_PLANESURFACE *PLANESURFACE;
    Dwg_Entity_EXTRUDEDSURFACE *EXTRUDEDSURFACE;
    Dwg_Entity_LOFTEDSURFACE *LOFTEDSURFACE;
    Dwg_Entity_REVOLVEDSURFACE *REVOLVEDSURFACE;
    Dwg_Entity_SWEPTSURFACE *SWEPTSURFACE;
    Dwg_Entity_TABLE *TABLE;
    Dwg_Entity_UNDERLAY *UNDERLAY;
    Dwg_Entity_WIPEOUT *WIPEOUT;
    Dwg_Entity_ARC_DIMENSION *ARC_DIMENSION;
    Dwg_Entity_MESH *MESH;

    Dwg_Entity_UNKNOWN_ENT *UNKNOWN_ENT;
  } tio;

实体

/**
 2D POLYLINE (15) entity
 */
typedef struct _dwg_entity_POLYLINE_2D
{
  COMMON_ENTITY_POLYLINE;

  BITCODE_BS flag;
  BITCODE_BS curve_type;
  BITCODE_BD start_width;
  BITCODE_BD end_width;
  BITCODE_BT thickness;
  BITCODE_BD elevation;
  BITCODE_BE extrusion;
} Dwg_Entity_POLYLINE_2D;
/**
 ARC (17) entity
 */
typedef struct _dwg_entity_ARC
{
  struct _dwg_object_entity *parent;

  BITCODE_3BD center;
  BITCODE_BD radius;
  BITCODE_BT thickness;
  BITCODE_BE extrusion;
  BITCODE_BD start_angle;
  BITCODE_BD end_angle;
} Dwg_Entity_ARC;

/**
 CIRCLE (18) entity
 */
typedef struct _dwg_entity_CIRCLE
{
  struct _dwg_object_entity *parent;

  BITCODE_3BD center;
  BITCODE_BD radius;
  BITCODE_BT thickness;
  BITCODE_BE extrusion;
} Dwg_Entity_CIRCLE;

/**
 LINE (19) entity
 */
typedef struct _dwg_entity_LINE
{
  struct _dwg_object_entity *parent;

  BITCODE_RC z_is_zero;
  BITCODE_3BD start;
  BITCODE_3BD end;
  BITCODE_BT thickness;
  BITCODE_BE extrusion;
} Dwg_Entity_LINE;

你可能感兴趣的:(C语言,C++,Qt)