using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace Mode
{
    public class MeasureOsnapOverrule : OsnapOverrule
    {
        public void StartSnap(ObjectId entId)//启动重定义
        {
            Overrule.AddOverrule(RXClass.GetClass(typeof(Line)), this, false);
            Overrule.Overruling = true;
            this.SetIdFilter(new ObjectId[] { entId });//过滤
        }
        public void StopSnap(ObjectId entId)//停止重定义
        {
            Overrule.RemoveOverrule(RXClass.GetClass(typeof(Line)), this);
            Overrule.Overruling = false;
            entId = ObjectId.Null;
        }
        public override void GetObjectSnapPoints(Entity entity, ObjectSnapModes snapMode, IntPtr gsSelectionMark,
            Point3d pickPoint, Point3d lastPoint, Matrix3d viewTransform,
            Point3dCollection snapPoints, IntegerCollection geometryIds)
        {
            Curve curve = entity as Curve;//强制转换为曲线
            snapPoints.Clear();//清除对象里的捕捉点集合
            snapMode = ObjectSnapModes.ModeNear;//ModeNear最近点
            snapPoints.Add(curve.StartPoint);//在起始点添加捕捉点
        }
        public override bool IsContentSnappable(Entity entity)
        {
            return false;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
[assembly: CommandClass(typeof(Mode.MyCommands))]
namespace Mode
{
    class MyCommands
    {
        [CommandMethod("add")]
        public static void RunMyOverruledSnap()//运行我的重定义捕捉
        {
            Document dwg = Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;
            ObjectId entId = ObjectId.Null;
            PromptEntityOptions opt = new PromptEntityOptions("\n选择一个直线:");
            opt.SetRejectMessage("\n选择不正确请选择直线");
            opt.AddAllowedClass(typeof(Line), true);
            PromptEntityResult res = ed.GetEntity(opt);
            if (res.Status == PromptStatus.OK)
            {
                entId = res.ObjectId;
            }
            MeasureOsnapOverrule mea = new MeasureOsnapOverrule();
            mea.StartSnap(entId);
        }
    }
}

实现CAD规则重定义,给直线添加一个捕捉点

posted on 2013-08-14 17:04  江春暮雪  阅读(565)  评论(0编辑  收藏  举报