欢迎加我的QQ群:193522571,一起来讨论、交流!

文字打断

/// <summary>
        /// 文字打断
        /// </summary>
        [CommandMethod("sText_break")]
        public void sText_break()
        {
            Document doc = MgdAcApplication.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            PromptEntityOptions optEnt = new PromptEntityOptions("\n选择单个文字");
            PromptEntityResult resEnt = ed.GetEntity(optEnt);
            if (resEnt.Status != PromptStatus.OK) return;

            using (Transaction acTrans = db.TransactionManager.StartTransaction())
            {
                if (acTrans.GetObject(resEnt.ObjectId, OpenMode.ForWrite).GetType().Name != "DBText")
                {
                    ed.WriteMessage("\n请选择单行文字");
                    return;
                }

                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;                

                DBText sText = acTrans.GetObject(resEnt.ObjectId, OpenMode.ForWrite) as DBText;
                //返回的数据中,sdPoint的X值是长度,Y值是高度
#if CadVersion2014
                Point2d sdPoint = Autodesk.AutoCAD.Internal.Utils.GetTextExtents(sText.TextStyleId, sText.TextString, sText.Height);
#elif CadVersion2010
                Point2d sdPoint = Autodesk.AutoCAD.Internal.Utils.GetTextExtents(sText.TextStyleId, sText.TextString, sText.Height);
#else
                Point2d sdPoint = Autodesk.AutoCAD.Internal.Utils.GetTextExtents(sText.TextStyle, sText.TextString, sText.Height);
#endif
                //取得单个字符的宽度,必须注意宽度系数,对于旋转的也可以
                double sCharLen = sdPoint.X * sText.WidthFactor / sText.TextString.Length;
                //取得鼠标离文字端点的直线长度
                Point3d MousePoint = resEnt.PickedPoint;
                Point3d TextPoint = sText.Position;
                //double sLen = Math.Sqrt(Math.Pow(MousePoint.X - TextPoint.X, 2) + Math.Pow(MousePoint.Y - TextPoint.Y, 2));
                double sLen = MousePoint.DistanceTo(TextPoint);
                //计算当前点离起点的字符数并向下取整
                double sCount = Math.Floor(sLen / sCharLen);

                //开始截断
                //第1个字符不变
                double sLenCount = sText.TextString.Length;
                string sTemp = sText.TextString;
                sText.TextString = sText.TextString.Substring(0, (int)sCount);

                //第2个字符位置
                DBText sCreateText = new DBText();
                double sHudu = sText.Rotation;
#if CadVersion2014
                Point2d sdP = Autodesk.AutoCAD.Internal.Utils.GetTextExtents(sText.TextStyleId, sText.TextString, sText.Height);
#elif CadVersion2010
                Point2d sdP = Autodesk.AutoCAD.Internal.Utils.GetTextExtents(sText.TextStyleId, sText.TextString, sText.Height);
#else
                Point2d sdP = Autodesk.AutoCAD.Internal.Utils.GetTextExtents(sText.TextStyle, sText.TextString, sText.Height);
#endif
                double sX = sText.Position.X + sdP.X * Math.Cos(sHudu) * sText.WidthFactor;
                double sY = sText.Position.Y + sdP.X * Math.Sin(sHudu) * sText.WidthFactor;
                sCreateText.Position = new Point3d(sX, sY, 0);
                sCreateText.TextString = sTemp.Substring((int)sCount);
                sCreateText.WidthFactor = sText.WidthFactor;
                sCreateText.Rotation = sHudu;                
                sCreateText.Height = sText.Height;

                acBlkTblRec.AppendEntity(sCreateText);
                acTrans.AddNewlyCreatedDBObject(sCreateText, true);

                acTrans.Commit();
            }
            ed.WriteMessage("\n文字打断完成");
        }

posted @ 2014-07-07 07:46  swtool  阅读(452)  评论(0)    收藏  举报
欢迎加我的QQ群:193522571,一起来讨论、交流!