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


 

發佈了27 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章