using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
[assembly: CommandClass(typeof(Rivilis.OsnapTest))]
namespace Rivilis
{
public class OsnapTest
{
[CommandMethod("OsTest")]
public void OsTest()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) return;
Editor ed = doc.Editor;
while (true)
{
using (PointMonitor pMon = new PointMonitor())
{
PromptPointOptions ppo =
new PromptPointOptions("\nУкажите точку (ENTER - завершение): ");
PromptPointResult ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK) break;
if (pMon.osnaps != 0)
{
ed.WriteMessage("\nОбъектные привязки: {0}", pMon.osnaps);
ed.WriteMessage("\nУказанная точка: {0}", pMon.pointPick);
ed.WriteMessage("\nТочка с учетом привязки: {0}", pMon.pointOsnap);
foreach (FullSubentityPath path in pMon.paths)
{
ed.WriteMessage("\nObjectIDs=");
foreach (ObjectId id in path.GetObjectIds())
ed.WriteMessage(" {0}", id);
if (path.SubentId.IndexPtr != IntPtr.Zero)
ed.WriteMessage(" -> SubentId = {0}",
path.SubentId.IndexPtr);
}
}
}
}
}
}
//////////////////////////////////////////////////////////////////////////
// Класс - обертка для PointMonitor
//////////////////////////////////////////////////////////////////////////
internal class PointMonitor : IDisposable
{
public FullSubentityPath[] paths = null;
public Point3d pointPick = Point3d.Origin;
public Point3d pointOsnap = Point3d.Origin;
public ObjectSnapMasks osnaps = (ObjectSnapMasks)0;
public PointMonitor()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc != null) {
ed = doc.Editor;
ed.TurnForcedPickOn();
ed.PointMonitor += ed_PointMonitor;
}
}
public void Dispose()
{
if (ed != null) {
ed.TurnForcedPickOff();
ed.PointMonitor -= ed_PointMonitor;
}
}
private Editor ed = null;
//////////////////////////////////////////////////////////////////////////
/// Обработчик события PointMonitor
//////////////////////////////////////////////////////////////////////////
void ed_PointMonitor(object sender, PointMonitorEventArgs e)
{
InputPointContext context = e.Context;
if (context.PointComputed && (context.History & PointHistoryBits.ObjectSnapped) != 0)
{
// Чистим предыдущее значение
paths = null; pointOsnap = pointPick; osnaps = (ObjectSnapMasks)0;
// Привязка
osnaps = context.ObjectSnapMask;
// Точка привязки
pointOsnap = context.ObjectSnappedPoint;
// Объекты участвующие в вычислении привязки
paths = context.GetPickedEntities();
}
else
{
// Указанная пользователем точка (без учета привязок)
pointPick = context.RawPoint;
}
}
}
}