0
点赞
收藏
分享

微信扫一扫

c# cad二次开发 通过选择txt文件将自动转换成多段线

白衣蓝剑冰魄 2023-06-06 阅读 99

c# cad二次开发 通过选择txt文件将自动转换成多段线,txt样式如下
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System.IO;
using win = System.Windows.Forms;

namespace _5_通过外部文件进行画图
{
public class Initiallization : Autodesk.AutoCAD.Runtime.IExtensionApplication //当调用类库时,自动加载执行
{
public void Initialize()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage(“\n加载成功!\n插件功能如下:”);
ed.WriteMessage(“\ntxt文件转成多段线(PLTXT)”);
}
public void Terminate()
{
Console.WriteLine(“清理!”);
}
}
public class Class1
{
///
/// 获取多段线坐标
///
[CommandMethod(“PLTXT”)]
public void PLine_txt()
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
//定义路径文件名
win.OpenFileDialog dialog = new win.OpenFileDialog();
dialog.Multiselect = false;//是否可以选择多个文件
dialog.Title = “请选择文件”;
dialog.Filter = “所有文件(.)|.”;
if (dialog.ShowDialog() == win.DialogResult.OK)
{
string filename = dialog.FileName;

            //string filename = @"D:\6-C#\0、程序开发\5、通过外部文件进行画图\新增017-1.txt";
            //读取txt文件内容
            string[] contents = File.ReadAllLines(dialog.FileName);
            //声明List对象,将数据进行整理
            List<List<string>> List = new List<List<string>>();
            for (int i = 0; i < contents.Length; i++)
            {
                //将数据按照","进行分列
                string[] cont = contents[i].Split(new char[] { ',' });
                List<string> subList = new List<string>();
                //判断是不是开头是“J”
                if (contents[i] == "")
                {
                    
                }
                else if (contents[i].Substring(0, 1) != "J")
                {
                }
                else
                {
                    for (int j = 0; j < cont.Length; j++)
                    {
                        subList.Add(cont[j]);
                    }
                    List.Add(subList);
                }
            }
            //声明一个多段线对象
            Polyline pline = new Polyline();
            for (int i = 0; i < List.Count; i++)
            {

                //数据转换
                if (List[i][0].Substring(0, 1) != "J")
                {
                    ed.WriteMessage("有问题\n");
                }
                else
                {
                    double X, Y;
                    bool bx = double.TryParse(List[i][2], out X);
                    bool by = double.TryParse(List[i][3], out Y);
                    //判断外部文件是否有问题


                    ed.WriteMessage(List[i][2], List[i][3]);
                    //将xy坐标添加到对象中
                    pline.AddVertexAt(i, new Point2d(X, Y), 0, 0, 0);
                }
            }
            Database db = HostApplicationServices.WorkingDatabase;
            db.AddEnityToModelSpace(pline);
        }
    }
}

}

举报

相关推荐

0 条评论