锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / 中级autocad格式处理开源项目CadLib
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft
头条号:软件技术及人才和养生
养生QQ群:122015030

CadLib


背景


中级autocad格式处理开源项目非常难找,经常找到的是大型的,大型的非常难移植,而程序有时候需要格式加载和显示一些中小的图形,用大型的开源项目不合适,这里推荐我找到的CadLib,对图元面向对象设计有所理解的朋友都能够掌握,有需要代码请联系锐英源,手机13803810136,微信同号。

 

介绍

什么是DXF?

图形交换格式 (DXF) 文件允许在 AutoCAD 和其他程序之间交换图形。DXF 文件可以是 ASCII 或二进制格式。因为 ASCII DXF 文件比二进制格式更常见,所以 CadLib 使用 ASCII DXF 格式。

什么是 CadLib?

CadLib 不是计算机辅助设计 (CAD) 程序。它是用于创建 CAD 程序中使用的 DXF 文件的工具。它由两部分组成。其中之一是用于创建 DXF 文件的动态链接库。另一部分是编程接口。它是一个集成了cadio.dll 函数的类。它可以在 Microsoft Visual C++ 项目中使用。此外, cadio.dll可以在其他 Win32 程序中使用。

为什么要使用 CadLib?

在某些程序中,需要创建用于其他程序(例如 AutoCad)的绘图输出。例如,在“Building Detail Sheet Generator Program”中,该程序需要创建图纸输出。用于传达绘图数据的最标准格式是 DXF。


Introduction

What is DXF?

Drawing Interchange Format (DXF) files enable the interchange of drawings between AutoCAD and other programs. DXF files can be either ASCII or binary formats. Because ASCII DXF files are more common than the binary format, CadLib uses ASCII DXF format.

What is CadLib?

The CadLib is not a Computer Aided Design (CAD) program. It is a tool for creating DXF files that are used in the CAD programs. It consists of two parts. One of them is a Dynamic Link Library to create the DXF file. The other part is the programming interface. It is a class that integrates the cadio.dll functions. It can be used in Microsoft Visual C++ projects. In addition, the cadio.dll can be used in other Win32 programs.

Why use CadLib?

In some programs, it is needed to create a drawing output for use in other programs such as AutoCad. For example, in a "Building Detail Sheet Generator Program", the program needs to create a drawing output. And the most standard format for communicating drawing data is DXF.

DXF 文件结构

DXF 格式是图形文件中包含的所有信息的标记数据表示。标记数据意味着文件中的每个数据元素前面都有一个整数,称为组码。组代码的值指示跟随的数据元素的类型。此值还指示给定对象(或记录)类型的数据元素的含义。图形文件中几乎所有用户指定的信息都可以用 DXF 格式表示。(来自 AutoCad 的 DXF 参考)

DXF 文件由一些部分组成。每个部分本身都有一些绘图数据。CadLib 使用以下部分:

  1. 标题
  2. 积木
  3. 实体

用于 CadLib 的 DXF 文件结构的主要参考是 AutoCad 的 DXF 参考。您可以在此处找到有关 DXF 文件结构的更多信息 。

 

DXF file structure

The DXF format is a tagged data representation of all the information contained in a drawing file. Tagged data means that each data element in the file is preceded by an integer number that is called a group code. A group code's value indicates what type of data element follows. This value also indicates the meaning of a data element for a given object (or record) type. Virtually all user-specified information in a drawing file can be represented in DXF format. (from AutoCad's DXF reference)

A DXF file consists of some sections. Each section has some drawing data in itself. The CadLib uses the following sections:

  1. HEADER
  2. TABLES
  3. BLOCKS
  4. ENTITIES

The main reference for DXF file structure that is used for CadLib is the AutoCad's DXF reference. You can find more information about DXF file structure here.

Classes类

The classes are interfaces between CadIO.dll and the main program. "Test" has come with CadLib to demonstrate how to generate a DXF file with CDxfFileWrite and CDrawing classes.这些类是CadIO.dll和主程序之间的接口。CadLib 附带了“Test”项目,用于演示如何使用CDxfFileWrite和CDrawing类生成 DXF 文件 。Test里是动态加载dll,此点要注意。

CDxfFileWrite class

