You can delete non-graphical and graphical objects with the Erase method.
使用 Erase 方法可以删除非图形对象和图形对象。
WarningWhile many non-graphical objects, such as the Layer table and Model space block table records have an Erase method, it should not be called. If Erase is called on one of these objects, an error will occur.
警告虽然许多非图形对象,像层表和模型空间块表记录都有 Erase 方法,但它不应该被调用。如果这些对象的任何的一个的 Erase 被调用,将会产生错误。
This example creates a lightweight polyline, then erases it.
本例创建一个轻量多段线,然后删除它。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("EraseObject")> _
Public Sub EraseObject()
'' 获得当前文档和数据库 Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
''启动一个事务 Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' 以只读方式打开块表 Open the Block table for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
'' 以写方式打开模型空间块表记录 Open the Block table record Model space for write
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
OpenMode.ForWrite)
'' 创建一条轻量多段线 Create a lightweight polyline
Dim acPoly As Polyline = New Polyline()
acPoly.SetDatabaseDefaults()
acPoly.AddVertexAt(0, New Point2d(2, 4), 0, 0, 0)
acPoly.AddVertexAt(1, New Point2d(4, 2), 0, 0, 0)
acPoly.AddVertexAt(2, New Point2d(6, 4), 0, 0, 0)
'' 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPoly)
acTrans.AddNewlyCreatedDBObject(acPoly, True)
'' 更新显示并显示一条提醒消息 Update the display and display an alert message
acDoc.Editor.Regen()
Application.ShowAlertDialog("Erase the newly added polyline.")
'' 从图形中删除多段线 Erase the polyline from the drawing
acPoly.Erase(True)
'' 保存新对象到数据库中 Save the new object to the database
acTrans.Commit()
End Using
End Sub
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("EraseObject")]
public static void EraseObject()
{
// 获得当前文档和数据库 Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// 启动一个事务 Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// 以只读方式打开块表 Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// 以写方式打开模型空间块表记录 Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// 创建一条轻量多段线 Create a lightweight polyline
Polyline acPoly = new Polyline();
acPoly.SetDatabaseDefaults();
acPoly.AddVertexAt(0, new Point2d(2, 4), 0, 0, 0);
acPoly.AddVertexAt(1, new Point2d(4, 2), 0, 0, 0);
acPoly.AddVertexAt(2, new Point2d(6, 4), 0, 0, 0);
// 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPoly);
acTrans.AddNewlyCreatedDBObject(acPoly, true);
// 更新显示并显示一条提醒消息 Update the display and display an alert message
acDoc.Editor.Regen();
Application.ShowAlertDialog("Erase the newly added polyline.");
// 从图形中删除多段线 Erase the polyline from the drawing
acPoly.Erase(true);
// 保存新对象到数据库中 Save the new object to the database
acTrans.Commit();
}
}