- UID
- 38377
- 积分
- 135
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2003-3-25
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
- using System;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Colors;
- using Autodesk.AutoCAD.Windows;
- using Autodesk.AutoCAD.Geometry;
- namespace MyTools
- {
- /// <summary>
- /// TestClass的说明。
- /// </summary>
- public class TestClass
- {
- public TestClass()
- {
- }
- public void CreateTable(Point3dCollection p3dC, string scale,Point3d p3d,int t)
- {
- int numRows = p3dC.Count + 1;
- string strT;
- if (t == 3)
- {
- strT = "#0.000";
- }
- else
- {
- strT = "#0.00";
- }
- Database db = HostApplicationServices.WorkingDatabase;
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- Transaction trans = db.TransactionManager.StartTransaction();
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
- double dScale = double.Parse(scale);
- double rowHeight;
- double columnWidth1;
- double columnWidth2;
- double textHeight1;
- double textHeight2;
- double columnWidth3;
- try
- {
- textHeight1 = 1.25 * (dScale / 500);//表头的字体高度
- textHeight2 = 0.9 * (dScale / 500);//坐标点的数字字体高度
- rowHeight = 2 * (dScale / 500);//行高
- columnWidth1 = 4 * (dScale / 500);//1列的列宽
- columnWidth2 = 11 * (dScale / 500);//2至3列的列宽
- columnWidth3 = 6 * (dScale / 500);//4列的列宽
- Table myTable = new Table();
- myTable.Position = p3d;
- myTable.NumRows = numRows;
- myTable.NumColumns = 4;
- myTable.SetColumnWidth(0,columnWidth1);
- myTable.SetColumnWidth(1,columnWidth2);
- myTable.SetColumnWidth(2,columnWidth2);
- myTable.SetColumnWidth(3,columnWidth3);
- myTable.SetRowHeight(rowHeight);
- ed.WriteMessage(rowHeight.ToString());
- //设置表头
- myTable.SetTextHeight(0, 0, textHeight1);
- myTable.SetTextString(0, 0, "序号");
- myTable.SetTextHeight(0, 1, textHeight1);
- myTable.SetTextString(0, 1, "X坐标");
- myTable.SetTextHeight(0, 2, textHeight1);
- myTable.SetTextString(0, 2, "Y坐标");
- myTable.SetTextHeight(0, 3, textHeight1);
- myTable.SetTextString(0, 3, "圆弧标记");
- //将坐标数值输入到表格中
- for (int i = 0; i < p3dC.Count; i++)
- {
- int n = i + 1;
- myTable.SetTextHeight(n, 0, textHeight2);
- myTable.SetTextString(n, 0, n.ToString());
- myTable.SetTextHeight(n, 1, textHeight2);
- myTable.SetTextString(n, 1, p3dC.X.ToString(strT));
- myTable.SetTextHeight(n, 2, textHeight2);
- myTable.SetTextString(n, 2, p3dC.Y.ToString(strT));
- myTable.SetTextHeight(n, 3, textHeight2);
- myTable.SetTextString(n, 3, p3dC.Z.ToString());
- }
- btr.AppendEntity(myTable);
- trans.AddNewlyCreatedDBObject(myTable, true);
- trans.Commit();
- }
- catch (System.Exception ex)
- {
- Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("输入的比例尺有误,请重新输入"+ ex.Message.ToString());
- }
- finally
- {
- trans.Dispose();
- }
- }
- static private Point3dCollection GetPLPoint(ObjectId PLid)//并非真正的3d集合,Z如果是1的话表示此点是圆弧的中点
- {
- Point3dCollection p3dCollection = new Point3dCollection();
- Transaction trans = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
- using (trans)
- {
- DBObject obj = trans.GetObject(PLid, OpenMode.ForRead);
- if (obj.GetType().Name == "Polyline"/*此语句可获得所获取对象obj的类型,如Polyline,Arc,Circle等。*/)
- {
- Polyline PL = obj as Polyline;
- int vn = PL.NumberOfVertices;
- for (int i = 0; i < vn; i++)
- {
- double vBulge = PL.GetBulgeAt(i);
- if (vBulge != 0)
- {
- p3dCollection.Add(PL.GetPoint3dAt(i));
- double len0 = PL.GetDistAtPoint(PL.GetPoint3dAt(i));
- double len1 = PL.GetDistAtPoint(PL.GetPoint3dAt(i + 1));
- double midlen = (len0 + len1) / 2;
- Point3d midP3d = PL.GetPointAtDist(midlen);
- Point3d m = new Point3d(midP3d.X, midP3d.Y, 1);
- p3dCollection.Add(m);
- }
- else
- {
- p3dCollection.Add(PL.GetPoint3dAt(i));
- }
- }
- }
- trans.Commit();
- trans.Dispose();
- return p3dCollection;
- }
- }
- [CommandMethod("zbb")]
- public void CreateVertexTable()
- {
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- PromptEntityResult per = ed.GetEntity("\n请选择多段线");
- Point3dCollection p3dCo = GetPLPoint(per.ObjectId);
- if (per.Status != PromptStatus.OK)
- {
- ed.WriteMessage("\n选择选段错误");
- }
- for (int i = 0; i < p3dCo.Count; i++)
- {
- Point3d point3dd = p3dCo;
- ed.WriteMessage("\n" + point3dd.ToString());
- }
- Point3d p3d;
- PromptPointOptions prPointOptions = new PromptPointOptions("\n请选择表格插入点:");
- PromptPointResult prPointRes = ed.GetPoint(prPointOptions);
- if (prPointRes.Status == PromptStatus.OK)
- {
- p3d = prPointRes.Value;
- }
- PromptStringOptions pso = new PromptStringOptions("\n请输入比例尺:");
- PromptResult prScale = ed.GetString(pso);
- string strScale = prScale.StringResult.ToString();
- if (prScale.Status != PromptStatus.OK)
- {
- ed.WriteMessage("\n输入比例尺错误");
- }
- PromptKeywordOptions opt = new PromptKeywordOptions("\n选择小数位数[三位(3)]<两位(2)>");
- opt.Keywords.Add("3");
- opt.Keywords.Add("2");
- PromptResult result = ed.GetKeywords(opt);
- if (result.Status == PromptStatus.OK)
- {
- switch (result.StringResult)
- {
- case "3":
- CreateTable(p3dCo, strScale, p3d, 3);
- break;
- case "2":
- CreateTable(p3dCo, strScale, p3d, 2);
- break;
- }
- }
- }
- }
- }
|
|