CDxfFileWrite gathers all the commands needed to directly create a DXF file. Usage of CDxfFileWrite is as follows:

  1. Create the DXF file
    CDxfFileWrite dxffile;
    dxffile.Create( "d:\\test.dxf" );
  2. Begin and end the HEADER section. It's here for compatibility with some CAD programs. Others work without having HEADER section.
    // Header Section ------------------------------------------
    dxffile.BeginSection(SEC_HEADER);
    dxffile.EndSection();
    // close HEADER section ------------------------------------
  3. Begin the TABLES section and put the LAYER, LTYPE, STYLE, DIMSTYLE table-types as many as you want and then close the section
    // Tables Section ------------------------------------------
    dxffile.BeginSection(SEC_TABLES);
    
    // LTYPE table type -------------------------
    dxffile.BeginTableType(TAB_LTYPE);
    
    DXFLTYPE ltype;
    double elem[4];
    
    // Continuous
    ZeroMemory(<ype, sizeof(ltype));
    ltype.Name = "Continuous";
    ltype.DescriptiveText = "Solid line";
    dxffile.AddLinetype(<ype);
    
    // DASHDOT2
    ZeroMemory(<ype, sizeof(ltype));
    ltype.Name = "DASHDOT2";
    ltype.DescriptiveText = "Dash dot (.5x) _._._._._._._._._._._._._._._.";
    ltype.ElementsNumber = 4;
    ltype.TotalPatternLength = 0.5;
    ltype.Elements = elem;
    elem[0] = 0.25;
    elem[1] = -0.125;
    elem[2] = 0.0;
    elem[3] = -0.125;
    dxffile.AddLinetype(<ype);
    
    dxffile.EndTableType();
    // close LTYPE table type -------------------
    
    // LAYER table type -------------------------
    result &= dxffile.BeginTableType(TAB_LAYER);
    result &= dxffile.AddLayer("Layer1", 1, "Continuous");
    result &= dxffile.AddLayer("Layer2", 2, "Continuous");
    result &= dxffile.AddLayer("Layer3", 3, "Continuous");
    result &= dxffile.AddLayer("Layer4", 4, "Continuous");
    result &= dxffile.EndTableType();
    // close LAYER table type -------------------
    
    // STYLE table type -------------------------
    dxffile.BeginTableType(TAB_STYLE);
    
    DXFSTYLE tstyle;
    ZeroMemory(&tstyle, sizeof(tstyle));
    tstyle.Name = "Style1";
    tstyle.PrimaryFontFilename = "TIMES.TTF";
    tstyle.Height = 0.3;
    tstyle.WidthFactor = 1;
    dxffile.AddTextStyle(&tstyle);
    
    dxffile.EndTableType();
    // close STYLE table type -------------------
    
    // DIMSTYLE table type ----------------------
    dxffile.BeginTableType(TAB_DIMSTYLE);
    
    DXFDIMSTYLE dimstyle;
    
    // DIM1
    ZeroMemory(&dimstyle, sizeof(dimstyle));
    dimstyle.Name = "DIM1"; // DimStyle Name
    dimstyle.DIMCLRD = 2; // Dimension line & Arrow heads color
    dimstyle.DIMDLE = 0.0000; // Dimension line size after Extensionline
    dimstyle.DIMCLRE = 2; // Extension line color
    dimstyle.DIMEXE = 0.1800; // Extension line size after Dimline
    dimstyle.DIMEXO = 0.0625; // Offset from origin
    dimstyle.DIMBLK1 = "ClosedFilled";// 1st Arrow head
    dimstyle.DIMBLK2 = "ClosedFilled";// 2nd Arrow head
    dimstyle.DIMASZ = 0.1800; // Arrow size
    dimstyle.DIMTXSTY = "Style1"; // Text style
    dimstyle.DIMCLRT = 3; // Text color
    dimstyle.DIMTXT = 0.1800; // Text height
    dimstyle.DIMTAD = 1; // Vertical Text Placement
    dimstyle.DIMGAP = 0.0900; // Offset from dimension line
    dxffile.AddDimStyle(&dimstyle);
    
    dxffile.EndTableType();
    // close DIMSTYLE table type ----------------
    
    dxffile.EndSection();
    // close TABLES section ------------------------------------
  4. Begin ENTITIES section and put entities data (LINE, CIRCLE, SOLID, TEXT, ARC, POINT, DIMLINEAR) and finally close the section
    // Entities Section ------------------------------------------
    dxffile.BeginSection(SEC_ENTITIES);
    
    // set current layer to Layer2
    dxffile.SetCurrentLayer("Layer2");
    // draw a line
    dxffile.Line(1.2, 3.3, 7.5, 7.7);
    // draw a circle
    dxffile.Circle(7.8, 4.3, 1.75);
    // set current layer to Layer4
    dxffile.SetCurrentLayer("Layer4");
    
    // draw a solid
    REALPOINT points[4];
    points[0].x = 10.4; points[0].y = 7.2;
    points[1].x = 13.6; points[1].y = 7.4;
    points[2].x = 13.1; points[2].y = 4.9;
    points[3].x = 10.9; points[3].y = 5.9;
    Solid(4, points);
    
    // set current textstyle to Style1
    dxffile.SetCurrentTextStyle("Style1");
    // draw text
    dxffile.Text("Sample Text", 5.9, 6.7, 0.3, 35);
    // draw a dimension line
    dxffile.SetCurrentDimStyle("DIM1");
    dxffile.DimLinear(6.05, 3, 9.55, 3, 9.55, 2, 0, "3.50");
    
    dxffile.EndSection();
    // close ENTITIES section ----------------------------------
  5. Close the DXF file
    dxffile.Close();

