给定桩号获取纵断面中的高程值(c# for civil3d)

通过civil3d提供的api,也就是纵断面Profile类提供的方法---public double ElevationAt(double station),就可以很轻松的获取纵断面对象某桩号处的高程值,进而可以批量获取高程值。下面给出实现的代码。

首先写一个纵断面Profile类的扩展方法(扩展方法相当有用),用于返回某桩号处的高程值。

 1         /// <summary>
 2         /// 给定桩号值获取纵断面中的高程值,2018年4月21日
 3         /// 如果给定的桩号在纵断面范围内,则返回对应的高程值,否则返回null
 4         /// </summary>
 5         /// <param name="profile"></param>
 6         /// <param name="station"></param>
 7         /// <returns></returns>
 8         public static double? GetElevationFromProfile(this Profile profile, double station)
 9         {
10             double? elevation = null;//定义一个可空类型的数据
11             var startStation = profile.StartingStation;
12             var endStation = profile.EndingStation;
13             if (station >= startStation && station <= endStation)//判断桩号在纵断面桩号范围之内
14             {
15                 elevation = profile.ElevationAt(station);
16             }
17 
18             return elevation;
19         }

然后就可以通过调用上面的方法,就可以获取桩号对应的高程数据了。

 1         [CommandMethod("getElvFromProfile")]
 2         public static void GetElvFromProfile()
 3         {
 4             Document doc = AcadApp.DocumentManager.MdiActiveDocument;
 5             Database db = doc.Database;
 6             Editor ed = doc.Editor;
 7             CivilDocument civilDoc = CivilApplication.ActiveDocument;
 8             var opt = new PromptEntityOptions("\n 请拾取一个纵断面对象 ");
 9             opt.SetRejectMessage("\n 对象必须是纵断面对象");
10             opt.AddAllowedClass(typeof(Profile), false);
11             var res = ed.GetEntity(opt);
12             if (res.Status != PromptStatus.OK)
13             {
14                 ed.WriteMessage("\n你已经取消了选择");
15                 return;
16             }
17             var doubleResult = ed.GetDouble("\n请输入一个桩号");
18             double station = doubleResult.Value;
20             using (DocumentLock docLock = doc.LockDocument())
21             using (Transaction trans = db.TransactionManager.StartTransaction())
22             {
23                 Profile profile = trans.GetObject(res.ObjectId, OpenMode.ForRead) as Profile;//获取纵断面对象
24                 double? elevation = profile.GetElevationFromProfile(station);//调用扩展方法,获取高程值
25                 //ed.WriteMessage(profile.GetElevationFromProfile(station).ToString());
26                 ed.WriteMessage(elevation?.ToString());
27             } 
28         }

如过想要批量获取高程值得话,借助循环就可以完成了。

猜你喜欢

转载自www.cnblogs.com/whlkx/p/8967681.html