CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

擦除集合对象的成员 (.NET)

2023-1-1 16:35| 发布者: admin| 查看: 477| 评论: 0|来自: AutoCAD

可以使用在成员对象上找到的方法擦除集合对象中的成员。例如,以下代码从对象中删除层 MyLayer。EraseLayerTable

从图形中拭除图层之前,应确保可以安全地将其移除。要确定是否可以擦除图层或其他命名对象(如块或文本样式),应使用 themethod。Purge

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("RemoveMyLayer")> _
Public Sub RemoveMyLayer()
  '' Get the current document and database, and start a transaction
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim acCurDb As Database = acDoc.Database
 
  Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
      '' Returns the layer table for the current database
      Dim acLyrTbl As LayerTable
      acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _
                                   OpenMode.ForRead)
 
      '' Check to see if MyLayer exists in the Layer table
      If acLyrTbl.Has("MyLayer") = True Then
          Dim acLyrTblRec As LayerTableRecord
          acLyrTblRec = acTrans.GetObject(acLyrTbl("MyLayer"), _
                                          OpenMode.ForWrite)
 
          Try
              acLyrTblRec.Erase()
              acDoc.Editor.WriteMessage(vbLf & "'MyLayer' was erased")
 
              '' Commit the changes
              acTrans.Commit()
          Catch
              acDoc.Editor.WriteMessage(vbLf & "'MyLayer' could not be erased")
          End Try
      Else
          acDoc.Editor.WriteMessage(vbLf & "'MyLayer' does not exist")
      End If
 
      '' Dispose of the transaction
  End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("RemoveMyLayer")]
public static void RemoveMyLayer()
{
  // Get the current document and database, and start a transaction
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;
 
  using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  {
      // Returns the layer table for the current database
      LayerTable acLyrTbl;
      acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
                                   OpenMode.ForRead) as LayerTable;
 
      // Check to see if MyLayer exists in the Layer table
      if (acLyrTbl.Has("MyLayer") == true)
      {
          LayerTableRecord acLyrTblRec;
          acLyrTblRec = acTrans.GetObject(acLyrTbl["MyLayer"],
                                          OpenMode.ForWrite) as LayerTableRecord;
 
          try
          {
              acLyrTblRec.Erase();
              acDoc.Editor.WriteMessage("\n'MyLayer' was erased");
 
              // Commit the changes
              acTrans.Commit();
          }
          catch
          {
              acDoc.Editor.WriteMessage("\n'MyLayer' could not be erased");
          }
      }
      else
      {
          acDoc.Editor.WriteMessage("\n'MyLayer' does not exist");
      }
 
      // Dispose of the transaction
  }
}

VBA/ActiveX 代码参考

Sub RemoveMyLayer()
  On Error Resume Next
 
  '' Get the layer "MyLayer" from the Layers collection
  Dim ABCLayer As AcadLayer
  Set ABCLayer = ThisDrawing.Layers.Item("MyLayer")
 
  '' Check for an error, if no error occurs the layer exists
  If Err = 0 Then
 
    '' Delete the layer
    ABCLayer.Delete
 
    '' Clear the current error
    Err.Clear
 
    '' Get the layer again if it is found the layer could not be removed
    Set ABCLayer = ThisDrawing.Layers.Item("MyLayer")
 
    '' Check for error, if an error is encountered the layer was removed
    If Err <> 0 Then
      ThisDrawing.Utility.prompt "'MyLayer' was removed"
    Else
      ThisDrawing.Utility.prompt "'MyLayer' could not be removed"
    End If
  Else
    ThisDrawing.Utility.prompt "'MyLayer' does not exist"
  End If
End Sub

擦除对象后,您不应稍后在程序中尝试再次访问该对象;否则将发生错误。上面的示例测试对象在再次访问之前是否存在。当发出擦除对象的请求时,您应该检查该对象是否存在于该方法或使用 a语句来捕获发生的任何异常。HasTry


路过

雷人

握手

鲜花

鸡蛋

最新评论

AutoCAD Moldflow UG MoldWizard模具开发4合1

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-7 00:25

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部