文字随线移动

 /// <summary>
        /// 作线的垂线
        /// </summary>
        [CommandMethod("sText_MoveOfLine")]
        public void sText_MoveOfLine()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;

            Entity entity = STools.Select(" 选择文字");
            if (entity == null || entity.GetType().Name != "DBText") return;

            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                entity = tran.GetObject(entity.ObjectId, OpenMode.ForWrite) as Entity;
                DBText MyText = entity as DBText;

                PromptEntityOptions optEnt = new PromptEntityOptions(" 选择曲线:");
                optEnt.SetRejectMessage(" 请选择曲线");
                optEnt.AddAllowedClass(typeof(Ellipse), true);
                optEnt.AddAllowedClass(typeof(Arc), true);
                optEnt.AddAllowedClass(typeof(Line), true);
                optEnt.AddAllowedClass(typeof(Spline), true);
                optEnt.AddAllowedClass(typeof(Autodesk.AutoCAD.DatabaseServices.Polyline), true);
                PromptEntityResult resEnt = ed.GetEntity(optEnt);
                if (resEnt.Status == PromptStatus.OK)
                {
                    using (Transaction trans = db.TransactionManager.StartTransaction())
                    {
                        BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                        BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                        Curve curve = (Curve)(trans.GetObject(resEnt.ObjectId, OpenMode.ForWrite));

                        TextJig jigText = new TextJig(MyText, curve);
                        PromptResult resJig = ed.Drag(jigText);
                        if (resJig.Status == PromptStatus.OK)
                        {
                            //btr.AppendEntity(jigText.ent);
                            //trans.AddNewlyCreatedDBObject(jigText.ent, true);
                            var mat = Matrix3d.Displacement(MyText.Position.GetVectorTo(jigText.ent.EndPoint));
                            MyText.TransformBy(mat);
                        }
                        trans.Commit();
                    }
                }
                tran.Commit();
            }
        }

//重载拖动类

   class TextJig:DrawJig
    {
        public Line ent;
        private DBText TextP1;
        private Curve curve;
        private Point3d curPt;

        /// <summary>
        /// 用于初始化一些事情
        /// </summary>
        /// <param name="entText">输入文字</param>
        /// <param name="entCurve1">输入曲线</param>
        public TextJig(DBText entText, Curve entCurve1)
        {
            ent = new Line(Point3d.Origin, Point3d.Origin);
            curve = entCurve1;
            TextP1 = entText;
        }

        protected override bool WorldDraw(WorldDraw draw)
        {
            Autodesk.AutoCAD.GraphicsInterface.WorldGeometry geo = draw.Geometry;
            if (geo != null)
            {
                geo.Draw(ent);
                geo.Draw(TextP1);
            }
            return true;
        }

        //与用户交互
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            JigPromptPointOptions optJigDis = new JigPromptPointOptions(" 请指定一个新的点");
            optJigDis.UserInputControls = UserInputControls.Accept3dCoordinates;
            PromptPointResult resJigDis = prompts.AcquirePoint(optJigDis);
            //curPt就是现在屏幕上鼠标位置点
            curPt = resJigDis.Value;
            //下面这句话很重要,不然会出错,必须保证这个点是在线上
            Point3d point = curve.GetClosestPointTo(curPt, false);
            if (resJigDis.Status == PromptStatus.Cancel)
            {
                return SamplerStatus.Cancel;
            }            
            Vector3d vtan = curve.GetFirstDerivative(point);
            Vector3d vnor0 = vtan.RotateBy(Math.PI / 2, Vector3d.ZAxis).GetNormal();
            Vector3d vnor1 = vtan.RotateBy(-Math.PI / 2, Vector3d.ZAxis).GetNormal();
            double vsize = (double)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("VIEWSIZE");
            Point3d point_per0 = point + vnor0.MultiplyBy(vsize / 5.0);
            Point3d point_per1 = point + vnor1.MultiplyBy(vsize / 5.0);
            //判断当前点在曲线的哪边,若相等的时候取默认的
            if (point_per1.DistanceTo(curPt) < point_per0.DistanceTo(curPt))
            {
                point_per0 = point_per1;
            }            
            ent.StartPoint = point;
            //以鼠标位置写入变长直线
            ent.EndPoint = curPt;
            //下面这句是写入固定长度直线
            //ent.EndPoint = point_per0;
            ent.ColorIndex = 4;
            Update();
            return SamplerStatus.OK;
        }

        private void Update()
        {
            double sX = ent.StartPoint.X;
            double sY = ent.StartPoint.Y;
            double eX = ent.EndPoint.X;
            double eY = ent.EndPoint.Y;
            double sAngle = STools.RadianToDegree(Math.Atan2(eY - sY, eX - sX));
            double sR = 0;
            if (sAngle < 0) sAngle += 360;

            if (sAngle >= 0 && sAngle <= 180)
            {
                sR = sAngle - 90;
            }
            if (sAngle > 180 && sAngle <= 360)
            {
                sR = sAngle - 270;
            }

            TextP1.Rotation = sR.DegreeToRadian();
            var mat = Matrix3d.Displacement(TextP1.Position.GetVectorTo(curPt));
            TextP1.TransformBy(mat);
        }
    }

原文地址:https://www.cnblogs.com/swtool/p/3828986.html