[摘要] 在做项目的过程中,轴网都是进行翻模或者拾取,而翻模出来的轴网往往都是不堪入目的,为了满足项目需要,做了轴网对齐的功能,废话不多说
在做项目的过程中,轴网都是进行翻模或者拾取,而翻模出来的轴网往往都是不堪入目的,为了满足项目需要,做了轴网对齐的功能,废话不多说,直接上干货。
首先做一些准备工作,先建立一个轴网过滤类,接下来会用到,代码如下:
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();
以上就是轴网对齐的基本代码,如有错误或者更好的实现方式请留言!
版权归个人所有转载请注明网址: