CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoCAD 2020 开发者帮助

清除未引用的命名对象 (.NET)

2024-5-18 18:25| 发布者: admin| 查看: 77| 评论: 0|原作者: admin|来自: AutoCAD

清除未引用的命名对象 (.NET)

可以随时从数据库中清除未引用的命名对象。不能清除由其他对象引用的命名对象。例如,字体文件可能由文本样式引用,或者图层上的对象可能被引用。清除可在保存到磁盘时减小图形文件的大小。

未参照的对象将使用该方法从图形数据库中清除。该方法需要要以 或 objects 的形式清除的对象列表。传递到方法中的 or 对象将使用可以从数据库中删除的对象进行更新。调用 后,必须擦除返回的每个对象。PurgePurgeObjectIdCollectionObjectIdGraphObjectIdCollectionObjectIdGraphPurgePurge

清除所有未参照的图层

以下示例演示了如何从数据库中清除所有未引用的图层。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("PurgeUnreferencedLayers")> _
Public Sub PurgeUnreferencedLayers()
    '' 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)

        '' Create an ObjectIdCollection to hold the object ids for each table record
        Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()

        '' Step through each layer and add it to the ObjectIdCollection
        For Each acObjId As ObjectId In acLyrTbl
            acObjIdColl.Add(acObjId)
        Next

        '' Remove the layers that are in use and return the ones that can be erased
        acCurDb.Purge(acObjIdColl)

        '' Step through the returned ObjectIdCollection
        For Each acObjId As ObjectId In acObjIdColl
            Dim acSymTblRec As SymbolTableRecord
            acSymTblRec = acTrans.GetObject(acObjId, _
                                            OpenMode.ForWrite)

            Try
                '' Erase the unreferenced layer
                acSymTblRec.Erase(True)
            Catch Ex As Autodesk.AutoCAD.Runtime.Exception
                '' Layer could not be deleted
                Application.ShowAlertDialog("Error:" & vbLf & Ex.Message)
            End Try
        Next

        '' Commit the changes and dispose of the transaction
        acTrans.Commit()
    End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("PurgeUnreferencedLayers")]
public static void PurgeUnreferencedLayers()
{
    // 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;

        // Create an ObjectIdCollection to hold the object ids for each table record
        ObjectIdCollection acObjIdColl = new ObjectIdCollection();

        // Step through each layer and add iterator to the ObjectIdCollection
        foreach (ObjectId acObjId in acLyrTbl)
        {
            acObjIdColl.Add(acObjId);
        }

        // Remove the layers that are in use and return the ones that can be erased
        acCurDb.Purge(acObjIdColl);

        // Step through the returned ObjectIdCollection
        // and erase each unreferenced layer
        foreach (ObjectId acObjId in acObjIdColl)
        {
            SymbolTableRecord acSymTblRec;
            acSymTblRec = acTrans.GetObject(acObjId,
                                            OpenMode.ForWrite) as SymbolTableRecord;

            try
            {
                // Erase the unreferenced layer
                acSymTblRec.Erase(true);
            }
            catch (Autodesk.AutoCAD.Runtime.Exception Ex)
            {
                // Layer could not be deleted
                Application.ShowAlertDialog("Error:\n" + Ex.Message);
            }
        }

        // Commit the changes and dispose of the transaction
        acTrans.Commit();
    }
}

VBA/ActiveX 交叉引用

在 ActiveX 自动化库中,您将使用该方法删除所有未引用的命名对象,并标识可以删除哪些对象。但是,使用 AutoCAD .NET API 时,您需要提供要清除的对象,然后该方法会返回给您实际可以清除的对象。因此,在使用 AutoCAD .NET API 从数据库中清除所有未引用的命名对象时,需要做更多的工作。PurgeAllPurge

ThisDrawing.PurgeAll

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-12-15 12:49

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部