程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> 關於VC++ >> 用CabLib創建DXF(繪圖交換格式)文件

用CabLib創建DXF(繪圖交換格式)文件

編輯:關於VC++

介紹

什麼是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格式說明,具體可以在下面網址找到參考:

http://www.autodesk.com/techpubs/autocad/acad2000/dxf/dxf_format.htm

http://www.martinreddy.net/gfx/3d/DXF12.spec

這裡的類是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文件最好的商業軟件,但它是開源的,可以自由修改代碼。

本文配套源碼

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved