Chinaunix首页 | 论坛 | 博客
  • 博客访问: 589246
  • 博文数量: 752
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:47
文章分类

全部博文(752)

文章存档

2011年(1)

2008年(751)

我的朋友

分类:

2008-10-13 16:47:51

用CabLib创建DXF(绘图交换格式)文件

作者:Omid Shahabi
译者:eastvc





介绍

什么是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等程序使用的图形文件,例如,在一个"发动机设计程序"中,程序需要输出图形,而最常用的交换数据的图形格式便是DXF。

DXF文件结构
DXF文件格式是图形文件中所有信息的标签数据描述。标签数据指的是文件中的被称为群码的整型数据前的每个数据项。 群码的值指示了下面数据的数据类型以及数据单元的含义。事实上图形文件中所有用户特殊信息都可以描述为DXF格式。(参考AutoCad的DXF参考说明)

DXF文件由许多区域组成,每个区域里有许多图形数据,CadLib可以使用如下区域:
1. 头部 HEADER
2. 表格 TABLES
3. 区块 BLOCKS
4. 实体 ENTITIES

CadLib中的DXF文件结构参考AutoCad中的DXF格式说明,具体可以在下面网址找到参考:



这里的类是CadIO.dll和主程序之间的接口。"Test"演示了如何用CadLib中的CDxfFileWrite和CDrawing类创建DXF文件。

CDxfFileWrite类

CDxfFileWrite封装了直接创建DXF文件的所有命令。使用方法如下:

1. 创建DXF文件

CDxfFileWrite dxffile;
dxffile.Create( "d:\\test.dxf" );
2. HEADER区域的开始与结束。这里是为了兼容CAD程序。其它的不需要HEADER区域。
// Header Section ------------------------------------------
dxffile.BeginSection(SEC_HEADER);
dxffile.EndSection();
// close HEADER section ------------------------------------
3. 开始表格区域并放置LAYER, LTYPE, STYLE, DIMSTYLE等你所需的表格类型,然后结束该区域。
// 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. 开始实体区域并放置LINE, CIRCLE, SOLID, TEXT, ARC, POINT, DIMLINEAR等数据,然后结束该区域。
// 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. 关闭DXF文件
dxffile.Close();

CDrawing 类

CDrawing封装了在内存中创建图形并保存到DXF文件中的功能。使用方法如下:

1. 创建内存绘图

CDrawing drw;
drw.Create();
2. 在内存中创建LAYER, LTYPE, STYLE, DIMSTYLE等所需要的表格类型
// 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. 生成实体数据(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. 将数据保存到DXF文件中
drw.SaveDXFFile(DxfFileName);
5. 删除CDrawing对象并释放内存

从DXF文件中加载数据

1. 创建内存绘图
CDrawing drw;
drw.Create( );

2. 使用LoadDXFFile加载DXF文件到内存
drw.LoadDXFFile("Sample.dxf");

结论

这些代码对于需要在程序中创建DXF文件的程序员来说是很有用的,CadLib虽然不是创建DXF文件最好的商业软件,但它是开源的,可以自由修改代码。
--------------------next---------------------

阅读(732) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~