-
- #region 提取一个图层上的各类元素
- [CommandMethod("BlockInLayerCAD")]
- public void BlockInLayerCAD()
- {
- //PromptStringOptions pStringOption = new PromptStringOptions("\n 输入一个图层名");
- //PromptResult layerName = pDocument.Editor.GetString(pStringOption);
-
- List layerNames = new List();
- 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 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.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();
|