我正在使用C#和.NET Framework编写AutoCAD 2014插件。我像这样扩展了Autodesk的Table
类:
public class OpeningDataTable : Autodesk.AutoCAD.DatabaseServices.Table
我的想法是,我想从AutoCAD图形中拉出已经在AutoCAD图形上绘制的表格作为
OpeningDataTable
的实例,以便可以使用编写的方法来操作数据。我这样做是这样的:OpeningDataTable myTable = checkForExistingTable(true);
public Autodesk.AutoCAD.DatabaseServices.Table checkForExistingTable(bool isWindow)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; //Current drawing
Transaction tr = doc.TransactionManager.StartTransaction();
DocumentLock docLock = doc.LockDocument();
TypedValue[] tableItem = new TypedValue[] { new TypedValue(0, "ACAD_TABLE") };
SelectionFilter tableSelecFilter = new SelectionFilter(tableItem);
Editor ed = doc.Editor; //Editor object to ask user where table goes, subclass of Document
using (tr)
{
PromptSelectionResult selectResult = ed.SelectAll(tableSelecFilter);
if (selectResult.Status == PromptStatus.OK)
{
SelectionSet tableSelSet = selectResult.Value;
for (int i = 0; i < tableSelSet.Count; i++)
{
Autodesk.AutoCAD.DatabaseServices.Table tableToCheck = (Autodesk.AutoCAD.DatabaseServices.Table)tr.GetObject(tableSelSet[i].ObjectId, OpenMode.ForRead);
String tableTitle = tableToCheck.Cells[0, 0].Value.ToString();
if(tableTitle.Equals("Window Schedule") && isWindow == true)
return (OpeningDataTable)tableToCheck;
if (tableTitle.Equals("Door Schedule") && isWindow == false)
return (OpeningDataTable)tableToCheck;
}
}
return null;
}
}
但是,我收到一条错误消息,说我无法将
Table
对象(父类)转换为OpeningDataTable
对象(子类)。有没有解决此问题的简单方法?
最佳答案
您将需要为OpeningDataTable
创建一个以Table
为参数的构造函数。
无法将Table
强制转换为OpeningDataTable
的原因是,Table
不是OpeningDataTable
,就像object
不是int
一样。
关于c# - 在C#中将父类强制转换为子类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26692925/