利用RevitNET.dll后台操作rvt文件

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.RevitAddIns;

namespace ProceeRvtFile
{
    class Program
    {
        static readonly string[] Searchs =
            RevitProductUtility.GetAllInstalledRevitProducts()
            .Select(x => x.InstallLocation).ToArray();

        static Program()
        {
            AddEnvironmentPaths(Searchs);

            AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
        }

        [STAThread]
        static void Main(string[] args)
        {
            Product _product = Product.GetInstalledProduct();

            var clientId = new ClientApplicationId(Guid.NewGuid(), "LK", "BIMAPI");

            //"I am authorized by Autodesk to use this UI-less functionality."只能为该字符串
            _product.Init(clientId, "I am authorized by Autodesk to use this UI-less functionality.");

            var Application = _product.Application;

            Document doc = Application.OpenDocumentFile("D:/1.rvt");
            Console.WriteLine("RVT文件已经打开");
            FilteredElementCollector wallCollector = new FilteredElementCollector(doc);
            var walls = wallCollector.OfClass(typeof(Wall)).ToArray();
            foreach(Wall wall in walls)
            {
                Console.WriteLine("Wall ElementId:{0}", wall.Id.IntegerValue);
            }

            doc.Close();
            _product?.Exit();


            Console.ReadKey(true);
        }

        static void AddEnvironmentPaths(params string[] paths)
        {
            var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };
            var newPath = string.Join(System.IO.Path.PathSeparator.ToString(), path.Concat(paths));
            //设置当前进程中的环境变量
            Environment.SetEnvironmentVariable("PATH", newPath);
        }

        private static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
        {
            var assemblyName = new AssemblyName(args.Name);

            //在安装路径中查找相关dll并加载
            foreach(var item in Searchs)
            {
                var file = string.Format("{0}.dll", System.IO.Path.Combine(item, assemblyName.Name));
                if (File.Exists(file))
                {
                    return Assembly.LoadFile(file);
                }
            }

            return args.RequestingAssembly;
        }
    }
}

需要引入RevitNET.dll,RevitAddInUtility.dll和RevitAPI.dll三个dll到项目中

有几个问题没有明白:

(1)为什么要设置当前进程的环境变量,如果不设置会提示找不到RevitNET.dll

(2)既然设置了环境变量,那么OnAssemblyResolve的意义何在

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值