CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

擦除图层 (.NET)

2023-1-1 13:20| 发布者: admin| 查看: 331| 评论: 0|来自: AutoCAD

您可以在绘图任务期间随时拭除图层。不能拭除当前图层、图层 0、外部参照相关图层或包含对象的图层。

要擦除图层,请使用该方法。建议使用清除功能来验证是否可以清除该层,同时验证它是否不是第 0 层、定义点或当前层。Erase

注意:块定义所参照的图层以及名为 DEFPOINTS 的特殊图层即使不包含可见对象,也无法删除。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("EraseLayer")> _
Public Sub EraseLayer()
    '' 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 Layer table for read
        Dim acLyrTbl As LayerTable
        acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _
                                     OpenMode.ForRead)

        Dim sLayerName As String = "ABC"

        If acLyrTbl.Has(sLayerName) = True Then
            '' Check to see if it is safe to erase layer
            Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()
            acObjIdColl.Add(acLyrTbl(sLayerName))
            acCurDb.Purge(acObjIdColl)

            If acObjIdColl.Count > 0 Then
                Dim acLyrTblRec As LayerTableRecord
                acLyrTblRec = acTrans.GetObject(acObjIdColl(0), OpenMode.ForWrite)

                Try
                    '' Erase the unreferenced layer
                    acLyrTblRec.Erase(True)

                    '' Save the changes and dispose of the transaction
                    acTrans.Commit()
                Catch Ex As Autodesk.AutoCAD.Runtime.Exception
                    '' Layer could not be deleted
                    Application.ShowAlertDialog("Error:\n" + Ex.Message)
                End Try
            End If
        End If
    End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("EraseLayer")]
public static void EraseLayer()
{
    // 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 Layer table for read
        LayerTable acLyrTbl;
        acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
                                        OpenMode.ForRead) as LayerTable;

        string sLayerName = "ABC";

        if (acLyrTbl.Has(sLayerName) == true)
        {
            // Check to see if it is safe to erase layer
            ObjectIdCollection acObjIdColl = new ObjectIdCollection();
            acObjIdColl.Add(acLyrTbl[sLayerName]);
            acCurDb.Purge(acObjIdColl);

            if (acObjIdColl.Count > 0)
            {
                LayerTableRecord acLyrTblRec;
                acLyrTblRec = acTrans.GetObject(acObjIdColl[0],
                                                OpenMode.ForWrite) as LayerTableRecord;

                try
                {
                    // Erase the unreferenced layer
                    acLyrTblRec.Erase(true);

                    // Save the changes and dispose of the transaction
                    acTrans.Commit();
                }
                catch (Autodesk.AutoCAD.Runtime.Exception Ex)
                {
                    // Layer could not be deleted
                    Application.ShowAlertDialog("Error:\n" + Ex.Message);
                }
            }
        }
    }
}

VBA/ActiveX 代码参考

Sub EraseLayer()
    On Error Resume Next
 
    Dim layerObj As AcadLayer
    Set layerObj = ThisDrawing.Layers("ABC")
 
    layerObj.Delete
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-5-19 14:30

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部