Revit二次开发之十五 RevitNet.dll 的使用

不羁岁月 提交于 2019-12-05 14:26:30

在revit开发中,经常面临如下问题

1、不开启revit创建项目;

2、不开启revit读取rvt的数据信息等

RevitNet.dll是Autodesk 用于开启一个revit操作的基本DLL 、无需启动界面并对 Revit 进行操作。也就是使用此dll可做到无需启动Revit便可进行创建、修改、读取等等。其基本API定义如下:

主要是获取Product对象,其代码如下:

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

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

            AddEnvironmentPaths(Searchs);

            //对程序集解析失败的时候发生
            AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
        }

        /// <summary>
        /// 当前的主函数
        /// </summary>
        /// <param name="args"></param>
        [STAThread]
        static void Main(string[] args)
        {
     
            Product _product = Product.GetInstalledProduct();

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

            // I am authorized by Autodesk to use this UI-less functionality. 必须是此字符串。 Autodesk 规定的.

            _product.Init(clientId, "I am authorized by Autodesk to use this UI-less functionality.");

            var Application = _product.Application;

        }

        /// <summary>
        /// 向系统中添加环境变量
        /// </summary>
        /// <param name="paths"></param>
        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);
        }

        /// <summary>
        /// 程序加载失败
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
        {
            var assemblyName = new AssemblyName(args.Name);

            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;
        }
    }
}

 注意事项:

1、Main函数必须标识为      [STAThread]

2、应用程序启动前,必须指定环境路径

 

主要类的函数说明:

Product:

        public APISettings Settings { get; }
        public Application Application { get; }   //当前的应用程序服务Application对象
        public static ICriticalSituationProcessor GetCriticalSituationProcessor();
        public static Product GetInstalledProduct();
        public static void RegisterCriticalSituationProcessor(ICriticalSituationProcessor processor);
        public sealed override void Dispose();
        public void EnableIFC(bool enable);
        public void Exit();
        public void Init(DB.ClientApplicationId id, string clientData);
        public void SetPreferredLanguage(ApplicationServices.LanguageType language);
        public void SetSettingsFileLocation(string strSettingsFileLocation);
 
创建一个新项目
         var projectrTemplate = Application.DefaultProjectTemplate;


            if (!File.Exists(projectrTemplate))
            {
                throw new FileNotFoundException("默认项目路径不存在 , 请指定 !");
            }

            var document = Application.NewProjectDocument(projectrTemplate);

打开一个老项目

      var Application = _product.Application;

           Document doc=  Application.OpenDocumentFile("url");

           var view = doc.ActiveView;
      

 

 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!