#region 提取一个图层上的各类元素 [CommandMethod("BlockInLayerCAD")] public void BlockInLayerCAD() { Document ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = HostApplicationServices.WorkingDatabase; //PromptStringOptions pStringOption = new PromptStringOptions(" 输入一个图层名"); //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(" 选择两个点作为文字取值范围"); txtPoint.Message = " 选择第一个点:"; PromptPointResult txtStartPoint = ed.Editor.GetPoint(txtPoint); startPoint = txtStartPoint.Value; txtPoint.Message = " 选择第二个点:"; 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