当前位置: 首页 > news >正文

004集—— txt格式坐标写入cad(CAD—C#二次开发入门)

如图所示原始坐标格式,xy按空格分开,将坐标按顺序在cad中画成多段线:

 坐标xy分开并按行重新输入txt,效果如下:

代码如下 :

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AcTools;
using Autodesk.AutoCAD.Geometry;
using System.IO;namespace Acdemo
{public class Acdemo{private void Addl(Entity ent ) {Database db = HostApplicationServices.WorkingDatabase;using (Transaction tr = db.TransactionManager .StartTransaction ()){BlockTable bt = (BlockTable)tr.GetObject (db.BlockTableId ,OpenMode .ForRead  );BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord .ModelSpace ],OpenMode.ForWrite );btr.AppendEntity (ent);tr.AddNewlyCreatedDBObject(ent,true );tr.Commit ();   }}[CommandMethod("xx")]public void Demo(){Database db = HostApplicationServices.WorkingDatabase;string filename = @"E:\d\8.txt";string filename1 = @"E:\d\999.txt";try{File.Delete(filename1); string contents = File.ReadAllText(filename);List<List<string>> list = new List<List<string>>();Polyline pl = new Polyline();  pl.Closed = true;   pl.ColorIndex = 3;string[] cont = contents.Split(new char[] { ' ', '\n' });double x, y;File.AppendAllLines(filename1, cont);for (int i = 0; i < cont.Length / 2; i++){   x = Convert.ToDouble (cont[2*i]);y = Convert.ToDouble(cont[2*i+1]);pl.AddVertexAt(i, new Point2d(x, y),0.0,0.0,0.0);}Addl (pl);}catch (System.Exception){throw;}}  }
}

其中有个封装函数addl,为封装事务写入实体到数据库的函。

若输入坐标格式有误,则用以下程序,可在命令行提示错误信息:

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AcTools;
using Autodesk.AutoCAD.Geometry;
using System.IO;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;namespace Acdemo
{public class Acdemo{private void Addl(Entity ent ) {Database db = HostApplicationServices.WorkingDatabase;using (Transaction tr = db.TransactionManager .StartTransaction ()){BlockTable bt = (BlockTable)tr.GetObject (db.BlockTableId ,OpenMode .ForRead  );BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord .ModelSpace ],OpenMode.ForWrite );btr.AppendEntity (ent);tr.AddNewlyCreatedDBObject(ent,true );tr.Commit ();   }}[CommandMethod("xx")]public void Demo(){Database db = HostApplicationServices.WorkingDatabase;string filename = @"E:\d\8.txt";string filename1 = @"E:\d\999.txt";try{File.Delete(filename1); string contents = File.ReadAllText(filename);List<List<string>> list = new List<List<string>>();Polyline pl = new Polyline();  pl.Closed = true;   pl.ColorIndex = 3;string[] cont = contents.Split(new char[] { ' ', '\n' });double x, y;File.AppendAllLines(filename1, cont);for (int i = 0; i < cont.Length / 2; i++){   //x = Convert.ToDouble (cont[2*i]);//y = Convert.ToDouble(cont[2*i+1]);bool bx = double.TryParse(cont[2*i], out x);bool by = double.TryParse (cont[2*i + 1],out y);if (bx==false ||  by == false ) {Editor ed = Application .DocumentManager.MdiActiveDocument .Editor ;ed.WriteMessage($"有错误,{cont[2 * i]},{cont[2 * i+1]}");}pl.AddVertexAt(i, new Point2d(x, y),0.0,0.0,0.0);}Addl (pl);}catch (System.Exception){throw;}}  }
}

 

http://www.lryc.cn/news/450638.html

相关文章:

  • CSS中的font-variation-settings:探索字体的可变性
  • 组合优化与凸优化 学习笔记5 对偶拉格朗日函数
  • 监控易监测对象及指标之:Exchange邮件服务器监测
  • 【机器学习基础】Transformer学习
  • mysql如何不使用窗口函数,去统计出入库情况
  • uni-app canvas文本自动换行
  • 【设计模式-职责链】
  • Prompt:在AI时代,提问比答案更有价值
  • whatis命令:关于命令的简短描述
  • ICM20948 DMP代码详解(54)
  • RabbitMQ的应用问题
  • C++14:通过make_index_sequence实现将tuple转换为array
  • Linux中修改MySQL密码
  • 华为OD真题机试-英文输入法(Java)
  • 【React 】入门Day01 —— 从基础概念到实战应用
  • 2024年9月总结及随笔之丢卡
  • sql语法学习 sql各种语法 sql增删改查 数据库各种操作 数据库指令
  • 鸡兔同笼,但是线性代数
  • 01---java面试八股文——springboot---10题
  • 计算机毕业设计 二手图书交易系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 【进阶OpenCV】 (3)--SIFT特征提取
  • HarmonyOS/OpenHarmony Audio 实现音频录制及播放功能
  • css 中 ~ 符号、text-indent、ellipsis、ellipsis-2、text-overflow: ellipsis、::before的使用
  • Activiti 工作流大致了解
  • 速盾:高防 CDN,网站安全的有力保障
  • 宝塔搭建nextcould 30docker搭建onlyoffic8.0
  • 【源码+文档+调试讲解】交通信息管理系统
  • 小阿轩yx-案例:Ansible剧本文件实践
  • 【ShuQiHere】深入理解微架构(Microarchitecture):LC-3 的底层实现 ️
  • Ubuntu24.04.1系统下VideoMamba环境配置