図面がより複雑になるにつれ、名前が意味を表すものにするため、あるいは挿入または添付した他の図面との競合を避けるため、オブジェクトの名前を変更することができます。現在の名前を取得したり、名前付きオブジェクトの名前を変更したりするために、Name プロパティを使用します。
画層 0 または CONTINUOUS 線種など、AutoCAD によって予約されているものを除いて、名前の付いたオブジェクトはいずれもその名前を変更できます。
名前は半角で 255 文字まで使用できます。文字や数字の他に、スペース(ただし、名前の先頭と最後のスペースは削除されます)や Microsoft® Windows® および AutoCAD が他の目的で使用していないものであれば、特殊文字も使用できます。使用できない特殊文字には、山括弧(< >)、スラッシュと円記号(/ ¥)、ダブル クォーテーション記号(")、コロン(:)、セミコロン(;)、疑問符(?)、カンマ(,)、アスタリスク(*)、縦棒(|)、等号(=)、シングル クォーテーション(')記号があります。Unicode フォントを使用して作成した特殊文字も使用できません。
この例では、画層 0 のコピーを作成し、新しい画層の名前を「MyLayer」に変更します。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
<CommandMethod("RenameLayer")> _
Public Sub RenameLayer()
'' 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()
'' Returns the layer table for the current database
Dim acLyrTbl As LayerTable
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _
OpenMode.ForWrite)
'' Clone layer 0 (copy it and its properties) as a new layer
Dim acLyrTblRec As LayerTableRecord
acLyrTblRec = acTrans.GetObject(acLyrTbl("0"), _
OpenMode.ForRead).Clone()
'' Change the name of the cloned layer
acLyrTblRec.Name = "MyLayer"
'' Add the cloned layer to the Layer table and transaction
acLyrTbl.Add(acLyrTblRec)
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, True)
'' Save changes and dispose of the transaction
acTrans.Commit()
End Using
End Sub
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("RenameLayer")]
public static void RenameLayer()
{
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Returns the layer table for the current database
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForWrite) as LayerTable;
// Clone layer 0 (copy it and its properties) as a new layer
LayerTableRecord acLyrTblRec;
acLyrTblRec = acTrans.GetObject(acLyrTbl["0"],
OpenMode.ForRead).Clone() as LayerTableRecord;
// Change the name of the cloned layer
acLyrTblRec.Name = "MyLayer";
// Add the cloned layer to the Layer table and transaction
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
// Save changes and dispose of the transaction
acTrans.Commit();
}
}