向集合对象添加新成员 (.NET)
若要向集合中添加新成员,请使用该方法。例如,以下代码创建一个新图层并将其添加到“图层”表中:Add VB.NETImports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
<CommandMethod("AddMyLayer")> _
Public Sub AddMyLayer()
'' 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 Not acLyrTbl.Has("MyLayer") Then
'' Open the Layer Table for write
acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForWrite)
'' Create a new layer table record and name the layer "MyLayer"
Using acLyrTblRec As LayerTableRecord = New LayerTableRecord
acLyrTblRec.Name = "MyLayer"
'' Add the new layer table record to the layer table and the transaction
acLyrTbl.Add(acLyrTblRec)
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, True)
End Using
'' Commit the changes
acTrans.Commit()
End If
'' Dispose of the transaction
End Using
End Sub
C#using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("AddMyLayer")]
public static void AddMyLayer()
{
// 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)
{
// Open the Layer Table for write
acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForWrite);
// Create a new layer table record and name the layer "MyLayer"
using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
{
acLyrTblRec.Name = "MyLayer";
// Add the new layer table record to the layer table and the transaction
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
}
// Commit the changes
acTrans.Commit();
}
// Dispose of the transaction
}
}
VBA/ActiveX 代码参考Sub AddMyLayer()
Dim newLayer as AcadLayer
Set newLayer = ThisDrawing.Layers.Add("MyLayer")
End Sub
相关概念父主题: |
|Archiver|CAD开发者社区
( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2025-10-29 09:57
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.