使用C#在AutoCAD中删除表格行

时间:2014-10-16 14:47:10

标签: c# .net autocad

我正在编写一种方法,根据第一列的值删除AutoCAD中的表格行。

我的代码是:

public void deleteRow(String openingName)
{
    Transaction tr = doc.TransactionManager.StartTransaction();
    DocumentLock docLock = doc.LockDocument();

    using (tr)
    using (docLock)
    {
        if (!IsWriteEnabled || !IsReadEnabled) //Committing transactions closes everything for reading and writing so it must be reopened
        {
            tr.GetObject(this.ObjectId, OpenMode.ForRead);
            tr.GetObject(this.ObjectId, OpenMode.ForWrite);
        }

        for (int i = 1; i < NumRows; i++)
        {
             if (Cells[i, 0].GetTextString(FormatOption.ForExpression).Equals(openingName))
             {
                 DeleteRows(i, 1);
             }
        }

        GenerateLayout();
        tr.Commit();
    }
}

但它所做的只是删除行中单元格的值,留下一个空行。

如何删除行本身?

2 个答案:

答案 0 :(得分:0)

我明白了。我需要摆脱GenerateLayout()调用

答案 1 :(得分:0)

if (!IsWriteEnabled || !IsReadEnabled) 
        {
           var tb = tr.GetObject(this.ObjectId, OpenMode.ForRead) as Table;
           // tr.GetObject(this.ObjectId, OpenMode.ForWrite);
           tb.UpgradeOpen();
           for (int i = 1; i < NumRows; i++)
           {
               if (Cells[i, 0].GetTextString(FormatOption.ForExpression).Equals(openingName))
               {
                 tb.DeleteRows(i, 1);
                }
            }
            tb.DowngradeOpen();
         }