在Revit二次开发中,如果只是简单的从模型中提取数据或不需要界面对Revit进行修改,我们一般使用RevitNet。
我们接下来对使用过程中出现的问题做一下总结:
1,异常:SEHException: 外部组件发生异常。
解决方法:Revit 内核必须加 STAThread 标签
RevitCoreContext.Instance.Run();
2,在Windows应用程序提示无法加载RevitNET.dll
可能的原因是:目标平台为Any CPU,首先32位。
应该取消首先32位的对勾,或者更改为X64
目标框架 注意与引用的项目一致(主要针对其它dll)
3,如何跨线程访问吗?
将RevitNet封装为一个控制台应用程序(Revit启动程序),然后通过命令行去启动Revit启动程序。
在启动过程可以传入参数。
Process process=new Process();//AppDomain.CurrentDomain.BaseDirectory +
process.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory ,"ExportStart.exe");//执行的exe路径
process.StartInfo.UseShellExecute = false;//不显示shell
process.StartInfo.CreateNoWindow = true;//不创建窗口
process.StartInfo.RedirectStandardInput = true;//打开流输入
process.StartInfo.RedirectStandardOutput = true;//打开流输出
process.StartInfo.RedirectStandardError = true;//打开错误流
process.StartInfo.Arguments = "" + revitCmd + " \"" + filePathList.First() + "\"";//输入参数,多个参数使用空间分割,如果一个参数包含空格,使用""包括此参数
process.Start();//执行
string msg = process.StandardOutput.ReadToEnd();//读取输出
process.WaitForExit();//等待执行完成
process.Close();//结束
4,未能加载由“RevitNET.dll”导入的过程?
解决办法:查看环境变量,将RevitInstallPath的path放至第一位.
其它涉及到操作多个版本Revit 的问题(这里说的是Revit文件的版本可能是2018,2019):
不启动获取Revit文件的版本
/// 获取Revit文件的版本
public static string GetRevitVision(string path)
{
string revitVision = null;
FileStream stream = new FileStream(path, FileMode.Open);
int size = 1024 * 1024;
byte[] bytes = new byte[size];
while (stream.Read(bytes, 0, size) > 0)
{
string str = Encoding.Unicode.GetString(bytes);
string pattern = @"Autodesk Revit \d{4}";
var match = Regex.Match(str, pattern);
if (match.Success)
{
revitVision = match.Value.Substring(match.Length - 4, 4);
//File.WriteAllText(@"D:\abc.txt", str);
break;
}
}
return revitVision;
}
1,程序运行目录(OutputDll)能否删除RevitAPI.dll或RevitAPIUI.dll等相关dll?
可以,设置环境变量后,Revit会去Revit安装目录下查找以上dll。
注意:如果如果程序运行目录(OutputDll)存在Revit相关的dll,版本必须正确,否则会报错。(如果不能保证版本正确,不如将Revit相关dll全部删除)
2,RevitNet启动程序,能否取消RevitAddInUtility.dll的引用?
可以取消,RevitAddInUtility.dll 此dll的作用是用于获取Revit的安装目录,可以手动指定Revit的安装目录。从而取消此dll的引用。(完全可以不安装Revit,只复制Revit需要的dll,但区别那些dll是Revit需要的会很麻烦。不过可以将Revit安装目录下的文件全部复制到指定位置)
转载链接:https://www.cnblogs.com/garrett/p/11277120.html
(二、)
在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);
a)创建一个新项目
var projectrTemplate = Application.DefaultProjectTemplate;
if (!File.Exists(projectrTemplate))
{
throw new FileNotFoundException("默认项目路径不存在 , 请指定 !");
}
var document = Application.NewProjectDocument(projectrTemplate);
b)打开一个老项目
var Application = _product.Application;
Document doc= Application.OpenDocumentFile("url");
var view = doc.ActiveView;