基于C#的AutoCAD二次开发之展高程点(txt、dat等文件)
我的开发环境为Visual Studio 2017 & AutoCAD 2014 & AutoCAD 2020。之前的一篇文章已经介绍过AutoCAD中基础图形的创建方法,今天给大家说下如何在AutoCAD中展高程点。高程点文件存储格式与CASS中读取的DAT格式一致,示例:【1,ZDH ,450000.000,4100000,20002,DYG,450000.000,4100000,2000 】其中,“1、2”代表的是“点号”,“ZDH、DYG”代表的是“代码”之后的分别是“东坐标、北坐标、高程值”即“Y、X、H ”或者是“点号,Y,X,H ”(不含引号)
- 数据与效果预览
数据准备
展点效果预览1
展点效果预览2
- 类库引用
using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using acad = Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
- 实现代码
[CommandMethod("showLevelPoint")]
public void showLevelPointDemo()
{
//打开高程点文件
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "文本文件|*.*|高程文件|*.dat|所有文件|*.*";
if (ofd.ShowDialog() != DialogResult.OK)
return;
//创建三维点集,用于存储读取高程点文件创建的高程点
Point3dCollection pc = new Point3dCollection();
//创建字符集合,用于存储点号名称
List<string> ptNameList = new List<string>();
string strFilePath = ofd.FileName;
StreamReader sr = new StreamReader(strFilePath);
string strLine;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;//获取当前的活动文档
//开始读取高程点文件
while ((strLine = sr.ReadLine()) != null)
{
try
{
//文件中每行以‘,’好进行分割
string[] strArray = strLine.Split(',');
if (strArray == null || strArray.GetLength(0) != 5)
continue;
/*
//用于展经纬度坐标点(点号,,x,y,z)
double x = Convert.ToDouble(strArray[2]);//x坐标数字对应第三列元组
double y = Convert.ToDouble(strArray[3]);//y坐标数字对应第四列元组
double z = Convert.ToDouble(strArray[4]);//z坐标数字对应第五列元组
*/
//用于投影后的坐标点(点号,,y,x,z)
double x = Convert.ToDouble(strArray[3]);//x坐标数字对应第四列元组
double y = Convert.ToDouble(strArray[2]);//y坐标数字对应第三列元组
double z = Convert.ToDouble(strArray[4]);//z坐标数字对应第五列元组
Point3d pt = new Point3d(y, x, z);
pc.Add(pt);
ed.WriteMessage(strLine + "\n");//在cad命令行中,输出展点信息
ptNameList.Add(strArray[0]);
}
catch (System.Exception e)
{
break;
}
}
sr.Close();
if (pc.Count == 0)
return;
Database db = HostApplicationServices.WorkingDatabase;
//使用事务的方式,创建对象
Transaction trans = db.TransactionManager.StartTransaction();
//获取块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
//获取记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
//将创建的点集和标注添加到地图文档中
for (int ii = 0; ii < pc.Count; ii++)
{
Point3d pt3d = pc[ii];
DBPoint dbpt = new DBPoint(pt3d);
dbpt.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(255, 0, 0);
btr.AppendEntity(dbpt);//添加读取的高程点
trans.AddNewlyCreatedDBObject(dbpt, true);
Vector3d offPt = new Vector3d(0.01, 0.01, 0);
DBText text = new DBText();
text.TextString = ptNameList[ii];
text.Position = pt3d + offPt;
text.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(0, 255, 0);
text.Height = 0.5;
btr.AppendEntity(text);/添加读取的高程点名称
trans.AddNewlyCreatedDBObject(text, true);
}
pc.Clear();
//提交事务
trans.Commit();
}
- 提示
如果展点后,对点样式不满意的话,可以在AutoCAD的命令行输入“PTYPE”,调整点的样式与大小。如果X、Y是反着的话,在代码里调整下读取序号就可以了。