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

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("\n" + 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}\n", layerName);}else{tr.Commit();ed.WriteMessage("图层已存在: {0}\n", layerName);}}}

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


 

http://www.lryc.cn/news/286213.html

相关文章:

  • [陇剑杯 2021]日志分析
  • Java面试汇总——jvm篇
  • 数据结构:完全二叉树(递归实现)
  • RK3568 移植Ubuntu
  • C++大学教程(第九版)6.34猜数字游戏 6.35 修改的猜数字游戏
  • 【立创EDA-PCB设计基础】5.布线设计规则设置
  • ElementUI简介以及相关操作
  • 内存耗尽排查思路
  • OpenCV书签 #差值哈希算法的原理与相似图片搜索实验
  • Unity中URP下获取主灯信息
  • 尝试着在Stable Diffusion里边使用SadTalker进行数字人制作
  • 链路聚合原理与配置
  • 第8章 通信网络安全
  • L1-092 进化论(Java)
  • SpringBoot 源码解析5:ConfigurationClassPostProcessor整体流程和@ComponentScan源码分析
  • 一.初识Linux 1-3操作系统概述Linux初识虚拟机介绍
  • Eureka整合seata分布式事务
  • 华为云磁盘性能指标(参考)
  • 利用OpenGL图形库实现人物动画移动效果
  • History命令解释,及一个相关的bash脚本(如何编写脚本程序从记录文件中提取history命令)
  • apisix 单机部署 linux
  • Redis 面试题 | 06.精选Redis高频面试题
  • 2008年苏州大学837复试机试C/C++
  • MySQL笔记-information_schema库中COLUMNS表的一些笔记
  • 归并排序模板
  • 【NVIDIA】Jetson Orin Nano系列:安装 Qt6、firefox、jtop、flameshot
  • Fastapi+Jsonp实现前后端跨域请求
  • MacOS受欢迎的数据库开发工具 Navicat Premium 15 中文版
  • helm---自动化一键部署
  • 求助帖(setiosflags)的左右对齐问题: