BIM建筑网
更专业的BIM技术学习网站!


Revit二次开发|轴网自动对齐

VIP免费下载全站资源
VIP优惠来袭,免费下载全站资料和课程,技术问题可以随时提问;
查看VIP会员

在做项目的过程中,轴网都是进行翻模或者拾取,而翻模出来的轴网往往都是不堪入目的,为了满足项目需要,做了轴网对齐的功能,废话不多说,直接上干货。

首先做一些准备工作,先建立一个轴网过滤类,接下来会用到,代码如下:

 class GridSelectionFilter : ISelectionFilter
 {


 public bool AllowElement(Element element)
 {
 int idValue = element.Category.Id.IntegerValue;
 if (idValue == (int)BuiltInCategory.OST_Grids)
 {
 return true;
 }
 return false;
 
 }

 public bool AllowReference(Reference reference, XYZ position)
 {
 return false;
 
 }
 }

接下来就是真正的主题轴网对齐,代码如下:

 //新建轴网选择过滤器;
 GridSelectionFilter gridSelectionFilter = new GridSelectionFilter();

 //框选轴网;
 IList<Element> re = sel.PickElementsByRectangle(gridSelectionFilter, "框选轴网");

 //选择一点作为轴网的对齐点;
 XYZ point = sel.PickPoint("请选择对齐点");

 //新建修改轴网事务;
 Transaction transaction = new Transaction(doc);
 transaction.Start("修改轴网");

 if (re.Count > 1)
 {
 foreach (Element elem in re)
 {
 Grid grid = elem as Grid;
 Line line = grid.Curve as Line;
 XYZ start = line.GetEndPoint(0);
 XYZ end = line.GetEndPoint(1);
 // string grid_Name = grid.Name;

 // TaskDialog.Show("revit", "start"+start.ToString() + "end:"+end.ToString());
 //判断选择点距离轴网最近的端点
 double minDistance = point.DistanceTo(end);
 double Maxdistance = point.DistanceTo(start);
 if (minDistance < Maxdistance)
 { //终止点距离选择点较近;
 XYZ tempPoint = new XYZ(point.X - end.X, point.Y - end.Y, point.Z - end.Z);
 double distance = Math.Sqrt(tempPoint.X * tempPoint.X + tempPoint.Y * tempPoint.Y + tempPoint.Z * tempPoint.Z);
 //获得轴网方向向量与终止点连接选择点向量的夹角;
 double angle = line.Direction.AngleTo(tempPoint);
 //新终止点的坐标为起始点加长度乘以单位向量;
 XYZ newEnd = start + (line.Length + distance * Math.Cos(angle)) * line.Direction;

 //构造新的线段用来生成新的轴网;
 Line newLine = Line.CreateBound(start, newEnd);

 Grid newgrid = Grid.Create(doc, newLine);
 string str = grid.Name;
 doc.Delete(grid.Id);
 //将原有轴网的名字赋值给新轴网;
 newgrid.Name = str;
 }
 else
 {

 //起始点距离轴网较近,构件新向量;
 XYZ tempPoint = new XYZ(point.X - start.X, point.Y - start.Y, point.Z - start.Z);
 double distance = Math.Sqrt(tempPoint.X * tempPoint.X + tempPoint.Y * tempPoint.Y + tempPoint.Z * tempPoint.Z);
 //获得轴网方向向量与起始点连接选择点向量的夹角;
 double angle = line.Direction.AngleTo(tempPoint);
 //新起始点的坐标为终止点减长度乘以单位向量;
 XYZ newStart = end - (line.Length - distance * Math.Cos(angle)) * line.Direction;

 //构造新的线段用来生成新的轴网;
 Line newLine = Line.CreateBound(newStart, end);

 Grid newgrid = Grid.Create(doc, newLine);
 string str = grid.Name;
 doc.Delete(grid.Id);
 //将原有轴网的名字赋值给新轴网;
 newgrid.Name = str;


 }


 }


 }
 transaction.Commit();

 

以上就是轴网对齐的基本代码,如有错误或者更好的实现方式请留言!

版权归个人所有转载请注明网址:

微信公众号:xuebim
关注建筑行业BIM发展、研究建筑新技术,汇集建筑前沿信息!
← 微信扫一扫,关注我们+
赞(0) 打赏
BIM建筑网 » Revit二次开发|轴网自动对齐
100套内部BIM资料,限时领!
付费搞来的,大家都在学!
领取资料 AI解答

评论 抢沙发

评论前必须登录!

 

BIM建筑网,更专业的BIM技术学习网站!

关注建筑新动态,分享建筑新技术

联系我们关于BIM建筑网

觉得文章有用就打赏一下小编吧

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

扫码登录

微信「关注」,快捷登录
扫码关注后会自动登录
注册登录代表您已同意《用户许可协议》
账号登录 | 其他登录

|登录

找回密码

|账号登录注册