CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

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

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

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

使用该方法从图形数据库中清除未参照的对象。该方法需要要以无对象形式清除的对象列表。传递到该方法中的理论对象使用可以从数据库中删除的对象进行更新。调用 之后,必须擦除返回的每个对象。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 )

GMT+8, 2024-5-19 11:59

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部