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

devtools/2024/10/4 11:12:34/

如图所示原始坐标格式,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.ppmy.cn/devtools/119739.html

相关文章

Redis: 主从复制故障分析及解决方案

常见故障及解决方案 1 &#xff09;主从数据一致性的问题 说到数据一致性&#xff0c;在主从模式下无非就两两种情况 一种就是主多从少另外一种是主少从多 主多从少 一般就是在网络延迟的情况下从服务器还没有把主服务器的数据全部复制过来这个时候可以等延迟结束以后&#xf…

哈希表和字符串哈希算法

哈希 哈希表&#xff08;Hash Table&#xff09;是一种数据结构&#xff0c;它可以通过一个哈希函数将键&#xff08;key&#xff09;映射到存储位置&#xff0c;从而实现高效的数据查找、插入和删除操作。哈希表的特点是能够在常数时间&#xff08;O(1)&#xff09;内完成查找…

TCP协议

TCP协议全讲解 tcp报头 tcp在传输层有发送缓冲区和接受缓冲区&#xff0c;我们在用户级缓冲区调用的write,read,recv,send等接口&#xff0c;都是将用户级缓冲区的数据拷贝给发送缓冲区。 4位首部长度 计算的时候有基本的大小单位&#xff0c;单位4字节。所以tcp报头的长度大…

《论文阅读》 用于产生移情反应的迭代联想记忆模型 ACL2024

《论文阅读》 用于产生移情反应的迭代联想记忆模型 ACL2024 前言简介任务定义模型架构Encoding Dialogue InformationCapturing Associated InformationPredicting Emotion and Generating Response损失函数问题前言 亲身阅读感受分享,细节画图解释,再也不用担心看不懂论文啦…

为VRoidStudio制作的vrm格式模型制作blendshape

零、效果展示 bs视频演示 一、准备相关插件 1、VRoidStudio&#xff08;免费&#xff09; 下载网址&#xff1a;https://vroid.com/en/studio 2、UniVRM&#xff08;免费&#xff09; 下载网址&#xff1a;https://github.com/vrm-c/UniVRM/releases 注意&#xff1a;unity…

如何使用ChatGPT API及Bito插件

目录 本章整体说明Open AI常用API接口工具&#xff1a;Postman调用API接口演示Java和Python调用Open AI API接口基于ChatGPT-4的代码生成插件Bito使用小练习&#xff1a;3分钟搭建一个自己专属的AI聊天网站 2-1 本章整体说明 本章将详细介绍如何使用ChatGPT API以及Bito插件&…

自动化测试CSS元素定位

1.1 CSS定位 1.1.1 绝对路径定位 目标 查找第一个文本为“猜猜看”的a标签 实现 CSS表达式 html>body>div>a[.”猜猜看”] python表达式 driver.find_element_by_css_selector(‘html>body>div>a[.”猜猜看”]’) 1.1.2 相对路径定位 目标 查找第…

网易云多久更新一次ip属地

‌在数字化时代&#xff0c;网络成为了我们日常生活中不可或缺的一部分。无论是社交娱乐还是工作学习&#xff0c;IP地址作为网络身份的象征&#xff0c;都扮演着重要的角色。对于网易云音乐这样的热门应用来说&#xff0c;IP属地的显示不仅关乎用户体验&#xff0c;也涉及用户…