日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#netdxf库读、写、绘制CAD的dxf文件

發布時間:2023/12/10 C# 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#netdxf库读、写、绘制CAD的dxf文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C#netdxf庫讀、寫、繪制CAD的dxf文件

1.netdxf介紹

是.net框架下C#開發的讀寫AutoCAD DXF文件的開源庫。支持AutoCad2000, AutoCad2004, AutoCad2007, AutoCad2010, AutoCad2013, and AutoCad2018 DXF的文本及二進制格式。源碼面向對象開發的程度極高,在AutoDesk的官方說明文檔里幾乎都能找到對應的類,自己做應用開發時遇到問題后首先要從該文檔入手。
源碼地址:https://github.com/haplokuon/netDxf。有了.net core的環境后,找到.sln解決方案文件,打開后可以直接編譯運行。包含兩個項目,一個是netdxf類庫項目,一個測試樣例應用程序項目。由于源碼的面向對象質量很高,應用開發直接學習樣例項目中的編程寫法即可。

2.netdxf案例源碼

源碼片段1(program.cs):

DxfDocument doc = Test(@"sample.dxf");//讀dxf文件HeaderVariable headerVariable;//dxf文件頭// The ExtMin and ExtMax header variables cannot be directly accessed, now they will be added as custom header variables if the DXF has them// they have been deleted since netDxf does not calculate themVector3 extMin;//讀cad自定義全局變量的寫法,全局變量的含義及用途參考CAD官方文件if (doc.DrawingVariables.TryGetCustomVariable("$EXTMIN", out headerVariable)){extMin = (Vector3)headerVariable.Value;}Vector3 extMax;if (doc.DrawingVariables.TryGetCustomVariable("$EXTMAX", out headerVariable)){extMax = (Vector3)headerVariable.Value;}Vector2 limMin;if (doc.DrawingVariables.TryGetCustomVariable("LIMMIN", out headerVariable)){limMin = (Vector2)headerVariable.Value;}Vector2 limMax;if (doc.DrawingVariables.TryGetCustomVariable("LIMMAX", out headerVariable)){limMax = (Vector2)headerVariable.Value;}

源碼片段2:

