How to get AutoCAD Mtext content

#region 提取一个图层上的各类元素
        [CommandMethod("BlockInLayerCAD")]
        public void BlockInLayerCAD()
        {
            Document ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = HostApplicationServices.WorkingDatabase;

            //PromptStringOptions pStringOption = new PromptStringOptions("\n 输入一个图层名");
            //PromptResult layerName = pDocument.Editor.GetString(pStringOption);
            List<string> layerNames = new List<string>();
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                #region 获取图层名字
                LayerTable pLayerTable = tran.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                foreach (ObjectId pObjectId in pLayerTable)
                {
                    LayerTableRecord pLayerTableRecord = tran.GetObject(pObjectId, OpenMode.ForRead) as LayerTableRecord;
                    layerNames.Add(pLayerTableRecord.Name);
                }
                #endregion
                string layerName = layerNames[0];
                string typeResult ="文字";
 
                TypedValue[] pTypedValue = new TypedValue[] { new TypedValue((int)DxfCode.LayerName, layerName) };
                SelectionFilter pSelectFilter = new SelectionFilter(pTypedValue);
                PromptSelectionResult pSelectionResult = ed.Editor.SelectAll(pSelectFilter);
                SelectionSet pSelectionSet = pSelectionResult.Value;
                Point3d startPoint = new Point3d();
                Point3d endPoint = new Point3d();
                if (typeResult != "全部")
                {
                    PromptPointOptions txtPoint = new PromptPointOptions("\n 选择两个点作为文字取值范围");
                    txtPoint.Message = "\n 选择第一个点:";
                    PromptPointResult txtStartPoint = ed.Editor.GetPoint(txtPoint);
                    startPoint = txtStartPoint.Value;
                    txtPoint.Message = "\n 选择第二个点:";
                    PromptPointResult txtEndPoint = ed.Editor.GetPoint(txtPoint);
                    endPoint = txtEndPoint.Value;
                }
                foreach (ObjectId selectedId in pSelectionSet.GetObjectIds())
                {
                    Entity pEntity = tran.GetObject(selectedId, OpenMode.ForRead) as Entity;
                    switch (typeResult)
                    {
                        case "文字":
                            if ((pEntity as MText) != null)
                            {
                                MText mText = pEntity as MText;
                                if (mText.Location.X > startPoint.X && mText.Location.Y < startPoint.Y && mText.Location.X < endPoint.X && mText.Location.Y > endPoint.Y)
                                {
                                    Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog((pEntity as MText).Text);
                                }
                            }
                            break;
                    }
                }
                tran.Commit();
            }
        }
#endregion


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值