using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using AcRx = Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcGe = Autodesk.AutoCAD.Geometry;
using AcEd = Autodesk.AutoCAD.EditorInput;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(TestRename.MyCommands))]
namespace TestRename
{
public class MyCommands
{
[CommandMethod("RenBlock")]
public void RenBlock()
{
AcAp.Document doc = AcAp.Application.DocumentManager.MdiActiveDocument;
AcEd.Editor ed = doc.Editor;
AcEd.PromptEntityOptions pr1 =
new AcEd.PromptEntityOptions("\nУкажите вставку блока для переименования: ");
pr1.SetRejectMessage("Это не вставка блока!");
pr1.AddAllowedClass(typeof(AcDb.BlockReference), true);
AcEd.PromptEntityResult res1 = ed.GetEntity(pr1);
if (res1.Status != AcEd.PromptStatus.OK) return;
AcDb.ObjectId idBtr = AcDb.ObjectId.Null;
string old_name;
using (AcDb.BlockReference br =
res1.ObjectId.Open(AcDb.OpenMode.ForRead) as AcDb.BlockReference)
{
idBtr = br.BlockTableRecord;
using (AcDb.BlockTableRecord btr =
idBtr.Open(AcDb.OpenMode.ForRead) as AcDb.BlockTableRecord) {
old_name = btr.Name;
}
}
AcEd.PromptStringOptions pr2 =
new AcEd.PromptStringOptions("\nУкажите новое имя блока: ");
pr2.DefaultValue = old_name;
AcEd.PromptResult res2 = ed.GetString(pr2);
if (res2.Status != AcEd.PromptStatus.OK) return;
string new_name = res2.StringResult;
if (String.Compare(new_name, old_name, true) == 0) return;
if (AcDb.SymbolUtilityServices.ValidateCompatibleSymbolName(
new_name, true, false,
AcDb.SymbolUtilityServices.IsCompatibilityMode(doc.Database)) != AcRx.ErrorStatus.OK)
{
ed.WriteMessage("\nНедопустимое имя для блока \'{0}\'!",new_name);
return;
}
using (AcDb.BlockTableRecord btr =
idBtr.Open(AcDb.OpenMode.ForWrite) as AcDb.BlockTableRecord)
{
try {
btr.Name = new_name;
}
catch
{
ed.WriteMessage("\nБлок \'{0}\' нельзя переименовать в \'{1}\'!",
old_name, new_name);
}
}
}
}
}