public static void AddHeaderVariable(){//DxfDocument doc = DxfDocument.Load(@"sample.dxf");//寫dxf文件DxfDocument doc = new DxfDocument();HeaderVariable headerVariable; // The ExtMin and ExtMax header variables cannot be directly accessed, now they will be added as custom header variables if the DXF has them// they have been deleted since netDxf does not calculate themVector3 extMin;if (doc.DrawingVariables.TryGetCustomVariable("$EXTMIN", out headerVariable)){extMin = (Vector3) headerVariable.Value;}Vector3 extMax;if (doc.DrawingVariables.TryGetCustomVariable("$EXTMAX", out headerVariable)){extMax = (Vector3) headerVariable.Value;}// you can try to get a header variable and modify it or create a new one if it does not existsif (doc.DrawingVariables.TryGetCustomVariable("$SPLINESEGS", out headerVariable)){headerVariable.Value = (short) 5; // make sure you pass the correct value type, the code group 70 corresponds to a short}else{doc.DrawingVariables.AddCustomVariable(new HeaderVariable("$SPLINESEGS", 70, (short) 5));} //移除并添加自定義變量的方法,如果沒有已存在該變量,直接添加會報錯// or you can remove a header variable, even if it does not exist and add a new onedoc.DrawingVariables.RemoveCustomVariable("$MEASUREMENT");doc.DrawingVariables.AddCustomVariable(new HeaderVariable("$MEASUREMENT", 70, (short) 0));//保存dxf到指定路徑doc.Save("test.dxf");}

3.應用開發案例

項目需要繪制斷面圖,有跨平臺的需求,開發時間也緊張。故利用c#開發效率高,同時net core支持跨平臺的特點,選用netdxf庫。部分源碼如下(刪減掉部分代碼,僅供參考):

DxfDocument doc = new DxfDocument(DxfVersion.AutoCad2007);AciColor textColor = new AciColor(0, 0, 0);#region Headerdoc.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;doc.DrawingVariables.LUnits = netDxf.Units.LinearUnitType.Architectural;doc.DrawingVariables.LUprec = 8;//hdm是自己寫的類Vector2 vmin = new Vector2(hdm.FrameMinX, hdm.FrameMinY);Vector2 vmax = new Vector2(hdm.FrameMaxX, hdm.FrameMaxY + 0.04);//第一個參數是CAD的全局變量,其含義參考CAD的官方文檔;第二參數是groupid,其值也參考官方文檔HeaderVariable limmin = new HeaderVariable("$LIMMIN", 20, vmin);doc.DrawingVariables.AddCustomVariable(limmin);HeaderVariable limmax = new HeaderVariable("$LIMMAX", 20, vmax);doc.DrawingVariables.AddCustomVariable(limmax);HeaderVariable extmin = new HeaderVariable("$EXTMIN", 20, vmin);doc.DrawingVariables.AddCustomVariable(extmin);HeaderVariable extmax = new HeaderVariable("$EXTMAX", 20, vmax);doc.DrawingVariables.AddCustomVariable(extmax);//全局變量的不同寫法doc.DrawingVariables.LwDisplay = true;//控制顯示線寬,如果不設置會導致線寬不生效//全局比例因子,配合線型對象的比例因子參數使用,否則虛線不生效doc.DrawingVariables.LtScale = 1000.0;#endregion...#region FrameLayer lyrFrame = new Layer("FrameLine"){Color = textColor,IsVisible = true,Linetype = Linetype.Continuous,Lineweight = Lineweight.W40};//以下代碼是繪制四條直線段構成邊框;Line是直線段對象,同CAD一樣還有MLine,Polyline,Rec等都可以用來繪制邊框,這個版本的MLine有bug,在CAD打開后線條顯示有偏移。Vector2 p1 = new Vector2(hdm.FrameMinX, hdm.FrameMaxY);Vector2 p2 = new Vector2(hdm.FrameMaxX, hdm.FrameMaxY);Vector2 p3 = new Vector2(hdm.FrameMaxX, hdm.FrameMinY);Vector2 p4 = new Vector2(hdm.FrameMinX, hdm.FrameMinY);Line fram1 = new Line(p1, p2) { Layer = lyrFrame };doc.Entities.Add(fram1);Line fram2 = new Line(p2, p3) { Layer = lyrFrame };doc.Entities.Add(fram2);Line fram3 = new Line(p3, p4) { Layer = lyrFrame };doc.Entities.Add(fram3);Line fram4 = new Line(p4, p1) { Layer = lyrFrame };doc.Entities.Add(fram4);#endregion...//如果要顯示中文,得用simsun字體,否則會亂碼TextStyle generalTextStyle = new TextStyle("text", "simsun", netDxf.Tables.FontStyle.Regular);AciColor textColor = new AciColor(0, 0, 0);Layer lyrTxt = new Layer("Text"){Color = textColor,IsVisible = true};double fontHeight = 0.006;//字體高度根據自身需要設置,我這里基本單位按照1m的長度搭配string titlestr = zdm.Title;double titlex = zdm.FrameMinX + (zdm.FrameMaxX - zdm.FrameMinX) / 2.0;double titley = zdm.FrameMaxY + 0.01;Text titletext = new Text(titlestr, new Vector2(titlex, titley), 0.01){Layer = lyrTxt,Style = generalTextStyle,Rotation = 0,Alignment = TextAlignment.BaselineCenter};//添加標題文本doc.Entities.Add(titletext);...//畫一條虛線直線段Layer lyrAssistLine = new Layer("dashedLine"){Color = new AciColor(0, 0, 0),IsVisible = true,Linetype = Linetype.Dashed,Lineweight=Lineweight.W13};Vector2 assist1 = new Vector2(centerPosX, ptBottomY);Vector2 assist2 = new Vector2(centerPosX,assitMinY);Line assistline = new Line(assist1, assist2) { Layer = lyrAssistLine, Linetype = Linetype.Dashed,Color=ptcolor , LinetypeScale = 0.00001};doc.Entities.Add(assistline);...// 保存文件,第二個參數true是保存為二進制格式,false是保存為文本格式doc.Save(dxffile, false);

總結

以上是生活随笔為你收集整理的C#netdxf库读、写、绘制CAD的dxf文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。