资讯详情

Revit二次开发之隐藏API 独立进程读取rvt文件

需要在项目中读取rvt但由于该格式为非公开格式,因此需要数据revit但是批量阅读是不可能一个一个用的revit打开软件。但是,这种方法仍然需要。revit依赖速度比软件快多少?

1.1. 新建控制台项目

a420d4a7a0e5e02ba3e3721f2a9622a4.png

1.2. 添加Revit API引用

我们找到revit安装目录下的这两个DLL添加到项目引用中

RevitNET.dll

RevitAPI.dll

修改属性:复制本地:FALSE

1.3. 为MAIN函数添加STAThread特性

[STAThread]///必须有

static void Main(string[] args)

{

}

若未添加,则报错:

异常:SEHException: 外部部件异常。

1.4. 修改控制台项目为64位

由于revit对于64位程序,我们的控制台程序必须是64位才能正常使用。

1.5. 添加封装好的revitContext类

可以直接使用,可以根据自己需要去修改,其中的Application就是revit主要的

public class RevitContext

{

#region private fields

Product _revitProduct;

private static bool isLoadEnv = false;//是否添加了环境变量

#endregion

#region public fields

///

/// revit程序目录

///

public static string RevitPath;

#endregion

#region event

public event EventHandler InitRevitFinished;

#endregion

#region public properties

///

/// 打开REVIT设置文件

///

public OpenOptions OpenOptions { get; set; }

///

/// Revit Application

///

public Autodesk.Revit.ApplicationServices.Application Application => this._revitProduct?.Application;

#endregion

#region constructors

///

///

///

/// revit安装目录

public RevitContext(string revitPath)

{

RevitPath = revitPath;

AddEnv();

}

///

/// 在使用这种结构方法之前需要调用 RevitContext.AddEnv();

///

public RevitContext()

{

}

#endregion

#region public methods

public void InitRevit()

{

this.OpenOptions = new OpenOptions

{

Audit = true,

AllowOpeningLocalByWrongUser = false,

DetachFromCentralOption = DetachFromCentralOption.DetachAndDiscardWorksets //分离中心模型

};

_revitProduct = Product.GetInstalledProduct();

var clientApplicationId = new ClientApplicationId(Guid.NewGuid(), "RevitContext", "BIM");

_revitProduct.SetPreferredLanguage(Autodesk.Revit.ApplicationServices.LanguageType.Chinese_Simplified);

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

OnInitRevitFinished();

}

public Document OpenFile(string filename, OpenOptions options = null)

{

if (options == null)

{

options = this.OpenOptions;

}

ModelPath model = new FilePath(filename);

return this._revitProduct.Application.OpenDocumentFile(model, options);

}

///

/// 获取默认三维视图

///

/// 文档

///

public View3D GetView3D(Document document)

{

if (document.ActiveView is View3D view3D && !view3D.IsPerspective && view3D.CanBePrinted)

{

return view3D;

}

FilteredElementCollector filter=new FilteredElementCollector(document);

return (View3D) filter.OfClass(typeof(View3D)).FirstElement();

}

///

/// 获取指定的三维视图

///

/// 文档

/// 指定视图名称

///

public View3D GetView3D(Document document,string viewName)

{

FilteredElementCollector filter = new FilteredElementCollector(document);

return (View3D)filter.OfClass(typeof(View3D)).FirstOrDefault(x => x.Name==viewName);

}

public IList GetElementsWithView(View3D view)

{

FilteredElementCollector collector=new FilteredElementCollector(view.Document,view.Id);

return collector.ToElements();

}

#endregion

#region public static methods

///

/// 添加revit为了加载相应的安装路径到环境变量DLL

///

/// 添加revit安装路径

public static void AddEnv(string revitPath=null)

{

if (isLoadEnv)

{

return;

}

if (revitPath!=null)

{

RevitPath = revitPath;

}

AddEnvironmentPaths(RevitPath);

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

}

#endregion

#region private static methods

///

/// 添加环境变量

///

/// revit安装路径

static void AddEnvironmentPaths(params string[] paths)

{

string[] first = {

Environment.GetEnvironmentVariable("PATH") ?? string.Empty

};

string value = string.Join(Path.PathSeparator.ToString(), first.Concat(paths));

Environment.SetEnvironmentVariable("PATH", value);

}

///

/// 动态加载revit相关的dll

///

///

///

///

static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)

{

var assemblyName = new AssemblyName(args.Name);

var text = $"{Path.Combine(RevitPath, assemblyName.Name)}.dll";

Assembly result;

if (File.Exists(text))

{

Console.WriteLine($"Load Revit Dll Path:{text}");

result = Assembly.LoadFrom(text);

}

else

{

result = null;

}

return result;

}

#endregion

#region private methods

private void OnInitRevitFinished()

{

this.InitRevitFinished?.Invoke(this, this._revitProduct);

}

#endregion

}

1.6. 使用并测试

class Program

{

static RevitContext revit;

static Program()

{

RevitContext.AddEnv(@"D:\Program Files\Autodesk\Navisworks Manage 2020\Loaders\Rx\");

}

[STAThread]//一定要有

static void Main(string[] args)

{

revit = new RevitContext();

revit.InitRevitFinished += InitRevitFinished;

revit.InitRevit();

Console.ReadKey();

}

private static void InitRevitFinished(object sender, Product revitProduct)

{

Console.WriteLine("当前使用Revit版本为:" + revitProduct.Application.VersionName);

Document document = revit.OpenFile(@"E:\test\2019\经典小文件\2020.rvt");

View3D view = revit.GetView3D(document);

if (view!=null)

{

Console.WriteLine(view.Name);

var elements =revit.GetElementsWithView(view);

foreach (var element in elements)

{

Console.WriteLine(element.Name);

}

}

}

}

完成!

1.7. 留下的坑:

如何在不指定revit路径的情况下加载(动态获取revit安装路径)

根据revit文件版本加载相应的revit路径

一个程序动态支持多版本revit

以上问题本人已有相应的方法。

原文出处:https://www.cnblogs.com/tchivs/p/11925856.html

标签: 国产smd铝电解电容rvt

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台