WEB开发网
开发学院软件开发VC 用CabLib创建DXF(绘图交换格式)文件 阅读

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

 2007-03-17 21:58:54 来源:WEB开发网   
核心提示: // Entities Section --dxffile.BeginSection(SEC_ENTITIES);// set current layer to Layer2dxffile.SetCurrentLayer("Layer2");// draw a line
// 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文件最好的商业软件,但它是开源的,可以自由修改代码。

上一页  1 2 3 

Tags:CabLib 创建 DXF

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接