using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AcApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcRt = Autodesk.AutoCAD.Runtime;
using AcAs = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcEd = Autodesk.AutoCAD.EditorInput;
[assembly: AcRt.CommandClass(typeof(Bargool.CommandClass))]
namespace Bargool
{
class CommandClass
{
[AcRt.CommandMethod("RemConsAssoc", AcRt.CommandFlags.Modal)]
public void RemConsAssoc()
{
AcAs.Document doc = AcApp.DocumentManager.MdiActiveDocument;
AcDb.Database db = doc.Database;
AcEd.Editor ed = doc.Editor;
AcEd.PromptEntityOptions prEnt = new AcEd.PromptEntityOptions("\nВыберите вхождение блока для удаления зависимостей: ");
prEnt.SetRejectMessage("Должно быть вхождение блока!");
prEnt.AddAllowedClass(typeof(AcDb.BlockReference), true);
AcEd.PromptEntityResult resEnt = ed.GetEntity(prEnt);
if (resEnt.Status != AcEd.PromptStatus.OK)
return;
DeleteAllConstraintsOnEntity(resEnt.ObjectId);
}
private void DeleteAllConstraintsOnEntity(AcDb.ObjectId objectId)
{
using (AcDb.Transaction tr = AcDb.HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
AcDb.BlockReference bref = tr.GetObject(objectId, AcDb.OpenMode.ForRead) as AcDb.BlockReference;
if (!bref.IsDynamicBlock)
return;
// Подбираем имя для статического блока и конвертим динамику в статику
int index = 0;
string blocknamebase = "ExDyn";
AcDb.BlockTable bt = (AcDb.BlockTable)tr.GetObject(AcDb.HostApplicationServices.WorkingDatabase.BlockTableId, AcDb.OpenMode.ForRead);
while (bt.Has(blocknamebase + index.ToString()))
index++;
string blockname = blocknamebase + index.ToString();
bref.ConvertToStaticBlock(blockname);
AcDb.BlockTableRecord btr = tr.GetObject(bt[blockname], AcDb.OpenMode.ForWrite) as AcDb.BlockTableRecord;
if (btr.ExtensionDictionary.IsNull)
return;
AcDb.DBDictionary extDic = tr.GetObject(btr.ExtensionDictionary, AcDb.OpenMode.ForWrite) as AcDb.DBDictionary;
foreach (AcDb.DBDictionaryEntry entry in extDic)
{
extDic.Remove(entry.Key);
}
btr.ReleaseExtensionDictionary();
tr.Commit();
}
}
}
}