- #region 提取一个图层上的各类元素
- [CommandMethod("BlockInLayerCAD")]
- public void BlockInLayerCAD()
- {
- //PromptStringOptions pStringOption = new PromptStringOptions("\n 输入一个图层名");
- //PromptResult layerName = pDocument.Editor.GetString(pStringOption);
-
- List<string> layerNames = new List<string>();
- using (Transaction tran = pDatabase.TransactionManager.StartTransaction())
- {
- #region 获取图层名字
- LayerTable pLayerTable = tran.GetObject(pDatabase.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 = string.Empty;
- string typeResult = string.Empty;
- FrmLayer frm = new FrmLayer(layerNames);
- frm.ShowDialog();
- if (frm.DialogResult == DialogResult.OK)
- {
- layerName = frm.selectLayer;
- typeResult = frm.blockType;
- }
- else
- {
- return;
- }
- TypedValue[] pTypedValue = new TypedValue[] { new TypedValue((int)DxfCode.LayerName, layerName) };
- SelectionFilter pSelectFilter = new SelectionFilter(pTypedValue);
- PromptSelectionResult pSelectionResult = pDocument.Editor.SelectAll(pSelectFilter);
- SelectionSet pSelectionSet = pSelectionResult.Value;
- StreamWriter txt = new StreamWriter(Stream.Null, Encoding.UTF8);
- if (typeResult != "全部")
- {
- if (File.Exists("C:\\cad\\cadMessage.txt"))
- {
- File.Delete("C:\\cad\\cadMessage.txt");
- }
- txt = File.AppendText("C:\\cad\\cadMessage.txt");
- }
- Point3d startPoint = new Point3d();
- Point3d endPoint = new Point3d();
- if (typeResult != "全部")
- {
- PromptPointOptions txtPoint = new PromptPointOptions("\n 选择两个点作为文字取值范围");
- txtPoint.Message = "\n 选择第一个点:";
- PromptPointResult txtStartPoint = pDocument.Editor.GetPoint(txtPoint);
- startPoint = txtStartPoint.Value;
- txtPoint.Message = "\n 选择第二个点:";
- PromptPointResult txtEndPoint = pDocument.Editor.GetPoint(txtPoint);
- endPoint = txtEndPoint.Value;
- Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(startPoint.X.ToString() + "," + startPoint.Y.ToString() + ";" + endPoint.X.ToString() + "," + endPoint.Y.ToString());
- }
- foreach (ObjectId selectedId in pSelectionSet.GetObjectIds())
- {
- Entity pEntity = tran.GetObject(selectedId, OpenMode.ForRead) as Entity;
- switch (typeResult)
- {
- case "全部":
- pEntity.ColorIndex = 4;
- break;
- 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)
- {
- txt.WriteLine((pEntity as MText).Contents.ToString());
- }
- }
- if ((pEntity as DBText) != null)
- {
- DBText pDBText = pEntity as DBText;
- //txtList.Add(pDBText);//这个留着后面测试分析用
- if (pDBText.Position.X > startPoint.X && pDBText.Position.X < endPoint.X)
- {
- if (pDBText.Position.Y < startPoint.Y && pDBText.Position.Y > endPoint.Y)
- {
- txt.WriteLine((pEntity as DBText).TextString.ToString());
- pDocument.Editor.WriteMessage(pDBText.TextString+"\n");
- txtList.Add(pDBText);//这个留着后面测试分析用
- }
- }
- }
- break;
- case "多段线":
- if ((pEntity as Polyline) != null)
- {
- Polyline pPolyline = pEntity as Polyline;
- txt.WriteLine("起点:" + pPolyline.StartPoint.X.ToString() + "," + pPolyline.StartPoint.Y.ToString());
- txt.WriteLine("终点:" + pPolyline.EndPoint.X.ToString() + "," + pPolyline.EndPoint.Y.ToString());
- }
- break;
- case "直线":
- if ((pEntity as Line) != null)
- {
- Line pLine = pEntity as Line;
- txt.WriteLine("起点:" + pLine.StartPoint.X.ToString() + "," + pLine.StartPoint.Y.ToString());
- txt.WriteLine("终点:" + pLine.EndPoint.X.ToString() + "," + pLine.EndPoint.Y.ToString());
- }
- break;
- case "圆":
- if ((pEntity as Circle) != null)
- {
- Circle pCircle = pEntity as Circle;
- txt.WriteLine("圆心:(" + pCircle.Center.X.ToString() + "," + pCircle.Center.Y.ToString() + "),半径:" + pCircle.Radius.ToString());
- }
- break;
- default:
- break;
- }
- }
- tran.Commit();
- txt.Flush();
- txt.Close();
- }
-
- }
- #endregion
上面是我写的取出DWG里面指定图层,指定类型的方法,然后涉及到一个窗体帮助选择图层和类型的,代码我附上,就不贴代码了哈。希望你有用...- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace CADappolication
- {
- public partial class FrmLayer : Form
- {
- public FrmLayer()
- {
- InitializeComponent();
- }
- List<string> layerName = new List<string>();
- public string selectLayer;
- public string blockType;
- public FrmLayer(List<string> layers)
- {
- layerName = layers;
- InitializeComponent();
- }
- public void FrmLayer_Load(object sender, EventArgs e)
- {
- if (layerName != null)
- {
- foreach (string layer in layerName)
- {
- comboBox1.Items.Add(layer);
- }
- comboBox2.Items.Add("全部");
- comboBox2.Items.Add("多段线");
- comboBox2.Items.Add("直线");
- comboBox2.Items.Add("文字");
- comboBox2.Items.Add("圆");
- comboBox2.SelectedIndex = 0;
- }
- else
- {
- MessageBox.Show("图层读取失败...");
- }
- }
- public void button1_Click(object sender, EventArgs e)
- {
- if (comboBox1.SelectedItem != null)
- {
- selectLayer = comboBox1.SelectedItem.ToString();
- blockType = comboBox2.SelectedItem.ToString();
- }
- else
- {
- MessageBox.Show("请选择个图层");
- return;
- }
- }
- }
- }
|