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 = (BlockTa