表格数据链接

CAD提供数据链接的功能,具体来说就是:将一个Excel的某一Sheet与CAD的某一表格关联起来。从而实现Excel表格转CAD表格的操作。
本文封装该接口。

链接Excel表格并插入CAD

重点在于数据链接的创建和绑定。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public static Table InsertExcelSheet0(this Document doc, string filePath, string sheetName, Point3d leftTopPoint, double tableWidth, string layoutName = null)
{
if (doc == null) return null;
Database db = doc.Database;
Editor ed = doc.Editor;

DataLinkManager dataLinkManager = db.DataLinkManager;
const string dataLinkName = "从Excel导入表格";
ObjectId dataLinkId = dataLinkManager.GetDataLink(dataLinkName);
if (dataLinkId != ObjectId.Null)
dataLinkManager.RemoveDataLink(dataLinkId);

DataLink dataLink = new DataLink();
dataLink.DataAdapterId = "AcExcel";
dataLink.Name = dataLinkName;
dataLink.Description = "Excel";
dataLink.ConnectionString = filePath + "!" + sheetName;

dataLinkId = dataLinkManager.AddDataLink(dataLink);

Table table = null;
using (Transaction transaction = db.TransactionManager.StartTransaction())
{
BlockTable bt = transaction.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId spaceId = ObjectId.Null;
if (layoutName == null)
spaceId = bt[BlockTableRecord.ModelSpace];
else
{
spaceId = GetLayoutIdByName(db, layoutName);
if (spaceId == ObjectId.Null)
return null;
}
BlockTableRecord btr = transaction.GetObject(spaceId, OpenMode.ForWrite) as BlockTableRecord;
transaction.AddNewlyCreatedDBObject(dataLink, true);
table = new Table();
table.TableStyle = db.Tablestyle;
table.Position = leftTopPoint;
table.Cells[0, 0].DataLink = dataLinkId;
table.GenerateLayout();
btr.AppendEntity(table);
transaction.AddNewlyCreatedDBObject(table, true);
table.Width = tableWidth;
table.Height = 1;
transaction.Commit();
}
ed.Regen();
return table;
}

插入链接表格并修改其文字样式

要修改链接表格的样式,必须先炸开表格再进行修改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55


public static void InsertExcelSheetIntoCAD(this Document doc, string filePath, string sheetName, Point3d insertPoint, double tableWidth,out double tableHeight,string fontStyleName = "Standard", int textColorIndex = 3, int lineColorIndex = 3, string layoutName = null)
{
tableHeight = 0.0;
Database db = doc.Database;
var table = doc.InsertExcelSheet0(filePath, sheetName, insertPoint, tableWidth, layoutName);
if (table == null) return;

var tablebox = table.GeometricExtents;
tableHeight = tablebox.MaxPoint.Y - tablebox.MinPoint.Y;

var styleId = db.GetTextStyleIdByName(fontStyleName, out double textSize, out double widthFactor);
if (styleId == ObjectId.Null)
return;

using (Transaction transaction = doc.TransactionManager.StartTransaction())
{
DBObjectCollection objs = new DBObjectCollection();
table.Explode(objs);
List<Entity> entities = new List<Entity>();
foreach (DBObject obj in objs)
{
if (obj is Entity)
{
var entity = (Entity)obj;
if (entity is MText mText)
{
DBObjectCollection childObjs = new DBObjectCollection();
mText.Explode(childObjs);

foreach (var childObj in childObjs)
{
if (childObj is DBText dbtext)
{
dbtext.TextString = dbtext.TextString.Trim();
dbtext.TextStyleId = styleId;
dbtext.ColorIndex = textColorIndex;
entities.Add(dbtext);
}
}
}
else
{
entity.ColorIndex = lineColorIndex;
entities.Add(entity);
}
}
}
DwgPainter.DeleteEntity(db, table.ObjectId);
DwgPainter.AddEntities(db, entities, layoutName);

transaction.Commit();
}
}

评论