首页 > 编程语言 >【转】【Revit】revit二次开发——基于teigha读取CAD几何与文字(提供源码、引用包与异常解决)

【转】【Revit】revit二次开发——基于teigha读取CAD几何与文字(提供源码、引用包与异常解决)

时间:2023-04-02 18:36:15浏览次数:51  
标签:包与 arc Teigha database 源码 new 二次开发 using Revit

1.下载64位Teigha完整包:

链接: https://pan.baidu.com/s/1KXW54YlkkWJZNQYhbc99kw 提取码: 0505

2.VS平台架构选择64位,.net版本4.0以上:

 

 

3.dll必须手动指定引用:(重点)

static string dll = @"D:\Desktop\Rvt\bin\Debug\TD_Mgd.dll";//引用位置
Assembly a = Assembly.UnsafeLoadFrom(dll);

4.以上注意事项处理不对,会引发以下异常:
1.不是有效的32位程序:
原因:

(1)加载外部程序集TD_Mgd.dll不合适

(2)处理器架构与引用dll不匹配

解决办法:

(1)使用64位Teigha库,不能使用32位,因为RevitAPI与RevitAPIUI是使用64位;

(2)更改平台架构为64位:

 

 

2.找不到指定模块或依赖项
这个问题困扰很多人,大多的解决办法是不行更有骗人的,其实就两行代码的问题。

 

问题的根本原因是 :

在类库项目中引用了外部程序集TD_Mgd.dll,.Net的安全机制阻止了本地的Assembly。具体可参见该博客:问题解决方案

解决办法:使用3说的,手动引用即可

代码参考:

1.revit接口代码

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
//1.通过选择获取元素
 Reference reference1 = uiDoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element,"点击图元");
Element item = revitDoc.GetElement(reference1);
ElementId elementId = item.GetTypeId();
Element element = revitDoc.GetElement(elementId);
//2.获取CAD链接文件路径
string cadStirng = Path.GetDirectoryName(revitDoc.PathName) + "\\" + revitDoc.GetElement(element .GetTypeId()).Name;
//3.获取CAD文字:
ReadCADUtils readCADUtils = new ReadCADUtils();
List<CADTextModel> cADTextList=readCADUtils.GetCADTextInfo(cadStirng);
foreach (var item in cADTextList)
{
  TaskDialog.Show("revit", item.Text+"\n"+"坐标原点为:"+item.Location);
}
return Result.Succeed;
}

2.获取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
    {
      static string dll = @"D:\Desktop\RvtToDwg\bin\Debug\TD_Mgd.dll";//手动指定引用dll路径
      Assembly a = Assembly.UnsafeLoadFrom(dll);
        /// <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));
        }
    }
public class CADTextModel
    {
        public XYZ Location { get; set; }
        public string Text { get; set; }
        public double Angle { get; set; }
        public string Layer { get; set; }
        public CADTextModel()
        {
            Location = null;
            Text = "";
            Angle = 0;
            Layer = "";
        }       
    }
}

原文地址:https://blog.csdn.net/helloyangkl/article/details/124859184

 

标签:包与,arc,Teigha,database,源码,new,二次开发,using,Revit
From: https://www.cnblogs.com/mqxs/p/17280954.html

相关文章

  • Uniapp开发的微商个人相册多端小程序源码
    ☑️品牌:Uniapp☑️语言:vue☑️类型:微商个人相册☑️支持:小程序......
  • 【开源项目】宁波~超经典智慧城市CIM/BIM数字孪生可视化项目——开源工程及源码!
    最新消息,数字孪生智慧宁波开源了其数据工程源码和工程,免费送出供大家学习、使用、分享。智慧宁波实现了一系列全面的功能,如实现长三角经济圈特效、智慧地铁特效、智慧灯杆特性等。这些项目利用数字孪生技术和物联网等先进技术,可以实现精细化管理和智能控制,提高城市管理效率和品质......
  • 智慧停车场微信小程序源码/全开源智能停车系统源码
    ☑️品牌:小程序☑️语言:小程序☑️类型:智慧停车场系统☑️支持:小程序......
  • 基于uniapp开发的zblog多端小程序源码
    ☑️品牌:uniapp☑️语言:vue☑️类型:文章资讯博客☑️支持:小程序......
  • ETCD源码阅读(四)
    DAY3:ETCD分布式锁:etcd/contrib/lock这一部分代码主要是为了展示ETCD实现分布式锁的原理(Lease),并且贴出了DDIA作者的一篇博文作为应用场景建模。那么我们就先来读这篇博文吧。为什么要使用分布式锁防止数据竞争:多个分布式下节点可能会同时修改同一份数据,如果不加锁,会导致数......
  • rocketmq-spring : 实战与源码解析一网打尽
    RocketMQ是大家耳熟能详的消息队列,开源项目rocketmq-spring可以帮助开发者在SpringBoot项目中快速整合RocketMQ。这篇文章会介绍SpringBoot项目使用rocketmq-springSDK实现消息收发的操作流程,同时笔者会从开发者的角度解读SDK的设计逻辑。1SDK简介项目地址:......
  • rocketmq-spring : 实战与源码解析一网打尽
    RocketMQ是大家耳熟能详的消息队列,开源项目rocketmq-spring可以帮助开发者在SpringBoot项目中快速整合RocketMQ。这篇文章会介绍SpringBoot项目使用rocketmq-springSDK实现消息收发的操作流程,同时笔者会从开发者的角度解读SDK的设计逻辑。1SDK简介项目地址:......
  • clang操作源码
    生成注释假设有下面的源码:structVec3{floatx,y,z;};structVec4{floatx,y,z,w;};生成这样的代码://[[CLASSINFO]]class:Vec3,ispod:true,isaggregate:truestructVec3{floatx,y,z;};//[[CLASSINFO]]usestruct-bindingmethod://c......
  • Linux下编译Sqlite源码
    1.下载wgethttps://www.sqlite.org/2023/sqlite-autoconf-3410200.tar.gz--no-check-certificate2.解压tarzxvfsqlite-autoconf-3410200.tar.gz 3.配置路径cdsqlite-autoconf-3410200/./configure--prefix=/data/sqlite#先建立该路径 4.编译make&&ma......
  • 使用 IntelliJ IDEA 构建 Spring Framework 5.3.21 源码问题解决
    源码版本1、下载地址:https://github.com/spring-projects/spring-framework/tags2、选择要构建的源码版本并下载,例如:5.3.21相关环境1、操作系统:Windows102、JDK版本:Jdk173、IDE工具:IntelliJIDEA2021.3.34、项目构建工具:Gradle7.3.3使用IntelliJIDEA构建Spring......