public class Class1 : IExtensionApplication
{
void IExtensionApplication.Initialize()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor editor = doc.Editor;
editor.WriteMessage("*********************************\n");
editor.WriteMessage("* SETDATA:选择文字信息进行导出 *\n");
editor.WriteMessage("* GETDATA:选择CSV文件进行导入 *\n");
editor.WriteMessage("*********************************\n");
}
//选文本信息导出
[CommandMethod("GETDATA")]
public void GETDATA()
{
// 获取当前文档和数据库
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// 获取当前图形数据库的路径
string path = db.Filename;
ed.WriteMessage("\n*********************************\n");
ed.WriteMessage("* SETDATA:选择文字信息进行导出 *\n");
ed.WriteMessage("* GETDATA:选择CSV文件进行导入 *\n");
ed.WriteMessage("*********************************\n");
// 提示用户选择文本
PromptSelectionResult selectionResult = doc.Editor.GetSelection(new SelectionFilter(new TypedValue[] { new TypedValue((int)DxfCode.Start, "TEXT") }));
if (selectionResult.Status == PromptStatus.OK)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
SelectionSet selection = selectionResult.Value;
File.Delete(path + "文字信息.csv");
StreamWriter mydata = new StreamWriter(path + "文字信息.csv", append: true, Encoding.Default);
string value = "图层,名称,X,Y,字高,颜色\t,";
mydata.WriteLine(value);
// 遍历选择集中的文本对象
foreach (ObjectId id in selection.GetObjectIds())
{
DBText text = trans.GetObject(id, OpenMode.ForRead) as DBText;
if (text != null)
{
// 获取图层名称
string layerName = text.Layer;
//文本信息
string textContent = text.TextString;
// 获取文本坐标
double xPos = text.Position.X;
double yPos = text.Position.Y;
// 获取文本颜色
int colorIndex = text.Color.ColorIndex;
// 获取文本字高
double textHeight = text.Height;
//拼接CSV字符串
value = layerName + "," + textContent + "," + xPos + "," + yPos + "," + textHeight + "," + colorIndex + "\t,";
mydata.WriteLine(value);
}
}
mydata.Close();
// 打印输入输出
ed.WriteMessage("\n导出完成");
trans.Commit();
}
}
}
[CommandMethod("SETDATA")]
public void SETDATA()
{
// 选择CSV文件
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".csv";
Nullable result = dlg.ShowDialog();
if (result != true)
return;
// 获取当前文档和数据库
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
ed.WriteMessage("\n*********************************\n");
ed.WriteMessage("* SETDATA:选择文字信息进行导出 *\n");
ed.WriteMessage("* GETDATA:选择CSV文件进行导入 *\n");
ed.WriteMessage("*********************************\n");
// 打开CSV文件并逐行处理
string filePath = dlg.FileName;
string[] csvLines = File.ReadAllLines(filePath);
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);
}
// 打印输入输出
ed.WriteMessage("\n导入完成");
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 = 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);
}
}
}
void IExtensionApplication.Terminate()
{
}
}
c# cad文字信息导出导入演示视频
百度网盘下载地址
链接:https://pan.baidu.com/s/1FCuyAtPzn8S89umT-V0IOA?pwd=1234
提取码:1234