CDrawing class

CDrawing class has all the commands to create a drawing in memory and save it as a DXF file. Usage of CDrawing is as follows:CDrawing类具有在内存中创建图形并将其保存为 DXF 文件的所有命令。用法CDrawing如下:

  1. Create the on-memory drawing
    CDrawing drw;
    drw.Create( );
  2. Create new LAYER, LTYPE, STYLE, DIMSTYLE table-types as many as you want.
    // Tables Section ------------------------------------------
    // LTYPE table type -------------------------
    LTYPE ltype;
    OBJHANDLE objhandle1;
    
    // Continuous
    ZeroMemory(<ype, sizeof(ltype));
    strcpy(ltype.Name, "Continuous");
    strcpy(ltype.DescriptiveText, "Solid line");
    objhandle1 = drw.AddLinetype(<ype);
    
    // DASHDOT2
    ZeroMemory(<ype, sizeof(ltype));
    strcpy(ltype.Name, "DASHDOT2");
    strcpy(ltype.DescriptiveText, 
      "Dash dot (.5x) _._._._._._._._._._._._._._._.");
    ltype.ElementsNumber = 4;
    ltype.PatternLength = 0.5;
    ltype.Elements[0] = 0.25;
    ltype.Elements[1] = -0.125;
    ltype.Elements[2] = 0.0;
    ltype.Elements[3] = -0.125;
    drw.AddLinetype(<ype);
    
    // LAYER table type -------------------------
    LAYER layer;
    
    // Layer1
    ZeroMemory(&layer, sizeof(layer));
    strcpy(layer.Name, "Layer1");
    layer.Color = 1;
    layer.LineTypeObjhandle = objhandle1; // Continuous
    drw.AddLayer(&layer);
    
    // Layer2
    ZeroMemory(&layer, sizeof(layer));
    strcpy(layer.Name, "Layer2");
    layer.Color = 2;
    layer.LineTypeObjhandle = objhandle1; // Continuous
    drw.AddLayer(&layer);
    
    // STYLE table type -------------------------
    STYLE style;
    
    ZeroMemory(&style, sizeof(style));
    strcpy(style.Name, "Style1");
    strcpy(style.PrimaryFontFilename, "TIMES.TTF");
    style.LastHeightUsed = 0.3;
    style.WidthFactor = 1;
    objhandle1 = drw.AddTextStyle(&style);
    
    // DIMSTYLE table type ----------------------
        DIMSTYLE dimstyle;
    
    // DIM1
    ZeroMemory(&dimstyle, sizeof(dimstyle));
    strcpy(dimstyle.Name, "DIM1"); // DimStyle Name
    dimstyle.dimclrd = 2; // Dimension line & Arrow heads color
    dimstyle.dimdle = 0.0000; // Dimension line size after Extensionline
    dimstyle.dimclre = 2; // Extension line color
    dimstyle.dimexe = 0.1800; // Extension line size after Dimline
    dimstyle.dimexo = 0.0625; // Offset from origin
    strcpy(dimstyle.dimblk1, "ClosedFilled");// 1st Arrow head
    strcpy(dimstyle.dimblk2, "ClosedFilled");// 2nd Arrow head
    dimstyle.dimasz = 0.1800; // Arrow size
    dimstyle.dimtxstyObjhandle = objhandle1;// Text style
    dimstyle.dimclrt = 3; // Text color
    dimstyle.dimtxt = 0.1800; // Text height
    dimstyle.dimtad = 1; // Vertical Text Placement
    dimstyle.dimgap = 0.0900; // Offset from dimension line
    drw.AddDimStyle(&dimstyle);
  3. Make entities data (LINE, CIRCLE, SOLID, TEXT, ARC, POINT, DIMLINEAR, POLYLINE).
    // Entities Section ------------------------------------------
    // set current layer to Layer2
    drw.SetLayer("Layer2");
    // draw a line
    drw.Line(1.2, 3.3, 7.5, 7.7);
    // draw a circle
    drw.Circle(7.8, 4.3, 1.75);
    // set current layer to Layer1
    drw.SetLayer("Layer1");
    
    // draw a solid
    REALPOINT points[4];
    points[0].x = 10.4; points[0].y = 7.2;
    points[1].x = 13.6; points[1].y = 7.4;
    points[2].x = 13.1; points[2].y = 4.9;
    points[3].x = 10.9; points[3].y = 5.9;
    drw.Solid(points[0], points[1], points[2], points[3]);
    
    // set current textstyle to Style1
    drw.SetTextStyle("Style1");
    // draw text
    drw.Text("Sample Text", 5.9, 6.7, 0.3, 35);
    // draw a dimension line
    drw.SetDimStyle("DIM1");
    drw.DimLinear(6.05, 3, 9.55, 3, 9.55, 2, 0, "3.50");
  4. Save data to a DXF file.
    drw.SaveDXFFile(DxfFileName);
  5. Destroy CDrawing and free allocated memory.
    drw.Destroy();

