天天看點

AutoCAD *.dxf檔案解析,使用dxflib搞定polyline/spline/ellipse等複雜圖形(★firecat推薦★)

一、簡介

DXF官方幫助:

https://help.autodesk.com/view/OARX/2019/ENU/

網絡可以搜尋到非常多的dxf解析部落格,但是幾乎沒有人能完整地實作polyline/spline/ellipse等複雜圖形的解析和繪制。

因為dxf的解析較為複雜,涉及的元素很多,例如block解析,圖形平移,縮放,參照系坐标等等,另外還有polyline/spline/ellipse等複雜圖形,而polyline又存在凸度的問題需要解決。

dxflib開源庫實作了dxf檔案的解析。所有的圖元解析完後,都會通過DL_CreationAdapter的虛函數接口回調,是以我們要繼承這個類,重寫想繪制的圖元的方法,比如直線對應的就是virtual void addLine(const DL_LineData&); 這個DL_LineData結構體資料儲存的就是我們要繪制直線的資料。

本人使用dxflib開源庫,連續奮戰,克服重重困難,終于實作了以下元素的解析和繪制:

元素:point、line、circle、arc、polyline、spline、ellipse、text

其他:layer、block、insert、min、max

繪制圖形,使用了opencv,把dxf轉化成為png檔案。以下舉例說明:

圖1:涵蓋了上述的所有元素

AutoCAD *.dxf檔案解析,使用dxflib搞定polyline/spline/ellipse等複雜圖形(★firecat推薦★)
圖2:橢圓和樣條曲線
AutoCAD *.dxf檔案解析,使用dxflib搞定polyline/spline/ellipse等複雜圖形(★firecat推薦★)
圖3:polyLine多線實體,注意四個角的倒角圓弧,就是凸度計算得來
AutoCAD *.dxf檔案解析,使用dxflib搞定polyline/spline/ellipse等複雜圖形(★firecat推薦★)
圖4:圖形旋轉平移縮放的經典案例
AutoCAD *.dxf檔案解析,使用dxflib搞定polyline/spline/ellipse等複雜圖形(★firecat推薦★)
二、DXF Spline的格式說明

Group codes Description 
100
 Subclass marker (AcDbSpline) 
 
210
 Normal vector (omitted if the spline is nonplanar)
DXF: X value; APP: 3D vector
 
220, 230
 DXF: Y and Z values of normal vector
 
70
 Spline flag (bit coded):
1 = Closed spline
2 = Periodic spline
4 = Rational spline
8 = Planar
16 = Linear (planar bit is also set) 
 
71
 Degree of the spline curve
 
72
 Number of knots
 
73
 Number of control points
 
74
 Number of fit points (if any)
 
42
 Knot tolerance (default = 0.0000001)
 
43
 Control-point tolerance (default = 0.0000001)
 
44
 Fit tolerance (default = 0.0000000001)
 
12
 Start tangent--may be omitted (in WCS).
DXF: X value; APP: 3D point.
 
22, 32
 DXF: Y and Z values of start tangent--may be omitted (in WCS).
 
13
 End tangent--may be omitted (in WCS).
DXF: X value; APP: 3D point.
 
23, 33
 DXF: Y and Z values of end tangent--may be omitted (in WCS)
 
40
 Knot value (one entry per knot)
 
41
 Weight (if not 1); with multiple group pairs, are present if all are not 1
 
10
 Control points (in WCS) one entry per control point.
DXF: X value; APP: 3D point
 
20, 30
 DXF: Y and Z values of control points (in WCS) (one entry per control point)
 
11
 Fit points (in WCS) one entry per fit point.
DXF: X value; APP: 3D point
 
21, 31
 DXF: Y and Z values of fit points (in WCS) (one entry per fit point)
 
----------------------------------------中文說明:---------------------------------
樣條曲線組碼
 
組碼
 說明
 
100
 子類标記 (AcDbSpline) 
 
210
 法向矢量(如果樣條曲線為非平面型,則省略)
 
DXF:X 值;APP:三維矢量
 
220, 230
 DXF:法向矢量的 Y 值和 Z 值(可選)
 
70
 樣條曲線标志(按位編碼):
 
1 = 閉合樣條曲線
 
2 = 周期樣條曲線
 
4 = 有理樣條曲線
 
8 = 平面
 
16 = 線性(同時設定平面位) 
 
71
 樣條曲線的階數
 
72
 節點數
 
73
 控制點數
 
74
 拟合點數(如果有)
 
42
 節點公差(預設值 = 0.0000001)
 
43
 控制點公差(預設值 = 0.0000001)
 
44
 拟合公差(預設值 = 0.0000000001)
 
12
 起點切向 — 可以省略(在 WCS 中)
 
DXF:X 值;APP:三維點
 
22, 32
 DXF:起點切向的 Y 值和 Z 值 — 可以省略(在 WCS 中)
 
13
 端點切向 — 可以省略(在 WCS 中)
 
DXF:X 值;APP:三維點
 
23, 33
 DXF:端點切向的 Y 值和 Z 值 — 可以省略(在 WCS 中)
 
40
 節點值(每個節點一個條目)
 
41
 權值(如果不為 1);對于多組對,如果均不為 1,則出現。
 
10
 控制點(在 WCS 中);每個控制點一個條目
 
DXF:X 值;APP:三維點
 
20, 30
 DXF:控制點的 Y 值和 Z 值(在 WCS 中);每個控制點一個條目
 
11
 拟合點(在 WCS 中);每個拟合點一個條目
 
DXF:X 值;APP:三維點
 
21, 31
 DXF:拟合點的 Y 值和 Z 值(在 WCS 中);每個拟合點一個條目

      

繼續閱讀