欢迎来到我的博客
Civil 3D开发与应用,欢迎加入QQ群:484124761
AutoCAD开发,欢迎加入QQ群:193522571

查询给定区域内曲面平均高程

2019年5月23日QQ群友聊天,

谈到如何查询曲面某范围内的平均高程,

对于整个曲面的平均高程,

在曲面特性内可以直接查看,

对于给定范围内的平均高程怎没有现成的命令可以实现,

虽然可以创建剪裁曲面,

创建新曲面后进行查看,

但对于多个范围的话,

重复操作也就很烦人了。

所以就又一次体现出学习一点点二次开发知识的必要性了!

 

曲面有现成的API可以查询给定范围的界内体积,

体积除以底面积,

就应该是平均高程!

就这么简单,

大致估计一下代码的行数,

应该不超过100行。

 

刚好电脑在手边,

那就试着写一下吧,

说不准这个功能那天自己也能用上。

 

代码如下:

 

 1 using Autodesk.AutoCAD.DatabaseServices;
 2 using Autodesk.AutoCAD.EditorInput;
 3 using Autodesk.AutoCAD.ApplicationServices;
 4 using Autodesk.AutoCAD.Geometry;
 5 using Autodesk.Civil.DatabaseServices;
 6 
 7 namespace 翻模工具
 8 {
 9     /// <summary>
10     /// 2019年5月23日
11     /// 计算选定范围内的平均曲面高程
12     /// </summary>
13     class MeanHeight
14     {
15         public void CalMeanHeigt()
16         {
17             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.CurrentDocument;
18             Database db = doc.Database;
19             TypedValue[] tv = new TypedValue[]
20                {
21                 new TypedValue((int)DxfCode.Start,"LWPOLYLINE")
22                };
23             SelectionFilter sf = new SelectionFilter(tv);
24             PromptSelectionResult psr = doc.Editor.GetSelection(sf);
25             if (psr.Status != PromptStatus.OK) return;
26             SelectionSet ss = psr.Value;
27             PromptEntityOptions peo = new PromptEntityOptions("拾取曲面");
28             peo.SetRejectMessage("\n请选择三角网曲面");
29             peo.AddAllowedClass(typeof(TinSurface), true);
30             PromptEntityResult per = doc.Editor.GetEntity(peo);
31             if (per.Status != PromptStatus.OK) return;
32             using (Transaction tr = doc.TransactionManager.StartTransaction())
33             {
34                 BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
35                 BlockTableRecord btr = bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;
36                 TinSurface sur = per.ObjectId.GetObject(OpenMode.ForRead) as TinSurface;
37                 foreach (ObjectId id in ss.GetObjectIds())
38                 {
39                     Polyline pl = id.GetObject(OpenMode.ForRead) as Polyline;
40                     Point3dCollection pts = new Point3dCollection();
41                     for (int i = 0; i < pl.NumberOfVertices; i++)
42                     {
43                         pts.Add(pl.GetPoint3dAt(i));
44                     }
45                     if (pts[0].DistanceTo(pts[pts.Count - 1]) != 0)
46                     {
47                         pts.Add(pts[0]);
48                     }
49                     double h = sur.GetBoundedVolumes(pts).Net / pl.Area;
50                     Point3d pt1 = pl.GeometricExtents.MaxPoint;
51                     Point3d pt2 = pl.GeometricExtents.MinPoint;
52                     Point3d pt = pt2 + (pt1 - pt2)/2;
53                     DBText t = new DBText();
54                     t.TextString = h.ToString("0.000");
55                     t.Position = pt;
56                     t.HorizontalMode = TextHorizontalMode.TextCenter;
57                     t.VerticalMode = TextVerticalMode.TextVerticalMid;
58                     t.AlignmentPoint = pt;
59                     t.Height = 2.5;
60                     t.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 1);
61                     btr.AppendEntity(t);
62                     tr.AddNewlyCreatedDBObject(t, true);
63                 }
64                 tr.Commit();
65             }
66         }
67     }
68 }

 

当然代码还不完善,

如果要有通用性,

还要进一步完善。

 

测试结果如下:

 

posted @ 2019-05-24 08:16  david96007  阅读(584)  评论(0编辑  收藏  举报