基于C#编程语言的CAD二次开发技术---展测量点坐标

一、引言

      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();
        }
    }
}

 

转载于:https://www.cnblogs.com/zhaoyangyucaiyun/p/5876060.html

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值