Revit二次开发——读取cad中的文字信息
Posted mqxs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Revit二次开发——读取cad中的文字信息相关的知识,希望对你有一定的参考价值。
Revit读取cad的文字信息需要借助Teigha的开源dll,在程序中添加下图中红色框的dll文件的引用,其他的dll文件全部放在同一个文件夹中即可,运行的时候,会自动把这些dll文件全部复制到bin文件当中,同时,在Revit中运行插件,Revit也会自动加载这些dll文件。
以下是关键方法,在Revit里读取cad文字信息,图层信息,几何信息等。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Autodesk.Revit; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using CreatBridgeForRevit2018.ElementsSelect; using CreatBridgeForRevit2018.Filter; using Teigha.Runtime; using Teigha.DatabaseServices; using System.IO; using System.Collections; using Teigha.Geometry; namespace CreatBridgeForRevit2018.ReadCAD { class ReadCADUtils { /// <summary> /// 取得链接cad的路径 /// </summary> /// <param name="cadLinkTypeID"></param> /// <param name="revitDoc"></param> /// <returns></returns> public string GetCADPath(ElementId cadLinkTypeID,Document revitDoc) { CADLinkType cadLinkType = revitDoc.GetElement(cadLinkTypeID) as CADLinkType; return ModelPathUtils.ConvertModelPathToUserVisiblePath(cadLinkType.GetExternalFileReference().GetAbsolutePath()); } /// <summary> /// 取得CAD的文字信息 /// </summary> /// <param name="dwgFile"></param> /// <returns></returns> public List<CADTextModel> GetCADTextInfo(string dwgFile) { List<CADTextModel> listCADModels = new List<CADTextModel>(); 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()) { StringBuilder sb = new StringBuilder(); 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); CADTextModel model = new CADTextModel(); switch (entity.GetRXClass().Name) { case "AcDbText": Teigha.DatabaseServices.DBText text = (Teigha.DatabaseServices.DBText)entity; model.Location = ConverCADPointToRevitPoint(text.Position); model.Text = text.TextString; model.Angel = text.Rotation; model.Layer = text.Layer; listCADModels.Add(model); break; case "AcDbMText": Teigha.DatabaseServices.MText mText = (Teigha.DatabaseServices.MText)entity; model.Location = ConverCADPointToRevitPoint(mText.Location); model.Text = mText.Text; model.Angel = mText.Rotation; model.Layer = mText.Layer; listCADModels.Add(model); break; } } } } } } } } } return listCADModels; } /// <summary> /// 取得cad的图层名称 /// </summary> /// <param name="dwgFile"></param> /// <returns></returns> public IList<string> GetLayerName(string dwgFile) { IList<string> cadLayerNames = new List<string>(); using (new Services()) { using (Database database = new Database(false, false)) { database.ReadDwgFile(dwgFile, FileShare.Read, true, ""); using (var trans = database.TransactionManager.StartTransaction()) { using (LayerTable lt = (LayerTable)trans.GetObject(database.LayerTableId, OpenMode.ForRead)) { foreach (ObjectId id in lt) { LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(id, OpenMode.ForRead); cadLayerNames.Add(ltr.Name); } } trans.Commit(); } } } return cadLayerNames; } /// <summary> /// 取得CAD里的线,包括直线、多段线、圆曲线 /// </summary> /// <param name="dwgFile"></param> /// <returns></returns> public List<CADGeometryModel> GetCADCurves(string dwgFile) { List<CADGeometryModel> listCADModels = new List<CADGeometryModel>(); 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()) { StringBuilder sb = new StringBuilder(); 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); CADGeometryModel geoModel = new CADGeometryModel(); switch (entity.GetRXClass().Name) { case "AcDbPolyline": Teigha.DatabaseServices.Polyline pl = (Teigha.DatabaseServices.Polyline)entity; IList<XYZ> listPoints = new List<XYZ>(); for (int i = 0; i < pl.NumberOfVertices; i++) { listPoints.Add(new XYZ(MillimetersToUnits(pl.GetPoint2dAt(i).X), MillimetersToUnits(pl.GetPoint2dAt(i).Y), 0)); } PolyLine polyLine = PolyLine.Create(listPoints); listCADModels.Add(new CADGeometryModel() {CadPolyLine= polyLine,LayerName=pl.Layer }); break; case "AcDbLine": Teigha.DatabaseServices.Line line = (Teigha.DatabaseServices.Line)entity; Autodesk.Revit.DB.Line revitLine = Autodesk.Revit.DB.Line.CreateBound(ConverCADPointToRevitPoint(line.StartPoint), ConverCADPointToRevitPoint(line.EndPoint)); listCADModels.Add(new CADGeometryModel() {CadCurve=revitLine as Autodesk.Revit.DB.Curve ,LayerName=line.Layer}); 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; } Autodesk.Revit.DB.Arc revitArc = Autodesk.Revit.DB.Arc.Create(Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(new XYZ( arc.Normal.X, arc.Normal.Y, arc.Normal.Z), ConverCADPointToRevitPoint(arc.Center)), MillimetersToUnits(arc.Radius), stara, enda); listCADModels.Add(new CADGeometryModel() { CadCurve = revitArc as Autodesk.Revit.DB.Curve, LayerName = arc.Layer }); break; default: break; } } } } } } } } } return listCADModels; } /// <summary> /// 毫米转化成英寸 /// </summary> /// <param name="value"></param> /// <returns></returns> private double MillimetersToUnits(double value) { return UnitUtils.ConvertToInternalUnits(value, DisplayUnitType.DUT_MILLIMETERS); } /// <summary> /// 将CAD里的点转化为Revit里的点 /// </summary> /// <param name="point"></param> /// <returns></returns> private XYZ ConverCADPointToRevitPoint(Point3d point) { return new XYZ(MillimetersToUnits(point.X), MillimetersToUnits(point.Y), MillimetersToUnits(point.Z)); } } }
需要额外引用一些dll文件。其中红色框框里的dll文件是需要加载进vs引用里的。其余的放在debug文件夹里。
结果如下,可以在Revit中正确读取cad的图层信息,文字位置,几何信息等:
原文链接:https://blog.csdn.net/niuge8905/article/details/77204680
以上是关于Revit二次开发——读取cad中的文字信息的主要内容,如果未能解决你的问题,请参考以下文章