C# Cad2016二次开发选择csv导入信息(七)

//选择csv导入信息
[CommandMethod("setdata")]
//本程序在AutoCAD的快捷命令是"DLLLOAD"
public void setdata()
{

    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

    dlg.DefaultExt = ".csv";
    // Display OpenFileDialog by calling ShowDialog method 
    Nullable<bool> result = dlg.ShowDialog();
    //判断确认按钮
    if (result == true)
    {
        //选择的文件路径
        string file_dir = dlg.FileName;

    }
}

 使用Microsoft.Win32.OpenFileDialog注意需要引用 PresentationFramework.dll控件

 [CommandMethod("setdata")]
 public void SetData()
 {
     // 选择CSV文件
     Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
     dlg.DefaultExt = ".csv";
     Nullable<bool> result = dlg.ShowDialog();
     if (result != true)
         return;

     // 获取当前文档和数据库
     Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
     Database db = doc.Database;
     Editor ed = doc.Editor;

     // 打开CSV文件并逐行处理
     string filePath = dlg.FileName;
     string[] csvLines = File.ReadAllLines(filePath);
     CheckAndCreateLayer("qlr权利人");
     using (Transaction trans = db.TransactionManager.StartTransaction())
     {
         BlockTable blockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
         BlockTableRecord modelSpace = trans.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

         foreach (string csvLine in csvLines)
         {
             string[] csvData = csvLine.Split(',');
             if (csvData.Length < 6)
             {
                 ed.WriteMessage("CSV数据格式不正确!");
                 continue;
             }

             string layerName = csvData[0];
             
             string name = csvData[1];//
             double x, y, height;
             int color;

             if (!double.TryParse(csvData[2], out x) || !double.TryParse(csvData[3], out y) ||
                 !double.TryParse(csvData[4], out height) || !int.TryParse(csvData[5], out color))
             {
                 ed.WriteMessage("CSV数据类型转换出错!");
                 continue;
             }

             // 检查图层是否已存在,不存在创建图层
             CheckAndCreateLayer(layerName);

             // 创建实体对象
             Point3d position = new Point3d(x, y, 0);
             DBText text = new DBText();
             text.Position = position;
             text.TextString = name;
             text.Height = height;
             text.Color = Color.FromColorIndex(ColorMethod.ByAci, (byte)color);
             //text.LayerId = ObjectId(layerName);
             LayerTableRecord ltr = new LayerTableRecord();
             text.LayerId = GetLayerId(layerName);
             // 添加实体到模型空间
             modelSpace.AppendEntity(text);
             trans.AddNewlyCreatedDBObject(text, true);
            
             //拼接CSV字符串
             string value = layerName + "," + name + "," + x + "," + y + "," + height + "," + color;
             ed.WriteMessage("
" + value);
         }

         trans.Commit();
     }
 }
 public ObjectId GetLayerId(string layerName)
 {
     Document doc = Application.DocumentManager.MdiActiveDocument;
     Database db = doc.Database;
     using (Transaction tr = db.TransactionManager.StartTransaction())
     {
         LayerTable layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
         if (layerTable.Has(layerName))
         {
             LayerTableRecord layerRecord = (LayerTableRecord)tr.GetObject(layerTable[layerName], OpenMode.ForRead);
             return layerRecord.ObjectId;
         }
     }
     return ObjectId.Null;
 }

 public void CheckAndCreateLayer(string layerName)
 {
     Document doc = Application.DocumentManager.MdiActiveDocument;
     Database db = doc.Database;
     Editor ed = doc.Editor;

     using (Transaction tr = db.TransactionManager.StartTransaction())
     {
         LayerTable layerTable = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

         if (!layerTable.Has(layerName))
         {
             LayerTableRecord layer = new LayerTableRecord();
             layer.Name = layerName;

             layerTable.UpgradeOpen();
             ObjectId layerId = layerTable.Add(layer);
             tr.AddNewlyCreatedDBObject(layer, true);

             // 设置新图层的属性
             layer.Color = Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.Red);
             layer.LineWeight = LineWeight.LineWeight030;

             tr.Commit();
             ed.WriteMessage("图层已创建: {0}
", layerName);

         }
         else
         {
             tr.Commit();
             ed.WriteMessage("图层已存在: {0}
", layerName);
         }
     }
 }

 注意事项 导入csv编码 utf8
导入视图如下