using System;
using System.Runtime.InteropServices;
using System.Reflection;
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;
using Microsoft.Win32;
[assembly: CommandClass(typeof(Rivilis.RemoveConsAssoc))]
namespace Rivilis
{
public class RemoveConsAssoc
{
// Заголовки диалогового окна в русском и английском AutoCAD,
// от которого мы хотим избавится.
const string rusTitle = "Размерные зависимости — неассоциативный размер";
const string engTitle = "Dimensional Constraints - Non Associative Dimension";
const uint WM_CLOSE = 0x0010;
static LocalCbtHook hook = new LocalCbtHook();
static void WinActivated(object sender, CbtEventArgs e)
{
if (e.Title.CompareTo(rusTitle) == 0 || e.Title.CompareTo(engTitle) == 0)
{
// Сразу закрываем окно
LocalCbtHook.SendMessage(e.Handle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}
[CommandMethod("RemConsAssoc", CommandFlags.Modal)]
public void RemConsAssoc()
{
AcAp.Document doc = AcAp.Application.DocumentManager.MdiActiveDocument;
AcDb.Database db = doc.Database;
AcEd.Editor ed = doc.Editor;
ed.WriteMessage("\nВыберите примитивы для удаления зависимостей");
AcEd.PromptSelectionOptions prSel = new AcEd.PromptSelectionOptions();
AcEd.PromptSelectionResult resSel = ed.GetSelection(prSel);
if (resSel.Status != AcEd.PromptStatus.OK)
{
ed.WriteMessage("\nНичего не выбрано!");
return;
}
if (!hook.IsInstalled)
{
hook.WindowActivated += WinActivated;
hook.Install();
}
foreach (ObjectId id in resSel.Value.GetObjectIds())
{
DeleteAllConstraintsOnEntity(id);
}
}
public void DeleteAllConstraintsOnEntity(ObjectId id)
{
using (AcDb.DBObject obj = id.Open(AcDb.OpenMode.ForWrite))
{
ObjectIdCollection idsAct = AcDb.AssocAction.GetActionsDependentOnObject(obj, true, true);
ObjectIdCollection idsDep = AcDb.AssocDependency.GetDependenciesOnObject(obj, true, true);
for (int i = 0; i < idsAct.Count; i++)
{
using (AcDb.Assoc2dConstraintGroup objAct =
idsAct[i].Open(AcDb.OpenMode.ForWrite) as AcDb.Assoc2dConstraintGroup)
{
if (objAct != null)
{
for (int j = 0; j < idsDep.Count; j++)
objAct.DeleteConstrainedGeometry(idsDep[j]);
}
}
}
}
}
}
}