AutoCAD saves layer setting information in an extension dictionary of the Layers table object. When you first save a layer state, AutoCAD does the following:
AutoCAD 将图层设置保存在层表对象的扩展词典中。用户首次保存图形中的图层状态时,AutoCAD 将执行以下操作:
Each time you save another layer setting in the drawing, AutoCAD creates another XRecord object describing the saved settings and stores the XRecord in the ACAD_LAYERSTATE dictionary. The following diagram illustrates the process.
每次保存图形中另一图层设置时,AutoCAD 均将创建另一个描述保存设置的 XRecord 对象,并将 XRecord 存储在 ACAD_LAYERSTATE 词典中。下面的图表说明了这个过程。
You do not need (and should not try) to directly manipulate the entries when working with layer states. Use the functions of the LayerStateManager object to access the dictionary. Once you have a reference to the dictionary, you can step through each of the entries which are represented as DBDictionaryEntry objects.
用户使用图层状态时,不需要(也不应该)直接操作项目,而应该使用 LayerStateManager 对象的函数访问字典。一旦用户拥有了一个字典的引用,就可以遍历字典中的每一个代表 DBDictionaryEntry 对象的项目。
If layer states have been saved in the current drawing, the following code lists the names of all saved layer states:
如果图层状态已经保存在当前的图形中,则下面的代码列出所有已经保存的图层状态名称:
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
<CommandMethod("ListLayerStates")> _
Public Sub ListLayerStates()
'' 获得当前文档和数据库 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()
Dim acLyrStMan As LayerStateManager
acLyrStMan = acCurDb.LayerStateManager
Dim acDbDict As DBDictionary
acDbDict = acTrans.GetObject(acLyrStMan.LayerStatesDictionaryId(True), _
OpenMode.ForRead)
Dim sLayerStateNames As String = ""
For Each acDbDictEnt As DBDictionaryEntry In acDbDict
sLayerStateNames = sLayerStateNames & vbLf & acDbDictEnt.Key
Next
Application.ShowAlertDialog("The saved layer settings in this drawing are:" & _
sLayerStateNames)
'' 销毁事务 Dispose of the transaction
End Using
End Sub
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("ListLayerStates")]
public static void ListLayerStates()
{
// 获得当前文档和数据库 Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// 启动一个事务 Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
LayerStateManager acLyrStMan;
acLyrStMan = acCurDb.LayerStateManager;
DBDictionary acDbDict;
acDbDict = acTrans.GetObject(acLyrStMan.LayerStatesDictionaryId(true),
OpenMode.ForRead) as DBDictionary;
string sLayerStateNames = "";
foreach (DBDictionaryEntry acDbDictEnt in acDbDict)
{
sLayerStateNames = sLayerStateNames + "\n" + acDbDictEnt.Key;
}
Application.ShowAlertDialog("The saved layer settings in this drawing are:" +
sLayerStateNames);
// Dispose of the transaction
}
}