基於C#編程語言的CAD二次開發技術---展測量點座標

1、引言數據庫

      AutoCAD 是目前世界上功能最強大的繪圖軟件。在測繪行業,使用 AutoCAD 直接繪圖,或用以 AutoCAD 爲平臺開發出的各類繪圖軟件來繪圖,大大提升了繪圖的精度、準度和速度。今天介紹一下如何用C#編寫將野外測量點座標展入到 AutoCAD 的.NET程序集。編程

2、知識準備編程語言

     一、瞭解dat座標文件的格式,本次以經常使用格式 「 點名,編碼,東座標,北座標,高程 」  爲例。編碼

     二、文件讀取,字符串處理spa

     三、AutoCAD .NET 開發基礎3d

3、須要注意的幾點code

      一、測圖比例尺(本代碼沒有設置繪圖比例尺功能)。orm

      二、測量座標系與 AutoCAD 座標系的區別:座標軸向、角度旋轉方向。對象

      三、順便提一下,在計算機編程中,角度都是弧度制的,然而測量經常使用的是角度制,趕上角度時,就不可避免會出現角度與弧度的互換。寫轉換代碼時,務必要注意編程語言的精度,避免出現精度損失。blog

4、效果圖

 

 

 

5、附源代碼,但願你們多多給予指點!!!

 

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();
        }
    }
}
相關文章
相關標籤/搜索