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

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.kler.cn/a/327125.html

相关文章:

  • 汽车IVI中控开发入门及进阶(四十):FDK AAC音频编解码软件库
  • GMSSL的不同python版本
  • 【信号滤波 (上)】傅里叶变换和滤波算法去除ADC采样中的噪声(Matlab/C++)
  • 如何解决vscode powershell乱码
  • Linux(Ubuntu)命令大全——已分类整理,学习、查看更加方便直观!(2024年最新编制)
  • 用人话讲计算机:Python篇!(十五)迭代器、生成器、装饰器
  • mobile_aloha训练过程中pycharm编辑器遇到的问题记录
  • 拿下奇怪的前端报错:SyntaxError: Unexpected token ‘??=‘或‘xxx‘ - 浅谈Nodejs版本过高过低的部分问题
  • ping香港服务器超时的原因通常有哪些?
  • 【笔记】电子绕核运动的轨道半径求解
  • 详解zookeeper四字命令
  • 大数据的挑战是小文件
  • 配音软件哪个好?配音小白用这些
  • Java入门(基础,常见API,JVM,JUC并发编程)
  • NVIDIA 的 Blackwell 架构:解析 B100、B200 和 GB200
  • ndb9300public-ndb2excel简介
  • Spring Boot 自动装配机制实战与业务案例
  • 随时随地点餐:Spring Boot 点餐系统
  • 数通 1
  • php如何实现局部替换功能
  • VS2022 Git功能的使用
  • Visual Studio代码编辑快捷键
  • 计算机视觉学习---图像增强
  • ‌Excel VBA进行间比法设计
  • golang 反射的介绍和使用
  • 完美解决Ubuntu下vi编辑器方向键变字母的问题