当前位置: 首页 > 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/news/327125.html

相关文章:

  • 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编辑器方向键变字母的问题
  • 集中式架构和分布式架构
  • 本地安装torch2.3.1,cuda12.1,python3.10
  • java如何查看线程死锁?
  • tesseract:一个.Net版本的开源OCR项目
  • element-plus 日历组件 Calendar设置每周第一天为周一-非国际化版
  • MobaXterm基本使用 -- 服务器状态、批量操作、显示/切换中文字体、修复zsh按键失灵
  • 从0学习React(3)
  • C# 解决Excel边框样式无法复制问题及实现格式刷功能
  • 前端DOM常用操作
  • 什么是IIC通信协议?