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;
            }
         
        }  
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值