重命名对象 (.NET)
随着图形变得越来越复杂,可以重命名对象以保持名称的含义,或避免与插入或附着的其他图形中的名称发生冲突。Name 属性用于获取当前名称或更改命名对象的名称。 您可以重命名任何命名对象,但 AutoCAD 保留的对象除外,例如图层 0 或 CONTINUOUS 线型。 名称长度最多为 255 个字符。除了字母和数字之外,名称还可以包含空格(尽管 AutoCAD 会删除名称前后直接显示的空格)以及 Microsoft® Windows® 或 AutoCAD 未用于其他目的的任何特殊字符。不能使用的特殊字符包括小于和大于符号 (< >)、正斜杠和反斜杠 (/ \)、引号 (“)、冒号 (:))、分号 (;))、问号 (?)、逗号 (,)、星号 (*)、竖线 (|)、等号 (=) 和单引号 (')。也不能使用使用 Unicode 字体创建的特殊字符。 重命名图层本示例创建图层“0”的副本,并将新图层重命名为“MyLayer”。 VB.NETImports 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 C#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(); } } 相关概念父主题: |
|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2024-12-15 23:59
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.