As your drawings become more complex, you can rename objects to keep the names meaningful or to avoid conflicts with names in other drawings you have inserted or attached. The Name property is used to get the current name or change the name of a named object.
图形变得比较复杂时,用户可以重命名对象,使名称有针对性或者避免与用户插入或附加到主图形的其他图形中的名称相冲突。Name 属性用于得到命名对象的当前名字或修改名字。
You can rename any named object except those reserved by AutoCAD, for example, layer 0 or the CONTINUOUS linetype.
用户可以重任何命名对象,除了那些被 AutoCAD 保留的外,例如,图层 0 或 CONTINUOUS 线型。
Names can be up to 255 characters long. In addition to letters and numbers, names can contain spaces (although AutoCAD removes spaces that appear directly before and after a name) and any special character not used by Microsoft® Windows® or AutoCAD for other purposes. Special characters that you cannot use include less-than and greater-than symbols (< >), forward slashes and backslashes (/ \), quotation marks ("), colons (:), semicolons (;), question marks (?), commas (,), asterisks (*), vertical bars (|), equal signs (=), and single quotes ('). You also cannot use special characters created with Unicode fonts.
名称最长可以包含 255 个字符。除了字母和数字之外,名称中还可以包含空格(虽然 AutoCAD 会删除名称前后的空格)以及 Microsoft Windows 或 AutoCAD 未用作其他用途的任何特殊字符。不能使用的特殊字符包括小于号和大于号 (< >)、正斜杠和反斜杠 (/ \)、引号 (")、冒号 (:)、分号 (;)、问号 (?)、逗号 (,) 星号 (*)、竖线 (|)、等号 (=) 和单引号 (')。也不能使用以 Unicode 字体创建的特殊字符。
This example creates a copy of layer "0" and renames the new layer to “MyLayer”.
本例创建图层 ‘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)
'' 复制 0 图层(复制它及它的属性)为一个新的图层 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;
// 复制 0 图层(复制它及它的属性)为一个新的图层 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();
}
}