一、引言
AutoCAD 是目前世界上功能最強大的繪圖軟件。在測繪行業,使用 AutoCAD 直接繪圖,或用以 AutoCAD 為平台開發出的各種繪圖軟件來繪圖,大大提高了繪圖的精度、准度和速度。今天介紹一下如何用C#編寫將野外測量點坐標展入到 AutoCAD 的.NET程序集。
二、知識准備
1、了解dat坐標文件的格式,本次以常用格式 “ 點名,編碼,東坐標,北坐標,高程 ” 為例。
2、文件讀取,字符串處理
3、AutoCAD .NET 開發基礎
三、需要注意的幾點
1、測圖比例尺(本代碼沒有設置繪圖比例尺功能)。
2、測量坐標系與 AutoCAD 坐標系的區別:坐標軸向、角度旋轉方向。
3、順便提一下,在計算機編程中,角度都是弧度制的,然而測量常用的是角度制,遇上角度時,就不可避免會出現角度與弧度的互換。寫轉換代碼時,務必要注意編程語言的精度,避免出現精度損失。
四、效果圖
五、附源代碼,希望大家多多給予指點!!!
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.Internal; using System.Windows.Forms; using System.IO; namespace CAD展點 { public class Class1 { [CommandMethod("InsertPoint")] public void InsertPoint() { //選擇點文件.dat OpenFileDialog OpenDlg = new OpenFileDialog(); OpenDlg.Title = "點文件.dat"; OpenDlg.Filter = "點文件(*.dat)|*.dat"; ReadDatFile datFile; if (OpenDlg.ShowDialog() == DialogResult.OK) { datFile = new ReadDatFile(OpenDlg.FileName.ToString()); } else { return; } //獲取當前文檔和數據庫 Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; //啟動事務 using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { //以讀模式打開塊表 BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; //以寫模式打開 Block 表記錄 Model 空間 BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; //創建點 try { foreach (var item in datFile.Result) { //點位 using (DBPoint pt = new DBPoint(new Point3d(item.X, item.Y, item.H))) { pt.ColorIndex = 1; //將新對象添加到塊表記錄和事務 acBlkTblRec.AppendEntity(pt); acTrans.AddNewlyCreatedDBObject(pt, true); //釋放DBObject對象 } //點名 using (DBText acText = new DBText()) { acText.Position = new Point3d(item.X+0.25, item.Y-0.375, item.H); acText.ColorIndex = 1; acText.Height = 1; acText.WidthFactor = 0.8; acText.TextString = item.Name; acBlkTblRec.AppendEntity(acText); acTrans.AddNewlyCreatedDBObject(acText, true); //釋放DBObject對象 } } MessageBox.Show("展點完成,共展"+datFile.Result.Count +"個點!"); } catch { MessageBox.Show("展點失敗!"); return; } //清空集合 datFile.Result.Clear(); //更改點樣式 acCurDb.Pdmode = 35; acCurDb.Pdsize = 0.5; //將新對象保存到數據庫 acTrans.Commit(); } } } class 坐標 { public string Name { get; set; } public double X { get; set; } public double Y { get; set; } public double H { get; set; } public 坐標(string name, double x, double y, double h) { Name = name; X = x; Y = y; H = h; } } class ReadDatFile { public List<坐標> Result { get; set; } public ReadDatFile(string file) { Result = new List<坐標>(); FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader m_streamReader = new StreamReader(fs); m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin); string strLine = m_streamReader.ReadLine(); int a1 = strLine.Split(',').Length - 1; // 從數據流中讀取每一行,直到文件的最后一行 坐標 pt; while (strLine != null) { string[] s = strLine.Split(',');//注意此處假設dat中分隔號是半角的逗號 try { pt = new 坐標(s[0], Convert.ToDouble(s[2]), Convert.ToDouble(s[3]), Convert.ToDouble(s[4])); Result.Add(pt); strLine = m_streamReader.ReadLine(); } catch { MessageBox.Show("請檢查dat坐標格式是否為:點名,編碼,東坐標,北坐標,高程"); return; } } fs.Close(); m_streamReader.Close(); } } }