Loading data from a DXF file

  1. Create the on-memory drawing.
    CDrawing drw;
    drw.Create( );
  2. Use LoadDXFFile member function to load DXF file into memory.
    drw.LoadDXFFile("Sample.dxf");

That's all!

Conclusion

Since I am a Civil Engineer, I decided to write a program to generate a beam or columns detail sheet without the use of AutoCAD. I have written a program that, with a little data about beam or column, will create the detail sheet automatically. Output of this program is a DXF file and it can be shown in AutoCAD or it can be plotted with it. This program can save the time for drawing the detail sheet with AutoCAD. If you are an AutoCAD operator, you will understand the meaning of words that are used in this article, or if you are a programmer who wants to write a program to create DXF files, first you need a little knowledge about AutoCAD or the drawing programs such as is mentioned above. This code can be useful for programmers who need to create DXF files from their programs. CadLib is not the best one and also there are many commercial software for creating DXF files but they are not open source. Feel free to change the code. Your comments in regards to this article will cause the improvement of CadLib.由于我是一名土木工程师,因此我决定编写一个程序,在不使用 AutoCAD 的情况下生成梁或柱详细信息表。我编写了一个程序,通过一些关于梁或柱的数据,它会自动创建详细信息表。这个程序的输出是一个 DXF 文件,它可以在 AutoCAD 中显示,也可以用它来绘制。这个程序可以节省用AutoCAD绘制细节表的时间。如果您是 AutoCAD 操作员,您会理解本文中使用的词的含义,或者如果您是想编写程序来创建 DXF 文件的程序员,首先您需要对 AutoCAD 或绘图程序有所了解比如上面提到的。此代码对于需要从其程序创建 DXF 文件的程序员非常有用。CadLib 不是最好的,也有许多用于创建 DXF 文件的商业软件,但它们不是开源的。随意更改代码。

友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:A、郑州市芯互联大厦北楼1803A(文化路优胜北路西北角),B、郑州大学北校区院内