Revit二次开发——提取剖面视图中截面轮廓

问题

在之前的业务开发中,需要用户定义剖面视图,然后通过程序自动提取相应剖面的截面轮廓。但是,由于Revit项目文件中的模型都是体的概念,即使剖面视图中显示的是二维轮廓的形状,依旧无法通过API直接获取相应的截面曲线或者截面坐标点。

解决思路

但是经过一番思考,我们可以采用曲线救国的方案,可以先通过将剖面视图自动导出为CAD的dwg文件格式,然后再通过Teigha.Net的类库去解析dwg文件,来获取到截面轮廓的曲线对象数据。

代码实例

剖面视图导出

SelectViewSectionList为剖面视图对象ViewSection的数组,将其批量导出为dwg文件。

		private Result GetSectionForRevit(ExternalCommandData arg)
        {
            var uidoc = arg.Application.ActiveUIDocument;
            var doc = uidoc.Document;

            for (int i = 0; i < this.SelectViewSectionList.Count; i++)
            {
                string path = this.ExportDwgByIsolateElements(doc, (ViewSection)SelectViewSectionList[i]);
                this.ConvertSectionDwgToTidaCAD(path);
            }
            return Result.Succeeded;
        }

        private string ExportDwgByIsolateElements(Document doc, ViewSection view)
        {
            if (view.IsTemporaryHideIsolateActive())
            {
                using (Transaction tx = new Transaction(doc))
                {
                    tx.Start("Disable Temporary Isolate");
                    view.DisableTemporaryViewMode(TemporaryViewMode.TemporaryHideIsolate);
                    tx.Commit();
                }
            }
            string dllPath = this.GetType().Assembly.Location;
            string path = Path.GetDirectoryName(dllPath) + "\\" + view.Name + ".dwg";
            bool isSuccess = ExportView.ExportDWG(doc, view, path);
            if (!isSuccess)
            {
                MessageBox.Show($"Revit输出{view.Name}图形失败");
            }

            return path;
        }

将Revit中Export函数进行简单的封装。

    public class ExportView 
    {
        public static bool ExportDWG(Document document, Autodesk.Revit.DB.View view, string filePath)
        {
            //导出设置选项
            DWGExportOptions exportOptions = new DWGExportOptions();
            exportOptions.FileVersion = ACADVersion.R2007;

            // Export the active view
            ICollection<ElementId> views = new List<ElementId>();
            views.Add(view.Id);
            // The document has to be saved already, therefore it has a valid PathName.
            bool exported = document.Export(Path.GetDirectoryName(filePath),
                Path.GetFileNameWithoutExtension(filePath), views, exportOptions);

            return exported;
        }
    }
解析Dwg文件

通过Teigha.Net类库解析dwg文件提取曲线信息,本案例采用的Teigha 3.09版本进行演示。

        private List<Curve> GetCurvesFromSectionDwg(string dwgPath) 
        {
            ReadCADUtils readCADUtils = new ReadCADUtils();
            return readCADUtils.GetCADCurves(dwgPath);
        }

将Teigha提取的功能进行简单的封装。

	public class ReadCADUtils
    {
        /// <summary>
        /// 取得CAD里的线,包括直线、多段线、圆曲线
        /// </summary>
        /// <param name="dwgFile"></param>
        /// <returns></returns>
        public List<Curve> GetCADCurves(string dwgFile)
        {
            List<Curve> result = new List<Curve>();
            using (new Services())
            {
                using (Database database = new Database(false, false))
                {
                    database.ReadDwgFile(dwgFile, FileShare.Read, true, "");
                    using (var trans = database.TransactionManager.StartTransaction())
                    {
                        using (BlockTable table = (BlockTable)database.BlockTableId.GetObject(OpenMode.ForRead))
                        {
                            using (SymbolTableEnumerator enumerator = table.GetEnumerator())
                            {
                                while (enumerator.MoveNext())
                                {
                                    using (BlockTableRecord record = (BlockTableRecord)enumerator.Current.GetObject(OpenMode.ForRead))
                                    {
                                        foreach (ObjectId id in record)
                                        {
                                            Entity entity = (Entity)id.GetObject(OpenMode.ForRead, false, false);
                                            switch (entity.GetRXClass().Name)
                                            {
                                                case "AcDbPolyline":
                                                    Teigha.DatabaseServices.Polyline pl = (Teigha.DatabaseServices.Polyline)entity;
                                                    Polyline polyline = new Polyline();
                                                    for (int i = 0; i < pl.NumberOfVertices; i++)
                                                    {
                                                        polyline.AddVertexAt(polyline.NumberOfVertices, new Point3d(pl.GetPoint2dAt(i).X, pl.GetPoint2dAt(i).Y, 0), pl.GetBulgeAt(i));
                                                    }
                                                    result.Add(polyline);
                                                    break;
                                                case "AcDbLine":
                                                    Teigha.DatabaseServices.Line line = (Teigha.DatabaseServices.Line)entity;
                                                    Point3d startPoint = new Point3d(line.StartPoint.X, line.StartPoint.Y, line.StartPoint.Z);
                                                    Point3d endPoint = new Point3d(line.EndPoint.X, line.EndPoint.Y, line.EndPoint.Z);
                                                    result.Add(new Line(startPoint, endPoint));
                                                    break;
                                                case "AcDbArc":
                                                    Teigha.DatabaseServices.Arc arc = (Teigha.DatabaseServices.Arc)entity;
                                                    double enda, stara;
                                                    if (arc.StartAngle > arc.EndAngle)
                                                    {
                                                        enda = arc.StartAngle;
                                                        stara = arc.EndAngle;
                                                    }
                                                    else
                                                    {
                                                        enda = arc.EndAngle;
                                                        stara = arc.StartAngle;
                                                    }
                                                    Point3d center = new Point3d(arc.Center.X, arc.Center.Y, arc.Center.Z);
                                                    result.Add(Arc.CreateArc(center, arc.Radius, stara, enda));
                                                    break;
                                                default:
                                                    break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return result;
        }
    }

其他

本人致力于建筑土木行业与计算机交叉融合,欢迎加入建筑与编程qq群,共同探讨技术难题,群号:516270086

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值